Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where the leader is never elected even if the majority of the members are alive. #442

Merged
merged 5 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,20 @@ handle_candidate(#pre_vote_rpc{term = Term} = Msg,
handle_candidate(#request_vote_rpc{}, State = #{current_term := Term}) ->
Reply = #request_vote_result{term = Term, vote_granted = false},
{candidate, State, [{reply, Reply}]};
handle_candidate(#pre_vote_rpc{term = Term, last_log_index = PeerLastIdx} = PreVote,
#{current_term := CurTerm,
cfg := #cfg{log_id = LogId}} = State)
when Term =:= CurTerm ->
%% This clause is necessary to prevent the liveness issue reported in https://github.com/rabbitmq/ra/issues/439
{LastIdx, _} = last_idx_term(State),
case PeerLastIdx > LastIdx of
sile marked this conversation as resolved.
Show resolved Hide resolved
true ->
?INFO("~ts: candidate pre_vote_rpc with higher last index received ~b -> ~b",
[LogId, LastIdx, PeerLastIdx]),
process_pre_vote(candidate, PreVote, State);
false ->
{candidate, State, []}
end;
handle_candidate(#pre_vote_rpc{}, State) ->
%% just ignore pre_votes that aren't of a higher term
{candidate, State, []};
Expand Down
23 changes: 23 additions & 0 deletions test/ra_server_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ all() ->
higher_term_detected,
pre_vote_election,
pre_vote_election_reverts,
candidate_receives_pre_vote,
leader_receives_pre_vote,
candidate_election,
is_new,
Expand Down Expand Up @@ -1927,6 +1928,28 @@ pre_vote_election_reverts(_Config) ->
= ra_server:handle_pre_vote(ISR, State),
ok.

candidate_receives_pre_vote(_Config) ->
% candidate ignores an pre_vote_rpc with lower term and lower index
Token = make_ref(),
State = (base_state(5, ?FUNCTION_NAME))#{votes => 1},
PreVoteRpc = #pre_vote_rpc{term = 5, candidate_id = ?N1,
token = Token,
machine_version = 0,
last_log_index = 3, last_log_term = 5},
{candidate, #{}, []}
= ra_server:handle_candidate(PreVoteRpc, State),

% candidate abdicates for higher term
{follower, #{current_term := 6}, _}
= ra_server:handle_candidate(PreVoteRpc#pre_vote_rpc{term = 6}, State),

% candidate replies `#pre_vote_result{vote_granted=true}` for higher index
{candidate, #{},
[{reply, #pre_vote_result{token = Token, vote_granted = true}}]}
= ra_server:handle_candidate(PreVoteRpc#pre_vote_rpc{last_log_index = 4}, State),

ok.

leader_receives_pre_vote(_Config) ->
% leader should emit rpcs to all nodes immediately upon receiving
% an pre_vote_rpc to put upstart followers back in their place
Expand Down
Loading