Skip to content

Commit

Permalink
Merge pull request #427 from sile/fix-non-voter
Browse files Browse the repository at this point in the history
Exclude `non_voter` server from quorum calculation
  • Loading branch information
michaelklishin authored May 17, 2024
2 parents 29306c0 + 924c024 commit ad6bb7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2916,7 +2916,7 @@ query_indexes(#{cfg := #cfg{id = Id},
query_index := QueryIndex}) ->
maps:fold(fun (PeerId, _, Acc) when PeerId == Id ->
Acc;
(_K, #{voter_status := #{membership := promotable}}, Acc) ->
(_K, #{voter_status := #{membership := Membership}}, Acc) when Membership =/= voter ->
Acc;
(_K, #{query_index := Idx}, Acc) ->
[Idx | Acc]
Expand All @@ -2928,7 +2928,7 @@ match_indexes(#{cfg := #cfg{id = Id},
{LWIdx, _} = ra_log:last_written(Log),
maps:fold(fun (PeerId, _, Acc) when PeerId == Id ->
Acc;
(_K, #{voter_status := #{membership := promotable}}, Acc) ->
(_K, #{voter_status := #{membership := Membership}}, Acc) when Membership =/= voter ->
Acc;
(_K, #{match_index := Idx}, Acc) ->
[Idx | Acc]
Expand Down Expand Up @@ -3246,7 +3246,7 @@ required_quorum(Cluster) ->

count_voters(Cluster) ->
maps:fold(
fun (_, #{voter_status := #{membership := promotable}}, Count) ->
fun (_, #{voter_status := #{membership := Membership}}, Count) when Membership =/= voter ->
Count;
(_, _, Count) ->
Count + 1
Expand Down
24 changes: 20 additions & 4 deletions test/ra_2_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ init_per_testcase(TestCase, Config) ->
ServerName2 = list_to_atom(atom_to_list(TestCase) ++ "2"),
ServerName3 = list_to_atom(atom_to_list(TestCase) ++ "3"),
ServerName4 = list_to_atom(atom_to_list(TestCase) ++ "4"),
ServerName5 = list_to_atom(atom_to_list(TestCase) ++ "5"),
[{test_case, TestCase},
{modname, TestCase},
{cluster_name, TestCase},
Expand All @@ -96,7 +97,9 @@ init_per_testcase(TestCase, Config) ->
{uid3, atom_to_binary(ServerName3, utf8)},
{server_id3, {ServerName3, node()}},
{uid4, atom_to_binary(ServerName4, utf8)},
{server_id4, {ServerName4, node()}}
{server_id4, {ServerName4, node()}},
{uid5, atom_to_binary(ServerName5, utf8)},
{server_id5, {ServerName5, node()}}
| Config].

enqueue(Server, Msg) ->
Expand Down Expand Up @@ -710,19 +713,32 @@ force_start_follower_as_single_member_nonvoter(Config) ->
{ok, [_], ServerId3} = ra:members(ServerId3),
ok = enqueue(ServerId3, msg2),

%% add a member
%% add a promotable member
ServerId4 = ?config(server_id4, Config),
UId4 = ?config(uid4, Config),
Conf4 = conf(ClusterName, UId4, ServerId4, PrivDir, [ServerId3]),
{ok, _, _} = ra:add_member(ServerId3, #{id => ServerId4, membership => promotable, uid => <<"test">>}),
{ok, _, _} = ra:add_member(ServerId3, #{id => ServerId4, membership => promotable, uid => <<"test4">>}),
%% the membership has changed but member not running yet
%% it is nonvoter and does not affect quorum size
{ok, _, _} = ra:process_command(ServerId3, {enq, banana}),
%% start new member
ok = ra:start_server(?SYS, Conf4#{membership => promotable, uid => <<"test">>}),
ok = ra:start_server(?SYS, Conf4#{membership => promotable, uid => <<"test4">>}),
{ok, _, ServerId3} = ra:members(ServerId4),
ok = enqueue(ServerId3, msg3),

%% add a non-voter member
ServerId5 = ?config(server_id5, Config),
UId5 = ?config(uid5, Config),
Conf5 = conf(ClusterName, UId5, ServerId5, PrivDir, [ServerId3]),
{ok, _, _} = ra:add_member(ServerId3, #{id => ServerId5, membership => non_voter, uid => <<"test5">>}),
%% the membership has changed but member not running yet
%% it is nonvoter and does not affect quorum size
{ok, _, _} = ra:process_command(ServerId3, {enq, banana}),
%% start new member
ok = ra:start_server(?SYS, Conf5#{membership => non_voter, uid => <<"test5">>}),
{ok, _, ServerId3} = ra:members(ServerId5),
ok = enqueue(ServerId3, msg4),

ok.

initial_members_query(Config) ->
Expand Down

0 comments on commit ad6bb7d

Please sign in to comment.