-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDispatcher.cxx
More file actions
268 lines (232 loc) · 10.5 KB
/
Dispatcher.cxx
File metadata and controls
268 lines (232 loc) · 10.5 KB
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
// 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.
/// \file Dispatcher.cxx
/// \brief Implementation of Dispatcher for O2 Data Sampling
///
/// \author Piotr Konopka, piotr.jan.konopka@cern.ch
#include "DataSampling/Dispatcher.h"
#include "DataSampling/DataSamplingPolicy.h"
#include "DataSampling/DataSamplingHeader.h"
#include "Framework/DataProcessingHeader.h"
#include "Framework/DataSpecUtils.h"
#include "Framework/Logger.h"
#include "Framework/ConfigParamRegistry.h"
#include "Framework/Monitoring.h"
#include "Framework/DataRefUtils.h"
#include "Framework/FairMQDeviceProxy.h"
#include "Framework/DataProcessingHelpers.h"
#include "Framework/DataRelayer.h"
#include <Configuration/ConfigurationInterface.h>
#include <Configuration/ConfigurationFactory.h>
#include <stdexcept>
using namespace o2::configuration;
using namespace o2::monitoring;
using namespace o2::framework;
namespace o2::utilities
{
Dispatcher::Dispatcher(std::string name, const std::string reconfigurationSource)
: mName(std::move(name)), mReconfigurationSource(reconfigurationSource)
{
}
Dispatcher::~Dispatcher() = default;
void Dispatcher::init(InitContext& ctx)
{
LOG(debug) << "Reading Data Sampling Policies...";
boost::property_tree::ptree policiesTree;
if (mReconfigurationSource.empty() == false) {
std::unique_ptr<ConfigurationInterface> cfg = ConfigurationFactory::getConfiguration(mReconfigurationSource);
policiesTree = cfg->getRecursive("dataSamplingPolicies");
mPolicies.clear();
} else if (ctx.options().isSet("sampling-config-ptree")) {
policiesTree = ctx.options().get<boost::property_tree::ptree>("sampling-config-ptree");
mPolicies.clear();
} else {
; // we use policies declared during workflow init.
}
for (auto&& policyConfig : policiesTree) {
// we don't want the Dispatcher to exit due to one faulty Policy
try {
mPolicies.emplace_back(std::make_shared<DataSamplingPolicy>(DataSamplingPolicy::fromConfiguration(policyConfig.second)));
} catch (std::exception& ex) {
LOG(warn) << "Could not load the Data Sampling Policy '"
<< policyConfig.second.get_optional<std::string>("id").value_or("") << "', because: " << ex.what();
} catch (...) {
LOG(warn) << "Could not load the Data Sampling Policy '"
<< policyConfig.second.get_optional<std::string>("id").value_or("") << "'";
}
}
auto& spec = ctx.services().get<const DeviceSpec>();
mDeviceID.runtimeInit(spec.id.substr(0, DataSamplingHeader::deviceIDTypeSize).c_str());
}
header::Stack extractAdditionalHeaders(const char* inputHeaderStack)
{
std::array<header::BaseHeader const*, 8> headers;
int count = 0;
const auto* first = header::BaseHeader::get(reinterpret_cast<const std::byte*>(inputHeaderStack));
for (const auto* current = first; current != nullptr; current = current->next()) {
if (current->description != header::DataHeader::sHeaderType && current->description != DataProcessingHeader::sHeaderType) {
headers[count++] = current;
}
}
// Poor man runtime pack expansion.
switch (count) {
case 0:
return header::Stack{};
case 1:
return header::Stack{*headers[0]};
case 2:
return header::Stack{*headers[0], *headers[1]};
case 3:
return header::Stack{*headers[0], *headers[1], *headers[2]};
case 4:
return header::Stack{*headers[0], *headers[1], *headers[2], *headers[3]};
case 5:
return header::Stack{*headers[0], *headers[1], *headers[2], *headers[3], *headers[4]};
case 6:
return header::Stack{*headers[0], *headers[1], *headers[2], *headers[3], *headers[4], *headers[5]};
case 7:
return header::Stack{*headers[0], *headers[1], *headers[2], *headers[3], *headers[4], *headers[5], *headers[6]};
case 8:
return header::Stack{*headers[0], *headers[1], *headers[2], *headers[3], *headers[4], *headers[5], *headers[6], *headers[7]};
default:
throw std::runtime_error(fmt::format("Too many headers to copy {}", count));
}
}
void Dispatcher::run(ProcessingContext& ctx)
{
// todo: consider matching (and deciding) in completion policy to save some time
// it is not trivial though, we would have to share state with the customize() method,
// which is not possible atm.
for (auto inputIt = ctx.inputs().begin(); inputIt != ctx.inputs().end(); inputIt++) {
const DataRef& firstPart = inputIt.getByPos(0);
if (firstPart.header == nullptr) {
continue;
}
const auto* firstInputHeader = DataRefUtils::getHeader<header::DataHeader*>(firstPart);
ConcreteDataMatcher inputMatcher{firstInputHeader->dataOrigin, firstInputHeader->dataDescription, firstInputHeader->subSpecification};
for (auto& policy : mPolicies) {
// fixme: in principle matching could be broken by having query "TST/RAWDATA/0" and having parts with just
// the first subspec == 0, but others could be different. However, we trust that DPL does necessary checks
// during workflow validation and when passing messages (e.g. query "TST/RAWDATA/0" should not match
// a "TST/RAWDATA/*" output.
if (auto route = policy->match(inputMatcher); route != nullptr && policy->decide(firstPart)) {
auto routeAsConcreteDataType = DataSpecUtils::asConcreteDataTypeMatcher(*route);
auto dsheader = prepareDataSamplingHeader(*policy, *firstInputHeader);
for (const auto& part : inputIt) {
if (part.header != nullptr) {
// We copy every header which is not DataHeader or DataProcessingHeader,
// so that custom data-dependent headers are passed forward,
// and we add a DataSamplingHeader.
header::Stack headerStack{
extractAdditionalHeaders(part.header),
dsheader};
const auto* partInputHeader = DataRefUtils::getHeader<header::DataHeader*>(part);
Output output{
routeAsConcreteDataType.origin,
routeAsConcreteDataType.description,
partInputHeader->subSpecification,
std::move(headerStack)};
send(ctx.outputs(), part, output);
}
}
}
}
}
if (ctx.inputs().isValid("timer-stats")) {
reportStats(ctx.services().get<Monitoring>());
}
auto& relayer = ctx.services().get<DataRelayer>();
auto timeslice = relayer.getOldestPossibleOutput().timeslice.value;
DataProcessingHelpers::broadcastOldestPossibleTimeslice(ctx.services(), timeslice);
}
void Dispatcher::reportStats(Monitoring& monitoring) const
{
uint64_t dispatcherTotalEvaluatedMessages = 0;
uint64_t dispatcherTotalAcceptedMessages = 0;
for (const auto& policy : mPolicies) {
dispatcherTotalEvaluatedMessages += policy->getTotalEvaluatedMessages();
dispatcherTotalAcceptedMessages += policy->getTotalAcceptedMessages();
}
monitoring.send(Metric{dispatcherTotalEvaluatedMessages, "Dispatcher_messages_evaluated", Verbosity::Prod}.addTag(tags::Key::Subsystem, tags::Value::DataSampling));
monitoring.send(Metric{dispatcherTotalAcceptedMessages, "Dispatcher_messages_passed", Verbosity::Prod}.addTag(tags::Key::Subsystem, tags::Value::DataSampling));
}
DataSamplingHeader Dispatcher::prepareDataSamplingHeader(const DataSamplingPolicy& policy, header::DataHeader const& original)
{
uint64_t sampleTime = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
return {
sampleTime,
policy.getTotalAcceptedMessages(),
policy.getTotalEvaluatedMessages(),
mDeviceID,
original};
}
void Dispatcher::send(DataAllocator& dataAllocator, const DataRef& inputData, const Output& output) const
{
const auto* inputHeader = DataRefUtils::getHeader<header::DataHeader*>(inputData);
dataAllocator.snapshot(output, inputData.payload, DataRefUtils::getPayloadSize(inputData), inputHeader->payloadSerializationMethod);
}
void Dispatcher::registerPolicy(std::unique_ptr<DataSamplingPolicy>&& policy)
{
mPolicies.emplace_back(std::move(policy));
}
const std::string& Dispatcher::getName()
{
return mName;
}
Inputs Dispatcher::getInputSpecs()
{
Inputs declaredInputs;
// Add data inputs. Avoid duplicates and inputs which include others (e.g. "TST/DATA" includes "TST/DATA/1".
for (const auto& policy : mPolicies) {
for (const auto& path : policy->getPathMap()) {
auto& potentiallyNewInput = path.first;
// The idea is that we remove all existing inputs which are covered by the potentially new input.
// If there are none which are broader than the new one, then we add it.
auto newInputIsBroader = [&potentiallyNewInput](const InputSpec& other) {
return DataSpecUtils::includes(potentiallyNewInput, other);
};
declaredInputs.erase(std::remove_if(declaredInputs.begin(), declaredInputs.end(), newInputIsBroader), declaredInputs.end());
auto declaredInputIsBroader = [&potentiallyNewInput](const InputSpec& other) {
return DataSpecUtils::includes(other, potentiallyNewInput);
};
if (std::none_of(declaredInputs.begin(), declaredInputs.end(), declaredInputIsBroader)) {
declaredInputs.push_back(potentiallyNewInput);
}
}
}
// add timer input
header::DataDescription timerDescription;
timerDescription.runtimeInit(("TIMER-" + mName).substr(0, 16).c_str());
declaredInputs.emplace_back(InputSpec{"timer-stats", "DS", timerDescription, 0, Lifetime::Timer});
return declaredInputs;
}
Outputs Dispatcher::getOutputSpecs()
{
Outputs declaredOutputs;
for (const auto& policy : mPolicies) {
for (const auto& [_policyInput, policyOutput] : policy->getPathMap()) {
(void)_policyInput;
// In principle Data Sampling Policies should have different outputs.
// We may add a check to be very gentle with users.
declaredOutputs.push_back(policyOutput);
}
}
return declaredOutputs;
}
framework::Options Dispatcher::getOptions()
{
return {{"period-timer-stats", framework::VariantType::Int, 10 * 1000000, {"Dispatcher's stats timer period"}}};
}
size_t Dispatcher::numberOfPolicies()
{
return mPolicies.size();
}
} // namespace o2::utilities