forked from griddb/griddb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_engine.h
2962 lines (2286 loc) · 72.4 KB
/
event_engine.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2017 TOSHIBA Digital Solutions Corporation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
@file
@brief Definition of EventEngine
*/
#ifndef EVENT_ENGINE_H_
#define EVENT_ENGINE_H_
#include "data_type.h"
#include "gs_error.h"
#include "util/net.h"
#include "util/container.h"
class PartitionGroupConfig;
struct NodeDescriptor;
/*!
@brief EventEngine
*/
class EventEngine {
public:
friend struct NodeDescriptor;
struct Config;
struct Source;
struct EventRequestOption;
class BufferManager;
class ExternalBuffer;
class Buffer;
class Event;
class EventContext;
class EventHandler;
class EventCoder;
class ThreadErrorHandler;
class Stats;
struct SocketInfo;
struct SocketStats;
struct Tool;
typedef int32_t EventType;
typedef int64_t EventMonotonicTime;
typedef util::FixedSizeAllocator<util::Mutex> FixedSizeAllocator;
typedef util::VariableSizeAllocator<> VariableSizeAllocator;
typedef util::XArrayOutStream< util::StdAllocator<
uint8_t, VariableSizeAllocator> > EventOutStream;
typedef util::ArrayByteInStream EventByteInStream;
typedef util::ByteStream<EventOutStream> EventByteOutStream;
typedef std::pair<uint64_t, uint64_t> BufferId;
typedef std::pair<SocketInfo, SocketStats> SocketStatsWithInfo;
/*!
@brief Event handling mode
*/
enum EventHandlingMode {
HANDLING_PARTITION_SERIALIZED,
HANDLING_IMMEDIATE,
HANDLING_QUEUEING
};
/*!
@brief Local event type
*/
enum LocalEventType {
LOCAL_EVENT_WORKER_STARTED,
LOCAL_EVENT_WORKER_TERMINATING,
LOCAL_IO_WORKER_STARTED,
LOCAL_IO_WORKER_TERMINATING
};
/*!
@brief Resume condition at pending
*/
enum PendingResumeCondition {
RESUME_ALWAYS,
RESUME_AT_PARTITION_CHANGE
};
EventEngine(
const Config &config, const Source &source, const char8_t *name);
~EventEngine();
void start();
void shutdown();
void waitForShutdown();
const char8_t* getName() const;
NodeDescriptor getServerND(NodeDescriptorId ndId);
NodeDescriptor getServerND(const util::SocketAddress &address);
NodeDescriptor resolveServerND(const util::SocketAddress &address);
bool setServerNodeId(
const util::SocketAddress &address, NodeDescriptorId ndId,
bool modifiable);
NodeDescriptor getSelfServerND();
NodeDescriptor getMulticastND();
template<typename T> void setUserDataType();
void setLocalHandler(EventHandler &handler);
void setCloseEventHandler(EventType type, EventHandler &handler);
void setDisconnectHandler(EventHandler &handler);
void setEventCoder(EventCoder &coder);
bool send(Event &ev, const NodeDescriptor &nd,
const EventRequestOption *option = NULL);
void resetConnection(const NodeDescriptor &nd);
void setHandler(EventType type, EventHandler &handler);
void setHandlingMode(EventType type, EventHandlingMode mode);
void setUnknownEventHandler(EventHandler &handler);
void setThreadErrorHandler(ThreadErrorHandler &handler);
void add(const Event &ev);
void addTimer(const Event &ev, int32_t timeoutMillis);
void addPeriodicTimer(const Event &ev, int32_t intervalMillis);
void addPending(const Event &ev, PendingResumeCondition condition);
void executeAndWait(EventContext &ec, Event &ev);
void getStats(Stats &stats);
bool getStats(PartitionGroupId pgId, Stats &stats);
void getSocketStats(util::Vector<SocketStatsWithInfo> &statsList);
EventMonotonicTime getMonotonicTime();
private:
template<typename T> struct UserDataTraits;
class NDPool;
class SocketPool;
class Dispatcher;
class Limitter;
struct Manipulator;
class ClockGenerator;
class Listener;
class IOWorker;
class EventWorker;
class EventSocket;
struct SocketReference;
struct EventProgressWatcher;
class DefaultEventCoder;
typedef void* (UserDataConstructor)(void*);
typedef void (UserDataDestructor)(void*);
typedef util::LockGuard<util::Mutex> LockGuard;
typedef std::vector< Event*, util::StdAllocator<
Event*, VariableSizeAllocator> > EventList;
EventEngine(const EventEngine&);
EventEngine& operator=(const EventEngine&);
void cleanWorkers();
void setUserDataType(
UserDataConstructor &constructor, UserDataDestructor &destructor,
size_t dataSize);
FixedSizeAllocator *fixedAllocator_;
const std::string name_;
UTIL_UNIQUE_PTR<const Config> config_;
UTIL_UNIQUE_PTR<const PartitionGroupConfig> pgConfig_;
UTIL_UNIQUE_PTR<Stats> stats_;
UTIL_UNIQUE_PTR<NDPool> ndPool_;
UTIL_UNIQUE_PTR<SocketPool> socketPool_;
UTIL_UNIQUE_PTR<Dispatcher> dispatcher_;
UTIL_UNIQUE_PTR<Limitter> limitter_;
util::SocketAddress selfAddress_;
UTIL_UNIQUE_PTR<ClockGenerator> clockGenerator_;
UTIL_UNIQUE_PTR<Listener> listener_;
IOWorker *ioWorkerList_;
EventWorker *eventWorkerList_;
BufferManager *bufferManager_;
};
typedef EventEngine::EventRequestOption EventRequestOption;
typedef EventEngine::Event Event;
typedef EventEngine::EventContext EventContext;
typedef EventEngine::EventHandler EventHandler;
typedef EventEngine::EventCoder EventCoder;
typedef EventEngine::EventType EventType;
typedef EventEngine::EventMonotonicTime EventMonotonicTime;
typedef EventEngine::EventByteInStream EventByteInStream;
typedef EventEngine::EventByteOutStream EventByteOutStream;
/*!
@brief Represents node descriptor
*/
struct NodeDescriptor {
public:
friend struct EventEngine::Manipulator;
struct Body;
/*!
@brief ND type
*/
enum Type {
ND_TYPE_CLIENT,
ND_TYPE_SERVER,
ND_TYPE_MULTICAST
};
static const NodeDescriptor EMPTY_ND;
explicit NodeDescriptor(Body &body);
NodeDescriptor();
NodeDescriptor(const NodeDescriptor &nd);
NodeDescriptor& operator=(const NodeDescriptor &nd);
~NodeDescriptor();
bool isEmpty() const;
bool isSelf() const;
Type getType() const;
NodeDescriptorId getId() const;
const util::SocketAddress& getAddress() const;
template<typename T> T& getUserData() const;
static const char8_t* typeToString(Type type);
std::ostream& format(std::ostream &stream) const;
private:
typedef EventEngine::UserDataConstructor UserDataConstructor;
void* resolveUserData(UserDataConstructor &constructor) const;
Body *body_;
};
std::ostream& operator<<(std::ostream &stream, const NodeDescriptor &nd);
/*!
@brief Represents config of EventEngine
*/
struct EventEngine::Config {
public:
Config();
Config& setClientNDEnabled(bool enabled);
Config& setServerNDAutoNumbering(bool enabled);
Config& setListenAddress(const char8_t *address, uint16_t port);
Config& setMulticastAddress(const char8_t *address, uint16_t port);
Config& setServerAddress(const char8_t *address, uint16_t port);
Config& setConcurrency(uint32_t concurrency);
Config& setPartitionCount(uint32_t partitionCount);
Config& setAllAllocatorGroup(util::AllocatorGroupId id);
bool clientNDEnabled_;
bool serverNDAutoNumbering_;
util::SocketAddress listenAddress_;
util::SocketAddress multicastAddress_;
Config& setMulticastIntefaceAddress(const char8_t *address, uint16_t port);
util::SocketAddress multicastInterfaceAddress_;
util::SocketAddress serverAddress_;
double ioConcurrencyRate_;
bool secondaryIOEnabled_;
bool ioNegotiationMinimum_;
bool keepaliveEnabled_;
int32_t keepaliveIdle_;
int32_t keepaliveInterval_;
int32_t keepaliveCount_;
bool tcpNoDelay_;
uint32_t backlogSize_;
int32_t multicastTTL_;
uint32_t ioBufferSize_;
int32_t reconnectIntervalMillis_;
int32_t connectTimeoutMillis_;
PartitionId filteredPartitionId_;
int64_t connectionCountLimit_;
int64_t sendBufferSizeLimit_;
uint32_t concurrency_;
uint32_t partitionCount_;
int64_t eventBufferSizeLimit_;
uint32_t clockIntervalMillis_;
uint32_t clockCorrectionMillis_;
uint32_t clockCorrectionMaxTrial_;
util::AllocatorGroupId allocatorGroupId_;
util::AllocatorGroupId workAllocatorGroupId_;
};
/*!
@brief EventEngine::Source
*/
struct EventEngine::Source {
Source(
VariableSizeAllocator &varAllocator,
FixedSizeAllocator &fixedAllocator);
VariableSizeAllocator& resolveVariableSizeAllocator() const;
FixedSizeAllocator& resolveFixedSizeAllocator() const;
VariableSizeAllocator *varAllocator_;
FixedSizeAllocator *fixedAllocator_;
BufferManager *bufferManager_;
};
/*!
@brief Represents the option of event request
*/
struct EventEngine::EventRequestOption {
EventRequestOption();
bool operator==(const EventRequestOption &option) const;
int32_t timeoutMillis_;
bool onSecondary_;
};
class EventEngine::BufferManager {
public:
virtual ~BufferManager();
virtual size_t getUnitSize() = 0;
virtual std::pair<BufferId, void*> allocate() = 0;
virtual void deallocate(const BufferId &id) = 0;
virtual void* latch(const BufferId &id) = 0;
virtual void unlatch(const BufferId &id) = 0;
};
class EventEngine::ExternalBuffer {
public:
class Reader;
class Writer;
explicit ExternalBuffer(BufferManager *manager = NULL);
~ExternalBuffer();
void clear();
void swap(ExternalBuffer &another);
bool isEmpty() const;
size_t getSize() const;
size_t getCapacity() const;
private:
ExternalBuffer(const ExternalBuffer&);
ExternalBuffer& operator=(const ExternalBuffer&);
BufferManager *manager_;
BufferId id_;
size_t size_;
size_t capacity_;
};
class EventEngine::ExternalBuffer::Reader {
public:
explicit Reader(const ExternalBuffer &buffer);
~Reader();
const void* data();
private:
Reader(const Reader&);
Reader& operator=(const Reader&);
BufferManager *manager_;
BufferId id_;
const void *data_;
};
class EventEngine::ExternalBuffer::Writer {
public:
explicit Writer(ExternalBuffer &buffer);
~Writer();
size_t tryAppend(const void *data, size_t size);
private:
Writer(const Writer&);
Writer& operator=(const Writer&);
ExternalBuffer &bufferRef_;
ExternalBuffer buffer_;
void *data_;
};
/*!
@brief Handles buffer in EventEngine
*/
class EventEngine::Buffer {
public:
typedef util::XArray< uint8_t,
util::StdAllocator<uint8_t, VariableSizeAllocator> > XArray;
explicit Buffer(VariableSizeAllocator &allocator);
Buffer();
~Buffer();
Buffer(const Buffer &another);
Buffer& operator=(const Buffer &another);
EventByteInStream getInStream() const;
EventByteOutStream getOutStream();
const XArray& getXArray() const;
XArray& getWritableXArray();
bool isEmpty() const;
void clear();
size_t getSize() const;
size_t getOffset() const;
void setOffset(size_t offset);
size_t getCapacity() const;
VariableSizeAllocator* getAllocator();
void transferTo(Buffer &another) const;
ExternalBuffer& prepareExternal(BufferManager *manager);
const ExternalBuffer* getExternal() const;
size_t getExternalOffset() const;
void setExternalOffset(size_t offset);
private:
struct Body;
mutable Body *body_;
size_t offset_;
size_t extOffset_;
};
/*!
@brief Represents event
*/
class EventEngine::Event {
public:
struct Source;
/*!
@brief Number of max extra message
*/
enum {
MAX_EXTRA_MESSAGE_COUNT = 2
};
Event(const Source &source, EventType type, PartitionId partitionId);
Event();
~Event();
Event(VariableSizeAllocator &varAllocator, const Event &ev);
Event(const Event &ev);
Event& operator=(const Event &ev);
void resetAttributes(EventType type, PartitionId partitionId,
const NodeDescriptor &senderND);
bool isEmpty() const;
EventType getType() const;
PartitionId getPartitionId() const;
const NodeDescriptor& getSenderND() const;
void setSenderND(const NodeDescriptor &nd);
EventMonotonicTime getQueuedMonotonicTime() const;
void setQueuedMonotonicTime(EventMonotonicTime time);
uint32_t getQueueingCount() const;
void incrementQueueingCount();
EventByteInStream getInStream() const;
EventByteOutStream getOutStream();
bool isMessageSpecified() const;
const Buffer& getMessageBuffer() const;
Buffer& getMessageBuffer();
void transferMessageBuffer(const Buffer &src);
size_t getExtraMessageCount() const;
void getExtraMessage(size_t index, const void *&ptr, size_t &size) const;
void addExtraMessage(const void *ptr, size_t size);
Source getSource();
uint16_t getTailMetaMessageSize() const;
void setTailMetaMessageSize(uint16_t size);
void clearTailMetaMessage();
bool isPartitionIdSpecified() const;
void setPartitionIdSpecified(bool specified);
void setPartitionId(PartitionId pId);
EventProgressWatcher* getProgressWatcher() const;
void setProgressWatcher(EventProgressWatcher *progressWatcher);
private:
typedef std::pair<const void *, size_t> ExtraMessage;
void mergeExtraMessages();
VariableSizeAllocator *allocator_;
EventType type_;
PartitionId partitionId_;
NodeDescriptor senderND_;
EventMonotonicTime queuedMonotonicTime_;
uint32_t queueingCount_;
uint16_t tailMetaMessageSize_;
bool partitionIdSpecified_;
Buffer messageBuffer_;
size_t extraMessageCount_;
ExtraMessage extraMessageList_[MAX_EXTRA_MESSAGE_COUNT];
EventProgressWatcher *progressWatcher_;
};
/*!
@brief Event::Source
*/
struct EventEngine::Event::Source {
Source(const EventEngine::Source &source);
explicit Source(VariableSizeAllocator &allocator);
VariableSizeAllocator *allocator_;
};
/*!
@brief Represents contextual information around the current event
*/
class EventEngine::EventContext {
public:
friend struct Manipulator;
struct Source;
typedef std::vector<bool> PendingPartitionCheckList;
explicit EventContext(const Source &source);
operator Event::Source&();
EventEngine& getEngine();
VariableSizeAllocator& getVariableSizeAllocator();
util::StackAllocator& getAllocator();
const util::DateTime& getHandlerStartTime() const;
void setHandlerStartTime(const util::DateTime &time);
EventMonotonicTime getHandlerStartMonotonicTime() const;
void setHandlerStartMonotonicTime(EventMonotonicTime time);
bool isPendingPartitionChanged(PartitionId partitionId) const;
void setPendingPartitionChanged(PartitionId partitionId);
bool resetPendingPartitionStatus();
EventProgressWatcher& getProgressWatcher();
uint32_t getWorkerId() const;
bool isOnIOWorker() const;
bool isOnSecondaryIOWorker() const;
int64_t getLastEventCycle() const;
private:
EventContext(const EventContext&);
EventContext& operator=(const EventContext&);
size_t getCheckListIndex(PartitionId partitionId) const;
EventEngine *ee_;
VariableSizeAllocator &varAllocator_;
util::StackAllocator &allocator_;
util::DateTime handlerStartTime_;
EventMonotonicTime handlerStartMonotonicTime_;
const PartitionId beginPartitionId_;
PendingPartitionCheckList *pendingPartitionCheckList_;
bool pendingPartitionChanged_;
EventProgressWatcher *progressWatcher_;
Event::Source eventSource_;
Stats &workerStats_;
uint32_t workerId_;
bool onIOWorker_;
bool onSecondaryIOWorker_;
const EventList *eventList_;
};
/*!
@brief EventContext::Source
*/
struct EventEngine::EventContext::Source {
explicit Source(
VariableSizeAllocator &varAllocator, util::StackAllocator &allocator,
Stats &workerStats);
EventEngine *ee_;
VariableSizeAllocator *varAllocator_;
util::StackAllocator *allocator_;
PartitionId beginPartitionId_;
PendingPartitionCheckList *pendingPartitionCheckList_;
EventProgressWatcher *progressWatcher_;
Stats *workerStats_;
uint32_t workerId_;
bool onIOWorker_;
bool onSecondaryIOWorker_;
const EventList *eventList_;
};
/*!
@brief Handles event
*/
class EventEngine::EventHandler {
public:
virtual void operator()(EventContext &ec, Event &ev) = 0;
};
/*!
@brief Encodes/Decodes event
*/
class EventEngine::EventCoder {
public:
virtual size_t decode(
EventEngine &ee, Buffer &buffer, Event &ev,
const NodeDescriptor &nd,
const util::SocketAddress &socketAddress,
EventRequestOption &option) = 0;
virtual int32_t encode(
EventEngine &ee, Event &ev,
const void *&ptr, size_t &size, int32_t index,
const EventRequestOption *option,
bool followingEventEncoding) = 0;
};
/*!
@brief Handles thread error
*/
class EventEngine::ThreadErrorHandler {
public:
virtual void operator()(EventContext &ec, std::exception &e) = 0;
};
/*!
@brief Represents the statistics of EventEngine
*/
class EventEngine::Stats {
public:
/*!
@brief Statistics type of EventEngine
*/
enum Type {
MAIN_LISTENER_COUNT,
MAIN_IO_WORKER_COUNT,
MAIN_EVENT_WORKER_COUNT,
MAIN_THREAD_ERROR_COUNT,
MAIN_TOTAL_ALLOC_SIZE,
MAIN_FREE_ALLOC_SIZE,
MAIN_HUGE_ALLOC_COUNT,
MAIN_HUGE_ALLOC_SIZE,
ND_SERVER_CREATE_COUNT,
ND_SERVER_REMOVE_COUNT,
ND_CLIENT_CREATE_COUNT,
ND_CLIENT_REMOVE_COUNT,
ND_MCAST_CREATE_COUNT,
ND_MCAST_REMOVE_COUNT,
ND_MODIFY_COUNT,
ND_MODIFY_ERROR_COUNT,
ND_USER_DATA_CREATE_COUNT,
ND_USER_DATA_REMOVE_COUNT,
SOCKET_CREATE_COUNT,
SOCKET_REMOVE_COUNT,
CLOCK_INITIALIZE_COUNT,
CLOCK_INITIALIZE_ERROR_COUNT,
CLOCK_UPDATE_ERROR_COUNT,
LISTENER_BIND_COUNT,
LISTENER_BIND_ERROR_COUNT,
WORKER_DISPATCH_COUNT,
WORKER_TIMED_DISPATCH_COUNT,
WORKER_DISPATCH_TIMEOUT_MAX,
WORKER_DISPATCH_TIMEOUT_MIN,
WORKER_IMMEDIATE_DISPATCH_COUNT,
WORKER_CLOSING_DISPATCH_COUNT,
WORKER_WAIT_START_COUNT,
WORKER_WAIT_ERROR_COUNT,
WORKER_WAIT_TIME_MAX,
WORKER_WAIT_TIME_TOTAL,
WORKER_HANDLED_EVENT_COUNT,
WORKER_LOCAL_EVENT_COUNT,
WORKER_UNKNOWN_EVENT_COUNT,
WORKER_STOPPED_THREAD_COUNT,
WORKER_HANDLING_THREAD_COUNT,
WORKER_IDLE_THREAD_COUNT,
WORKER_WAITING_THREAD_COUNT,
IO_CYCLE_COUNT,
IO_MANAGED_SOCKET_COUNT,
IO_POLLING_SOCKET_COUNT,
IO_CONNECTING_SOCKET_COUNT,
IO_SUSPENDING_SOCKET_COUNT,
IO_REQUEST_CANCEL_COUNT,
IO_REQUEST_CONNECT_COUNT,
IO_REQUEST_ADD_COUNT,
IO_REQUEST_MOVE_COUNT,
IO_REQUEST_REMOVE_COUNT,
IO_REQUEST_MODIFY_COUNT,
IO_REQUEST_SUSPEND_COUNT,
IO_RECEIVE_BUFFER_SIZE_CURRENT,
IO_RECEIVE_BUFFER_SIZE_MAX,
IO_SEND_BUFFER_SIZE_CURRENT,
IO_SEND_BUFFER_SIZE_MAX,
IO_ERROR_OPEN_COUNT,
IO_ERROR_CONNECT_COUNT,
IO_ERROR_RECEIVE_COUNT,
IO_ERROR_SEND_COUNT,
IO_ERROR_OTHER_COUNT,
EVENT_CYCLE_COUNT,
EVENT_ACTIVE_ADD_COUNT,
EVENT_PENDING_ADD_COUNT,
EVENT_CANCELED_ADD_COUNT,
EVENT_ACTIVE_QUEUE_SIZE_CURRENT,
EVENT_ACTIVE_QUEUE_SIZE_MAX,
EVENT_ACTIVE_BUFFER_SIZE_CURRENT,
EVENT_ACTIVE_BUFFER_SIZE_MAX,
EVENT_PENDING_QUEUE_SIZE_CURRENT,
EVENT_PENDING_QUEUE_SIZE_MAX,
STATS_LIVE_TYPE_START,
EVENT_ACTIVE_EXECUTABLE_COUNT =
STATS_LIVE_TYPE_START,
EVENT_CYCLE_HANDLING_COUNT,
EVENT_CYCLE_HANDLING_AFTER_COUNT,
STATS_TYPE_MAX
};
Stats();
int64_t get(Type type) const;
void set(Type type, int64_t value);
void updateMax(Type type, int64_t value);
void updateMin(Type type, int64_t value);
void increment(Type type);
void decrement(Type type);
void merge(Type type, int64_t value, bool sumOnly = false);
void mergeAll(const Stats &stats, bool sumOnly = false);
static const char8_t* typeToString(Type type);
void dump(std::ostream &out) const;
private:
static const char8_t *const STATS_TYPE_NAMES[STATS_TYPE_MAX];
static const Type STATS_MAX_INITIAL_VALUE_TYPES[];
static const Type STATS_MIN_INITIAL_VALUE_TYPES[];
static const Type STATS_MAX_TYPES[];
static const Type STATS_MIN_TYPES[];
template<size_t N>
static bool findType(const Type (&typeArray)[N], Type type);
int64_t valueList_[STATS_TYPE_MAX];
};
struct EventEngine::SocketInfo {
SocketInfo();
NodeDescriptor nd_;
util::SocketAddress localAddress_;
util::SocketAddress remoteAddress_;
bool multicast_;
};
struct EventEngine::SocketStats {
SocketStats();
int64_t dispatchingEventCount_;
int64_t sendingEventCount_;
util::DateTime initialTime_;
};
struct EventEngine::Tool {
static bool getLiveStats(
EventEngine &ee, PartitionGroupId pgId, Stats::Type type,
int64_t &value, const EventContext *ec, const Event *ev,
const EventMonotonicTime *now = NULL);
};
struct NodeDescriptor::Body {
public:
friend struct NodeDescriptor;
friend class EventEngine::NDPool;
/*!
@brief Socket type
*/
enum SocketType {
ND_SOCKET_SENDER,
ND_SOCKET_RECEIVER,
ND_SOCKET_MAX
};
enum SocketMode {
SOCKET_MODE_SINGLE,
SOCKET_MODE_RESOLVING,
SOCKET_MODE_FULL,
SOCKET_MODE_MIXED
};
typedef EventEngine::EventSocket EventSocket;
typedef EventEngine::NDPool NDPool;
typedef EventEngine::LockGuard LockGuard;
typedef EventEngine::VariableSizeAllocator VariableSizeAllocator;
Body(
NodeDescriptorId id, Type type, bool self, bool onSecondary,
EventEngine &ee);
~Body();
EventEngine& getEngine();
util::Mutex& getLock();
Body* findPrimary();
Body* findSecondary();
void setSocketMode(LockGuard &ndGuard, SocketMode mode);
SocketMode getSocketMode(LockGuard &ndGuard);
bool findSocketType(
LockGuard &ndGuard, EventSocket *socket, SocketType &type);
void setSocket(LockGuard &ndGuard, EventSocket *socket, SocketType type);
EventSocket* getSocket(LockGuard &ndGuard);
bool removeSocket(LockGuard &ndGuard, EventSocket *socket);
int32_t acceptEventTimeout(
LockGuard &ndGuard, EventType type, int32_t timeoutMillis);
private:
typedef std::pair<EventType, int64_t> TimeMapEntry;
typedef std::map<
EventType, int64_t, std::map<EventType, int64_t>::key_compare,
util::StdAllocator<TimeMapEntry, VariableSizeAllocator> > TimeMap;
Body(const Body&);
Body& operator=(const Body&);
void* resolveUserData(UserDataConstructor &constructor);
static Body* duplicateReference(Body *body);
static void removeReference(Body *&body);
const NodeDescriptorId id_;
const Type type_;
const bool self_;
const bool onSecondary_;
SocketMode mode_;
util::Atomic<const util::SocketAddress*> address_;
EventEngine &ee_;
util::Atomic<uint64_t> refCount_;
util::Atomic<void*> userData_;
Body *freeLink_;
util::Mutex mutex_;
TimeMap *eventTimeMap_;
EventSocket *socketList_[ND_SOCKET_MAX];
Body *another_;
};
struct EventEngine::Buffer::Body {
static Body* newInstance(VariableSizeAllocator &allocator);
static Body* duplicateReference(Body *body);
static void removeReference(Body *&body);
explicit Body(VariableSizeAllocator &allocator);
VariableSizeAllocator& getAllocator();
ExternalBuffer& prepareExternal(BufferManager *manager);
Body(const Body&);
Body& operator=(const Body&);
uint64_t refCount_;
XArray storage_;
ExternalBuffer ext_;
};
class EventEngine::NDPool {
public:
friend struct NodeDescriptor::Body;
typedef std::pair<EventType, int64_t> TimeMapEntry;
typedef std::map<
EventType, int64_t, std::map<EventType, int64_t>::key_compare,
util::StdAllocator<TimeMapEntry, VariableSizeAllocator> > TimeMap;
explicit NDPool(EventEngine &ee);
~NDPool();
NodeDescriptor getServerND(NodeDescriptorId ndId);
NodeDescriptor getServerND(const util::SocketAddress &address);
NodeDescriptor resolveServerND(const util::SocketAddress &address);
bool setServerNodeId(
const util::SocketAddress &address, NodeDescriptorId ndId,
bool modifiable);
NodeDescriptor getSelfServerND();
NodeDescriptor getMulticastND();
void setUserDataType(
UserDataConstructor &constructor, UserDataDestructor &destructor,
size_t dataSize);
NodeDescriptor allocateClientND();
int64_t& prepareLastEventTime(TimeMap *&map, EventType type);
void releaseTimeMap(TimeMap *&map);
struct SocketAddressLess :
public std::binary_function<util::SocketAddress, util::SocketAddress, bool> {
bool operator()(
const util::SocketAddress &left,
const util::SocketAddress &right) const;
};
private:
struct NDMapValue {
NDMapValue();
NodeDescriptor nd_;
util::SocketAddress *address_;
};
typedef std::vector< NodeDescriptor,
util::StdAllocator<NodeDescriptor, VariableSizeAllocator> > NDList;
typedef std::pair<util::SocketAddress, NDMapValue> NDMapPair;
typedef std::map<
util::SocketAddress,
NDMapValue,
SocketAddressLess,
util::StdAllocator<NDMapPair, VariableSizeAllocator> > NDMap;
NDPool(const NDPool&);
NDPool& operator=(const NDPool&);
NodeDescriptor putServerND(