-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlowControllerAction.h
403 lines (318 loc) · 10.9 KB
/
FlowControllerAction.h
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
#pragma once
#include <RxConcurrent/ThreadPool/ThreadPool.h>
#include <RxConcurrent/ThreadPool/ThreadPoolFactory.h>
#include "CommonDefines.h"
#include "FutureFactory.h"
#include "FlowController.h"
#include "FlowControllerPolicy.h"
namespace BaseLib { namespace Concurrent { namespace details {
// ----------------------------------------------------
// FlowControllerState
// - Queue - FIFO/Random/etc. If FIFO then share ringbuffer from subjects-group to all specific subject instances.
// ----------------------------------------------------
template <typename V>
class FlowControllerState
{
public:
typedef Collection::RingBufferQueue<V> EventQueue;
public:
FlowControllerState(
int queueCapacity = 100,
int historyDepth = 5,
std::string threadPoolId = std::string(""))
: threadPoolId_(threadPoolId)
, scheduledTask_(std::make_shared<ScheduledFutureTaskNoOp<bool>>())
, action_()
, queue_(Collection::RingBufferQueuePolicy::LimitedTo(queueCapacity))
, history_(Collection::RingBufferQueuePolicy::Overwrite(historyDepth))
{ }
virtual ~FlowControllerState()
{ }
bool IsInitialized() const
{
return scheduledTask_.IsAttached();
}
bool Initialize(ScheduledFutureTask<bool> scheduledTask)
{
scheduledTask_ = scheduledTask;
return scheduledTask_.IsAttached();
}
ScheduledFutureTask<bool>& Task()
{
return scheduledTask_;
}
const ScheduledFutureTask<bool>& Task() const
{
return scheduledTask_;
}
std::shared_ptr<Templates::Action0<bool>> Action() const
{
return action_;
}
bool Set(std::shared_ptr<Templates::Action0<bool>> action)
{
action_ = action;
return action_ != nullptr;
}
EventQueue& Queue()
{
return queue_;
}
const EventQueue& Queue() const
{
return queue_;
}
EventQueue& History()
{
return history_;
}
const EventQueue& History() const
{
return history_;
}
private:
std::string threadPoolId_;
ScheduledFutureTask<bool> scheduledTask_;
std::shared_ptr<Templates::Action0<bool>> action_;
EventQueue queue_;
EventQueue history_;
};
// ----------------------------------------------------
// FlowControllerAction
// ----------------------------------------------------
/**
* Acts like a flow controller for producers and consumers.
*
* Responsibility:
* - The controller takes into account the user defined action's policy
* - Store last n events based on policy (History)
* - run attached action based on interval policy
* - TODO: report deadline violations?
*/
template <typename V>
class FlowControllerAction
: public BaseLib::Concurrent::FlowController<V>
, public Templates::LockableType<FlowControllerAction<V> >
, protected Templates::ContextObjectShared<FlowControllerPolicy, FlowControllerState<V>, FlowControllerStatus>
{
public:
FlowControllerAction(FlowControllerPolicy policy = FlowControllerPolicy::Default())
: Templates::ContextObjectShared<FlowControllerPolicy, FlowControllerState<V>, FlowControllerStatus>
(
new FlowControllerPolicy(policy),
new FlowControllerState<V>(policy.MaxQueueSize().Limit(), policy.History().Depth()),
new FlowControllerStatus(Duration::FromMinutes(5), Count::Create(1000))
)
{ }
virtual ~FlowControllerAction()
{ }
CLASS_TRAITS(FlowControllerAction)
friend class Templates::DoubleCheckedLocking;
// ---------------------------------------------------------
// Set active action
// ---------------------------------------------------------
// TODO: Instead of setting action, just set a std::function?
virtual bool Set(std::shared_ptr<Templates::Action0<bool>> action)
{
return this->data()->Set(action);
}
bool IsActionSet() const
{
return this->data()->Action() != nullptr;
}
int AvailableCapacity() const
{
return this->config()->MaxQueueSize().Limit() - this->data()->Queue().Length();
}
// ---------------------------------------------------------
// Flow controller get and update status
// ---------------------------------------------------------
virtual void Trigger()
{
this->data()->Task().Trigger();
}
virtual void Resume()
{
ASSERT(!IsEmpty());
this->data()->Task().Resume();
}
virtual void Suspend()
{
this->data()->Task().Suspend();
}
virtual bool IsResumable() const
{
return !IsEmpty() && IsSuspended();
}
virtual bool IsSuspendable() const
{
return IsEmpty() && !IsSuspended();
}
virtual bool IsResumed() const
{
return !this->data()->Task().IsSuspended();
}
virtual bool IsSuspended() const
{
return this->data()->Task().IsSuspended();
}
virtual bool IsEmpty() const
{
return this->data()->Queue().IsEmpty();
}
virtual const FlowControllerStatus& Status() const
{
return this->status().delegate();
}
// ---------------------------------------------------------
// Access to events
// ---------------------------------------------------------
virtual std::list<V> Pull()
{
int numElements = INT_MAX; //Strategy::ComputeNumPacketsToDequeue::Perform(this->status().delegate(), this->config()->Throughput());
std::list<V> events = this->data()->Queue().template DequeueUpto<std::list<V>>(numElements);
//IINFO() << "Queue size " << this->data()->Queue().Length() << " Dequeue max: " << numElements << ". Dequeued " << events.size();
this->status()->NextState(FlowStatusKind::DEQUEUED, events.size());
return events;
}
virtual std::list<V> History() const
{
return this->data()->History().template PeekAll<std::list<V>>();
}
// ----------------------------------------------------
// scheduled to run periodically based on subject's interval policy
// triggered by subject to notify observer
// One FlowController for each subject/observer
//
// handler = identify_handler (event);
// handler.handle_event (event);
// ----------------------------------------------------
// TODO: Instead of hard-wiring an action implementation, attach a scheduler/call-back action
virtual bool Invoke()
{
if(isDone())
{
IINFO() << "Flow controller is done!";
return false;
}
// ---------------------------------------------------
// TODO: Store dispatched events? Clean up received events
// Check if window allows more events to be sent out
// Check if event queue is empty
// TODO: Make as policy: "Only execute action when events in queue"
// ---------------------------------------------------
if(!this->data()->Queue().IsEmpty())
{
Status::ExecutionStatus status;
Strategy::DefaultActionExecution<bool>::Perform(this->data()->Action().operator*(), status);
if(!status.IsSuccess())
{
ICRITICAL() << "Failed to execute action from flow controller";
return false;
}
}
// ----------------------------------------
// TODO: Make as policy: "Suspend this task if queue is empty"
// ----------------------------------------
//Templates::DoubleCheckedLocking::ConditionalSuspend<FlowControllerAction<V> >(this);
return true;
}
virtual bool Cancel()
{
return this->data()->Task().Cancel();
}
virtual bool IsDone() const
{
return this->data()->Task().IsDone();
}
virtual bool IsSuccess() const
{
return this->data()->Task().IsSuccess();
}
// ---------------------------------------------------------
// Reactor methods
// ---------------------------------------------------------
virtual bool Next(V event)
{
bool initialize = Templates::DoubleCheckedLocking::Initialize<FlowControllerAction<V>>(this);
ASSERT(initialize);
if(isDone())
{
IINFO() << "Flow controller is done!";
return false;
}
// -------------------------
// cache event
// -------------------------
bool isPushed = this->data()->Queue().Enqueue(event, 1000);
if(isPushed)
{
bool isAdded = this->data()->History().TryEnqueue(event);
ASSERT(isAdded);
this->status()->NextState(FlowStatusKind::ENQUEUED, 1);
//IINFO() << "Pushed event! Queue size " << this->data()->Queue().Length() << " history " << this->data()->History().Length();
//Templates::DoubleCheckedLocking::ConditionalResume<FlowControllerAction<V>>(this);
this->Resume();
return true;
}
else
{
ICRITICAL() << "Failed to push event! Queue size " << this->data()->Queue().Length() << " history " << this->data()->History().Length();
//ASSERT(isPushed);
this->status()->NextState(FlowStatusKind::REJECTED, 1);
return false;
}
}
virtual bool Complete()
{
return this->data()->Task().Cancel();
}
virtual bool Error(BaseLib::Exception)
{
return this->data()->Task().Cancel();
}
virtual Duration NextSchedule(const Status::ExecutionStatus&, const TaskPolicy&)
{
if(!this->data()->Queue().IsEmpty() && !this->data()->Task().IsExecuting())
{
return Duration::Zero();
}
return Duration::FromMilliseconds(1000);
}
protected:
// ---------------------------------------------------------
// Initialize methods
// ---------------------------------------------------------
virtual bool Initialize()
{
auto action = this->shared_from_this();
ASSERT(action);
bool suspended = false;
ScheduledFutureTask<bool> scheduledTask = ThreadPoolFactory::Instance().GetDefault()->template Schedule<bool>(
[action]() -> bool { return action->Invoke(); },
[action](const Status::ExecutionStatus& status, const TaskPolicy& policy) -> Duration { return action->NextSchedule(status, policy); },
this->config()->Task(),
suspended
);
return this->data()->Initialize(scheduledTask);
}
virtual bool IsInitialized() const
{
return this->data()->IsInitialized();
}
private:
bool isDone() const
{
return isFlowControllerDone() && isActionDone();
}
bool isFlowControllerDone() const
{
return this->data()->Task().IsCancelled() || this->data()->Task().IsDone();
}
bool isActionDone() const
{
ASSERT(this->data()->Action());
return this->data()->Action()->IsDone();
}
};
}}}