Skip to content

Commit 42d5d30

Browse files
hsun324facebook-github-bot
authored andcommitted
Fix inaccuracy with handover candidates at start of log
Summary: Fix an issue where, when the log is very short, the computation of handover candidates can erroneously consider a peer that has never responded to a heartbeat as eligible for handover. In addition, disallow handing over leadership to a peer whose election weight is zero. Reviewed By: jaher Differential Revision: D80034548 fbshipit-source-id: 1f17d0f6676204f56d139ddce1318c4e8324d8fa
1 parent 2f2954f commit 42d5d30

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/wa_raft_server.erl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ follower(
15261526
) ->
15271527
?RAFT_COUNT('wa_raft.follower.handover'),
15281528
?SERVER_LOG_NOTICE(State0, "evaluating handover RPC from ~0p.", [Sender]),
1529-
case ?RAFT_LEADER_ELIGIBLE(App) of
1529+
case ?RAFT_LEADER_ELIGIBLE(App) andalso ?RAFT_ELECTION_WEIGHT(App) =/= 0 of
15301530
true ->
15311531
case append_entries(?FUNCTION_NAME, PrevLogIndex, PrevLogTerm, LogEntries, length(LogEntries), State0) of
15321532
{ok, true, _, State1} ->
@@ -2875,11 +2875,13 @@ is_eligible_for_handover(
28752875
last_applied_indices = LastAppliedIndices
28762876
}
28772877
) ->
2878-
% A peer whose matching index is unknown should not be eligible for handovers.
2879-
MatchIndex = maps:get(CandidateId, MatchIndices, 0),
2880-
% A peer whose last applied index is unknown should not be eligible for handovers.
2881-
LastAppliedIndex = maps:get(CandidateId, LastAppliedIndices, 0),
2882-
MatchIndex >= MatchCutoffIndex andalso LastAppliedIndex >= ApplyCutoffIndex.
2878+
% A peer whose matching index or last applied index is unknown should not be eligible for handovers.
2879+
case {maps:find(CandidateId, MatchIndices), maps:find(CandidateId, LastAppliedIndices)} of
2880+
{{ok, MatchIndex}, {ok, LastAppliedIndex}} ->
2881+
MatchIndex >= MatchCutoffIndex andalso LastAppliedIndex >= ApplyCutoffIndex;
2882+
_ ->
2883+
false
2884+
end.
28832885

28842886
%%------------------------------------------------------------------------------
28852887
%% RAFT Server - State Machine Implementation - Configuration Changes

0 commit comments

Comments
 (0)