From 132b4e6ae6ea232635713429289181ae88b1455c Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Thu, 3 Dec 2026 11:09:22 +0100 Subject: [PATCH 1/2] DPL: do not let a completed input channel hold back the others A time-pipelined source whose lane ends early, e.g. an AOD reader lane with no files left with --readers 2, leaves a completed input channel in its consumers whose oldest possible timeslice never advances again. Before, that stale value was still used: - as the reference of the channel balancing in doPrepare: the channel of the live lane was suspended once it was ahead of it, and, as no polled channel was still running, the consumer went to EndOfStream with data still in flight; - in the minimum of TimesliceIndex::setOldestPossibleInput: the oldest possible output stopped advancing, so the dummy sink stopped reporting consumed timeslices and the rate limiting stopped offering them. Either way the remaining reader lane starved forever on its shared memory or timeslice offers, and the workflow hung. A completed channel with nothing left to relay is now ignored for both, and the oldest possible input is recomputed when a channel gets there. A channel whose end of stream arrived behind backpressured data still counts until that data is relayed. The timeslices of the ended lane are then skipped by ordered consumers (e.g. the AOD writer) through the oldest possible timeslice, which happens asynchronously: at end of stream, when nothing can arrive anymore, skip to the oldest pending timeslice directly, or the last pending ones are dropped. Add a workflow test where one of two pipelined source lanes ends at once: before, its consumer got 10 of 100 timeframes. At end of stream a device drains its pending inputs in doRun, calling tryDispatchComputation until nothing is left. The first of those calls which processed something broadcast the end of stream at its end and went Idle, so whatever the later calls produced or forwarded went out after it, and the end of stream was then sent a second time by doRun. Downstream the channel was already completed, and that data was dropped. This happens systematically to ordered consumers with outputs or forwards, e.g. behind a time pipelined source with a lane which ended, as their last timeslices are only released by the drain. tryDispatchComputation no longer sends the end of stream when called from the drain: doRun sends it once, after all the output. The test gets a consumer of the output of the ordered consumer, which must receive all timeframes. --- Framework/Core/CMakeLists.txt | 1 + .../include/Framework/DataProcessingDevice.h | 3 +- Framework/Core/src/DataProcessingDevice.cxx | 36 +++++++-- Framework/Core/src/TimesliceIndex.cxx | 12 ++- .../test/test_PipelinedSourceEndOfStream.cxx | 74 +++++++++++++++++++ 5 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 Framework/Core/test/test_PipelinedSourceEndOfStream.cxx diff --git a/Framework/Core/CMakeLists.txt b/Framework/Core/CMakeLists.txt index b31e951bb7ec1..fa22f4434fb19 100644 --- a/Framework/Core/CMakeLists.txt +++ b/Framework/Core/CMakeLists.txt @@ -394,6 +394,7 @@ foreach(w Forwarding ParallelPipeline ParallelProducer + PipelinedSourceEndOfStream SlowConsumer SlowProducerWithWildCard SimpleDataProcessingDevice01 diff --git a/Framework/Core/include/Framework/DataProcessingDevice.h b/Framework/Core/include/Framework/DataProcessingDevice.h index b2281274acc87..436f17a8aa094 100644 --- a/Framework/Core/include/Framework/DataProcessingDevice.h +++ b/Framework/Core/include/Framework/DataProcessingDevice.h @@ -90,7 +90,8 @@ class DataProcessingDevice : public fair::mq::Device static void doRun(ServiceRegistryRef); static void doPrepare(ServiceRegistryRef); static void handleData(ServiceRegistryRef, InputChannelInfo&); - static bool tryDispatchComputation(ServiceRegistryRef ref, std::vector& completed); + /// @a drainingAtEndOfStream: the caller sends the end of stream itself, once all is processed. + static bool tryDispatchComputation(ServiceRegistryRef ref, std::vector& completed, bool drainingAtEndOfStream = false); protected: void error(const char* msg); diff --git a/Framework/Core/src/DataProcessingDevice.cxx b/Framework/Core/src/DataProcessingDevice.cxx index b181eda458056..43de1388f95fd 100644 --- a/Framework/Core/src/DataProcessingDevice.cxx +++ b/Framework/Core/src/DataProcessingDevice.cxx @@ -1550,7 +1550,9 @@ void DataProcessingDevice::doPrepare(ServiceRegistryRef ref) O2_SIGNPOST_END(device, dpid, "do_prepare", "Nothing to poll. Waiting for next iteration."); return; } - auto currentOldest = state.inputChannelInfos[pollOrder.front()].oldestForChannel; + // A completed channel with nothing left to relay will not send anything anymore, so it must not hold back the others. + auto oldestRunning = std::ranges::find_if(pollOrder, [&infos = state.inputChannelInfos](int a) { return infos[a].state != InputChannelState::Completed || infos[a].parts.Size(); }); + auto currentOldest = state.inputChannelInfos[oldestRunning != pollOrder.end() ? *oldestRunning : pollOrder.front()].oldestForChannel; auto currentNewest = state.inputChannelInfos[pollOrder.back()].oldestForChannel; auto delta = currentNewest.value - currentOldest.value; O2_SIGNPOST_EVENT_EMIT(device, dpid, "do_prepare", "Oldest possible timeframe range %" PRIu64 " => %" PRIu64 " delta %" PRIu64, @@ -1729,8 +1731,27 @@ void DataProcessingDevice::doRun(ServiceRegistryRef ref) bool shouldProcess = DataProcessingHelpers::hasOnlyGenerated(spec) == false; - while (DataProcessingDevice::tryDispatchComputation(ref, streamContext.completed) && shouldProcess) { - relayer.processDanglingInputs(context.expirationHandlers, *context.registry, false); + auto& decongestion = ref.get(); + while (true) { + while (DataProcessingDevice::tryDispatchComputation(ref, streamContext.completed, true) && shouldProcess) { + relayer.processDanglingInputs(context.expirationHandlers, *context.registry, false); + } + // No input can arrive anymore, so an ordered consumer must not wait for the timeslices it misses. + if (!shouldProcess || !decongestion.orderedCompletionPolicyActive) { + break; + } + auto& index = ref.get(); + auto oldest = std::numeric_limits::max(); + for (size_t si = 0; si < index.size(); si++) { + if (index.isValid(TimesliceSlot{si})) { + oldest = std::min(oldest, (int64_t)VariableContextHelpers::getTimeslice(index.getVariablesForSlot(TimesliceSlot{si})).value); + } + } + if (oldest == std::numeric_limits::max() || oldest <= decongestion.nextTimeslice) { + break; + } + decongestion.nextTimeslice = oldest; + index.rescan(); } auto& timingInfo = ref.get(); @@ -2115,6 +2136,11 @@ void DataProcessingDevice::handleData(ServiceRegistryRef ref, InputChannelInfo& } auto it = std::remove_if(parts.fParts.begin(), parts.fParts.end(), [](auto& msg) -> bool { return msg.get() == nullptr; }); parts.fParts.erase(it, parts.end()); + // Once completed with nothing left to relay, the channel no longer holds back the oldest possible input. + if (info.channelType == ChannelAccountingType::DPL && info.state == InputChannelState::Completed && parts.Size() == 0) { + auto& context = ref.get(); + context.domainInfoUpdatedCallback(*context.registry, info.oldestForChannel.value, info.id); + } if (parts.fParts.size()) { LOG(debug) << parts.fParts.size() << " messages backpressured"; } @@ -2186,7 +2212,7 @@ void update_maximum(std::atomic& maximum_value, T const& value) noexcept } } // namespace -bool DataProcessingDevice::tryDispatchComputation(ServiceRegistryRef ref, std::vector& completed) +bool DataProcessingDevice::tryDispatchComputation(ServiceRegistryRef ref, std::vector& completed, bool drainingAtEndOfStream) { auto& context = ref.get(); LOGP(debug, "DataProcessingDevice::tryDispatchComputation"); @@ -2620,7 +2646,7 @@ bool DataProcessingDevice::tryDispatchComputation(ServiceRegistryRef ref, std::v O2_SIGNPOST_END(device, sid, "device", "Start processing ready actions"); // We now broadcast the end of stream if it was requested - if (state.streaming == StreamingState::EndOfStreaming) { + if (state.streaming == StreamingState::EndOfStreaming && !drainingAtEndOfStream) { LOGP(detail, "Broadcasting end of stream"); for (auto& channel : spec.outputChannels) { DataProcessingHelpers::sendEndOfStream(ref, channel); diff --git a/Framework/Core/src/TimesliceIndex.cxx b/Framework/Core/src/TimesliceIndex.cxx index df98eea3cc9d0..1bfe5fa79d94e 100644 --- a/Framework/Core/src/TimesliceIndex.cxx +++ b/Framework/Core/src/TimesliceIndex.cxx @@ -148,12 +148,14 @@ TimesliceIndex::OldestInputInfo TimesliceIndex::setOldestPossibleInput(Timeslice timestamp.value, channel.value, mChannels[channel.value].oldestForChannel.value); } mChannels[channel.value].oldestForChannel = timestamp; - OldestInputInfo result{timestamp, channel}; + // A completed channel with nothing left to relay will not send anything anymore. + auto done = [](InputChannelInfo const& info) { return info.state == InputChannelState::Completed && info.parts.Size() == 0; }; + OldestInputInfo result{done(mChannels[channel.value]) ? TimesliceId{TimesliceId::INVALID} : timestamp, channel}; bool changed = false; for (int ci = 0; ci < mChannels.size(); ci++) { - // Check if this is a real channel. Skip otherwise. + // Check if this is a real channel which can still send data. Skip otherwise. auto& channelRef = mChannels[ci]; - if (channelRef.channelType != ChannelAccountingType::DPL) { + if (channelRef.channelType != ChannelAccountingType::DPL || done(channelRef)) { continue; } auto& a = channelRef.oldestForChannel; @@ -162,6 +164,10 @@ TimesliceIndex::OldestInputInfo TimesliceIndex::setOldestPossibleInput(Timeslice result = {a, ChannelIndex{ci}}; } } + if (TimesliceId::isValid(result.timeslice) == false) { + // Every channel completed: nothing can arrive anymore. + result = mOldestPossibleInput; + } if (changed && mOldestPossibleInput.timeslice.value != result.timeslice.value) { O2_SIGNPOST_EVENT_EMIT(timeslice_index, tid, "setOldestPossibleInput", "Success (channel %d): Oldest possible input is %zu due to channel %d", channel.value, result.timeslice.value, result.channel.value); diff --git a/Framework/Core/test/test_PipelinedSourceEndOfStream.cxx b/Framework/Core/test/test_PipelinedSourceEndOfStream.cxx new file mode 100644 index 0000000000000..f8d1688c89228 --- /dev/null +++ b/Framework/Core/test/test_PipelinedSourceEndOfStream.cxx @@ -0,0 +1,74 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// 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. +#include "Framework/CompletionPolicyHelpers.h" + +void customize(std::vector& policies) +{ + policies.push_back(o2::framework::CompletionPolicyHelpers::consumeWhenAllOrdered("ordered-consumer")); +} + +#include "Framework/CallbackService.h" +#include "Framework/ControlService.h" +#include "Framework/DeviceSpec.h" +#include "Framework/EndOfStreamContext.h" +#include "Framework/Logger.h" +#include "Framework/runDataProcessing.h" + +using namespace o2::framework; + +// A source with two time-pipelined lanes, like the AOD reader with --readers 2, +// where one lane has nothing to read and ends immediately. The other lane must +// still deliver all its data: the completed channel must hold back neither the +// polling of the other channel nor the oldest possible timeslice. +// ordered-consumer sends what it gets on to after-ordered, which must get all of +// it before the end of stream of ordered-consumer. +constexpr int nTimeframes = 100; + +DataProcessorSpec consumer(std::string name, InputSpec input = {"data", "TST", "PIPELINED"}, std::vector outputs = {}) +{ + return DataProcessorSpec{ + .name = name, + .inputs = {input}, + .outputs = outputs, + .algorithm = adaptStateful([relay = !outputs.empty()](CallbackService& callbacks) { + static int received = 0; + callbacks.set([](EndOfStreamContext&) { + if (received != nTimeframes) { + LOGP(fatal, "Received {} timeframes, expected {}", received, nTimeframes); + } + }); + return adaptStateless([relay](InputRecord& inputs, DataAllocator& allocator) { + received++; + if (relay) { + allocator.make(OutputRef{"out"}) = inputs.get("data"); + } + }); + })}; +} + +WorkflowSpec defineDataProcessing(ConfigContext const&) +{ + return WorkflowSpec{ + timePipeline(DataProcessorSpec{ + .name = "producer", + .outputs = {OutputSpec{{"data"}, "TST", "PIPELINED"}}, + .algorithm = adaptStateless([](DataAllocator& outputs, ControlService& control, DeviceSpec const& spec) { + static int sent = 0; + if (spec.inputTimesliceId == 1 || sent == nTimeframes) { + control.endOfStream(); + control.readyToQuit(QuitRequest::Me); + return; + } + outputs.make(Output{"TST", "PIPELINED", 0}) = sent++; + })}, + 2), + consumer("consumer"), consumer("ordered-consumer", {"data", "TST", "PIPELINED"}, {OutputSpec{{"out"}, "TST", "ORDERED"}}), consumer("after-ordered", {"data", "TST", "ORDERED"})}; +} From ed98476d57ee84108458dca0de7eea39326363a4 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:07:03 +0100 Subject: [PATCH 2/2] DPL: do not report the timeslices of an ended source lane as dropped The lanes of a time pipelined source driven by an enumeration, e.g. the AOD reader with --readers N, get the timeslices t with t % N == lane. When a lane ends before the others, e.g. a reader lane with no files left, the ordered consumers downstream (e.g. the AOD writer) skip its timeslices through the oldest possible timeslice, and reported each of them as CRITICAL "Some Lifetime::Timeframe data got dropped", although they were never produced. Downstream, such a skip cannot be told from a real drop. Each device now knows L, the lcm of the lane counts of such sources upstream of it, computed with the topology. With L == 1, as in all topologies without them, a skip is reported at once as before. With L > 1 the oldest skipped timeslice is kept per t % L, and reported only when a later timeslice of the same residue is consumed: its lane was then alive, and the skipped timeslice was really lost. The report comes up to L timeslices later, and a loss at the very end of a lane is not reported there (the device which dropped it still reports it). The skip to the oldest pending timeslice at end of stream is reported the same way, rather than silently. The test gets an ordered consumer fed by the lanes directly, and one behind a device which drops a timeframe on request; neither must report the ended lane. Two more runs check that a timeframe missing on the live lane, or dropped upstream, is still reported. --- Framework/Core/CMakeLists.txt | 9 +++++ .../include/Framework/DataProcessingHelpers.h | 4 ++ Framework/Core/include/Framework/DeviceSpec.h | 3 ++ Framework/Core/src/CommonServices.cxx | 10 ++--- .../Core/src/CompletionPolicyHelpers.cxx | 3 +- Framework/Core/src/DataProcessingDevice.cxx | 2 +- Framework/Core/src/DataProcessingHelpers.cxx | 38 ++++++++++++++++++ Framework/Core/src/DecongestionService.h | 4 ++ Framework/Core/src/DeviceSpecHelpers.cxx | 12 ++++++ .../test/test_PipelinedSourceEndOfStream.cxx | 40 +++++++++++++------ 10 files changed, 104 insertions(+), 21 deletions(-) diff --git a/Framework/Core/CMakeLists.txt b/Framework/Core/CMakeLists.txt index fa22f4434fb19..9521a7b8ace10 100644 --- a/Framework/Core/CMakeLists.txt +++ b/Framework/Core/CMakeLists.txt @@ -432,6 +432,15 @@ if (BUILD_TESTING) # TODO: DanglingInput test not working for the moment [ERROR] Unable to relay # part. [WARN] Incoming data is already obsolete, not relaying. set_property(TEST test_Framework_test_DanglingInputs PROPERTY DISABLED TRUE) +# The ended lane never produces its timeslices, so the ordered consumers must not report them as dropped, +# but they must report a timeframe really missing on the live lane, or dropped by a device upstream. +set_property(TEST test_Framework_test_PipelinedSourceEndOfStream PROPERTY FAIL_REGULAR_EXPRESSION "CRITICAL\\] Some Lifetime::Timeframe data got dropped") +foreach(missing direct relayed) + add_test(NAME test_Framework_test_PipelinedSourceEndOfStream_${missing} + COMMAND O2test-framework-PipelinedSourceEndOfStream ${DPL_WORKFLOW_TESTS_EXTRA_OPTIONS} --run --shm-segment-size 20000000 --missing ${missing}) + set_tests_properties(test_Framework_test_PipelinedSourceEndOfStream_${missing} PROPERTIES LABELS "framework;workflow" TIMEOUT 30 + PASS_REGULAR_EXPRESSION "ordered-${missing}\\]: [^ ]*CRITICAL\\] Some Lifetime::Timeframe data got dropped starting at 20[^0-9]") +endforeach() endif() diff --git a/Framework/Core/include/Framework/DataProcessingHelpers.h b/Framework/Core/include/Framework/DataProcessingHelpers.h index e19474447ed12..dff8a896c53a1 100644 --- a/Framework/Core/include/Framework/DataProcessingHelpers.h +++ b/Framework/Core/include/Framework/DataProcessingHelpers.h @@ -47,6 +47,10 @@ struct DataProcessingHelpers { static bool sendOldestPossibleTimeframe(ServiceRegistryRef const& ref, OutputChannelInfo const& info, OutputChannelState& state, size_t timeslice); /// Broadcast the oldest possible timeslice to all channels in output static void broadcastOldestPossibleTimeslice(ServiceRegistryRef const& ref, size_t timeslice); + /// An ordered consumer skips the timeslices from @a from to @a to, excluded: report them as dropped. + static void skipTimeslices(ServiceRegistryRef const& ref, int64_t from, int64_t to); + /// An ordered consumer consumes @a timeslice: report what was skipped before in the same source lane. + static void consumeTimeslice(ServiceRegistryRef const& ref, int64_t timeslice); /// change the device StreamingState to newState static void switchState(ServiceRegistryRef const& ref, StreamingState newState); /// check if spec is a source devide diff --git a/Framework/Core/include/Framework/DeviceSpec.h b/Framework/Core/include/Framework/DeviceSpec.h index a22fc7eb1b7dc..c1f0ba98e6a35 100644 --- a/Framework/Core/include/Framework/DeviceSpec.h +++ b/Framework/Core/include/Framework/DeviceSpec.h @@ -68,6 +68,9 @@ struct DeviceSpec { size_t inputTimesliceId; /// The maximum number of time pipelining for this device. size_t maxInputTimeslices; + /// The lcm of the lane counts of the time pipelined, enumeration driven sources upstream: their + /// lane l gets the timeslices t with t % lanes == l, and a lane can end before the others. + size_t upstreamSourceLanes = 1; /// The completion policy to use for this device. CompletionPolicy completionPolicy; DispatchPolicy dispatchPolicy; diff --git a/Framework/Core/src/CommonServices.cxx b/Framework/Core/src/CommonServices.cxx index 751d7849df1c1..025d25d2d1037 100644 --- a/Framework/Core/src/CommonServices.cxx +++ b/Framework/Core/src/CommonServices.cxx @@ -597,17 +597,11 @@ auto decongestionCallbackOrdered = [](AsyncTask& task, size_t id) -> void { auto& ref = task.user().ref; auto& decongestion = ref.get(); - auto& state = ref.get(); auto& timesliceIndex = ref.get(); - O2_SIGNPOST_ID_GENERATE(cid, async_queue); int64_t oldNextTimeslice = decongestion.nextTimeslice; decongestion.nextTimeslice = std::max(decongestion.nextTimeslice, (int64_t)oldestPossibleOutput.timeslice.value); if (oldNextTimeslice != decongestion.nextTimeslice) { - if (state.transitionHandling != TransitionHandlingState::NoTransition && DefaultsHelpers::onlineDeploymentMode()) { - O2_SIGNPOST_EVENT_EMIT_WARN(async_queue, cid, "oldest_possible_timeslice", "Stop transition requested. Some Lifetime::Timeframe data got dropped starting at %" PRIi64, oldNextTimeslice); - } else { - O2_SIGNPOST_EVENT_EMIT_CRITICAL(async_queue, cid, "oldest_possible_timeslice", "Some Lifetime::Timeframe data got dropped starting at %" PRIi64, oldNextTimeslice); - } + DataProcessingHelpers::skipTimeslices(ref, oldNextTimeslice, decongestion.nextTimeslice); timesliceIndex.rescan(); } }; @@ -676,6 +670,7 @@ o2::framework::ServiceSpec break; } } + decongestion->pendingGaps.assign(services.get().upstreamSourceLanes, -1); auto& queue = services.get(); decongestion->oldestPossibleTimesliceTask = AsyncQueueHelpers::create(queue, {.name = "oldest-possible-timeslice", .score = 100}); return ServiceHandle{TypeIdHelpers::uniqueId(), decongestion, ServiceKind::Serial}; @@ -752,6 +747,7 @@ o2::framework::ServiceSpec decongestion->nextEnumerationTimesliceRewinded = false; decongestion->lastTimeslice = 0; decongestion->nextTimeslice = 0; + std::ranges::fill(decongestion->pendingGaps, -1); decongestion->oldestPossibleTimesliceTask = {0}; auto &state = services.get(); for (auto &channel : state.inputChannelInfos) { diff --git a/Framework/Core/src/CompletionPolicyHelpers.cxx b/Framework/Core/src/CompletionPolicyHelpers.cxx index d2c63cbf4c90b..08c55e9c82d23 100644 --- a/Framework/Core/src/CompletionPolicyHelpers.cxx +++ b/Framework/Core/src/CompletionPolicyHelpers.cxx @@ -11,6 +11,7 @@ #include "Framework/CompletionPolicyHelpers.h" #include "Framework/CompletionPolicy.h" +#include "Framework/DataProcessingHelpers.h" #include "Framework/DataProcessingHeader.h" #include "Framework/InputSpan.h" #include "Framework/DeviceSpec.h" @@ -191,7 +192,7 @@ CompletionPolicy CompletionPolicyHelpers::consumeWhenAllOrdered(const char* name return CompletionPolicy::CompletionOp::Retry; } } - decongestionService.nextTimeslice++; + DataProcessingHelpers::consumeTimeslice(ref, decongestionService.nextTimeslice++); return CompletionPolicy::CompletionOp::ConsumeAndRescan; }; return CompletionPolicy{name, matcher, callbackFull}; diff --git a/Framework/Core/src/DataProcessingDevice.cxx b/Framework/Core/src/DataProcessingDevice.cxx index 43de1388f95fd..fba1994bb7e0c 100644 --- a/Framework/Core/src/DataProcessingDevice.cxx +++ b/Framework/Core/src/DataProcessingDevice.cxx @@ -1750,7 +1750,7 @@ void DataProcessingDevice::doRun(ServiceRegistryRef ref) if (oldest == std::numeric_limits::max() || oldest <= decongestion.nextTimeslice) { break; } - decongestion.nextTimeslice = oldest; + DataProcessingHelpers::skipTimeslices(ref, std::exchange(decongestion.nextTimeslice, oldest), oldest); index.rescan(); } diff --git a/Framework/Core/src/DataProcessingHelpers.cxx b/Framework/Core/src/DataProcessingHelpers.cxx index a29e7b345c94c..ae01b0dfa8729 100644 --- a/Framework/Core/src/DataProcessingHelpers.cxx +++ b/Framework/Core/src/DataProcessingHelpers.cxx @@ -118,6 +118,44 @@ void DataProcessingHelpers::broadcastOldestPossibleTimeslice(ServiceRegistryRef } } +namespace +{ +void reportDroppedTimeslices(ServiceRegistryRef const& ref, int64_t timeslice) +{ + O2_SIGNPOST_ID_GENERATE(sid, device); + if (ref.get().transitionHandling != TransitionHandlingState::NoTransition && DefaultsHelpers::onlineDeploymentMode()) { + O2_SIGNPOST_EVENT_EMIT_WARN(device, sid, "oldest_possible_timeslice", "Stop transition requested. Some Lifetime::Timeframe data got dropped starting at %" PRIi64, timeslice); + } else { + O2_SIGNPOST_EVENT_EMIT_CRITICAL(device, sid, "oldest_possible_timeslice", "Some Lifetime::Timeframe data got dropped starting at %" PRIi64, timeslice); + } +} +} // namespace + +void DataProcessingHelpers::skipTimeslices(ServiceRegistryRef const& ref, int64_t from, int64_t to) +{ + // Behind time pipelined sources (DeviceSpec::upstreamSourceLanes) the timeslices of a lane which ended are + // skipped too, without any loss: report a gap only once a later timeslice of the same lane is consumed. + auto& gaps = ref.get().pendingGaps; + auto lanes = (int64_t)gaps.size(); + if (lanes <= 1) { + reportDroppedTimeslices(ref, from); + return; + } + O2_SIGNPOST_ID_GENERATE(sid, device); + O2_SIGNPOST_EVENT_EMIT(device, sid, "oldest_possible_timeslice", "Skipping timeslices from %" PRIi64 " to %" PRIi64 ", dropped if their lane goes on", from, to); + for (auto t = from; t < std::min(to, from + lanes); t++) { + gaps[t % lanes] = gaps[t % lanes] < 0 ? t : gaps[t % lanes]; + } +} + +void DataProcessingHelpers::consumeTimeslice(ServiceRegistryRef const& ref, int64_t timeslice) +{ + auto& gaps = ref.get().pendingGaps; + if (gaps.size() > 1 && gaps[timeslice % gaps.size()] >= 0) { + reportDroppedTimeslices(ref, std::exchange(gaps[timeslice % gaps.size()], -1)); + } +} + void DataProcessingHelpers::switchState(ServiceRegistryRef const& ref, StreamingState newState) { auto& state = ref.get(); diff --git a/Framework/Core/src/DecongestionService.h b/Framework/Core/src/DecongestionService.h index fe7fce8d640db..f0a801d8eaa5d 100644 --- a/Framework/Core/src/DecongestionService.h +++ b/Framework/Core/src/DecongestionService.h @@ -12,6 +12,7 @@ #ifndef O2_FRAMEWORK_DECONGESTIONSERVICE_H_ #define O2_FRAMEWORK_DECONGESTIONSERVICE_H_ #include "Framework/AsyncQueue.h" +#include namespace o2::framework { @@ -31,6 +32,9 @@ struct DecongestionService { /// The next timeslice we should consume, when running in order, /// using an ordered completion policy. int64_t nextTimeslice = 0; + /// The oldest timeslice skipped by an ordered consumer and not reported yet, per source lane, -1 if none + /// (see DataProcessingHelpers::skipTimeslices). + std::vector pendingGaps; /// Ordered completion policy is active. bool orderedCompletionPolicyActive = false; /// consumeWhenPastOldestPossibleTimeframe completion policy is active. diff --git a/Framework/Core/src/DeviceSpecHelpers.cxx b/Framework/Core/src/DeviceSpecHelpers.cxx index 4c19e7a6ff17b..ae36718204672 100644 --- a/Framework/Core/src/DeviceSpecHelpers.cxx +++ b/Framework/Core/src/DeviceSpecHelpers.cxx @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -1231,8 +1232,19 @@ void DeviceSpecHelpers::dataProcessorSpecs2DeviceSpecs(const WorkflowSpec& workf WorkflowHelpers::validateEdges(workflow, policiesVector, logicalEdges, outputs); + std::vector upstreamSourceLanes(workflow.size(), 1); + for (bool changed = true; changed;) { + changed = false; + for (auto& edge : logicalEdges) { + auto& producer = workflow[edge.producer]; + bool enumeratedSource = std::ranges::all_of(producer.inputs, [](InputSpec const& input) { return input.lifetime == Lifetime::Enumeration; }); + auto lanes = std::lcm(upstreamSourceLanes[edge.consumer], std::lcm(upstreamSourceLanes[edge.producer], enumeratedSource ? producer.maxInputTimeslices : 1)); + changed |= std::exchange(upstreamSourceLanes[edge.consumer], lanes) != lanes; + } + } for (auto& device : devices) { device.resourceMonitoringInterval = resourcesMonitoringInterval; + device.upstreamSourceLanes = upstreamSourceLanes[std::ranges::find(workflow, device.name, &DataProcessorSpec::name) - workflow.begin()]; } auto findDeviceIndex = [&deviceIndex](size_t processorIndex, size_t timeslice) { diff --git a/Framework/Core/test/test_PipelinedSourceEndOfStream.cxx b/Framework/Core/test/test_PipelinedSourceEndOfStream.cxx index f8d1688c89228..daacac17c2d33 100644 --- a/Framework/Core/test/test_PipelinedSourceEndOfStream.cxx +++ b/Framework/Core/test/test_PipelinedSourceEndOfStream.cxx @@ -9,10 +9,16 @@ // granted to it by virtue of its status as an Intergovernmental Organization // or submit itself to any jurisdiction. #include "Framework/CompletionPolicyHelpers.h" +#include "Framework/ConfigParamSpec.h" void customize(std::vector& policies) { - policies.push_back(o2::framework::CompletionPolicyHelpers::consumeWhenAllOrdered("ordered-consumer")); + policies.push_back(o2::framework::CompletionPolicyHelpers::consumeWhenAllOrdered("ordered-.*")); +} + +void customize(std::vector& options) +{ + options.push_back(o2::framework::ConfigParamSpec{"missing", o2::framework::VariantType::String, "", {"timeframe missing: direct or relayed"}}); } #include "Framework/CallbackService.h" @@ -30,45 +36,55 @@ using namespace o2::framework; // polling of the other channel nor the oldest possible timeslice. // ordered-consumer sends what it gets on to after-ordered, which must get all of // it before the end of stream of ordered-consumer. +// No ordered consumer, whether it reads the lanes directly (ordered-direct) or +// behind another device (ordered-consumer, ordered-relayed), must report the +// timeslices of the ended lane as dropped. A timeframe really missing must be: +// with --missing direct, the live lane does not send it to ordered-direct; with +// --missing relayed, relay drops it on its way to ordered-relayed. constexpr int nTimeframes = 100; +constexpr int missingTimeframe = 10; -DataProcessorSpec consumer(std::string name, InputSpec input = {"data", "TST", "PIPELINED"}, std::vector outputs = {}) +DataProcessorSpec consumer(std::string name, InputSpec input, std::vector outputs = {}, int expected = nTimeframes, int drop = -1) { return DataProcessorSpec{ .name = name, .inputs = {input}, .outputs = outputs, - .algorithm = adaptStateful([relay = !outputs.empty()](CallbackService& callbacks) { + .algorithm = adaptStateful([relay = !outputs.empty(), expected, drop](CallbackService& callbacks) { static int received = 0; - callbacks.set([](EndOfStreamContext&) { - if (received != nTimeframes) { - LOGP(fatal, "Received {} timeframes, expected {}", received, nTimeframes); + callbacks.set([expected](EndOfStreamContext&) { + if (received != expected) { + LOGP(fatal, "Received {} timeframes, expected {}", received, expected); } }); - return adaptStateless([relay](InputRecord& inputs, DataAllocator& allocator) { + return adaptStateless([relay, drop](InputRecord& inputs, DataAllocator& allocator) { received++; - if (relay) { + if (relay && inputs.get("data") != drop) { allocator.make(OutputRef{"out"}) = inputs.get("data"); } }); })}; } -WorkflowSpec defineDataProcessing(ConfigContext const&) +WorkflowSpec defineDataProcessing(ConfigContext const& config) { + auto missing = config.options().get("missing"); return WorkflowSpec{ timePipeline(DataProcessorSpec{ .name = "producer", - .outputs = {OutputSpec{{"data"}, "TST", "PIPELINED"}}, - .algorithm = adaptStateless([](DataAllocator& outputs, ControlService& control, DeviceSpec const& spec) { + .outputs = {OutputSpec{{"data"}, "TST", "PIPELINED", 0}, OutputSpec{{"direct"}, "TST", "PIPELINED", 1}}, + .algorithm = adaptStateless([missing](DataAllocator& outputs, ControlService& control, DeviceSpec const& spec) { static int sent = 0; if (spec.inputTimesliceId == 1 || sent == nTimeframes) { control.endOfStream(); control.readyToQuit(QuitRequest::Me); return; } + if (missing != "direct" || sent != missingTimeframe) { + outputs.make(Output{"TST", "PIPELINED", 1}) = sent; + } outputs.make(Output{"TST", "PIPELINED", 0}) = sent++; })}, 2), - consumer("consumer"), consumer("ordered-consumer", {"data", "TST", "PIPELINED"}, {OutputSpec{{"out"}, "TST", "ORDERED"}}), consumer("after-ordered", {"data", "TST", "ORDERED"})}; + consumer("consumer", {"data", "TST", "PIPELINED", 0}), consumer("ordered-consumer", {"data", "TST", "PIPELINED", 0}, {OutputSpec{{"out"}, "TST", "ORDERED"}}), consumer("after-ordered", {"data", "TST", "ORDERED"}), consumer("ordered-direct", {"data", "TST", "PIPELINED", 1}, {}, nTimeframes - (missing == "direct")), consumer("relay", {"data", "TST", "PIPELINED", 0}, {OutputSpec{{"out"}, "TST", "RELAYED"}}, nTimeframes, missing == "relayed" ? missingTimeframe : -1), consumer("ordered-relayed", {"data", "TST", "RELAYED"}, {}, nTimeframes - (missing == "relayed"))}; }