|
| 1 | +#if !defined(__CLING__) || defined(__ROOTCLING__) |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <chrono> |
| 5 | +#include <iostream> |
| 6 | +#include <string> |
| 7 | +#include <fstream> |
| 8 | +#include <regex> |
| 9 | +#include <unistd.h> |
| 10 | +#include <TRandom.h> |
| 11 | +#include "Framework/Logger.h" |
| 12 | +#include "CCDB/CcdbApi.h" |
| 13 | + |
| 14 | +#endif |
| 15 | + |
| 16 | +// macro to populate the CCDB emulating the rates that we expect for |
| 17 | +// Run 3, as read (in terms of size and rate) from an external file |
| 18 | + |
| 19 | +using DurSec = std::chrono::duration<double, std::ratio<1, 1>>; |
| 20 | + |
| 21 | +struct CCDBObj { |
| 22 | + std::string path; |
| 23 | + float validity; |
| 24 | + size_t sz; |
| 25 | + size_t count = 0; |
| 26 | + std::decay_t<decltype(std::chrono::high_resolution_clock::now())> lastUpdate{}; |
| 27 | + CCDBObj(const std::string& _path, size_t _sz, float _val) : path(_path), validity(_val), sz(_sz) {} |
| 28 | +}; |
| 29 | + |
| 30 | +std::vector<CCDBObj> readObjectsList(const std::string& fname); |
| 31 | +void pushObject(o2::ccdb::CcdbApi& api, const CCDBObj& obj); |
| 32 | + |
| 33 | +void populateCCDB(const std::string& fname = "cdbSizeV0.txt", const std::string& ccdbHost = "http://localhost:8080" /*"http://ccdb-test.cern.ch:8080"*/) |
| 34 | +{ |
| 35 | + auto objs = readObjectsList(fname); |
| 36 | + if (objs.empty()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + o2::ccdb::CcdbApi api; |
| 41 | + api.init(ccdbHost.c_str()); // or http://localhost:8080 for a local installation |
| 42 | + |
| 43 | + while (true) { |
| 44 | + auto timeLoopStart = std::chrono::high_resolution_clock::now(); |
| 45 | + double minTLeft = 1e99; |
| 46 | + for (auto& o : objs) { |
| 47 | + DurSec elapsedSeconds = timeLoopStart - o.lastUpdate; |
| 48 | + if (elapsedSeconds.count() > o.validity || !o.count) { |
| 49 | + std::cout << "Storing entry: " << o.path << " copy " << o.count |
| 50 | + << " after " << (o.count ? elapsedSeconds.count() : 0.) << "s\n"; |
| 51 | + pushObject(api, o); |
| 52 | + o.count++; |
| 53 | + o.lastUpdate = timeLoopStart; |
| 54 | + if (minTLeft < 0.9 * o.validity) { |
| 55 | + minTLeft = o.validity; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + usleep(minTLeft * 0.9 * 1e6); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +std::vector<CCDBObj> readObjectsList(const std::string& fname) |
| 64 | +{ |
| 65 | + std::vector<CCDBObj> objs; |
| 66 | + std::ifstream inFile(fname); |
| 67 | + if (!inFile.is_open()) { |
| 68 | + LOG(ERROR) << "Failed to open input file " << fname; |
| 69 | + return objs; |
| 70 | + } |
| 71 | + std::string str; |
| 72 | + while (std::getline(inFile, str)) { |
| 73 | + str = std::regex_replace(str, std::regex("^\\s+|\\s+$|\\s+\r\n$|\\s+\r$|\\s+\n$"), "$1"); |
| 74 | + if (str[0] == '#' || str.empty()) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + std::stringstream ss(str); |
| 78 | + std::string path; |
| 79 | + float sz = 0.f, sec = 0.f; |
| 80 | + ss >> path; |
| 81 | + ss >> sz; |
| 82 | + ss >> sec; |
| 83 | + if (sz == 0 || sec == 0) { |
| 84 | + LOG(ERROR) << "Invalid data for " << path; |
| 85 | + objs.clear(); |
| 86 | + break; |
| 87 | + } |
| 88 | + LOG(INFO) << "Account " << path << " size= " << sz << " validity= " << sec; |
| 89 | + objs.emplace_back(path, sz, sec); |
| 90 | + } |
| 91 | + return objs; |
| 92 | +} |
| 93 | + |
| 94 | +void pushObject(o2::ccdb::CcdbApi& api, const CCDBObj& obj) |
| 95 | +{ |
| 96 | + std::vector<uint8_t> buff(obj.sz); |
| 97 | + for (auto& c : buff) { |
| 98 | + c = gRandom->Integer(0xff); |
| 99 | + } |
| 100 | + std::map<std::string, std::string> metadata; // can be empty |
| 101 | + metadata["responsible"] = "nobody"; |
| 102 | + metadata["custom"] = "whatever"; |
| 103 | + auto now = std::chrono::system_clock::now(); |
| 104 | + auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now); |
| 105 | + auto timeStamp = now_ms.time_since_epoch(); |
| 106 | + api.storeAsTFileAny(&buff, obj.path, metadata, timeStamp.count(), 1670700184549); // one year validity time |
| 107 | +} |
0 commit comments