forked from cms-sw/cmssw
/
IOVEditor.cc
241 lines (211 loc) · 9.1 KB
/
IOVEditor.cc
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
#include "CondCore/CondDB/interface/IOVEditor.h"
#include "CondCore/CondDB/interface/Utils.h"
#include "SessionImpl.h"
//
namespace cond {
namespace persistency {
// implementation details. holds only data.
class IOVEditorData {
public:
explicit IOVEditorData():
tag( "" ),
timeType( cond::invalid ),
payloadType(""),
synchronizationType( cond::SYNCH_ANY ),
description(""),
iovBuffer(){
}
std::string tag;
cond::TimeType timeType;
std::string payloadType;
cond::SynchronizationType synchronizationType;
std::string description;
cond::Time_t endOfValidity = cond::time::MAX_VAL;
cond::Time_t lastValidatedTime = cond::time::MIN_VAL;
boost::posix_time::ptime creationTime;
bool change = false;
bool exists = false;
// buffer for the iov sequence
std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> > iovBuffer;
bool validationMode = false;
};
IOVEditor::IOVEditor():
m_data(),
m_session(){
}
IOVEditor::IOVEditor( const std::shared_ptr<SessionImpl>& session ):
m_data( new IOVEditorData ),
m_session( session ){
}
IOVEditor::IOVEditor( const std::shared_ptr<SessionImpl>& session,
const std::string& tag,
cond::TimeType timeType,
const std::string& payloadObjectType,
cond::SynchronizationType synchronizationType,
const boost::posix_time::ptime& creationTime ):
m_data( new IOVEditorData ),
m_session( session ){
m_data->tag = tag;
m_data->timeType = timeType;
m_data->payloadType = payloadObjectType;
m_data->synchronizationType = synchronizationType;
m_data->creationTime = creationTime;
m_data->change = true;
}
IOVEditor::IOVEditor( const IOVEditor& rhs ):
m_data( rhs.m_data ),
m_session( rhs.m_session ){
}
IOVEditor& IOVEditor::operator=( const IOVEditor& rhs ){
m_data = rhs.m_data;
m_session = rhs.m_session;
return *this;
}
void IOVEditor::load( const std::string& tag ){
checkTransaction( "IOVEditor::load" );
// loads the current header data in memory
if( !m_session->iovSchema().tagTable().select( tag, m_data->timeType, m_data->payloadType, m_data->synchronizationType,
m_data->endOfValidity, m_data->description, m_data->lastValidatedTime ) ){
cond::throwException( "Tag \""+tag+"\" has not been found in the database.","IOVEditor::load");
}
m_data->tag = tag;
m_data->exists = true;
m_data->change = false;
}
std::string IOVEditor::tag() const {
return m_data.get()? m_data->tag : "" ;
}
cond::TimeType IOVEditor::timeType() const {
return m_data.get() ? m_data->timeType : cond::invalid;
}
std::string IOVEditor::payloadType() const {
return m_data.get() ? m_data->payloadType : "";
}
cond::SynchronizationType IOVEditor::synchronizationType() const {
return m_data.get()? m_data->synchronizationType : cond::SYNCH_ANY ;
}
cond::Time_t IOVEditor::endOfValidity() const {
return m_data.get() ? m_data->endOfValidity : cond::time::MIN_VAL;
}
void IOVEditor::setEndOfValidity( cond::Time_t time ){
if( m_data.get() ) {
m_data->endOfValidity = time;
m_data->change = true;
}
}
std::string IOVEditor::description() const {
return m_data.get() ? m_data->description : "";
}
void IOVEditor::setDescription( const std::string& description ){
if( m_data.get() ) {
m_data->description = description;
m_data->change = true;
}
}
cond::Time_t IOVEditor::lastValidatedTime() const {
return m_data.get() ? m_data->lastValidatedTime : cond::time::MIN_VAL;
}
void IOVEditor::setLastValidatedTime(cond::Time_t time ){
if( m_data.get() ) {
m_data->lastValidatedTime = time;
m_data->change = true;
}
}
void IOVEditor::setValidationMode(){
if( m_data.get() ) m_data->validationMode = true;
}
void IOVEditor::insert( cond::Time_t since, const cond::Hash& payloadHash, bool checkType ){
boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
insert( since, payloadHash, now, checkType );
}
void IOVEditor::insert( cond::Time_t since, const cond::Hash& payloadHash, const boost::posix_time::ptime& insertionTime, bool ){
if( m_data.get() ){
// here the type check could be added
m_data->iovBuffer.push_back( std::tie( since, payloadHash, insertionTime ) );
}
}
bool iovSorter( const std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime>& f, const std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime>& s ){
return std::get<0>(f) < std::get<0>(s);
}
bool IOVEditor::flush( const std::string& logText, const boost::posix_time::ptime& operationTime ){
bool ret = false;
checkTransaction( "IOVEditor::flush" );
if( m_data->change ){
if( m_data->description.empty() ) throwException( "A non-empty description string is mandatory.","IOVEditor::flush" );
if( m_data->validationMode ) m_session->iovSchema().tagTable().setValidationMode();
if( !m_data->exists ){
// set the creation time ( only available in the migration from v1...)
if( m_data->creationTime.is_not_a_date_time() ) m_data->creationTime = operationTime;
m_session->iovSchema().tagTable().insert( m_data->tag, m_data->timeType, m_data->payloadType,
m_data->synchronizationType, m_data->endOfValidity,
m_data->description, m_data->lastValidatedTime, m_data->creationTime );
if( m_session->iovSchema().tagLogTable().exists() )
m_session->iovSchema().tagLogTable().insert( m_data->tag, m_data->creationTime, cond::getUserName(),cond::getHostName(), cond::getCommand(),
std::string("New tag created."),std::string("-") );
m_data->exists = true;
ret = true;
} else {
m_session->iovSchema().tagTable().update( m_data->tag, m_data->endOfValidity, m_data->description,
m_data->lastValidatedTime, operationTime );
if( m_session->iovSchema().tagLogTable().exists() )
m_session->iovSchema().tagLogTable().insert( m_data->tag, operationTime, cond::getUserName(),cond::getHostName(), cond::getCommand(),
std::string("Tag header updated."),std::string("-") );
ret = true;
}
m_data->change = false;
}
if( m_data->iovBuffer.size() ) {
std::sort(m_data->iovBuffer.begin(),m_data->iovBuffer.end(),iovSorter);
cond::Time_t l = std::get<0>(m_data->iovBuffer.front());
//We do not allow for IOV updates (i.e. insertion in the past or overriding) on tags whose syncrosization is not "ANY" or "VALIDATION".
//This policy is stricter than the one deployed in the Condition Upload service,
//which allows insertions in the past or overriding for IOVs larger than the first condition safe run for HLT ("HLT"/"EXPRESS" synchronizations) and Tier0 ("PROMPT"/"PCL").
//This is intended: in the C++ API we have not got a way to determine the first condition safe runs.
if( m_data->synchronizationType != cond::SYNCH_ANY && m_data->synchronizationType != cond::SYNCH_VALIDATION ){
// retrieve the last since
cond::Time_t last = 0;
cond::Hash h;
m_session->iovSchema().iovTable().getLastIov( m_data->tag, last, h );
// check if the min iov is greater then the last since
if( l <= last ){
std::stringstream msg;
msg << "Can't insert iov since "<<l<<" on the tag "<< m_data->tag<<": last since is "<<last<<
" and synchronization is \""<<cond::synchronizationTypeNames( m_data->synchronizationType )<<"\"";
throwException( msg.str(),"IOVEditor::flush");
}
}
// set the insertion time ( only for the migration from v1 will be available... )
for( auto& iov : m_data->iovBuffer ){
boost::posix_time::ptime& insertionTime = std::get<2>(iov);
if( insertionTime.is_not_a_date_time() ) insertionTime = operationTime;
}
// insert the new iovs
m_session->iovSchema().iovTable().insertMany( m_data->tag, m_data->iovBuffer );
std::stringstream msg;
msg << m_data->iovBuffer.size() << " iov(s) inserted.";
if( m_session->iovSchema().tagLogTable().exists() ){
std::string lt = logText;
if( lt.empty() ) lt = "-";
m_session->iovSchema().tagLogTable().insert( m_data->tag, operationTime, cond::getUserName(), cond::getHostName(), cond::getCommand(),
msg.str(),lt );
}
m_data->iovBuffer.clear();
ret = true;
}
return ret;
}
bool IOVEditor::flush( const std::string& logText ){
return flush( logText, boost::posix_time::microsec_clock::universal_time() );
}
bool IOVEditor::flush( const boost::posix_time::ptime& operationTime ){
return flush( std::string("-"), operationTime );
}
bool IOVEditor::flush(){
return flush( std::string("-"), boost::posix_time::microsec_clock::universal_time() );
}
void IOVEditor::checkTransaction( const std::string& ctx ){
if( !m_session.get() ) throwException("The session is not active.",ctx );
if( !m_session->isTransactionActive( false ) ) throwException("The transaction is not active.",ctx );
}
}
}