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 startTransaction();
49  void commitTransaction();
50 
51  //
54 
55  //
56  template <typename T>
58  if (!payload)
59  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::writeOne");
60  std::lock_guard<std::recursive_mutex> lock(m_mutex);
63  Hash thePayloadHash("");
64  try {
65  this->initDB();
66  Record& myrecord = this->lookUpRecord(recordName);
67  m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
68  bool newTag = isNewTagRequest(recordName);
69  if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
70  cond::TagInfo_t tInfo;
71  this->getTagInfo(myrecord.m_idName, tInfo);
72  cond::Time_t lastSince = tInfo.lastInterval.since;
73  if (lastSince == cond::time::MAX_VAL)
74  lastSince = 0;
75  if (time <= lastSince) {
76  m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
77  << ", because is less or equal to last available since = " << lastSince;
78  if (m_autoCommit)
80  scope.close();
81  return thePayloadHash;
82  }
83  }
84  thePayloadHash = m_session.storePayload(*payload);
85  std::string payloadType = cond::demangledName(typeid(T));
86  if (newTag) {
87  createNewIOV(thePayloadHash, payloadType, time, myrecord);
88  } else {
89  appendSinceTime(thePayloadHash, time, myrecord);
90  }
91  if (m_autoCommit) {
93  m_logger.logWarning() << "Waiting " << m_writeTransactionDelay << "s before commit the changes...";
94  ::sleep(m_writeTransactionDelay);
95  }
97  }
98  } catch (const std::exception& er) {
99  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeOne");
100  }
101  scope.close();
102  return thePayloadHash;
103  }
104 
105  // close the IOVSequence setting lastTill
106  void closeIOV(Time_t lastTill, const std::string& recordName);
107 
108  // this one we need to avoid to adapt client code around... to be removed in the long term!
109  template <typename T>
110  void createNewIOV(const T* firstPayloadObj,
111  cond::Time_t firstSinceTime,
112  cond::Time_t,
113  const std::string& recordName) {
114  if (!firstPayloadObj)
115  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::createNewIOV");
116  std::lock_guard<std::recursive_mutex> lock(m_mutex);
117  Record& myrecord = this->lookUpRecord(recordName);
118  if (!myrecord.m_isNewTag) {
119  cond::throwException(myrecord.m_tag + " is not a new tag", "PoolDBOutputService::createNewIOV");
120  }
123  try {
124  this->initDB();
125  Hash payloadId = m_session.storePayload(*firstPayloadObj);
126  createNewIOV(payloadId, cond::demangledName(typeid(T)), firstSinceTime, myrecord);
127  } catch (const std::exception& er) {
128  cond::throwException(std::string(er.what()), "PoolDBOutputService::createNewIov");
129  }
130  scope.close();
131  }
132 
133  //
134  template <typename T>
135  void appendSinceTime(const T* payloadObj, cond::Time_t sinceTime, const std::string& recordName) {
136  if (!payloadObj)
137  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::appendSinceTime");
138  std::lock_guard<std::recursive_mutex> lock(m_mutex);
139  Record& myrecord = this->lookUpRecord(recordName);
140  if (myrecord.m_isNewTag) {
141  cond::throwException(std::string("Cannot append to non-existing tag ") + myrecord.m_tag,
142  "PoolDBOutputService::appendSinceTime");
143  }
146  try {
147  appendSinceTime(m_session.storePayload(*payloadObj), sinceTime, myrecord);
148  } catch (const std::exception& er) {
149  cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
150  }
151  scope.close();
152  }
153 
154  void createNewIOV(const std::string& firstPayloadId, cond::Time_t firstSinceTime, const std::string& recordName);
155 
156  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
157 
158  // Remove the payload and its valid sinceTime from the database
159  //
160  void eraseSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
161 
162  //
163  // Service time utility method
164  // return the infinity value according to the given timetype
165  //
166  cond::Time_t endOfTime() const;
167  //
168  // Service time utility method
169  // return beginning of time value according to the given timetype
170  //
171  cond::Time_t beginOfTime() const;
172  //
173  // Service time utility method
174  // return the time value of the current edm::Event according to the
175  // given timetype
176  //
177  cond::Time_t currentTime() const;
178 
179  // optional. User can inject additional information into the log associated with a given record
181  const std::string& provenance,
182  const std::string& usertext);
183 
184  // Retrieve tag information
186 
187  void forceInit();
188 
190 
191  private:
192  struct Record {
195 
202  };
203 
204  //
205  void doStartTransaction();
206  void doCommitTransaction();
207 
208  //
210 
211  //
212  void createNewIOV(const std::string& firstPayloadId,
213  const std::string payloadType,
214  cond::Time_t firstSinceTime,
215  Record& record);
216 
217  // Append the payload and its valid sinceTime into the database
218  // Note: the iov index appended to MUST pre-existing and the existing
219  // conditions data are retrieved from the DB
220  //
221  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, Record& record);
222 
223  //use these to control transaction interval
229 
230  void fillRecord(edm::ParameterSet& pset, const std::string& gTimeTypeStr);
231 
232  void initDB();
233 
236 
237  private:
239  std::recursive_mutex m_mutex;
241  std::vector<cond::Time_t> m_currentTimes;
242 
247  unsigned int m_writeTransactionDelay = 0;
249 
250  std::map<std::string, Record> m_records;
251  std::map<std::string, cond::UserLogInfo> m_logheaders;
252 
253  }; //PoolDBOutputService
254  } // namespace service
255 } // namespace cond
256 #endif
cond::persistency::TransactionScope
Definition: Session.h:231
cond::service::PoolDBOutputService::Record::m_isNewTag
bool m_isNewTag
Definition: PoolDBOutputService.h:198
service
Definition: service.py:1
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::service::PoolDBOutputService::Record
Definition: PoolDBOutputService.h:192
ConnectionPool.h
cond::service::PoolDBOutputService::initDB
void initDB()
Definition: PoolDBOutputService.cc:129
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:251
funct::false
false
Definition: Factorize.h:29
cond::service::PoolDBOutputService::beginOfTime
cond::Time_t beginOfTime() const
Definition: PoolDBOutputService.cc:187
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:275
cond::service::PoolDBOutputService::m_timetype
cond::TimeType m_timetype
Definition: PoolDBOutputService.h:240
edm
HLT enums.
Definition: AlignableModifier.h:19
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::Record::Record
Record()
Definition: PoolDBOutputService.h:193
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::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
cond::service::PoolDBOutputService
Definition: PoolDBOutputService.h:33
cond::service::PoolDBOutputService::m_records
std::map< std::string, Record > m_records
Definition: PoolDBOutputService.h:250
cond::persistency::Logger::logInfo
EchoedLogStream< edm::LogInfo > logInfo()
Definition: Logger.cc:157
cond::service::PoolDBOutputService::m_autoCommit
bool m_autoCommit
Definition: PoolDBOutputService.h:246
cond::service::PoolDBOutputService::Record::timetypestr
std::string timetypestr() const
Definition: PoolDBOutputService.h:196
cond::persistency::Logger
Definition: Logger.h:85
ActivityRegistry.h
cond::service::PoolDBOutputService::m_session
cond::persistency::Session m_session
Definition: PoolDBOutputService.h:244
cond::service::PoolDBOutputService::Record::m_idName
std::string m_idName
Definition: PoolDBOutputService.h:199
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
cond::service::PoolDBOutputService::Record::m_onlyAppendUpdatePolicy
bool m_onlyAppendUpdatePolicy
Definition: PoolDBOutputService.h:201
cond::persistency::ConnectionPool
Definition: ConnectionPool.h:35
edm::ActivityRegistry
Definition: ActivityRegistry.h:133
Event
cond::service::PoolDBOutputService::commitTransaction
void commitTransaction()
Definition: PoolDBOutputService.cc:124
jets_cff.payload
payload
Definition: jets_cff.py:32
cond::service::PoolDBOutputService::postEndJob
void postEndJob()
Definition: PoolDBOutputService.cc:143
cond::service::PoolDBOutputService::doStartTransaction
void doStartTransaction()
Definition: PoolDBOutputService.cc:105
cond::service::PoolDBOutputService::m_connection
cond::persistency::ConnectionPool m_connection
Definition: PoolDBOutputService.h:243
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
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:248
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:112
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:347
cond::service::PoolDBOutputService::m_writeTransactionDelay
unsigned int m_writeTransactionDelay
Definition: PoolDBOutputService.h:247
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:241
cond::service::PoolDBOutputService::m_logger
cond::persistency::Logger m_logger
Definition: PoolDBOutputService.h:238
cond::service::PoolDBOutputService::endOfTime
cond::Time_t endOfTime() const
Definition: PoolDBOutputService.cc:185
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:9515
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::service::PoolDBOutputService::logger
cond::persistency::Logger & logger()
Definition: PoolDBOutputService.h:189
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:57
cond::UserLogInfo
Definition: Types.h:22
cond::service::PoolDBOutputService::isNewTagRequest
bool isNewTagRequest(const std::string &recordName)
Definition: PoolDBOutputService.cc:100
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
mps_fire.result
result
Definition: mps_fire.py:311
cond::service::PoolDBOutputService::preGlobalBeginRun
void preGlobalBeginRun(edm::GlobalContext const &)
Definition: PoolDBOutputService.cc:159
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:245
cond::service::PoolDBOutputService::m_mutex
std::recursive_mutex m_mutex
Definition: PoolDBOutputService.h:239
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
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