Skip to content

Commit

Permalink
Merge pull request #9919 from rabbitmq/mergify/bp/v3.12.x/pr-9909
Browse files Browse the repository at this point in the history
Dynamic Shovels: support old (pre-3.13.0, 3.12.8) and new supervisor child ID formats (backport #9909)
  • Loading branch information
michaelklishin committed Nov 15, 2023
2 parents a7e6c60 + f0844ce commit da160cf
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions deps/rabbitmq_shovel/src/rabbit_shovel_dyn_worker_sup_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ obfuscated_uris_parameters(Def) when is_list(Def) ->
rabbit_shovel_parameters:obfuscate_uris_in_definition(Def).

child_exists(Name) ->
lists:any(fun ({{_, N}, _, _, _}) -> N =:= Name end,
lists:any(fun ({{_, N}, _, _, _}) -> N =:= Name;
%% older format, pre 3.13.0 and 3.12.8. See rabbitmq/rabbitmq-server#9894.
({N, _, _, _}) -> N =:= Name
end,
mirrored_supervisor:which_children(?SUPERVISOR)).

stop_child({VHost, ShovelName} = Name) ->
Expand All @@ -83,19 +86,34 @@ stop_child({VHost, ShovelName} = Name) ->
%% See rabbit_shovel_worker:terminate/2

cleanup_specs() ->
SpecsSet = sets:from_list([element(2, element(1, S)) || S <- mirrored_supervisor:which_children(?SUPERVISOR)]),
Children = mirrored_supervisor:which_children(?SUPERVISOR),

%% older format, pre 3.13.0 and 3.12.8. See rabbitmq/rabbitmq-server#9894
OldStyleSpecsSet = sets:from_list([element(1, S) || S <- Children]),
NewStyleSpecsSet = sets:from_list([element(2, element(1, S)) || S <- Children]),
ParamsSet = sets:from_list([ {proplists:get_value(vhost, S), proplists:get_value(name, S)}
|| S <- rabbit_runtime_parameters:list_component(<<"shovel">>) ]),
F = fun(Name, ok) ->
_ = mirrored_supervisor:delete_child(?SUPERVISOR, id(Name)),
try
_ = mirrored_supervisor:delete_child(?SUPERVISOR, id(Name))
catch _:_:_Stacktrace ->
ok
end,
ok
end,
ok = sets:fold(F, ok, sets:subtract(SpecsSet, ParamsSet)).
%% Try both formats to cover the transitionary mixed version cluster period.
AllSpecs = sets:union(NewStyleSpecsSet, OldStyleSpecsSet),
%% Delete any supervisor children that do not have their respective runtime parameters in the database.
SetToCleanUp = sets:subtract(AllSpecs, ParamsSet),
ok = sets:fold(F, ok, SetToCleanUp).

%%----------------------------------------------------------------------------

init([]) ->
{ok, {{one_for_one, 3, 10}, []}}.

id({V, S} = Name) ->
{[V, S], Name}.
{[V, S], Name};
%% older format, pre 3.13.0 and 3.12.8. See rabbitmq/rabbitmq-server#9894
id(Other) ->
Other.

0 comments on commit da160cf

Please sign in to comment.