Project
Loading...
Searching...
No Matches
CCDBFetcherHelper.cxx
Go to the documentation of this file.
1// Copyright 2019-2025 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#include "CCDBFetcherHelper.h"
13#include "Framework/Signpost.h"
16#include <TError.h>
17#include <TMemFile.h>
18
20
21namespace o2::framework
22{
23
25{
26 // find the first = sign in the string. If present drop everything after it
27 // and between it and the previous /.
28 auto pos = path.find('=');
29 if (pos == std::string::npos) {
30 auto entry = remappings.find(path);
31 return apis[entry == remappings.end() ? "" : entry->second];
32 }
33 auto pos2 = path.rfind('/', pos);
34 if (pos2 == std::string::npos || pos2 == pos - 1 || pos2 == 0) {
35 throw runtime_error_f("Malformed path %s", path.c_str());
36 }
37 auto entry = remappings.find(path.substr(0, pos2));
38 return apis[entry == remappings.end() ? "" : entry->second];
39}
40
41namespace
42{
43bool isOnlineRun(DataTakingContext const& dtc)
44{
46}
47} // namespace
48
50{
51 auto defHost = options.get<std::string>("condition-backend");
52 auto checkRate = options.get<int>("condition-tf-per-query");
53 auto checkMult = options.get<int>("condition-tf-per-query-multiplier");
54 helper.timeToleranceMS = options.get<int64_t>("condition-time-tolerance");
55 helper.queryPeriodGlo = checkRate > 0 ? checkRate : std::numeric_limits<int>::max();
56 helper.queryPeriodFactor = checkMult > 0 ? checkMult : 1;
57 LOGP(info, "CCDB Backend at: {}, validity check for every {} TF{}", defHost, helper.queryPeriodGlo, helper.queryPeriodFactor == 1 ? std::string{} : fmt::format(", (query for high-rate objects downscaled by {})", helper.queryPeriodFactor));
58 LOGP(info, "Hook to enable signposts for CCDB messages at {}", (void*)&private_o2_log_ccdb->stacktrace);
59 auto remapString = options.get<std::string>("condition-remap");
60 ParserResult result = parseRemappings(remapString.c_str());
61 if (!result.error.empty()) {
62 throw runtime_error_f("Error while parsing remapping string %s", result.error.c_str());
63 }
64 helper.remappings = result.remappings;
65 helper.apis[""].init(defHost); // default backend
66 LOGP(info, "Initialised default CCDB host {}", defHost);
67 //
68 for (auto& entry : helper.remappings) { // init api instances for every host seen in the remapping
69 if (helper.apis.find(entry.second) == helper.apis.end()) {
70 helper.apis[entry.second].init(entry.second);
71 LOGP(info, "Initialised custom CCDB host {}", entry.second);
72 }
73 LOGP(info, "{} is remapped to {}", entry.first, entry.second);
74 }
75 helper.createdNotBefore = std::to_string(options.get<int64_t>("condition-not-before"));
76 helper.createdNotAfter = std::to_string(options.get<int64_t>("condition-not-after"));
77}
78
80{
81 std::unordered_map<std::string, std::string> remappings;
82 std::string currentUrl = "";
83
84 enum ParsingStates {
85 IN_BEGIN,
86 IN_BEGIN_URL,
87 IN_BEGIN_TARGET,
88 IN_END_TARGET,
89 IN_END_URL
90 };
91 ParsingStates state = IN_BEGIN;
92
93 while (true) {
94 switch (state) {
95 case IN_BEGIN: {
96 if (*str == 0) {
97 return {remappings, ""};
98 }
99 state = IN_BEGIN_URL;
100 }
101 case IN_BEGIN_URL: {
102 if ((strncmp("http://", str, 7) != 0) && (strncmp("https://", str, 8) != 0 && (strncmp("file://", str, 7) != 0))) {
103 return {remappings, "URL should start with either http:// or https:// or file://"};
104 }
105 state = IN_END_URL;
106 } break;
107 case IN_END_URL: {
108 char const* c = strchr(str, '=');
109 if (c == nullptr) {
110 return {remappings, "Expecting at least one target path, missing `='?"};
111 }
112 if ((c - str) == 0) {
113 return {remappings, "Empty url"};
114 }
115 currentUrl = std::string_view(str, c - str);
116 state = IN_BEGIN_TARGET;
117 str = c + 1;
118 } break;
119 case IN_BEGIN_TARGET: {
120 if (*str == 0) {
121 return {remappings, "Empty target"};
122 }
123 state = IN_END_TARGET;
124 } break;
125 case IN_END_TARGET: {
126 char const* c = strpbrk(str, ",;");
127 if (c == nullptr) {
128 if (remappings.count(str)) {
129 return {remappings, fmt::format("Path {} requested more than once.", str)};
130 }
131 remappings[std::string(str)] = currentUrl;
132 return {remappings, ""};
133 }
134 if ((c - str) == 0) {
135 return {remappings, "Empty target"};
136 }
137 auto key = std::string(str, c - str);
138 if (remappings.count(str)) {
139 return {remappings, fmt::format("Path {} requested more than once.", key)};
140 }
141 remappings[key] = currentUrl;
142 if (*c == ';') {
143 state = IN_BEGIN_URL;
144 } else {
145 state = IN_BEGIN_TARGET;
146 }
147 str = c + 1;
148 } break;
149 }
150 }
151}
152
153auto CCDBFetcherHelper::populateCacheWith(std::shared_ptr<CCDBFetcherHelper> const& helper,
154 std::vector<CCDBFetcherHelper::FetchOp> const& ops,
155 TimingInfo& timingInfo,
157 DataAllocator& allocator) -> std::vector<CCDBFetcherHelper::Response>
158{
159 int objCnt = -1;
160 // We use the timeslice, so that we hook into the same interval as the rest of the
161 // callback.
162 static bool isOnline = isOnlineRun(dtc);
163
164 auto sid = _o2_signpost_id_t{(int64_t)timingInfo.timeslice};
165 O2_SIGNPOST_START(ccdb, sid, "populateCacheWith", "Starting to populate cache with CCDB objects");
166 std::vector<Response> responses;
167 for (auto& op : ops) {
168 int64_t timestampToUse = op.timestamp;
169 O2_SIGNPOST_EVENT_EMIT(ccdb, sid, "populateCacheWith", "Fetching object for route %{public}s", DataSpecUtils::describe(op.spec).data());
170 objCnt++;
171 auto concrete = DataSpecUtils::asConcreteDataMatcher(op.spec);
172 Output output{concrete.origin, concrete.description, concrete.subSpec};
173 auto&& v = allocator.makeVector<char>(output);
174 std::map<std::string, std::string> metadata;
175 std::map<std::string, std::string> headers;
176 std::string path = op.url;
177 std::string etag = "";
178 int chRate = helper->queryPeriodGlo;
179 bool checkValidity = false;
180 if (op.runDependent > 0) {
181 if (op.runDependent == 1) {
182 metadata["runNumber"] = std::format("{}", op.runNumber);
183 } else if (op.runDependent == 2) {
184 timestampToUse = op.runNumber;
185 } else {
186 LOGP(fatal, "Undefined ccdb-run-dependent option {} for spec {}/{}/{}", op.runDependent,
187 concrete.origin.as<std::string>(), concrete.description.as<std::string>(), int(concrete.subSpec));
188 }
189 }
190 for (auto m : op.metadata) {
191 O2_SIGNPOST_EVENT_EMIT(ccdb, sid, "populateCacheWith", "Adding metadata %{public}s: %{public}s to the request", m.key.data(), m.value.data());
192 metadata[m.key] = m.value;
193 }
194 if (op.queryRate != 0) {
195 chRate = op.queryRate * helper->queryPeriodFactor;
196 }
197
198 const auto url2uuid = helper->mapURL2UUID.find(path);
199 if (url2uuid != helper->mapURL2UUID.end()) {
200 etag = url2uuid->second.etag;
201 // We check validity every chRate timeslices or if the cache is expired
202 uint64_t validUntil = url2uuid->second.cacheValidUntil;
203 // When the cache was populated. If the cache was populated after the timestamp, we need to check validity.
204 uint64_t cachePopulatedAt = url2uuid->second.cachePopulatedAt;
205 // If timestamp is before the time the element was cached or after the claimed validity, we need to check validity, again
206 // when online.
207 bool cacheExpired = (validUntil <= timestampToUse) || (op.timestamp < cachePopulatedAt);
208 checkValidity = (std::abs(int(timingInfo.tfCounter - url2uuid->second.lastCheckedTF)) >= chRate) && (isOnline || cacheExpired);
209 } else {
210 checkValidity = true; // never skip check if the cache is empty
211 }
212
213 O2_SIGNPOST_EVENT_EMIT(ccdb, sid, "populateCacheWith", "checkValidity is %{public}s for tfID %d of %{public}s", checkValidity ? "true" : "false", timingInfo.tfCounter, path.data());
214
215 const auto& api = helper->getAPI(path);
216 if (checkValidity && (!api.isSnapshotMode() || etag.empty())) { // in the snapshot mode the object needs to be fetched only once
217 LOGP(detail, "Loading {} for timestamp {}", path, timestampToUse);
218 api.loadFileToMemory(v, path, metadata, timestampToUse, &headers, etag, helper->createdNotAfter, helper->createdNotBefore);
219 if ((headers.count("Error") != 0) || (etag.empty() && v.empty())) {
220 LOGP(fatal, "Unable to find CCDB object {}/{}", path, timestampToUse);
221 // FIXME: I should send a dummy message.
222 continue;
223 }
224 // printing in case we find a default entry
225 if (headers.find("default") != headers.end()) {
226 LOGP(detail, "******** Default entry used for {} ********", path);
227 }
228 helper->mapURL2UUID[path].lastCheckedTF = timingInfo.tfCounter;
229 if (etag.empty()) {
230 helper->mapURL2UUID[path].etag = headers["ETag"]; // update uuid
231 helper->mapURL2UUID[path].cachePopulatedAt = timestampToUse;
232 helper->mapURL2UUID[path].cacheMiss++;
233 helper->mapURL2UUID[path].size = v.size();
234 helper->mapURL2UUID[path].minSize = std::min(v.size(), helper->mapURL2UUID[path].minSize);
235 helper->mapURL2UUID[path].maxSize = std::max(v.size(), helper->mapURL2UUID[path].maxSize);
236 auto size = v.size();
237 api.appendFlatHeader(v, headers);
238 auto cacheId = allocator.adoptContainer(output, std::move(v), DataAllocator::CacheStrategy::Always, header::gSerializationMethodCCDB);
239 helper->mapURL2DPLCache[path] = cacheId;
240 responses.emplace_back(Response{.id = cacheId, .size = size, .request = nullptr});
241 O2_SIGNPOST_EVENT_EMIT(ccdb, sid, "populateCacheWith", "Caching %{public}s for %{public}s (DPL id %" PRIu64 ", size %zu)", path.data(), headers["ETag"].data(), cacheId.value, size);
242 continue;
243 }
244 if (v.size()) { // but should be overridden by fresh object
245 // somewhere here pruneFromCache should be called
246 helper->mapURL2UUID[path].etag = headers["ETag"]; // update uuid
247 helper->mapURL2UUID[path].cachePopulatedAt = timestampToUse;
248 helper->mapURL2UUID[path].cacheValidUntil = headers["Cache-Valid-Until"].empty() ? 0 : std::stoul(headers["Cache-Valid-Until"]);
249 helper->mapURL2UUID[path].cacheMiss++;
250 helper->mapURL2UUID[path].size = v.size();
251 helper->mapURL2UUID[path].minSize = std::min(v.size(), helper->mapURL2UUID[path].minSize);
252 helper->mapURL2UUID[path].maxSize = std::max(v.size(), helper->mapURL2UUID[path].maxSize);
253 auto size = v.size();
254 api.appendFlatHeader(v, headers);
255 auto cacheId = allocator.adoptContainer(output, std::move(v), DataAllocator::CacheStrategy::Always, header::gSerializationMethodCCDB);
256 helper->mapURL2DPLCache[path] = cacheId;
257 responses.emplace_back(Response{.id = cacheId, .size = size, .request = nullptr});
258 O2_SIGNPOST_EVENT_EMIT(ccdb, sid, "populateCacheWith", "Caching %{public}s for %{public}s (DPL id %" PRIu64 ")", path.data(), headers["ETag"].data(), cacheId.value);
259 // one could modify the adoptContainer to take optional old cacheID to clean:
260 // mapURL2DPLCache[URL] = ctx.outputs().adoptContainer(output, std::move(outputBuffer), DataAllocator::CacheStrategy::Always, mapURL2DPLCache[URL]);
261 continue;
262 } else {
263 // Only once the etag is actually used, we get the information on how long the object is valid
264 helper->mapURL2UUID[path].cacheValidUntil = headers["Cache-Valid-Until"].empty() ? 0 : std::stoul(headers["Cache-Valid-Until"]);
265 }
266 }
267 // cached object is fine
268 auto cacheId = helper->mapURL2DPLCache[path];
269 O2_SIGNPOST_EVENT_EMIT(ccdb, sid, "populateCacheWith", "Reusing %{public}s for %{public}s (DPL id %" PRIu64 ")", path.data(), headers["ETag"].data(), cacheId.value);
270 helper->mapURL2UUID[path].cacheHit++;
271 responses.emplace_back(Response{.id = cacheId, .size = helper->mapURL2UUID[path].size, .request = nullptr});
272 allocator.adoptFromCache(output, cacheId, header::gSerializationMethodCCDB);
273 // the outputBuffer was not used, can we destroy it?
274 }
275 O2_SIGNPOST_END(ccdb, sid, "populateCacheWith", "Finished populating cache with CCDB objects");
276 return responses;
277};
278
279} // namespace o2::framework
benchmark::State & state
size_t cachePopulatedAt
std::string etag
uint32_t op
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
uint16_t pos
Definition RawData.h:3
uint32_t c
Definition RawData.h:2
#define O2_DECLARE_DYNAMIC_LOG(name)
Definition Signpost.h:489
#define O2_SIGNPOST_END(log, id, name, format,...)
Definition Signpost.h:608
#define O2_SIGNPOST_EVENT_EMIT(log, id, name, format,...)
Definition Signpost.h:522
#define O2_SIGNPOST_START(log, id, name, format,...)
Definition Signpost.h:602
StringRef key
const GLfloat * m
Definition glcorearb.h:4066
GLuint64EXT * result
Definition glcorearb.h:5662
GLuint entry
Definition glcorearb.h:5735
GLsizeiptr size
Definition glcorearb.h:659
const GLdouble * v
Definition glcorearb.h:832
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
bool isOnlineRun(DataTakingContext const &dtc)
RuntimeErrorRef runtime_error_f(const char *,...)
constexpr o2::header::SerializationMethod gSerializationMethodCCDB
Definition DataHeader.h:329
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
o2::ccdb::CcdbApi & getAPI(const std::string &path)
std::unordered_map< std::string, std::string > remappings
static auto populateCacheWith(std::shared_ptr< CCDBFetcherHelper > const &helper, std::vector< FetchOp > const &ops, TimingInfo &timingInfo, DataTakingContext &dtc, DataAllocator &allocator) -> std::vector< Response >
std::unordered_map< std::string, o2::ccdb::CcdbApi > apis
static void initialiseHelper(CCDBFetcherHelper &helper, ConfigParamRegistry const &options)
static ParserResult parseRemappings(char const *)
static std::string describe(InputSpec const &spec)
static ConcreteDataMatcher asConcreteDataMatcher(InputSpec const &input)
DeploymentMode deploymentMode
Where we thing this is running.
header::DataOrigin origin
Definition Output.h:28
const std::string str