Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example macro for Milestone Week 2 - CCDB test #5385

Merged
merged 1 commit into from Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Detectors/Calibration/CMakeLists.txt
Expand Up @@ -42,4 +42,18 @@ o2_add_executable(ccdb-populator-workflow
O2::DataFormatsTOF
O2::CCDB)

install(FILES testMacros/populateCCDB.C
DESTINATION share/macro/)

o2_add_test_root_macro(testMacros/populateCCDB.C
PUBLIC_LINK_LIBRARIES O2::CCDB
O2::Framework)

o2_add_executable(populate-ccdb
COMPONENT_NAME calibration
SOURCES testMacros/populateCCDB.cxx
PUBLIC_LINK_LIBRARIES ROOT::MathCore
O2::Framework
O2::CCDB)

add_subdirectory(workflow)
107 changes: 107 additions & 0 deletions Detectors/Calibration/testMacros/populateCCDB.C
@@ -0,0 +1,107 @@
#if !defined(__CLING__) || defined(__ROOTCLING__)

#include <string>
#include <chrono>
#include <iostream>
#include <string>
#include <fstream>
#include <regex>
#include <unistd.h>
#include <TRandom.h>
#include "Framework/Logger.h"
#include "CCDB/CcdbApi.h"

#endif

// macro to populate the CCDB emulating the rates that we expect for
// Run 3, as read (in terms of size and rate) from an external file

using DurSec = std::chrono::duration<double, std::ratio<1, 1>>;

struct CCDBObj {
std::string path;
float validity;
size_t sz;
size_t count = 0;
std::decay_t<decltype(std::chrono::high_resolution_clock::now())> lastUpdate{};
CCDBObj(const std::string& _path, size_t _sz, float _val) : path(_path), validity(_val), sz(_sz) {}
};

std::vector<CCDBObj> readObjectsList(const std::string& fname);
void pushObject(o2::ccdb::CcdbApi& api, const CCDBObj& obj);

void populateCCDB(const std::string& fname = "cdbSizeV0.txt", const std::string& ccdbHost = "http://localhost:8080" /*"http://ccdb-test.cern.ch:8080"*/)
{
auto objs = readObjectsList(fname);
if (objs.empty()) {
return;
}

o2::ccdb::CcdbApi api;
api.init(ccdbHost.c_str()); // or http://localhost:8080 for a local installation

while (true) {
auto timeLoopStart = std::chrono::high_resolution_clock::now();
double minTLeft = 1e99;
for (auto& o : objs) {
DurSec elapsedSeconds = timeLoopStart - o.lastUpdate;
if (elapsedSeconds.count() > o.validity || !o.count) {
std::cout << "Storing entry: " << o.path << " copy " << o.count
<< " after " << (o.count ? elapsedSeconds.count() : 0.) << "s\n";
pushObject(api, o);
o.count++;
o.lastUpdate = timeLoopStart;
if (minTLeft < 0.9 * o.validity) {
minTLeft = o.validity;
}
}
}
usleep(minTLeft * 0.9 * 1e6);
}
}

std::vector<CCDBObj> readObjectsList(const std::string& fname)
{
std::vector<CCDBObj> objs;
std::ifstream inFile(fname);
if (!inFile.is_open()) {
LOG(ERROR) << "Failed to open input file " << fname;
return objs;
}
std::string str;
while (std::getline(inFile, str)) {
str = std::regex_replace(str, std::regex("^\\s+|\\s+$|\\s+\r\n$|\\s+\r$|\\s+\n$"), "$1");
if (str[0] == '#' || str.empty()) {
continue;
}
std::stringstream ss(str);
std::string path;
float sz = 0.f, sec = 0.f;
ss >> path;
ss >> sz;
ss >> sec;
if (sz == 0 || sec == 0) {
LOG(ERROR) << "Invalid data for " << path;
objs.clear();
break;
}
LOG(INFO) << "Account " << path << " size= " << sz << " validity= " << sec;
objs.emplace_back(path, sz, sec);
}
return objs;
}

void pushObject(o2::ccdb::CcdbApi& api, const CCDBObj& obj)
{
std::vector<uint8_t> buff(obj.sz);
for (auto& c : buff) {
c = gRandom->Integer(0xff);
}
std::map<std::string, std::string> metadata; // can be empty
metadata["responsible"] = "nobody";
metadata["custom"] = "whatever";
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto timeStamp = now_ms.time_since_epoch();
api.storeAsTFileAny(&buff, obj.path, metadata, timeStamp.count(), 1670700184549); // one year validity time
}
62 changes: 62 additions & 0 deletions Detectors/Calibration/testMacros/populateCCDB.cxx
@@ -0,0 +1,62 @@
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// executable to populate the CCDB emulating the rates that we expect for
// Run 3, as read (in terms of size and rate) from an external file

#include "populateCCDB.C"
#include <TRandom.h>
#include <boost/program_options.hpp>
#include <iostream>

namespace bpo = boost::program_options;

bool initOptionsAndParse(bpo::options_description& options, int argc, char* argv[], bpo::variables_map& vm)
{
options.add_options()(
"host", bpo::value<std::string>()->default_value("ccdb-test.cern.ch:8080"), "CCDB server")(
"in-file-name,n", bpo::value<std::string>()->default_value("cdbSizeV0.txt"), "File name with list of CCDB entries to upload")(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the filename I had on my disk, will change it to something more meaningful when uploading the file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, anyway the name of the file is an argument.

"help,h", "Produce help message.");

try {
bpo::store(parse_command_line(argc, argv, options), vm);

// help
if (vm.count("help")) {
std::cout << options << std::endl;
return false;
}

bpo::notify(vm);
} catch (const bpo::error& e) {
std::cerr << e.what() << "\n\n";
std::cerr << "Error parsing command line arguments; Available options:\n";

std::cerr << options << std::endl;
return false;
}
return true;
}

int main(int argc, char* argv[])
{
bpo::options_description options("Allowed options");
bpo::variables_map vm;
if (!initOptionsAndParse(options, argc, argv, vm)) {
return -1;
}

// call populate "macro"
auto& inputFile = vm["in-file-name"].as<std::string>();
auto& ccdbHost = vm["host"].as<std::string>();
populateCCDB(inputFile, ccdbHost);

return (0);
}