Skip to content

Commit e679155

Browse files
Merge pull request #9965 from cloudamqp/fix_dup_shovel
Fix handling shovels with old supervisor id format
2 parents c0551ea + 76f4ef1 commit e679155

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

deps/rabbitmq_shovel/src/rabbit_shovel_dyn_worker_sup_sup.erl

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@ start_child({VHost, ShovelName} = Name, Def) ->
4141
LockId = rabbit_shovel_locks:lock(Name),
4242
cleanup_specs(),
4343
rabbit_log_shovel:debug("Starting a mirrored supervisor named '~ts' in virtual host '~ts'", [ShovelName, VHost]),
44-
Result = case mirrored_supervisor:start_child(
44+
case child_exists(Name)
45+
orelse mirrored_supervisor:start_child(
4546
?SUPERVISOR,
4647
{id(Name), {rabbit_shovel_dyn_worker_sup, start_link, [Name, obfuscated_uris_parameters(Def)]},
4748
transient, ?WORKER_WAIT, worker, [rabbit_shovel_dyn_worker_sup]}) of
49+
true -> ok;
4850
{ok, _Pid} -> ok;
4951
{error, {already_started, _Pid}} -> ok
5052
end,
5153
%% release the lock if we managed to acquire one
5254
rabbit_shovel_locks:unlock(LockId),
53-
Result.
55+
ok.
5456

5557
obfuscated_uris_parameters(Def) when is_map(Def) ->
5658
to_map(rabbit_shovel_parameters:obfuscate_uris_in_definition(to_list(Def)));
@@ -70,8 +72,18 @@ stop_child({VHost, ShovelName} = Name) ->
7072
case get({shovel_worker_autodelete, Name}) of
7173
true -> ok; %% [1]
7274
_ ->
73-
ok = mirrored_supervisor:terminate_child(?SUPERVISOR, id(Name)),
74-
ok = mirrored_supervisor:delete_child(?SUPERVISOR, id(Name)),
75+
case mirrored_supervisor:terminate_child(?SUPERVISOR, id(Name)) of
76+
ok ->
77+
ok = mirrored_supervisor:delete_child(?SUPERVISOR, id(Name));
78+
{error, not_found} ->
79+
%% try older format, pre 3.13.0 and 3.12.8. See rabbitmq/rabbitmq-server#9894.
80+
case mirrored_supervisor:terminate_child(?SUPERVISOR, old_id(Name)) of
81+
ok ->
82+
ok = mirrored_supervisor:delete_child(?SUPERVISOR, old_id(Name));
83+
{error, not_found} ->
84+
ok
85+
end
86+
end,
7587
rabbit_shovel_status:remove(Name)
7688
end,
7789
rabbit_shovel_locks:unlock(LockId),
@@ -88,23 +100,26 @@ stop_child({VHost, ShovelName} = Name) ->
88100
cleanup_specs() ->
89101
Children = mirrored_supervisor:which_children(?SUPERVISOR),
90102

91-
%% older format, pre 3.13.0 and 3.12.8. See rabbitmq/rabbitmq-server#9894
92-
OldStyleSpecsSet = sets:from_list([element(1, S) || S <- Children]),
93-
NewStyleSpecsSet = sets:from_list([element(2, element(1, S)) || S <- Children]),
94-
ParamsSet = sets:from_list([ {proplists:get_value(vhost, S), proplists:get_value(name, S)}
95-
|| S <- rabbit_runtime_parameters:list_component(<<"shovel">>) ]),
96-
F = fun(Name, ok) ->
103+
SupIdSet = sets:from_list([element(1, S) || S <- Children]),
104+
ParamsSet = sets:from_list(
105+
lists:flatmap(
106+
fun(S) ->
107+
Name = {proplists:get_value(vhost, S), proplists:get_value(name, S)},
108+
%% Supervisor Id format was different pre 3.13.0 and 3.12.8.
109+
%% Try both formats to cover the transitionary mixed version cluster period.
110+
[id(Name), old_id(Name)]
111+
end,
112+
rabbit_runtime_parameters:list_component(<<"shovel">>))),
113+
F = fun(SupId, ok) ->
97114
try
98-
_ = mirrored_supervisor:delete_child(?SUPERVISOR, id(Name))
115+
_ = mirrored_supervisor:delete_child(?SUPERVISOR, SupId)
99116
catch _:_:_Stacktrace ->
100117
ok
101118
end,
102119
ok
103120
end,
104-
%% Try both formats to cover the transitionary mixed version cluster period.
105-
AllSpecs = sets:union(NewStyleSpecsSet, OldStyleSpecsSet),
106121
%% Delete any supervisor children that do not have their respective runtime parameters in the database.
107-
SetToCleanUp = sets:subtract(AllSpecs, ParamsSet),
122+
SetToCleanUp = sets:subtract(SupIdSet, ParamsSet),
108123
ok = sets:fold(F, ok, SetToCleanUp).
109124

110125
%%----------------------------------------------------------------------------
@@ -113,7 +128,8 @@ init([]) ->
113128
{ok, {{one_for_one, 3, 10}, []}}.
114129

115130
id({V, S} = Name) ->
116-
{[V, S], Name};
131+
{[V, S], Name}.
132+
117133
%% older format, pre 3.13.0 and 3.12.8. See rabbitmq/rabbitmq-server#9894
118-
id(Other) ->
119-
Other.
134+
old_id({_V, _S} = Name) ->
135+
Name.

0 commit comments

Comments
 (0)