Skip to content

Commit 7b9ae6d

Browse files
committed
QC-442 Generalized QC device
1 parent de3e7d1 commit 7b9ae6d

15 files changed

Lines changed: 914 additions & 4 deletions

Framework/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ src/QCInputsAdapters.cxx
141141
src/QCInputsFactory.cxx
142142
src/LateTaskRunner.cxx
143143
src/LateTaskRunnerFactory.cxx
144-
src/LateTaskInterface.cxx)
144+
src/LateTaskInterface.cxx
145+
src/Actor.cxx
146+
src/ActorHelpers.cxx)
145147

146148
target_include_directories(
147149
O2QualityControl
@@ -273,6 +275,7 @@ endforeach()
273275
add_executable(o2-qc-test-core
274276
test/testActivity.cxx
275277
test/testActivityHelpers.cxx
278+
test/testActorTraits.cxx
276279
test/testAggregatorInterface.cxx
277280
test/testAggregatorRunner.cxx
278281
test/testCheck.cxx

Framework/include/QualityControl/Actor.h

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,275 @@
55
#ifndef ACTOR_H
66
#define ACTOR_H
77

8+
#include <concepts>
9+
#include <string_view>
10+
#include <memory>
11+
#include <type_traits>
12+
#include <format>
13+
14+
#include "QualityControl/ActorTraits.h"
15+
#include "QualityControl/runnerUtils.h"
16+
#include "QualityControl/ServicesConfig.h"
17+
18+
#include <Framework/CallbackService.h>
19+
#include <Framework/InitContext.h>
20+
#include <Framework/ProcessingContext.h>
21+
#include <Framework/EndOfStreamContext.h>
22+
#include <Framework/ConcreteDataMatcher.h>
23+
24+
25+
namespace o2::monitoring {
26+
class Monitoring;
27+
}
28+
29+
namespace o2::bkp {
30+
enum class DplProcessType;
31+
}
32+
33+
namespace o2::quality_control::core {
34+
35+
36+
class Bookkeeping;
37+
38+
// anything we want to hide in the source file to avoid exposing headers
39+
namespace impl
40+
{
41+
// fixme: to my understanding, we cannot declare these are Actor members and have them compiled within .cxx, because it's a template
42+
std::shared_ptr<monitoring::Monitoring> initMonitoring(std::string_view url, std::string_view detector = "");
43+
void startMonitoring(monitoring::Monitoring&, int runNumber);
44+
45+
void initBookkeeping(std::string_view url);
46+
void startBookkeeping(int runNumber, std::string_view actorName, std::string_view detectorName, const o2::bkp::DplProcessType& processType, std::string_view args);
47+
Bookkeeping& getBookkeeping();
48+
49+
}
50+
51+
52+
// todo catch and handle exceptions
53+
// todo check if onProcess() and onInit() are implemented by ConcreteActor
54+
// todo consider having a check to avoid overriding Actor methods
55+
// Actor is supposed to contain any common logic for QC actors (starting services, handling exceptions) and bridging with DPL (specs, registering callbacks).
56+
template<typename ConcreteActor>
57+
requires ValidActorTraits<ActorTraits<ConcreteActor>>
58+
class Actor
59+
{
60+
private:
61+
using traits = ActorTraits<ConcreteActor>;
62+
63+
// helpers to avoid repeated static_casts to call ConcreteActor methods
64+
ConcreteActor& concreteActor() { return static_cast<ConcreteActor&>(*this); }
65+
const ConcreteActor& concreteActor() const { return static_cast<const ConcreteActor&>(*this); }
66+
67+
// a trick to prevent bugs like "class Derived2 : public Base<Derived1>"
68+
// see https://www.fluentcpp.com/2017/05/12/curiously-recurring-template-pattern/
69+
Actor(){};
70+
friend ConcreteActor;
71+
72+
// helper to check if ConcreteActor needs a given service
73+
template<Service S>
74+
consteval bool requiresService() {
75+
for (const auto& required : traits::sRequiredServices) {
76+
if (required == S) {
77+
return true;
78+
}
79+
}
80+
return false;
81+
// todo: when we can use C++23
82+
// return std::ranges::contains(ActorTraitsT::sRequiredServices, S);
83+
}
84+
85+
public:
86+
87+
88+
explicit Actor(const ServicesConfig& servicesConfig) :
89+
mServicesConfig{servicesConfig},
90+
mActivity(servicesConfig.activity)
91+
{
92+
// todo extract to a separate method
93+
// todo also do it for onProcess and onInit
94+
// compile-time checks which can be performed only once ConcreteActor is a complete type, i.e. inside a function body
95+
96+
// mandatory methods
97+
static_assert( requires(ConcreteActor& actor, framework::ProcessingContext& pCtx) { { actor.onProcess(pCtx) } -> std::convertible_to<void>; });
98+
static_assert( requires(ConcreteActor& actor, framework::InitContext& iCtx) { { actor.onInit(iCtx) } -> std::convertible_to<void>; });
99+
100+
// mandatory if specific features are enabled
101+
if constexpr (traits::sDetectorSpecific) {
102+
static_assert( requires(const ConcreteActor& actor) { { actor.getDetectorName() } -> std::convertible_to<std::string_view>; });
103+
}
104+
105+
if constexpr(traits::sCriticality == Criticality::UserDefined) {
106+
static_assert( requires(const ConcreteActor& actor) { { actor.isCritical() } -> std::convertible_to<bool>; });
107+
}
108+
109+
if constexpr (runsUserCode<traits>()) {
110+
static_assert(requires(const ConcreteActor& actor) { { actor.getUserCodeName() } -> std::convertible_to<std::string_view>; });
111+
}
112+
}
113+
114+
bool isCritical() const { return true; } // todo
115+
116+
void init(framework::InitContext& ictx)
117+
{
118+
// we set the fallback activity. fields might get overwritten once runtime values become available
119+
mActivity = mServicesConfig.activity;
120+
121+
initServices(ictx);
122+
initDplCallbacks(ictx);
123+
124+
concreteActor().onInit(ictx);
125+
}
126+
127+
void initServices(framework::InitContext& ictx)
128+
{
129+
// fixme: this still forces non-detector-specific actors to have the function
130+
std::string detectorName;
131+
if constexpr (traits::sDetectorSpecific) {
132+
detectorName = std::string{concreteActor().getDetectorName()};
133+
}
134+
135+
// init services
136+
if constexpr (requiresService<Service::InfoLogger>()) {
137+
std::string facility;
138+
if constexpr (runsUserCode<traits>()) {
139+
facility = std::format("{}/{}", traits::sActorTypeShort, concreteActor().getUserCodeName());
140+
} else {
141+
facility = std::format("{}/", traits::sActorTypeShort);
142+
}
143+
144+
// todo now we use the version from runnerUtils, but the implementation could be moved to Actor.cxx once we migrate all actors
145+
initInfologger(ictx, mServicesConfig.infologgerDiscardParameters, facility, detectorName);
146+
}
147+
if constexpr (requiresService<Service::Monitoring>()) {
148+
mMonitoring = impl::initMonitoring(mServicesConfig.monitoringUrl, detectorName);
149+
}
150+
if constexpr (requiresService<Service::Bookkeeping>()) {
151+
impl::initBookkeeping(mServicesConfig.bookkeepingUrl);
152+
}
153+
// todo: init the rest, such as QCDB
154+
}
155+
156+
void initDplCallbacks(framework::InitContext& ictx)
157+
{
158+
try {
159+
auto& callbacks = ictx.services().get<framework::CallbackService>();
160+
161+
// we steal services reference, because it is not available as an argument of these callbacks
162+
framework::ServiceRegistryRef services = ictx.services();
163+
164+
callbacks.set<framework::CallbackService::Id::Start>([this, services]() { this->start(services); });
165+
callbacks.set<framework::CallbackService::Id::Stop>([this, services]() { this->stop(services); });
166+
callbacks.set<framework::CallbackService::Id::Reset>([this, services]() { this->reset(services); });
167+
168+
// we register optional callbacks like that:
169+
if constexpr (requires { &ConcreteActor::endOfStream; }) {
170+
callbacks.set<framework::CallbackService::Id::EndOfStream>([this](framework::EndOfStreamContext& eosContext) {
171+
this->endOfStream(eosContext);
172+
});
173+
}
174+
if constexpr (requires { &ConcreteActor::finaliseCCDB; }) {
175+
callbacks.set<framework::CallbackService::Id::CCDBDeserialised>([this](framework::ConcreteDataMatcher& matcher, void* obj) {
176+
this->finaliseCCDB(matcher, obj);
177+
});
178+
}
179+
} catch (framework::RuntimeErrorRef& ref) {
180+
ILOG(Fatal) << "Error during callback registration: " << framework::error_from_ref(ref).what << ENDM;
181+
throw;
182+
}
183+
}
184+
185+
186+
void process(framework::ProcessingContext& ctx)
187+
{
188+
concreteActor().onProcess(ctx);
189+
}
190+
191+
void start(framework::ServiceRegistryRef services) {
192+
ILOG(Debug, Trace) << traits::sActorTypeKebabCase << " start" << ENDM;
193+
// todo catch and handle exceptions
194+
195+
mActivity = computeActivity(services, mActivity);
196+
197+
if constexpr (requiresService<Service::InfoLogger>()) {
198+
QcInfoLogger::setRun(mActivity.mId);
199+
QcInfoLogger::setPartition(mActivity.mPartitionName);
200+
}
201+
if constexpr (requiresService<Service::Monitoring>()) {
202+
impl::startMonitoring(*mMonitoring, mActivity.mId);
203+
}
204+
if constexpr (requiresService<Service::Bookkeeping>()) {
205+
// todo not sure if these are constexpr, check
206+
auto actorName = runsUserCode<traits>() ? concreteActor().getUserCodeName() : traits::sActorTypeKebabCase;
207+
auto detectorName = traits::sDetectorSpecific ? concreteActor().getDetectorName() : "";
208+
209+
// todo: get args
210+
impl::startBookkeeping(mActivity.mId, actorName, detectorName, traits::sDplProcessType, "");
211+
}
212+
213+
// fixme: should we just have empty methods in the base class for the sake of readability?
214+
if constexpr (requires {ConcreteActor::onStart;}) {
215+
concreteActor().onStart(services, mActivity);
216+
}
217+
}
218+
219+
void stop(framework::ServiceRegistryRef services) {
220+
ILOG(Debug, Trace) << traits::sActorTypeKebabCase << " stop" << ENDM;
221+
222+
mActivity = computeActivity(services, mActivity);
223+
224+
if constexpr (requires {ConcreteActor::onStop;}) {
225+
concreteActor().onStop(services, mActivity);
226+
}
227+
}
228+
229+
void reset(framework::ServiceRegistryRef services) {
230+
ILOG(Debug, Trace) << traits::sActorTypeKebabCase << " reset" << ENDM;
231+
232+
mActivity = mServicesConfig.activity;
233+
234+
if constexpr (requires {ConcreteActor::onReset;}) {
235+
concreteActor().onReset(services, mActivity);
236+
}
237+
}
238+
239+
void endOfStream(framework::EndOfStreamContext& eosContext) {
240+
ILOG(Debug, Trace) << traits::sActorTypeKebabCase << " endOfStream" << ENDM;
241+
// todo catch and handle exceptions
242+
if constexpr (requires {ConcreteActor::onEndOfStream;}) {
243+
concreteActor().onEndOfStream(eosContext);
244+
}
245+
}
246+
void finaliseCCDB(framework::ConcreteDataMatcher& matcher, void* obj)
247+
{
248+
ILOG(Debug, Trace) << traits::sActorTypeKebabCase << " finaliseCCDB" << ENDM;
249+
// todo catch and handle exceptions
250+
if constexpr (requires {ConcreteActor::onFinaliseCCDB;}) {
251+
concreteActor().onFinaliseCCDB(matcher, obj);
252+
}
253+
}
254+
255+
protected:
256+
257+
monitoring::Monitoring& getMonitoring() requires (requiresService<Service::Monitoring>())
258+
{
259+
return *mMonitoring;
260+
}
261+
262+
Bookkeeping& getBookkeeping() requires (requiresService<Service::Bookkeeping>())
263+
{
264+
return impl::getBookkeeping();
265+
}
266+
267+
const Activity& getActivity() const { return mActivity; }
268+
269+
private:
270+
Activity mActivity;
271+
const ServicesConfig mServicesConfig;
272+
273+
std::shared_ptr<monitoring::Monitoring> mMonitoring;
274+
275+
};
276+
277+
278+
}
8279
#endif //ACTOR_H

0 commit comments

Comments
 (0)