Skip to content

Commit

Permalink
Reset cluster_change_permitted when becoming leader
Browse files Browse the repository at this point in the history
As if there had been a leader change before this could reset to true
even before the leader has fully applied it's noop command for the new
term. This could result in occasional stale consistent read results.
  • Loading branch information
kjnilsson committed Dec 21, 2022
1 parent 895fd48 commit cc79766
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,8 @@ machine_query(QueryFun, #{cfg := #cfg{effective_machine_module = MacMod},

become(leader, #{cluster := Cluster, log := Log0} = State) ->
Log = ra_log:release_resources(maps:size(Cluster) + 2, random, Log0),
State#{log => Log};
State#{log => Log,
cluster_change_permitted => false};
become(follower, #{log := Log0} = State) ->
%% followers should only ever need a single segment open at any one
%% time
Expand Down
22 changes: 22 additions & 0 deletions test/ra_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ all_tests() ->
local_query_stale,
members,
consistent_query,
consistent_query_after_restart,
consistent_query_minority,
consistent_query_leader_change,
consistent_query_stale,
Expand Down Expand Up @@ -544,6 +545,27 @@ consistent_query(Config) ->
{ok, 14, Leader} = ra:consistent_query(A, fun(S) -> S end),
terminate_cluster(Cluster).

new_value(A, _) ->
A.

consistent_query_after_restart(Config) ->
DataDir = filename:join([?config(priv_dir, Config), "data"]),
[A, B] = Cluster = start_local_cluster(2, ?config(test_name, Config),
{simple, fun ?MODULE:new_value/2, 0}),
%% this test occasionally reproduces a stale read bug after a restart
%% NB: occasionally....
[begin
{ok, _, _} = ra:process_command(A, N, ?PROCESS_COMMAND_TIMEOUT),
application:stop(ra),
restart_ra(DataDir),
ok = ra:restart_server(A),
ok = ra:restart_server(B),
?assertMatch({ok, N, _}, ra:consistent_query(A, fun(S) -> S end))
end || N <- lists:seq(1, 30)],

terminate_cluster(Cluster),
ok.

consistent_query_minority(Config) ->
[A, _, _] = Cluster = start_local_cluster(3, ?config(test_name, Config),
add_machine()),
Expand Down
1 change: 1 addition & 0 deletions test/ra_log_segment_writer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
%% Copyright (c) 2017-2022 VMware, Inc. or its affiliates. All rights reserved.
%%
-module(ra_log_segment_writer_SUITE).
-compile(nowarn_export_all).
-compile(export_all).

-include_lib("common_test/include/ct.hrl").
Expand Down

0 comments on commit cc79766

Please sign in to comment.