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