Skip to content

Commit

Permalink
fix: follower prefers older voter status
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Valiushko committed Sep 13, 2023
1 parent 221a016 commit ac707d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/ra.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ key_metrics({Name, N} = ServerId) when N == node() ->
case whereis(Name) of
undefined ->
Counters#{state => noproc,
voter_status => unknown};
voter_status => noproc};
_ ->
case ets:lookup(ra_state, Name) of
[] ->
Expand Down
22 changes: 12 additions & 10 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
max_pipeline_count => non_neg_integer(),
ra_event_formatter => {module(), atom(), [term()]},
counter => counters:counters_ref(),
voter_status => ra_voter_status(),
init_non_voter => ra_nvid(),
system_config => ra_system:config()}.

-type mutable_config() :: #{cluster_name => ra_cluster_name(),
Expand Down Expand Up @@ -2367,7 +2367,7 @@ apply_with({Idx, Term, {'$ra_cluster_change', CmdMeta, NewCluster, ReplyType}},
[log_id(State0), maps:keys(NewCluster)]),
%% we are recovering and should apply the cluster change
State0#{cluster => NewCluster,
voter_status => voter_status(id(State0), NewCluster),
voter_status => current_voter_status(NewCluster, State0),
cluster_change_permitted => true,
cluster_index_term => {Idx, Term}};
_ ->
Expand Down Expand Up @@ -2574,15 +2574,8 @@ pre_append_log_follower({Idx, Term, Cmd} = Entry,
end;
pre_append_log_follower({Idx, Term, {'$ra_cluster_change', _, Cluster, _}},
State) ->
OldStatus = {_, NewState} = voter_status(State),
NewStatus = {_, OldState} = voter_status(id(State), Cluster),
Self = maps:get(nvid, OldState, undefined),
MyStatus = case maps:get(nvid, NewState, undefined) of
Self -> NewStatus;
_ -> OldStatus
end,
State#{cluster => Cluster,
voter_status => MyStatus,
voter_status => current_voter_status(Cluster, State),
cluster_index_term => {Idx, Term}};
pre_append_log_follower(_, State) ->
State.
Expand Down Expand Up @@ -2961,6 +2954,15 @@ voter_status(PeerId, Cluster) ->
maps:get(voter_status, Peer, {voter, #{}})
end.

current_voter_status(Cluster, State) ->
{_, NReason} = New = voter_status(id(State), Cluster),
{_, CReason} = Current = voter_status(State),
Self = maps:get(nvid, CReason, undefined),
case maps:get(nvid, NReason, undefined) of
Self -> New;
_ -> Current
end.

-spec required_quorum(ra_cluster()) -> pos_integer().
required_quorum(Cluster) ->
Voters = count_voters(Cluster),
Expand Down
12 changes: 6 additions & 6 deletions src/ra_server_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -786,16 +786,16 @@ follower({call, From}, {log_fold, Fun, Term}, State) ->
fold_log(From, Fun, Term, State);
follower(EventType, Msg, #state{conf = #conf{name = Name},
server_state = SS0} = State0) ->
Voter0 = ra_server:voter_status(SS0),
VoterStatus0 = ra_server:voter_status(SS0),
case handle_follower(Msg, State0) of
{follower, State1, Effects} ->
{State2, Actions} = ?HANDLE_EFFECTS(Effects, EventType, State1),
State = #state{server_state = SS} = follower_leader_change(State0, State2),
case ra_server:voter_status(SS) of
Voter0 ->
VoterStatus0 ->
ok;
Voter ->
true = ets:update_element(ra_state, Name, {3, Voter})
VoterStatus ->
true = ets:update_element(ra_state, Name, {3, VoterStatus})
end,
{keep_state, State, Actions};
{pre_vote, State1, Effects} ->
Expand Down Expand Up @@ -1037,8 +1037,8 @@ format_status(Opt, [_PDict, StateName,
handle_enter(RaftState, OldRaftState,
#state{conf = #conf{name = Name},
server_state = ServerState0} = State) ->
Voter = ra_server:voter_status(ServerState0),
true = ets:insert(ra_state, {Name, RaftState, Voter}),
VoterStatus = ra_server:voter_status(ServerState0),
true = ets:insert(ra_state, {Name, RaftState, VoterStatus}),
{ServerState, Effects} = ra_server:handle_state_enter(RaftState,
ServerState0),
case RaftState == leader orelse OldRaftState == leader of
Expand Down

0 comments on commit ac707d3

Please sign in to comment.