/
TaskRunner.cxx
466 lines (390 loc) · 16.2 KB
/
TaskRunner.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// 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.
///
/// \file TaskRunner.cxx
/// \author Barthelemy von Haller
/// \author Piotr Konopka
///
#include "QualityControl/TaskRunner.h"
#include <memory>
// O2
#include <Common/Exceptions.h>
#include <Configuration/ConfigurationFactory.h>
#include <Monitoring/MonitoringFactory.h>
#if __has_include(<Framework/DataSampling.h>)
#include <Framework/DataSampling.h>
#else
#include <DataSampling/DataSampling.h>
using namespace o2::utilities;
#endif
#include <Framework/CallbackService.h>
#include <Framework/CompletionPolicyHelpers.h>
#include <Framework/TimesliceIndex.h>
#include <Framework/DataSpecUtils.h>
#include <Framework/DataDescriptorQueryBuilder.h>
#include <Framework/ConfigParamRegistry.h>
#include "QualityControl/QcInfoLogger.h"
#include "QualityControl/TaskFactory.h"
#include <string>
#include <memory>
#include <boost/property_tree/ini_parser.hpp>
using namespace std;
const auto current_diagnostic = boost::current_exception_diagnostic_information;
namespace o2::quality_control::core
{
using namespace o2::framework;
using namespace o2::header;
using namespace o2::configuration;
using namespace o2::monitoring;
using namespace std::chrono;
using namespace AliceO2::Common;
TaskRunner::TaskRunner(const std::string& taskName, const std::string& configurationSource, size_t id)
: mDeviceName(createTaskRunnerIdString() + "-" + taskName),
mRunNumber(0),
mMonitorObjectsSpec({ "mo" }, createTaskDataOrigin(), createTaskDataDescription(taskName), id)
{
try {
mTaskConfig.taskName = taskName;
mTaskConfig.parallelTaskID = id;
mConfigFile = ConfigurationFactory::getConfiguration(configurationSource);
loadTopologyConfig();
} catch (...) {
// catch the configuration exception and print it to avoid losing it
ILOG(Fatal) << "Unexpected exception during configuration:\n"
<< current_diagnostic(true) << ENDM;
throw;
}
}
void TaskRunner::init(InitContext& iCtx)
{
ILOG(Info) << "Initializing TaskRunner" << ENDM;
ILOG(Info) << "Loading configuration" << ENDM;
try {
loadTaskConfig();
} catch (...) {
// catch the configuration exception and print it to avoid losing it
ILOG(Fatal) << "Unexpected exception during configuration:\n"
<< current_diagnostic(true) << ENDM;
throw;
}
// registering state machine callbacks
iCtx.services().get<CallbackService>().set(CallbackService::Id::Start, [this, &options = iCtx.options()]() { start(options); });
iCtx.services().get<CallbackService>().set(CallbackService::Id::Stop, [this]() { stop(); });
iCtx.services().get<CallbackService>().set(CallbackService::Id::Reset, [this]() { reset(); });
// setup monitoring
std::string monitoringUrl = mConfigFile->get<std::string>("qc.config.monitoring.url", "infologger:///debug?qc");
mCollector = MonitoringFactory::Get(monitoringUrl);
mCollector->enableProcessMonitoring();
mCollector->addGlobalTag(tags::Key::Subsystem, tags::Value::QC);
mCollector->addGlobalTag("TaskName", mTaskConfig.taskName);
// setup publisher
mObjectsManager = std::make_shared<ObjectsManager>(mTaskConfig);
// setup user's task
TaskFactory f;
mTask.reset(f.create(mTaskConfig, mObjectsManager));
// init user's task
mTask->loadCcdb(mTaskConfig.conditionUrl);
mTask->initialize(iCtx);
mNoMoreCycles = false;
mCycleNumber = 0;
}
void TaskRunner::run(ProcessingContext& pCtx)
{
if (mNoMoreCycles) {
ILOG(Info) << "The maximum number of cycles (" << mTaskConfig.maxNumberCycles << ") has been reached"
<< " or the device has received an EndOfStream signal. Won't start a new cycle." << ENDM;
return;
}
if (!mCycleOn) {
startCycle();
}
auto [dataReady, timerReady] = validateInputs(pCtx.inputs());
if (dataReady) {
mTask->monitorData(pCtx);
mNumberMessages++;
}
if (timerReady) {
finishCycle(pCtx.outputs());
if (mResetAfterPublish) {
mTask->reset();
}
if (mTaskConfig.maxNumberCycles < 0 || mCycleNumber < mTaskConfig.maxNumberCycles) {
startCycle();
} else {
mNoMoreCycles = true;
}
}
}
CompletionPolicy::CompletionOp TaskRunner::completionPolicyCallback(o2::framework::CompletionPolicy::InputSet inputs)
{
// fixme: we assume that there is one timer input and the rest are data inputs. If some other implicit inputs are
// added, this will break.
size_t dataInputsExpected = inputs.size() - 1;
size_t dataInputsPresent = 0;
CompletionPolicy::CompletionOp action = CompletionPolicy::CompletionOp::Wait;
for (auto& input : inputs) {
if (input.header == nullptr || input.payload == nullptr) {
continue;
}
const auto* dataHeader = CompletionPolicyHelpers::getHeader<DataHeader>(input);
assert(dataHeader);
if (!strncmp(dataHeader->dataDescription.str, "TIMER", 5)) {
action = CompletionPolicy::CompletionOp::Consume;
} else {
dataInputsPresent++;
}
}
LOG(DEBUG) << "Completion policy callback. "
<< "Total inputs possible: " << inputs.size()
<< ", data inputs: " << dataInputsPresent
<< ", timer inputs: " << (action == CompletionPolicy::CompletionOp::Consume);
if (dataInputsPresent == dataInputsExpected) {
action = CompletionPolicy::CompletionOp::Consume;
}
LOG(DEBUG) << "Action: " << action;
return action;
}
void TaskRunner::setResetAfterPublish(bool resetAfterPublish) { mResetAfterPublish = resetAfterPublish; }
std::string TaskRunner::createTaskRunnerIdString()
{
return std::string("QC-TASK-RUNNER");
}
header::DataOrigin TaskRunner::createTaskDataOrigin()
{
return header::DataOrigin{ "QC" };
}
header::DataDescription TaskRunner::createTaskDataDescription(const std::string& taskName)
{
if (taskName.empty()) {
BOOST_THROW_EXCEPTION(FatalException() << errinfo_details("Empty taskName for task's data description"));
}
o2::header::DataDescription description;
description.runtimeInit(std::string(taskName.substr(0, header::DataDescription::size - 3) + "-mo").c_str());
return description;
}
void TaskRunner::endOfStream(framework::EndOfStreamContext& eosContext)
{
ILOG(Info) << "Received an EndOfStream, finishing the current cycle" << ENDM;
finishCycle(eosContext.outputs());
mNoMoreCycles = true;
}
void TaskRunner::start(const ConfigParamRegistry& options)
{
try {
mRunNumber = options.get<int>("runNumber");
ILOG(Info) << "Run number found in options: " << mRunNumber << ENDM;
} catch (std::invalid_argument& ia) {
ILOG(Info) << "Run number not found in options, using 0 instead." << ENDM;
mRunNumber = 0;
}
startOfActivity();
if (mNoMoreCycles) {
ILOG(Info) << "The maximum number of cycles (" << mTaskConfig.maxNumberCycles << ") has been reached"
<< " or the device has received an EndOfStream signal. Won't start a new cycle." << ENDM;
return;
}
startCycle();
}
void TaskRunner::stop()
{
if (mCycleOn) {
mTask->endOfCycle();
mCycleNumber++;
mCycleOn = false;
}
endOfActivity();
mTask->reset();
mRunNumber = 0;
}
void TaskRunner::reset()
{
mTask.reset();
mCollector.reset();
mObjectsManager.reset();
mRunNumber = 0;
}
std::tuple<bool /*data ready*/, bool /*timer ready*/> TaskRunner::validateInputs(const framework::InputRecord& inputs)
{
size_t dataInputsPresent = 0;
bool timerReady = false;
for (auto& input : inputs) {
if (input.header != nullptr && input.payload != nullptr) {
const auto* dataHeader = get<DataHeader*>(input.header);
assert(dataHeader);
if (!strncmp(dataHeader->dataDescription.str, "TIMER", 5)) {
timerReady = true;
} else {
dataInputsPresent++;
}
}
}
bool dataReady = dataInputsPresent == inputs.size() - 1;
return { dataReady, timerReady };
}
void TaskRunner::loadTopologyConfig()
{
auto taskConfigTree = getTaskConfigTree();
auto policiesFilePath = mConfigFile->get<std::string>("dataSamplingPolicyFile", "");
ConfigurationInterface* config = policiesFilePath.empty() ? mConfigFile.get() : ConfigurationFactory::getConfiguration(policiesFilePath).get();
auto policiesTree = config->getRecursive("dataSamplingPolicies");
auto dataSourceTree = taskConfigTree.get_child("dataSource");
auto type = dataSourceTree.get<std::string>("type");
if (type == "dataSamplingPolicy") {
auto policyName = dataSourceTree.get<std::string>("name");
ILOG(Info) << "policyName : " << policyName << ENDM;
mInputSpecs = DataSampling::InputSpecsForPolicy(config, policyName);
} else if (type == "direct") {
auto inputsQuery = dataSourceTree.get<std::string>("query");
mInputSpecs = DataDescriptorQueryBuilder::parse(inputsQuery.c_str());
} else {
std::string message = std::string("Configuration error : dataSource type unknown : ") + type;
BOOST_THROW_EXCEPTION(AliceO2::Common::FatalException() << AliceO2::Common::errinfo_details(message));
}
mInputSpecs.emplace_back(InputSpec{ "timer-cycle", createTaskDataOrigin(), createTaskDataDescription("TIMER-" + mTaskConfig.taskName), 0, Lifetime::Timer });
// needed to avoid having looping at the maximum speed
mTaskConfig.cycleDurationSeconds = taskConfigTree.get<int>("cycleDurationSeconds", 10);
mOptions.push_back({ "period-timer-cycle", framework::VariantType::Int, static_cast<int>(mTaskConfig.cycleDurationSeconds * 1000000), { "timer period" } });
}
boost::property_tree::ptree TaskRunner::getTaskConfigTree() const
{
auto tasksConfigList = mConfigFile->getRecursive("qc.tasks");
auto taskConfigTree = tasksConfigList.find(mTaskConfig.taskName);
if (taskConfigTree == tasksConfigList.not_found()) {
std::string message = "No configuration found for task \"" + mTaskConfig.taskName + "\"";
BOOST_THROW_EXCEPTION(AliceO2::Common::FatalException() << AliceO2::Common::errinfo_details(message));
}
return taskConfigTree->second;
}
void TaskRunner::loadTaskConfig()
{
auto taskConfigTree = getTaskConfigTree();
string test = taskConfigTree.get<std::string>("detectorName", "MISC");
mTaskConfig.detectorName = validateDetectorName(taskConfigTree.get<std::string>("detectorName", "MISC"));
mTaskConfig.moduleName = taskConfigTree.get<std::string>("moduleName");
mTaskConfig.className = taskConfigTree.get<std::string>("className");
mTaskConfig.maxNumberCycles = taskConfigTree.get<int>("maxNumberCycles", -1);
mTaskConfig.consulUrl = mConfigFile->get<std::string>("qc.config.consul.url", "http://consul-test.cern.ch:8500");
mTaskConfig.conditionUrl = mConfigFile->get<std::string>("qc.config.conditionDB.url", "http://ccdb-test.cern.ch:8080");
try {
mTaskConfig.customParameters = mConfigFile->getRecursiveMap("qc.tasks." + mTaskConfig.taskName + ".taskParameters");
} catch (...) {
ILOG(Info) << "No custom parameters for " << mTaskConfig.taskName << ENDM;
}
ILOG(Info) << "Configuration loaded : " << ENDM;
ILOG(Info) << ">> Task name : " << mTaskConfig.taskName << ENDM;
ILOG(Info) << ">> Module name : " << mTaskConfig.moduleName << ENDM;
ILOG(Info) << ">> Detector name : " << mTaskConfig.detectorName << ENDM;
ILOG(Info) << ">> Cycle duration seconds : " << mTaskConfig.cycleDurationSeconds << ENDM;
ILOG(Info) << ">> Max number cycles : " << mTaskConfig.maxNumberCycles << ENDM;
}
std::string TaskRunner::validateDetectorName(std::string name) const
{
// name must be a detector code from DetID or one of the few allowed general names
int nDetectors = 16;
const char* detNames[16] = // once we can use DetID, remove this hard-coded list
{ "ITS", "TPC", "TRD", "TOF", "PHS", "CPV", "EMC", "HMP", "MFT", "MCH", "MID", "ZDC", "FT0", "FV0", "FDD", "ACO" };
vector<string> permitted = { "MISC", "DAQ", "GENERAL", "TST", "BMK", "CTP", "TRG", "DCS" };
for (auto i = 0; i < nDetectors; i++) {
permitted.push_back(detNames[i]);
// permitted.push_back(o2::detectors::DetID::getName(i));
}
auto it = std::find(permitted.begin(), permitted.end(), name);
if (it == permitted.end()) {
std::string permittedString;
for (auto i : permitted)
permittedString += i + ' ';
ILOG(Error) << "Invalid detector name : " << name << "\n"
<< " Placeholder 'MISC' will be used instead\n"
<< " Note: list of permitted detector names :" << permittedString << ENDM;
return "MISC";
}
return name;
}
void TaskRunner::startOfActivity()
{
// stats
mTimerTotalDurationActivity.reset();
mTotalNumberObjectsPublished = 0;
// We take the run number as set from the FairMQ options if it is there, otherwise the one from the config file
int run = mRunNumber > 0 ? mRunNumber : mConfigFile->get<int>("qc.config.Activity.number");
Activity activity(run,
mConfigFile->get<int>("qc.config.Activity.type"));
mTask->startOfActivity(activity);
mObjectsManager->updateServiceDiscovery();
}
void TaskRunner::endOfActivity()
{
Activity activity(mRunNumber,
mConfigFile->get<int>("qc.config.Activity.type"));
mTask->endOfActivity(activity);
mObjectsManager->removeAllFromServiceDiscovery();
double rate = mTotalNumberObjectsPublished / mTimerTotalDurationActivity.getTime();
mCollector->send(Metric{ "qc_objects_published" }.addValue(rate, "per_second_whole_run"));
}
void TaskRunner::startCycle()
{
QcInfoLogger::GetInstance() << "cycle " << mCycleNumber << " in " << mTaskConfig.taskName << ENDM;
mTask->startOfCycle();
mNumberMessages = 0;
mNumberObjectsPublishedInCycle = 0;
mTimerDurationCycle.reset();
mCycleOn = true;
}
void TaskRunner::finishCycle(DataAllocator& outputs)
{
mTask->endOfCycle();
mNumberObjectsPublishedInCycle += publish(outputs);
mTotalNumberObjectsPublished += mNumberObjectsPublishedInCycle;
publishCycleStats();
mObjectsManager->updateServiceDiscovery();
mCycleNumber++;
mCycleOn = false;
if (mTaskConfig.maxNumberCycles == mCycleNumber) {
ILOG(Info) << "The maximum number of cycles (" << mTaskConfig.maxNumberCycles << ") has been reached."
<< " The task will not do anything from now on." << ENDM;
}
}
void TaskRunner::publishCycleStats()
{
double cycleDuration = mTimerDurationCycle.getTime();
double rate = mNumberObjectsPublishedInCycle / (cycleDuration + mLastPublicationDuration);
double wholeRunRate = mTotalNumberObjectsPublished / mTimerTotalDurationActivity.getTime();
double totalDurationActivity = mTimerTotalDurationActivity.getTime();
mCollector->send({ mNumberMessages, "qc_messages_received_in_cycle" });
// monitoring metrics
mCollector->send(Metric{ "qc_duration" }
.addValue(cycleDuration, "module_cycle")
.addValue(mLastPublicationDuration, "publication")
.addValue(totalDurationActivity, "activity_whole_run"));
mCollector->send(Metric{ "qc_objects_published" }
.addValue(mNumberObjectsPublishedInCycle, "in_cycle")
.addValue(rate, "per_second")
.addValue(mTotalNumberObjectsPublished, "whole_run")
.addValue(wholeRunRate, "per_second_whole_run"));
}
int TaskRunner::publish(DataAllocator& outputs)
{
ILOG(Info) << "Send data from " << mTaskConfig.taskName << " len: " << mObjectsManager->getNumberPublishedObjects() << ENDM;
AliceO2::Common::Timer publicationDurationTimer;
auto concreteOutput = framework::DataSpecUtils::asConcreteDataMatcher(mMonitorObjectsSpec);
// getNonOwningArray creates a TObjArray containing the monitoring objects, but not
// owning them. The array is created by new and must be cleaned up by the caller
std::unique_ptr<MonitorObjectCollection> array(mObjectsManager->getNonOwningArray());
int objectsPublished = array->GetEntries();
outputs.snapshot(
Output{ concreteOutput.origin,
concreteOutput.description,
concreteOutput.subSpec,
mMonitorObjectsSpec.lifetime },
*array);
mLastPublicationDuration = publicationDurationTimer.getTime();
return objectsPublished;
}
} // namespace o2::quality_control::core