From 58218c1d2702cdda3020193e039236e982e35462 Mon Sep 17 00:00:00 2001 From: Karl Nilsson Date: Tue, 5 Sep 2023 14:13:21 +0100 Subject: [PATCH] ensure term is updated for snapshot installations --- src/ra_server.erl | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/ra_server.erl b/src/ra_server.erl index 0695a2e8..e69bb923 100644 --- a/src/ra_server.erl +++ b/src/ra_server.erl @@ -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, @@ -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 @@ -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},