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  template <typename T>
63  std::lock_guard<std::recursive_mutex> lock(m_mutex);
66  Hash thePayloadHash("");
67  try {
68  this->initDB();
69  auto& myrecord = this->getRecord(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  }
97  } catch (const std::exception& er) {
98  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeOne");
99  }
100  scope.close();
101  return thePayloadHash;
102  }
103 
104  template <typename T>
105  void writeMany(const std::map<Time_t, std::shared_ptr<T> >& iovAndPayloads, const std::string& recordName) {
106  if (iovAndPayloads.empty())
107  return;
108  std::lock_guard<std::recursive_mutex> lock(m_mutex);
111  try {
112  this->initDB();
113  auto& myrecord = this->getRecord(recordName);
114  m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
115  bool newTag = isNewTagRequest(recordName);
116  cond::Time_t lastSince;
118  if (newTag) {
119  std::string payloadType = cond::demangledName(typeid(T));
120  editor = m_session.createIov(payloadType, myrecord.m_tag, myrecord.m_timetype, cond::SYNCH_ANY);
121  editor.setDescription("New Tag");
122  } else {
123  editor = m_session.editIov(myrecord.m_tag);
124  if (myrecord.m_onlyAppendUpdatePolicy) {
125  cond::TagInfo_t tInfo;
126  this->getTagInfo(myrecord.m_idName, tInfo);
127  lastSince = tInfo.lastInterval.since;
128  if (lastSince == cond::time::MAX_VAL)
129  lastSince = 0;
130  }
131  }
132  for (auto& iovEntry : iovAndPayloads) {
133  Time_t time = iovEntry.first;
134  auto payload = iovEntry.second;
135  if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
136  if (time <= lastSince) {
137  m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
138  << ", because is less or equal to last available since = " << lastSince;
139  continue;
140  }
141  }
142  auto payloadHash = m_session.storePayload(*payload);
143  editor.insert(time, payloadHash);
144  }
145  cond::UserLogInfo a = this->lookUpUserLogInfo(myrecord.m_idName);
146  editor.flush(a.usertext);
147  if (m_autoCommit) {
149  }
150  } catch (const std::exception& er) {
151  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeMany");
152  }
153  scope.close();
154  return;
155  }
156 
157  // close the IOVSequence setting lastTill
158  void closeIOV(Time_t lastTill, const std::string& recordName);
159 
160  template <typename T>
161  void createOneIOV(const T& payload, cond::Time_t firstSinceTime, const std::string& recordName) {
162  std::lock_guard<std::recursive_mutex> lock(m_mutex);
165  try {
166  this->initDB();
167  auto& myrecord = this->getRecord(recordName);
168  if (!myrecord.m_isNewTag) {
169  cond::throwException(myrecord.m_tag + " is not a new tag", "PoolDBOutputService::createNewIOV");
170  }
171  Hash payloadId = m_session.storePayload(payload);
172  createNewIOV(payloadId, cond::demangledName(typeid(payload)), firstSinceTime, myrecord);
173  if (m_autoCommit) {
175  }
176  } catch (const std::exception& er) {
177  cond::throwException(std::string(er.what()), "PoolDBOutputService::createNewIov");
178  }
179  scope.close();
180  }
181 
182  template <typename T>
183  void appendOneIOV(const T& payload, cond::Time_t sinceTime, const std::string& recordName) {
184  std::lock_guard<std::recursive_mutex> lock(m_mutex);
187  try {
188  bool dbexists = this->initDB(true);
189  if (!dbexists) {
190  cond::throwException(std::string("Target database does not exist."),
191  "PoolDBOutputService::appendSinceTime");
192  }
193  auto& myrecord = this->lookUpRecord(recordName);
194  if (myrecord.m_isNewTag) {
195  cond::throwException(std::string("Cannot append to non-existing tag ") + myrecord.m_tag,
196  "PoolDBOutputService::appendSinceTime");
197  }
198  appendSinceTime(m_session.storePayload(payload), sinceTime, myrecord);
199  if (m_autoCommit) {
201  }
202  } catch (const std::exception& er) {
203  cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
204  }
205  scope.close();
206  }
207 
208  void createNewIOV(const std::string& firstPayloadId, cond::Time_t firstSinceTime, const std::string& recordName);
209 
210  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
211 
212  // Remove the payload and its valid sinceTime from the database
213  //
214  void eraseSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
215 
216  //
217  // Service time utility method
218  // return the infinity value according to the given timetype
219  //
220  cond::Time_t endOfTime() const;
221  //
222  // Service time utility method
223  // return beginning of time value according to the given timetype
224  //
225  cond::Time_t beginOfTime() const;
226  //
227  // Service time utility method
228  // return the time value of the current edm::Event according to the
229  // given timetype
230  //
231  cond::Time_t currentTime() const;
232 
233  // optional. User can inject additional information into the log associated with a given record
235  const std::string& provenance,
236  const std::string& usertext);
237 
238  // Retrieve tag information
240 
241  void forceInit();
242 
244 
245  struct Record {
248 
254  unsigned int m_refreshTime = 0;
256  };
257 
258  const Record& lookUpRecord(const std::string& recordName);
259 
260  private:
261  //
262  void doStartTransaction();
263  void doCommitTransaction();
264 
265  //
267 
268  //
269  void createNewIOV(const std::string& firstPayloadId,
270  const std::string payloadType,
271  cond::Time_t firstSinceTime,
272  Record& record);
273 
274  // Append the payload and its valid sinceTime into the database
275  // Note: the iov index appended to MUST pre-existing and the existing
276  // conditions data are retrieved from the DB
277  //
278  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const Record& record);
279 
280  //use these to control transaction interval
286 
287  void fillRecord(edm::ParameterSet& pset, const std::string& gTimeTypeStr);
288 
289  bool initDB(bool readOnly = false);
290 
292 
294 
295  private:
297  std::recursive_mutex m_mutex;
299  std::vector<cond::Time_t> m_currentTimes;
300 
305  unsigned int m_writeTransactionDelay = 0;
307 
308  std::map<std::string, Record> m_records;
309  std::map<std::string, cond::UserLogInfo> m_logheaders;
310 
311  }; //PoolDBOutputService
312  } // namespace service
313 } // namespace cond
314 #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)
const Record & lookUpRecord(const std::string &recordName)
std::map< std::string, cond::UserLogInfo > m_logheaders
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
std::string to_string(const V &value)
Definition: OMSAccess.h:71
void setDescription(const std::string &description)
Definition: IOVEditor.cc:139
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
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)
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)
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
Record & getRecord(const std::string &recordName)
std::string name
Definition: Time.h:39
void preGlobalBeginRun(edm::GlobalContext const &)
bool appendSinceTime(const std::string &payloadId, cond::Time_t sinceTime, const std::string &recordName)
void createNewIOV(const std::string &firstPayloadId, cond::Time_t firstSinceTime, const std::string &recordName)
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
void writeMany(const std::map< Time_t, std::shared_ptr< T > > &iovAndPayloads, const std::string &recordName)
Definition: plugin.cc:23
HLT enums.
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)
cond::persistency::Session session() const