-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft.h
734 lines (594 loc) · 24.6 KB
/
raft.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
#ifndef raft_h
#define raft_h
#include <algorithm>
#include <atomic>
#include <chrono>
#include <ctime>
#include <mutex>
#include <random>
#include <stdarg.h>
#include <thread>
#include "raft_protocol.h"
#include "raft_state_machine.h"
#include "raft_storage.h"
#include "rpc.h"
using std::chrono::system_clock;
template <typename state_machine, typename command> class raft {
static_assert(std::is_base_of<raft_state_machine, state_machine>(),
"state_machine must inherit from raft_state_machine");
static_assert(std::is_base_of<raft_command, command>(), "command must inherit from raft_command");
friend class thread_pool;
#define RAFT_LOG(fmt, args...) \
do { \
auto now = \
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()) \
.count(); \
printf("[%ld][%s:%d][node %d term %d] " fmt "\n", now, __FILE__, __LINE__, my_id, current_term, ##args); \
} while (0);
public:
raft(rpcs *rpc_server, std::vector<rpcc *> rpc_clients, int idx, raft_storage<command> *storage,
state_machine *state);
~raft();
// start the raft node.
// Please make sure all of the rpc request handlers have been registered before this method.
void start();
// stop the raft node.
// Please make sure all of the background threads are joined in this method.
// Notice: you should check whether is server should be stopped by calling is_stopped().
// Once it returns true, you should break all of your long-running loops in the background threads.
void stop();
// send a new command to the raft nodes.
// This method returns true if this raft node is the leader that successfully appends the log.
// If this node is not the leader, returns false.
bool new_command(command cmd, int &term, int &index);
// returns whether this node is the leader, you should also set the current term;
bool is_leader(int &term);
// save a snapshot of the state machine and compact the log.
bool save_snapshot();
private:
std::mutex mtx; // A big lock to protect the whole data structure
ThrPool *thread_pool;
raft_storage<command> *storage; // To persist the raft log
state_machine *state; // The state machine that applies the raft log, e.g. a kv store
rpcs *rpc_server; // RPC server to recieve and handle the RPC requests
std::vector<rpcc *> rpc_clients; // RPC clients of all raft nodes including this node
int my_id; // The index of this node in rpc_clients, start from 0
std::atomic_bool stopped;
enum raft_role { follower, candidate, leader };
raft_role role; // volatile
int current_term; // persist
std::thread *background_election;
std::thread *background_ping;
std::thread *background_commit;
std::thread *background_apply;
// persist states
int vote_for;
std::vector<log_entry<command>> log;
std::vector<char> snapshot;
// volatile states
int commitIndex;
int lastApplied;
// candidate volatile states
int vote_count;
std::vector<bool> votedNodes;
// leader volatile states
std::vector<int> nextIndex;
std::vector<int> matchIndex;
std::vector<int> matchCount;
// times
system_clock::time_point last_time;
system_clock::duration follower_timeout;
system_clock::duration candidate_timeout;
private:
// RPC handlers
int request_vote(request_vote_args arg, request_vote_reply &reply);
int append_entries(append_entries_args<command> arg, append_entries_reply &reply);
int install_snapshot(install_snapshot_args arg, install_snapshot_reply &reply);
// RPC helpers
void send_request_vote(int target, request_vote_args arg);
void handle_request_vote_reply(int target, const request_vote_args &arg, const request_vote_reply &reply);
void send_append_entries(int target, append_entries_args<command> arg);
void handle_append_entries_reply(int target, const append_entries_args<command> &arg,
const append_entries_reply &reply);
void send_install_snapshot(int target, install_snapshot_args arg);
void handle_install_snapshot_reply(int target, const install_snapshot_args &arg,
const install_snapshot_reply &reply);
private:
bool is_stopped();
int num_nodes() { return rpc_clients.size(); }
// background workers
void run_background_ping();
void run_background_election();
void run_background_commit();
void run_background_apply();
// Your code here:
void init_time();
inline int get_term(int index);
std::vector<log_entry<command>> get_entries(int begin_index, int end_index);
bool judge_append(int pre_log_index, int pre_log_term);
bool judge_snapshot(int last_included_index, int last_included_term);
bool judge_vote(int last_log_index, int last_log_term);
void set_role_and_term(raft_role role, int term);
void init_state();
};
template <typename state_machine, typename command>
raft<state_machine, command>::raft(rpcs *server, std::vector<rpcc *> clients, int idx, raft_storage<command> *storage,
state_machine *state)
: storage(storage), state(state), rpc_server(server), rpc_clients(clients), my_id(idx), stopped(false),
role(follower), current_term(0), background_election(nullptr), background_ping(nullptr),
background_commit(nullptr), background_apply(nullptr) {
thread_pool = new ThrPool(32);
// recover from storage
if (!storage->restore(current_term, vote_for, log, snapshot)) {
init_state();
snapshot.clear();
storage->updateTotal(current_term, vote_for, log, snapshot);
}
if (!snapshot.empty()) {
state->apply_snapshot(snapshot);
}
// volatile states
commitIndex = log.front().index;
lastApplied = log.front().index;
// candidate volatile states
vote_count = 0;
votedNodes.assign(num_nodes(), false);
// leader volatile states
nextIndex.assign(num_nodes(), 1);
matchIndex.assign(num_nodes(), 0);
matchCount.clear();
// initialize times
last_time = std::chrono::system_clock::now();
init_time();
// Register the rpcs.
rpc_server->reg(raft_rpc_opcodes::op_request_vote, this, &raft::request_vote);
rpc_server->reg(raft_rpc_opcodes::op_append_entries, this, &raft::append_entries);
rpc_server->reg(raft_rpc_opcodes::op_install_snapshot, this, &raft::install_snapshot);
}
template <typename state_machine, typename command> raft<state_machine, command>::~raft() {
if (background_ping) {
delete background_ping;
}
if (background_election) {
delete background_election;
}
if (background_commit) {
delete background_commit;
}
if (background_apply) {
delete background_apply;
}
delete thread_pool;
}
/******************************************************************
Public Interfaces
*******************************************************************/
template <typename state_machine, typename command> void raft<state_machine, command>::stop() {
RAFT_LOG("stop");
stopped.store(true);
background_ping->join();
background_election->join();
background_commit->join();
background_apply->join();
thread_pool->destroy();
}
template <typename state_machine, typename command> bool raft<state_machine, command>::is_stopped() {
return stopped.load();
}
template <typename state_machine, typename command> bool raft<state_machine, command>::is_leader(int &term) {
term = current_term;
return role == leader;
}
template <typename state_machine, typename command> void raft<state_machine, command>::start() {
RAFT_LOG("start");
this->background_election = new std::thread(&raft::run_background_election, this);
this->background_ping = new std::thread(&raft::run_background_ping, this);
this->background_commit = new std::thread(&raft::run_background_commit, this);
this->background_apply = new std::thread(&raft::run_background_apply, this);
}
template <typename state_machine, typename command>
bool raft<state_machine, command>::new_command(command cmd, int &term, int &index) {
std::unique_lock<std::mutex> lock(mtx);
if (role != leader) {
return false;
}
term = current_term;
index = log.back().index + 1;
log_entry<command> entry(index, term, cmd);
log.push_back(entry);
nextIndex[my_id] = index + 1;
matchIndex[my_id] = index;
matchCount.push_back(1);
if (!storage->appendLog(entry, log.size())) {
storage->updateLog(log);
}
return true;
}
template <typename state_machine, typename command> bool raft<state_machine, command>::save_snapshot() {
std::unique_lock<std::mutex> lock(mtx);
snapshot = state->snapshot();
if (lastApplied <= log.back().index) {
log.erase(log.begin(), log.begin() + lastApplied - log.front().index);
} else {
log.clear();
}
storage->updateSnapshot(snapshot);
storage->updateLog(log);
return true;
}
/******************************************************************
RPC Related
*******************************************************************/
template <typename state_machine, typename command>
int raft<state_machine, command>::request_vote(request_vote_args arg, request_vote_reply &reply) {
std::unique_lock<std::mutex> lock(mtx);
last_time = std::chrono::system_clock::now();
reply.term = current_term;
reply.voteGranted = false;
if (arg.term < current_term) {
return 0;
}
if (arg.term > current_term) {
set_role_and_term(follower, arg.term);
}
if (vote_for == -1 || vote_for == arg.candidateId) {
if (judge_vote(arg.lastLogIndex, arg.lastLogTerm)) {
vote_for = arg.candidateId;
reply.voteGranted = true;
storage->updateMetadata(current_term, vote_for);
}
}
return 0;
}
template <typename state_machine, typename command>
void raft<state_machine, command>::handle_request_vote_reply(int target, const request_vote_args &arg,
const request_vote_reply &reply) {
std::unique_lock<std::mutex> lock(mtx);
if (reply.term > current_term) {
set_role_and_term(follower, reply.term);
return;
}
if (role != candidate) {
return;
}
if (reply.voteGranted && !votedNodes[target]) {
votedNodes[target] = true;
++vote_count;
if (vote_count > num_nodes() / 2) {
set_role_and_term(leader, current_term);
}
}
return;
}
template <typename state_machine, typename command>
int raft<state_machine, command>::append_entries(append_entries_args<command> arg, append_entries_reply &reply) {
std::unique_lock<std::mutex> lock(mtx);
last_time = system_clock::now();
reply.term = current_term;
reply.success = false;
if (arg.term < current_term) {
return 0;
}
if (arg.term > current_term || role == candidate) {
set_role_and_term(follower, arg.term);
}
if (arg.prevLogIndex <= log.back().index && arg.prevLogTerm == get_term(arg.prevLogIndex)) {
reply.success = true;
if (!arg.entries.empty()) {
if (arg.prevLogIndex < log.back().index) {
if (arg.prevLogIndex + 1 <= log.back().index) {
log.erase(log.begin() + arg.prevLogIndex + 1 - log.front().index, log.end());
}
log.insert(log.end(), arg.entries.begin(), arg.entries.end());
storage->updateLog(log);
} else {
log.insert(log.end(), arg.entries.begin(), arg.entries.end());
if (!storage->appendLog(arg.entries, log.size())) {
storage->updateLog(log);
}
}
}
if (arg.leaderCommit > commitIndex) {
commitIndex = std::min(arg.leaderCommit, log.back().index);
}
}
return 0;
}
template <typename state_machine, typename command>
void raft<state_machine, command>::handle_append_entries_reply(int target, const append_entries_args<command> &arg,
const append_entries_reply &reply) {
std::unique_lock<std::mutex> lock(mtx);
if (reply.term > current_term) {
set_role_and_term(follower, reply.term);
return;
}
if (role != leader) {
return;
}
if (reply.success) {
int prev = matchIndex[target];
matchIndex[target] = std::max(matchIndex[target], (int)(arg.prevLogIndex + arg.entries.size()));
nextIndex[target] = matchIndex[target] + 1;
int last = std::max(prev - commitIndex, 0) - 1;
for (int i = matchIndex[target] - commitIndex - 1; i > last; --i) {
++matchCount[i];
if (matchCount[i] > num_nodes() / 2 && get_term(commitIndex + i + 1) == current_term) {
commitIndex += i + 1;
matchCount.erase(matchCount.begin(), matchCount.begin() + i + 1);
break;
}
}
} else {
nextIndex[target] = std::min(nextIndex[target], arg.prevLogIndex);
}
return;
}
template <typename state_machine, typename command>
int raft<state_machine, command>::install_snapshot(install_snapshot_args arg, install_snapshot_reply &reply) {
std::unique_lock<std::mutex> lock(mtx);
last_time = system_clock::now();
reply.term = current_term;
if (arg.term < current_term) {
return 0;
}
if (arg.term > current_term || role == candidate) {
set_role_and_term(follower, arg.term);
}
if (arg.lastIncludedIndex <= log.back().index && arg.lastIncludedTerm == get_term(arg.lastIncludedIndex)) {
int end_index = arg.lastIncludedIndex;
if (end_index <= log.back().index) {
log.erase(log.begin(), log.begin() + end_index - log.front().index);
} else {
log.clear();
}
} else {
log.assign(1, log_entry<command>(arg.lastIncludedIndex, arg.lastIncludedTerm));
}
snapshot = arg.snapshot;
state->apply_snapshot(snapshot);
lastApplied = arg.lastIncludedIndex;
commitIndex = std::max(commitIndex, arg.lastIncludedIndex);
storage->updateLog(log);
storage->updateSnapshot(arg.snapshot);
return 0;
}
template <typename state_machine, typename command>
void raft<state_machine, command>::handle_install_snapshot_reply(int target, const install_snapshot_args &arg,
const install_snapshot_reply &reply) {
std::unique_lock<std::mutex> lock(mtx);
if (role != leader) {
return;
}
if (reply.term > current_term) {
set_role_and_term(follower, reply.term);
return;
}
matchIndex[target] = std::max(matchIndex[target], arg.lastIncludedIndex);
nextIndex[target] = matchIndex[target] + 1;
return;
}
template <typename state_machine, typename command>
void raft<state_machine, command>::send_request_vote(int target, request_vote_args arg) {
request_vote_reply reply;
if (rpc_clients[target]->call(raft_rpc_opcodes::op_request_vote, arg, reply) == 0) {
handle_request_vote_reply(target, arg, reply);
} else{
}
}
template <typename state_machine, typename command>
void raft<state_machine, command>::send_append_entries(int target, append_entries_args<command> arg) {
append_entries_reply reply;
if (rpc_clients[target]->call(raft_rpc_opcodes::op_append_entries, arg, reply) == 0) {
handle_append_entries_reply(target, arg, reply);
} else{
}
}
template <typename state_machine, typename command>
void raft<state_machine, command>::send_install_snapshot(int target, install_snapshot_args arg) {
install_snapshot_reply reply;
if (rpc_clients[target]->call(raft_rpc_opcodes::op_install_snapshot, arg, reply) == 0) {
handle_install_snapshot_reply(target, arg, reply);
} else{
}
}
/******************************************************************
Background Workers
*******************************************************************/
template <typename state_machine, typename command> void raft<state_machine, command>::run_background_election() {
std::unique_lock<std::mutex> lock(mtx, std::defer_lock);
system_clock::time_point current_time;
while (true) {
if (is_stopped()){
return;
}
lock.lock();
current_time = system_clock::now();
if(role == follower) {
if (current_time - last_time > follower_timeout) {
set_role_and_term(candidate, current_term + 1);
// initial request arguments
request_vote_args args{};
args.term = current_term;
args.candidateId = my_id;
args.lastLogIndex = log.back().index;
args.lastLogTerm = log.back().term;
// send vote request to others
for (int i = 0; i < num_nodes(); ++i) {
if (i == my_id)
continue;
thread_pool->addObjJob(this, &raft::send_request_vote, i, args);
}
// update election timer
last_time = system_clock::now();
}
} else if(role == candidate){
if (current_time - last_time > candidate_timeout) {
set_role_and_term(follower, current_term);
}
}
// do nothing for leader
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return;
}
template <typename state_machine, typename command> void raft<state_machine, command>::run_background_commit() {
// Send logs/snapshots to the follower.
// Only work for the leader.
std::unique_lock<std::mutex> lock(mtx, std::defer_lock);
while (true) {
if (is_stopped()){
return;
}
lock.lock();
if (role == leader) {
int last_log_index = this->log.back().index;
for (int i = 0; i < num_nodes(); ++i) {
if (i == my_id)
continue;
if (nextIndex[i] <= last_log_index) {
if (nextIndex[i] > log.front().index) {
int pre_log_index = nextIndex[i] - 1;
append_entries_args<command> args(current_term, my_id, commitIndex, pre_log_index, get_term(pre_log_index),get_entries(nextIndex[i], last_log_index + 1));
thread_pool->addObjJob(this, &raft::send_append_entries, i, args);
} else {
install_snapshot_args args(current_term, my_id, log.front().index, log.front().term, snapshot);
thread_pool->addObjJob(this, &raft::send_install_snapshot, i, args);
}
}
}
}
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(12));
}
return;
}
template <typename state_machine, typename command> void raft<state_machine, command>::run_background_apply() {
// Apply committed logs the state machine
// Work for all the nodes.
std::unique_lock<std::mutex> lock(mtx, std::defer_lock);
std::vector<log_entry<command>> entries;
while (true) {
if (is_stopped())
return;
lock.lock();
if (commitIndex > lastApplied) {
entries = get_entries(lastApplied + 1, commitIndex + 1);
for (log_entry<command> &entry : entries) {
state->apply_log(entry.cmd);
}
lastApplied = commitIndex;
}
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(11));
}
return;
}
template <typename state_machine, typename command> void raft<state_machine, command>::run_background_ping() {
// Send empty append_entries RPC to the followers.
// Only work for the leader.
std::unique_lock<std::mutex> lock(mtx, std::defer_lock);
while (true) {
if (is_stopped())
return;
lock.lock();
if (role == leader) {
static append_entries_args<command> args{};
args.term = current_term;
args.leaderId = my_id;
args.leaderCommit = commitIndex;
for (int i = 0; i < num_nodes(); ++i) {
if (i == my_id)
continue;
args.prevLogIndex = nextIndex[i] - 1;
args.prevLogTerm = get_term(args.prevLogIndex);
thread_pool->addObjJob(this, &raft::send_append_entries, i, args);
}
}
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(150)); // Adjust param: ping period
}
return;
}
/******************************************************************
Other functions
*******************************************************************/
template <typename state_machine, typename command> void raft<state_machine, command>::init_time() {
static std::random_device rd;
static std::minstd_rand gen(rd());
static std::uniform_int_distribution<int> follower_dis(300, 500); // Adjust param
static std::uniform_int_distribution<int> candidate_dis(800, 1000); // Adjust param
follower_timeout = std::chrono::duration_cast<system_clock::duration>(std::chrono::milliseconds(follower_dis(gen)));
candidate_timeout = std::chrono::duration_cast<system_clock::duration>(std::chrono::milliseconds(candidate_dis(gen)));
}
template <typename state_machine, typename command> inline int raft<state_machine, command>::get_term(int index) {
return log[index - log.front().index].term;
}
template <typename state_machine, typename command>
inline std::vector<log_entry<command>> raft<state_machine, command>::get_entries(int begin_index, int end_index) {
std::vector<log_entry<command>> ret;
if (begin_index < end_index) {
ret.assign(log.begin() + begin_index - log.front().index, log.begin() + end_index - log.front().index);
}
return ret;
}
template <typename state_machine, typename command>
bool raft<state_machine, command>::judge_vote(int last_log_index, int last_log_term){
return last_log_term > log.back().term || last_log_index >= log.back().index;
}
template <typename state_machine, typename command>
bool raft<state_machine, command>::judge_append(int pre_log_index, int pre_log_term){
if(pre_log_index > log.back().index || pre_log_term != get_term(pre_log_index)){
return false;
}
return true;
}
template <typename state_machine, typename command>
bool raft<state_machine, command>::judge_snapshot(int last_included_index, int last_included_term){
if(last_included_index > log.back().index || last_included_term != get_term(last_included_index)){
return false;
}
return true;
}
template <typename state_machine, typename command>
void raft<state_machine, command>::set_role_and_term(raft_role role, int term){
this->role = role;
current_term = term;
switch (role) {
case leader:{
// reinitialize leader volatile states
nextIndex.assign(num_nodes(), log.back().index + 1);
matchIndex.assign(num_nodes(), 0);
matchIndex[my_id] = log.back().index;
matchCount.assign(log.back().index - commitIndex, 0);
break;
}
case follower:{
current_term = term;
vote_for = -1;
storage->updateMetadata(current_term, vote_for);
// re-randomize timeouts
init_time();
break;
}
case candidate:{
// increment current term
current_term = term;
vote_for = my_id;
vote_count = 1;
votedNodes.assign(num_nodes(), false);
votedNodes[my_id] = true;
// do persist
storage->updateMetadata(current_term, vote_for);
// re-randomize timeouts
init_time();
}
}
}
template <typename state_machine, typename command>
void raft<state_machine, command>::init_state(){
current_term = 0;
vote_for = -1;
log.assign(1, log_entry<command>(0, 0));
}
#endif // raft_h