-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDataRelayer.h
More file actions
226 lines (190 loc) · 8.25 KB
/
DataRelayer.h
File metadata and controls
226 lines (190 loc) · 8.25 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
// 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.
#ifndef O2_FRAMEWORK_DATARELAYER_H_
#define O2_FRAMEWORK_DATARELAYER_H_
#include "Framework/RootSerializationSupport.h"
#include "Framework/InputRoute.h"
#include "Framework/DataDescriptorMatcher.h"
#include "Framework/ForwardRoute.h"
#include "Framework/CompletionPolicy.h"
#include <fairmq/Message.h>
#include "Framework/TimesliceIndex.h"
#include "Framework/Tracing.h"
#include "Framework/TimesliceSlot.h"
#include "Framework/ServiceRegistryRef.h"
#include <cstddef>
#include <mutex>
#include <vector>
#include <functional>
#include <fairmq/FwdDecls.h>
namespace o2::monitoring
{
class Monitoring;
}
namespace o2::framework
{
enum struct CacheEntryStatus : int {
EMPTY,
PENDING,
RUNNING,
DONE
};
class DataRelayer
{
public:
/// DataRelayer is thread safe because we have a lock around
/// each method and there is no particular order in which
/// methods need to be called.
constexpr static ServiceKind service_kind = ServiceKind::Global;
/// This represents what the DataRelayer did when
/// inserting a set of messages in the cache.
struct RelayChoice {
enum struct Type {
WillRelay, /// Ownership of the data has been taken
Invalid, /// The incoming data was not valid and has been dropped
Backpressured, /// The incoming data was not relayed, because we are backpressured
Dropped /// The incoming data was not relayed and has been dropped
};
/// What was the outcome of the relay operation.
Type type;
// The timeslice affected by the given operation.
TimesliceId timeslice;
};
struct ActivityStats {
int newSlots = 0;
int expiredSlots = 0;
};
struct PruneOp {
TimesliceSlot slot = {-1ULL};
};
struct RecordAction {
TimesliceSlot slot;
TimesliceId timeslice;
CompletionPolicy::CompletionOp op;
};
enum struct InputType : int {
Invalid = 0,
Data = 1,
SourceInfo = 2,
DomainInfo = 3
};
struct InputInfo {
InputInfo(size_t p, size_t s, InputType t, ChannelIndex i)
: position(p), size(s), type(t), index(i)
{
}
size_t position;
size_t size;
InputType type;
ChannelIndex index;
};
DataRelayer(CompletionPolicy const&,
std::vector<InputRoute> const& routes,
TimesliceIndex&,
ServiceRegistryRef,
int);
/// This invokes the appropriate `InputRoute::danglingChecker` on every
/// entry in the cache and if it returns true, it creates a new
/// cache entry by invoking the associated `InputRoute::expirationHandler`.
/// @a createNew true if the dangling inputs are allowed to create new slots.
/// @return true if there were expirations, false if not.
ActivityStats processDanglingInputs(std::vector<ExpirationHandler> const&,
ServiceRegistryRef context, bool createNew);
using OnDropCallback = std::function<void(TimesliceSlot, std::vector<std::vector<fair::mq::MessagePtr>>&, TimesliceIndex::OldestOutputInfo info)>;
// Callback for when some messages are about to be owned by the the DataRelayer
using OnInsertionCallback = std::function<void(ServiceRegistryRef&, std::span<fair::mq::MessagePtr>&)>;
/// Prune all the pending entries in the cache.
void prunePending(OnDropCallback);
/// Prune the cache for a given slot
void pruneCache(TimesliceSlot slot, OnDropCallback onDrop = nullptr);
/// This is to relay a whole set of fair::mq::Messages, all which are part
/// of the same set of split parts.
/// @a rawHeader raw header pointer
/// @a messages pointer to array of messages
/// @a nMessages size of the array
/// @a nPayloads number of payploads in the message sequence, default is 1
/// which is the standard header-payload message pair, in this
/// case nMessages / 2 pairs will be inserted and considered
/// separate parts
/// @a onDrop function to be called if an message is dropped
/// Notice that we expect that the header is an O2 Header Stack
RelayChoice relay(void const* rawHeader,
std::unique_ptr<fair::mq::Message>* messages,
InputInfo const& info,
size_t nMessages,
size_t nPayloads = 1,
OnInsertionCallback onInsertion = nullptr,
OnDropCallback onDrop = nullptr);
/// This is to set the oldest possible @a timeslice this relayer can
/// possibly see on an input channel @a channel.
void setOldestPossibleInput(TimesliceId timeslice, ChannelIndex channel);
/// This is to retrieve the oldest possible @a timeslice this relayer can
/// possibly have in output.
[[nodiscard]] TimesliceIndex::OldestOutputInfo getOldestPossibleOutput() const;
/// @returns the actions ready to be performed.
void getReadyToProcess(std::vector<RecordAction>& completed);
/// Returns an input registry associated to the given timeslice and gives
/// ownership to the caller. This is because once the inputs are out of the
/// DataRelayer they need to be deleted once the processing is concluded.
std::vector<std::vector<fair::mq::MessagePtr>> consumeAllInputsForTimeslice(TimesliceSlot id);
std::vector<std::vector<fair::mq::MessagePtr>> consumeExistingInputsForTimeslice(TimesliceSlot id);
/// Returns how many timeslices we can handle in parallel
[[nodiscard]] size_t getParallelTimeslices() const;
/// Tune the maximum number of in flight timeslices this can handle.
void setPipelineLength(size_t s);
/// Send metrics with the VariableContext information
void sendContextState();
void publishMetrics();
/// Get timeslice associated to a given slot.
/// Notice how this avoids exposing the timesliceIndex directly
/// so that we can mutex on it.
TimesliceId getTimesliceForSlot(TimesliceSlot slot);
/// Mark a given slot as done so that the GUI
/// can reflect that.
void updateCacheStatus(TimesliceSlot slot, CacheEntryStatus oldStatus, CacheEntryStatus newStatus);
/// Get the firstTForbit associate to a given slot.
uint32_t getFirstTFOrbitForSlot(TimesliceSlot slot);
/// Get the firstTFCounter associate to a given slot.
uint32_t getFirstTFCounterForSlot(TimesliceSlot slot);
/// Get the runNumber associated to a given slot
uint32_t getRunNumberForSlot(TimesliceSlot slot);
/// Get the creation time associated to a given slot
uint64_t getCreationTimeForSlot(TimesliceSlot slot);
/// Remove all pending messages
void clear();
/// Rescan the whole data to see if there is anything new we should do,
/// e.g. as consequnce of an OOB event.
void rescan() { mTimesliceIndex.rescan(); };
[[nodiscard]] size_t getCacheSize() const { return mCache.size(); }
[[nodiscard]] size_t getNumberOfTimeslices() const { return mTimesliceIndex.size(); }
[[nodiscard]] size_t getNumberOfUniqueInputs() const { return mDistinctRoutesIndex.size(); }
private:
ServiceRegistryRef mContext;
/// This is the actual cache of all the parts in flight.
/// Notice that we store them as a NxM sized vector, where
/// N is the maximum number of inflight timeslices, while
/// M is the number of inputs which are requested.
std::vector<std::vector<fair::mq::MessagePtr>> mCache;
/// This is the index which maps a given timestamp to the associated
/// cacheline.
TimesliceIndex& mTimesliceIndex;
CompletionPolicy mCompletionPolicy;
std::vector<size_t> mDistinctRoutesIndex;
std::vector<InputSpec> mInputs;
std::vector<data_matcher::DataDescriptorMatcher> mInputMatchers;
std::vector<data_matcher::VariableContext> mVariableContextes;
std::vector<CacheEntryStatus> mCachedStateMetrics;
std::vector<PruneOp> mPruneOps;
size_t mMaxLanes;
O2_LOCKABLE_NAMED(std::recursive_mutex, mMutex, "data relayer mutex");
};
} // namespace o2::framework
#endif // O2_FRAMEWORK_DATARELAYER_H_