CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  //
59  bool isNewTagRequest(const std::string& recordName);
60 
61  template <typename T>
62  Hash writeOneIOV(const T& payload, Time_t time, const std::string& recordName) {
63  std::lock_guard<std::recursive_mutex> lock(m_mutex);
66  Hash thePayloadHash("");
67  try {
68  this->initDB();
69  Record& myrecord = this->lookUpRecord(recordName);
70  m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
71  bool newTag = isNewTagRequest(recordName);
72  if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
73  cond::TagInfo_t tInfo;
74  this->getTagInfo(myrecord.m_idName, tInfo);
75  cond::Time_t lastSince = tInfo.lastInterval.since;
76  if (lastSince == cond::time::MAX_VAL)
77  lastSince = 0;
78  if (time <= lastSince) {
79  m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
80  << ", because is less or equal to last available since = " << lastSince;
81  if (m_autoCommit)
83  scope.close();
84  return thePayloadHash;
85  }
86  }
87  thePayloadHash = m_session.storePayload(payload);
88  std::string payloadType = cond::demangledName(typeid(payload));
89  if (newTag) {
90  createNewIOV(thePayloadHash, payloadType, time, myrecord);
91  } else {
92  appendSinceTime(thePayloadHash, time, myrecord);
93  }
94  if (m_autoCommit) {
96  m_logger.logWarning() << "Waiting " << m_writeTransactionDelay << "s before commit the changes...";
97  ::sleep(m_writeTransactionDelay);
98  }
100  }
101  } catch (const std::exception& er) {
102  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeOne");
103  }
104  scope.close();
105  return thePayloadHash;
106  }
107 
108  // warning: takes over the ownership of pointer "payload". Deprecated. Please move to the above referece-based function
109  template <typename T>
110  Hash writeOne(const T* payloadPtr, Time_t time, const std::string& recordName) {
111  if (!payloadPtr)
112  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::writeOne");
113  std::unique_ptr<const T> payload(payloadPtr);
114  return writeOneIOV<T>(*payload, time, recordName);
115  }
116 
117  template <typename T>
118  void writeMany(const std::map<Time_t, std::shared_ptr<T> >& iovAndPayloads, const std::string& recordName) {
119  if (iovAndPayloads.empty())
120  return;
121  std::lock_guard<std::recursive_mutex> lock(m_mutex);
124  try {
125  this->initDB();
126  Record& myrecord = this->lookUpRecord(recordName);
127  m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
128  bool newTag = isNewTagRequest(recordName);
129  cond::Time_t lastSince;
131  if (newTag) {
132  std::string payloadType = cond::demangledName(typeid(T));
133  editor = m_session.createIov(payloadType, myrecord.m_tag, myrecord.m_timetype, cond::SYNCH_ANY);
134  editor.setDescription("New Tag");
135  } else {
136  editor = m_session.editIov(myrecord.m_tag);
137  if (myrecord.m_onlyAppendUpdatePolicy) {
138  cond::TagInfo_t tInfo;
139  this->getTagInfo(myrecord.m_idName, tInfo);
140  lastSince = tInfo.lastInterval.since;
141  if (lastSince == cond::time::MAX_VAL)
142  lastSince = 0;
143  }
144  }
145  for (auto& iovEntry : iovAndPayloads) {
146  Time_t time = iovEntry.first;
147  auto payload = iovEntry.second;
148  if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
149  if (time <= lastSince) {
150  m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
151  << ", because is less or equal to last available since = " << lastSince;
152  continue;
153  }
154  }
155  auto payloadHash = m_session.storePayload(*payload);
156  editor.insert(time, payloadHash);
157  }
158  cond::UserLogInfo a = this->lookUpUserLogInfo(myrecord.m_idName);
159  editor.flush(a.usertext);
160  if (m_autoCommit) {
162  }
163  } catch (const std::exception& er) {
164  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeMany");
165  }
166  scope.close();
167  return;
168  }
169 
170  // close the IOVSequence setting lastTill
171  void closeIOV(Time_t lastTill, const std::string& recordName);
172 
173  template <typename T>
174  void createOneIOV(const T& payload, cond::Time_t firstSinceTime, const std::string& recordName) {
175  std::lock_guard<std::recursive_mutex> lock(m_mutex);
176  Record& myrecord = this->lookUpRecord(recordName);
177  if (!myrecord.m_isNewTag) {
178  cond::throwException(myrecord.m_tag + " is not a new tag", "PoolDBOutputService::createNewIOV");
179  }
182  try {
183  this->initDB();
184  Hash payloadId = m_session.storePayload(payload);
185  createNewIOV(payloadId, cond::demangledName(typeid(payload)), firstSinceTime, myrecord);
186  } catch (const std::exception& er) {
187  cond::throwException(std::string(er.what()), "PoolDBOutputService::createNewIov");
188  }
189  scope.close();
190  }
191 
192  // warning: takes over the ownership of pointer "payload" - deprecated. Please move to the above reference-based function
193  template <typename T>
194  void createNewIOV(const T* payloadPtr, cond::Time_t firstSinceTime, cond::Time_t, const std::string& recordName) {
195  if (!payloadPtr)
196  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::createNewIOV");
197  std::unique_ptr<const T> payload(payloadPtr);
198  this->createOneIOV<T>(*payload, firstSinceTime, recordName);
199  }
200 
201  template <typename T>
202  void appendOneIOV(const T& payload, cond::Time_t sinceTime, const std::string& recordName) {
203  std::lock_guard<std::recursive_mutex> lock(m_mutex);
204  Record& myrecord = this->lookUpRecord(recordName);
205  if (myrecord.m_isNewTag) {
206  cond::throwException(std::string("Cannot append to non-existing tag ") + myrecord.m_tag,
207  "PoolDBOutputService::appendSinceTime");
208  }
211  try {
212  appendSinceTime(m_session.storePayload(payload), sinceTime, myrecord);
213  } catch (const std::exception& er) {
214  cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
215  }
216  scope.close();
217  }
218 
219  // warning: takes over the ownership of pointer "payload" - deprecated. Please move to the above reference-based function
220  template <typename T>
221  void appendSinceTime(const T* payloadPtr, cond::Time_t sinceTime, const std::string& recordName) {
222  if (!payloadPtr)
223  throwException("Provided payload pointer is invalid.", "PoolDBOutputService::appendSinceTime");
224  std::unique_ptr<const T> payload(payloadPtr);
225  this->appendOneIOV<T>(*payload, sinceTime, recordName);
226  }
227 
228  void createNewIOV(const std::string& firstPayloadId, cond::Time_t firstSinceTime, const std::string& recordName);
229 
230  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
231 
232  // Remove the payload and its valid sinceTime from the database
233  //
234  void eraseSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
235 
236  //
237  // Service time utility method
238  // return the infinity value according to the given timetype
239  //
240  cond::Time_t endOfTime() const;
241  //
242  // Service time utility method
243  // return beginning of time value according to the given timetype
244  //
245  cond::Time_t beginOfTime() const;
246  //
247  // Service time utility method
248  // return the time value of the current edm::Event according to the
249  // given timetype
250  //
251  cond::Time_t currentTime() const;
252 
253  // optional. User can inject additional information into the log associated with a given record
254  void setLogHeaderForRecord(const std::string& recordName,
255  const std::string& provenance,
256  const std::string& usertext);
257 
258  // Retrieve tag information
259  bool tagInfo(const std::string& recordName, cond::TagInfo_t& result);
260 
261  void forceInit();
262 
264 
265  private:
266  struct Record {
269 
276  };
277 
278  //
279  void doStartTransaction();
280  void doCommitTransaction();
281 
282  //
283  bool getTagInfo(const std::string& recordName, cond::TagInfo_t& result);
284 
285  //
286  void createNewIOV(const std::string& firstPayloadId,
287  const std::string payloadType,
288  cond::Time_t firstSinceTime,
289  Record& record);
290 
291  // Append the payload and its valid sinceTime into the database
292  // Note: the iov index appended to MUST pre-existing and the existing
293  // conditions data are retrieved from the DB
294  //
295  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, Record& record);
296 
297  //use these to control transaction interval
303 
304  void fillRecord(edm::ParameterSet& pset, const std::string& gTimeTypeStr);
305 
306  void initDB();
307 
308  Record& lookUpRecord(const std::string& recordName);
309  cond::UserLogInfo& lookUpUserLogInfo(const std::string& recordName);
310 
311  private:
313  std::recursive_mutex m_mutex;
315  std::vector<cond::Time_t> m_currentTimes;
316 
321  unsigned int m_writeTransactionDelay = 0;
323 
324  std::map<std::string, Record> m_records;
325  std::map<std::string, cond::UserLogInfo> m_logheaders;
326 
327  }; //PoolDBOutputService
328  } // namespace service
329 } // namespace cond
330 #endif
const TimeTypeSpecs timeTypeSpecs[]
Definition: Time.cc:16
Iov_t lastInterval
Definition: Types.h:73
void preEventProcessing(edm::StreamContext const &)
void fillRecord(edm::ParameterSet &pset, const std::string &gTimeTypeStr)
std::map< std::string, cond::UserLogInfo > m_logheaders
EchoedLogStream< edm::LogWarning > logWarning()
Definition: Logger.cc:169
cond::persistency::Session session() const
Time_t since
Definition: Types.h:53
std::vector< cond::Time_t > m_currentTimes
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:179
void setDescription(const std::string &description)
Definition: IOVEditor.cc:139
tuple recordName
Definition: align_cfg.py:66
void preGlobalBeginLumi(edm::GlobalContext const &)
Transaction & transaction()
Definition: Session.cc:52
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:18
Hash writeOne(const T *payloadPtr, Time_t time, const std::string &recordName)
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
TimeType
Definition: Time.h:19
std::string tag(const std::string &recordName)
tuple result
Definition: mps_fire.py:311
void appendSinceTime(const T *payloadPtr, cond::Time_t sinceTime, const std::string &recordName)
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
void closeIOV(Time_t lastTill, const std::string &recordName)
unsigned long long Time_t
Definition: Time.h:14
bool isNewTagRequest(const std::string &recordName)
Record & lookUpRecord(const std::string &recordName)
cond::persistency::Logger & logger()
cond::persistency::ConnectionPool m_connection
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
std::string Hash
Definition: Types.h:43
cond::persistency::Session m_session
void setLogHeaderForRecord(const std::string &recordName, const std::string &provenance, const std::string &usertext)
std::map< std::string, Record > m_records
cond::persistency::Logger m_logger
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:130
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:159
std::string name
Definition: Time.h:39
void preGlobalBeginRun(edm::GlobalContext const &)
bool getTagInfo(const std::string &recordName, cond::TagInfo_t &result)
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:186
std::string usertext
Definition: Types.h:24
void writeMany(const std::map< Time_t, std::shared_ptr< T > > &iovAndPayloads, const std::string &recordName)
cond::persistency::Session newReadOnlySession(const std::string &connectionString, const std::string &transactionId)
double a
Definition: hdecay.h:119
void postModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
void preModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
EchoedLogStream< edm::LogInfo > logInfo()
Definition: Logger.cc:157
long double T
bool tagInfo(const std::string &recordName, cond::TagInfo_t &result)
void eraseSinceTime(const std::string &payloadId, cond::Time_t sinceTime, const std::string &recordName)
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
cond::UserLogInfo & lookUpUserLogInfo(const std::string &recordName)
PoolDBOutputService(const edm::ParameterSet &iConfig, edm::ActivityRegistry &iAR)
void createNewIOV(const T *payloadPtr, cond::Time_t firstSinceTime, cond::Time_t, const std::string &recordName)