Skip to content

Commit

Permalink
Mirrored supervisor: more logging at debug level
Browse files Browse the repository at this point in the history
(cherry picked from commit 4f3d5e5)
  • Loading branch information
michaelklishin authored and mergify-bot committed Aug 5, 2021
1 parent 84597a2 commit 3d4f9c1
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions deps/rabbit_common/src/mirrored_supervisor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,20 @@ handle_call({start_child, ChildSpec}, _From,
delegate = Delegate,
group = Group,
tx_fun = TxFun}) ->
rabbit_log:debug("Mirrored supervisor: asked to consider starting a child, group: ~p", [Group]),
{reply, case maybe_start(Group, TxFun, Overall, Delegate, ChildSpec) of
already_in_mnesia -> {error, already_present};
{already_in_mnesia, Pid} -> {error, {already_started, Pid}};
Else -> Else
already_in_mnesia ->
rabbit_log:debug("Mirrored supervisor: maybe_start for group ~p,"
" overall ~p returned 'record already present'", [Group, Overall]),
{error, already_present};
{already_in_mnesia, Pid} ->
rabbit_log:debug("Mirrored supervisor: maybe_start for group ~p,"
" overall ~p returned 'already running: ~p'", [Group, Overall, Pid]),
{error, {already_started, Pid}};
Else ->
rabbit_log:debug("Mirrored supervisor: maybe_start for group ~p,"
" overall ~p returned ~p", [Group, Overall, Else]),
Else
end, State};

handle_call({delete_child, Id}, _From, State = #state{delegate = Delegate,
Expand Down Expand Up @@ -384,17 +394,31 @@ tell_all_peers_to_die(Group, Reason) ->
[cast(P, {die, Reason}) || P <- pg:get_members(Group) -- [self()]].

maybe_start(Group, TxFun, Overall, Delegate, ChildSpec) ->
rabbit_log:debug("Mirrored supervisor: asked to consider starting, group: ~p", [Group]),
try TxFun(fun() -> check_start(Group, Overall, Delegate, ChildSpec) end) of
start -> start(Delegate, ChildSpec);
undefined -> already_in_mnesia;
Pid -> {already_in_mnesia, Pid}
start ->
rabbit_log:debug("Mirrored supervisor: check_start for group ~p,"
" overall ~p returned 'do start'", [Group, Overall]),
start(Delegate, ChildSpec);
undefined ->
rabbit_log:debug("Mirrored supervisor: check_start for group ~p,"
" overall ~p returned 'undefined'", [Group, Overall]),
already_in_mnesia;
Pid ->
rabbit_log:debug("Mirrored supervisor: check_start for group ~p,"
" overall ~p returned 'already running (~p)'", [Group, Overall, Pid]),
{already_in_mnesia, Pid}
catch
%% If we are torn down while in the transaction...
{error, E} -> {error, E}
end.

check_start(Group, Overall, Delegate, ChildSpec) ->
case mnesia:wread({?TABLE, {Group, id(ChildSpec)}}) of
rabbit_log:debug("Mirrored supervisor: check_start for group ~p, id: ~p", [Group, id(ChildSpec)]),
ReadResult = mnesia:wread({?TABLE, {Group, id(ChildSpec)}}),
rabbit_log:debug("Mirrored supervisor: check_start table ~s read for key ~p returned ~p",
[?TABLE, {Group, id(ChildSpec)}, ReadResult]),
case ReadResult of
[] -> _ = write(Group, Overall, ChildSpec),
start;
[S] -> #mirrored_sup_childspec{key = {Group, Id},
Expand Down Expand Up @@ -427,6 +451,7 @@ start(Delegate, ChildSpec) ->
apply(?SUPERVISOR, start_child, [Delegate, ChildSpec]).

stop(Group, TxFun, Delegate, Id) ->
rabbit_log:debug("Mirrored supervisor: asked to stop, group: ~p, child ID: ~p", [Group, Id]),
try TxFun(fun() -> check_stop(Group, Delegate, Id) end) of
deleted -> apply(?SUPERVISOR, delete_child, [Delegate, Id]);
running -> {error, running}
Expand Down

0 comments on commit 3d4f9c1

Please sign in to comment.