66% % @hidden
77-module (ra_server ).
88
9+ -include_lib (" stdlib/include/assert.hrl" ).
910-include (" ra.hrl" ).
1011-include (" ra_server.hrl" ).
1112
@@ -1160,6 +1161,7 @@ handle_follower(#append_entries_rpc{term = Term,
11601161 State00 = #{cfg := # cfg {log_id = LogId ,
11611162 id = Id } = Cfg ,
11621163 log := Log00 ,
1164+ commit_index := CommitIndex ,
11631165 current_term := CurTerm })
11641166 when Term >= CurTerm ->
11651167 ok = incr_counter (Cfg , ? C_RA_SRV_AER_RECEIVED_FOLLOWER , 1 ),
@@ -1175,13 +1177,14 @@ handle_follower(#append_entries_rpc{term = Term,
11751177 case Entries of
11761178 [] ->
11771179 ok = incr_counter (Cfg , ? C_RA_SRV_AER_RECEIVED_FOLLOWER_EMPTY , 1 ),
1178- LastIdx = ra_log :last_index_term (Log1 ),
1180+ { LastIdx , _ } = ra_log :last_index_term (Log1 ),
11791181 Log2 = case Entries0 of
1180- [] when element ( 1 , LastIdx ) > PLIdx ->
1182+ [] when LastIdx > PLIdx ->
11811183 % % if no entries were sent we need to reset
11821184 % % last index to match the leader
1183- ? DEBUG (" ~ts : resetting last index to ~b " ,
1184- [LogId , PLIdx ]),
1185+ ? DEBUG (" ~ts : resetting last index to ~b from ~b in term ~b " ,
1186+ [LogId , PLIdx , LastIdx , Term ]),
1187+ ? assertNot (PLIdx =< CommitIndex ),
11851188 {ok , L } = ra_log :set_last_index (PLIdx , Log1 ),
11861189 L ;
11871190 _ ->
@@ -1218,7 +1221,6 @@ handle_follower(#append_entries_rpc{term = Term,
12181221 % % alternative where the WAL writes the last index, term
12191222 % % it wrote for each UID into an ETS table and query
12201223 % % this.
1221- % Log = ra_log:reset_to_last_known_written(Log1),
12221224 Log = Log1 ,
12231225 {await_condition ,
12241226 State1 #{log => Log ,
@@ -1249,7 +1251,7 @@ handle_follower(#append_entries_rpc{term = Term,
12491251 % % NB: this is the commit index before update
12501252 LastApplied = maps :get (last_applied , State00 ),
12511253 ? INFO (" ~ts : term mismatch - follower had entry at ~b with term ~b "
1252- " but not with term ~b~n "
1254+ " but not with term ~b . "
12531255 " Asking leader ~w to resend from ~b " ,
12541256 [LogId , PLIdx , OtherTerm , PLTerm , LeaderId , LastApplied + 1 ]),
12551257 % This situation arises when a minority leader replicates entries
@@ -1346,16 +1348,16 @@ handle_follower(#request_vote_rpc{term = Term, candidate_id = Cand,
13461348 LastIdxTerm = last_idx_term (State1 ),
13471349 case is_candidate_log_up_to_date (LLIdx , LLTerm , LastIdxTerm ) of
13481350 true ->
1349- ? INFO (" ~ts : granting vote for ~w with last indexterm ~w "
1351+ ? INFO (" ~ts : granting vote for ~w with last {index, term} ~w "
13501352 " for term ~b previous term was ~b " ,
13511353 [LogId , Cand , {LLIdx , LLTerm }, Term , CurTerm ]),
13521354 Reply = # request_vote_result {term = Term , vote_granted = true },
13531355 State = update_term_and_voted_for (Term , Cand , State1 ),
13541356 {follower , State , [{reply , Reply }]};
13551357 false ->
13561358 ? INFO (" ~ts : declining vote for ~w for term ~b ,"
1357- " candidate last log index term was: ~w~n "
1358- " last log entry idxterm seen was : ~w " ,
1359+ " candidate last log { index, term} was: ~w "
1360+ " last log entry {index, term} is : ~w " ,
13591361 [LogId , Cand , Term , {LLIdx , LLTerm }, {LastIdxTerm }]),
13601362 Reply = # request_vote_result {term = Term , vote_granted = false },
13611363 {follower , update_term (Term , State1 ), [{reply , Reply }]}
@@ -2399,7 +2401,7 @@ process_pre_vote(FsmState, #pre_vote_rpc{term = Term, candidate_id = Cand,
23992401 TheirMacVer =< OurMacVer ) ->
24002402 ? DEBUG (" ~ts : granting pre-vote for ~w "
24012403 " machine version (their:ours:effective) ~b :~b :~b "
2402- " with last indexterm ~w "
2404+ " with last {index, term} ~w "
24032405 " for term ~b previous term ~b " ,
24042406 [log_id (State0 ), Cand , TheirMacVer , OurMacVer , EffMacVer ,
24052407 {LLIdx , LLTerm }, Term , CurTerm ]),
@@ -2413,8 +2415,8 @@ process_pre_vote(FsmState, #pre_vote_rpc{term = Term, candidate_id = Cand,
24132415 start_election_timeout ]};
24142416 false ->
24152417 ? DEBUG (" ~ts : declining pre-vote for ~w for term ~b ,"
2416- " candidate last log index term was: ~w~n "
2417- " Last log entry idxterm seen was : ~w " ,
2418+ " candidate last log { index, term} was: ~w "
2419+ " last log entry {index, term} seen is : ~w " ,
24182420 [log_id (State0 ), Cand , Term , {LLIdx , LLTerm }, LastIdxTerm ]),
24192421 case FsmState of
24202422 follower ->
0 commit comments