Skip to content

Commit 297069f

Browse files
Sarah Hassanfacebook-github-bot
authored andcommitted
adjust snapshot available command handling for valid witness snapshots
Summary: For valid witness snapshot with non empty root directory, witness should no longer install snapshot directly but transition to stalled first. In stalled state, after installing snapshot if loaded config says we're witness transition to witness else transition to follower. Reviewed By: hsun324 Differential Revision: D72427209 fbshipit-source-id: cb62efb738a7e7118ff35ac8e795c45574df7005
1 parent 11be6c1 commit 297069f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/wa_raft_server.erl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,14 @@ stalled(Type, ?SNAPSHOT_AVAILABLE_COMMAND(Root, #raft_log_pos{index = SnapshotIn
811811
true -> advance_term(?FUNCTION_NAME, SnapshotTerm, undefined, State1);
812812
false -> State1
813813
end,
814+
NextState = case is_self_witness(State2) of
815+
true -> witness;
816+
false -> follower
817+
end,
814818
% At this point, we assume that we received some cluster membership configuration from
815819
% our peer so it is safe to transition to an operational state.
816820
reply(Type, ok),
817-
{next_state, follower, State2}
821+
{next_state, NextState, State2}
818822
catch
819823
_:Reason ->
820824
?LOG_WARNING("Server[~0p, term ~0p, stalled] failed to load available snapshot ~p due to ~p",
@@ -1828,7 +1832,7 @@ command(StateName, Type, ?ADJUST_MEMBERSHIP_COMMAND(Action, Peer, ConfigIndex),
18281832
%% [Snapshot Available] Follower and candidate nodes might switch to stalled to install snapshot.
18291833
command(StateName, Type, ?SNAPSHOT_AVAILABLE_COMMAND(_Root, #raft_log_pos{index = SnapshotIndex}) = Event,
18301834
#raft_state{name = Name, current_term = CurrentTerm, last_applied = LastAppliedIndex} = State)
1831-
when StateName =:= follower orelse StateName =:= candidate ->
1835+
when StateName =:= follower orelse StateName =:= candidate orelse StateName =:= witness ->
18321836
case SnapshotIndex > LastAppliedIndex of
18331837
true ->
18341838
?LOG_NOTICE("Server[~0p, term ~0p, ~0p] got snapshot with newer index ~p compared to currently applied index ~p",
@@ -1931,6 +1935,10 @@ config_witnesses(#{witness := Witnesses}) ->
19311935
config_witnesses(_Config) ->
19321936
[].
19331937

1938+
-spec is_self_witness(#raft_state{}) -> boolean().
1939+
is_self_witness(#raft_state{self = #raft_identity{name = Name, node = Node}} = RaftState) ->
1940+
lists:member({Name, Node}, config_witnesses(config(RaftState))).
1941+
19341942
-spec config_identities(Config :: config()) -> Peers :: [#raft_identity{}].
19351943
config_identities(#{membership := Membership}) ->
19361944
[#raft_identity{name = Name, node = Node} || {Name, Node} <- Membership];

0 commit comments

Comments
 (0)