forked from tarantool/tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft.c
2454 lines (2124 loc) · 62.9 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
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 2010-2020, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "raft_test_utils.h"
/**
* Test result is a real returned value of main_f. Fiber_join can not be used,
* because it expects if a returned value < 0 then diag is not empty. But in
* unit tests it can be violated - check_plan() does not set diag.
*/
static int test_result;
static void
raft_test_leader_election(void)
{
raft_start_test(24);
struct raft_node node;
raft_node_create(&node);
is(node.net.count, 1, "1 pending message at start");
ok(node.update_count > 0, "trigger worked");
node.update_count = 0;
ok(raft_node_net_check_msg(&node,
0 /* Index. */,
RAFT_STATE_FOLLOWER /* State. */,
1 /* Term. */,
0 /* Vote. */,
NULL /* Vclock. */
), "broadcast at start");
raft_node_net_drop(&node);
double death_timeout = node.cfg_death_timeout;
raft_run_next_event();
ok(raft_time() >= death_timeout, "next event is leader death");
/* Elections are started with a new term, which is persisted. */
ok(raft_node_check_full_state(&node,
RAFT_STATE_CANDIDATE /* State. */,
0 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
2 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "elections with a new term");
is(raft_vote_count(&node.raft), 1, "single vote for self");
ok(node.update_count > 0, "trigger worked");
node.update_count = 0;
/* Check if all async work is done properly. */
is(node.journal.size, 1, "1 record in the journal");
ok(raft_node_journal_check_row(
&node,
0 /* Index. */,
2 /* Term. */,
1 /* Vote. */
), "term and vote are on disk");
is(node.net.count, 1, "1 pending message");
ok(raft_node_net_check_msg(&node,
0 /* Index. */,
RAFT_STATE_CANDIDATE /* State. */,
2 /* Term. */,
1 /* Vote. */,
"{0: 1}" /* Vclock. */
), "term bump and vote are sent");
raft_node_net_drop(&node);
/* Simulate first response. Nothing should happen, quorum is 3. */
is(raft_node_send_vote_response(&node,
2 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "vote response from 2");
is(raft_vote_count(&node.raft), 2, "2 votes - 1 self and 1 foreign");
ok(!node.has_work, "no work to do - not enough votes yet");
raft_run_for(node.cfg_election_timeout / 2);
is(node.raft.state, RAFT_STATE_CANDIDATE, "still candidate, waiting "
"for elections");
is(node.update_count, 0, "trigger is the same");
/* Simulate second response. Quorum is reached. */
is(raft_node_send_vote_response(&node,
2 /* Term. */,
1 /* Vote. */,
3 /* Source. */
), 0, "vote response from 3");
is(raft_vote_count(&node.raft), 3, "2 votes - 1 self and 2 foreign");
is(node.raft.state, RAFT_STATE_LEADER, "became leader");
ok(node.update_count > 0, "trigger worked");
node.update_count = 0;
/* New leader should do a broadcast when elected. */
ok(!node.has_work, "no work - broadcast should be done");
is(node.journal.size, 1, "no new rows in the journal - state change "
"is not persisted");
is(node.net.count, 1, "1 pending message");
ok(raft_node_net_check_msg(&node,
0 /* Index. */,
RAFT_STATE_LEADER /* State. */,
2 /* Term. */,
1 /* Vote. */,
NULL /* Vclock. */
), "sent new-leader notification");
raft_node_net_drop(&node);
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_recovery(void)
{
raft_start_test(13);
struct raft_msg msg;
struct raft_node node;
raft_node_create(&node);
raft_run_next_event();
is(node.raft.state, RAFT_STATE_CANDIDATE, "became candidate");
/* Candidate's checkpoint. */
raft_checkpoint_remote(&node.raft, &msg);
ok(raft_msg_check(&msg,
RAFT_STATE_CANDIDATE /* State. */,
2 /* Term. */,
1 /* Vote. */,
"{0: 1}" /* Vclock. */
), "remote checkpoint of a candidate");
raft_checkpoint_local(&node.raft, &msg);
/* State and vclock are not persisted in a local checkpoint. */
ok(raft_msg_check(&msg,
0 /* State. */,
2 /* Term. */,
1 /* Vote. */,
NULL /* Vclock. */
), "local checkpoint of a candidate");
is(raft_node_send_vote_response(&node,
2 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "vote response from 2");
is(raft_node_send_vote_response(&node,
2 /* Term. */,
1 /* Vote. */,
3 /* Source. */
), 0, "vote response from 3");
is(node.raft.state, RAFT_STATE_LEADER, "became leader");
ok(raft_node_check_full_state(&node,
RAFT_STATE_LEADER /* State. */,
1 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
2 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "election is finished");
/* Leader's checkpoint. */
raft_checkpoint_remote(&node.raft, &msg);
/* Leader does not send vclock. */
ok(raft_msg_check(&msg,
RAFT_STATE_LEADER /* State. */,
2 /* Term. */,
1 /* Vote. */,
NULL /* Vclock. */
), "remote checkpoint of a leader");
raft_checkpoint_local(&node.raft, &msg);
/* State and vclock are not persisted in a local checkpoint. */
ok(raft_msg_check(&msg,
0 /* State. */,
2 /* Term. */,
1 /* Vote. */,
NULL /* Vclock. */
), "local checkpoint of a leader");
/* Restart leads to state loss. Look at follower's checkpoint. */
raft_node_restart(&node);
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
2 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "restart always as a follower");
is(raft_vote_count(&node.raft), 1, "vote count is restored correctly");
raft_checkpoint_remote(&node.raft, &msg);
ok(raft_msg_check(&msg,
RAFT_STATE_FOLLOWER /* State. */,
2 /* Term. */,
1 /* Vote. */,
NULL /* Vclock. */
), "remote checkpoint of a leader");
raft_checkpoint_local(&node.raft, &msg);
ok(raft_msg_check(&msg,
0 /* State. */,
2 /* Term. */,
1 /* Vote. */,
NULL /* Vclock. */
), "local checkpoint of a leader");
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_bad_msg(void)
{
raft_start_test(11);
struct raft_msg msg;
struct raft_node node;
struct vclock vclock;
raft_node_create(&node);
msg = (struct raft_msg){
.state = 0,
.term = 10,
};
is(raft_node_process_msg(&node, &msg, 2), -1, "state can't be 0");
is(node.raft.term, 1, "term from the bad message wasn't used");
raft_vclock_from_string(&vclock, "{2: 1}");
msg = (struct raft_msg){
.state = RAFT_STATE_CANDIDATE,
.term = 10,
.vote = 3,
.vclock = &vclock,
};
is(raft_node_process_msg(&node, &msg, 2), -1, "node can't be a "
"candidate but vote for another node");
is(node.raft.term, 1, "term from the bad message wasn't used");
msg = (struct raft_msg){
.state = RAFT_STATE_CANDIDATE,
.term = 10,
.vote = 2,
};
is(raft_node_process_msg(&node, &msg, 2), -1, "node can't be a "
"candidate without vclock");
is(node.raft.term, 1, "term from the bad message wasn't used");
msg = (struct raft_msg){
.state = RAFT_STATE_FOLLOWER,
.term = 0,
};
is(raft_node_process_msg(&node, &msg, 2), -1, "term can't be 0");
msg = (struct raft_msg){
.state = 10000,
.term = 10,
.vote = 2,
};
is(raft_node_process_msg(&node, &msg, 2), -1, "bad state");
is(node.raft.term, 1, "term from the bad message wasn't used");
msg = (struct raft_msg){
.state = -1,
.term = 10,
.vote = 2,
};
is(raft_node_process_msg(&node, &msg, 2), -1, "bad negative state");
is(node.raft.term, 1, "term from the bad message wasn't used");
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_vote(void)
{
raft_start_test(6);
struct raft_node node;
raft_node_create(&node);
/* Vote for other node. */
is(raft_node_send_vote_request(&node,
2 /* Term. */,
"{}" /* Vclock. */,
2 /* Source. */
), 0, "vote request from 2");
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
2 /* Term. */,
2 /* Vote. */,
2 /* Volatile term. */,
2 /* Volatile vote. */,
"{0: 2}" /* Vclock. */
), "voted for 2");
is(raft_node_send_vote_request(&node,
2 /* Term. */,
"{}" /* Vclock. */,
3 /* Source. */
), 0, "vote request from 3");
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
2 /* Term. */,
2 /* Vote. */,
2 /* Volatile term. */,
2 /* Volatile vote. */,
"{0: 2}" /* Vclock. */
), "still kept vote for 2");
/* If the candidate didn't become a leader, start own election. */
double ts = raft_time();
raft_run_next_event();
ok(raft_time() - ts >= node.cfg_election_timeout, "election timeout "
"passed");
ok(raft_node_check_full_state(&node,
RAFT_STATE_CANDIDATE /* State. */,
0 /* Leader. */,
3 /* Term. */,
1 /* Vote. */,
3 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 3}" /* Vclock. */
), "became candidate");
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_vote_skip(void)
{
raft_start_test(39);
struct raft_node node;
raft_node_create(&node);
/* Everything is skipped if the term is outdated. */
raft_run_next_event();
is(node.raft.state, RAFT_STATE_CANDIDATE, "became candidate");
is(node.raft.term, 2, "term is bumped");
is(raft_node_send_vote_response(&node,
1 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "message is accepted");
is(raft_vote_count(&node.raft), 1, "but ignored - too old term");
/* Competing vote requests are skipped. */
is(raft_node_send_vote_response(&node,
2 /* Term. */,
3 /* Vote. */,
2 /* Source. */
), 0, "message is accepted");
is(raft_vote_count(&node.raft), 1,
"but ignored - vote not for this node");
is(node.raft.state, RAFT_STATE_CANDIDATE, "this node does not give up");
/* Vote requests are ignored when node is disabled. */
raft_node_cfg_is_enabled(&node, false);
is(raft_node_send_follower(&node,
3 /* Term. */,
2 /* Source. */
), 0, "message is accepted");
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
3 /* Term. */,
0 /* Vote. */,
3 /* Volatile term. */,
0 /* Volatile vote. */,
"{0: 2}" /* Vclock. */
), "term bump to be able to vote again");
is(raft_node_send_vote_request(&node,
3 /* Term. */,
"{}" /* Vclock. */,
2 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 0, "but ignored - node is disabled");
/* Disabled node still takes term from the vote request. */
is(raft_node_send_vote_request(&node,
4 /* Term. */,
"{}" /* Vclock. */,
2 /* Source. */
), 0, "message is accepted");
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
4 /* Term. */,
0 /* Vote. */,
4 /* Volatile term. */,
0 /* Volatile vote. */,
"{0: 3}" /* Vclock. */
), "term is bumped, but vote request is ignored");
raft_node_cfg_is_enabled(&node, true);
/* Not a candidate won't accept vote request for self. */
is(raft_node_send_vote_response(&node,
4 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 0, "but ignored - vote works only on a candidate");
/* Ignore vote response for some third node. */
is(raft_node_send_vote_response(&node,
4 /* Term. */,
3 /* Vote. */,
2 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 0, "but ignored - sender != vote, so it is not a "
"request");
/* Ignore if leader is already known. */
is(raft_node_send_leader(&node,
4 /* Term. */,
2 /* Source. */
), 0, "message is accepted");
is(node.raft.leader, 2, "leader is accepted");
is(raft_node_send_vote_request(&node,
4 /* Term. */,
"{}" /* Vclock. */,
3 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 0, "but ignored - leader is already known");
is(node.raft.leader, 2, "leader is not changed");
/* Ignore too small vclock. */
/*
* Need to turn off the candidate role to bump the term and not become
* a candidate.
*/
raft_node_cfg_is_candidate(&node, false);
raft_node_journal_follow(&node, 1, 5);
raft_node_journal_follow(&node, 2, 5);
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
2 /* Leader. */,
4 /* Term. */,
0 /* Vote. */,
4 /* Volatile term. */,
0 /* Volatile vote. */,
"{0: 3, 1: 5, 2: 5}" /* Vclock. */
), "vclock is bumped");
is(raft_node_send_vote_request(&node,
5 /* Term. */,
"{1: 4}" /* Vclock. */,
3 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 0, "but ignored - vclock is too small");
is(node.raft.term, 5, "new term");
is(node.raft.leader, 0, "leader is dropped in the new term");
/* Ignore incomparable vclock. */
is(raft_node_send_vote_request(&node,
5 /* Term. */,
"{1: 4, 2: 6}" /* Vclock. */,
3 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 0, "but ignored - vclock is incomparable");
/* Ignore if voted in the current term. */
is(raft_node_send_vote_request(&node,
6 /* Term. */,
"{1: 5, 2: 5}" /* Vclock. */,
2 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 2, "voted");
is(raft_node_send_vote_request(&node,
6 /* Term. */,
"{1: 5, 2: 5}" /* Vclock. */,
3 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 2, "but ignored - already voted in the term");
/* After restart it still will ignore requests in the current term. */
raft_node_restart(&node);
is(raft_node_send_vote_request(&node,
6 /* Term. */,
"{1: 5, 2: 5}" /* Vclock. */,
3 /* Source. */
), 0, "message is accepted");
is(node.raft.vote, 2, "but ignored - already voted in the term");
raft_node_cfg_is_candidate(&node, true);
/*
* Vote response with a bigger term must be skipped, but it will bump
* the term.
*/
/* Re-create the node so as not to write the vclock each time. */
raft_node_destroy(&node);
raft_node_create(&node);
/*
* Set quorum to 2 to ensure the node does not count the bigger-term
* vote and doesn't become a leader.
*/
raft_node_cfg_election_quorum(&node, 2);
raft_run_next_event();
is(node.raft.state, RAFT_STATE_CANDIDATE, "became candidate");
is(node.raft.term, 2, "term is bumped");
is(raft_node_send_vote_response(&node,
3 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "message is accepted");
ok(raft_node_check_full_state(&node,
RAFT_STATE_CANDIDATE /* State. */,
0 /* Leader. */,
3 /* Term. */,
1 /* Vote. */,
3 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 3}" /* Vclock. */
), "term is bumped and became candidate");
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_vote_during_wal_write(void)
{
raft_start_test(11);
struct raft_node node;
/*
* Vote request from another node causes WAL flush before the current
* node can make a vote decision.
*/
raft_node_create(&node);
/*
* Server1 wins elections in the current term.
*/
raft_run_next_event();
is(node.raft.state, RAFT_STATE_CANDIDATE, "became candidate");
is(raft_node_send_vote_response(
&node,
2 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "vote response from 2");
is(raft_node_send_vote_response(
&node,
2 /* Term. */,
1 /* Vote. */,
3 /* Source. */
), 0, "vote response from 3");
raft_node_journal_follow(&node, 1, 3);
raft_node_journal_follow(&node, 2, 5);
ok(raft_node_check_full_state(
&node,
RAFT_STATE_LEADER /* State. */,
1 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
2 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1, 1: 3, 2: 5}" /* Vclock. */
), "became leader");
/*
* Server1 WAL is blocked and it gets a vote request with a matching
* vclock.
*/
raft_node_block(&node);
is(raft_node_send_vote_request(
&node,
3 /* Term. */,
"{1: 3, 2: 5}" /* Vclock. */,
2 /* Source. */
), 0, "vote request in a new term but WAL is blocked");
/*
* A WAL write ends, which was started before the vote request arrived.
*/
raft_node_journal_follow(&node, 1, 1);
raft_node_unblock(&node);
/*
* Server1 rejects the vote request then, because its own vclock became
* bigger after the WAL sync. Instead, it voted for self.
*/
ok(raft_node_check_full_state(
&node,
RAFT_STATE_CANDIDATE /* State. */,
0 /* Leader. */,
3 /* Term. */,
1 /* Vote. */,
3 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 3, 1: 4, 2: 5}" /* Vclock. */
), "canceled the vote for other node and voted for self");
raft_node_destroy(&node);
raft_node_create(&node);
/*
* Vote request for self works always even if there were some pending
* rows in the WAL queue when the vote was issued.
*/
raft_run_next_event();
is(node.raft.state, RAFT_STATE_CANDIDATE, "became candidate");
is(node.raft.term, 2, "term is 2");
raft_node_block(&node);
/* Start new term on election timeout, but can't persist anything. */
raft_run_next_event();
is(node.raft.term, 2, "term is 2");
is(node.raft.volatile_term, 3, "volatile term is 3");
/* WAL queue is flushed and there was some data before the vote. */
raft_node_journal_follow(&node, 1, 10);
raft_node_unblock(&node);
ok(raft_node_check_full_state(
&node,
RAFT_STATE_CANDIDATE /* State. */,
0 /* Leader. */,
3 /* Term. */,
1 /* Vote. */,
3 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 2, 1: 10}" /* Vclock. */
), "vote for self worked even though the WAL had non-empty queue");
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_leader_resign(void)
{
raft_start_test(24);
struct raft_node node;
/*
* When a node resignes from leader role voluntarily, the other nodes
* will start next election.
*/
raft_node_create(&node);
is(raft_node_send_leader(&node, 1, 2), 0, "message is accepted");
is(node.raft.leader, 2, "leader is elected");
is(raft_node_send_follower(&node, 1, 2), 0, "message is accepted");
is(node.raft.leader, 0, "leader has resigned");
ok(raft_node_check_full_state(&node,
RAFT_STATE_CANDIDATE /* State. */,
0 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
2 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "became candidate");
raft_node_destroy(&node);
/* Resign does not do anything if the node is not a candidate. */
raft_node_create(&node);
is(raft_node_send_leader(&node, 1, 2), 0, "message is accepted");
is(node.raft.leader, 2, "leader is elected");
raft_node_cfg_is_candidate(&node, false);
/* Multiple candidate reset won't break anything. */
raft_node_cfg_is_candidate(&node, false);
int update_count = node.update_count;
is(raft_node_send_follower(&node, 1, 2), 0, "message is accepted");
is(node.raft.leader, 0, "leader has resigned");
is(node.update_count, update_count + 1, "resign makes a broadcast");
raft_run_for(node.cfg_death_timeout * 2);
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
1 /* Term. */,
0 /* Vote. */,
1 /* Volatile term. */,
0 /* Volatile vote. */,
"{}" /* Vclock. */
), "still follower");
raft_node_destroy(&node);
/* Resign by refusing to be a candidate. */
raft_node_create(&node);
raft_run_next_event();
is(raft_node_send_vote_response(&node,
2 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "vote from 2");
is(raft_node_send_vote_response(&node,
2 /* Term. */,
1 /* Vote. */,
3 /* Source. */
), 0, "vote from 3");
ok(raft_node_check_full_state(&node,
RAFT_STATE_LEADER /* State. */,
1 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
2 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "became leader");
raft_node_net_drop(&node);
raft_node_cfg_is_candidate(&node, false);
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
2 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "the leader has resigned");
ok(raft_node_net_check_msg(&node,
0 /* Index. */,
RAFT_STATE_FOLLOWER /* State. */,
2 /* Term. */,
1 /* Vote. */,
NULL /* Vclock. */
), "resign notification is sent");
/*
* gh-6129: resign of a remote leader during a local WAL write should
* schedule a new election after the WAL write.
*
* Firstly start a new term.
*/
raft_node_block(&node);
raft_node_cfg_is_candidate(&node, true);
raft_run_next_event();
/* Volatile term is new, but the persistent one is not updated yet. */
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
3 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "new election is waiting for WAL write");
/* Now another node wins the election earlier. */
is(raft_node_send_leader(&node,
3 /* Term. */,
2 /* Source. */
), 0, "message is accepted");
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
2 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
3 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "the leader is accepted");
/*
* The leader resigns and triggers a new election round on the first
* node. A new election is triggered, but still waiting for the previous
* WAL write to end.
*/
is(raft_node_send_follower(&node,
3 /* Term. */,
2 /* Source. */
), 0, "message is accepted");
/* Note how the volatile term is updated again. */
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
0 /* Leader. */,
2 /* Term. */,
1 /* Vote. */,
4 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "the leader has resigned, new election is scheduled");
raft_node_unblock(&node);
/* Ensure the node still collects votes after the WAL write. */
is(raft_node_send_vote_response(&node,
4 /* Term. */,
1 /* Vote. */,
2 /* Source. */
), 0, "vote from 2");
is(raft_node_send_vote_response(&node,
4 /* Term. */,
1 /* Vote. */,
3 /* Source. */
), 0, "vote from 3");
raft_run_next_event();
ok(raft_node_check_full_state(&node,
RAFT_STATE_LEADER /* State. */,
1 /* Leader. */,
4 /* Term. */,
1 /* Vote. */,
4 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 2}" /* Vclock. */
), "the leader is elected");
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_split_brain(void)
{
raft_start_test(4);
struct raft_node node;
raft_node_create(&node);
/*
* Split brain is ignored, as there is nothing to do with it
* automatically.
*/
is(raft_node_send_leader(&node,
2 /* Term. */,
2 /* Source. */
), 0, "first leader notification");
is(node.raft.leader, 2, "leader is found");
is(raft_node_send_leader(&node,
2 /* Term. */,
3 /* Source. */
), 0, "second leader notification");
is(node.raft.leader, 2, "split brain, the old leader is kept");
raft_node_destroy(&node);
raft_finish_test();
}
static void
raft_test_heartbeat(void)
{
raft_start_test(12);
struct raft_node node;
raft_node_create(&node);
/* Let the node know there is a leader somewhere. */
is(raft_node_send_leader(&node,
2 /* Term. */,
2 /* Source. */
), 0, "leader notification");
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
2 /* Leader. */,
2 /* Term. */,
0 /* Vote. */,
2 /* Volatile term. */,
0 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "follow the leader after notification");
/* Leader can send the same message many times. */
is(raft_node_send_leader(&node,
2 /* Term. */,
2 /* Source. */
), 0, "leader notification");
/* The node won't do anything if it is not a candidate. */
raft_node_cfg_is_candidate(&node, false);
raft_run_for(node.cfg_death_timeout * 2);
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
2 /* Leader. */,
2 /* Term. */,
0 /* Vote. */,
2 /* Volatile term. */,
0 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "follow the leader because no candidate");
raft_node_cfg_is_candidate(&node, true);
/* Heartbeats from the leader are accepted. */
for (int i = 0; i < 5; ++i) {
raft_run_for(node.cfg_death_timeout / 2);
raft_node_send_heartbeat(&node, 2);
}
ok(raft_node_check_full_state(&node,
RAFT_STATE_FOLLOWER /* State. */,
2 /* Leader. */,
2 /* Term. */,
0 /* Vote. */,
2 /* Volatile term. */,
0 /* Volatile vote. */,
"{0: 1}" /* Vclock. */
), "follow the leader because had heartbeats");
/* Heartbeats not from the leader won't do anything. */
double start = raft_time();
raft_run_for(node.cfg_death_timeout / 3);
raft_node_send_heartbeat(&node, 3);
raft_run_for(node.cfg_death_timeout / 3);
raft_node_send_heartbeat(&node, 0);
raft_run_next_event();
double deadline = start + node.cfg_death_timeout;
/*
* Compare == with 0.1 precision. Because '/ 3' operations above will
* make the doubles contain some small garbage.
*/
ok(raft_time() + 0.1 >= deadline && raft_time() - 0.1 <= deadline,
"death timeout passed");
ok(raft_node_check_full_state(&node,
RAFT_STATE_CANDIDATE /* State. */,
0 /* Leader. */,
3 /* Term. */,
1 /* Vote. */,
3 /* Volatile term. */,
1 /* Volatile vote. */,
"{0: 2}" /* Vclock. */
), "enter candidate state when no heartbeats from the leader");
/* Non-candidate ignores heartbeats. */
raft_node_cfg_is_candidate(&node, false);
raft_node_send_heartbeat(&node, 2);
raft_node_cfg_is_candidate(&node, true);
/* Leader ignores all heartbeats - nothing to wait for. */