CMS 3D CMS Logo

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