示例#1
0
int cond::MigrateGTUtilities::execute(){

  std::string gtag = getOptionValue<std::string>("globaltag");
  std::string release = getOptionValue<std::string>("release");
  bool debug = hasDebug();
  std::string destConnect = getOptionValue<std::string>("destConnect" );
  std::string sourceConnect = getOptionValue<std::string>("sourceConnect");
  bool verbose = hasOptionValue("verbose");
  bool dryRun = hasOptionValue("dryRun");
  bool onlineMode = hasOptionValue("online");

  std::vector<std::tuple<std::string,std::string,std::string,std::string,std::string> > gtlist;
  if(! getGTList( gtag, gtlist ) ) throw std::runtime_error( std::string("Source GT ")+gtag+" has not been found." );

  ConnectionPool connPool;
  if( hasDebug() ) connPool.setMessageVerbosity( coral::Debug );
  Session session = connPool.createSession( destConnect, !dryRun );
  session.transaction().start( dryRun );

  if( session.existsGlobalTag( gtag ) ){
    std::cout <<"GT "<<gtag<<" already exists in the destination database."<<std::endl;
    return 1;
  }
  GTEditor newGT;
  if( !dryRun ){
    newGT = session.createGlobalTag( gtag );
    newGT.setDescription( "GT "+gtag+" migrated from account "+sourceConnect );
    newGT.setRelease( release );
    newGT.setSnapshotTime( boost::posix_time::time_from_string(std::string(cond::time::MAX_TIMESTAMP) ) );
  }
  std::cout <<"Processing "<<gtlist.size()<<" tags."<<std::endl;
  size_t nerr = 0;
  for(auto gtitem : gtlist ){

    std::string tag = std::get<0>( gtitem );
    std::string payloadTypeName = std::get<1>( gtitem );
    std::string recordName = std::get<2>( gtitem );
    std::string recordLabel = std::get<3>( gtitem );
    std::string connectionString = std::get<4>( gtitem );
    
    std::cout <<"--> Processing tag "<<tag<<" (objectType: "<<payloadTypeName<<") on account "<<connectionString<<std::endl;
    std::string sourceConn = convert( connectionString, onlineMode );    // "oracle://cms_orcon_adg/"+account;
    std::string destTag("");
    cond::MigrationStatus status;
    bool exists = session.checkMigrationLog( sourceConn, tag, destTag, status );
    if(!exists || status==cond::ERROR){
      std::cout <<"    ERROR: Tag "<<tag<<" from "<<sourceConn<<" has not been migrated to the destination database."<<std::endl; 
      if( !dryRun ){
	return 1;
      } else {
	nerr++;
      }
    } else {
      std::cout <<"    Inserting tag "<<destTag<<std::endl; 
    }
    if( !dryRun ) newGT.insert( recordName, recordLabel, destTag );
  }
  if( !dryRun )newGT.flush(); 

  session.transaction().commit();
  std::cout << std::endl;
  if( !dryRun ) {
    std::cout <<"Global Tag \""<<gtag<<"\" imported."<<std::endl;
  } else {
    std::cout <<"Importing Global Tag \""<<gtag<<"\" will run with "<<nerr<<" error(s)"<<std::endl; 
  }
  return 0;
}
int run( const std::string& connectionString ){
  try{

    //*************
    std::cout <<"> Connecting with db in "<<connectionString<<std::endl;
    ConnectionPool connPool;
    connPool.setMessageVerbosity( coral::Debug );
    Session session = connPool.createSession( connectionString, true );
    session.transaction().start( false );
    std::string pay0("Payload #0");
    std::string pay1("Payload #1");
    std::string pay2("Payload #2");
    std::string pay3("Payload #3");
    std::string pay4("Payload #4");
    std::string pay5("Payload #5");
    auto p0 = session.storePayload( pay0 );
    auto p1 = session.storePayload( pay1 );
    auto p2 = session.storePayload( pay2 );

    IOVEditor editor;
    if( !session.existsIov( "MyTag" ) ){
      editor = session.createIov<std::string>( "MyTag", cond::runnumber ); 
      editor.setDescription("Test for timestamp selection");
      editor.insert( 1, p0 );
      editor.insert( 101, p1 );
      editor.insert( 201, p2 );
      std::cout <<"> inserted 3 iovs..."<<std::endl;
      editor.flush();
      std::cout <<"> iov changes flushed..."<<std::endl;
    }
    session.transaction().commit();
    boost::posix_time::ptime snap0 = boost::posix_time::microsec_clock::universal_time();
    std::cout <<"> iov changes committed!..."<<std::endl;
    ::sleep(2);
    boost::posix_time::ptime notime;
    session.transaction().start();
    readTag( "MyTag", session, notime );
    session.transaction().commit();
    session.transaction().start( false );
    auto p3 = session.storePayload( pay3 );
    auto p4 = session.storePayload( pay4 );
    auto p5 = session.storePayload( pay5 );
    editor = session.editIov( "MyTag" );
    editor.insert( 101, p3 );
    editor.insert( 222, p4 );
    editor.flush();
    session.transaction().commit();
    boost::posix_time::ptime snap1 = boost::posix_time::microsec_clock::universal_time();
    ::sleep(2);
    session.transaction().start();
    readTag( "MyTag", session, notime );
    session.transaction().commit();
    session.transaction().start( false );
    editor = session.editIov( "MyTag" );
    editor.insert( 102, p5 );
    editor.flush();
    session.transaction().commit();
    session.transaction().start();
    readTag( "MyTag", session, notime );
    session.transaction().commit();
    session.transaction().start();
    readTag( "MyTag", session, snap0 );
    session.transaction().commit();
    session.transaction().start();
    readTag( "MyTag", session, snap1 );
    session.transaction().commit();
    // 
    session.transaction().start( false );
    GTEditor gtWriter = session.createGlobalTag("MY_TEST_GT_V0");
    gtWriter.setDescription( "test GT" );
    gtWriter.setRelease( "CMSSW_7_5_X" );
    gtWriter.setSnapshotTime( snap0 );
    gtWriter.insert( "myrecord", "MyTag" );
    gtWriter.flush();
    session.transaction().commit();
    session.transaction().start();
    GTProxy gtReader = session.readGlobalTag("MY_TEST_GT_V0");
    boost::posix_time::ptime snap2 = gtReader.snapshotTime();
    readTag( "MyTag", session, snap2 );
    session.transaction().commit();
  } catch (const std::exception& e){
    std::cout << "ERROR: " << e.what() << std::endl;
    return -1;
  } catch (...){
    std::cout << "UNEXPECTED FAILURE." << std::endl;
    return -1;
  }
  std::cout <<"## Run successfully completed."<<std::endl;
  return 0;
}