Skip to content

Commit

Permalink
ensure term is updated for snapshot installations
Browse files Browse the repository at this point in the history
  • Loading branch information
kjnilsson committed Sep 5, 2023
1 parent 2d370fc commit 58218c1
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1136,15 +1136,14 @@ handle_follower(#request_vote_rpc{term = Term, candidate_id = Cand,
[LogId, Cand, {LLIdx, LLTerm}, Term, CurTerm]),
Reply = #request_vote_result{term = Term, vote_granted = true},
State = update_term_and_voted_for(Term, Cand, State1),
{follower, State#{voted_for => Cand, current_term => Term},
[{reply, Reply}]};
{follower, State, [{reply, Reply}]};
false ->
?INFO("~ts: declining vote for ~w for term ~b,"
" candidate last log index term was: ~w~n"
" last log entry idxterm seen was: ~w",
[LogId, Cand, Term, {LLIdx, LLTerm}, {LastIdxTerm}]),
Reply = #request_vote_result{term = Term, vote_granted = false},
{follower, State1#{current_term => Term}, [{reply, Reply}]}
{follower, update_term(Term, State1), [{reply, Reply}]}
end;
handle_follower(#request_vote_rpc{term = Term, candidate_id = Candidate},
State = #{current_term := CurTerm,
Expand Down Expand Up @@ -1196,8 +1195,8 @@ handle_follower(#install_snapshot_rpc{term = Term,
SnapState0 = ra_log:snapshot_state(Log0),
{ok, SS} = ra_snapshot:begin_accept(Meta, SnapState0),
Log = ra_log:set_snapshot_state(SS, Log0),
{receive_snapshot, State0#{log => Log,
leader_id => LeaderId},
{receive_snapshot, update_term(Term, State0#{log => Log,
leader_id => LeaderId}),
[{next_event, Rpc}, {record_leader_msg, LeaderId}]};
handle_follower(#request_vote_result{}, State) ->
%% handle to avoid logging as unhandled
Expand Down Expand Up @@ -1269,19 +1268,19 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,
end,

{#{cluster := ClusterIds}, MacState} = ra_log:recover_snapshot(Log),
State = State0#{cfg => Cfg,
log => Log,
current_term => Term,
commit_index => SnapIndex,
last_applied => SnapIndex,
cluster => make_cluster(Id, ClusterIds),
machine_state => MacState},
State = update_term(Term,
State0#{cfg => Cfg,
log => Log,
commit_index => SnapIndex,
last_applied => SnapIndex,
cluster => make_cluster(Id, ClusterIds),
machine_state => MacState}),
%% it was the last snapshot chunk so we can revert back to
%% follower status
{follower, persist_last_applied(State), [{reply, Reply} | Effs]};
next ->
Log = ra_log:set_snapshot_state(SnapState, Log0),
State = State0#{log => Log},
State = update_term(Term, State0#{log => Log}),
{receive_snapshot, State, [{reply, Reply}]}
end;
handle_receive_snapshot({ra_log_event, Evt},
Expand Down

0 comments on commit 58218c1

Please sign in to comment.