1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
#ifndef CondCore_PoolDBOutputService_h
#define CondCore_PoolDBOutputService_h
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "CondCore/CondDB/interface/ConnectionPool.h"
#include "CondCore/CondDB/interface/Session.h"
#include "CondCore/CondDB/interface/Logger.h"
#include <string>
#include <map>
#include <mutex>
//
// Package: DBOutputService
// Class : PoolDBOutputService
//
/**\class PoolDBOutputService PoolDBOutputService.h CondCore/DBOutputService/interface/PoolDBOutputService.h
Description: edm service for writing conditions object to DB.
*/
//
// Author: Zhen Xie
// Fixes and other changes: Giacomo Govi
//
namespace edm {
class Event;
class EventSetup;
class ParameterSet;
} // namespace edm
namespace cond {
namespace service {
class PoolDBOutputService {
public:
PoolDBOutputService(const edm::ParameterSet& iConfig, edm::ActivityRegistry& iAR);
static const std::string kSharedResource;
virtual ~PoolDBOutputService();
//use these to control connections
void postEndJob();
cond::persistency::Session newReadOnlySession(const std::string& connectionString,
const std::string& transactionId);
// return the database session in use
cond::persistency::Session session() const;
//
void lockRecords();
//
void releaseLocks();
//
void startTransaction();
void commitTransaction();
//
std::string tag(const std::string& recordName);
bool isNewTagRequest(const std::string& recordName);
template <typename T>
Hash writeOneIOV(const T& payload, Time_t time, const std::string& recordName) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
doStartTransaction();
cond::persistency::TransactionScope scope(m_session.transaction());
Hash thePayloadHash("");
try {
this->initDB();
auto& myrecord = this->getRecord(recordName);
m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
bool newTag = isNewTagRequest(recordName);
if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
cond::TagInfo_t tInfo;
this->getTagInfo(myrecord.m_idName, tInfo);
cond::Time_t lastSince = tInfo.lastInterval.since;
if (lastSince == cond::time::MAX_VAL)
lastSince = 0;
if (time <= lastSince) {
m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
<< ", because is less or equal to last available since = " << lastSince;
if (m_autoCommit)
doCommitTransaction();
scope.close();
return thePayloadHash;
}
}
thePayloadHash = m_session.storePayload(payload);
std::string payloadType = cond::demangledName(typeid(payload));
if (newTag) {
createNewIOV(thePayloadHash, payloadType, time, myrecord);
} else {
appendSinceTime(thePayloadHash, time, myrecord);
}
if (m_autoCommit) {
doCommitTransaction();
}
} catch (const std::exception& er) {
cond::throwException(std::string(er.what()), "PoolDBOutputService::writeOne");
}
scope.close();
return thePayloadHash;
}
template <typename T>
void writeMany(const std::map<Time_t, std::shared_ptr<T> >& iovAndPayloads, const std::string& recordName) {
if (iovAndPayloads.empty())
return;
std::lock_guard<std::recursive_mutex> lock(m_mutex);
doStartTransaction();
cond::persistency::TransactionScope scope(m_session.transaction());
try {
this->initDB();
auto& myrecord = this->getRecord(recordName);
m_logger.logInfo() << "Tag mapped to record " << recordName << ": " << myrecord.m_tag;
bool newTag = isNewTagRequest(recordName);
cond::Time_t lastSince = 0;
cond::persistency::IOVEditor editor;
if (newTag) {
std::string payloadType = cond::demangledName(typeid(T));
editor = m_session.createIov(payloadType, myrecord.m_tag, myrecord.m_timetype, cond::SYNCH_ANY);
editor.setDescription("New Tag");
} else {
editor = m_session.editIov(myrecord.m_tag);
if (myrecord.m_onlyAppendUpdatePolicy) {
cond::TagInfo_t tInfo;
this->getTagInfo(myrecord.m_idName, tInfo);
lastSince = tInfo.lastInterval.since;
if (lastSince == cond::time::MAX_VAL)
lastSince = 0;
}
}
for (auto& iovEntry : iovAndPayloads) {
Time_t time = iovEntry.first;
auto payload = iovEntry.second;
if (myrecord.m_onlyAppendUpdatePolicy && !newTag) {
if (time <= lastSince) {
m_logger.logInfo() << "Won't append iov with since " << std::to_string(time)
<< ", because is less or equal to last available since = " << lastSince;
continue;
}
}
auto payloadHash = m_session.storePayload(*payload);
editor.insert(time, payloadHash);
}
cond::UserLogInfo a = this->lookUpUserLogInfo(myrecord.m_idName);
editor.flush(a.usertext);
if (m_autoCommit) {
doCommitTransaction();
}
} catch (const std::exception& er) {
cond::throwException(std::string(er.what()), "PoolDBOutputService::writeMany");
}
scope.close();
return;
}
// close the IOVSequence setting lastTill
void closeIOV(Time_t lastTill, const std::string& recordName);
template <typename T>
void createOneIOV(const T& payload, cond::Time_t firstSinceTime, const std::string& recordName) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
doStartTransaction();
cond::persistency::TransactionScope scope(m_session.transaction());
try {
this->initDB();
auto& myrecord = this->getRecord(recordName);
if (!myrecord.m_isNewTag) {
cond::throwException(myrecord.m_tag + " is not a new tag", "PoolDBOutputService::createNewIOV");
}
Hash payloadId = m_session.storePayload(payload);
createNewIOV(payloadId, cond::demangledName(typeid(payload)), firstSinceTime, myrecord);
if (m_autoCommit) {
doCommitTransaction();
}
} catch (const std::exception& er) {
cond::throwException(std::string(er.what()), "PoolDBOutputService::createNewIov");
}
scope.close();
}
template <typename T>
void appendOneIOV(const T& payload, cond::Time_t sinceTime, const std::string& recordName) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
doStartTransaction();
cond::persistency::TransactionScope scope(m_session.transaction());
try {
bool dbexists = this->initDB(true);
if (!dbexists) {
cond::throwException(std::string("Target database does not exist."),
"PoolDBOutputService::appendSinceTime");
}
auto& myrecord = this->lookUpRecord(recordName);
if (myrecord.m_isNewTag) {
cond::throwException(std::string("Cannot append to non-existing tag ") + myrecord.m_tag,
"PoolDBOutputService::appendSinceTime");
}
appendSinceTime(m_session.storePayload(payload), sinceTime, myrecord);
if (m_autoCommit) {
doCommitTransaction();
}
} catch (const std::exception& er) {
cond::throwException(std::string(er.what()), "PoolDBOutputService::appendSinceTime");
}
scope.close();
}
void createNewIOV(const std::string& firstPayloadId, cond::Time_t firstSinceTime, const std::string& recordName);
bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
// Remove the payload and its valid sinceTime from the database
//
void eraseSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const std::string& recordName);
//
// Service time utility method
// return the infinity value according to the given timetype
//
cond::Time_t endOfTime() const;
//
// Service time utility method
// return beginning of time value according to the given timetype
//
cond::Time_t beginOfTime() const;
//
// Service time utility method
// return the time value of the current edm::Event according to the
// given timetype
//
cond::Time_t currentTime() const;
// optional. User can inject additional information into the log associated with a given record
void setLogHeaderForRecord(const std::string& recordName,
const std::string& provenance,
const std::string& usertext);
// Retrieve tag information
bool tagInfo(const std::string& recordName, cond::TagInfo_t& result);
void forceInit();
cond::persistency::Logger& logger() { return m_logger; }
struct Record {
Record()
: m_tag(), m_isNewTag(true), m_idName(), m_timetype(cond::runnumber), m_onlyAppendUpdatePolicy(false) {}
std::string timetypestr() const { return cond::timeTypeSpecs[m_timetype].name; }
std::string m_tag;
bool m_isNewTag;
std::string m_idName;
cond::TimeType m_timetype;
unsigned int m_refreshTime = 0;
bool m_onlyAppendUpdatePolicy;
};
const Record& lookUpRecord(const std::string& recordName);
private:
//
void doStartTransaction();
void doCommitTransaction();
//
bool getTagInfo(const std::string& recordName, cond::TagInfo_t& result);
//
void createNewIOV(const std::string& firstPayloadId,
const std::string payloadType,
cond::Time_t firstSinceTime,
Record& record);
// Append the payload and its valid sinceTime into the database
// Note: the iov index appended to MUST pre-existing and the existing
// conditions data are retrieved from the DB
//
bool appendSinceTime(const std::string& payloadId, cond::Time_t sinceTime, const Record& record);
//use these to control transaction interval
void preEventProcessing(edm::StreamContext const&);
void preGlobalBeginLumi(edm::GlobalContext const&);
void preGlobalBeginRun(edm::GlobalContext const&);
void preModuleEvent(edm::StreamContext const&, edm::ModuleCallingContext const&);
void postModuleEvent(edm::StreamContext const&, edm::ModuleCallingContext const&);
void fillRecord(edm::ParameterSet& pset, const std::string& gTimeTypeStr);
bool initDB(bool readOnly = false);
Record& getRecord(const std::string& recordName);
cond::UserLogInfo& lookUpUserLogInfo(const std::string& recordName);
private:
cond::persistency::Logger m_logger;
std::recursive_mutex m_mutex;
cond::TimeType m_timetype;
std::vector<cond::Time_t> m_currentTimes;
cond::persistency::ConnectionPool m_connection;
cond::persistency::Session m_session;
bool m_transactionActive;
bool m_autoCommit;
unsigned int m_writeTransactionDelay = 0;
bool m_dbInitialised;
std::map<std::string, Record> m_records;
std::map<std::string, cond::UserLogInfo> m_logheaders;
}; //PoolDBOutputService
} // namespace service
} // namespace cond
#endif
|