Skip to content

Commit

Permalink
Add configuration setting to restore old machine upgrade behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbbell committed Jan 6, 2025
1 parent 782d1ab commit 30cc92a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@
%% The simple machine config is version that can only be used for simple state
%% machines that cannot access any of the advanced features.

-type upgrade_machine_when() :: after_election | when_available_everywhere.
%% When a new Ra machine version should be upgraded to.
%%
%% <ul>
%% <li>`after_election': when a leader is elected and if it supports a newer
%% machine version, it switches to that new version. This was the default and
%% only possible behavior in Ra up to 2.15.</li>
%% <li>`when_available_everywhere': a new machine version it used only when
%% all members of the cluster support it. This is the default behavior
%% starting with Ra 2.16.</li>
%% </ul>

-type ra_server_config() :: #{id := ra_server_id(),
uid := ra_uid(),
%% a friendly name to refer to a particular
Expand All @@ -218,7 +230,8 @@
counter => counters:counters_ref(),
membership => ra_membership(),
system_config => ra_system:config(),
has_changed => boolean()
has_changed => boolean(),
upgrade_machine_when => upgrade_machine_when()
}.

-type ra_server_info_key() :: machine_version | atom().
Expand Down Expand Up @@ -271,7 +284,8 @@
command_reply_mode/0,
ra_event_formatter_fun/0,
effect/0,
effects/0
effects/0,
upgrade_machine_when/0
]).

-spec name(ClusterName :: ra_cluster_name(), UniqueSuffix::string()) -> atom().
Expand Down Expand Up @@ -358,6 +372,10 @@ init(#{id := Id,
end,
MacMod = ra_machine:which_module(Machine, MacVer),

UpgradeMachineWhen = maps:get(
upgrade_machine_when, Config,
when_available_everywhere),

CommitIndex = max(LastApplied, SnapshotIdx),
Cfg = #cfg{id = Id,
uid = UId,
Expand All @@ -371,6 +389,7 @@ init(#{id := Id,
effective_handle_aux_fun = ra_machine:which_aux_fun(MacMod),
max_pipeline_count = MaxPipelineCount,
max_append_entries_rpc_batch_size = MaxAERBatchSize,
upgrade_machine_when = UpgradeMachineWhen,
counter = maps:get(counter, Config, undefined),
system_config = SystemConfig},
put_counter(Cfg, ?C_RA_SVR_METRIC_COMMIT_INDEX, CommitIndex),
Expand Down Expand Up @@ -3445,19 +3464,25 @@ after_log_append_reply(Cmd, Idx, Term, Effects0) ->

post_election_effects(
#{cfg := #cfg{effective_machine_version = EffectiveMacVer,
machine = Mac}} = State) ->
machine = Mac,
upgrade_machine_when = UpgradeMachineWhen}} = State) ->
Peers = peers(State),
PeerIds = maps:keys(Peers),
case PeerIds of
[] ->

UpgradeMachineNow = (
PeerIds =:= [] orelse
UpgradeMachineWhen =:= after_election),

case UpgradeMachineNow of
true ->
%% This node is alone in the cluster, we can send the `noop'
%% command with the newer machine version right away.
MacVer = ra_machine:version(Mac),
Noop = {noop, #{ts => erlang:system_time(millisecond)},
MacVer},
NoopEffect = {next_event, cast, {command, Noop}},
[NoopEffect];
_ ->
false ->
%% We continue to send the `noop' command immediately after
%% becoming a leader, but compared to Ra 2.15 and older, we don't
%% set the machine version to the latest: we keep the same
Expand Down
1 change: 1 addition & 0 deletions src/ra_server.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
effective_handle_aux_fun :: undefined | {handle_aux, 5 | 6},
max_pipeline_count = ?DEFAULT_MAX_PIPELINE_COUNT :: non_neg_integer(),
max_append_entries_rpc_batch_size = ?AER_CHUNK_SIZE :: non_neg_integer(),
upgrade_machine_when = when_available_everywhere :: ra_server:upgrade_machine_when(),
counter :: undefined | counters:counters_ref(),
system_config :: ra_system:config()
}).

0 comments on commit 30cc92a

Please sign in to comment.