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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")( | ||
"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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.