Skip to content

Commit

Permalink
cache raft membership information
Browse files Browse the repository at this point in the history
Summary: This allows us to cache membership information similarly to how we cache state and current term to `shardmd` can observe it.

Differential Revision: D53683966

fbshipit-source-id: 5a3e9bc00b93c2d409906b522c6cdf0bb29029ad
  • Loading branch information
Sarah Hassan authored and facebook-github-bot committed Feb 12, 2024
1 parent 8805d79 commit 70dd4b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/wa_raft_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@

%% Public API
-export([
get_state/2,
get_current_term/2,
get_leader/2,
get_stale/2
get_membership/2,
get_stale/2,
get_state/2
]).

%% Internal API
-export([
init_tables/0,
set_state/3,
delete_state/2,
set_current_term/3,
set_leader/3,
set_stale/3
set_membership/3,
set_stale/3,
set_state/3
]).

%% Local RAFT server's current FSM state
Expand All @@ -36,6 +38,8 @@
-define(RAFT_CURRENT_LEADER_KEY(Table, Partition), {leader, Table, Partition}).
%% Local RAFT server's current stale flag - indicates if the server thinks its data is stale
-define(RAFT_STALE_KEY(Table, Partition), {stale, Table, Partition}).
%% Local RAFT server's most recently known membership
-define(RAFT_MEMBERSHIP_KEY(Table, Partition), {membership, Table, Partition}).

%%-------------------------------------------------------------------
%% RAFT Info - Public API
Expand Down Expand Up @@ -65,6 +69,10 @@ get_state(Table, Partition) ->
get_stale(Table, Partition) ->
get(?RAFT_STALE_KEY(Table, Partition), true).

-spec get_membership(wa_raft:table(), wa_raft:partition()) -> wa_raft_server:membership() | undefined.
get_membership(Table, Partition) ->
get(?RAFT_MEMBERSHIP_KEY(Table, Partition), undefined).

%%-------------------------------------------------------------------
%% RAFT Info - Internal API
%%-------------------------------------------------------------------
Expand Down Expand Up @@ -101,3 +109,7 @@ delete_state(Table, Partition) ->
-spec set_stale(wa_raft:table(), wa_raft:partition(), boolean()) -> true.
set_stale(Table, Partition, Stale) ->
set(?RAFT_STALE_KEY(Table, Partition), Stale).

-spec set_membership(wa_raft:table(), wa_raft:partition(), wa_raft_server:membership()) -> true.
set_membership(Table, Partition, Membership) ->
set(?RAFT_MEMBERSHIP_KEY(Table, Partition), Membership).
6 changes: 4 additions & 2 deletions src/wa_raft_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1820,9 +1820,10 @@ config_index(#raft_state{cached_config = undefined} = State) ->
%% available in the RAFT log and so needs to be kept in sync with what
%% the RAFT server expects is in storage.
-spec load_config(State :: #raft_state{}) -> NewState :: #raft_state{}.
load_config(#raft_state{storage = Storage} = State) ->
load_config(#raft_state{storage = Storage, table = Table, partition = Partition} = State) ->
case wa_raft_storage:read_metadata(Storage, config) of
{ok, #raft_log_pos{index = ConfigIndex}, Config} ->
wa_raft_info:set_membership(Table, Partition, maps:get(membership, Config, [])),
State#raft_state{cached_config = {ConfigIndex, maybe_upgrade_config(Config)}};
undefined ->
State#raft_state{cached_config = undefined};
Expand All @@ -1840,7 +1841,8 @@ maybe_upgrade_config(#{version := ?RAFT_CONFIG_CURRENT_VERSION} = Config) ->
%% being applied. If it is, then update the cached configuration.
-spec maybe_update_config(Index :: wa_raft_log:log_index(), Term :: wa_raft_log:log_term(),
Op :: wa_raft_acceptor:op() | [] | undefined, State :: #raft_state{}) -> NewState :: #raft_state{}.
maybe_update_config(Index, _Term, {_Ref, {config, Config}}, State) ->
maybe_update_config(Index, _Term, {_Ref, {config, Config}}, #raft_state{table = Table, partition = Partition} = State) ->
wa_raft_info:set_membership(Table, Partition, maps:get(membership, Config, [])),
State#raft_state{cached_config = {Index, Config}};
maybe_update_config(_Index, _Term, _Op, State) ->
State.
Expand Down

0 comments on commit 70dd4b5

Please sign in to comment.