-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathra_server_SUITE.erl
2957 lines (2653 loc) · 130 KB
/
ra_server_SUITE.erl
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
%% This Source Code Form is subject to the terms of the Mozilla Public
%%
%% Copyright (c) 2017-2023 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries.
%%
-module(ra_server_SUITE).
-compile(nowarn_export_all).
-compile(export_all).
-include("src/ra.hrl").
-include("src/ra_server.hrl").
-include_lib("eunit/include/eunit.hrl").
all() ->
[
init_test,
recover_restores_cluster_changes,
election_timeout,
follower_aer_term_mismatch,
follower_aer_term_mismatch_snapshot,
follower_handles_append_entries_rpc,
candidate_handles_append_entries_rpc,
append_entries_reply_success_promotes_nonvoter,
append_entries_reply_success,
append_entries_reply_no_success,
append_entries_reply_no_success_from_unknown_peer,
follower_request_vote,
follower_pre_vote,
pre_vote_receives_pre_vote,
await_condition_receives_pre_vote,
request_vote_rpc_with_lower_term,
leader_does_not_abdicate_to_unknown_peer,
higher_term_detected,
pre_vote_election,
pre_vote_election_reverts,
candidate_receives_pre_vote,
leader_receives_pre_vote,
candidate_election,
is_new,
command,
command_notify,
leader_enters_from_await_condition,
leader_noop_operation_enables_cluster_change,
leader_noop_increments_machine_version,
follower_machine_version,
follower_install_snapshot_machine_version,
leader_server_join,
leader_server_join_nonvoter,
leader_server_leave,
leader_is_removed,
follower_cluster_change,
leader_applies_new_cluster,
leader_applies_new_cluster_nonvoter,
leader_appends_cluster_change_then_steps_before_applying_it,
leader_receives_install_snapshot_rpc,
follower_installs_snapshot,
follower_ignores_installs_snapshot_with_higher_machine_version,
follower_receives_stale_snapshot,
receive_snapshot_timeout,
snapshotted_follower_received_append_entries,
leader_received_append_entries_reply_with_stale_last_index,
leader_receives_install_snapshot_result,
leader_received_append_entries_reply_and_promotes_voter,
leader_replies_to_append_entries_rpc_with_lower_term,
follower_aer_1,
follower_aer_2,
follower_aer_3,
follower_aer_4,
follower_aer_5,
follower_catchup_condition,
wal_down_condition_follower,
wal_down_condition_leader,
wal_down_condition_leader_commands,
update_release_cursor,
follower_heartbeat,
follower_heartbeat_reply,
candidate_heartbeat,
candidate_heartbeat_reply,
pre_vote_heartbeat,
pre_vote_heartbeat_reply,
leader_heartbeat,
leader_heartbeat_reply_same_term,
leader_heartbeat_reply_node_size_5,
leader_heartbeat_reply_lower_term,
leader_heartbeat_reply_higher_term,
leader_consistent_query_delay,
leader_consistent_query,
await_condition_heartbeat_dropped,
await_condition_heartbeat_reply_dropped,
receive_snapshot_heartbeat_dropped,
receive_snapshot_heartbeat_reply_dropped,
handle_down
].
-define(MACFUN, fun (E, _) -> E end).
-define(N1, {n1, node()}).
-define(N2, {n2, node()}).
-define(N3, {n3, node()}).
-define(N4, {n4, node()}).
-define(N5, {n5, node()}).
groups() ->
[ {tests, [], all()} ].
init_per_suite(Config) ->
ok = logger:set_primary_config(level, all),
Config.
end_per_suite(Config) ->
Config.
init_per_testcase(TestCase, Config) ->
ok = logger:set_primary_config(level, all),
ok = setup_log(),
[{test_case, TestCase} | Config].
end_per_testcase(_TestCase, Config) ->
meck:unload(),
Config.
id(X) -> X.
ra_server_init(Conf) ->
ra_server:recover(ra_server:init(Conf)).
setup_log() ->
ok = meck:new(ra_log, []),
ok = meck:new(ra_snapshot, [passthrough]),
ok = meck:new(ra_machine, [passthrough]),
meck:expect(ra_log, init, fun(C) -> ra_log_memory:init(C) end),
meck:expect(ra_log_meta, store, fun (_, U, K, V) ->
put({U, K}, V), ok
end),
meck:expect(ra_log_meta, store_sync, fun (_, U, K, V) ->
put({U, K}, V), ok
end),
meck:expect(ra_log_meta, fetch, fun(_, U, K) ->
get({U, K})
end),
meck:expect(ra_log_meta, fetch, fun (_, U, K, D) ->
ra_lib:default(get({U, K}), D)
end),
meck:expect(ra_snapshot, begin_accept,
fun(_Meta, SS) ->
{ok, SS}
end),
meck:expect(ra_snapshot, accept_chunk,
fun(_Data, _OutOf, _Flag, SS) ->
{ok, SS}
end),
meck:expect(ra_snapshot, abort_accept, fun(SS) -> SS end),
meck:expect(ra_snapshot, accepting, fun(_SS) -> undefined end),
meck:expect(ra_log, snapshot_state, fun (_) -> snap_state end),
meck:expect(ra_log, set_snapshot_state, fun (_, Log) -> Log end),
meck:expect(ra_log, install_snapshot, fun (_, _, Log) -> {Log, []} end),
meck:expect(ra_log, recover_snapshot, fun ra_log_memory:recover_snapshot/1),
meck:expect(ra_log, snapshot_index_term, fun ra_log_memory:snapshot_index_term/1),
meck:expect(ra_log, fold, fun ra_log_memory:fold/5),
meck:expect(ra_log, release_resources, fun ra_log_memory:release_resources/3),
meck:expect(ra_log, append_sync,
fun({Idx, Term, _} = E, L) ->
L1 = ra_log_memory:append(E, L),
{LX, _} = ra_log_memory:handle_event({written, {Idx, Idx, Term}}, L1),
LX
end),
meck:expect(ra_log, write_config, fun ra_log_memory:write_config/2),
meck:expect(ra_log, next_index, fun ra_log_memory:next_index/1),
meck:expect(ra_log, append, fun ra_log_memory:append/2),
meck:expect(ra_log, write, fun ra_log_memory:write/2),
meck:expect(ra_log, handle_event, fun ra_log_memory:handle_event/2),
meck:expect(ra_log, last_written, fun ra_log_memory:last_written/1),
meck:expect(ra_log, last_index_term, fun ra_log_memory:last_index_term/1),
meck:expect(ra_log, set_last_index, fun ra_log_memory:set_last_index/2),
meck:expect(ra_log, fetch_term, fun ra_log_memory:fetch_term/2),
meck:expect(ra_log, needs_cache_flush, fun (_) -> false end),
meck:expect(ra_log, exists,
fun ({Idx, Term}, L) ->
case ra_log_memory:fetch_term(Idx, L) of
{Term, Log} -> {true, Log};
{_, Log} -> {false, Log}
end
end),
meck:expect(ra_log, update_release_cursor,
fun ra_log_memory:update_release_cursor/5),
ok.
init_test(_Config) ->
#{cluster := Cluster,
current_term := CurrentTerm,
log := Log0} = Base = base_state(3, ?FUNCTION_NAME),
Id = ra_server:id(Base),
UId = ra_server:uid(Base),
% ensure it is written to the log
InitConf = #{cluster_name => init,
id => Id,
uid => UId,
log_init_args => #{uid => <<>>},
machine => {module, ?FUNCTION_NAME, #{}},
initial_members => []}, % init without known peers
% new
#{current_term := 0,
voted_for := undefined} = ra_server_init(InitConf),
% previous data
ok = ra_log_meta:store(ra_log_meta, UId, voted_for, some_server),
ok = ra_log_meta:store(ra_log_meta, UId, current_term, CurrentTerm),
meck:expect(ra_log, init, fun (_) -> Log0 end),
#{current_term := 5,
voted_for := some_server} = ra_server_init(InitConf),
% snapshot
SnapshotMeta = #{index => 3, term => 5,
cluster => dehydrate_cluster(Cluster),
machine_version => 1},
SnapshotData = "hi1+2+3",
{LogS, _, _} = ra_log_memory:install_snapshot(SnapshotMeta, SnapshotData, Log0),
meck:expect(ra_log, init, fun (_) -> LogS end),
#{current_term := 5,
commit_index := 3,
machine_state := "hi1+2+3",
cluster := ClusterOut,
voted_for := some_server} = ra_server_init(InitConf),
?assertEqual(dehydrate_cluster(Cluster), dehydrate_cluster(ClusterOut)),
ok.
recover_restores_cluster_changes(_Config) ->
N1 = ?N1, N2 = ?N2,
InitConf = #{cluster_name => ?FUNCTION_NAME,
id => N1,
uid => <<"n1">>,
log_init_args => #{uid => <<>>},
machine => {simple, fun erlang:'+'/2, 0},
initial_members => []}, % init without known peers
% new
{leader, State00, _} =
ra_server:handle_candidate(#request_vote_result{term = 1,
vote_granted = true},
(ra_server_init(InitConf))#{votes => 0,
current_term => 1,
voted_for => N1}),
{leader, State0 = #{cluster := Cluster0,
current_term := 1}, _} =
ra_server:handle_leader({command, {noop, meta(), 0}}, State00),
?assert(maps:size(Cluster0) =:= 1),
{leader, State, _} = ra_server:handle_leader(written_evt({1, 1, 1}), State0),
?assertMatch(#{current_term := 1}, State),
?assertMatch(#{cluster_change_permitted := true}, State),
% n2 joins
{leader, #{cluster := Cluster,
log := Log0}, _} =
ra_server:handle_leader({command, {'$ra_join', meta(),
N2, await_consensus}}, State),
{LIdx, _} = ra_log:last_index_term(Log0),
?assertEqual(2, maps:size(Cluster)),
% intercept ra_log:init call to simulate persisted log data
% ok = meck:new(ra_log, [passthrough]),
meck:expect(ra_log, init, fun (_) -> Log0 end),
meck:expect(ra_log_meta, fetch,
fun (_, _, last_applied, 0) ->
LIdx - 1;
% element(1, ra_log:last_index_term(Log0));
(_, _, _, Def) ->
Def
end),
#{cluster := #{?N1 := _, N2 := _}} = ra_server_init(InitConf),
ok.
election_timeout(_Config) ->
N1 = ?N1, N2 = ?N2, N3 = ?N3,
State = base_state(3, ?FUNCTION_NAME),
Msg = election_timeout,
% follower
{pre_vote, #{current_term := 5, votes := 0,
pre_vote_token := Token},
[{next_event, cast, #pre_vote_result{term = 5, token = Token,
vote_granted = true}},
{send_vote_requests,
[{N2, #pre_vote_rpc{term = 5, token = Token,
last_log_index = 3,
last_log_term = 5,
candidate_id = N1}},
{N3, _}]}]} =
ra_server:handle_follower(Msg, State),
% non-voters ignore election_timeout
NVState = State#{membership => promotable},
{follower, NVState, []} = ra_server:handle_follower(Msg, NVState),
{await_condition, NVState, []} = ra_server:handle_await_condition(Msg, NVState),
% pre_vote
{pre_vote, #{current_term := 5, votes := 0,
pre_vote_token := Token1},
[{next_event, cast, #pre_vote_result{term = 5, token = Token1,
vote_granted = true}},
{send_vote_requests,
[{N2, #pre_vote_rpc{term = 5, token = Token1,
last_log_index = 3,
last_log_term = 5,
candidate_id = N1}},
{N3, _}]}]} =
ra_server:handle_pre_vote(Msg, State),
%% assert tokens are not the same
?assertNotEqual(Token, Token1),
% candidate
VoteRpc = #request_vote_rpc{term = 6, candidate_id = N1,
last_log_index = 3, last_log_term = 5},
VoteForSelfEvent = {next_event, cast,
#request_vote_result{term = 6, vote_granted = true}},
{candidate, #{current_term := 6, votes := 0},
[VoteForSelfEvent, {send_vote_requests, [{N2, VoteRpc}, {N3, VoteRpc}]}]} =
ra_server:handle_candidate(Msg, State),
ok.
follower_aer_1(_Config) ->
% Scenario 1
N1 = ?N1,
Self = N1,
% AER with index [1], commit = 0, commit_index = 0
Init = empty_state(3, n1),
AER1 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 0,
prev_log_term = 0, leader_commit = 0,
entries = [entry(1, 1, one)]},
{follower, State1 = #{leader_id := N1, current_term := 1,
commit_index := 0, last_applied := 0}, _} =
ra_server:handle_follower(AER1, Init),
% AER with index [2], leader_commit = 1, commit_index = 1
AER2 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 1,
prev_log_term = 1, leader_commit = 1,
entries = [entry(2, 1, two)]},
{follower, State2 = #{leader_id := N1, current_term := 1,
commit_index := 1, last_applied := 1,
%% validate entry was applied to machine
machine_state := {simple, _, one}},
_} = ra_server:handle_follower(AER2, State1),
% {written, 1} -> last_applied: 1 - replies with last_index = 1, next_index = 3
{follower, State3 = #{leader_id := N1, current_term := 1,
commit_index := 1, last_applied := 1,
machine_state := {simple, _, one}},
[{cast, N1, {Self, #append_entries_reply{next_index = 3,
last_term = 1,
last_index = 1}}}]}
= ra_server:handle_follower({ra_log_event, {written, {1, 1, 1}}}, State2),
% AER with index [3], commit = 3 -> commit_index = 3
AER3 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 2,
prev_log_term = 1, leader_commit = 3,
entries = [entry(3, 1, tre)]},
{follower, State4 = #{leader_id := N1, current_term := 1,
commit_index := 3, last_applied := 3,
machine_state := {simple, _, tre}},
_} = ra_server:handle_follower(AER3, State3),
% {written, 2} -> last_applied: 3, commit_index = 3 reply = 2, next_index = 4
{follower, State5 = #{leader_id := N1, current_term := 1,
commit_index := 3, last_applied := 3,
machine_state := {_, _, tre}},
[{cast, N1, {Self, #append_entries_reply{next_index = 4,
last_term = 1,
last_index = 2}}}]}
= ra_server:handle_follower({ra_log_event, {written, {2, 2, 1}}}, State4),
ok = logger:set_primary_config(level, all),
% AER with index [] -> last_applied: 2 - replies with last_index = 2,
% next_index = 4
% empty AER before {written, 3} is received
AER4 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 3,
prev_log_term = 1, leader_commit = 3,
entries = []},
{follower, State6 = #{leader_id := N1, current_term := 1,
commit_index := 3, last_applied := 3,
machine_state := {simple, _, tre}},
[{cast, N1, {Self, #append_entries_reply{next_index = 4,
last_term = 1,
last_index = 2}}} | _]}
= ra_server:handle_follower(AER4, State5),
% {written, 3} -> commit_index = 3, last_applied = 3 : reply last_index = 3
{follower, #{leader_id := N1, current_term := 1,
commit_index := 3, last_applied := 3,
machine_state := {_, _, tre}},
[{cast, N1, {Self, #append_entries_reply{next_index = 4,
last_term = 1,
last_index = 3}}}]}
= ra_server:handle_follower({ra_log_event, {written, {3, 3, 1}}}, State6),
ok.
follower_aer_2(_Config) ->
N1 = ?N1, N2 = ?N2,
% Scenario 2
% empty AER applies previously replicated entry
% AER with index [1], leader_commit = 0
Init = empty_state(3, n2),
AER1 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 0,
prev_log_term = 0, leader_commit = 0,
entries = [entry(1, 1, one)]},
{follower, State1 = #{leader_id := N1, current_term := 1,
commit_index := 0, last_applied := 0},
_} = ra_server:handle_follower(AER1, Init),
% {written, 1} -> last_applied: 0, reply: last_applied = 1, next_index = 2
{follower, State2 = #{leader_id := N1, current_term := 1,
commit_index := 0, last_applied := 0,
machine_state := {simple, _, <<>>}},
[{cast, N1, {N2, #append_entries_reply{next_index = 2,
last_term = 1,
last_index = 1}}}]}
= ra_server:handle_follower({ra_log_event, {written, {1, 1, 1}}}, State1),
% AER with index [], leader_commit = 1 -> last_applied: 1, reply: last_index = 1, next_index = 2
AER2 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 1,
prev_log_term = 1, leader_commit = 1,
entries = []},
{follower, #{leader_id := N1, current_term := 1,
commit_index := 1, last_applied := 1,
machine_state := {simple, _, one}},
_} = ra_server:handle_follower(AER2, State2),
ok.
follower_aer_3(_Config) ->
N1 = ?N1, N2 = ?N2,
% Scenario 3
% AER with index [1], commit_index = 1
Init = empty_state(3, n2),
AER1 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 0,
prev_log_term = 0, leader_commit = 1,
entries = [entry(1, 1, one)]},
{follower, State1 = #{leader_id := N1, current_term := 1,
commit_index := 1, last_applied := 1},
_} = ra_server:handle_follower(AER1, Init),
% {written, 1} -> last_applied: 1 - reply: last_index = 1, next_index = 2
{follower, State2 = #{leader_id := N1, current_term := 1,
commit_index := 1, last_applied := 1,
machine_state := {simple, _, one}},
[{cast, N1, {N2, #append_entries_reply{next_index = 2,
last_term = 1,
last_index = 1}}}]}
= ra_server:handle_follower({ra_log_event, {written, {1, 1, 1}}}, State1),
% AER with index [3] -> last_applied = 1 - reply(false):
% last_index, 1, next_index = 2
AER2 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 2,
prev_log_term = 1, leader_commit = 3,
entries = [entry(3, 1, tre)]},
{await_condition, State3 = #{leader_id := N1, current_term := 1,
commit_index := 3, last_applied := 1},
[{cast, N1, {N2, #append_entries_reply{next_index = 2,
success = false,
last_term = 1,
last_index = 1}}},
{record_leader_msg, N1}]}
= ra_server:handle_follower(AER2, State2),
% AER with index [2,3,4], commit_index = 3 -> commit_index = 3
AER3 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 1,
prev_log_term = 1, leader_commit = 3,
entries = [
entry(2, 1, two),
entry(3, 1, tre),
entry(4, 1, for)
]},
{follower, State4 = #{leader_id := N1, current_term := 1,
commit_index := 3, last_applied := 3}, _}
= ra_server:handle_follower(AER3, State3),
% {written, 4} -> last_applied: 3 - reply: last_index = 4, next_index = 5
{follower, State5 = #{leader_id := N1, current_term := 1,
commit_index := 3, last_applied := 3,
machine_state := {_, _, tre}},
[{cast, N1, {N2, #append_entries_reply{next_index = 5,
success = true,
last_term = 1,
last_index = 4}}} | _]}
= ra_server:handle_follower({ra_log_event, {written, {4, 4, 1}}}, State4),
% AER with index [2,3,4], commit_index = 4
% async failed AER reverted back leader's next_index for follower
% however an updated commit index is now known
AER4 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 1,
prev_log_term = 1, leader_commit = 4,
entries = [
entry(2, 1, two),
entry(3, 1, tre),
entry(4, 1, for)
]},
{follower, #{leader_id := N1, current_term := 1, commit_index := 4,
last_applied := 4, machine_state := {_, _, for}}, _}
= ra_server:handle_follower(AER4, State5),
% TODO: scenario where the batch is partially already seen and partiall not
% + increments commit_index
ok.
follower_aer_4(_Config) ->
N1 = ?N1, N2 = ?N2,
% Scenario 4 - commit index
% AER with index [1,2,3,4], commit_index = 10 -> commit_index = 4,
% last_applied = 4
% follower catching up scenario
Init = empty_state(3, n2),
AER1 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 0,
prev_log_term = 0, leader_commit = 10,
entries = [
entry(1, 1, one),
entry(2, 1, two),
entry(3, 1, tre),
entry(4, 1, for)
]},
{follower, State1 = #{leader_id := N1, current_term := 1,
commit_index := 10, last_applied := 4},
_} = ra_server:handle_follower(AER1, Init),
% {written, 4} -> last_applied = 4, commit_index = 4
{follower, _State2 = #{leader_id := N1, current_term := 1,
commit_index := 10, last_applied := 4,
machine_state := {_, _, for}},
[{cast, N1, {N2, #append_entries_reply{next_index = 5,
last_term = 1,
last_index = 4}}}]}
= ra_server:handle_follower({ra_log_event, {written, {4, 4, 1}}}, State1),
% AER with index [5], commit_index = 10 -> last_applied = 4, commit_index = 5
ok.
follower_aer_5(_Config) ->
N1 = ?N1, N5 = ?N5,
%% Scenario
%% Leader with smaller log is elected and sends empty aer
%% Follower should truncate it's log and reply with an appropriate
%% next index
Init = empty_state(3, n2),
AER1 = #append_entries_rpc{term = 1, leader_id = N1, prev_log_index = 0,
prev_log_term = 0, leader_commit = 10,
entries = [
entry(1, 1, one),
entry(2, 1, two),
entry(3, 1, tre),
entry(4, 1, for)
]},
%% set up follower state
{follower, State00, _} = ra_server:handle_follower(AER1, Init),
%% TODO also test when written even occurs after
{follower, State0, _} = ra_server:handle_follower(
{ra_log_event, {written, {4, 4, 1}}}, State00),
% now an AER from another leader in a higher term is received
% This is what the leader sends immediately before committing it;s noop
AER2 = #append_entries_rpc{term = 2, leader_id = N5, prev_log_index = 3,
prev_log_term = 1, leader_commit = 3,
entries = []},
{follower, _State1, Effects} = ra_server:handle_follower(AER2, State0),
{cast, N5, {_, M}} = hd(Effects),
?assertMatch(#append_entries_reply{next_index = 4,
last_term = 1,
last_index = 3}, M),
% ct:pal("Effects ~p~n State: ~p", [Effects, State1]),
ok.
follower_aer_term_mismatch(_Config) ->
State = (base_state(3, ?FUNCTION_NAME))#{last_applied => 2,
commit_index => 3},
AE = #append_entries_rpc{term = 6,
leader_id = ?N1,
prev_log_index = 3,
prev_log_term = 6, % higher log term
leader_commit = 3},
% term mismatch scenario follower has index 3 but for different term
% rewinds back to last_applied + 1 as next index and enters await condition
{await_condition, #{condition := _},
[{_, _, {_, Reply}} | _]} = ra_server:handle_follower(AE, State),
?assertMatch(#append_entries_reply{term = 6,
success = false,
next_index = 3,
last_index = 2,
last_term = 3}, Reply),
ok.
follower_aer_term_mismatch_snapshot(_Config) ->
%% case when we have to revert all the way back to a snapshot
State0 = (base_state(3, ?FUNCTION_NAME))#{last_applied => 3,
commit_index => 3
},
Log0 = maps:get(log, State0),
Meta = #{index => 3,
term => 5,
cluster => #{},
machine_version => 1},
Data = <<"hi3">>,
{Log, _, _} = ra_log_memory:install_snapshot(Meta, Data, Log0),
State = maps:put(log, Log, State0),
AE = #append_entries_rpc{term = 6,
leader_id = ?N1,
prev_log_index = 3,
prev_log_term = 6, % higher log term
leader_commit = 3},
% term mismatch scenario follower has index 3 but for different term
% rewinds back to last_applied + 1 as next index and enters await condition
{await_condition, #{condition := _},
[{_, _, {_, Reply}} | _]} = ra_server:handle_follower(AE, State),
?assertMatch(#append_entries_reply{term = 6,
success = false,
next_index = 4,
last_index = 3,
last_term = 5}, Reply),
ok.
follower_handles_append_entries_rpc(_Config) ->
N1 = ?N1,
State = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1},
EmptyAE = #append_entries_rpc{term = 5,
leader_id = ?N1,
prev_log_index = 3,
prev_log_term = 5,
leader_commit = 3},
% success case - everything is up to date leader id got updated
{follower, #{leader_id := N1, current_term := 5}, _} =
ra_server:handle_follower(EmptyAE, State),
% success case when leader term is higher
% reply term should be updated
% replies for empty rpcs are sent immediately
{follower, #{leader_id := N1, current_term := 6} = _State1,
[{cast, N1, {N1, #append_entries_reply{term = 6, success = true,
next_index = 4, last_index = 3,
last_term = 5}}} | _]}
= ra_server:handle_follower(EmptyAE#append_entries_rpc{term = 6}, State),
% reply false if term < current_term (5.1)
{follower, _, [{cast, N1, {N1, #append_entries_reply{term = 5, success = false}}}]}
= ra_server:handle_follower(EmptyAE#append_entries_rpc{term = 4}, State),
% reply false if log doesn't contain a term matching entry at prev_log_index
{await_condition, _,
[{cast, N1, {N1, #append_entries_reply{term = 5, success = false}}},
%% it was still a valid leader message only that we need to back up a bit
{record_leader_msg, _}]}
= ra_server:handle_follower(EmptyAE#append_entries_rpc{prev_log_index = 4},
State),
% there is an entry but not with a matching term
{await_condition, _, [{cast, N1, {N1, #append_entries_reply{term = 5, success = false}}},
{record_leader_msg, _}]}
= ra_server:handle_follower(EmptyAE#append_entries_rpc{prev_log_term = 4},
State),
% truncate/overwrite if a existing entry conflicts (diff term) with
% a new one (5.3)
AE = #append_entries_rpc{term = 5, leader_id = ?N1,
prev_log_index = 1, prev_log_term = 1,
leader_commit = 2,
entries = [{2, 4, {'$usr', meta(), <<"hi">>,
after_log_append}}]},
{follower, #{log := Log},
[{cast, N1, {N1, #append_entries_reply{term = 5, success = true,
next_index = 3, last_index = 2,
last_term = 4}}}]}
= begin
{follower, Inter3, _} =
ra_server:handle_follower(AE, State#{last_applied => 1}),
ra_server:handle_follower({ra_log_event, {written, {2, 2, 4}}}, Inter3)
end,
[{0, 0, undefined},
{1, 1, _}, {2, 4, _}] = ra_log_memory:to_list(Log),
% append new entries not in the log
% if leader_commit > the last entry received ensure last_applied does not
% match commit_index
% ExpectedLogEntry = usr(<<"hi4">>),
{follower, #{commit_index := 5, last_applied := 4,
machine_state := <<"hi4">>},
[{cast, N1, {N1, #append_entries_reply{term = 5, success = true,
last_index = 4,
last_term = 5}}}]}
= begin
{follower, Inter4, _}
= ra_server:handle_follower(
EmptyAE#append_entries_rpc{entries = [{4, 5, usr(<<"hi4">>)}],
leader_commit = 5},
State#{commit_index => 1, last_applied => 1,
machine_state => <<"hi1">>}),
ra_server:handle_follower(written_evt({4, 4, 5}), Inter4)
end,
ok.
follower_catchup_condition(_Config) ->
N1 = ?N1,
State0 = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1},
EmptyAE = #append_entries_rpc{term = 5,
leader_id = N1,
prev_log_index = 3,
prev_log_term = 5,
leader_commit = 3},
% from follower to await condition
{await_condition, State = #{condition := _}, _} =
ra_server:handle_follower(EmptyAE#append_entries_rpc{term = 5,
prev_log_index = 4},
State0),
% append entry with a lower leader term should not enter await condition
% even if prev_log_index is higher than last index
{follower, _, [_]}
= ra_server:handle_follower(EmptyAE#append_entries_rpc{term = 4,
prev_log_index = 4},
State),
% append entry when prev log index exists but the term is different should
% not also enter await condition as it then rewinds and request resends
% of all log entries since last known commit index
{await_condition, _, [_, _]}
= ra_server:handle_follower(EmptyAE#append_entries_rpc{term = 6,
prev_log_term = 4,
prev_log_index = 3},
State),
% append entry when term is ok but there is a gap should remain in await
% condition we do not want to send a reply here
{await_condition, _, []} =
ra_server:handle_await_condition(EmptyAE#append_entries_rpc{term = 5,
prev_log_index = 4},
State),
% success case - it transitions back to follower state
{follower, _, [{next_event, EmptyAE}]} =
ra_server:handle_await_condition(EmptyAE, State),
ISRpc = #install_snapshot_rpc{term = 99, leader_id = N1,
chunk_state = {1, last},
meta = #{index => 99,
term => 99,
cluster => #{},
machine_version => 0},
data = []},
{follower, _, [_NextEvent]} =
ra_server:handle_await_condition(ISRpc, State),
{await_condition, _, []} =
ra_server:handle_await_condition({ra_log_event, {written, {99, 99, 99}}},
State),
Msg = #request_vote_rpc{candidate_id = ?N2, term = 6, last_log_index = 3,
last_log_term = 5},
{follower, State, [{next_event, Msg}]} =
ra_server:handle_await_condition(Msg, State),
{follower, _, [{cast, N1, {N1, #append_entries_reply{success = false,
next_index = 4}}},
{record_leader_msg, _}]}
= ra_server:handle_await_condition(await_condition_timeout, State),
{pre_vote, _, _} = ra_server:handle_await_condition(election_timeout, State),
ok.
wal_down_condition_follower(_Config) ->
N1 = ?N1,
State0 = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1},
EmptyAE = #append_entries_rpc{term = 5,
leader_id = N1,
prev_log_index = 3,
prev_log_term = 5,
leader_commit = 3},
% meck:new(ra_log, [passthrough]),
meck:expect(ra_log, write, fun (_Es, _L) -> {error, wal_down} end),
meck:expect(ra_log, can_write, fun (_L) -> false end),
meck:expect(ra_log, reset_to_last_known_written, fun (L) -> L end),
% ra log fails
{await_condition, State1 = #{condition := _}, [{record_leader_msg, _}]}
= ra_server:handle_follower(EmptyAE#append_entries_rpc{entries = [{4, 5, yo}]},
State0),
% stay in await condition as ra_log_wal is not available
{await_condition, State2 = #{condition := _}, []}
= ra_server:handle_await_condition(
EmptyAE#append_entries_rpc{entries = [{4, 5, yo}]}, State1),
meck:expect(ra_log, can_write, fun (_L) -> true end),
% exit condition
{follower, State, [_]}
= ra_server:handle_await_condition(
EmptyAE#append_entries_rpc{entries = [{4, 5, yo}]}, State2),
?assertNot(maps:is_key(condition, State)),
ok.
wal_down_condition_leader(_Config) ->
%% the leader should enter a short await condition then if that times out
%% trigger a leader change. Only if running in clustered mode ofc.
State0 = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1},
Cmd = {'$usr', meta(), <<"hi4">>, after_log_append},
% meck:new(ra_log, [passthrough]),
meck:expect(ra_log, append, fun (_Es, _L) -> error(wal_down) end),
meck:expect(ra_log, can_write, fun (_L) -> false end),
meck:expect(ra_log, reset_to_last_known_written, fun (L) -> L end),
%% when the wal is down the leader should transition to await_condition,
%% on timeout a leader change effect should be emitted
%% such that it can concede leadership in the interest of Ra cluster
%% progress
{await_condition,
#{condition := #{predicate_fun := _,
transition_to := leader,
timeout :=
#{duration := 5000,
effects := [{next_event, cast,
{transfer_leadership, _}}],
transition_to := leader}}} = State1, _}
= ra_server:handle_leader({command, Cmd}, State0),
?assertEqual(5000, ra_server:get_condition_timeout(State1, 9999)),
% if awaiting for the condition times out, return to the leader and begin transferring leadership
% process
{leader, #{} = State2, [{next_event, cast,
{transfer_leadership, _}}]}
= ra_server:handle_await_condition(await_condition_timeout, State1),
%% but not if the condition no longer manifests
meck:expect(ra_log, append, fun (_Es, L) -> L end),
meck:expect(ra_log, can_write, fun (_L) -> true end),
{leader, #{} = State2, []}
= ra_server:handle_await_condition(await_condition_timeout, State1),
ok.
wal_down_condition_leader_commands(_Config) ->
%% the leader should enter a short await condition then if that times out
%% trigger a leader change. Only if running in clustered mode ofc.
State0 = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1},
Cmd = {'$usr', meta(), <<"hi4">>, after_log_append},
% meck:new(ra_log, [passthrough]),
meck:expect(ra_log, append, fun (_Es, _L) -> error(wal_down) end),
meck:expect(ra_log, can_write, fun (_L) -> false end),
meck:expect(ra_log, reset_to_last_known_written, fun (L) -> L end),
%% when the wal is down the leader should transition to awai_condition,
%% on timeout it should attempt a leader change effect should be emitted
%% such that it can concede leadership in the interest of Ra cluster
%% progress
{await_condition,
#{condition := #{predicate_fun := _,
transition_to := leader,
timeout := #{duration := 5000,
effects := [{next_event, cast,
{transfer_leadership, _}}],
transition_to := leader}}} = _State1, _}
= ra_server:handle_leader({commands, [Cmd]}, State0),
ok.
update_release_cursor(_Config) ->
State00 = #{cfg := Cfg} = (base_state(3, ?FUNCTION_NAME)),
meck:expect(?FUNCTION_NAME, version, fun () -> 1 end),
State0 = State00#{cfg => Cfg#cfg{machine_versions = [{1, 1}, {0, 0}]}},
%% need to match on something for this macro
?assertMatch({#{cfg := #cfg{machine_version = 0}}, []},
ra_server:update_release_cursor(2, some_state, State0)),
ok.
candidate_handles_append_entries_rpc(_Config) ->
N1 = ?N1,
State = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1},
EmptyAE = #append_entries_rpc{term = 4,
leader_id = ?N1,
prev_log_index = 3,
prev_log_term = 5,
leader_commit = 3},
% Lower term
{candidate, _,
[{cast, N1, {N1, #append_entries_reply{term = 5, success = false,
last_index = 3, last_term = 5}}}]}
= ra_server:handle_candidate(EmptyAE, State),
ok.
append_entries_reply_success_promotes_nonvoter(_Config) ->
N1 = ?N1, N2 = ?N2, N3 = ?N3,
NonVoter = #{membership => promotable, target => 3, uid => <<"uid">>},
Cluster = #{N1 => new_peer_with(#{next_index => 5, match_index => 4}),
N2 => new_peer_with(#{next_index => 1, match_index => 0,
commit_index_sent => 3,
voter_status => NonVoter}),
N3 => new_peer_with(#{next_index => 2, match_index => 1})},
State0 = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1,
last_applied => 1,
cluster => Cluster,
machine_state => <<"hi1">>},
Ack = #append_entries_reply{term = 5, success = true,
next_index = 4,
last_index = 3, last_term = 5},
% doesn't progress commit_index, non voter ack doesn't raise majority
{leader, #{cluster := #{N2 := #{next_index := 4,
match_index := 3,
voter_status := NonVoter}},
commit_index := 1,
last_applied := 1,
machine_state := <<"hi1">>} = State1,
[{next_event, info, pipeline_rpcs},
{next_event, {command, {'$ra_join', _,
#{id := N2,
voter_status := #{membership := voter,
uid := <<"uid">>}},
noreply}} = RaJoin}
]} = ra_server:handle_leader({N2, Ack}, State0),
% pipeline to N3
{leader, #{cluster := #{N3 := #{next_index := 4,
match_index := 1}},
commit_index := 1,
last_applied := 1,
machine_state := <<"hi1">>} = State2,
[{send_rpc, N3,
#append_entries_rpc{term = 5, leader_id = N1,
prev_log_index = 1,
prev_log_term = 1,
leader_commit = 1,
entries = [{2, 3, {'$usr', _, <<"hi2">>, _}},
{3, 5, {'$usr', _, <<"hi3">>, _}}]}
}]} = ra_server:handle_leader(pipeline_rpcs, State1),
% ra_join translates into cluster update
{leader, #{cluster := #{N2 := #{next_index := 5,
match_index := 3,
voter_status := #{membership := voter,
uid := <<"uid">>}}},
cluster_change_permitted := false,
commit_index := 1,
last_applied := 1,
machine_state := <<"hi1">>} = State3,
[{send_rpc, N3,
#append_entries_rpc{term = 5, leader_id = N1,
prev_log_index = 3,
prev_log_term = 5,
leader_commit = 1,
entries = [{4, 5, {'$ra_cluster_change', _,
#{N2 := #{voter_status := #{membership := voter,
uid := <<"uid">>}}},
_}}]}},
{send_rpc, N2,
#append_entries_rpc{term = 5, leader_id = N1,
prev_log_index = 3,
prev_log_term = 5,
leader_commit = 1,
entries = [{4, 5, {'$ra_cluster_change', _,
#{N2 := #{voter_status := #{membership := voter,
uid := <<"uid">>}}},
_}}]}} |
_]} = ra_server:handle_leader(RaJoin, State2),
Ack2 = #append_entries_reply{term = 5, success = true,
next_index = 5,
last_index = 4, last_term = 5},
% voter ack, raises commit_index
{leader, #{cluster := #{N2 := #{next_index := 5,
match_index := 4}},
commit_index := 3,
last_applied := 3,
machine_state := <<"hi3">>},
[{next_event, info, pipeline_rpcs},
{aux, eval}]} = ra_server:handle_leader({N2, Ack2}, State3),
ok.
append_entries_reply_success(_Config) ->
N1 = ?N1, N2 = ?N2, N3 = ?N3,
Cluster = #{N1 => new_peer_with(#{next_index => 5, match_index => 4}),
N2 => new_peer_with(#{next_index => 1, match_index => 0,
commit_index_sent => 3}),
N3 => new_peer_with(#{next_index => 2, match_index => 1})},
State0 = (base_state(3, ?FUNCTION_NAME))#{commit_index => 1,
last_applied => 1,
cluster => Cluster,
machine_state => <<"hi1">>},
Msg = {N2, #append_entries_reply{term = 5, success = true,
next_index = 4,
last_index = 3, last_term = 5}},
% update match index
{leader, #{cluster := #{N2 := #{next_index := 4,
match_index := 3}},
commit_index := 3,
last_applied := 3,
machine_state := <<"hi3">>} = State,
[{next_event, info, pipeline_rpcs},
{aux, eval}]} = ra_server:handle_leader(Msg, State0),
{leader, #{cluster := #{N2 := #{next_index := 4,
match_index := 3}},
commit_index := 3,
last_applied := 3,
machine_state := <<"hi3">>},