Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-01-13 02:34:45

0001 //
0002 // Package:     CondCore/ESSources
0003 // Module:      CondDBESSource
0004 //
0005 // Description: <one line class summary>
0006 //
0007 // Implementation:
0008 //     <Notes on implementation>
0009 //
0010 // Author:      Zhen Xie
0011 // Fixes and other changes: Giacomo Govi
0012 //
0013 #include "CondDBESSource.h"
0014 
0015 #include <boost/algorithm/string.hpp>
0016 #include "CondCore/CondDB/interface/Exception.h"
0017 #include "CondCore/CondDB/interface/Time.h"
0018 #include "CondCore/CondDB/interface/Types.h"
0019 #include "CondCore/CondDB/interface/Utils.h"
0020 
0021 #include "CondCore/ESSources/interface/ProductResolverFactory.h"
0022 #include "CondCore/ESSources/interface/ProductResolver.h"
0023 
0024 #include "CondCore/CondDB/interface/PayloadProxy.h"
0025 #include "FWCore/Catalog/interface/SiteLocalConfig.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0028 #include "FWCore/ServiceRegistry/interface/Service.h"
0029 #include <exception>
0030 
0031 #include <iomanip>
0032 
0033 #include <nlohmann/json.hpp>
0034 
0035 using json = nlohmann::json;
0036 
0037 namespace {
0038   /* utility ot build the name of the plugin corresponding to a given record
0039      se ESSources
0040    */
0041   std::string buildName(std::string const& iRecordName) { return iRecordName + std::string("@NewProxy"); }
0042 
0043   std::string joinRecordAndLabel(std::string const& iRecordName, std::string const& iLabelName) {
0044     return iRecordName + std::string("@") + iLabelName;
0045   }
0046 
0047   /* utility class to return a IOVs associated to a given "name"
0048      This implementation return the IOV associated to a record...
0049      It is essentialy a workaround to get the full IOV out of the tag colector
0050      that is not accessible as hidden in the ESSource
0051      FIXME: need to support label??
0052    */
0053   class CondGetterFromESSource : public cond::persistency::CondGetter {
0054   public:
0055     CondGetterFromESSource(CondDBESSource::ResolverMap const& ip) : m_resolvers(ip) {}
0056     ~CondGetterFromESSource() override {}
0057 
0058     cond::persistency::IOVProxy get(std::string name) const override {
0059       CondDBESSource::ResolverMap::const_iterator p = m_resolvers.find(name);
0060       if (p != m_resolvers.end())
0061         return (*p).second->iovProxy();
0062       return cond::persistency::IOVProxy();
0063     }
0064 
0065     CondDBESSource::ResolverMap const& m_resolvers;
0066   };
0067 
0068   // This needs to be re-design and implemented...
0069   // dump the state of a ProductResolver
0070   void dumpInfo(std::ostream& out, std::string const& recName, cond::ProductResolverWrapperBase const& proxy) {
0071     //cond::SequenceState state(proxy.proxy()->iov().state());
0072     out << recName << " / " << proxy.label() << ": " << proxy.connString() << ", " << proxy.tag()
0073         << "\n  "
0074         //  << state.size() << ", " << state.revision()  << ", "
0075         //  << cond::time::to_boost(state.timestamp())     << "\n  "
0076         //  << state.comment()
0077         << "\n  "
0078         //  << "refresh " << proxy.proxy()->stats.nRefresh
0079         //  << "/" << proxy.proxy()->stats.nArefresh
0080         //  << ", reconnect " << proxy.proxy()->stats.nReconnect
0081         //  << "/" << proxy.proxy()->stats.nAreconnect
0082         //  << ", make " << proxy.proxy()->stats.nMake
0083         //  << ", load " << proxy.proxy()->stats.nLoad
0084         ;
0085     //if ( proxy.proxy()->stats.nLoad>0) {
0086     out << "Time look up, payloadIds:" << std::endl;
0087     const auto& pids = *proxy.requests();
0088     for (const auto& id : pids)
0089       out << "   " << id.since << " - " << id.till << " : " << id.payloadId << std::endl;
0090   }
0091 
0092   void dumpInfoJson(json& jsonData, const std::string& recName, cond::ProductResolverWrapperBase const& proxy) {
0093     json recordData;
0094     recordData["label"] = proxy.label();
0095     recordData["connectionString"] = proxy.connString();
0096     recordData["tag"] = proxy.tag();
0097 
0098     // Code to fill the JSON structure
0099 
0100     recordData["timeLookupPayloadIds"] = json::array();
0101     const auto& pids = *proxy.requests();
0102     for (const auto& id : pids) {
0103       json payloadIdData;
0104       payloadIdData["since"] = id.since;
0105       payloadIdData["till"] = id.till;
0106       payloadIdData["payloadId"] = id.payloadId;
0107       recordData["timeLookupPayloadIds"].push_back(payloadIdData);
0108     }
0109 
0110     jsonData[recName].push_back(recordData);
0111   }
0112 
0113 }  // namespace
0114 
0115 /*
0116  *  config Param
0117  *  RefreshEachRun: if true will refresh the IOV at each new run (or lumiSection)
0118  *  DumpStat: if true dump the statistics of all ProductResolver (currently on cout)
0119  *  DBParameters: configuration set of the connection
0120  *  globaltag: The GlobalTag
0121  *  toGet: list of record label tag connection-string to add/overwrite the content of the global-tag
0122  */
0123 CondDBESSource::CondDBESSource(const edm::ParameterSet& iConfig)
0124     : m_jsonDumpFilename(iConfig.getUntrackedParameter<std::string>("JsonDumpFileName", "")),
0125       m_connection(),
0126       m_connectionString(""),
0127       m_frontierKey(""),
0128       m_lastRun(0),   // for the stat
0129       m_lastLumi(0),  // for the stat
0130       m_policy(NOREFRESH),
0131       m_doDump(iConfig.getUntrackedParameter<bool>("DumpStat", false)) {
0132   if (iConfig.getUntrackedParameter<bool>("RefreshAlways", false)) {
0133     m_policy = REFRESH_ALWAYS;
0134   }
0135   if (iConfig.getUntrackedParameter<bool>("RefreshOpenIOVs", false)) {
0136     m_policy = REFRESH_OPEN_IOVS;
0137   }
0138   if (iConfig.getUntrackedParameter<bool>("RefreshEachRun", false)) {
0139     m_policy = REFRESH_EACH_RUN;
0140   }
0141   if (iConfig.getUntrackedParameter<bool>("ReconnectEachRun", false)) {
0142     m_policy = RECONNECT_EACH_RUN;
0143   }
0144 
0145   Stats s = {0, 0, 0, 0, 0, 0, 0, 0};
0146   m_stats = s;
0147 
0148   /*parameter set parsing
0149    */
0150   std::string globaltag("");
0151   if (iConfig.exists("globaltag")) {
0152     globaltag = iConfig.getParameter<std::string>("globaltag");
0153     // the global tag _requires_ a connection string
0154     m_connectionString = iConfig.getParameter<std::string>("connect");
0155 
0156     if (!globaltag.empty()) {
0157       edm::Service<edm::SiteLocalConfig> siteLocalConfig;
0158       if (siteLocalConfig.isAvailable()) {
0159         if (siteLocalConfig->useLocalConnectString()) {
0160           std::string const& localConnectPrefix = siteLocalConfig->localConnectPrefix();
0161           std::string const& localConnectSuffix = siteLocalConfig->localConnectSuffix();
0162           m_connectionString = localConnectPrefix + globaltag + localConnectSuffix;
0163         }
0164       }
0165     }
0166   } else if (iConfig.exists("connect"))  // default connection string
0167     m_connectionString = iConfig.getParameter<std::string>("connect");
0168 
0169   // frontier key
0170   m_frontierKey = iConfig.getUntrackedParameter<std::string>("frontierKey", "");
0171 
0172   // snapshot
0173   boost::posix_time::ptime snapshotTime;
0174   if (iConfig.exists("snapshotTime")) {
0175     std::string snapshotTimeString = iConfig.getParameter<std::string>("snapshotTime");
0176     if (!snapshotTimeString.empty())
0177       snapshotTime = boost::posix_time::time_from_string(snapshotTimeString);
0178   }
0179 
0180   // connection configuration
0181   if (iConfig.exists("DBParameters")) {
0182     edm::ParameterSet connectionPset = iConfig.getParameter<edm::ParameterSet>("DBParameters");
0183     m_connection.setParameters(connectionPset);
0184   }
0185   m_connection.configure();
0186 
0187   // load specific record/tag info - it will overwrite the global tag ( if any )
0188   std::map<std::string, cond::GTEntry_t> replacements;
0189   std::map<std::string, boost::posix_time::ptime> specialSnapshots;
0190   if (iConfig.exists("toGet")) {
0191     typedef std::vector<edm::ParameterSet> Parameters;
0192     Parameters toGet = iConfig.getParameter<Parameters>("toGet");
0193     for (Parameters::iterator itToGet = toGet.begin(); itToGet != toGet.end(); ++itToGet) {
0194       std::string recordname = itToGet->getParameter<std::string>("record");
0195       if (recordname.empty())
0196         throw cond::Exception("ESSource: The record name has not been provided in a \"toGet\" entry.");
0197       std::string labelname = itToGet->getUntrackedParameter<std::string>("label", "");
0198       std::string pfn("");
0199       if (m_connectionString.empty() || itToGet->exists("connect"))
0200         pfn = itToGet->getParameter<std::string>("connect");
0201       std::string tag("");
0202       std::string fqTag("");
0203       if (itToGet->exists("tag")) {
0204         tag = itToGet->getParameter<std::string>("tag");
0205         fqTag = cond::persistency::fullyQualifiedTag(tag, pfn);
0206       }
0207       boost::posix_time::ptime tagSnapshotTime =
0208           boost::posix_time::time_from_string(std::string(cond::time::MAX_TIMESTAMP));
0209       if (itToGet->exists("snapshotTime"))
0210         tagSnapshotTime = boost::posix_time::time_from_string(itToGet->getParameter<std::string>("snapshotTime"));
0211       if (itToGet->exists("refreshTime")) {
0212         cond::Time_t refreshTime = itToGet->getParameter<unsigned long long>("refreshTime");
0213         m_refreshTimeForRecord.insert(std::make_pair(recordname, std::make_pair(refreshTime, true)));
0214       }
0215 
0216       std::string recordLabelKey = joinRecordAndLabel(recordname, labelname);
0217       replacements.insert(
0218           std::make_pair(recordLabelKey, cond::GTEntry_t(std::make_tuple(recordname, labelname, fqTag))));
0219       specialSnapshots.insert(std::make_pair(recordLabelKey, tagSnapshotTime));
0220     }
0221   }
0222 
0223   // get the global tag, merge with "replacement" store in "tagCollection"
0224   std::vector<std::string> globaltagList;
0225   std::vector<std::string> connectList;
0226   std::vector<std::string> pfnPrefixList;
0227   std::vector<std::string> pfnPostfixList;
0228   if (!globaltag.empty()) {
0229     std::string pfnPrefix(iConfig.getUntrackedParameter<std::string>("pfnPrefix", ""));
0230     std::string pfnPostfix(iConfig.getUntrackedParameter<std::string>("pfnPostfix", ""));
0231     boost::split(globaltagList, globaltag, boost::is_any_of("|"), boost::token_compress_off);
0232     fillList(m_connectionString, connectList, globaltagList.size(), "connection");
0233     fillList(pfnPrefix, pfnPrefixList, globaltagList.size(), "pfnPrefix");
0234     fillList(pfnPostfix, pfnPostfixList, globaltagList.size(), "pfnPostfix");
0235   }
0236 
0237   cond::GTMetadata_t gtMetadata;
0238   fillTagCollectionFromDB(connectList, pfnPrefixList, pfnPostfixList, globaltagList, replacements, gtMetadata);
0239   // if no job specific setting has been found, use the GT timestamp
0240   if (snapshotTime.is_not_a_date_time())
0241     snapshotTime = gtMetadata.snapshotTime;
0242 
0243   TagCollection::iterator it;
0244   TagCollection::iterator itBeg = m_tagCollection.begin();
0245   TagCollection::iterator itEnd = m_tagCollection.end();
0246 
0247   std::map<std::string, cond::persistency::Session> sessions;
0248 
0249   /* load ProductResolver Plugin (it is strongly typed due to EventSetup ideosyncrasis)
0250    * construct proxy
0251    * contrary to EventSetup the "object-name" is not used as identifier: multiple entries in a record are
0252    * dinstinguished only by their label...
0253    * done in two step: first create ResolverWrapper loading ALL required dictionaries
0254    * this will allow to initialize POOL in one go for each "database"
0255    * The real initialization of the Data-Resolvers is done in the second loop 
0256    */
0257   std::vector<std::unique_ptr<cond::ProductResolverWrapperBase>> resolverWrappers(m_tagCollection.size());
0258   size_t ipb = 0;
0259   for (it = itBeg; it != itEnd; ++it) {
0260     size_t ind = ipb++;
0261     resolverWrappers[ind] = std::unique_ptr<cond::ProductResolverWrapperBase>{
0262         cond::ProductResolverFactory::get()->tryToCreate(buildName(it->second.recordName()))};
0263     if (!resolverWrappers[ind].get()) {
0264       edm::LogWarning("CondDBESSource") << "Plugin for Record " << it->second.recordName() << " has not been found.";
0265     }
0266   }
0267 
0268   // now all required libraries have been loaded
0269   // init sessions and DataResolvers
0270   ipb = 0;
0271   for (it = itBeg; it != itEnd; ++it) {
0272     std::string connStr = m_connectionString;
0273     std::string tag = it->second.tagName();
0274     std::pair<std::string, std::string> tagParams = cond::persistency::parseTag(it->second.tagName());
0275     if (!tagParams.second.empty()) {
0276       connStr = tagParams.second;
0277       tag = tagParams.first;
0278     }
0279     std::map<std::string, cond::persistency::Session>::iterator p = sessions.find(connStr);
0280     cond::persistency::Session nsess;
0281     if (p == sessions.end()) {
0282       std::string oracleConnStr = cond::persistency::convertoToOracleConnection(connStr);
0283       std::tuple<std::string, std::string, std::string> connPars =
0284           cond::persistency::parseConnectionString(oracleConnStr);
0285       std::string dbService = std::get<1>(connPars);
0286       std::string dbAccount = std::get<2>(connPars);
0287       if ((dbService == "cms_orcon_prod" || dbService == "cms_orcon_adg") && dbAccount != "CMS_CONDITIONS")
0288         edm::LogWarning("CondDBESSource")
0289             << "[WARNING] You are reading tag \"" << tag << "\" from V1 account \"" << connStr
0290             << "\". The concerned Conditions might be out of date." << std::endl;
0291       //open db get tag info (i.e. the IOV token...)
0292       nsess = m_connection.createReadOnlySession(connStr, "");
0293       sessions.insert(std::make_pair(connStr, nsess));
0294     } else
0295       nsess = (*p).second;
0296 
0297     // ownership...
0298     ResolverP resolver(std::move(resolverWrappers[ipb++]));
0299     //  instert in the map
0300     if (resolver.get()) {
0301       m_resolvers.insert(std::make_pair(it->second.recordName(), resolver));
0302       // initialize
0303       boost::posix_time::ptime tagSnapshotTime = snapshotTime;
0304       auto tagSnapshotIter = specialSnapshots.find(it->first);
0305       if (tagSnapshotIter != specialSnapshots.end())
0306         tagSnapshotTime = tagSnapshotIter->second;
0307       // finally, if the snapshot is set to infinity, reset the snapshot to null, to take the full iov set...
0308       if (tagSnapshotTime == boost::posix_time::time_from_string(std::string(cond::time::MAX_TIMESTAMP)))
0309         tagSnapshotTime = boost::posix_time::ptime();
0310 
0311       resolver->lateInit(nsess, tag, tagSnapshotTime, it->second.recordLabel(), connStr, &m_queue, &m_mutex);
0312     }
0313   }
0314 
0315   // one loaded expose all other tags to the Proxy!
0316   CondGetterFromESSource visitor(m_resolvers);
0317   ResolverMap::iterator b = m_resolvers.begin();
0318   ResolverMap::iterator e = m_resolvers.end();
0319   for (; b != e; b++) {
0320     (*b).second->proxy(0)->loadMore(visitor);
0321 
0322     /// required by eventsetup
0323     EventSetupRecordKey recordKey(EventSetupRecordKey::TypeTag::findType((*b).first));
0324     if (recordKey.type() != EventSetupRecordKey::TypeTag()) {
0325       findingRecordWithKey(recordKey);
0326       usingRecordWithKey(recordKey);
0327     }
0328   }
0329 
0330   m_stats.nData = m_resolvers.size();
0331 }
0332 
0333 void CondDBESSource::fillList(const std::string& stringList,
0334                               std::vector<std::string>& listToFill,
0335                               const unsigned int listSize,
0336                               const std::string& type) {
0337   boost::split(listToFill, stringList, boost::is_any_of("|"), boost::token_compress_off);
0338   // If it is one clone it for each GT
0339   if (listToFill.size() == 1) {
0340     for (unsigned int i = 1; i < listSize; ++i) {
0341       listToFill.push_back(stringList);
0342     }
0343   }
0344   // else if they don't match the number of GTs throw an exception
0345   else if (listSize != listToFill.size()) {
0346     throw cond::Exception(
0347         std::string("ESSource: number of global tag components does not match number of " + type + " strings"));
0348   }
0349 }
0350 
0351 void CondDBESSource::printStatistics(const Stats& stats) const {
0352   std::cout << "CondDBESSource Statistics\n"
0353             << "DataProxy " << stats.nData << " setInterval " << stats.nSet << " Runs " << stats.nRun << " Lumis "
0354             << stats.nLumi << " Refresh " << stats.nRefresh << " Actual Refresh " << stats.nActualRefresh
0355             << " Reconnect " << stats.nReconnect << " Actual Reconnect " << stats.nActualReconnect << std::endl;
0356 }
0357 
0358 void saveJsonToFile(const json& jsonData, const std::string& filename) {
0359   std::ofstream outputFile(filename);
0360   if (outputFile.is_open()) {
0361     outputFile << jsonData.dump(2) << std::endl;
0362     std::cout << "JSON data saved in file '" << filename << "'" << std::endl;
0363   } else {
0364     std::cerr << "Error opening file to write JSON data." << std::endl;
0365   }
0366 }
0367 
0368 CondDBESSource::~CondDBESSource() {
0369   //dump info FIXME: find a more suitable place...
0370   if (m_doDump) {
0371     //Output CondDBESSource Statistics to the console
0372     printStatistics(m_stats);
0373 
0374     ResolverMap::iterator b = m_resolvers.begin();
0375     ResolverMap::iterator e = m_resolvers.end();
0376     for (; b != e; b++) {
0377       dumpInfo(std::cout, (*b).first, *(*b).second);
0378       std::cout << "\n" << std::endl;
0379     }
0380   }
0381   //if filename was provided for iConfig by process.GlobalTag.JsonDumpFileName =cms.untracked.string("CondDBESSource.json")
0382   if (!m_jsonDumpFilename.empty()) {
0383     json jsonData;
0384 
0385     for (const auto& entry : m_resolvers) {
0386       std::string recName = entry.first;
0387       const auto& proxy = *entry.second;
0388       dumpInfoJson(jsonData, recName, proxy);
0389     }
0390     //Save the dump data to a file in JSON format
0391     saveJsonToFile(jsonData, m_jsonDumpFilename);
0392   }
0393   // FIXME
0394   // We shall eventually close transaction and session...
0395 }
0396 
0397 //
0398 // invoked by EventSetUp: for a given record return the smallest IOV for which iTime is valid
0399 // limit to next run/lumisection of Refresh is required
0400 //
0401 void CondDBESSource::setIntervalFor(const EventSetupRecordKey& iKey,
0402                                     const edm::IOVSyncValue& iTime,
0403                                     edm::ValidityInterval& oInterval) {
0404   std::string recordname = iKey.name();
0405 
0406   edm::LogInfo("CondDBESSource") << "Getting data for record \"" << recordname
0407                                  << "\" to be consumed on Run: " << iTime.eventID().run()
0408                                  << " - Lumiblock:" << iTime.luminosityBlockNumber()
0409                                  << " - Timestamp: " << iTime.time().value() << "; from CondDBESSource::setIntervalFor";
0410 
0411   std::lock_guard<std::mutex> guard(m_mutex);
0412   m_stats.nSet++;
0413 
0414   if (iTime.eventID().run() != m_lastRun) {
0415     m_lastRun = iTime.eventID().run();
0416     m_stats.nRun++;
0417   }
0418   if (iTime.luminosityBlockNumber() != m_lastLumi) {
0419     m_lastLumi = iTime.luminosityBlockNumber();
0420     m_stats.nLumi++;
0421   }
0422 
0423   bool doRefresh = false;
0424   cond::Time_t defaultIovSize = cond::time::MAX_VAL;
0425   bool refreshThisRecord = false;
0426   bool reconnectThisRecord = false;
0427 
0428   auto iR = m_refreshTimeForRecord.find(recordname);
0429   refreshThisRecord = iR != m_refreshTimeForRecord.end();
0430   if (refreshThisRecord) {
0431     defaultIovSize = iR->second.first;
0432     reconnectThisRecord = iR->second.second;
0433     iR->second.second = false;
0434   }
0435 
0436   if (m_policy == REFRESH_EACH_RUN || m_policy == RECONNECT_EACH_RUN) {
0437     // find out the last run number for the proxy of the specified record
0438     std::map<std::string, unsigned int>::iterator iRec = m_lastRecordRuns.find(recordname);
0439     if (iRec != m_lastRecordRuns.end()) {
0440       cond::Time_t lastRecordRun = iRec->second;
0441       if (lastRecordRun != m_lastRun) {
0442         // a refresh is required!
0443         doRefresh = true;
0444         iRec->second = m_lastRun;
0445         edm::LogInfo("CondDBESSource") << "Preparing refresh for record \"" << recordname
0446                                        << "\" since there has been a transition from run/lumi " << lastRecordRun
0447                                        << " to run/lumi " << m_lastRun << "; from CondDBESSource::setIntervalFor";
0448       }
0449     } else {
0450       doRefresh = true;
0451       m_lastRecordRuns.insert(std::make_pair(recordname, m_lastRun));
0452       edm::LogInfo("CondDBESSource") << "Preparing refresh for record \"" << recordname << "\" for " << iTime.eventID()
0453                                      << ", timestamp: " << iTime.time().value()
0454                                      << "; from CondDBESSource::setIntervalFor";
0455     }
0456     if (!doRefresh)
0457       edm::LogInfo("CondDBESSource") << "Though enabled, refresh not needed for record \"" << recordname << "\" for "
0458                                      << iTime.eventID() << ", timestamp: " << iTime.time().value()
0459                                      << "; from CondDBESSource::setIntervalFor";
0460   } else if (m_policy == REFRESH_ALWAYS || m_policy == REFRESH_OPEN_IOVS) {
0461     doRefresh = true;
0462     edm::LogInfo("CondDBESSource") << "Forcing refresh for record \"" << recordname << "\" for " << iTime.eventID()
0463                                    << ", timestamp: " << iTime.time().value()
0464                                    << "; from CondDBESSource::setIntervalFor";
0465   }
0466 
0467   oInterval = edm::ValidityInterval::invalidInterval();
0468 
0469   // compute the smallest interval (assume all objects have the same timetype....)
0470   cond::ValidityInterval recordValidity(1, cond::TIMELIMIT);
0471   cond::TimeType timetype = cond::TimeType::invalid;
0472   bool userTime = true;
0473 
0474   //FIXME use equal_range
0475   ResolverMap::const_iterator pmBegin = m_resolvers.lower_bound(recordname);
0476   ResolverMap::const_iterator pmEnd = m_resolvers.upper_bound(recordname);
0477   if (pmBegin == pmEnd) {
0478     edm::LogInfo("CondDBESSource") << "No ProductResolver (Pluging) found for record \"" << recordname
0479                                    << "\"; from CondDBESSource::setIntervalFor";
0480     return;
0481   }
0482 
0483   for (ResolverMap::const_iterator pmIter = pmBegin; pmIter != pmEnd; ++pmIter) {
0484     edm::LogInfo("CondDBESSource") << "Processing record \"" << recordname << "\" and label \""
0485                                    << pmIter->second->label() << "\" for " << iTime.eventID()
0486                                    << ", timestamp: " << iTime.time().value()
0487                                    << "; from CondDBESSource::setIntervalFor";
0488 
0489     timetype = (*pmIter).second->timeType();
0490 
0491     cond::Time_t abtime = cond::time::fromIOVSyncValue(iTime, timetype);
0492     userTime = (0 == abtime);
0493 
0494     if (userTime)
0495       return;  //  oInterval invalid to avoid that make is called...
0496 
0497     if (doRefresh || refreshThisRecord) {
0498       std::string recKey = joinRecordAndLabel(recordname, pmIter->second->label());
0499       TagCollection::const_iterator tcIter = m_tagCollection.find(recKey);
0500       if (tcIter == m_tagCollection.end()) {
0501         edm::LogInfo("CondDBESSource") << "No Tag found for record \"" << recordname << "\" and label \""
0502                                        << pmIter->second->label() << "\"; from CondDBESSource::setIntervalFor";
0503         return;
0504       }
0505 
0506       // first reconnect if required
0507       if (m_policy == RECONNECT_EACH_RUN || reconnectThisRecord) {
0508         edm::LogInfo("CondDBESSource")
0509             << "Checking if the session must be closed and re-opened for getting correct conditions"
0510             << "; from CondDBESSource::setIntervalFor";
0511         std::string transId;
0512         if (!reconnectThisRecord) {
0513           transId = std::to_string(abtime);
0514         } else {
0515           transId = cond::time::transactionIdForLumiTime(abtime, defaultIovSize, m_frontierKey);
0516         }
0517         std::string connStr = m_connectionString;
0518         std::pair<std::string, std::string> tagParams = cond::persistency::parseTag(tcIter->second.tagName());
0519         if (!tagParams.second.empty())
0520           connStr = tagParams.second;
0521         std::map<std::string, std::pair<cond::persistency::Session, std::string>>* sessionPool = &m_sessionPool;
0522         if (refreshThisRecord) {
0523           sessionPool = &m_sessionPoolForLumiConditions;
0524         }
0525         auto iSess = sessionPool->find(connStr);
0526         bool reopen = false;
0527         if (iSess != sessionPool->end()) {
0528           if (iSess->second.second != transId) {
0529             // the available session is open for a different run: reopen
0530             reopen = true;
0531             iSess->second.second = transId;
0532           }
0533         } else {
0534           // no available session: probably first run analysed...
0535           iSess =
0536               sessionPool->insert(std::make_pair(connStr, std::make_pair(cond::persistency::Session(), transId))).first;
0537           reopen = true;
0538         }
0539         if (reopen) {
0540           iSess->second.first = m_connection.createReadOnlySession(connStr, transId);
0541           edm::LogInfo("CondDBESSource") << "Re-opening the session with connection string " << connStr
0542                                          << " and new transaction Id " << transId
0543                                          << "; from CondDBESSource::setIntervalFor";
0544         }
0545 
0546         edm::LogInfo("CondDBESSource") << "Reconnecting to \"" << connStr << "\" for getting new payload for record \""
0547                                        << recordname << "\" and label \"" << pmIter->second->label()
0548                                        << "\" from IOV tag \"" << tcIter->second.tagName() << "\" to be consumed by "
0549                                        << iTime.eventID() << ", timestamp: " << iTime.time().value()
0550                                        << "; from CondDBESSource::setIntervalFor";
0551         pmIter->second->session() = iSess->second.first;
0552         pmIter->second->reload();
0553         m_stats.nReconnect++;
0554       } else {
0555         edm::LogInfo("CondDBESSource") << "Refreshing IOV sequence labeled by tag \"" << tcIter->second.tagName()
0556                                        << "\" for getting new payload for record \"" << recordname << "\" and label \""
0557                                        << pmIter->second->label() << "\" to be consumed by " << iTime.eventID()
0558                                        << ", timestamp: " << iTime.time().value()
0559                                        << "; from CondDBESSource::setIntervalFor";
0560         pmIter->second->reload();
0561         m_stats.nRefresh++;
0562       }
0563     }
0564 
0565     //query the IOVSequence
0566     cond::ValidityInterval validity = (*pmIter).second->setIntervalFor(abtime);
0567 
0568     edm::LogInfo("CondDBESSource") << "Validity coming from IOV sequence for record \"" << recordname
0569                                    << "\" and label \"" << pmIter->second->label() << "\": (" << validity.first << ", "
0570                                    << validity.second << ") for time (type: " << cond::timeTypeNames(timetype) << ") "
0571                                    << abtime << "; from CondDBESSource::setIntervalFor";
0572 
0573     recordValidity.first = std::max(recordValidity.first, validity.first);
0574     recordValidity.second = std::min(recordValidity.second, validity.second);
0575     if (refreshThisRecord && recordValidity.second == cond::TIMELIMIT) {
0576       iR->second.second = true;
0577       if (defaultIovSize)
0578         recordValidity.second = cond::time::tillTimeForIOV(abtime, defaultIovSize, timetype);
0579       else {
0580         recordValidity.second = 0;
0581       }
0582     }
0583   }
0584 
0585   if (m_policy == REFRESH_OPEN_IOVS) {
0586     doRefresh = (recordValidity.second == cond::timeTypeSpecs[timetype].endValue);
0587     edm::LogInfo("CondDBESSource") << "Validity for record \"" << recordname
0588                                    << "\" and the corresponding label(s) coming from Condition DB: ("
0589                                    << recordValidity.first << ", " << recordValidity.first
0590                                    << ") as the last IOV element in the IOV sequence is infinity"
0591                                    << "; from CondDBESSource::setIntervalFor";
0592   }
0593 
0594   // to force refresh we set end-value to the minimum such an IOV can extend to: current run or lumiblock
0595   if ((!userTime) && recordValidity.second != 0) {
0596     edm::IOVSyncValue start = cond::time::toIOVSyncValue(recordValidity.first, timetype, true);
0597     edm::IOVSyncValue stop = doRefresh ? cond::time::limitedIOVSyncValue(iTime, timetype)
0598                                        : cond::time::toIOVSyncValue(recordValidity.second, timetype, false);
0599 
0600     if (start == edm::IOVSyncValue::invalidIOVSyncValue() && stop != edm::IOVSyncValue::invalidIOVSyncValue()) {
0601       start = edm::IOVSyncValue::beginOfTime();
0602     }
0603     oInterval = edm::ValidityInterval(start, stop);
0604   }
0605 
0606   edm::LogInfo("CondDBESSource") << "Setting validity for record \"" << recordname
0607                                  << "\" and corresponding label(s): starting at " << oInterval.first().eventID()
0608                                  << ", timestamp: " << oInterval.first().time().value() << ", ending at "
0609                                  << oInterval.last().eventID() << ", timestamp: " << oInterval.last().time().value()
0610                                  << ", for " << iTime.eventID() << ", timestamp: " << iTime.time().value()
0611                                  << "; from CondDBESSource::setIntervalFor";
0612 }
0613 
0614 //required by EventSetup System
0615 edm::eventsetup::ESProductResolverProvider::KeyedResolversVector CondDBESSource::registerResolvers(
0616     const EventSetupRecordKey& iRecordKey, unsigned int iovIndex) {
0617   KeyedResolversVector keyedResolversVector;
0618 
0619   std::string recordname = iRecordKey.name();
0620 
0621   ResolverMap::const_iterator b = m_resolvers.lower_bound(recordname);
0622   ResolverMap::const_iterator e = m_resolvers.upper_bound(recordname);
0623   if (b == e) {
0624     edm::LogInfo("CondDBESSource") << "No ProductResolver (Pluging) found for record \"" << recordname
0625                                    << "\"; from CondDBESSource::registerResolvers";
0626     return keyedResolversVector;
0627   }
0628 
0629   for (ResolverMap::const_iterator p = b; p != e; ++p) {
0630     if (nullptr != (*p).second.get()) {
0631       edm::eventsetup::TypeTag type = (*p).second->type();
0632       DataKey key(type, edm::eventsetup::IdTags((*p).second->label().c_str()));
0633       keyedResolversVector.emplace_back(key, (*p).second->esResolver(iovIndex));
0634     }
0635   }
0636   return keyedResolversVector;
0637 }
0638 
0639 void CondDBESSource::initConcurrentIOVs(const EventSetupRecordKey& key, unsigned int nConcurrentIOVs) {
0640   std::string recordname = key.name();
0641   ResolverMap::const_iterator b = m_resolvers.lower_bound(recordname);
0642   ResolverMap::const_iterator e = m_resolvers.upper_bound(recordname);
0643   for (ResolverMap::const_iterator p = b; p != e; ++p) {
0644     if (p->second) {
0645       p->second->initConcurrentIOVs(nConcurrentIOVs);
0646     }
0647   }
0648 }
0649 
0650 // Fills tag collection from the given globaltag
0651 void CondDBESSource::fillTagCollectionFromGT(const std::string& connectionString,
0652                                              const std::string& prefix,
0653                                              const std::string& postfix,
0654                                              const std::string& roottag,
0655                                              std::set<cond::GTEntry_t>& tagcoll,
0656                                              cond::GTMetadata_t& gtMetadata) {
0657   if (!roottag.empty()) {
0658     if (connectionString.empty())
0659       throw cond::Exception(std::string("ESSource: requested global tag ") + roottag +
0660                             std::string(" but not connection string given"));
0661     std::tuple<std::string, std::string, std::string> connPars =
0662         cond::persistency::parseConnectionString(connectionString);
0663     if (std::get<2>(connPars) == "CMS_COND_31X_GLOBALTAG") {
0664       edm::LogWarning("CondDBESSource")
0665           << "[WARNING] You are reading Global Tag \"" << roottag
0666           << "\" from V1 account \"CMS_COND_31X_GLOBALTAG\". The concerned Conditions might be out of date."
0667           << std::endl;
0668     } else if (roottag.rfind("::All") != std::string::npos && std::get<2>(connPars) == "CMS_CONDITIONS") {
0669       edm::LogWarning("CondDBESSource") << "[WARNING] You are trying to read Global Tag \"" << roottag
0670                                         << "\" - postfix \"::All\" should not be used for V2." << std::endl;
0671     }
0672     cond::persistency::Session session = m_connection.createSession(connectionString);
0673     session.transaction().start(true);
0674     cond::persistency::GTProxy gtp = session.readGlobalTag(roottag, prefix, postfix);
0675     gtMetadata.snapshotTime = gtp.snapshotTime();
0676     for (const auto& gte : gtp) {
0677       tagcoll.insert(gte);
0678     }
0679     session.transaction().commit();
0680   }
0681 }
0682 
0683 // fills tagcollection merging with replacement
0684 // Note: it assumem the coraldbList and roottagList have the same length. This checked in the constructor that prepares the two lists before calling this method.
0685 void CondDBESSource::fillTagCollectionFromDB(const std::vector<std::string>& connectionStringList,
0686                                              const std::vector<std::string>& prefixList,
0687                                              const std::vector<std::string>& postfixList,
0688                                              const std::vector<std::string>& roottagList,
0689                                              std::map<std::string, cond::GTEntry_t>& replacement,
0690                                              cond::GTMetadata_t& gtMetadata) {
0691   std::set<cond::GTEntry_t> tagcoll;
0692 
0693   auto connectionString = connectionStringList.begin();
0694   auto prefix = prefixList.begin();
0695   auto postfix = postfixList.begin();
0696   for (auto roottag = roottagList.begin(); roottag != roottagList.end();
0697        ++roottag, ++connectionString, ++prefix, ++postfix) {
0698     fillTagCollectionFromGT(*connectionString, *prefix, *postfix, *roottag, tagcoll, gtMetadata);
0699   }
0700 
0701   std::set<cond::GTEntry_t>::iterator tagCollIter;
0702   std::set<cond::GTEntry_t>::iterator tagCollBegin = tagcoll.begin();
0703   std::set<cond::GTEntry_t>::iterator tagCollEnd = tagcoll.end();
0704 
0705   // FIXME the logic is a bit perverse: can be surely linearized (at least simplified!) ....
0706   for (tagCollIter = tagCollBegin; tagCollIter != tagCollEnd; ++tagCollIter) {
0707     std::string recordLabelKey = joinRecordAndLabel(tagCollIter->recordName(), tagCollIter->recordLabel());
0708     std::map<std::string, cond::GTEntry_t>::iterator fid = replacement.find(recordLabelKey);
0709     if (fid != replacement.end()) {
0710       if (!fid->second.tagName().empty()) {
0711         cond::GTEntry_t tagMetadata(
0712             std::make_tuple(tagCollIter->recordName(), tagCollIter->recordLabel(), fid->second.tagName()));
0713         m_tagCollection.insert(std::make_pair(recordLabelKey, tagMetadata));
0714         edm::LogInfo("CondDBESSource") << "Replacing tag \"" << tagCollIter->tagName() << "\" for record \""
0715                                        << tagMetadata.recordName() << "\" and label \"" << tagMetadata.recordLabel()
0716                                        << "\" with tag " << tagMetadata.tagName()
0717                                        << "\"; from CondDBESSource::fillTagCollectionFromDB";
0718       } else {
0719         m_tagCollection.insert(std::make_pair(recordLabelKey, *tagCollIter));
0720       }
0721       replacement.erase(fid);
0722     } else {
0723       m_tagCollection.insert(std::make_pair(recordLabelKey, *tagCollIter));
0724     }
0725   }
0726   std::map<std::string, cond::GTEntry_t>::iterator replacementIter;
0727   std::map<std::string, cond::GTEntry_t>::iterator replacementBegin = replacement.begin();
0728   std::map<std::string, cond::GTEntry_t>::iterator replacementEnd = replacement.end();
0729   for (replacementIter = replacementBegin; replacementIter != replacementEnd; ++replacementIter) {
0730     if (replacementIter->second.tagName().empty()) {
0731       std::stringstream msg;
0732       msg << "ESSource: no tag provided for record " << replacementIter->second.recordName();
0733       if (!replacementIter->second.recordLabel().empty())
0734         msg << " and label " << replacementIter->second.recordLabel();
0735       throw cond::Exception(msg.str());
0736     }
0737     m_tagCollection.insert(*replacementIter);
0738   }
0739 }
0740 
0741 // backward compatibility for configuration files
0742 class PoolDBESSource : public CondDBESSource {
0743 public:
0744   explicit PoolDBESSource(const edm::ParameterSet& ps) : CondDBESSource(ps) {}
0745 };
0746 
0747 #include "FWCore/Framework/interface/SourceFactory.h"
0748 //define this as a plug-in
0749 DEFINE_FWK_EVENTSETUP_SOURCE(PoolDBESSource);