Skip to content

Commit

Permalink
Exclude witnesses from handover candidates
Browse files Browse the repository at this point in the history
Summary: Exclude witness peers when determining good handover candidates as witnesses cannot become leader.

Reviewed By: jaher

Differential Revision: D70976290

fbshipit-source-id: 64d8ade85f5a40c15e94c925d0d07187264b2e14
  • Loading branch information
hsun324 authored and facebook-github-bot committed Mar 11, 2025
1 parent 2b8c63c commit 2c22006
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/wa_raft_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,14 @@ config_membership(#{membership := Membership}) when Membership =/= [] ->
config_membership(_Config) ->
error(membership_not_set).

-spec config_replicas(Config :: config()) -> Replicas :: membership().
config_replicas(#{membership := Membership, witness := Witnesses}) ->
Membership -- Witnesses;
config_replicas(#{membership := Membership}) ->
Membership;
config_replicas(_Config) ->
error(membership_not_set).

-spec config_witnesses(Config :: config()) -> Witnesses :: [peer()].
config_witnesses(#{witness := Witnesses}) ->
Witnesses;
Expand Down Expand Up @@ -2474,10 +2482,10 @@ heartbeat(?IDENTITY_REQUIRES_MIGRATION(_, FollowerId) = Sender,

-spec compute_handover_candidates(State :: #raft_state{}) -> [node()].
compute_handover_candidates(#raft_state{application = App, log_view = View, match_index = MatchIndex} = State) ->
Membership = config_membership(config(State)),
Replicas = config_replicas(config(State)),
LastLogIndex = wa_raft_log:last_index(View),
MaxHandoverLogEntries = ?RAFT_HANDOVER_MAX_ENTRIES(App),
[Peer || {_Name, Peer} <- Membership, Peer =/= node(), LastLogIndex - maps:get(Peer, MatchIndex, 0) =< MaxHandoverLogEntries].
[Peer || {_Name, Peer} <- Replicas, Peer =/= node(), LastLogIndex - maps:get(Peer, MatchIndex, 0) =< MaxHandoverLogEntries].

-spec adjust_config(Action :: {add, peer()} | {remove, peer()} | {add_witness, peer()} | {remove_witness, peer()} | {refresh, undefined},
Config :: config(), State :: #raft_state{}) -> {ok, NewConfig :: config()} | {error, Reason :: atom()}.
Expand Down

0 comments on commit 2c22006

Please sign in to comment.