149 std::string parentFileReplacement;
150 if (ctx.options().isSet(
"aod-parent-base-path-replacement")) {
151 parentFileReplacement = ctx.options().get<std::string>(
"aod-parent-base-path-replacement");
153 int parentAccessLevel = 0;
154 if (ctx.options().isSet(
"aod-parent-access-level")) {
155 parentAccessLevel = ctx.options().get<
int>(
"aod-parent-access-level");
157 std::vector<std::pair<std::string, int>> originLevelMapping;
158 if (ctx.options().isSet(
"aod-origin-level-mapping")) {
159 auto originLevelMappingStr = ctx.options().get<std::string>(
"aod-origin-level-mapping");
160 for (
auto pairRange : originLevelMappingStr | std::views::split(
',')) {
161 std::string_view pair{pairRange.begin(), pairRange.end()};
162 auto colonPos = pair.find(
':');
163 if (colonPos == std::string_view::npos) {
164 LOGP(fatal,
"Badly formatted aod-origin-level-mapping entry: \"{}\"", pair);
167 std::string
key(pair.substr(0, colonPos));
168 std::string_view valueStr = pair.substr(colonPos + 1);
170 auto [
ptr, ec] = std::from_chars(valueStr.data(), valueStr.data() + valueStr.size(),
value);
171 if (ec == std::errc{}) {
172 originLevelMapping.emplace_back(std::move(
key),
value);
174 LOGP(fatal,
"Unable to parse level in aod-origin-level-mapping entry: \"{}\"", pair);
191 if (!options.
isSet(
"aod-file-private")) {
192 LOGP(fatal,
"No input file defined!");
193 throw std::runtime_error(
"Processing is stopped!");
196 auto filename = options.
get<std::string>(
"aod-file-private");
198 auto maxRate = options.
get<
float>(
"aod-max-io-rate");
201 auto didir = std::make_shared<DataInputDirector>(std::vector<std::string>{
filename},
DataInputDirectorContext{&monitoring, parentAccessLevel, parentFileReplacement, originLevelMapping});
202 if (options.
isSet(
"aod-reader-json")) {
203 auto jsonFile = options.
get<std::string>(
"aod-reader-json");
204 if (!didir->readJson(jsonFile)) {
205 LOGP(error,
"Check the JSON document! Can not be properly parsed!");
214 bool reportTFN =
false;
215 bool reportTFFileName =
false;
218 std::vector<OutputRoute> requestedTables;
220 for (
auto route :
routes) {
223 TFNumberHeader =
header::DataHeader(concrete.description, concrete.origin, concrete.subSpec);
227 TFFileNameHeader =
header::DataHeader(concrete.description, concrete.origin, concrete.subSpec);
228 reportTFFileName =
true;
230 requestedTables.emplace_back(route);
233 int level = originLevelMapping.empty() ? -1 : 0;
234 auto fileCounter = std::make_shared<int>(0);
235 auto numTF = std::make_shared<int>(-1);
236 bool const skipInvalidReads = shouldSkipInvalidReads();
245 didir, reportTFN, reportTFFileName,
level](
Monitoring& monitoring,
DataAllocator& outputs,
ControlService& control,
DeviceSpec const& device,
DataProcessingStats& dpstats,
ArrowContext& arrowContext,
MessageContext& messageContext,
StringContext& stringContext) {
250 int ntf = *numTF + 1;
251 static int currentFileCounter = -1;
252 static int filesProcessed = 0;
253 if (currentFileCounter != *fileCounter) {
254 currentFileCounter = *fileCounter;
255 monitoring.send(
Metric{(uint64_t)++filesProcessed,
"files-opened"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
259 static size_t totalSizeUncompressed = 0;
260 static size_t totalSizeCompressed = 0;
261 static uint64_t totalDFSent = 0;
262 static uint64_t totalInvalidReadSkipped = 0;
263 static uint64_t totalFileOpenSkipped = 0;
266 if (!watchdog->update()) {
267 LOGP(info,
"Run time exceeds run time limit of {} seconds. Exiting gracefully...", watchdog->runTimeLimit);
268 LOGP(info,
"Stopping reader {} after time frame {}.", device.
inputTimesliceId, watchdog->numberTimeFrames - 1);
269 didir->closeInputFiles();
270 monitoring.flushBuffer();
276 int64_t startTime = uv_hrtime();
277 int64_t startSize = totalSizeCompressed;
278 auto discardTimeframe = [&didir, &arrowContext, &messageContext, &stringContext]() {
280 didir->finishTimeFrame(
true);
281 arrowContext.clear();
282 messageContext.discard();
283 stringContext.
clear();
285 auto skipInvalidRead = [&fcnt, &ntf, &discardTimeframe, &dpstats, &fileCounter, &numTF, &device](
ConcreteDataMatcher const& concrete,
AODReadError const& e) {
286 auto skippedTimeframes = ++totalInvalidReadSkipped;
287 LOGP(error,
"Invalid AOD read for table {}: fileCounter {}, timeFrame {}. Skipping timeframe (skipped timeframes: {}). Reason: {}",
288 concrete.origin.as<std::string>(), fcnt, ntf, skippedTimeframes, describeException(e));
294 enum class TFReaderState {
296 READ_FIRST_TABLE_FROM_NEXT_FILE,
303 auto readState = TFReaderState::READ_FIRST_TABLE;
304 [[maybe_unused]]
auto stateName = [](TFReaderState
state) ->
char const* {
306 case TFReaderState::READ_FIRST_TABLE:
307 return "READ_FIRST_TABLE";
308 case TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE:
309 return "READ_FIRST_TABLE_FROM_NEXT_FILE";
310 case TFReaderState::READ_NEXT_TABLE:
311 return "READ_NEXT_TABLE";
312 case TFReaderState::TRY_NEXT_FILE:
313 return "TRY_NEXT_FILE";
314 case TFReaderState::TIMEFRAME_READ:
315 return "TIMEFRAME_READ";
316 case TFReaderState::INVALID_TIMEFRAME:
317 return "INVALID_TIMEFRAME";
318 case TFReaderState::INVALID_FILE:
319 return "INVALID_FILE";
324 auto transitionTo = [&](TFReaderState nextState) {
326 "%{public}s -> %{public}s (fileCounter %d, timeFrame %d)",
327 stateName(readState), stateName(nextState), fcnt, ntf);
328 readState = nextState;
330 size_t routeIndex = 0;
331 auto reportTimeframe = [&didir, &fcnt, &ntf, &outputs, &TFNumberHeader, &TFFileNameHeader, reportTFN, reportTFFileName](
header::DataHeader const& dh) {
334 auto timeFrameNumber = didir->getTimeFrameNumber(dh, fcnt, ntf);
335 auto o =
Output(TFNumberHeader);
336 outputs.make<uint64_t>(o) = timeFrameNumber;
339 if (reportTFFileName) {
342 auto fileAndFolder = didir->getFileFolder(dh, fcnt, ntf);
343 auto rootFS = std::dynamic_pointer_cast<TFileFileSystem>(fileAndFolder.filesystem());
344 auto*
f =
dynamic_cast<TFile*
>(rootFS->GetFile());
345 std::string currentFilename(
f->GetFile()->GetName());
346 if (strcmp(
f->GetEndpointUrl()->GetProtocol(),
"file") == 0 &&
f->GetEndpointUrl()->GetFile()[0] !=
'/') {
348 static std::string pwd = gSystem->pwd() + std::string(
"/");
349 currentFilename = pwd + std::string(
f->GetName());
351 outputs.make<std::string>(
o2) = currentFilename;
354 auto tryReadTable = [&device, &didir, &fcnt, &ntf, &outputs, &reportTimeframe, &requestedTables, &routeIndex, &skipInvalidRead, &discardTimeframe, &dpstats, skipInvalidReads](TFReaderState currentState) -> TFReaderState {
355 while (routeIndex < requestedTables.size() &&
356 (device.
inputTimesliceId % requestedTables[routeIndex].maxTimeslices) != requestedTables[routeIndex].timeslice) {
359 if (routeIndex == requestedTables.size()) {
360 return TFReaderState::TIMEFRAME_READ;
363 auto& route = requestedTables[routeIndex];
365 auto dh =
header::DataHeader(concrete.description, concrete.origin, concrete.subSpec);
366 bool wasAOD = std::ranges::any_of(route.matcher.metadata, [](
ConfigParamSpec const& p) { return p.name.starts_with(
"aod-origin-replaced"); });
368 if (currentState == TFReaderState::READ_FIRST_TABLE || currentState == TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE) {
369 didir->beginTimeFrame();
372 if (!didir->readTree(outputs, dh, fcnt, ntf, totalSizeCompressed, totalSizeUncompressed, wasAOD)) {
373 return TFReaderState::TRY_NEXT_FILE;
376 if (!skipInvalidReads) {
381 skipInvalidRead(concrete, e);
382 return TFReaderState::INVALID_TIMEFRAME;
384 auto skippedFiles = ++totalFileOpenSkipped;
385 LOGP(error,
"Unable to open AOD input for table {}: fileCounter {}, timeFrame {}. Skipping file (skipped files: {}). Reason: {}",
386 concrete.origin.as<std::string>(), fcnt, ntf, skippedFiles, describeException(e));
389 return TFReaderState::INVALID_FILE;
395 if (currentState == TFReaderState::READ_FIRST_TABLE || currentState == TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE) {
399 return TFReaderState::READ_NEXT_TABLE;
401 while (readState != TFReaderState::TIMEFRAME_READ) {
403 case TFReaderState::READ_FIRST_TABLE:
404 transitionTo(tryReadTable(readState));
406 case TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE:
407 case TFReaderState::READ_NEXT_TABLE:
408 transitionTo(tryReadTable(readState));
409 if (readState == TFReaderState::TRY_NEXT_FILE) {
412 LOGP(fatal,
"Can not retrieve tree for table {}: fileCounter {}, timeFrame {}", concrete.origin.as<std::string>(), fcnt, ntf);
413 throw std::runtime_error(
"Processing is stopped!");
416 case TFReaderState::INVALID_FILE:
418 messageContext.clear();
420 case TFReaderState::TRY_NEXT_FILE:
421 didir->finishTimeFrame();
423 if (didir->atEnd(fcnt)) {
424 LOGP(info,
"No input files left to read for reader {}!", device.
inputTimesliceId);
425 didir->closeInputFiles();
426 monitoring.flushBuffer();
433 transitionTo(TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE);
435 case TFReaderState::INVALID_TIMEFRAME:
437 messageContext.clear();
440 transitionTo(TFReaderState::READ_FIRST_TABLE);
442 case TFReaderState::TIMEFRAME_READ:
446 didir->finishTimeFrame();
447 int64_t stopSize = totalSizeCompressed;
448 int64_t bytesDelta = stopSize - startSize;
449 int64_t stopTime = uv_hrtime();
450 float currentDelta = float(stopTime - startTime) / 1000000000;
451 if (ceil(maxRate) > 0.) {
452 float extraTime = (bytesDelta / 1000000 - currentDelta * maxRate) / maxRate;
454 if (extraTime > 0.) {
455 LOGP(info,
"Read {} MB in {} s. Sleeping for {} seconds to stay within {} MB/s limit.", bytesDelta / 1000000, currentDelta, extraTime, maxRate);
456 uv_sleep(extraTime * 1000);
463 dpstats.processCommandQueue();
464 monitoring.send(
Metric{(uint64_t)totalDFSent,
"df-sent"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
465 monitoring.send(
Metric{(uint64_t)totalSizeUncompressed / 1000,
"aod-bytes-read-uncompressed"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
466 monitoring.send(
Metric{(uint64_t)totalSizeCompressed / 1000,
"aod-bytes-read-compressed"}.addTag(Key::Subsystem, monitoring::tags::Value::DPL));
476 auto firstRoute = std::ranges::find_if(requestedTables, [&didir,
level](
auto const& route) {
478 return didir->getLevelForOrigin(concrete.origin) ==
level;
481 auto dh =
header::DataHeader(concrete.description, concrete.origin, concrete.subSpec);
482 auto fileAndFolder = didir->getFileFolder(dh, fcnt, ntf);
485 if (fileAndFolder.filesystem() ==
nullptr) {
488 if (didir->atEnd(fcnt)) {
489 LOGP(info,
"No input files left to read for reader {}!", device.
inputTimesliceId);
490 didir->closeInputFiles();
491 monitoring.flushBuffer();