-
Notifications
You must be signed in to change notification settings - Fork 225
/
callback.hxx
291 lines (242 loc) · 7.37 KB
/
callback.hxx
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
/************************************************************************
Copyright 2017-2019 eBay Inc.
Author/Developer(s): Jung-Sang Ahn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/
#ifndef _CALLBACK_H_
#define _CALLBACK_H_
#include <cstdint>
#include <functional>
#include <string>
namespace nuraft {
class cb_func {
public:
enum Type {
/**
* Got request from peer or client.
* ctx: pointer to request.
*/
ProcessReq = 1,
/**
* Got append entry response from peer.
* ctx: pointer to new matched index number.
*/
GotAppendEntryRespFromPeer = 2,
/**
* Appended logs and executed pre-commit locally.
* Happens on leader only.
* ctx: pointer to last log number.
*/
AppendLogs = 3,
/**
* Heartbeat timer wakes up.
* Happens on leader only.
* ctx: pointer to last log number.
*/
HeartBeat = 4,
/**
* Joined a cluster.
* Happens on follower only.
* ctx: pointer to cluster config.
*/
JoinedCluster = 5,
/**
* Became a leader.
* ctx: pointer to term number.
*/
BecomeLeader = 6,
/**
* Request append entries to followers.
* Happens on leader only.
*/
RequestAppendEntries = 7,
/**
* Save snapshot chunk or object, in receiver's side.
* ctx: snapshot_sync_req.
*/
SaveSnapshot = 8,
/**
* Committed a new config.
* ctx: pointer to log index of new config.
*/
NewConfig = 9,
/**
* Removed from a cluster.
* Happens on follower only.
* ctx: null.
*/
RemovedFromCluster = 10,
/**
* Became a follower.
* ctx: pointer to term number.
*/
BecomeFollower = 11,
/**
* The difference of committed log index between the follower and the
* master became smaller than a user-specified threshold.
* Happens on follower only.
* ctx: null.
*/
BecomeFresh = 12,
/**
* The difference of committed log index between the follower and the
* master became larger than a user-specified threshold.
* Happens on follwer only.
* ctx: null
*/
BecomeStale = 13,
/**
* Got append entry request from leader.
* It will be invoked only for acceptable logs.
* ctx: pointer to request.
*/
GotAppendEntryReqFromLeader = 14,
/**
* This node is out of log range, which means that
* leader has no valid log or snapshot to send for this node.
* ctx: pointer to `OutOfLogRangeWarningArgs`.
*/
OutOfLogRangeWarning = 15,
/**
* New connection is established.
* Mostly this event happens in below cases:
* 1) Leader sends message to follower, then follower will fire
* this event.
* 2) Candidate sends vote request to peer, then the peer (receiver)
* will fire this event.
* ctx: pointer to `ConnectionArgs`.
*/
ConnectionOpened = 16,
/**
* Connection is closed.
* ctx: pointer to `ConnectionArgs`.
*/
ConnectionClosed = 17,
/**
* Invoked when a session receives a message from the valid leader
* first time. This callback is preceded by `ConnectionOpened`
* event.
* ctx: pointer to `ConnectionArgs`.
*/
NewSessionFromLeader = 18,
/**
* Executed a log in the state machine.
* ctx: pointer to the log index.
*/
StateMachineExecution = 19,
/**
* Just sent an append entries request.
* ctx: pointer to `req_msg` instance.
*/
SentAppendEntriesReq = 20,
/**
* Just received an append entries request.
* ctx: pointer to `req_msg` instance.
*/
ReceivedAppendEntriesReq = 21,
/**
* Just sent an append entries response.
* ctx: pointer to `resp_msg` instance.
*/
SentAppendEntriesResp = 22,
/**
* Just received an append entries response.
* ctx: pointer to `resp_msg` instance.
*/
ReceivedAppendEntriesResp = 23,
/**
* When cluster size is 2 and `auto_adjust_quorum_for_small_cluster_` is on,
* this server attempts to adjust the quorum size to 1.
* ctx: null
*/
AutoAdjustQuorum = 24,
/**
* Adding a server failed due to RPC errors and timeout expiry.
* ctx: null
*/
ServerJoinFailed = 25,
/**
* Snapshot creation begins.
* ctx: pointer to `uint64_t` (committed_idx).
*/
SnapshotCreationBegin = 26,
/**
* Got a resgination request either automatically or manually.
* ctx: null.
*/
ResignationFromLeader = 27,
};
struct Param {
Param(int32_t my_id = -1,
int32_t leader_id = -1,
int32_t peer_id = -1,
void* _ctx = nullptr)
: myId(my_id)
, leaderId(leader_id)
, peerId(peer_id)
, ctx(_ctx)
{}
int32_t myId;
int32_t leaderId;
int32_t peerId;
void* ctx;
};
enum ReturnCode {
Ok = 0,
ReturnNull = -1,
};
struct OutOfLogRangeWarningArgs {
OutOfLogRangeWarningArgs(uint64_t x = 0) : startIdxOfLeader(x) {}
uint64_t startIdxOfLeader;
};
struct ConnectionArgs {
ConnectionArgs(uint64_t id = 0,
const std::string& addr = std::string(),
uint32_t p = 0,
int32_t srv_id = -1,
bool is_leader = false)
: sessionId(id), address(addr), port(p)
, srvId(srv_id), isLeader(is_leader) {}
/**
* ID of session.
*/
uint64_t sessionId;
/**
* Endpoint address.
*/
std::string address;
/**
* Endpoint port.
*/
uint32_t port;
/**
* Endpoint server ID if given.
*/
int32_t srvId;
/**
* `true` if the endpoint server is leader.
*/
bool isLeader;
};
using func_type = std::function<ReturnCode(Type, Param*)>;
cb_func() : func(nullptr) {}
cb_func(func_type _func) : func(_func) {}
ReturnCode call(Type type, Param* param) {
if (func) {
return func(type, param);
}
return Ok;
}
private:
func_type func;
};
}
#endif //_CALLBACK_H_