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 2 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
17 changes: 17 additions & 0 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,23 @@ 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} = Msg,
#{current_term := CurTerm,
cfg := #cfg{log_id = LogId}} = State0)
when Term =:= CurTerm ->
{LastIdx, _} = last_idx_term(State0),
case PeerLastIdx > LastIdx of
sile marked this conversation as resolved.
Show resolved Hide resolved
true ->
%% To prevent the liveness issue reported in https://github.com/rabbitmq/ra/issues/439,
%% if a candidate member receives a `#pre_vote_rpc{}` with a higher last index,
%% it should transition to the follower state.
?INFO("~ts: candidate pre_vote_rpc with higher last index received ~b -> ~b",
[LogId, LastIdx, PeerLastIdx]),
State = update_term_and_voted_for(Term, undefined, State0),
{follower, State, [{next_event, Msg}]};
false ->
{candidate, State0, []}
end;
handle_candidate(#pre_vote_rpc{}, State) ->
%% just ignore pre_votes that aren't of a higher term
{candidate, State, []};
Expand Down
22 changes: 22 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,27 @@ 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 abdicates for higher index
{follower, #{}, _}
= 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