Skip to content

Commit

Permalink
Merge pull request #467 from rabbitmq/md/snapshot-installed-old-state
Browse files Browse the repository at this point in the history
Add `ra_machine:snapshot_installed/4` callback to pass old state
  • Loading branch information
michaelklishin authored Aug 27, 2024
2 parents e2cffb7 + 0290e03 commit b5f6b38
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
35 changes: 31 additions & 4 deletions src/ra_machine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
-export([init/2,
apply/4,
tick/3,
snapshot_installed/3,
snapshot_installed/5,
state_enter/3,
overview/2,
query/3,
Expand Down Expand Up @@ -210,6 +210,7 @@

-optional_callbacks([tick/2,
snapshot_installed/2,
snapshot_installed/4,
state_enter/2,
init_aux/1,
handle_aux/5,
Expand Down Expand Up @@ -243,6 +244,14 @@

-callback snapshot_installed(ra_snapshot:meta(), state()) -> effects().

-callback snapshot_installed(Meta, OldMacVer, OldState, NewState) -> Effects
when
Meta :: ra_snapshot:meta(),
OldMacVer :: version(),
OldState :: state(),
NewState :: state(),
Effects :: effects().

-callback init_aux(Name :: atom()) -> AuxState :: term().

-callback handle_aux(ra_server:ra_state(),
Expand Down Expand Up @@ -302,9 +311,27 @@ apply(Mod, Metadata, Cmd, State) ->
tick(Mod, TimeMs, State) ->
?OPT_CALL(Mod:tick(TimeMs, State), []).

-spec snapshot_installed(module(), ra_snapshot:meta(), state()) -> effects().
snapshot_installed(Mod, Meta, State) ->
?OPT_CALL(Mod:snapshot_installed(Meta, State), []).
-spec snapshot_installed(Module, Meta, OldMacVer, OldState, NewState) ->
Effects when
Module :: module(),
Meta :: ra_snapshot:meta(),
OldMacVer :: version(),
OldState :: state(),
NewState :: state(),
Effects :: effects().

snapshot_installed(Mod, Meta, OldMacVer, OldState, NewState) ->
try
Mod:snapshot_installed(Meta, OldMacVer, OldState, NewState)
catch
error:undef ->
try
Mod:snapshot_installed(Meta, NewState)
catch
error:undef ->
[]
end
end.

%% @doc called when the ra_server_proc enters a new state
-spec state_enter(module(), ra_server:ra_state() | eol, state()) ->
Expand Down
5 changes: 4 additions & 1 deletion src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,8 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,
machine_versions = MachineVersions,
machine = Machine} = Cfg0,
log := Log0,
current_term := CurTerm} = State0)
current_term := CurTerm,
machine_state := OldMacState} = State0)
when Term >= CurTerm ->
?DEBUG("~ts: receiving snapshot chunk: ~b / ~w, index ~b, term ~b",
[LogId, Num, ChunkFlag, SnapIndex, SnapTerm]),
Expand Down Expand Up @@ -1377,6 +1378,8 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,

SnapInstalledEffs = ra_machine:snapshot_installed(EffMacMod,
SnapMeta,
CurEffMacVer,
OldMacState,
MacState),

State = update_term(Term,
Expand Down
2 changes: 1 addition & 1 deletion test/coordination_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ apply(#{index := _Idx}, {segment_writer_or_wal_crash_follower, _}, State) ->
apply(#{index := Idx}, _Cmd, State) ->
{State, ok, [{release_cursor, Idx, State}]}.

snapshot_installed(Meta, _State) ->
snapshot_installed(Meta, _OldMacVer, _OldState, _NewState) ->
case whereis(snapshot_installed_proc) of
undefined ->
[];
Expand Down

0 comments on commit b5f6b38

Please sign in to comment.