CMS 3D CMS Logo

PoolDBOutputService.cc
Go to the documentation of this file.
10 //
11 #include <vector>
12 #include <memory>
13 #include <cassert>
14 
15 //In order to make PoolDBOutputService::currentTime() to work we have to keep track
16 // of which stream is presently being processed on a given thread during the call of
17 // a module which calls that method.
18 static thread_local int s_streamIndex = -1;
19 
21  Record thisrecord;
22 
23  thisrecord.m_idName = recordPset.getParameter<std::string>("record");
24  thisrecord.m_tag = recordPset.getParameter<std::string>("tag");
25 
26  thisrecord.m_timetype =
27  cond::time::timeTypeFromName(recordPset.getUntrackedParameter<std::string>("timetype", gTimeTypeStr));
28 
29  thisrecord.m_onlyAppendUpdatePolicy = recordPset.getUntrackedParameter<bool>("onlyAppendUpdatePolicy", false);
30 
31  m_records.insert(std::make_pair(thisrecord.m_idName, thisrecord));
32 
33  cond::UserLogInfo userloginfo;
34  m_logheaders.insert(std::make_pair(thisrecord.m_idName, userloginfo));
35 }
36 
38  : m_logger(iConfig.getUntrackedParameter<std::string>("jobName", "DBOutputService")),
39  m_currentTimes{},
40  m_session(),
41  m_transactionActive(false),
42  m_dbInitialised(false),
43  m_records(),
44  m_logheaders() {
45  std::string timetypestr = iConfig.getUntrackedParameter<std::string>("timetype", "runnumber");
46  m_timetype = cond::time::timeTypeFromName(timetypestr);
47  m_autoCommit = iConfig.getUntrackedParameter<bool>("autoCommit", false);
48  m_writeTransactionDelay = iConfig.getUntrackedParameter<unsigned int>("writeTransactionDelay", 0);
49 
50  edm::ParameterSet connectionPset = iConfig.getParameter<edm::ParameterSet>("DBParameters");
51  m_connection.setParameters(connectionPset);
52  m_connection.setLogDestination(m_logger);
53  m_connection.configure();
54  std::string connectionString = iConfig.getParameter<std::string>("connect");
55  m_session = m_connection.createSession(connectionString, true);
56  bool saveLogsOnDb = iConfig.getUntrackedParameter<bool>("saveLogsOnDB", false);
57  if (saveLogsOnDb)
58  m_logger.setDbDestination(connectionString, m_connection);
59  // implicit start
60  doStartTransaction();
61 
62  typedef std::vector<edm::ParameterSet> Parameters;
63  Parameters toPut = iConfig.getParameter<Parameters>("toPut");
64  for (Parameters::iterator itToPut = toPut.begin(); itToPut != toPut.end(); ++itToPut)
65  fillRecord(*itToPut, timetypestr);
66 
67  iAR.watchPostEndJob(this, &cond::service::PoolDBOutputService::postEndJob);
68  iAR.watchPreallocate(
69  [this](edm::service::SystemBounds const& iBounds) { m_currentTimes.resize(iBounds.maxNumberOfStreams()); });
70  if (m_timetype == cond::timestamp) { //timestamp
72  iAR.watchPreModuleEvent(this, &cond::service::PoolDBOutputService::preModuleEvent);
73  iAR.watchPostModuleEvent(this, &cond::service::PoolDBOutputService::postModuleEvent);
74  } else if (m_timetype == cond::runnumber) { //runnumber
75  //NOTE: this assumes only one run is being processed at a time.
76  // This is true for 7_1_X but plan are to allow multiple in flight at a time
77  s_streamIndex = 0;
78  iAR.watchPreGlobalBeginRun(this, &cond::service::PoolDBOutputService::preGlobalBeginRun);
79  } else if (m_timetype == cond::lumiid) {
80  //NOTE: this assumes only one lumi is being processed at a time.
81  // This is true for 7_1_X but plan are to allow multiple in flight at a time
82  s_streamIndex = 0;
83  iAR.watchPreGlobalBeginLumi(this, &cond::service::PoolDBOutputService::preGlobalBeginLumi);
84  }
85 }
86 
88  const std::string& transactionId) {
90  ret = m_connection.createReadOnlySession(connectionString, transactionId);
91  return ret;
92 }
93 
95 
97  return this->lookUpRecord(recordName).m_tag;
98 }
99 
101  Record& myrecord = this->lookUpRecord(recordName);
102  return myrecord.m_isNewTag;
103 }
104 
106  if (!m_transactionActive) {
107  m_session.transaction().start(false);
108  m_transactionActive = true;
109  }
110 }
111 
113  if (m_transactionActive) {
114  m_session.transaction().commit();
115  m_transactionActive = false;
116  }
117 }
118 
120  std::lock_guard<std::recursive_mutex> lock(m_mutex);
121  doStartTransaction();
122 }
123 
125  std::lock_guard<std::recursive_mutex> lock(m_mutex);
126  doCommitTransaction();
127 }
128 
130  if (!m_dbInitialised) {
131  if (!m_session.existsDatabase())
132  m_session.createDatabase();
133  else {
134  for (auto& iR : m_records) {
135  if (m_session.existsIov(iR.second.m_tag))
136  iR.second.m_isNewTag = false;
137  }
138  }
139  m_dbInitialised = true;
140  }
141 }
142 
143 void cond::service::PoolDBOutputService::postEndJob() { commitTransaction(); }
144 
146  m_currentTimes[iContext.streamID().value()] = iContext.timestamp().value();
147 }
148 
150  edm::ModuleCallingContext const&) {
151  s_streamIndex = iContext.streamID().value();
152 }
153 
155  edm::ModuleCallingContext const&) {
156  s_streamIndex = -1;
157 }
158 
160  for (auto& time : m_currentTimes) {
161  time = iContext.luminosityBlockID().run();
162  }
163 }
164 
166  for (auto& time : m_currentTimes) {
167  time = iContext.luminosityBlockID().value();
168  }
169 }
170 
172 
174  std::lock_guard<std::recursive_mutex> lock(m_mutex);
175  doStartTransaction();
176  cond::persistency::TransactionScope scope(m_session.transaction());
177  try {
178  initDB();
179  } catch (const std::exception& er) {
180  cond::throwException(std::string(er.what()), "PoolDBOutputService::forceInit");
181  }
182  scope.close();
183 }
184 
186 
188 
190  assert(-1 != s_streamIndex);
191  return m_currentTimes[s_streamIndex];
192 }
193 
195  cond::Time_t firstSinceTime,
196  const std::string& recordName) {
197  std::lock_guard<std::recursive_mutex> lock(m_mutex);
198  Record& myrecord = this->lookUpRecord(recordName);
199  if (!myrecord.m_isNewTag) {
200  cond::throwException(myrecord.m_tag + " is not a new tag", "PoolDBOutputService::createNewIOV");
201  }
202  m_logger.logInfo() << "Creating new tag " << myrecord.m_tag << ", adding iov with since " << firstSinceTime
203  << " pointing to payload id " << firstPayloadId;
204  doStartTransaction();
205  cond::persistency::TransactionScope scope(m_session.transaction());
206  try {
207  this->initDB();
209  m_session.createIovForPayload(firstPayloadId, myrecord.m_tag, myrecord.m_timetype, cond::SYNCH_ANY);
210  editor.setDescription("New Tag");
211  editor.insert(firstSinceTime, firstPayloadId);
212  cond::UserLogInfo a = this->lookUpUserLogInfo(myrecord.m_idName);
213  editor.flush(a.usertext);
214  myrecord.m_isNewTag = false;
215  } catch (const std::exception& er) {
216  cond::throwException(std::string(er.what()), "PoolDBOutputService::createNewIov");
217  }
218  scope.close();
219 }
220 
222  const std::string payloadType,
223  cond::Time_t firstSinceTime,
224  Record& myrecord) {
225  m_logger.logInfo() << "Creating new tag " << myrecord.m_tag << " for payload type " << payloadType
226  << ", adding iov with since " << firstSinceTime;
227  // FIX ME: synchronization type and description have to be passed as the other parameters?
229  m_session.createIov(payloadType, myrecord.m_tag, myrecord.m_timetype, cond::SYNCH_ANY);
230  editor.setDescription("New Tag");
231  editor.insert(firstSinceTime, firstPayloadId);
232  cond::UserLogInfo a = this->lookUpUserLogInfo(myrecord.m_idName);
233  editor.flush(a.usertext);
234  myrecord.m_isNewTag = false;
235 }
236 
239  const std::string& recordName) {
240  std::lock_guard<std::recursive_mutex> lock(m_mutex);
241  Record& myrecord = this->lookUpRecord(recordName);
242  if (myrecord.m_isNewTag) {
243  cond::throwException(std::string("Cannot append to non-existing tag ") + myrecord.m_tag,
244  "PoolDBOutputService::appendSinceTime");
245  }
246  bool ret = false;
247  doStartTransaction();
248  cond::persistency::TransactionScope scope(m_session.transaction());
249  try {
250  ret = appendSinceTime(payloadId, time, myrecord);
251  } catch (const std::exception& er) {
252  cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
253  }
254  scope.close();
255  return ret;
256 }
257 
260  Record& myrecord) {
261  m_logger.logInfo() << "Updating existing tag " << myrecord.m_tag << ", adding iov with since " << time;
263  try {
264  cond::persistency::IOVEditor editor = m_session.editIov(myrecord.m_tag);
265  payloadType = editor.payloadType();
266  editor.insert(time, payloadId);
267  cond::UserLogInfo a = this->lookUpUserLogInfo(myrecord.m_idName);
268  editor.flush(a.usertext);
269  } catch (const std::exception& er) {
270  cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
271  }
272  return true;
273 }
274 
276  cond::Time_t sinceTime,
277  const std::string& recordName) {
278  std::lock_guard<std::recursive_mutex> lock(m_mutex);
279  Record& myrecord = this->lookUpRecord(recordName);
280  if (myrecord.m_isNewTag) {
281  cond::throwException(std::string("Cannot delete from non-existing tag ") + myrecord.m_tag,
282  "PoolDBOutputService::appendSinceTime");
283  }
284  m_logger.logInfo() << "Updating existing tag " << myrecord.m_tag << ", removing iov with since " << sinceTime
285  << " pointing to payload id " << payloadId;
286  doStartTransaction();
287  cond::persistency::TransactionScope scope(m_session.transaction());
288  try {
289  cond::persistency::IOVEditor editor = m_session.editIov(myrecord.m_tag);
290  editor.erase(sinceTime, payloadId);
291  cond::UserLogInfo a = this->lookUpUserLogInfo(recordName);
292  editor.flush(a.usertext);
293 
294  } catch (const std::exception& er) {
295  cond::throwException(std::string(er.what()), "PoolDBOutputService::eraseSinceTime");
296  }
297  scope.close();
298 }
299 
301  const std::string& recordName) {
302  std::map<std::string, Record>::iterator it = m_records.find(recordName);
303  if (it == m_records.end()) {
304  cond::throwException("The record \"" + recordName + "\" has not been registered.",
305  "PoolDBOutputService::lookUpRecord");
306  }
307  return it->second;
308 }
309 
311  std::map<std::string, cond::UserLogInfo>::iterator it = m_logheaders.find(recordName);
312  if (it == m_logheaders.end())
313  throw cond::Exception("Log db was not set for record " + recordName +
314  " from PoolDBOutputService::lookUpUserLogInfo");
315  return it->second;
316 }
317 
319  std::lock_guard<std::recursive_mutex> lock(m_mutex);
320  Record& myrecord = lookUpRecord(recordName);
321  if (myrecord.m_isNewTag) {
322  cond::throwException(std::string("Cannot close non-existing tag ") + myrecord.m_tag,
323  "PoolDBOutputService::closeIOV");
324  }
325  m_logger.logInfo() << "Updating existing tag " << myrecord.m_tag << ", closing with end of validity " << lastTill;
326  doStartTransaction();
327  cond::persistency::TransactionScope scope(m_session.transaction());
328  try {
329  cond::persistency::IOVEditor editor = m_session.editIov(myrecord.m_tag);
330  editor.setEndOfValidity(lastTill);
331  editor.flush("Tag closed.");
332  } catch (const std::exception& er) {
333  cond::throwException(std::string(er.what()), "PoolDBOutputService::closeIOV");
334  }
335  scope.close();
336 }
337 
339  const std::string& dataprovenance,
340  const std::string& usertext) {
341  cond::UserLogInfo& myloginfo = this->lookUpUserLogInfo(recordName);
342  myloginfo.provenance = dataprovenance;
343  myloginfo.usertext = usertext;
344 }
345 
346 // Still required.
348  Record& record = lookUpRecord(recordName);
349  result.name = record.m_tag;
350  m_logger.logDebug() << "Fetching tag info for " << record.m_tag;
351  doStartTransaction();
352  bool ret = false;
353  cond::persistency::TransactionScope scope(m_session.transaction());
354  try {
355  //use iovproxy to find out.
356  if (m_session.existsIov(record.m_tag)) {
357  cond::persistency::IOVProxy iov = m_session.readIov(record.m_tag);
358  result.lastInterval = iov.getLast();
359  ret = true;
360  }
361  } catch (const std::exception& er) {
362  cond::throwException(std::string(er.what()), "PoolDBOutputService::tagInfo");
363  }
364  scope.close();
365  return ret;
366 }
367 
368 // Still required.
370  std::lock_guard<std::recursive_mutex> lock(m_mutex);
371  return getTagInfo(recordName, result);
372 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:355
edm::GlobalContext::luminosityBlockID
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:55
cond::persistency::TransactionScope
Definition: Session.h:231
cond::service::PoolDBOutputService::Record::m_isNewTag
bool m_isNewTag
Definition: PoolDBOutputService.h:198
cond::service::PoolDBOutputService::lookUpRecord
Record & lookUpRecord(const std::string &recordName)
Definition: PoolDBOutputService.cc:300
cond::service::PoolDBOutputService::tag
std::string tag(const std::string &recordName)
Definition: PoolDBOutputService.cc:96
cond::persistency::IOVEditor::insert
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:139
cond::service::PoolDBOutputService::Record
Definition: PoolDBOutputService.h:192
cond::lumiid
Definition: Time.h:19
cond::service::PoolDBOutputService::initDB
void initDB()
Definition: PoolDBOutputService.cc:129
cond::TimeTypeSpecs::beginValue
Time_t beginValue
Definition: Time.h:41
cond::service::PoolDBOutputService::m_logheaders
std::map< std::string, cond::UserLogInfo > m_logheaders
Definition: PoolDBOutputService.h:251
cond::persistency::IOVProxy::getLast
cond::Iov_t getLast()
Definition: IOVProxy.cc:386
cond::service::PoolDBOutputService::beginOfTime
cond::Time_t beginOfTime() const
Definition: PoolDBOutputService.cc:187
Exception.h
s_streamIndex
static thread_local int s_streamIndex
Definition: PoolDBOutputService.cc:18
cond::service::PoolDBOutputService::eraseSinceTime
void eraseSinceTime(const std::string &payloadId, cond::Time_t sinceTime, const std::string &recordName)
Definition: PoolDBOutputService.cc:275
Exception.h
cond::service::PoolDBOutputService::Record::m_timetype
cond::TimeType m_timetype
Definition: PoolDBOutputService.h:200
align_cfg.recordName
recordName
Definition: align_cfg.py:66
cond::service::PoolDBOutputService::preModuleEvent
void preModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
Definition: PoolDBOutputService.cc:149
cond::service::PoolDBOutputService::tagInfo
bool tagInfo(const std::string &recordName, cond::TagInfo_t &result)
Definition: PoolDBOutputService.cc:369
cond::persistency::IOVEditor
Definition: IOVEditor.h:28
cond::service::PoolDBOutputService::preGlobalBeginLumi
void preGlobalBeginLumi(edm::GlobalContext const &)
Definition: PoolDBOutputService.cc:165
GlobalPosition_Frontier_DevDB_cff.record
record
Definition: GlobalPosition_Frontier_DevDB_cff.py:10
cond::service::PoolDBOutputService::appendSinceTime
void appendSinceTime(const T *payloadObj, cond::Time_t sinceTime, const std::string &recordName)
Definition: PoolDBOutputService.h:135
cond::service::PoolDBOutputService::closeIOV
void closeIOV(Time_t lastTill, const std::string &recordName)
Definition: PoolDBOutputService.cc:318
cms::cuda::assert
assert(be >=bs)
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::Timestamp::value
TimeValue_t value() const
Definition: Timestamp.h:45
edm::StreamID::value
unsigned int value() const
Definition: StreamID.h:42
cond::service::PoolDBOutputService::m_records
std::map< std::string, Record > m_records
Definition: PoolDBOutputService.h:250
cond::service::PoolDBOutputService::Record::m_idName
std::string m_idName
Definition: PoolDBOutputService.h:199
cond::timestamp
Definition: Time.h:19
PoolDBOutputService.h
cond::service::PoolDBOutputService::lookUpUserLogInfo
cond::UserLogInfo & lookUpUserLogInfo(const std::string &recordName)
Definition: PoolDBOutputService.cc:310
cond::timeTypeSpecs
const TimeTypeSpecs timeTypeSpecs[]
Definition: Time.cc:16
edm::StreamContext
Definition: StreamContext.h:31
cond::service::PoolDBOutputService::createNewIOV
void createNewIOV(const T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t, const std::string &recordName)
Definition: PoolDBOutputService.h:110
cond::service::PoolDBOutputService::session
cond::persistency::Session session() const
Definition: PoolDBOutputService.cc:94
cond::service::PoolDBOutputService::preEventProcessing
void preEventProcessing(edm::StreamContext const &)
Definition: PoolDBOutputService.cc:145
EventID.h
cond::service::PoolDBOutputService::Record::m_onlyAppendUpdatePolicy
bool m_onlyAppendUpdatePolicy
Definition: PoolDBOutputService.h:201
edm::ActivityRegistry
Definition: ActivityRegistry.h:132
cond::service::PoolDBOutputService::commitTransaction
void commitTransaction()
Definition: PoolDBOutputService.cc:124
cond::service::PoolDBOutputService::postEndJob
void postEndJob()
Definition: PoolDBOutputService.cc:143
cond::service::PoolDBOutputService::doStartTransaction
void doStartTransaction()
Definition: PoolDBOutputService.cc:105
cond::service::PoolDBOutputService::postModuleEvent
void postModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
Definition: PoolDBOutputService.cc:154
cond::service::PoolDBOutputService::fillRecord
void fillRecord(edm::ParameterSet &pset, const std::string &gTimeTypeStr)
Definition: PoolDBOutputService.cc:20
cond::persistency::IOVEditor::setDescription
void setDescription(const std::string &description)
Definition: IOVEditor.cc:114
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cond::persistency::IOVProxy
Definition: IOVProxy.h:92
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
edm::GlobalContext
Definition: GlobalContext.h:29
edm::service::SystemBounds
Definition: SystemBounds.h:29
cond::service::PoolDBOutputService::doCommitTransaction
void doCommitTransaction()
Definition: PoolDBOutputService.cc:112
edm::ParameterSet
Definition: ParameterSet.h:36
a
double a
Definition: hdecay.h:119
GlobalContext.h
Timestamp.h
cond::UserLogInfo::usertext
std::string usertext
Definition: Types.h:24
cond::SYNCH_ANY
Definition: Types.h:28
CommonMethods.lock
def lock()
Definition: CommonMethods.py:82
edm::StreamContext::streamID
StreamID const & streamID() const
Definition: StreamContext.h:54
cond::runnumber
Definition: Time.h:19
cond::persistency::Session
Definition: Session.h:63
cond::Time_t
unsigned long long Time_t
Definition: Time.h:14
cond::service::PoolDBOutputService::getTagInfo
bool getTagInfo(const std::string &recordName, cond::TagInfo_t &result)
Definition: PoolDBOutputService.cc:347
cond::persistency::Exception
Base exception class for the object to relational access.
Definition: Exception.h:11
cond::TimeTypeSpecs::endValue
Time_t endValue
Definition: Time.h:42
edm::service::SystemBounds::maxNumberOfStreams
unsigned int maxNumberOfStreams() const
Definition: SystemBounds.h:35
HLT_2018_cff.payloadType
payloadType
Definition: HLT_2018_cff.py:8133
DBConfiguration_cff.toPut
toPut
Definition: DBConfiguration_cff.py:25
edm::LuminosityBlockID::value
uint64_t value() const
Definition: LuminosityBlockID.cc:13
cond::persistency::IOVEditor::payloadType
std::string payloadType() const
Definition: IOVEditor.cc:88
cond::service::PoolDBOutputService::endOfTime
cond::Time_t endOfTime() const
Definition: PoolDBOutputService.cc:185
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
std
Definition: JetResolutionObject.h:76
cond::service::PoolDBOutputService::setLogHeaderForRecord
void setLogHeaderForRecord(const std::string &recordName, const std::string &provenance, const std::string &usertext)
Definition: PoolDBOutputService.cc:338
cond::service::PoolDBOutputService::Record::m_tag
std::string m_tag
Definition: PoolDBOutputService.h:197
cond::persistency::IOVEditor::setEndOfValidity
void setEndOfValidity(cond::Time_t validity)
Definition: IOVEditor.cc:104
cond::UserLogInfo
Definition: Types.h:22
cond::service::PoolDBOutputService::isNewTagRequest
bool isNewTagRequest(const std::string &recordName)
Definition: PoolDBOutputService.cc:100
cond::time::timeTypeFromName
TimeType timeTypeFromName(const std::string &name)
Definition: Time.cc:25
cond::service::PoolDBOutputService::~PoolDBOutputService
virtual ~PoolDBOutputService()
Definition: PoolDBOutputService.cc:171
cond::service::PoolDBOutputService::newReadOnlySession
cond::persistency::Session newReadOnlySession(const std::string &connectionString, const std::string &transactionId)
Definition: PoolDBOutputService.cc:87
edm::LuminosityBlockID::run
RunNumber_t run() const
Definition: LuminosityBlockID.h:41
cond::persistency::IOVEditor::erase
void erase(cond::Time_t since, const cond::Hash &payloadHash)
Definition: IOVEditor.cc:154
edm::StreamContext::timestamp
Timestamp const & timestamp() const
Definition: StreamContext.h:62
mps_fire.result
result
Definition: mps_fire.py:303
cond::service::PoolDBOutputService::preGlobalBeginRun
void preGlobalBeginRun(edm::GlobalContext const &)
Definition: PoolDBOutputService.cc:159
ParameterSet.h
StreamContext.h
cond::TagInfo_t
Definition: Types.h:69
ntuplemaker.time
time
Definition: ntuplemaker.py:310
l1RCTOmdsFedVectorProducer_cfi.connectionString
connectionString
Definition: l1RCTOmdsFedVectorProducer_cfi.py:4
cond::persistency::IOVEditor::flush
bool flush()
Definition: IOVEditor.cc:295
Parameters
vector< ParameterSet > Parameters
Definition: HLTMuonPlotter.cc:25
cond::UserLogInfo::provenance
std::string provenance
Definition: Types.h:23
SystemBounds.h
cond::service::PoolDBOutputService::currentTime
cond::Time_t currentTime() const
Definition: PoolDBOutputService.cc:189
cond::service::PoolDBOutputService::startTransaction
void startTransaction()
Definition: PoolDBOutputService.cc:119
cond::service::PoolDBOutputService::forceInit
void forceInit()
Definition: PoolDBOutputService.cc:173
cond::service::PoolDBOutputService::PoolDBOutputService
PoolDBOutputService(const edm::ParameterSet &iConfig, edm::ActivityRegistry &iAR)
Definition: PoolDBOutputService.cc:37
cond::throwException
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:18
edm::ModuleCallingContext
Definition: ModuleCallingContext.h:29