CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ExportIOVUtilities.cc
Go to the documentation of this file.
2 
8 
11 
15 
19 #include <iterator>
20 #include <limits>
21 #include <iostream>
22 #include <fstream>
23 #include <sstream>
24 #include <stdexcept>
25 
31  addConnectOption("sourceConnect","s","source connection string(required)");
32  addConnectOption("destConnect","d","destionation connection string(required)");
33  addOption<std::string>("inputTag","i","tag to export( default = destination tag)");
34  addOption<std::string>("destTag","t","destination tag (required)");
35  addOption<cond::Time_t>("beginTime","b","begin time (first since) (optional)");
36  addOption<cond::Time_t>("endTime","e","end time (last till) (optional)");
37  addOption<bool>("outOfOrder","o","allow out of order merge (optional, default=false)");
38  addOption<size_t>("bunchSize","n","iterate with bunches of specific size (optional)");
39  addOption<std::string>("usertext","x","user text, to be included in usertext column (optional, must be enclosed in double quotes)");
41 }
42 
44 }
45 
47 
48  std::string sourceConnect = getOptionValue<std::string>("sourceConnect");
49  std::string destConnect = getOptionValue<std::string>("destConnect");
50 
51  std::string destTag = getOptionValue<std::string>("destTag");
52  std::string inputTag(destTag);
53  if( hasOptionValue("inputTag") ) inputTag = getOptionValue<std::string>("inputTag");
54  std::string usertext("no user comments");
55  if( hasOptionValue("usertext")) usertext = getOptionValue<std::string>("usertext");
56  bool doLog = hasOptionValue("logDB");
57 
59  if( hasOptionValue("beginTime" )) since = getOptionValue<cond::Time_t>("beginTime");
61  if( hasOptionValue("endTime" )) till = getOptionValue<cond::Time_t>("endTime");
62 
63  size_t bunchSize = 1;
64  if(hasOptionValue("bunchSize")) bunchSize = getOptionValue<size_t>("bunchSize");
65 
66  std::string sqlOutputFileName("sqlmonitoring.out");
67  bool debug=hasDebug();
68  bool outOfOrder = hasOptionValue("outOfOrder");
69 
70  std::string sourceIovToken("");
71  std::string destIovToken("");
72  bool newIOV = false;
73  cond::TimeType sourceIovType;
74 
75  cond::DbSession sourceDb = openDbSession("sourceConnect", Auth::COND_READER_ROLE, true);
76  cond::DbSession destDb = openDbSession("destConnect", Auth::COND_WRITER_ROLE );
77 
78  std::auto_ptr<cond::Logger> logDb;
79  cond::DbSession logSession;
80 
81  std::string payloadToken("");
82  std::string payloadClasses("");
83  int iovSize = 0;
84  int ncopied = 0;
86  if (doLog) {
87  logSession = openDbSession( "logDB",Auth::COND_WRITER_ROLE );
88  logDb.reset(new cond::Logger(logSession));
89  logDb->createLogDBIfNonExist();
90  a.provenance=sourceConnect+"/"+inputTag;
91  a.usertext="exportIOV V4.0;";
92  }
93 
94  // find tag in source
95  sourceDb.transaction().start(true);
96  cond::MetaData sourceMetadata(sourceDb);
97  sourceIovToken=sourceMetadata.getToken(inputTag);
98  if(sourceIovToken.empty())
99  throw std::runtime_error(std::string("tag ")+inputTag+std::string(" not found") );
100 
101  if(debug){
102  std::cout<<"source iov token "<<sourceIovToken<<std::endl;
103  }
104 
105  cond::IOVProxy sourceIov( sourceDb );
106  sourceIov.load( sourceIovToken );
107  sourceIovType = sourceIov.timetype();
108  std::string const & timetypestr = cond::timeTypeSpecs[sourceIovType].name;
109  if(debug){
110  std::cout<<"source iov type "<<sourceIovType<<std::endl;
111  }
112 
113  if( doLog ){
114  std::set<std::string> pclasses = sourceIov.payloadClasses();
115  iovSize = sourceIov.size();
116  std::ostringstream stream;
117  std::copy(pclasses.begin(), pclasses.end(), std::ostream_iterator<std::string>(stream, ", "));
118  payloadClasses = stream.str();
119  }
120 
121  try{
122  // find tag in destination
124  transaction.start(false);
125 
126  int oldSize=0;
127  cond::IOVEditor destIov( destDb );
130 
131  cond::MetaData destMetadata( destDb );
132  if( destMetadata.hasTag(destTag) ){
133  destIovToken=destMetadata.getToken(destTag);
134  destIov.load( destIovToken );
135  oldSize = destIov.proxy().size();
136  if (sourceIovType!=destIov.timetype()) {
137  throw std::runtime_error("iov type in source and dest differs");
138  }
139  } else {
140  newIOV = true;
141  destIovToken=destIov.create( sourceIovType, sourceIov.iov().lastTill(),sourceIov.iov().metadata() );
142  destMetadata.addMapping(destTag,destIovToken,sourceIovType);
143  destIov.setScope( cond::IOVSequence::Tag );
144  }
145  if(debug){
146  std::cout<<"dest iov token "<<destIovToken<<std::endl;
147  std::cout<<"dest iov type "<<sourceIovType<<std::endl;
148  }
149 
150  since = std::max(since, cond::timeTypeSpecs[sourceIovType].beginValue);
151  till = std::min(till, cond::timeTypeSpecs[sourceIovType].endValue);
152 
153  boost::shared_ptr<IOVImportIterator> importIterator = destIov.importIterator();
154  importIterator->setUp( sourceIov, since, till, outOfOrder, bunchSize );
155 
156  size_t totalImported = 0;
157  if( bunchSize>1 ){
158  unsigned int iter = 0;
159  while( importIterator->hasMoreElements() ){
160  if(iter>0){
161  transaction.commit();
162  transaction.start();
163  destIov.reload();
164  }
165  iter++;
166  size_t imported = importIterator->importMoreElements();
167  totalImported += imported;
168  std::cout <<"Iteration #"<<iter<<": "<<imported<<" element(s)."<<std::endl;
169  }
170  } else {
171  totalImported = importIterator->importAll();
172  }
173  std::cout <<totalImported<<" element(s) exported."<<std::endl;
174 
175  ::sleep(1);
176 
177  // grab info
178  // call IOV proxy with keep open option: it is required to lookup the payload class. A explicit commit will be needed at the end.
179  if (doLog) {
180  IOVProxy diov = destIov.proxy();
181  std::ostringstream stream;
182  std::copy(diov.payloadClasses().begin(), diov.payloadClasses().end(), std::ostream_iterator<std::string>(stream, ", "));
183  payloadClasses = stream.str();
184  iovSize = diov.size();
185  ncopied = iovSize-oldSize;
186  if ( ncopied == 1) {
187  // get last object
188  const IOVElement& last = diov.iov().iovs().back();
189  payloadToken=last.token();
190  payloadClasses = destDb.classNameForItem( payloadToken );
191  }
192  if (newIOV) a.usertext+= "new tag;";
193  std::ostringstream ss;
194  ss << "since="<< since <<", till="<< till << ", " << usertext << ";";
195  ss << " copied="<< ncopied <<";";
196  a.usertext +=ss.str();
197  logDb->logOperationNow(a,destConnect,payloadClasses,payloadToken,destTag,timetypestr,iovSize-1,since);
198  }
199  transaction.commit();
200  sourceDb.transaction().commit();
201  }catch ( cond::Exception const& er ){
202  if (doLog) {
203  if (newIOV) a.usertext+= "new tag;";
204  std::ostringstream ss;
205  ss << "since="<< since <<", till="<< till << ", " << usertext << ";";
206  ss << " copied="<< ncopied <<";";
207  a.usertext +=ss.str();
208  logDb->logFailedOperationNow(a,destConnect,payloadClasses,payloadToken,destTag,timetypestr,iovSize-1,since,std::string(er.what()));
209  }
210  sourceDb.transaction().commit();
211  throw;
212  }
213 
214  return 0;
215 }
216 
217 
virtual char const * what() const
Definition: Exception.cc:141
const TimeTypeSpecs timeTypeSpecs[]
Definition: Time.cc:22
std::string usertext
Definition: LogDBEntry.h:9
cond::Time_t lastTill() const
Definition: IOVSequence.h:65
std::string create(cond::TimeType timetype)
Definition: IOVEditor.cc:260
IOVSequence const & iov() const
Definition: IOVProxy.cc:276
void load(const std::string &token)
Definition: IOVEditor.cc:202
std::string classNameForItem(const std::string &objectId)
Definition: DbSession.cc:292
std::string token() const
Definition: IOVElement.h:35
DbTransaction & transaction()
Definition: DbSession.cc:208
int commit()
commit transaction.
Container const & iovs() const
Definition: IOVSequence.cc:76
std::string const & metadata() const
Definition: IOVSequence.h:79
void addConnectOption()
Definition: Utilities.cc:111
void load(const std::string &token)
Definition: IOVProxy.cc:200
std::set< std::string > const & payloadClasses() const
Definition: IOVProxy.cc:294
const std::string getToken(const std::string &tagname) const
Definition: MetaData.cc:52
void setScope(cond::IOVSequence::ScopeType scope)
set the scope
Definition: IOVEditor.cc:395
TimeType
Definition: Time.h:21
bool lockContainer(const std::string &name)
Definition: Database.cc:265
int start(bool readOnly=false)
start transaction
unsigned long long Time_t
Definition: Time.h:16
TimeType timetype() const
Definition: IOVEditor.cc:538
bool addMapping(const std::string &name, const std::string &token, cond::TimeType timetype=cond::runnumber)
Definition: MetaData.cc:36
const T & max(const T &a, const T &b)
void addAuthenticationOptions()
Definition: Utilities.cc:104
bool hasTag(const std::string &name) const
Definition: MetaData.cc:68
static const std::string COND_WRITER_ROLE
Definition: Auth.h:18
int size() const
Definition: IOVProxy.cc:272
std::string provenance
Definition: LogDBEntry.h:8
void addSQLOutputOption()
Definition: Utilities.cc:141
void addConfigFileOption()
Definition: Utilities.cc:136
int start(bool readOnly=false)
start transaction
ExportIOVUtilities(std::string const &name)
static const std::string COND_READER_ROLE
Definition: Auth.h:19
void addLogDBOption()
Definition: Utilities.cc:126
bool createIOVContainerIfNecessary()
Definition: IOVEditor.cc:249
#define debug
Definition: HDRShower.cc:19
cond::IOVProxy proxy() const
Definition: IOVEditor.cc:546
void addDictionaryOption()
Definition: Utilities.cc:131
std::string name
Definition: Time.h:43
static std::string container()
Definition: IOVNames.h:7
double a
Definition: hdecay.h:121
TimeType timetype() const
Definition: IOVProxy.cc:281
tuple cout
Definition: gather_cfg.py:121
boost::shared_ptr< IOVImportIterator > importIterator()
Definition: IOVEditor.cc:531
int commit()
commit transaction. Will disconnect from database if connection timeout==0 or connectted time close t...
ora::Database & storage()
Definition: DbSession.cc:215