File indexing completed on 2024-04-06 12:01:31
0001 #include "CondCore/DBOutputService/interface/OnlineDBOutputService.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include <curl/curl.h>
0004
0005
0006 static size_t getHtmlCallback(void* contents, size_t size, size_t nmemb, void* ptr) {
0007
0008 ((std::string*)ptr)->append((char*)contents, size * nmemb);
0009 return size * nmemb;
0010 }
0011
0012 bool getInfoFromService(const std::string& urlString, std::string& info) {
0013 CURL* curl;
0014 CURLcode res;
0015 std::string htmlBuffer;
0016 char errbuf[CURL_ERROR_SIZE];
0017
0018 curl = curl_easy_init();
0019 bool ret = false;
0020 if (curl) {
0021 struct curl_slist* chunk = nullptr;
0022 chunk = curl_slist_append(chunk, "content-type:document/plain");
0023 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
0024 curl_easy_setopt(curl, CURLOPT_URL, urlString.c_str());
0025 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getHtmlCallback);
0026 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlBuffer);
0027 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
0028 res = curl_easy_perform(curl);
0029 if (CURLE_OK == res) {
0030 info = htmlBuffer;
0031 ret = true;
0032 } else {
0033 size_t len = strlen(errbuf);
0034 fprintf(stderr, "\nlibcurl: (%d) ", res);
0035 if (len)
0036 fprintf(stderr, "%s%s", errbuf, ((errbuf[len - 1] != '\n') ? "\n" : ""));
0037 else
0038 fprintf(stderr, "%s\n", curl_easy_strerror(res));
0039 }
0040 curl_easy_cleanup(curl);
0041 }
0042 return ret;
0043 }
0044
0045 namespace cond {
0046
0047 cond::Time_t getLatestLumiFromFile(const std::string& fileName) {
0048 cond::Time_t lastLumiProcessed = cond::time::MIN_VAL;
0049 std::ifstream lastLumiFile(fileName);
0050 if (lastLumiFile) {
0051 lastLumiFile >> lastLumiProcessed;
0052 } else {
0053 throw Exception(std::string("Can't access lastLumi file ") + fileName);
0054 }
0055 return lastLumiProcessed;
0056 }
0057
0058 cond::Time_t getLastLumiFromOMS(const std::string& omsServiceUrl) {
0059 cond::Time_t lastLumiProcessed = cond::time::MIN_VAL;
0060 std::string info("");
0061 if (!getInfoFromService(omsServiceUrl, info))
0062 throw Exception("Can't get data from OMS Service.");
0063 std::istringstream sinfo(info);
0064 std::string srun;
0065 if (!std::getline(sinfo, srun, ',')) {
0066 throw Exception("Can't get run runmber info from OMS Service.");
0067 }
0068 std::string slumi;
0069 if (!std::getline(sinfo, slumi, ',')) {
0070 throw Exception("Can't get lumi id from OMS Service.");
0071 }
0072 unsigned int run = std::stoul(srun);
0073 unsigned int lumi = std::stoul(slumi);
0074 lastLumiProcessed = cond::time::lumiTime(run, lumi);
0075 return lastLumiProcessed;
0076 }
0077
0078 }
0079
0080 cond::service::OnlineDBOutputService::OnlineDBOutputService(const edm::ParameterSet& iConfig,
0081 edm::ActivityRegistry& iAR)
0082 : PoolDBOutputService(iConfig, iAR),
0083 m_runNumber(iConfig.getUntrackedParameter<unsigned long long>("runNumber", 1)),
0084 m_latencyInLumisections(iConfig.getUntrackedParameter<unsigned int>("latency", 1)),
0085 m_omsServiceUrl(iConfig.getUntrackedParameter<std::string>("omsServiceUrl", "")),
0086 m_preLoadConnectionString(iConfig.getUntrackedParameter<std::string>("preLoadConnectionString", "")),
0087 m_frontierKey(iConfig.getUntrackedParameter<std::string>("frontierKey", "")),
0088 m_debug(iConfig.getUntrackedParameter<bool>("debugLogging", false)) {
0089 if (m_omsServiceUrl.empty()) {
0090 m_lastLumiFile = iConfig.getUntrackedParameter<std::string>("lastLumiFile", "");
0091 }
0092 }
0093
0094 cond::service::OnlineDBOutputService::~OnlineDBOutputService() {}
0095
0096 cond::Time_t cond::service::OnlineDBOutputService::getLastLumiProcessed() {
0097 cond::Time_t lastLumiProcessed = cond::time::MIN_VAL;
0098 std::string info("");
0099 if (!m_omsServiceUrl.empty()) {
0100 lastLumiProcessed = cond::getLastLumiFromOMS(m_omsServiceUrl);
0101 logger().logInfo() << "Last lumi: " << lastLumiProcessed
0102 << " Current run: " << cond::time::unpack(lastLumiProcessed).first
0103 << " lumi id:" << cond::time::unpack(lastLumiProcessed).second;
0104 } else {
0105 if (!m_lastLumiFile.empty()) {
0106 lastLumiProcessed = cond::getLatestLumiFromFile(m_lastLumiFile);
0107 auto upkTime = cond::time::unpack(lastLumiProcessed);
0108 logger().logInfo() << "Last lumi: " << lastLumiProcessed << " Current run: " << upkTime.first
0109 << " lumi id:" << upkTime.second;
0110 } else {
0111 lastLumiProcessed = cond::time::lumiTime(m_runNumber, 1);
0112 }
0113 }
0114 return lastLumiProcessed;
0115 }
0116
0117 cond::Iov_t cond::service::OnlineDBOutputService::preLoadIov(const PoolDBOutputService::Record& record,
0118 cond::Time_t targetTime) {
0119 auto transId = cond::time::transactionIdForLumiTime(targetTime, record.m_refreshTime, m_frontierKey);
0120 cond::persistency::Session session = PoolDBOutputService::newReadOnlySession(m_preLoadConnectionString, transId);
0121 cond::persistency::TransactionScope transaction(session.transaction());
0122 transaction.start(true);
0123 cond::persistency::IOVProxy proxy = session.readIov(record.m_tag);
0124 auto iov = proxy.getInterval(targetTime);
0125 transaction.commit();
0126 return iov;
0127 }