-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathCommonMessageBackends.cxx
More file actions
124 lines (110 loc) · 5.13 KB
/
CommonMessageBackends.cxx
File metadata and controls
124 lines (110 loc) · 5.13 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
// 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/CommonMessageBackends.h"
#include "Framework/MessageContext.h"
#include "Framework/ArrowContext.h"
#include "Framework/StringContext.h"
#include "Framework/DataProcessor.h"
#include "Framework/ServiceRegistry.h"
#include "Framework/RawDeviceService.h"
#include "Framework/DeviceSpec.h"
#include "Framework/EndOfStreamContext.h"
#include "Framework/Tracing.h"
#include "Framework/DeviceMetricsInfo.h"
#include "Framework/DeviceInfo.h"
#include "CommonMessageBackendsHelpers.h"
#include <Monitoring/Monitoring.h>
#include <Headers/DataHeader.h>
#include <fairmq/ProgOptions.h>
#include <fairmq/Device.h>
#include <uv.h>
#include <boost/program_options/variables_map.hpp>
#include <csignal>
namespace o2::framework
{
class EndOfStreamContext;
class ProcessingContext;
o2::framework::ServiceSpec CommonMessageBackends::fairMQDeviceProxy()
{
return ServiceSpec{
.name = "fairmq-device-proxy",
.init = [](ServiceRegistryRef, DeviceState&, fair::mq::ProgOptions& options) -> ServiceHandle {
auto* proxy = new FairMQDeviceProxy();
return ServiceHandle{.hash = TypeIdHelpers::uniqueId<FairMQDeviceProxy>(), .instance = proxy, .kind = ServiceKind::Serial};
},
.start = [](ServiceRegistryRef services, void* instance) {
auto* proxy = static_cast<FairMQDeviceProxy*>(instance);
auto& outputs = services.get<DeviceSpec const>().outputs;
auto& inputs = services.get<DeviceSpec const>().inputs;
auto& forwards = services.get<DeviceSpec const>().forwards;
auto* device = services.get<RawDeviceService>().device();
/// Notice that we do it here (and not in the init) because
/// some of the channels are added only later on to the party,
/// (e.g. by ECS) and Init might not be late enough to
/// account for them.
std::function<fair::mq::Channel&(std::string const&)> bindByName = [device](std::string const& channelName) -> fair::mq::Channel& {
auto channel = device->GetChannels().find(channelName);
if (channel == device->GetChannels().end()) {
LOGP(fatal, "Expected channel {} not configured.", channelName);
}
return channel->second.at(0);
};
std::function<bool()> newStateCallback = [device]() -> bool {
return device->NewStatePending();
};
proxy->bind(outputs, inputs, forwards, bindByName, newStateCallback); },
};
}
o2::framework::ServiceSpec CommonMessageBackends::fairMQBackendSpec()
{
return ServiceSpec{
.name = "fairmq-backend",
.uniqueId = CommonServices::simpleServiceId<MessageContext>(),
.init = [](ServiceRegistryRef services, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
auto& proxy = services.get<FairMQDeviceProxy>();
auto context = new MessageContext(proxy);
auto& spec = services.get<DeviceSpec const>();
auto& dataSender = services.get<DataSender>();
auto dispatcher = [&dataSender](fair::mq::Parts&& parts, ChannelIndex channelIndex, unsigned int) {
dataSender.send(parts, channelIndex);
};
auto matcher = [policy = spec.dispatchPolicy](o2::header::DataHeader const& header) {
if (policy.triggerMatcher == nullptr) {
return true;
}
return policy.triggerMatcher(Output{header});
};
if (spec.dispatchPolicy.action == DispatchPolicy::DispatchOp::WhenReady) {
context->init(DispatchControl{dispatcher, matcher});
}
return ServiceHandle{.hash = TypeIdHelpers::uniqueId<MessageContext>(), .instance = context, .kind = ServiceKind::Stream};
},
.configure = CommonServices::noConfiguration(),
.preProcessing = CommonMessageBackendsHelpers<MessageContext>::clearContext(),
.postProcessing = CommonMessageBackendsHelpers<MessageContext>::sendCallback(),
.preEOS = CommonMessageBackendsHelpers<MessageContext>::clearContextEOS(),
.postEOS = CommonMessageBackendsHelpers<MessageContext>::sendCallbackEOS(),
.kind = ServiceKind::Stream};
}
o2::framework::ServiceSpec CommonMessageBackends::stringBackendSpec()
{
return ServiceSpec{
.name = "string-backend",
.uniqueId = CommonServices::simpleServiceId<StringContext>(),
.init = CommonMessageBackendsHelpers<StringContext>::createCallback(),
.configure = CommonServices::noConfiguration(),
.preProcessing = CommonMessageBackendsHelpers<StringContext>::clearContext(),
.postProcessing = CommonMessageBackendsHelpers<StringContext>::sendCallback(),
.preEOS = CommonMessageBackendsHelpers<StringContext>::clearContextEOS(),
.postEOS = CommonMessageBackendsHelpers<StringContext>::sendCallbackEOS(),
.kind = ServiceKind::Stream};
}
} // namespace o2::framework