forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchEventProcessor.h
More file actions
220 lines (196 loc) · 7.65 KB
/
BatchEventProcessor.h
File metadata and controls
220 lines (196 loc) · 7.65 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
#pragma once
#include "Disruptor/AlertException.h"
#include "Disruptor/ArgumentNullException.h"
#include "Disruptor/IDataProvider.h"
#include "Disruptor/IEventHandler.h"
#include "Disruptor/IEventProcessor.h"
#include "Disruptor/IExceptionHandler.h"
#include "Disruptor/ILifecycleAware.h"
#include "Disruptor/InvalidOperationException.h"
#include "Disruptor/ISequenceBarrier.h"
#include "Disruptor/ISequenceReportingEventHandler.h"
#include "Disruptor/ITimeoutHandler.h"
#include "Disruptor/Sequence.h"
#include "Disruptor/TimeoutException.h"
namespace Disruptor
{
/**
* Convenience class for handling the batching semantics of consuming events from a RingBuffer<T>
* and delegating the available events to an IEventHandler<T>. If the BatchEventProcessor<T>
* also implements ILifecycleAware it will be notified just after the thread is started and just before the thread is shutdown.
*
* \tparam T Event implementation storing the data for sharing during exchange or parallel coordination of an event.
*/
template <class T>
class BatchEventProcessor : public IEventProcessor
{
public:
/**
* Construct a BatchEventProcessor<T>
* that will automatically track the progress by updating its sequence when the IEventHandler<T>.OnEvent returns.
*
* \param dataProvider dataProvider to which events are published
* \param sequenceBarrier SequenceBarrier on which it is waiting.
* \param eventHandler eventHandler is the delegate to which events are dispatched.
*/
BatchEventProcessor(const std::shared_ptr< IDataProvider< T > >& dataProvider,
const std::shared_ptr< ISequenceBarrier >& sequenceBarrier,
const std::shared_ptr< IEventHandler< T > >& eventHandler)
: m_running(false)
, m_dataProvider(dataProvider)
, m_dataProviderRef(*m_dataProvider)
, m_sequenceBarrier(sequenceBarrier)
, m_sequenceBarrierRef(*m_sequenceBarrier)
, m_eventHandler(eventHandler)
, m_eventHandlerRef(*m_eventHandler)
, m_sequence(std::make_shared< Sequence >())
, m_sequenceRef(*m_sequence)
{
auto sequenceReportingHandler = std::dynamic_pointer_cast< ISequenceReportingEventHandler< T > >(eventHandler);
if (sequenceReportingHandler != nullptr)
sequenceReportingHandler->setSequenceCallback(m_sequence);
m_timeoutHandler = std::dynamic_pointer_cast< ITimeoutHandler >(eventHandler);
}
/**
* \see IEventProcessor::Sequence
*/
std::shared_ptr< ISequence > sequence() const override
{
return m_sequence;
};
/**
* Signal that this IEventProcessor should stop when it has finished consuming at the next clean break. It will call ISequenceBarrier::Alert
* to notify the thread to check status.
* \see IEventProcessor
* \see ISequenceBarrier::Alert
*/
void halt() override
{
m_running = false;
m_sequenceBarrier->alert();
}
/**
* \see IEventProcessor::IsRunning
*/
bool isRunning() const override
{
return m_running;
}
/**
* Set a new IExceptionHandler<T> for handling exceptions propagated out of the BatchEventProcessor<T>
*
* \param exceptionHandler exceptionHandler to replace the existing exceptionHandler.
*/
void setExceptionHandler(const std::shared_ptr< IExceptionHandler< T > >& exceptionHandler)
{
if (exceptionHandler == nullptr)
DISRUPTOR_THROW_ARGUMENT_NULL_EXCEPTION(exceptionHandler);
m_exceptionHandler = exceptionHandler;
}
/**
* It is ok to have another thread rerun this method after a halt().
*/
void run() override
{
if (m_running.exchange(true) != false)
{
DISRUPTOR_THROW_INVALID_OPERATION_EXCEPTION("Thread is already running");
}
m_sequenceBarrierRef.clearAlert();
notifyStart();
auto nextSequence = m_sequenceRef.value() + 1;
T* evt = nullptr;
while (true)
{
try
{
auto availableSequence = m_sequenceBarrierRef.waitFor(nextSequence);
while (nextSequence <= availableSequence)
{
evt = &m_dataProviderRef[nextSequence];
m_eventHandlerRef.onEvent(*evt, nextSequence, nextSequence == availableSequence);
nextSequence++;
}
m_sequenceRef.setValue(availableSequence);
}
catch (const TimeoutException&)
{
notifyTimeout(m_sequenceRef.value());
}
catch (const AlertException&)
{
if (m_running == false)
{
break;
}
}
catch (const std::exception& ex)
{
m_exceptionHandler->handleEventException(ex, nextSequence, *evt);
m_sequenceRef.setValue(nextSequence);
nextSequence++;
}
}
notifyShutdown();
m_running = false;
}
private:
void notifyTimeout(std::int64_t availableSequence) const
{
try
{
if (m_timeoutHandler)
m_timeoutHandler->onTimeout(availableSequence);
}
catch (std::exception& ex)
{
if (m_exceptionHandler)
m_exceptionHandler->handleOnTimeoutException(ex, availableSequence);
}
}
void notifyStart()
{
auto sequenceReportingHandler = std::dynamic_pointer_cast< ILifecycleAware >(m_eventHandler);
if (sequenceReportingHandler != nullptr)
{
try
{
sequenceReportingHandler->onStart();
}
catch (std::exception& ex)
{
if (m_exceptionHandler)
m_exceptionHandler->handleOnStartException(ex);
}
}
}
void notifyShutdown()
{
auto sequenceReportingHandler = std::dynamic_pointer_cast< ILifecycleAware >(m_eventHandler);
if (sequenceReportingHandler != nullptr)
{
try
{
sequenceReportingHandler->onShutdown();
}
catch (std::exception& ex)
{
if (m_exceptionHandler)
m_exceptionHandler->handleOnShutdownException(ex);
}
}
}
private:
std::atomic<bool> m_running;
std::shared_ptr< IDataProvider< T > > m_dataProvider;
IDataProvider< T >& m_dataProviderRef;
std::shared_ptr< ISequenceBarrier > m_sequenceBarrier;
ISequenceBarrier& m_sequenceBarrierRef;
std::shared_ptr< IEventHandler< T > > m_eventHandler;
IEventHandler< T >& m_eventHandlerRef;
std::shared_ptr< Sequence > m_sequence;
Sequence& m_sequenceRef;
std::shared_ptr< ITimeoutHandler > m_timeoutHandler;
std::shared_ptr< IExceptionHandler< T > > m_exceptionHandler;
};
} // namespace Disruptor