146 std::string parentFileReplacement;
147 if (ctx.options().isSet(
"aod-parent-base-path-replacement")) {
148 parentFileReplacement = ctx.options().get<std::string>(
"aod-parent-base-path-replacement");
150 int parentAccessLevel = 0;
151 if (ctx.options().isSet(
"aod-parent-access-level")) {
152 parentAccessLevel = ctx.options().get<
int>(
"aod-parent-access-level");
154 std::vector<std::pair<std::string, int>> originLevelMapping;
155 if (ctx.options().isSet(
"aod-origin-level-mapping")) {
156 auto originLevelMappingStr = ctx.options().get<std::string>(
"aod-origin-level-mapping");
157 for (
auto pairRange : originLevelMappingStr | std::views::split(
',')) {
158 std::string_view pair{pairRange.begin(), pairRange.end()};
159 auto colonPos = pair.find(
':');
160 if (colonPos == std::string_view::npos) {
161 LOGP(fatal,
"Badly formatted aod-origin-level-mapping entry: \"{}\"", pair);
164 std::string
key(pair.substr(0, colonPos));
165 std::string_view valueStr = pair.substr(colonPos + 1);
167 auto [
ptr, ec] = std::from_chars(valueStr.data(), valueStr.data() + valueStr.size(),
value);
168 if (ec == std::errc{}) {
169 originLevelMapping.emplace_back(std::move(
key),
value);
171 LOGP(fatal,
"Unable to parse level in aod-origin-level-mapping entry: \"{}\"", pair);
188 if (!options.
isSet(
"aod-file-private")) {
189 LOGP(fatal,
"No input file defined!");
190 throw std::runtime_error(
"Processing is stopped!");
193 auto filename = options.
get<std::string>(
"aod-file-private");
195 auto maxRate = options.
get<
float>(
"aod-max-io-rate");
198 auto didir = std::make_shared<DataInputDirector>(std::vector<std::string>{
filename},
DataInputDirectorContext{&monitoring, parentAccessLevel, parentFileReplacement, originLevelMapping});
199 if (options.
isSet(
"aod-reader-json")) {
200 auto jsonFile = options.
get<std::string>(
"aod-reader-json");
201 if (!didir->readJson(jsonFile)) {
202 LOGP(error,
"Check the JSON document! Can not be properly parsed!");
211 bool reportTFN =
false;
212 bool reportTFFileName =
false;
215 std::vector<OutputRoute> requestedTables;
217 for (
auto route :
routes) {
220 TFNumberHeader =
header::DataHeader(concrete.description, concrete.origin, concrete.subSpec);
224 TFFileNameHeader =
header::DataHeader(concrete.description, concrete.origin, concrete.subSpec);
225 reportTFFileName =
true;
227 requestedTables.emplace_back(route);
230 int level = originLevelMapping.empty() ? -1 : 0;
231 auto fileCounter = std::make_shared<int>(0);
232 auto numTF = std::make_shared<int>(-1);
233 bool const skipInvalidReads = shouldSkipInvalidReads();
242 didir, reportTFN, reportTFFileName,
level](
Monitoring& monitoring,
DataAllocator& outputs,
ControlService& control,
DeviceSpec const& device,
DataProcessingStats& dpstats,
ArrowContext& arrowContext,
MessageContext& messageContext,
StringContext& stringContext) {
247 int ntf = *numTF + 1;
248 static int currentFileCounter = -1;
249 static int filesProcessed = 0;
250 if (currentFileCounter != *fileCounter) {
251 currentFileCounter = *fileCounter;
252 monitoring.send(
Metric{(uint64_t)++filesProcessed,
"files-opened"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
256 static size_t totalSizeUncompressed = 0;
257 static size_t totalSizeCompressed = 0;
258 static uint64_t totalDFSent = 0;
259 static uint64_t totalInvalidReadSkipped = 0;
262 if (!watchdog->update()) {
263 LOGP(info,
"Run time exceeds run time limit of {} seconds. Exiting gracefully...", watchdog->runTimeLimit);
264 LOGP(info,
"Stopping reader {} after time frame {}.", device.
inputTimesliceId, watchdog->numberTimeFrames - 1);
265 didir->closeInputFiles();
266 monitoring.flushBuffer();
272 int64_t startTime = uv_hrtime();
273 int64_t startSize = totalSizeCompressed;
275 auto skippedTimeframes = ++totalInvalidReadSkipped;
276 LOGP(error,
"Invalid AOD read for table {}: fileCounter {}, timeFrame {}. Skipping timeframe (skipped timeframes: {}). Reason: {}",
277 concrete.
origin.
as<std::string>(), fcnt, ntf, skippedTimeframes, describeException(e));
279 arrowContext.
clear();
281 stringContext.
clear();
286 enum class TFReaderState {
288 READ_FIRST_TABLE_FROM_NEXT_FILE,
294 auto readState = TFReaderState::READ_FIRST_TABLE;
295 [[maybe_unused]]
auto stateName = [](TFReaderState
state) ->
char const* {
297 case TFReaderState::READ_FIRST_TABLE:
298 return "READ_FIRST_TABLE";
299 case TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE:
300 return "READ_FIRST_TABLE_FROM_NEXT_FILE";
301 case TFReaderState::READ_NEXT_TABLE:
302 return "READ_NEXT_TABLE";
303 case TFReaderState::TRY_NEXT_FILE:
304 return "TRY_NEXT_FILE";
305 case TFReaderState::TIMEFRAME_READ:
306 return "TIMEFRAME_READ";
307 case TFReaderState::INVALID_TIMEFRAME:
308 return "INVALID_TIMEFRAME";
313 auto transitionTo = [&](TFReaderState nextState) {
315 "%{public}s -> %{public}s (fileCounter %d, timeFrame %d)",
316 stateName(readState), stateName(nextState), fcnt, ntf);
317 readState = nextState;
319 size_t routeIndex = 0;
320 auto reportTimeframe = [&didir, &fcnt, &ntf, &outputs, &TFNumberHeader, &TFFileNameHeader, reportTFN, reportTFFileName](
header::DataHeader const& dh) {
323 auto timeFrameNumber = didir->getTimeFrameNumber(dh, fcnt, ntf);
324 auto o =
Output(TFNumberHeader);
325 outputs.make<uint64_t>(o) = timeFrameNumber;
328 if (reportTFFileName) {
331 auto fileAndFolder = didir->getFileFolder(dh, fcnt, ntf);
332 auto rootFS = std::dynamic_pointer_cast<TFileFileSystem>(fileAndFolder.filesystem());
333 auto*
f =
dynamic_cast<TFile*
>(rootFS->GetFile());
334 std::string currentFilename(
f->GetFile()->GetName());
335 if (strcmp(
f->GetEndpointUrl()->GetProtocol(),
"file") == 0 &&
f->GetEndpointUrl()->GetFile()[0] !=
'/') {
337 static std::string pwd = gSystem->pwd() + std::string(
"/");
338 currentFilename = pwd + std::string(
f->GetName());
340 outputs.make<std::string>(
o2) = currentFilename;
343 auto tryReadTable = [&device, &didir, &fcnt, &ntf, &outputs, &reportTimeframe, &requestedTables, &routeIndex, &skipInvalidRead, skipInvalidReads](TFReaderState currentState) -> TFReaderState {
344 while (routeIndex < requestedTables.size() &&
345 (device.
inputTimesliceId % requestedTables[routeIndex].maxTimeslices) != requestedTables[routeIndex].timeslice) {
348 if (routeIndex == requestedTables.size()) {
349 return TFReaderState::TIMEFRAME_READ;
352 auto& route = requestedTables[routeIndex];
355 bool wasAOD = std::ranges::any_of(route.matcher.metadata, [](
ConfigParamSpec const& p) { return p.name.starts_with(
"aod-origin-replaced"); });
358 if (!didir->readTree(outputs, dh, fcnt, ntf, totalSizeCompressed, totalSizeUncompressed, wasAOD)) {
359 return TFReaderState::TRY_NEXT_FILE;
362 if (!skipInvalidReads) {
365 skipInvalidRead(concrete, e);
366 return TFReaderState::INVALID_TIMEFRAME;
369 if (currentState == TFReaderState::READ_FIRST_TABLE || currentState == TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE) {
373 return TFReaderState::READ_NEXT_TABLE;
375 while (readState != TFReaderState::TIMEFRAME_READ) {
377 case TFReaderState::READ_FIRST_TABLE:
378 transitionTo(tryReadTable(readState));
380 case TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE:
381 case TFReaderState::READ_NEXT_TABLE:
382 transitionTo(tryReadTable(readState));
383 if (readState == TFReaderState::TRY_NEXT_FILE) {
386 LOGP(fatal,
"Can not retrieve tree for table {}: fileCounter {}, timeFrame {}", concrete.
origin.
as<std::string>(), fcnt, ntf);
387 throw std::runtime_error(
"Processing is stopped!");
390 case TFReaderState::TRY_NEXT_FILE:
392 if (didir->atEnd(fcnt)) {
393 LOGP(info,
"No input files left to read for reader {}!", device.
inputTimesliceId);
394 didir->closeInputFiles();
395 monitoring.flushBuffer();
402 transitionTo(TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE);
404 case TFReaderState::INVALID_TIMEFRAME:
406 case TFReaderState::TIMEFRAME_READ:
410 int64_t stopSize = totalSizeCompressed;
411 int64_t bytesDelta = stopSize - startSize;
412 int64_t stopTime = uv_hrtime();
413 float currentDelta = float(stopTime - startTime) / 1000000000;
414 if (ceil(maxRate) > 0.) {
415 float extraTime = (bytesDelta / 1000000 - currentDelta * maxRate) / maxRate;
417 if (extraTime > 0.) {
418 LOGP(info,
"Read {} MB in {} s. Sleeping for {} seconds to stay within {} MB/s limit.", bytesDelta / 1000000, currentDelta, extraTime, maxRate);
419 uv_sleep(extraTime * 1000);
427 monitoring.send(
Metric{(uint64_t)totalDFSent,
"df-sent"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
428 monitoring.send(
Metric{(uint64_t)totalSizeUncompressed / 1000,
"aod-bytes-read-uncompressed"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
429 monitoring.send(
Metric{(uint64_t)totalSizeCompressed / 1000,
"aod-bytes-read-compressed"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
439 auto firstRoute = std::ranges::find_if(requestedTables, [&didir,
level](
auto const& route) {
441 return didir->getLevelForOrigin(concrete.
origin) ==
level;
445 auto fileAndFolder = didir->getFileFolder(dh, fcnt, ntf);
448 if (fileAndFolder.filesystem() ==
nullptr) {
451 if (didir->atEnd(fcnt)) {
452 LOGP(info,
"No input files left to read for reader {}!", device.
inputTimesliceId);
453 didir->closeInputFiles();
454 monitoring.flushBuffer();