Skip to content

Commit

Permalink
Format and update docs for ra:transfer_leadership/2
Browse files Browse the repository at this point in the history
  • Loading branch information
anhanhnguyen committed Dec 6, 2024
1 parent 5270889 commit 9b65836
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/ra.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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()}.
Expand Down
11 changes: 7 additions & 4 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/ra_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down

0 comments on commit 9b65836

Please sign in to comment.