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

ra_server: handle higher-term AERs in receive_snapshot state #470

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -1411,6 +1411,20 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,
State = update_term(Term, State0#{log => Log}),
{receive_snapshot, State, [{reply, Reply}]}
end;
handle_receive_snapshot(#append_entries_rpc{term = Term} = Msg,
#{current_term := CurTerm,
cfg := #cfg{log_id = LogId},
log := Log0} = State)
when Term > CurTerm ->
?INFO("~ts: follower receiving snapshot saw append_entries_rpc from ~w for term ~b "
"abdicates term: ~b!",
[LogId, Msg#append_entries_rpc.leader_id,
Term, CurTerm]),
SnapState0 = ra_log:snapshot_state(Log0),
SnapState = ra_snapshot:abort_accept(SnapState0),
Log = ra_log:set_snapshot_state(SnapState, Log0),
{follower, update_term(Term, clear_leader_id(State#{log => Log})),
[{next_event, Msg}]};
handle_receive_snapshot({ra_log_event, Evt},
State = #{cfg := #cfg{id = _Id, log_id = LogId},
log := Log0}) ->
Expand Down
13 changes: 9 additions & 4 deletions src/ra_server_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,15 @@ receive_snapshot(EventType, Msg, State0) ->
{receive_snapshot, State1, Effects} ->
{#state{conf = Conf} = State, Actions} =
?HANDLE_EFFECTS(Effects, EventType, State1),
#conf{receive_snapshot_timeout = ReceiveSnapshotTimeout} = Conf,
{keep_state, State,
[{state_timeout, ReceiveSnapshotTimeout,
receive_snapshot_timeout} | Actions]};
TimeoutActions = case Msg of
kjnilsson marked this conversation as resolved.
Show resolved Hide resolved
#install_snapshot_rpc{} ->
%% Reset timeout only on receive snapshot progress.
[{state_timeout, Conf#conf.receive_snapshot_timeout,
receive_snapshot_timeout}];
_ ->
[]
end,
{keep_state, State, TimeoutActions ++ Actions};
{follower, State1, Effects} ->
{State2, Actions} = ?HANDLE_EFFECTS(Effects, EventType, State1),
State = follower_leader_change(State0, State2),
Expand Down
33 changes: 33 additions & 0 deletions test/ra_server_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ all() ->
follower_ignores_installs_snapshot_with_higher_machine_version,
follower_receives_stale_snapshot,
receive_snapshot_timeout,
receive_snapshot_new_leader_aer,
snapshotted_follower_received_append_entries,
leader_received_append_entries_reply_with_stale_last_index,
leader_receives_install_snapshot_result,
Expand Down Expand Up @@ -2107,6 +2108,38 @@ receive_snapshot_timeout(_Config) ->
undefined = ra_snapshot:accepting(SS),
ok.

receive_snapshot_new_leader_aer(_Config) ->
N1 = ?N1, N2 = ?N2, N3 = ?N3,
#{N3 := {_, FState0 = #{cluster := Config,
current_term := CurTerm,
commit_index := CommitIdx}, _}}
= init_servers([N1, N2, N3], {module, ra_queue, #{}}),
FState = FState0#{last_applied => 3},
LastTerm = 1, % snapshot term
Idx = 6,
ISRpc = #install_snapshot_rpc{term = CurTerm, leader_id = N1,
meta = snap_meta(Idx, LastTerm, Config),
chunk_state = {1, last},
data = []},
{receive_snapshot, FState1,
[{next_event, ISRpc}, {record_leader_msg, _}]} =
ra_server:handle_follower(ISRpc, FState),

%% revert back to follower on next-term AER
AER = #append_entries_rpc{term = CurTerm + 1, leader_id = N1,
prev_log_index = CommitIdx,
prev_log_term = CurTerm,
leader_commit = CommitIdx,
entries = []},
{follower, #{log := Log, current_term := NewTerm}, _}
= ra_server:handle_receive_snapshot(AER, FState1),
%% term should be updated
NewTerm = CurTerm + 1,
%% snapshot should be aborted
SS = ra_log:snapshot_state(Log),
undefined = ra_snapshot:accepting(SS),
ok.

snapshotted_follower_received_append_entries(_Config) ->
N1 = ?N1, N2 = ?N2, N3 = ?N3,
#{N3 := {_, FState0 = #{cluster := Config}, _}} =
Expand Down
Loading