CMS 3D CMS Logo

IOVEditor.cc
Go to the documentation of this file.
4 #include "SessionImpl.h"
5 //
6 
7 namespace cond {
8 
9  namespace persistency {
10 
11  // implementation details. holds only data.
12  class IOVEditorData {
13  public:
14  explicit IOVEditorData()
15  : tag(""),
16  timeType(cond::invalid),
17  payloadType(""),
19  description(""),
20  iovBuffer(),
21  deleteBuffer(),
22  changes() {}
30  boost::posix_time::ptime creationTime;
31  bool change = false;
32  bool metadataChange = false;
33  bool exists = false;
34  // buffer for the iov sequence
35  std::vector<std::tuple<cond::Time_t, cond::Hash, boost::posix_time::ptime> > iovBuffer;
36  std::vector<std::tuple<cond::Time_t, cond::Hash> > deleteBuffer;
37  std::set<std::string> changes;
38  int protectionCode = 0;
39  };
40 
41  IOVEditor::IOVEditor() : m_data(), m_session() {}
42 
43  IOVEditor::IOVEditor(const std::shared_ptr<SessionImpl>& session) : m_data(new IOVEditorData), m_session(session) {}
44 
45  IOVEditor::IOVEditor(const std::shared_ptr<SessionImpl>& session,
46  const std::string& tag,
47  cond::TimeType timeType,
48  const std::string& payloadObjectType,
49  cond::SynchronizationType synchronizationType,
50  const boost::posix_time::ptime& creationTime)
51  : m_data(new IOVEditorData), m_session(session) {
52  m_data->tag = tag;
53  m_data->timeType = timeType;
54  m_data->payloadType = payloadObjectType;
55  m_data->synchronizationType = synchronizationType;
56  m_data->creationTime = creationTime;
57  m_data->change = true;
58  m_data->metadataChange = true;
59  }
60 
61  IOVEditor::IOVEditor(const IOVEditor& rhs) : m_data(rhs.m_data), m_session(rhs.m_session) {}
62 
64 
66  m_data = rhs.m_data;
67  m_session = rhs.m_session;
68  return *this;
69  }
70 
72  checkTransaction("IOVEditor::load");
73  // loads the current header data in memory
74  if (!m_session->iovSchema().tagTable().select(tag,
75  m_data->timeType,
76  m_data->payloadType,
77  m_data->synchronizationType,
78  m_data->endOfValidity,
79  m_data->lastValidatedTime,
80  m_data->protectionCode)) {
81  cond::throwException("Tag \"" + tag + "\" has not been found in the database.", "IOVEditor::load");
82  }
83  if (m_data->protectionCode) {
84  if (m_data->protectionCode & cond::auth::COND_DBTAG_WRITE_ACCESS_CODE) {
85  bool writeAllowed = m_session->iovSchema().tagAccessPermissionTable().getAccessPermission(
86  tag,
87  m_session->principalName,
90  if (!writeAllowed)
92  "Tag \"" + tag + "\" can't be accessed for update by db-user \"" + m_session->principalName + "\".",
93  "IOVEditor::load");
94  }
95  if (m_data->protectionCode & cond::auth::COND_DBTAG_LOCK_ACCESS_CODE) {
96  bool mylock = m_session->iovSchema().tagAccessPermissionTable().getAccessPermission(
98  if (!mylock)
100  "Tag \"" + tag + "\" can't be accessed for update, because it has been locked by an other session.",
101  "IOVEditor::load");
102  }
103  }
104  m_data->tag = tag;
105  m_data->exists = true;
106  m_data->change = false;
107  }
108 
109  std::string IOVEditor::tag() const { return m_data.get() ? m_data->tag : ""; }
110 
111  cond::TimeType IOVEditor::timeType() const { return m_data.get() ? m_data->timeType : cond::invalid; }
112 
113  std::string IOVEditor::payloadType() const { return m_data.get() ? m_data->payloadType : ""; }
114 
116  return m_data.get() ? m_data->synchronizationType : cond::SYNCH_ANY;
117  }
118 
120  if (m_data.get()) {
121  m_data->synchronizationType = synchronizationType;
122  m_data->change = true;
123  m_data->changes.insert("SynchronizationType");
124  }
125  }
126 
127  cond::Time_t IOVEditor::endOfValidity() const { return m_data.get() ? m_data->endOfValidity : cond::time::MIN_VAL; }
128 
130  if (m_data.get()) {
131  m_data->endOfValidity = time;
132  m_data->change = true;
133  m_data->changes.insert("EndOfValidity");
134  }
135  }
136 
137  std::string IOVEditor::description() const { return m_data.get() ? m_data->description : ""; }
138 
140  if (m_data.get()) {
141  m_data->description = description;
142  m_data->metadataChange = true;
143  m_data->changes.insert("Description");
144  }
145  }
146 
148  return m_data.get() ? m_data->lastValidatedTime : cond::time::MIN_VAL;
149  }
150 
152  if (m_data.get()) {
153  m_data->lastValidatedTime = time;
154  m_data->change = true;
155  m_data->changes.insert("LastValidatedTime");
156  }
157  }
158 
159  void IOVEditor::insert(cond::Time_t since, const cond::Hash& payloadHash, bool checkType) {
160  boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
161  insert(since, payloadHash, now, checkType);
162  }
163 
165  const cond::Hash& payloadHash,
166  const boost::posix_time::ptime& insertionTime,
167  bool) {
168  if (m_data.get()) {
169  // here the type check could be added
170  m_data->iovBuffer.push_back(std::tie(since, payloadHash, insertionTime));
171  }
172  }
173 
174  void IOVEditor::erase(cond::Time_t since, const cond::Hash& payloadHash) {
175  if (m_data.get()) {
176  m_data->deleteBuffer.push_back(std::tie(since, payloadHash));
177  }
178  }
179 
180  bool iovSorter(const std::tuple<cond::Time_t, cond::Hash, boost::posix_time::ptime>& f,
181  const std::tuple<cond::Time_t, cond::Hash, boost::posix_time::ptime>& s) {
182  return std::get<0>(f) < std::get<0>(s);
183  }
184 
185  bool IOVEditor::flush(const std::string& logText,
186  const boost::posix_time::ptime& operationTime,
187  bool forceInsertion) {
188  bool ret = false;
189  checkTransaction("IOVEditor::flush");
190  std::string lt = logText;
191  if (lt.empty())
192  lt = "-";
193  if (m_data->change || m_data->metadataChange) {
194  if (m_data->metadataChange && m_data->description.empty())
195  throwException("A non-empty description string is mandatory.", "IOVEditor::flush");
196  //if (m_data->validationMode)
197  // m_session->iovSchema().tagTable().setValidationMode();
198  if (!m_data->exists) {
199  // set the creation time ( only available in the migration from v1...)
200  if (m_data->creationTime.is_not_a_date_time())
201  m_data->creationTime = operationTime;
202  m_session->iovSchema().tagTable().insert(m_data->tag,
203  m_data->timeType,
204  m_data->payloadType,
205  m_data->synchronizationType,
206  m_data->endOfValidity,
207  m_data->description,
208  m_data->lastValidatedTime,
209  m_data->creationTime);
210  if (m_session->iovSchema().tagLogTable().exists())
211  m_session->iovSchema().tagLogTable().insert(m_data->tag,
212  m_data->creationTime,
213  cond::getUserName(),
214  cond::getHostName(),
215  cond::getCommand(),
216  std::string("New tag created."),
217  lt);
218  m_data->exists = true;
219  ret = true;
220  } else {
221  if (m_data->change) {
222  m_session->iovSchema().tagTable().update(m_data->tag,
223  m_data->synchronizationType,
224  m_data->endOfValidity,
225  m_data->lastValidatedTime,
226  operationTime);
227  }
228  if (m_data->metadataChange) {
229  m_session->iovSchema().tagTable().updateMetadata(m_data->tag, m_data->description, operationTime);
230  }
231  if (m_session->iovSchema().tagLogTable().exists()) {
232  std::string action("Tag header updated. Changes involve: ");
233  size_t i = 0;
234  for (const auto& c : m_data->changes) {
235  action += c;
236  if (i == (m_data->changes.size() - 1))
237  action += ".";
238  else
239  action += ", ";
240  i++;
241  }
242  m_session->iovSchema().tagLogTable().insert(
243  m_data->tag, operationTime, cond::getUserName(), cond::getHostName(), cond::getCommand(), action, lt);
244  }
245  ret = true;
246  }
247  m_data->change = false;
248  }
249  if (!m_data->iovBuffer.empty()) {
250  std::sort(m_data->iovBuffer.begin(), m_data->iovBuffer.end(), iovSorter);
251  cond::Time_t l = std::get<0>(m_data->iovBuffer.front());
252  //We do not allow for IOV updates (i.e. insertion in the past or overriding) on tags whose syncrosization is not "ANY" or "VALIDATION".
253  //This policy is stricter than the one deployed in the Condition Upload service,
254  //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").
255  //This is intended: in the C++ API we have no way to determine the first condition safe runs.
256  if (!forceInsertion && m_data->synchronizationType != cond::SYNCH_ANY &&
257  m_data->synchronizationType != cond::SYNCH_VALIDATION) {
258  // retrieve the last since
259  cond::Time_t last = 0;
260  cond::Hash h;
261  boost::posix_time::ptime no_time;
262  m_session->iovSchema().iovTable().getLastIov(m_data->tag, no_time, last, h);
263  // check if the min iov is greater then the last since
264  if (l <= last) {
265  std::stringstream msg;
266  msg << "Can't insert iov since " << l << " on the tag " << m_data->tag << ": last since is " << last
267  << " and synchronization is \"" << cond::synchronizationTypeNames(m_data->synchronizationType) << "\"";
268  throwException(msg.str(), "IOVEditor::flush");
269  }
270  }
271  // set the insertion time ( only for the migration from v1 will be available... )
272  for (auto& iov : m_data->iovBuffer) {
273  boost::posix_time::ptime& insertionTime = std::get<2>(iov);
274  if (insertionTime.is_not_a_date_time())
275  insertionTime = operationTime;
276  }
277  // insert the new iovs
278  m_session->iovSchema().iovTable().insertMany(m_data->tag, m_data->iovBuffer);
279  ret = true;
280  }
281  if (!m_data->deleteBuffer.empty()) {
282  // delete the specified iovs
283  m_session->iovSchema().iovTable().eraseMany(m_data->tag, m_data->deleteBuffer);
284  ret = true;
285  }
286  if (m_session->iovSchema().tagLogTable().exists()) {
287  std::stringstream msg;
288  if (!m_data->iovBuffer.empty())
289  msg << m_data->iovBuffer.size() << " iov(s) inserted";
290  if (!msg.str().empty())
291  msg << "; ";
292  else
293  msg << ".";
294  if (!m_data->deleteBuffer.empty())
295  msg << m_data->deleteBuffer.size() << " iov(s) deleted.";
296  if (ret) {
297  m_session->iovSchema().tagLogTable().insert(
298  m_data->tag, operationTime, cond::getUserName(), cond::getHostName(), cond::getCommand(), msg.str(), lt);
299  }
300  }
301  m_data->iovBuffer.clear();
302  m_data->deleteBuffer.clear();
303  m_data->changes.clear();
304  return ret;
305  }
306 
307  bool IOVEditor::flush(const std::string& logText) {
308  return flush(logText, boost::posix_time::microsec_clock::universal_time(), false);
309  }
310 
311  bool IOVEditor::flush(const boost::posix_time::ptime& operationTime) {
312  return flush(std::string("-"), operationTime, false);
313  }
314 
316  return flush(std::string("-"), boost::posix_time::microsec_clock::universal_time(), false);
317  }
318 
319  bool IOVEditor::flush(const std::string& logText, bool forceInsertion) {
320  return flush(logText, boost::posix_time::microsec_clock::universal_time(), forceInsertion);
321  }
322 
323  bool IOVEditor::isLocked() const { return m_data->protectionCode & cond::auth::COND_DBTAG_LOCK_ACCESS_CODE; }
324 
326  if (isLocked())
327  return;
328  checkTransaction("IOVEditor::lock");
329  m_session->iovSchema().tagAccessPermissionTable().setAccessPermission(m_data->tag,
330  m_session->sessionHash,
334  m_session->iovSchema().tagTable().setProtectionCode(m_data->tag, cond::auth::COND_DBTAG_LOCK_ACCESS_CODE);
335  m_session->lockedTags.insert(m_data->tag);
336  std::string lt("-");
337  std::string action("Lock set by session ");
338  action += m_session->sessionHash;
339  m_session->iovSchema().tagLogTable().insert(m_data->tag,
340  boost::posix_time::microsec_clock::universal_time(),
341  cond::getUserName(),
342  cond::getHostName(),
343  cond::getCommand(),
344  action,
345  lt);
346  }
347 
349  if (!isLocked())
350  return;
351  checkTransaction("IOVEditor::unlock");
352  m_session->iovSchema().tagAccessPermissionTable().removeAccessPermission(
355  m_session->iovSchema().tagTable().unsetProtectionCode(m_data->tag, cond::auth::COND_DBTAG_LOCK_ACCESS_CODE);
356  m_session->lockedTags.erase(m_data->tag);
357  std::string lt("-");
358  std::string action("Lock released by session ");
359  action += m_session->sessionHash;
360  m_session->iovSchema().tagLogTable().insert(m_data->tag,
361  boost::posix_time::microsec_clock::universal_time(),
362  cond::getUserName(),
363  cond::getHostName(),
364  cond::getCommand(),
365  action,
366  lt);
367  }
368 
370  if (!m_session.get())
371  throwException("The session is not active.", ctx);
372  if (!m_session->isTransactionActive(false))
373  throwException("The transaction is not active.", ctx);
374  }
375 
376  } // namespace persistency
377 } // namespace cond
std::string payloadType() const
Definition: IOVEditor.cc:113
std::string synchronizationTypeNames(SynchronizationType type)
Definition: Types.cc:45
ret
prodAgent to be discontinued
cond::Time_t lastValidatedTime() const
Definition: IOVEditor.cc:147
void checkTransaction(const std::string &ctx)
Definition: IOVEditor.cc:369
static constexpr int COND_DBTAG_WRITE_ACCESS_CODE
Definition: Auth.h:46
const Time_t MIN_VAL(0)
void load(const std::string &tag)
Definition: IOVEditor.cc:71
void setDescription(const std::string &description)
Definition: IOVEditor.cc:139
std::set< std::string > changes
Definition: IOVEditor.cc:37
void setLastValidatedTime(cond::Time_t time)
Definition: IOVEditor.cc:151
std::shared_ptr< SessionImpl > m_session
Definition: IOVEditor.h:99
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:18
TimeType
Definition: Time.h:19
unsigned long long Time_t
Definition: Time.h:14
std::string tag() const
Definition: IOVEditor.cc:109
cond::Time_t endOfValidity() const
Definition: IOVEditor.cc:127
std::string Hash
Definition: Types.h:43
double f[11][100]
IOVEditor & operator=(const IOVEditor &rhs)
Definition: IOVEditor.cc:65
static constexpr int COND_DBTAG_LOCK_ACCESS_CODE
Definition: Auth.h:45
std::vector< std::tuple< cond::Time_t, cond::Hash, boost::posix_time::ptime > > iovBuffer
Definition: IOVEditor.cc:35
void erase(cond::Time_t since, const cond::Hash &payloadHash)
Definition: IOVEditor.cc:174
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:159
static constexpr int COND_DBKEY_CREDENTIAL_CODE
Definition: Auth.h:43
std::shared_ptr< IOVEditorData > m_data
Definition: IOVEditor.h:98
tuple msg
Definition: mps_check.py:286
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)
Definition: IOVEditor.cc:180
std::string description() const
Definition: IOVEditor.cc:137
boost::posix_time::ptime creationTime
Definition: IOVEditor.cc:30
cond::SynchronizationType synchronizationType
Definition: IOVEditor.cc:26
cond::TimeType timeType() const
Definition: IOVEditor.cc:111
SynchronizationType
Definition: Types.h:27
void setEndOfValidity(cond::Time_t validity)
Definition: IOVEditor.cc:129
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void setSynchronizationType(cond::SynchronizationType synchronizationType)
Definition: IOVEditor.cc:119
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
static constexpr int COND_SESSION_HASH_CODE
Definition: Auth.h:42
std::vector< std::tuple< cond::Time_t, cond::Hash > > deleteBuffer
Definition: IOVEditor.cc:36
cond::SynchronizationType synchronizationType() const
Definition: IOVEditor.cc:115