From 4196322e419a5d3d7453b9606de0d534d7c58eaa Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Thu, 25 Apr 2024 10:03:43 +0900 Subject: [PATCH 1/4] Ensure that non voters never become leader --- src/ra_server.erl | 18 ++++++++++++++++++ test/ra_server_SUITE.erl | 1 + 2 files changed, 19 insertions(+) diff --git a/src/ra_server.erl b/src/ra_server.erl index 54ec223f..3786e24b 100644 --- a/src/ra_server.erl +++ b/src/ra_server.erl @@ -1261,11 +1261,23 @@ handle_follower(election_timeout, {follower, State, []}; handle_follower(election_timeout, State) -> call_for_election(pre_vote, State); +handle_follower(try_become_leader, + #{cfg := #cfg{log_id = LogId}, + membership := Membership} = State) when Membership =/= voter -> + ?DEBUG("~s: follower ignored try_become_leader, non-voter: ~p0", + [LogId, Membership]), + {follower, State, []}; handle_follower(try_become_leader, State) -> call_for_election(pre_vote, State); handle_follower({register_external_log_reader, Pid}, #{log := Log0} = State) -> {Log, Effs} = ra_log:register_reader(Pid, Log0), {follower, State#{log => Log}, Effs}; +handle_follower(force_member_change, + #{cfg := #cfg{log_id = LogId}, + membership := Membership} = State) when Membership =/= voter -> + ?DEBUG("~s: follower ignored force_member_change, non-voter: ~p0", + [LogId, Membership]), + {follower, State, []}; handle_follower(force_member_change, #{cfg := #cfg{id = Id, log_id = LogId}} = State0) -> @@ -1368,6 +1380,12 @@ handle_await_condition(#request_vote_rpc{} = Msg, State) -> {follower, State, [{next_event, Msg}]}; handle_await_condition(#pre_vote_rpc{} = PreVote, State) -> process_pre_vote(await_condition, PreVote, State); +handle_await_condition(election_timeout, + #{cfg := #cfg{log_id = LogId}, + membership := Membership} = State) when Membership =/= voter -> + ?DEBUG("~s: await_condition ignored election_timeout, non-voter: ~p0", + [LogId, Membership]), + {await_condition, State, []}; handle_await_condition(election_timeout, State) -> call_for_election(pre_vote, State); handle_await_condition(await_condition_timeout, diff --git a/test/ra_server_SUITE.erl b/test/ra_server_SUITE.erl index 24808907..02cf4dbf 100644 --- a/test/ra_server_SUITE.erl +++ b/test/ra_server_SUITE.erl @@ -285,6 +285,7 @@ election_timeout(_Config) -> % non-voters ignore election_timeout NVState = State#{membership => promotable}, {follower, NVState, []} = ra_server:handle_follower(Msg, NVState), + {await_condition, NVState, []} = ra_server:handle_await_condition(Msg, NVState), % pre_vote {pre_vote, #{current_term := 5, votes := 0, From 381ea9699ae53183d82dd90779b392e30745cc1a Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Thu, 25 Apr 2024 10:19:30 +0900 Subject: [PATCH 2/4] Remove trailing 0 from log messages --- src/ra_server.erl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ra_server.erl b/src/ra_server.erl index 3786e24b..913dea03 100644 --- a/src/ra_server.erl +++ b/src/ra_server.erl @@ -1264,7 +1264,7 @@ handle_follower(election_timeout, State) -> handle_follower(try_become_leader, #{cfg := #cfg{log_id = LogId}, membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: follower ignored try_become_leader, non-voter: ~p0", + ?DEBUG("~s: follower ignored try_become_leader, non-voter: ~p", [LogId, Membership]), {follower, State, []}; handle_follower(try_become_leader, State) -> @@ -1275,7 +1275,7 @@ handle_follower({register_external_log_reader, Pid}, #{log := Log0} = State) -> handle_follower(force_member_change, #{cfg := #cfg{log_id = LogId}, membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: follower ignored force_member_change, non-voter: ~p0", + ?DEBUG("~s: follower ignored force_member_change, non-voter: ~p", [LogId, Membership]), {follower, State, []}; handle_follower(force_member_change, @@ -1383,7 +1383,7 @@ handle_await_condition(#pre_vote_rpc{} = PreVote, State) -> handle_await_condition(election_timeout, #{cfg := #cfg{log_id = LogId}, membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: await_condition ignored election_timeout, non-voter: ~p0", + ?DEBUG("~s: await_condition ignored election_timeout, non-voter: ~p", [LogId, Membership]), {await_condition, State, []}; handle_await_condition(election_timeout, State) -> From cc30411d99e8ba120ab61d66c6918704584b7361 Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Thu, 25 Apr 2024 11:32:06 +0900 Subject: [PATCH 3/4] Update log messages: s/non-voter/replicate membership state/ --- src/ra_server.erl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ra_server.erl b/src/ra_server.erl index 913dea03..14b551b5 100644 --- a/src/ra_server.erl +++ b/src/ra_server.erl @@ -1264,7 +1264,7 @@ handle_follower(election_timeout, State) -> handle_follower(try_become_leader, #{cfg := #cfg{log_id = LogId}, membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: follower ignored try_become_leader, non-voter: ~p", + ?DEBUG("~s: follower ignored try_become_leader, replicate membership state: ~p", [LogId, Membership]), {follower, State, []}; handle_follower(try_become_leader, State) -> @@ -1275,7 +1275,7 @@ handle_follower({register_external_log_reader, Pid}, #{log := Log0} = State) -> handle_follower(force_member_change, #{cfg := #cfg{log_id = LogId}, membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: follower ignored force_member_change, non-voter: ~p", + ?DEBUG("~s: follower ignored force_member_change, replicate membership state: ~p", [LogId, Membership]), {follower, State, []}; handle_follower(force_member_change, @@ -1383,7 +1383,7 @@ handle_await_condition(#pre_vote_rpc{} = PreVote, State) -> handle_await_condition(election_timeout, #{cfg := #cfg{log_id = LogId}, membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: await_condition ignored election_timeout, non-voter: ~p", + ?DEBUG("~s: await_condition ignored election_timeout, replicate membership state: ~p", [LogId, Membership]), {await_condition, State, []}; handle_await_condition(election_timeout, State) -> From 01afd9b89182b96dcc45aa98d33a830255f20cf5 Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Tue, 7 May 2024 13:44:54 +0900 Subject: [PATCH 4/4] Remove controversial changes --- src/ra_server.erl | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/ra_server.erl b/src/ra_server.erl index 14b551b5..bfc53072 100644 --- a/src/ra_server.erl +++ b/src/ra_server.erl @@ -1261,23 +1261,11 @@ handle_follower(election_timeout, {follower, State, []}; handle_follower(election_timeout, State) -> call_for_election(pre_vote, State); -handle_follower(try_become_leader, - #{cfg := #cfg{log_id = LogId}, - membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: follower ignored try_become_leader, replicate membership state: ~p", - [LogId, Membership]), - {follower, State, []}; handle_follower(try_become_leader, State) -> call_for_election(pre_vote, State); handle_follower({register_external_log_reader, Pid}, #{log := Log0} = State) -> {Log, Effs} = ra_log:register_reader(Pid, Log0), {follower, State#{log => Log}, Effs}; -handle_follower(force_member_change, - #{cfg := #cfg{log_id = LogId}, - membership := Membership} = State) when Membership =/= voter -> - ?DEBUG("~s: follower ignored force_member_change, replicate membership state: ~p", - [LogId, Membership]), - {follower, State, []}; handle_follower(force_member_change, #{cfg := #cfg{id = Id, log_id = LogId}} = State0) ->