This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathraft.c
305 lines (257 loc) · 7.29 KB
/
raft.c
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
#include "../include/raft.h"
#include <string.h>
#include "assert.h"
#include "byte.h"
#include "callbacks.h"
#include "configuration.h"
#include "convert.h"
#include "election.h"
#include "err.h"
#include "flags.h"
#include "heap.h"
#include "log.h"
#include "membership.h"
#include "tracing.h"
#define DEFAULT_ELECTION_TIMEOUT 1000 /* One second */
#define DEFAULT_HEARTBEAT_TIMEOUT 100 /* One tenth of a second */
#define DEFAULT_INSTALL_SNAPSHOT_TIMEOUT 30000 /* 30 seconds */
#define DEFAULT_SNAPSHOT_THRESHOLD 1024
#define DEFAULT_SNAPSHOT_TRAILING 2048
/* Number of milliseconds after which a server promotion will be aborted if the
* server hasn't caught up with the logs yet. */
#define DEFAULT_MAX_CATCH_UP_ROUNDS 10
#define DEFAULT_MAX_CATCH_UP_ROUND_DURATION (5 * 1000)
#define tracef(...) Tracef(r->tracer, __VA_ARGS__)
int raft_version_number(void)
{
return RAFT_VERSION_NUMBER;
}
static int ioFsmVersionCheck(struct raft *r,
struct raft_io *io,
struct raft_fsm *fsm);
int raft_init(struct raft *r,
struct raft_io *io,
struct raft_fsm *fsm,
const raft_id id,
const char *address)
{
int rv;
assert(r != NULL);
rv = ioFsmVersionCheck(r, io, fsm);
if (rv != 0) {
goto err;
}
r->io = io;
r->io->data = r;
r->fsm = fsm;
r->tracer = &StderrTracer;
raft_tracer_maybe_enable(r->tracer, true);
r->id = id;
/* Make a copy of the address */
r->address = RaftHeapMalloc(strlen(address) + 1);
if (r->address == NULL) {
rv = RAFT_NOMEM;
goto err;
}
strcpy(r->address, address);
r->current_term = 0;
r->voted_for = 0;
r->log = logInit();
if (r->log == NULL) {
rv = RAFT_NOMEM;
goto err_after_address_alloc;
}
raft_configuration_init(&r->configuration);
raft_configuration_init(&r->configuration_last_snapshot);
r->configuration_committed_index = 0;
r->configuration_uncommitted_index = 0;
r->election_timeout = DEFAULT_ELECTION_TIMEOUT;
r->heartbeat_timeout = DEFAULT_HEARTBEAT_TIMEOUT;
r->install_snapshot_timeout = DEFAULT_INSTALL_SNAPSHOT_TIMEOUT;
r->commit_index = 0;
r->last_applied = 0;
r->last_stored = 0;
r->state = RAFT_UNAVAILABLE;
r->leader_state.voter_contacts = 0;
rv = raftInitCallbacks(r);
if (rv != 0) {
goto err_after_address_alloc;
}
r->transfer = NULL;
r->snapshot.pending.term = 0;
r->snapshot.threshold = DEFAULT_SNAPSHOT_THRESHOLD;
r->snapshot.trailing = DEFAULT_SNAPSHOT_TRAILING;
r->snapshot.put.data = NULL;
r->close_cb = NULL;
memset(r->errmsg, 0, sizeof r->errmsg);
r->pre_vote = false;
r->max_catch_up_rounds = DEFAULT_MAX_CATCH_UP_ROUNDS;
r->max_catch_up_round_duration = DEFAULT_MAX_CATCH_UP_ROUND_DURATION;
rv = r->io->init(r->io, r->id, r->address);
if (rv != 0) {
ErrMsgTransfer(r->io->errmsg, r->errmsg, "io");
goto err_after_callbacks_alloc;
}
return 0;
err_after_callbacks_alloc:
raftDestroyCallbacks(r);
err_after_address_alloc:
RaftHeapFree(r->address);
err:
assert(rv != 0);
return rv;
}
static void ioCloseCb(struct raft_io *io)
{
struct raft *r = io->data;
tracef("io close cb");
raftDestroyCallbacks(r);
raft_free(r->address);
logClose(r->log);
raft_configuration_close(&r->configuration);
raft_configuration_close(&r->configuration_last_snapshot);
if (r->close_cb != NULL) {
r->close_cb(r);
}
}
void raft_close(struct raft *r, void (*cb)(struct raft *r))
{
assert(r->close_cb == NULL);
if (r->state != RAFT_UNAVAILABLE) {
convertToUnavailable(r);
}
r->close_cb = cb;
r->io->close(r->io, ioCloseCb);
}
void raft_register_state_cb(struct raft *r, raft_state_cb cb)
{
struct raft_callbacks *cbs = raftGetCallbacks(r);
assert(cbs != NULL);
cbs->state_cb = cb;
}
void raft_set_election_timeout(struct raft *r, const unsigned msecs)
{
r->election_timeout = msecs;
}
void raft_set_heartbeat_timeout(struct raft *r, const unsigned msecs)
{
r->heartbeat_timeout = msecs;
}
void raft_set_install_snapshot_timeout(struct raft *r, const unsigned msecs)
{
r->install_snapshot_timeout = msecs;
}
void raft_set_snapshot_threshold(struct raft *r, unsigned n)
{
r->snapshot.threshold = n;
}
void raft_set_snapshot_trailing(struct raft *r, unsigned n)
{
r->snapshot.trailing = n;
}
void raft_set_max_catch_up_rounds(struct raft *r, unsigned n)
{
r->max_catch_up_rounds = n;
}
void raft_set_max_catch_up_round_duration(struct raft *r, unsigned msecs)
{
r->max_catch_up_round_duration = msecs;
}
void raft_set_pre_vote(struct raft *r, bool enabled)
{
r->pre_vote = enabled;
}
const char *raft_errmsg(struct raft *r)
{
return r->errmsg;
}
int raft_voter_contacts(struct raft *r)
{
if (r->state == RAFT_LEADER) {
return (int)r->leader_state.voter_contacts;
} else {
return -1;
}
}
int raft_bootstrap(struct raft *r, const struct raft_configuration *conf)
{
int rv;
if (r->state != RAFT_UNAVAILABLE) {
return RAFT_BUSY;
}
rv = r->io->bootstrap(r->io, conf);
if (rv != 0) {
return rv;
}
return 0;
}
int raft_recover(struct raft *r, const struct raft_configuration *conf)
{
int rv;
if (r->state != RAFT_UNAVAILABLE) {
return RAFT_BUSY;
}
rv = r->io->recover(r->io, conf);
if (rv != 0) {
return rv;
}
return 0;
}
const char *raft_strerror(int errnum)
{
return errCodeToString(errnum);
}
void raft_configuration_init(struct raft_configuration *c)
{
configurationInit(c);
}
void raft_configuration_close(struct raft_configuration *c)
{
configurationClose(c);
}
int raft_configuration_add(struct raft_configuration *c,
const raft_id id,
const char *address,
const int role)
{
return configurationAdd(c, id, address, role);
}
int raft_configuration_encode(const struct raft_configuration *c,
struct raft_buffer *buf)
{
return configurationEncode(c, buf);
}
unsigned long long raft_digest(const char *text, unsigned long long n)
{
struct byteSha1 sha1;
uint8_t value[20];
uint64_t n64 = byteFlip64((uint64_t)n);
uint64_t digest;
byteSha1Init(&sha1);
byteSha1Update(&sha1, (const uint8_t *)text, (uint32_t)strlen(text));
byteSha1Update(&sha1, (const uint8_t *)&n64, (uint32_t)(sizeof n64));
byteSha1Digest(&sha1, value);
memcpy(&digest, value + (sizeof value - sizeof digest), sizeof digest);
return byteFlip64(digest);
}
static int ioFsmVersionCheck(struct raft *r,
struct raft_io *io,
struct raft_fsm *fsm)
{
if (io->version == 0) {
ErrMsgPrintf(r->errmsg, "io->version must be set");
return -1;
}
if (fsm->version == 0) {
ErrMsgPrintf(r->errmsg, "fsm->version must be set");
return -1;
}
if ((fsm->version > 2 && fsm->snapshot_async != NULL) &&
((io->version < 2) || (io->async_work == NULL))) {
ErrMsgPrintf(
r->errmsg,
"async snapshot requires io->version > 1 and async_work method.");
return -1;
}
return 0;
}