CMS 3D CMS Logo

PoolDBOutputService.h
Go to the documentation of this file.
1 #ifndef CondCore_PoolDBOutputService_h
2 #define CondCore_PoolDBOutputService_h
8 #include <string>
9 #include <map>
10 #include <mutex>
11 
12 //
13 // Package: DBOutputService
14 // Class : PoolDBOutputService
15 //
19 //
20 // Author: Zhen Xie
21 // Fixes and other changes: Giacomo Govi
22 //
23 
24 namespace edm {
25  class Event;
26  class EventSetup;
27  class ParameterSet;
28 } // namespace edm
29 namespace cond {
30 
31  namespace service {
32 
34  public:
36 
37  virtual ~PoolDBOutputService();
38 
39  //use these to control connections
40  void postEndJob();
41 
43  const std::string& transactionId);
44  // return the database session in use
46 
47  //
48  void lockRecords();
49 
50  //
51  void releaseLocks();
52 
53  //
54  void startTransaction();
55  void commitTransaction();
56 
57  //
60 
61  //
62  template <typename T>
64  if (!payload)
65  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::writeOne");
66  std::lock_guard<std::recursive_mutex> lock(m_mutex);
69  Hash thePayloadHash("");
70  try {
71  this->initDB();
72  Record& myrecord = this->lookUpRecord(recordName);
73  m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
74  bool newTag = isNewTagRequest(recordName);
75  if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
76  cond::TagInfo_t tInfo;
77  this->getTagInfo(myrecord.m_idName, tInfo);
78  cond::Time_t lastSince = tInfo.lastInterval.since;
79  if (lastSince == cond::time::MAX_VAL)
80  lastSince = 0;
81  if (time <= lastSince) {
82  m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
83  << ", because is less or equal to last available since = " << lastSince;
84  if (m_autoCommit)
86  scope.close();
87  return thePayloadHash;
88  }
89  }
90  thePayloadHash = m_session.storePayload(*payload);
91  std::string payloadType = cond::demangledName(typeid(T));
92  if (newTag) {
93  createNewIOV(thePayloadHash, payloadType, time, myrecord);
94  } else {
95  appendSinceTime(thePayloadHash, time, myrecord);
96  }
97  if (m_autoCommit) {
99  m_logger.logWarning() << "Waiting " << m_writeTransactionDelay << "s before commit the changes...";
100  ::sleep(m_writeTransactionDelay);
101  }
103  }
104  } catch (const std::exception& er) {
105  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeOne");
106  }
107  scope.close();
108  return thePayloadHash;
109  }
110 
111  // close the IOVSequence setting lastTill
112  void closeIOV(Time_t lastTill, const std::string& recordName);
113 
114  // this one we need to avoid to adapt client code around... to be removed in the long term!
115  template <typename T>
116  void createNewIOV(const T* firstPayloadObj,
117  cond::Time_t firstSinceTime,
118  cond::Time_t,
119  const std::string& recordName) {
120  if (!firstPayloadObj)
121  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::createNewIOV");
122  std::lock_guard<std::recursive_mutex> lock(m_mutex);
123  Record& myrecord = this->lookUpRecord(recordName);
124  if (!myrecord.m_isNewTag) {
125  cond::throwException(myrecord.m_tag + " is not a new tag", "PoolDBOutputService::createNewIOV");
126  }
129  try {
130  this->initDB();
131  Hash payloadId = m_session.storePayload(*firstPayloadObj);
132  createNewIOV(payloadId, cond::demangledName(typeid(T)), firstSinceTime, myrecord);
133  } catch (const std::exception& er) {
134  cond::throwException(std::string(er.what()), "PoolDBOutputService::createNewIov");
135  }
136  scope.close();
137  }
138 
139  //
140  template <typename T>
141  void appendSinceTime(const T* payloadObj, cond::Time_t sinceTime, const std::string& recordName) {
142  if (!payloadObj)
143  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::appendSinceTime");
144  std::lock_guard<std::recursive_mutex> lock(m_mutex);
145  Record& myrecord = this->lookUpRecord(recordName);
146  if (myrecord.m_isNewTag) {
147  cond::throwException(std::string("Cannot append to non-existing tag ") + myrecord.m_tag,
148  "PoolDBOutputService::appendSinceTime");
149  }
152  try {
153  appendSinceTime(m_session.storePayload(*payloadObj), sinceTime, myrecord);
154  } catch (const std::exception& er) {
155  cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
156  }
157  scope.close();
158  }
159 
160  void createNewIOV(const std::string& firstPayloadId, cond::Time_t firstSinceTime, const std::string& recordName);
161 
162  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
163 
164  // Remove the payload and its valid sinceTime from the database
165  //
166  void eraseSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
167 
168  //
169  // Service time utility method
170  // return the infinity value according to the given timetype
171  //
172  cond::Time_t endOfTime() const;
173  //
174  // Service time utility method
175  // return beginning of time value according to the given timetype
176  //
177  cond::Time_t beginOfTime() const;
178  //
179  // Service time utility method
180  // return the time value of the current edm::Event according to the
181  // given timetype
182  //
183  cond::Time_t currentTime() const;
184 
185  // optional. User can inject additional information into the log associated with a given record
187  const std::string& provenance,
188  const std::string& usertext);
189 
190  // Retrieve tag information
192 
193  void forceInit();
194 
196 
197  private:
198  struct Record {
201 
208  };
209 
210  //
211  void doStartTransaction();
212  void doCommitTransaction();
213 
214  //
216 
217  //
218  void createNewIOV(const std::string& firstPayloadId,
219  const std::string payloadType,
220  cond::Time_t firstSinceTime,
221  Record& record);
222 
223  // Append the payload and its valid sinceTime into the database
224  // Note: the iov index appended to MUST pre-existing and the existing
225  // conditions data are retrieved from the DB
226  //
227  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, Record& record);
228 
229  //use these to control transaction interval
235 
236  void fillRecord(edm::ParameterSet& pset, const std::string& gTimeTypeStr);
237 
238  void initDB();
239 
242 
243  private:
245  std::recursive_mutex m_mutex;
247  std::vector<cond::Time_t> m_currentTimes;
248 
253  unsigned int m_writeTransactionDelay = 0;
255 
256  std::map<std::string, Record> m_records;
257  std::map<std::string, cond::UserLogInfo> m_logheaders;
258 
259  }; //PoolDBOutputService
260  } // namespace service
261 } // namespace cond
262 #endif
cond::persistency::TransactionScope
Definition: Session.h:231
cond::service::PoolDBOutputService::Record::m_isNewTag
bool m_isNewTag
Definition: PoolDBOutputService.h:204
service
Definition: service.py:1
cond::service::PoolDBOutputService::lookUpRecord
Record & lookUpRecord(const std::string &recordName)
Definition: PoolDBOutputService.cc:328
cond::service::PoolDBOutputService::tag
std::string tag(const std::string &recordName)
Definition: PoolDBOutputService.cc:124
cond::service::PoolDBOutputService::Record
Definition: PoolDBOutputService.h:198
ConnectionPool.h
cond::service::PoolDBOutputService::initDB
void initDB()
Definition: PoolDBOutputService.cc:157
cond::TimeType
TimeType
Definition: Time.h:19
MessageLogger.h
cond::service::PoolDBOutputService::m_logheaders
std::map< std::string, cond::UserLogInfo > m_logheaders
Definition: PoolDBOutputService.h:257
funct::false
false
Definition: Factorize.h:29
cond::service::PoolDBOutputService::beginOfTime
cond::Time_t beginOfTime() const
Definition: PoolDBOutputService.cc:215
cond::Hash
std::string Hash
Definition: Types.h:43
cond::service::PoolDBOutputService::eraseSinceTime
void eraseSinceTime(const std::string &payloadId, cond::Time_t sinceTime, const std::string &recordName)
Definition: PoolDBOutputService.cc:303
cond::service::PoolDBOutputService::m_timetype
cond::TimeType m_timetype
Definition: PoolDBOutputService.h:246
edm
HLT enums.
Definition: AlignableModifier.h:19
cond::service::PoolDBOutputService::Record::m_timetype
cond::TimeType m_timetype
Definition: PoolDBOutputService.h:206
align_cfg.recordName
recordName
Definition: align_cfg.py:66
cond::service::PoolDBOutputService::Record::Record
Record()
Definition: PoolDBOutputService.h:199
cond::service::PoolDBOutputService::preModuleEvent
void preModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
Definition: PoolDBOutputService.cc:177
cond::service::PoolDBOutputService::tagInfo
bool tagInfo(const std::string &recordName, cond::TagInfo_t &result)
Definition: PoolDBOutputService.cc:397
cond::service::PoolDBOutputService::preGlobalBeginLumi
void preGlobalBeginLumi(edm::GlobalContext const &)
Definition: PoolDBOutputService.cc:193
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:141
cond::service::PoolDBOutputService::closeIOV
void closeIOV(Time_t lastTill, const std::string &recordName)
Definition: PoolDBOutputService.cc:346
cond::service::PoolDBOutputService
Definition: PoolDBOutputService.h:33
cond::service::PoolDBOutputService::m_records
std::map< std::string, Record > m_records
Definition: PoolDBOutputService.h:256
cond::persistency::Logger::logInfo
EchoedLogStream< edm::LogInfo > logInfo()
Definition: Logger.cc:157
cond::service::PoolDBOutputService::m_autoCommit
bool m_autoCommit
Definition: PoolDBOutputService.h:252
cond::service::PoolDBOutputService::Record::timetypestr
std::string timetypestr() const
Definition: PoolDBOutputService.h:202
cond::persistency::Logger
Definition: Logger.h:85
ActivityRegistry.h
cond::service::PoolDBOutputService::m_session
cond::persistency::Session m_session
Definition: PoolDBOutputService.h:250
cond::service::PoolDBOutputService::Record::m_idName
std::string m_idName
Definition: PoolDBOutputService.h:205
cond::service::PoolDBOutputService::lookUpUserLogInfo
cond::UserLogInfo & lookUpUserLogInfo(const std::string &recordName)
Definition: PoolDBOutputService.cc:338
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:116
cond::service::PoolDBOutputService::session
cond::persistency::Session session() const
Definition: PoolDBOutputService.cc:92
cond::service::PoolDBOutputService::preEventProcessing
void preEventProcessing(edm::StreamContext const &)
Definition: PoolDBOutputService.cc:173
cond::service::PoolDBOutputService::Record::m_onlyAppendUpdatePolicy
bool m_onlyAppendUpdatePolicy
Definition: PoolDBOutputService.h:207
cond::persistency::ConnectionPool
Definition: ConnectionPool.h:35
edm::ActivityRegistry
Definition: ActivityRegistry.h:133
Event
cond::service::PoolDBOutputService::commitTransaction
void commitTransaction()
Definition: PoolDBOutputService.cc:152
jets_cff.payload
payload
Definition: jets_cff.py:32
cond::service::PoolDBOutputService::postEndJob
void postEndJob()
Definition: PoolDBOutputService.cc:171
cond::service::PoolDBOutputService::doStartTransaction
void doStartTransaction()
Definition: PoolDBOutputService.cc:133
cond::service::PoolDBOutputService::m_connection
cond::persistency::ConnectionPool m_connection
Definition: PoolDBOutputService.h:249
cond::service::PoolDBOutputService::postModuleEvent
void postModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
Definition: PoolDBOutputService.cc:182
cond::service::PoolDBOutputService::fillRecord
void fillRecord(edm::ParameterSet &pset, const std::string &gTimeTypeStr)
Definition: PoolDBOutputService.cc:20
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cond
Definition: plugin.cc:23
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
funct::true
true
Definition: Factorize.h:173
cond::service::PoolDBOutputService::m_dbInitialised
bool m_dbInitialised
Definition: PoolDBOutputService.h:254
edm::GlobalContext
Definition: GlobalContext.h:29
cond::persistency::Logger::logWarning
EchoedLogStream< edm::LogWarning > logWarning()
Definition: Logger.cc:169
cond::service::PoolDBOutputService::doCommitTransaction
void doCommitTransaction()
Definition: PoolDBOutputService.cc:140
edm::ParameterSet
Definition: ParameterSet.h:47
ParameterSet
Definition: Functions.h:16
CommonMethods.lock
def lock()
Definition: CommonMethods.py:82
cond::runnumber
Definition: Time.h:19
cond::persistency::Session
Definition: Session.h:63
cond::Iov_t::since
Time_t since
Definition: Types.h:53
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:375
cond::service::PoolDBOutputService::m_writeTransactionDelay
unsigned int m_writeTransactionDelay
Definition: PoolDBOutputService.h:253
cond::time::MAX_VAL
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
cond::service::PoolDBOutputService::m_currentTimes
std::vector< cond::Time_t > m_currentTimes
Definition: PoolDBOutputService.h:247
cond::service::PoolDBOutputService::m_logger
cond::persistency::Logger m_logger
Definition: PoolDBOutputService.h:244
cond::service::PoolDBOutputService::endOfTime
cond::Time_t endOfTime() const
Definition: PoolDBOutputService.cc:213
cond::service::PoolDBOutputService::releaseLocks
void releaseLocks()
Definition: PoolDBOutputService.cc:109
cond::TagInfo_t::lastInterval
Iov_t lastInterval
Definition: Types.h:73
cond::persistency::Session::storePayload
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:186
cond::persistency::Session::transaction
Transaction & transaction()
Definition: Session.cc:52
HLT_FULL_cff.payloadType
payloadType
Definition: HLT_FULL_cff.py:9452
cond::service::PoolDBOutputService::setLogHeaderForRecord
void setLogHeaderForRecord(const std::string &recordName, const std::string &provenance, const std::string &usertext)
Definition: PoolDBOutputService.cc:366
cond::service::PoolDBOutputService::Record::m_tag
std::string m_tag
Definition: PoolDBOutputService.h:203
cond::service::PoolDBOutputService::logger
cond::persistency::Logger & logger()
Definition: PoolDBOutputService.h:195
T
long double T
Definition: Basic3DVectorLD.h:48
cond::service::PoolDBOutputService::writeOne
Hash writeOne(const T *payload, Time_t time, const std::string &recordName)
Definition: PoolDBOutputService.h:63
cond::UserLogInfo
Definition: Types.h:22
cond::service::PoolDBOutputService::isNewTagRequest
bool isNewTagRequest(const std::string &recordName)
Definition: PoolDBOutputService.cc:128
cond::service::PoolDBOutputService::~PoolDBOutputService
virtual ~PoolDBOutputService()
Definition: PoolDBOutputService.cc:199
cond::service::PoolDBOutputService::newReadOnlySession
cond::persistency::Session newReadOnlySession(const std::string &connectionString, const std::string &transactionId)
Definition: PoolDBOutputService.cc:85
mps_fire.result
result
Definition: mps_fire.py:311
cond::service::PoolDBOutputService::lockRecords
void lockRecords()
Definition: PoolDBOutputService.cc:94
cond::service::PoolDBOutputService::preGlobalBeginRun
void preGlobalBeginRun(edm::GlobalContext const &)
Definition: PoolDBOutputService.cc:187
EventSetup
cond::TimeTypeSpecs::name
std::string name
Definition: Time.h:39
cond::TagInfo_t
Definition: Types.h:69
ntuplemaker.time
time
Definition: ntuplemaker.py:310
l1RCTOmdsFedVectorProducer_cfi.connectionString
connectionString
Definition: l1RCTOmdsFedVectorProducer_cfi.py:4
Logger.h
Session.h
cond::service::PoolDBOutputService::m_transactionActive
bool m_transactionActive
Definition: PoolDBOutputService.h:251
cond::service::PoolDBOutputService::m_mutex
std::recursive_mutex m_mutex
Definition: PoolDBOutputService.h:245
cond::service::PoolDBOutputService::currentTime
cond::Time_t currentTime() const
Definition: PoolDBOutputService.cc:217
cond::service::PoolDBOutputService::startTransaction
void startTransaction()
Definition: PoolDBOutputService.cc:147
cond::service::PoolDBOutputService::forceInit
void forceInit()
Definition: PoolDBOutputService.cc:201
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
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