From 9b658366978fb5644eb1a03584a9daa2b588238b Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 6 Dec 2024 15:20:21 +0700 Subject: [PATCH] Format and update docs for ra:transfer_leadership/2 --- src/ra.erl | 3 ++- src/ra_server.erl | 11 +++++++---- test/ra_SUITE.erl | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ra.erl b/src/ra.erl index 91e7124e..d0534491 100644 --- a/src/ra.erl +++ b/src/ra.erl @@ -1154,8 +1154,9 @@ initial_members(ServerId) -> initial_members(ServerId, Timeout) -> ra_server_proc:state_query(ServerId, initial_members, Timeout). -%% @doc Transfers leadership from the leader to a follower. +%% @doc Transfers leadership from the leader to a voter follower. %% Returns `already_leader' if the transfer target is already the leader. +%% Leadership cannot be transferred to non-voters. %% @end -spec transfer_leadership(ra_server_id(), ra_server_id()) -> ok | already_leader | {error, term()} | {timeout, ra_server_id()}. diff --git a/src/ra_server.erl b/src/ra_server.erl index 7532a7ce..877dd55f 100644 --- a/src/ra_server.erl +++ b/src/ra_server.erl @@ -830,11 +830,14 @@ handle_leader({transfer_leadership, ServerId}, #{cfg := #cfg{log_id = LogId}, cluster := Cluster} = State) -> case Cluster of - #{ServerId := #{voter_status := #{membership := Membership}}} when Membership =/= voter -> - ?DEBUG("~ts: transfer leadership requested but non-voter member ~w", [LogId, ServerId]), - {leader, State, [{reply, {error, non_voter_member}}]}; + #{ServerId := #{voter_status := #{membership := Membership}}} + when Membership =/= voter -> + ?INFO("~ts: transfer leadership requested but non-voter member ~w", + [LogId, ServerId]), + {leader, State, [{reply, {error, non_voter}}]}; _ -> - ?DEBUG("~ts: transfer leadership to ~w requested", [LogId, ServerId]), + ?DEBUG("~ts: transfer leadership to ~w requested", + [LogId, ServerId]), %% TODO find a timeout gen_statem:cast(ServerId, try_become_leader), {await_condition, diff --git a/test/ra_SUITE.erl b/test/ra_SUITE.erl index 83d1e55e..2d8cb621 100644 --- a/test/ra_SUITE.erl +++ b/test/ra_SUITE.erl @@ -1233,7 +1233,7 @@ transfer_leadership(Config) -> {ok, _, _} = ra:add_member(NewLeader, NonVoterMember), ok = ra:start_server(default, Name, NonVoterMember, add_machine(), Members), ct:pal("Transferring leadership from ~p to ~p", [NewLeader, NonVoterMemberId]), - ?assertEqual({error, non_voter_member}, ra:transfer_leadership(NewLeader, NonVoterMemberId)), + ?assertEqual({error, non_voter}, ra:transfer_leadership(NewLeader, NonVoterMemberId)), terminate_cluster([NonVoterMemberId | Members]). transfer_leadership_two_node(Config) ->