CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Logger.cc
Go to the documentation of this file.
9 #include "RelationalAccess/ISchema.h"
10 #include "RelationalAccess/ITable.h"
11 #include "RelationalAccess/ITableDataEditor.h"
12 #include "RelationalAccess/IQuery.h"
13 #include "RelationalAccess/ICursor.h"
14 #include "RelationalAccess/TableDescription.h"
15 #include "RelationalAccess/ITablePrivilegeManager.h"
16 #include "CoralBase/Attribute.h"
17 #include "CoralBase/AttributeList.h"
18 #include "CoralBase/AttributeSpecification.h"
19 #include "LogDBNames.h"
20 #include <boost/date_time/posix_time/posix_time_types.hpp> //no i/o just types
21 
22 #include <sstream>
23 #include <exception>
24 namespace cond{
25  template <class T>
27  std::stringstream ss;
28  ss<<t;
29  return ss.str();
30  }
31 }
32 
34  m_sessionHandle(sessionHandle),
35  m_locked(false),
36  m_statusEditorHandle(0),
37  m_sequenceManager(0),
38  m_logTableExists(false){
39 }
40 
41 void cond::Logger::connect( const std::string& logConnectionString, bool readOnly ){
43  if( readOnly ) role = Auth::COND_READER_ROLE;
44  m_sessionHandle.open( logConnectionString, role, readOnly );
45 }
46 
47 void
49  if(m_logTableExists) return;
50  cond::DbScopedTransaction trans( m_sessionHandle );
51  trans.start(false);
52  if(m_sessionHandle.nominalSchema().existsTable(cond::LogDBNames::SequenceTableName()) &&
53  m_sessionHandle.nominalSchema().existsTable(cond::LogDBNames::LogTableName())){
54  m_logTableExists=true;
55  trans.commit();
56  return;
57  }
58  //create sequence table
59  cond::SequenceManager sequenceGenerator(m_sessionHandle,cond::LogDBNames::SequenceTableName());
60  if( !sequenceGenerator.existSequencesTable() ){
61  sequenceGenerator.createSequencesTable();
62  }
63  //create log table
64  coral::TableDescription description( "CONDLOG" );
65  description.setName(cond::LogDBNames::LogTableName());
66  description.insertColumn(std::string("LOGID"),
67  coral::AttributeSpecification::typeNameForType<unsigned long long>() );
68  description.setPrimaryKey( std::vector<std::string>( 1, std::string("LOGID")));
69  description.insertColumn(std::string("EXECTIME"),
70  coral::AttributeSpecification::typeNameForType<std::string>() );
71  description.setNotNullConstraint(std::string("EXECTIME"));
72 
73  description.insertColumn(std::string("IOVTAG"),
74  coral::AttributeSpecification::typeNameForType<std::string>() );
75  description.setNotNullConstraint(std::string("IOVTAG"));
76 
77  description.insertColumn(std::string("IOVTIMETYPE"),
78  coral::AttributeSpecification::typeNameForType<std::string>() );
79  description.setNotNullConstraint(std::string("IOVTIMETYPE"));
80 
81  description.insertColumn(std::string("PAYLOADCLASS"),
82  coral::AttributeSpecification::typeNameForType<std::string>() );
83  description.setNotNullConstraint(std::string("PAYLOADCLASS"));
84 
85  description.insertColumn(std::string("PAYLOADTOKEN"),
86  coral::AttributeSpecification::typeNameForType<std::string>() );
87  description.setNotNullConstraint(std::string("PAYLOADTOKEN"));
88 
89  description.insertColumn(std::string("PAYLOADINDEX"),
90  coral::AttributeSpecification::typeNameForType<unsigned int>() );
91  description.setNotNullConstraint(std::string("PAYLOADINDEX"));
92 
93  description.insertColumn(std::string("LASTSINCE"),
94  coral::AttributeSpecification::typeNameForType<unsigned long long>() );
95  description.setNotNullConstraint(std::string("LASTSINCE"));
96 
97  description.insertColumn(std::string("DESTINATIONDB"),
98  coral::AttributeSpecification::typeNameForType<std::string>() );
99  description.setNotNullConstraint(std::string("DESTINATIONDB"));
100 
101  description.insertColumn(std::string("PROVENANCE"),
102  coral::AttributeSpecification::typeNameForType<std::string>() );
103  description.insertColumn(std::string("USERTEXT"),
104  coral::AttributeSpecification::typeNameForType<std::string>() );
105  description.insertColumn(std::string("EXECMESSAGE"),
106  coral::AttributeSpecification::typeNameForType<std::string>() );
107  m_sessionHandle.nominalSchema().createTable( description );
108  m_logTableExists=true;
109  trans.commit();
110 }
111 void
113  const cond::UserLogInfo& userlogInfo,
114  const std::string& destDB,
115  const std::string& payloadClass,
116  const std::string& payloadToken,
117  const std::string& iovtag,
118  const std::string& iovtimetype,
119  unsigned int payloadIdx,
120  unsigned long long lastSince
121  ){
122  cond::DbScopedTransaction trans( m_sessionHandle );
123  trans.start(false);
124  //aquireutctime
125  //using namespace boost::posix_time;
126  boost::posix_time::ptime p=boost::posix_time::microsec_clock::universal_time();
127  std::string now=cond::to_string(p.date().year())+"-"+cond::to_string(p.date().month())+"-"+cond::to_string(p.date().day())+"-"+cond::to_string(p.time_of_day().hours())+":"+cond::to_string(p.time_of_day().minutes())+":"+cond::to_string(p.time_of_day().seconds());
128  //aquireentryid
129  if(!m_sequenceManager){
130  m_sequenceManager=new cond::SequenceManager(m_sessionHandle,cond::LogDBNames::SequenceTableName());
131  }
132  unsigned long long targetLogId=m_sequenceManager->incrementId(LogDBNames::LogTableName());
133  //insert log record with the new id
134  this->insertLogRecord(targetLogId,now,destDB,payloadClass,payloadToken,userlogInfo,iovtag,iovtimetype,payloadIdx,lastSince,"OK");
135  trans.commit();
136 }
137 void
139  const cond::UserLogInfo& userlogInfo,
140  const std::string& destDB,
141  const std::string& payloadClass,
142  const std::string& payloadToken,
143  const std::string& iovtag,
144  const std::string& iovtimetype,
145  unsigned int payloadIdx,
146  unsigned long long lastSince,
147  const std::string& exceptionMessage
148  ){
149  cond::DbScopedTransaction trans( m_sessionHandle );
150  trans.start(false);
151  //aquireutctime
152  boost::posix_time::ptime p=boost::posix_time::microsec_clock::universal_time();
153  std::string now=cond::to_string(p.date().year())+"-"+cond::to_string(p.date().month())+"-"+cond::to_string(p.date().day())+"-"+cond::to_string(p.time_of_day().hours())+":"+cond::to_string(p.time_of_day().minutes())+":"+cond::to_string(p.time_of_day().seconds());
154  //aquireentryid
155  if(!m_sequenceManager){
156  m_sequenceManager=new cond::SequenceManager(m_sessionHandle,cond::LogDBNames::SequenceTableName());
157  }
158  unsigned long long targetLogId=m_sequenceManager->incrementId(LogDBNames::LogTableName());
159  //insert log record with the new id
160  this->insertLogRecord(targetLogId,now,destDB,payloadClass,payloadToken,userlogInfo,iovtag,iovtimetype,payloadIdx,lastSince,exceptionMessage);
161  trans.commit();
162 }
163 
164 void
166  LogDBEntry& logentry,
167  bool filterFailedOp) const{
168  //construct where
170  whereClause+=".PROVENANCE=:provenance";
171  if(filterFailedOp){
172  whereClause+=std::string(" AND ");
173  whereClause+=cond::LogDBNames::LogTableName();
174  whereClause+=std::string(".EXECMESSAGE=:execmessage");
175  }
176  coral::AttributeList BindVariableList;
177  BindVariableList.extend("provenance",typeid(std::string) );
178  BindVariableList.extend("execmessage",typeid(std::string) );
179  BindVariableList["provenance"].data<std::string>()=provenance;
180  BindVariableList["execmessage"].data<std::string>()="OK";
181  m_sessionHandle.transaction().start(true);
182  {
183  std::auto_ptr<coral::IQuery> query(m_sessionHandle.nominalSchema().tableHandle( cond::LogDBNames::LogTableName() ).newQuery());
184  // construct select
185  query->addToOutputList( "LOGID" );
186  query->defineOutputType( "LOGID", "unsigned long long" );
187  query->addToOutputList( "DESTINATIONDB" );
188  query->addToOutputList( "PROVENANCE" );
189  query->addToOutputList( "USERTEXT" );
190  query->addToOutputList( "IOVTAG" );
191  query->addToOutputList( "IOVTIMETYPE" );
192  query->addToOutputList( "PAYLOADINDEX" );
193  query->defineOutputType( "PAYLOADINDEX", "unsigned int" );
194  query->addToOutputList( "LASTSINCE" );
195  query->defineOutputType( "LASTSINCE", "unsigned long long" );
196  query->addToOutputList( "PAYLOADCLASS" );
197  query->addToOutputList( "PAYLOADTOKEN" );
198  query->addToOutputList( "EXECTIME" );
199  query->addToOutputList( "EXECMESSAGE" );
200 
201  query->setCondition( whereClause, BindVariableList );
202  query->addToOrderList( cond::LogDBNames::LogTableName()+".LOGID desc" );
203  query->limitReturnedRows();
204  coral::ICursor& cursor = query->execute();
205  if( cursor.next() ) {
206  const coral::AttributeList& row = cursor.currentRow();
207  logentry.logId=row["LOGID"].data<unsigned long long>();
208  logentry.destinationDB=row["DESTINATIONDB"].data<std::string>();
209  logentry.provenance=row["PROVENANCE"].data<std::string>();
210  logentry.usertext=row["USERTEXT"].data<std::string>();
211  logentry.iovtag=row["IOVTAG"].data<std::string>();
212  logentry.iovtimetype=row["IOVTIMETYPE"].data<std::string>();
213  logentry.payloadIdx=row["PAYLOADINDEX"].data<unsigned int>();
214  logentry.lastSince=row["LASTSINCE"].data<unsigned long long>();
215  logentry.payloadClass=row["PAYLOADCLASS"].data<std::string>();
216  logentry.payloadToken=row["PAYLOADTOKEN"].data<std::string>();
217  logentry.exectime=row["EXECTIME"].data<std::string>();
218  logentry.execmessage=row["EXECMESSAGE"].data<std::string>();
219  }
220  }
221  m_sessionHandle.transaction().commit();
222 }
223 void
225  const std::string & connectionStr,
226  cond::LogDBEntry& logentry,
227  bool filterFailedOp) const{
228  coral::AttributeList BindVariableList;
229  BindVariableList.extend("IOVTAG",typeid(std::string) );
230  BindVariableList["IOVTAG"].data<std::string>()=iovtag;
231  std::string whereClause("");
232  whereClause+=std::string("IOVTAG=:IOVTAG");
233  if(connectionStr!=""){
234  whereClause+=std::string(" AND ");
235  whereClause+=std::string("DESTINATIONDB=:DESTINATIONDB");
236  BindVariableList.extend("DESTINATIONDB",typeid(std::string) );
237  BindVariableList["DESTINATIONDB"].data<std::string>()=connectionStr;
238  }
239  if(filterFailedOp){
240  whereClause+=std::string(" AND ");
241  whereClause+=std::string("EXECMESSAGE=:EXECMESSAGE");
242  BindVariableList.extend("EXECMESSAGE",typeid(std::string) );
243  BindVariableList["EXECMESSAGE"].data<std::string>()="OK";
244  }
245  m_sessionHandle.transaction().start(true);
246  {
247  std::auto_ptr<coral::IQuery> query( m_sessionHandle.nominalSchema().tableHandle(cond::LogDBNames::LogTableName()).newQuery() );
248  // construct select
249  query->addToOutputList( "LOGID" );
250  query->defineOutputType( "LOGID", "unsigned long long" );
251  query->addToOutputList( "DESTINATIONDB" );
252  query->addToOutputList( "PROVENANCE" );
253  query->addToOutputList( "USERTEXT" );
254  query->addToOutputList( "IOVTAG" );
255  query->addToOutputList( "IOVTIMETYPE" );
256  query->addToOutputList( "PAYLOADINDEX" );
257  query->defineOutputType( "PAYLOADINDEX", "unsigned int" );
258  query->addToOutputList( "LASTSINCE" );
259  query->defineOutputType( "LASTSINCE", "unsigned long long" );
260  query->addToOutputList( "PAYLOADCLASS" );
261  query->addToOutputList( "PAYLOADTOKEN" );
262  query->addToOutputList( "EXECTIME" );
263  query->addToOutputList( "EXECMESSAGE" );
264 
265  query->setCondition( whereClause, BindVariableList );
266  query->addToOrderList( "LOGID DESC" );
267  query->limitReturnedRows();
268  coral::ICursor& cursor = query->execute();
269  if( cursor.next() ) {
270  const coral::AttributeList& row = cursor.currentRow();
271  logentry.logId=row["LOGID"].data<unsigned long long>();
272  logentry.destinationDB=row["DESTINATIONDB"].data<std::string>();
273  logentry.provenance=row["PROVENANCE"].data<std::string>();
274  logentry.usertext=row["USERTEXT"].data<std::string>();
275  logentry.iovtag=row["IOVTAG"].data<std::string>();
276  logentry.iovtimetype=row["IOVTIMETYPE"].data<std::string>();
277  logentry.payloadIdx=row["PAYLOADINDEX"].data<unsigned int>();
278  logentry.lastSince=row["LASTSINCE"].data<unsigned long long>();
279  logentry.payloadClass=row["PAYLOADCLASS"].data<std::string>();
280  logentry.payloadToken=row["PAYLOADTOKEN"].data<std::string>();
281  logentry.exectime=row["EXECTIME"].data<std::string>();
282  logentry.execmessage=row["EXECMESSAGE"].data<std::string>();
283 
284  }
285  }
286  m_sessionHandle.transaction().commit();
287 }
288 void
290  LogDBEntry& logentry,
291  bool filterFailedOp ) const{
292  LookupLastEntryByTag(iovtag,"",logentry,filterFailedOp);
293 }
294 void
295 cond::Logger::insertLogRecord(unsigned long long logId,
296  const std::string& utctime,
297  const std::string& destDB,
298  const std::string& payloadClass,
299  const std::string& payloadToken,
300  const cond::UserLogInfo& userLogInfo,
301  const std::string& iovtag,
302  const std::string& iovtimetype,
303  unsigned int payloadIdx,
304  unsigned long long lastSince,
305  const std::string& exceptionMessage){
306  try{
307  coral::AttributeList rowData;
308  rowData.extend<unsigned long long>("LOGID");
309  rowData.extend<std::string>("EXECTIME");
310  rowData.extend<std::string>("DESTINATIONDB");
311  rowData.extend<std::string>("PAYLOADCLASS");
312  rowData.extend<std::string>("PAYLOADTOKEN");
313  rowData.extend<std::string>("PROVENANCE");
314  rowData.extend<std::string>("USERTEXT");
315  rowData.extend<std::string>("IOVTAG");
316  rowData.extend<std::string>("IOVTIMETYPE");
317  rowData.extend<unsigned int>("PAYLOADINDEX");
318  rowData.extend<unsigned long long>("LASTSINCE");
319  rowData.extend<std::string>("EXECMESSAGE");
320  rowData["LOGID"].data< unsigned long long >() = logId;
321  rowData["EXECTIME"].data< std::string >() = utctime;
322  rowData["DESTINATIONDB"].data< std::string >() = destDB;
323  rowData["PAYLOADCLASS"].data< std::string >() = payloadClass;
324  std::string ptok = payloadToken;
325  if( payloadToken.empty() ) ptok = "NA";
326  rowData["PAYLOADTOKEN"].data< std::string >() = ptok;
327  rowData["PROVENANCE"].data< std::string >() = userLogInfo.provenance;
328  rowData["USERTEXT"].data< std::string >() = userLogInfo.usertext;
329  rowData["IOVTAG"].data< std::string >() = iovtag;
330  rowData["IOVTIMETYPE"].data< std::string >() = iovtimetype;
331  rowData["PAYLOADINDEX"].data< unsigned int >() = payloadIdx;
332  rowData["LASTSINCE"].data< unsigned long long >() = lastSince;
333  rowData["EXECMESSAGE"].data< std::string >() = exceptionMessage;
334  m_sessionHandle.nominalSchema().tableHandle(cond::LogDBNames::LogTableName()).dataEditor().insertRow(rowData);
335  }catch(const std::exception& er){
336  throw cond::Exception(std::string(er.what()));
337  }
338 }
339 
341  if( m_sequenceManager ){
342  delete m_sequenceManager;
343  m_sequenceManager=0;
344  }
345 }
std::string usertext
Definition: LogDBEntry.h:9
std::string exectime
Definition: LogDBEntry.h:27
std::string usertext
Definition: LogDBEntry.h:20
std::string iovtimetype
Definition: LogDBEntry.h:22
void logFailedOperationNow(const cond::UserLogInfo &userlogInfo, const std::string &destDB, const std::string &payloadToken, const std::string &payloadClass, const std::string &iovtag, const std::string &iovtimetype, unsigned int payloadIdx, unsigned long long lastSince, const std::string &exceptionMessage)
Definition: Logger.cc:138
void connect(const std::string &logConnectionString, bool readOnly=false)
Definition: Logger.cc:41
unsigned int payloadIdx
Definition: LogDBEntry.h:23
std::string payloadToken
Definition: LogDBEntry.h:26
unsigned long long logId
Definition: LogDBEntry.h:17
void LookupLastEntryByProvenance(const std::string &provenance, LogDBEntry &logentry, bool filterFailedOp=true) const
Definition: Logger.cc:165
std::string payloadClass
Definition: LogDBEntry.h:25
bool existSequencesTable()
Whether sequence table exists.
std::string destinationDB
Definition: LogDBEntry.h:18
std::string to_string(const T &t)
Definition: Logger.cc:26
void createSequencesTable()
Creates the table holding the sequences.
unsigned long long lastSince
Definition: LogDBEntry.h:24
static const std::string COND_WRITER_ROLE
Definition: Auth.h:18
std::string execmessage
Definition: LogDBEntry.h:28
std::string provenance
Definition: LogDBEntry.h:8
int start(bool readOnly=false)
start transaction
void logOperationNow(const cond::UserLogInfo &userlogInfo, const std::string &destDB, const std::string &payloadToken, const std::string &payloadClass, const std::string &iovtag, const std::string &iovtimetype, unsigned int payloadIdx, unsigned long long lastSince)
Definition: Logger.cc:112
static const std::string COND_READER_ROLE
Definition: Auth.h:19
tuple description
Definition: idDealer.py:66
void createLogDBIfNonExist()
Definition: Logger.cc:48
static std::string SequenceTableName()
Definition: LogDBNames.cc:3
std::string provenance
Definition: LogDBEntry.h:19
void LookupLastEntryByTag(const std::string &iovtag, LogDBEntry &logentry, bool filterFailedOp=true) const
Definition: Logger.cc:289
tuple query
Definition: o2o.py:269
volatile std::atomic< bool > shutdown_flag false
Logger(DbSession &sessionHandle)
Definition: Logger.cc:33
long double T
int commit()
commit transaction. Will disconnect from database if connection timeout==0 or connectted time close t...
std::string iovtag
Definition: LogDBEntry.h:21
void insertLogRecord(unsigned long long logId, const std::string &utctime, const std::string &destDB, const std::string &payloadToken, const std::string &payloadClass, const cond::UserLogInfo &userLogInfo, const std::string &iovtag, const std::string &iovtimetype, unsigned int payloadIdx, unsigned long long lastSince, const std::string &exceptionMessage)
Definition: Logger.cc:295
static std::string LogTableName()
Definition: LogDBNames.cc:7