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

Non-voters and automatic promotion #375

Merged
merged 8 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 7 additions & 6 deletions src/ra.erl
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,14 @@ start_server(System, ClusterName, {_, _} = ServerId, Machine, ServerIds) ->
start_server(System, ClusterName, #{id => ServerId}, Machine, ServerIds);
start_server(System, ClusterName, #{id := {_, _}} = Conf0, Machine, ServerIds)
when is_atom(System) ->
UId = new_uid(ra_lib:to_binary(ClusterName)),
UId = maps:get(uid, Conf0,
new_uid(ra_lib:to_binary(ClusterName))),
Conf = #{cluster_name => ClusterName,
uid => UId,
initial_members => ServerIds,
log_init_args => #{uid => UId},
machine => Machine},
start_server(System, maps:merge(Conf, Conf0)).
start_server(System, maps:merge(Conf0, Conf)).

%% @doc Starts a ra server in the default system
%% @param Conf a ra_server_config() configuration map.
Expand Down Expand Up @@ -1134,15 +1135,15 @@ key_metrics({Name, N} = ServerId) when N == node() ->
case whereis(Name) of
undefined ->
Counters#{state => noproc,
non_voter => noproc};
membership => unknown};
_ ->
case ets:lookup(ra_state, Name) of
[] ->
Counters#{state => unknown,
non_voter => unknown};
[{_, State, NonVoter}] ->
membership => unknown};
[{_, State, Membership}] ->
Counters#{state => State,
non_voter => NonVoter}
membership => Membership}
end
end;
key_metrics({_, N} = ServerId) ->
Expand Down
24 changes: 14 additions & 10 deletions src/ra.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@
%% Subset of ra_server:ra_server_config().
%% Both `ra:add_member` and `ra:start_server` must be called with the same values.
-type ra_new_server() :: #{id := ra_server_id(),

%% If set, server will start as non-voter until later promoted by the
%% leader.
non_voter => boolean(),
% Defaults to `voter` is absent.
michaelklishin marked this conversation as resolved.
Show resolved Hide resolved
membership => ra_membership(),
% Required for `promotable` in the above.
uid => ra_uid()}.

-type ra_peer_status() :: normal |
{sending_snapshot, pid()} |
suspended |
disconnected.

-type ra_voter_status() :: #{non_voter => boolean(),
-type ra_membership() :: voter | promotable | non_voter | unknown.

-type ra_voter_status() :: #{membership => ra_membership(),
uid => ra_uid(),
target => ra_index()}.

Expand All @@ -64,15 +65,19 @@
% the commit index last sent
% used for evaluating pipeline status
commit_index_sent := non_neg_integer(),
%% whether the peer is part of the consensus
voter_status := ra_voter_status(),
%% Whether the peer is part of the consensus.
%% Defaults to "yes" if absent.
voter_status => ra_voter_status(),
%% indicates that a snapshot is being sent
%% to the peer
status := ra_peer_status()}.

-type ra_cluster() :: #{ra_server_id() => ra_peer_state()}.

-type ra_cluster_servers() :: [ra_server_id()].
%% Dehydrated cluster:
-type ra_cluster_servers() :: [ra_server_id()]. % Deprecated
-type ra_peer_snapshot() :: #{voter_status => ra_voter_status()}.
-type ra_cluster_snapshot() :: #{ra_server_id() => ra_peer_snapshot()}.

%% represent a unique entry in the ra log
-type log_entry() :: {ra_index(), ra_term(), term()}.
Expand Down Expand Up @@ -154,8 +159,7 @@

-type snapshot_meta() :: #{index := ra_index(),
term := ra_term(),
cluster := ra_cluster_servers(),
cluster_state => ra_cluster(), %% TODO replace `cluster`
cluster := ra_cluster_snapshot(),
machine_version := ra_machine:version()}.

-record(install_snapshot_rpc,
Expand Down
2 changes: 1 addition & 1 deletion src/ra_directory.erl
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ overview(System) when is_atom(System) ->
pid => Pid,
parent => Parent,
state => S,
non_voter => V,
membership => V,
cluster_name => ClusterName,
snapshot_state => maps:get(UId, Snaps,
undefined)}}
Expand Down
5 changes: 3 additions & 2 deletions src/ra_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -637,14 +637,15 @@ update_release_cursor0(Idx, Cluster, MacVersion, MacState,
#?MODULE{cfg = #cfg{snapshot_interval = SnapInter},
reader = Reader,
snapshot_state = SnapState} = State0) ->
ClusterServerIds = maps:keys(Cluster),
ClusterServerIds = maps:map(fun (_, V) ->
maps:with([voter_status], V)
end, Cluster),
SnapLimit = case ra_snapshot:current(SnapState) of
undefined -> SnapInter;
{I, _} -> I + SnapInter
end,
Meta = #{index => Idx,
cluster => ClusterServerIds,
cluster_state => Cluster,
machine_version => MacVersion},
% The release cursor index is the last entry _not_ contributing
% to the current state. I.e. the last entry that can be discarded.
Expand Down
Loading