Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include "CondCore/DBOutputService/interface/OnlineDBOutputService.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include <curl/curl.h>
//

static size_t getHtmlCallback(void* contents, size_t size, size_t nmemb, void* ptr) {
  // Cast ptr to std::string pointer and append contents to that string
  ((std::string*)ptr)->append((char*)contents, size * nmemb);
  return size * nmemb;
}

bool getInfoFromService(const std::string& urlString, std::string& info) {
  CURL* curl;
  CURLcode res;
  std::string htmlBuffer;
  char errbuf[CURL_ERROR_SIZE];

  curl = curl_easy_init();
  bool ret = false;
  if (curl) {
    struct curl_slist* chunk = nullptr;
    chunk = curl_slist_append(chunk, "content-type:document/plain");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    curl_easy_setopt(curl, CURLOPT_URL, urlString.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getHtmlCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlBuffer);
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
    res = curl_easy_perform(curl);
    if (CURLE_OK == res) {
      info = htmlBuffer;
      ret = true;
    } else {
      size_t len = strlen(errbuf);
      fprintf(stderr, "\nlibcurl: (%d) ", res);
      if (len)
        fprintf(stderr, "%s%s", errbuf, ((errbuf[len - 1] != '\n') ? "\n" : ""));
      else
        fprintf(stderr, "%s\n", curl_easy_strerror(res));
    }
    curl_easy_cleanup(curl);
  }
  return ret;
}

namespace cond {

  cond::Time_t getLatestLumiFromFile(const std::string& fileName) {
    cond::Time_t lastLumiProcessed = cond::time::MIN_VAL;
    std::ifstream lastLumiFile(fileName);
    if (lastLumiFile) {
      lastLumiFile >> lastLumiProcessed;
    } else {
      throw Exception(std::string("Can't access lastLumi file ") + fileName);
    }
    return lastLumiProcessed;
  }

  cond::Time_t getLastLumiFromOMS(const std::string& omsServiceUrl) {
    cond::Time_t lastLumiProcessed = cond::time::MIN_VAL;
    std::string info("");
    if (!getInfoFromService(omsServiceUrl, info))
      throw Exception("Can't get data from OMS Service.");
    std::istringstream sinfo(info);
    std::string srun;
    if (!std::getline(sinfo, srun, ',')) {
      throw Exception("Can't get run runmber info from OMS Service.");
    }
    std::string slumi;
    if (!std::getline(sinfo, slumi, ',')) {
      throw Exception("Can't get lumi id from OMS Service.");
    }
    unsigned int run = std::stoul(srun);
    unsigned int lumi = std::stoul(slumi);
    lastLumiProcessed = cond::time::lumiTime(run, lumi);
    return lastLumiProcessed;
  }

}  // namespace cond

cond::service::OnlineDBOutputService::OnlineDBOutputService(const edm::ParameterSet& iConfig,
                                                            edm::ActivityRegistry& iAR)
    : PoolDBOutputService(iConfig, iAR),
      m_runNumber(iConfig.getUntrackedParameter<unsigned long long>("runNumber", 1)),
      m_latencyInLumisections(iConfig.getUntrackedParameter<unsigned int>("latency", 1)),
      m_omsServiceUrl(iConfig.getUntrackedParameter<std::string>("omsServiceUrl", "")),
      m_preLoadConnectionString(iConfig.getUntrackedParameter<std::string>("preLoadConnectionString", "")),
      m_frontierKey(iConfig.getUntrackedParameter<std::string>("frontierKey", "")),
      m_debug(iConfig.getUntrackedParameter<bool>("debugLogging", false)) {
  if (m_omsServiceUrl.empty()) {
    m_lastLumiFile = iConfig.getUntrackedParameter<std::string>("lastLumiFile", "");
  }
}

cond::service::OnlineDBOutputService::~OnlineDBOutputService() {}

cond::Time_t cond::service::OnlineDBOutputService::getLastLumiProcessed() {
  cond::Time_t lastLumiProcessed = cond::time::MIN_VAL;
  std::string info("");
  if (!m_omsServiceUrl.empty()) {
    lastLumiProcessed = cond::getLastLumiFromOMS(m_omsServiceUrl);
    logger().logInfo() << "Last lumi: " << lastLumiProcessed
                       << " Current run: " << cond::time::unpack(lastLumiProcessed).first
                       << " lumi id:" << cond::time::unpack(lastLumiProcessed).second;
  } else {
    if (!m_lastLumiFile.empty()) {
      lastLumiProcessed = cond::getLatestLumiFromFile(m_lastLumiFile);
      auto upkTime = cond::time::unpack(lastLumiProcessed);
      logger().logInfo() << "Last lumi: " << lastLumiProcessed << " Current run: " << upkTime.first
                         << " lumi id:" << upkTime.second;
    } else {
      lastLumiProcessed = cond::time::lumiTime(m_runNumber, 1);
    }
  }
  return lastLumiProcessed;
}

cond::Iov_t cond::service::OnlineDBOutputService::preLoadIov(const PoolDBOutputService::Record& record,
                                                             cond::Time_t targetTime) {
  auto transId = cond::time::transactionIdForLumiTime(targetTime, record.m_refreshTime, m_frontierKey);
  cond::persistency::Session session = PoolDBOutputService::newReadOnlySession(m_preLoadConnectionString, transId);
  cond::persistency::TransactionScope transaction(session.transaction());
  transaction.start(true);
  cond::persistency::IOVProxy proxy = session.readIov(record.m_tag);
  auto iov = proxy.getInterval(targetTime);
  transaction.commit();
  return iov;
}