CMS 3D CMS Logo

IOVEditor.cc
Go to the documentation of this file.
3 #include "SessionImpl.h"
4 //
5 
6 namespace cond {
7 
8  namespace persistency {
9 
10  // implementation details. holds only data.
11  class IOVEditorData {
12  public:
13  explicit IOVEditorData()
14  : tag(""),
15  timeType(cond::invalid),
16  payloadType(""),
18  description(""),
19  iovBuffer(),
20  changes() {}
28  boost::posix_time::ptime creationTime;
29  bool change = false;
30  bool exists = false;
31  // buffer for the iov sequence
32  std::vector<std::tuple<cond::Time_t, cond::Hash, boost::posix_time::ptime> > iovBuffer;
33  bool validationMode = false;
34  std::set<std::string> changes;
35  };
36 
37  IOVEditor::IOVEditor() : m_data(), m_session() {}
38 
39  IOVEditor::IOVEditor(const std::shared_ptr<SessionImpl>& session) : m_data(new IOVEditorData), m_session(session) {}
40 
41  IOVEditor::IOVEditor(const std::shared_ptr<SessionImpl>& session,
42  const std::string& tag,
44  const std::string& payloadObjectType,
46  const boost::posix_time::ptime& creationTime)
47  : m_data(new IOVEditorData), m_session(session) {
48  m_data->tag = tag;
49  m_data->timeType = timeType;
50  m_data->payloadType = payloadObjectType;
51  m_data->synchronizationType = synchronizationType;
52  m_data->creationTime = creationTime;
53  m_data->change = true;
54  }
55 
57 
59  m_data = rhs.m_data;
60  m_session = rhs.m_session;
61  return *this;
62  }
63 
65  checkTransaction("IOVEditor::load");
66  // loads the current header data in memory
67  if (!m_session->iovSchema().tagTable().select(tag,
68  m_data->timeType,
69  m_data->payloadType,
70  m_data->synchronizationType,
71  m_data->endOfValidity,
72  m_data->description,
73  m_data->lastValidatedTime)) {
74  cond::throwException("Tag \"" + tag + "\" has not been found in the database.", "IOVEditor::load");
75  }
76  m_data->tag = tag;
77  m_data->exists = true;
78  m_data->change = false;
79  }
80 
81  std::string IOVEditor::tag() const { return m_data.get() ? m_data->tag : ""; }
82 
83  cond::TimeType IOVEditor::timeType() const { return m_data.get() ? m_data->timeType : cond::invalid; }
84 
85  std::string IOVEditor::payloadType() const { return m_data.get() ? m_data->payloadType : ""; }
86 
88  return m_data.get() ? m_data->synchronizationType : cond::SYNCH_ANY;
89  }
90 
92  if (m_data.get()) {
93  m_data->synchronizationType = synchronizationType;
94  m_data->change = true;
95  m_data->changes.insert("SynchronizationType");
96  }
97  }
98 
99  cond::Time_t IOVEditor::endOfValidity() const { return m_data.get() ? m_data->endOfValidity : cond::time::MIN_VAL; }
100 
102  if (m_data.get()) {
103  m_data->endOfValidity = time;
104  m_data->change = true;
105  m_data->changes.insert("EndOfValidity");
106  }
107  }
108 
109  std::string IOVEditor::description() const { return m_data.get() ? m_data->description : ""; }
110 
112  if (m_data.get()) {
113  m_data->description = description;
114  m_data->change = true;
115  m_data->changes.insert("Description");
116  }
117  }
118 
120  return m_data.get() ? m_data->lastValidatedTime : cond::time::MIN_VAL;
121  }
122 
124  if (m_data.get()) {
125  m_data->lastValidatedTime = time;
126  m_data->change = true;
127  m_data->changes.insert("LastValidatedTime");
128  }
129  }
130 
132  if (m_data.get())
133  m_data->validationMode = true;
134  }
135 
136  void IOVEditor::insert(cond::Time_t since, const cond::Hash& payloadHash, bool checkType) {
137  boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
138  insert(since, payloadHash, now, checkType);
139  }
140 
142  const cond::Hash& payloadHash,
143  const boost::posix_time::ptime& insertionTime,
144  bool) {
145  if (m_data.get()) {
146  // here the type check could be added
147  m_data->iovBuffer.push_back(std::tie(since, payloadHash, insertionTime));
148  }
149  }
150 
151  bool iovSorter(const std::tuple<cond::Time_t, cond::Hash, boost::posix_time::ptime>& f,
152  const std::tuple<cond::Time_t, cond::Hash, boost::posix_time::ptime>& s) {
153  return std::get<0>(f) < std::get<0>(s);
154  }
155 
156  bool IOVEditor::flush(const std::string& logText,
157  const boost::posix_time::ptime& operationTime,
158  bool forceInsertion) {
159  bool ret = false;
160  checkTransaction("IOVEditor::flush");
161  std::string lt = logText;
162  if (lt.empty())
163  lt = "-";
164  if (m_data->change) {
165  if (m_data->description.empty())
166  throwException("A non-empty description string is mandatory.", "IOVEditor::flush");
167  if (m_data->validationMode)
168  m_session->iovSchema().tagTable().setValidationMode();
169  if (!m_data->exists) {
170  // set the creation time ( only available in the migration from v1...)
171  if (m_data->creationTime.is_not_a_date_time())
172  m_data->creationTime = operationTime;
173  m_session->iovSchema().tagTable().insert(m_data->tag,
174  m_data->timeType,
175  m_data->payloadType,
176  m_data->synchronizationType,
177  m_data->endOfValidity,
178  m_data->description,
179  m_data->lastValidatedTime,
180  m_data->creationTime);
181  if (m_session->iovSchema().tagLogTable().exists())
182  m_session->iovSchema().tagLogTable().insert(m_data->tag,
183  m_data->creationTime,
184  cond::getUserName(),
185  cond::getHostName(),
186  cond::getCommand(),
187  std::string("New tag created."),
188  lt);
189  m_data->exists = true;
190  ret = true;
191  } else {
192  m_session->iovSchema().tagTable().update(m_data->tag,
193  m_data->synchronizationType,
194  m_data->endOfValidity,
195  m_data->description,
196  m_data->lastValidatedTime,
197  operationTime);
198  if (m_session->iovSchema().tagLogTable().exists()) {
199  std::string action("Tag header updated. Changes involve: ");
200  size_t i = 0;
201  for (auto c : m_data->changes) {
202  action += c;
203  if (i == (m_data->changes.size() - 1))
204  action += ".";
205  else
206  action += ", ";
207  i++;
208  }
209  m_session->iovSchema().tagLogTable().insert(
210  m_data->tag, operationTime, cond::getUserName(), cond::getHostName(), cond::getCommand(), action, lt);
211  }
212  ret = true;
213  }
214  m_data->change = false;
215  }
216  if (!m_data->iovBuffer.empty()) {
217  std::sort(m_data->iovBuffer.begin(), m_data->iovBuffer.end(), iovSorter);
218  cond::Time_t l = std::get<0>(m_data->iovBuffer.front());
219  //We do not allow for IOV updates (i.e. insertion in the past or overriding) on tags whose syncrosization is not "ANY" or "VALIDATION".
220  //This policy is stricter than the one deployed in the Condition Upload service,
221  //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").
222  //This is intended: in the C++ API we have not got a way to determine the first condition safe runs.
223  if (!forceInsertion && m_data->synchronizationType != cond::SYNCH_ANY &&
224  m_data->synchronizationType != cond::SYNCH_VALIDATION) {
225  // retrieve the last since
226  cond::Time_t last = 0;
227  cond::Hash h;
228  boost::posix_time::ptime no_time;
229  m_session->iovSchema().iovTable().getLastIov(m_data->tag, no_time, last, h);
230  // check if the min iov is greater then the last since
231  if (l <= last) {
232  std::stringstream msg;
233  msg << "Can't insert iov since " << l << " on the tag " << m_data->tag << ": last since is " << last
234  << " and synchronization is \"" << cond::synchronizationTypeNames(m_data->synchronizationType) << "\"";
235  throwException(msg.str(), "IOVEditor::flush");
236  }
237  }
238  // set the insertion time ( only for the migration from v1 will be available... )
239  for (auto& iov : m_data->iovBuffer) {
240  boost::posix_time::ptime& insertionTime = std::get<2>(iov);
241  if (insertionTime.is_not_a_date_time())
242  insertionTime = operationTime;
243  }
244  // insert the new iovs
245  m_session->iovSchema().iovTable().insertMany(m_data->tag, m_data->iovBuffer);
246  std::stringstream msg;
247  msg << m_data->iovBuffer.size() << " iov(s) inserted.";
248  if (m_session->iovSchema().tagLogTable().exists()) {
249  m_session->iovSchema().tagLogTable().insert(
250  m_data->tag, operationTime, cond::getUserName(), cond::getHostName(), cond::getCommand(), msg.str(), lt);
251  }
252  m_data->iovBuffer.clear();
253  m_data->changes.clear();
254  ret = true;
255  }
256  return ret;
257  }
258 
259  bool IOVEditor::flush(const std::string& logText) {
260  return flush(logText, boost::posix_time::microsec_clock::universal_time(), false);
261  }
262 
263  bool IOVEditor::flush(const boost::posix_time::ptime& operationTime) {
264  return flush(std::string("-"), operationTime, false);
265  }
266 
268  return flush(std::string("-"), boost::posix_time::microsec_clock::universal_time(), false);
269  }
270 
271  bool IOVEditor::flush(const std::string& logText, bool forceInsertion) {
272  return flush(logText, boost::posix_time::microsec_clock::universal_time(), forceInsertion);
273  }
274 
276  if (!m_session.get())
277  throwException("The session is not active.", ctx);
278  if (!m_session->isTransactionActive(false))
279  throwException("The transaction is not active.", ctx);
280  }
281 
282  } // namespace persistency
283 } // namespace cond
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
std::string synchronizationTypeNames(SynchronizationType type)
Definition: Types.cc:44
ret
prodAgent to be discontinued
void checkTransaction(const std::string &ctx)
Definition: IOVEditor.cc:275
const Time_t MIN_VAL(0)
void load(const std::string &tag)
Definition: IOVEditor.cc:64
void setDescription(const std::string &description)
Definition: IOVEditor.cc:111
std::set< std::string > changes
Definition: IOVEditor.cc:34
void setLastValidatedTime(cond::Time_t time)
Definition: IOVEditor.cc:123
std::shared_ptr< SessionImpl > m_session
Definition: IOVEditor.h:92
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:18
TimeType
Definition: Time.h:19
std::string description() const
Definition: IOVEditor.cc:109
unsigned long long Time_t
Definition: Time.h:14
std::string Hash
Definition: Types.h:43
double f[11][100]
IOVEditor & operator=(const IOVEditor &rhs)
Definition: IOVEditor.cc:58
cond::SynchronizationType synchronizationType() const
Definition: IOVEditor.cc:87
std::vector< std::tuple< cond::Time_t, cond::Hash, boost::posix_time::ptime > > iovBuffer
Definition: IOVEditor.cc:32
cond::Time_t endOfValidity() const
Definition: IOVEditor.cc:99
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:136
std::shared_ptr< IOVEditorData > m_data
Definition: IOVEditor.h:91
tuple msg
Definition: mps_check.py:285
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:151
cond::TimeType timeType() const
Definition: IOVEditor.cc:83
boost::posix_time::ptime creationTime
Definition: IOVEditor.cc:28
cond::Time_t lastValidatedTime() const
Definition: IOVEditor.cc:119
cond::SynchronizationType synchronizationType
Definition: IOVEditor.cc:24
Definition: plugin.cc:23
SynchronizationType
Definition: Types.h:27
void setEndOfValidity(cond::Time_t validity)
Definition: IOVEditor.cc:101
std::string tag() const
Definition: IOVEditor.cc:81
void setSynchronizationType(cond::SynchronizationType synchronizationType)
Definition: IOVEditor.cc:91
std::string payloadType() const
Definition: IOVEditor.cc:85
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