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 
38 
39  virtual ~PoolDBOutputService();
40 
41  //use these to control connections
42  void postEndJob();
43 
45  const std::string& transactionId);
46  // return the database session in use
48 
49  //
50  void lockRecords();
51 
52  //
53  void releaseLocks();
54 
55  //
56  void startTransaction();
57  void commitTransaction();
58 
59  //
62 
63  template <typename T>
65  std::lock_guard<std::recursive_mutex> lock(m_mutex);
68  Hash thePayloadHash("");
69  try {
70  this->initDB();
71  auto& myrecord = this->getRecord(recordName);
72  m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
73  bool newTag = isNewTagRequest(recordName);
74  if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
75  cond::TagInfo_t tInfo;
76  this->getTagInfo(myrecord.m_idName, tInfo);
77  cond::Time_t lastSince = tInfo.lastInterval.since;
78  if (lastSince == cond::time::MAX_VAL)
79  lastSince = 0;
80  if (time <= lastSince) {
81  m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
82  << ", because is less or equal to last available since = " << lastSince;
83  if (m_autoCommit)
85  scope.close();
86  return thePayloadHash;
87  }
88  }
89  thePayloadHash = m_session.storePayload(payload);
90  std::string payloadType = cond::demangledName(typeid(payload));
91  if (newTag) {
92  createNewIOV(thePayloadHash, payloadType, time, myrecord);
93  } else {
94  appendSinceTime(thePayloadHash, time, myrecord);
95  }
96  if (m_autoCommit) {
98  }
99  } catch (const std::exception& er) {
100  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeOne");
101  }
102  scope.close();
103  return thePayloadHash;
104  }
105 
106  template <typename T>
107  void writeMany(const std::map<Time_t, std::shared_ptr<T> >& iovAndPayloads, const std::string& recordName) {
108  if (iovAndPayloads.empty())
109  return;
110  std::lock_guard<std::recursive_mutex> lock(m_mutex);
113  try {
114  this->initDB();
115  auto& myrecord = this->getRecord(recordName);
116  m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
117  bool newTag = isNewTagRequest(recordName);
118  cond::Time_t lastSince = 0;
120  if (newTag) {
121  std::string payloadType = cond::demangledName(typeid(T));
122  editor = m_session.createIov(payloadType, myrecord.m_tag, myrecord.m_timetype, cond::SYNCH_ANY);
123  editor.setDescription("New Tag");
124  } else {
125  editor = m_session.editIov(myrecord.m_tag);
126  if (myrecord.m_onlyAppendUpdatePolicy) {
127  cond::TagInfo_t tInfo;
128  this->getTagInfo(myrecord.m_idName, tInfo);
129  lastSince = tInfo.lastInterval.since;
130  if (lastSince == cond::time::MAX_VAL)
131  lastSince = 0;
132  }
133  }
134  for (auto& iovEntry : iovAndPayloads) {
135  Time_t time = iovEntry.first;
136  auto payload = iovEntry.second;
137  if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
138  if (time <= lastSince) {
139  m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
140  << ", because is less or equal to last available since = " << lastSince;
141  continue;
142  }
143  }
144  auto payloadHash = m_session.storePayload(*payload);
145  editor.insert(time, payloadHash);
146  }
147  cond::UserLogInfo a = this->lookUpUserLogInfo(myrecord.m_idName);
148  editor.flush(a.usertext);
149  if (m_autoCommit) {
151  }
152  } catch (const std::exception& er) {
153  cond::throwException(std::string(er.what()), "PoolDBOutputService::writeMany");
154  }
155  scope.close();
156  return;
157  }
158 
159  // close the IOVSequence setting lastTill
160  void closeIOV(Time_t lastTill, const std::string& recordName);
161 
162  template <typename T>
163  void createOneIOV(const T& payload, cond::Time_t firstSinceTime, const std::string& recordName) {
164  std::lock_guard<std::recursive_mutex> lock(m_mutex);
167  try {
168  this->initDB();
169  auto& myrecord = this->getRecord(recordName);
170  if (!myrecord.m_isNewTag) {
171  cond::throwException(myrecord.m_tag + " is not a new tag", "PoolDBOutputService::createNewIOV");
172  }
173  Hash payloadId = m_session.storePayload(payload);
174  createNewIOV(payloadId, cond::demangledName(typeid(payload)), firstSinceTime, myrecord);
175  if (m_autoCommit) {
177  }
178  } catch (const std::exception& er) {
179  cond::throwException(std::string(er.what()), "PoolDBOutputService::createNewIov");
180  }
181  scope.close();
182  }
183 
184  template <typename T>
185  void appendOneIOV(const T& payload, cond::Time_t sinceTime, const std::string& recordName) {
186  std::lock_guard<std::recursive_mutex> lock(m_mutex);
189  try {
190  bool dbexists = this->initDB(true);
191  if (!dbexists) {
192  cond::throwException(std::string("Target database does not exist."),
193  "PoolDBOutputService::appendSinceTime");
194  }
195  auto& myrecord = this->lookUpRecord(recordName);
196  if (myrecord.m_isNewTag) {
197  cond::throwException(std::string("Cannot append to non-existing tag ") + myrecord.m_tag,
198  "PoolDBOutputService::appendSinceTime");
199  }
200  appendSinceTime(m_session.storePayload(payload), sinceTime, myrecord);
201  if (m_autoCommit) {
203  }
204  } catch (const std::exception& er) {
205  cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
206  }
207  scope.close();
208  }
209 
210  void createNewIOV(const std::string& firstPayloadId, cond::Time_t firstSinceTime, const std::string& recordName);
211 
212  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
213 
214  // Remove the payload and its valid sinceTime from the database
215  //
216  void eraseSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
217 
218  //
219  // Service time utility method
220  // return the infinity value according to the given timetype
221  //
222  cond::Time_t endOfTime() const;
223  //
224  // Service time utility method
225  // return beginning of time value according to the given timetype
226  //
227  cond::Time_t beginOfTime() const;
228  //
229  // Service time utility method
230  // return the time value of the current edm::Event according to the
231  // given timetype
232  //
233  cond::Time_t currentTime() const;
234 
235  // optional. User can inject additional information into the log associated with a given record
237  const std::string& provenance,
238  const std::string& usertext);
239 
240  // Retrieve tag information
242 
243  void forceInit();
244 
246 
247  struct Record {
250 
256  unsigned int m_refreshTime = 0;
258  };
259 
260  const Record& lookUpRecord(const std::string& recordName);
261 
262  private:
263  //
264  void doStartTransaction();
265  void doCommitTransaction();
266 
267  //
269 
270  //
271  void createNewIOV(const std::string& firstPayloadId,
272  const std::string payloadType,
273  cond::Time_t firstSinceTime,
274  Record& record);
275 
276  // Append the payload and its valid sinceTime into the database
277  // Note: the iov index appended to MUST pre-existing and the existing
278  // conditions data are retrieved from the DB
279  //
280  bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const Record& record);
281 
282  //use these to control transaction interval
288 
289  void fillRecord(edm::ParameterSet& pset, const std::string& gTimeTypeStr);
290 
291  bool initDB(bool readOnly = false);
292 
294 
296 
297  private:
299  std::recursive_mutex m_mutex;
301  std::vector<cond::Time_t> m_currentTimes;
302 
307  unsigned int m_writeTransactionDelay = 0;
309 
310  std::map<std::string, Record> m_records;
311  std::map<std::string, cond::UserLogInfo> m_logheaders;
312 
313  }; //PoolDBOutputService
314  } // namespace service
315 } // namespace cond
316 #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
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
static std::string to_string(const XMLCh *ch)
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
static const std::string kSharedResource
void writeMany(const std::map< Time_t, std::shared_ptr< T > > &iovAndPayloads, const std::string &recordName)
HLT enums.
cond::persistency::Session newReadOnlySession(const std::string &connectionString, const std::string &transactionId)
double a
Definition: hdecay.h:121
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