Skip to content

Commit f90c9d3

Browse files
committed
Notify of runtime parameter deletion when deleting a vhost
1 parent d8fcfc6 commit f90c9d3

File tree

5 files changed

+90
-8
lines changed

5 files changed

+90
-8
lines changed

deps/rabbit/src/rabbit_db_rtparams.erl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
get/1,
1515
get_or_set/2,
1616
get_all/0, get_all/2,
17-
delete/1, delete/3]).
17+
delete/1, delete/3,
18+
delete_vhost/1]).
1819

1920
-export([khepri_vhost_rp_path/3,
2021
khepri_global_rp_path/1,
@@ -340,6 +341,55 @@ delete_matching_in_khepri(VHostName, Comp, Name) ->
340341
Key = {?any(VHostName), ?any(Comp), ?any(Name)},
341342
delete_in_khepri(Key).
342343

344+
%% -------------------------------------------------------------------
345+
%% delete_vhost().
346+
%% -------------------------------------------------------------------
347+
348+
-spec delete_vhost(VHostName) -> Ret when
349+
VHostName :: vhost:name(),
350+
Ret :: {ok, Deletions} | {error, Reason :: any()},
351+
Deletions :: [#runtime_parameters{}].
352+
%% @doc Deletes all runtime parameters belonging to the given virtual host.
353+
%%
354+
%% @returns an OK tuple containing the deleted runtime parameters if
355+
%% successful, or an error tuple otherwise.
356+
%%
357+
%% @private
358+
359+
delete_vhost(VHostName) when is_binary(VHostName) ->
360+
rabbit_khepri:handle_fallback(
361+
#{mnesia => fun() -> delete_vhost_in_mnesia(VHostName) end,
362+
khepri => fun() -> delete_vhost_in_khepri(VHostName) end}).
363+
364+
delete_vhost_in_mnesia(VHostName) ->
365+
rabbit_mnesia:execute_mnesia_transaction(
366+
fun() ->
367+
Deletions = delete_vhost_in_mnesia_tx(VHostName),
368+
{ok, Deletions}
369+
end).
370+
371+
delete_vhost_in_mnesia_tx(VHostName) ->
372+
Match = #runtime_parameters{key = {VHostName, '_', '_'},
373+
_ = '_'},
374+
[begin
375+
mnesia:delete(?MNESIA_TABLE, Key, write),
376+
Record
377+
end
378+
|| #runtime_parameters{key = Key} = Record
379+
<- mnesia:match_object(?MNESIA_TABLE, Match, read)].
380+
381+
delete_vhost_in_khepri(VHostName) ->
382+
Path = khepri_vhost_rp_path(
383+
VHostName, ?KHEPRI_WILDCARD_STAR, ?KHEPRI_WILDCARD_STAR),
384+
case rabbit_khepri:adv_delete_many(Path) of
385+
{ok, Props} ->
386+
{ok, rabbit_khepri:collect_payloads(Props)};
387+
{error, _} = Err ->
388+
Err
389+
end.
390+
391+
%% -------------------------------------------------------------------
392+
343393
khepri_rp_path() ->
344394
[?MODULE].
345395

deps/rabbit/src/rabbit_khepri.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
clear_payload/1,
134134
delete/1, delete/2,
135135
delete_or_fail/1,
136+
adv_delete_many/1,
136137

137138
transaction/1,
138139
transaction/2,
@@ -950,6 +951,9 @@ delete_or_fail(Path) ->
950951
Error
951952
end.
952953

954+
adv_delete_many(Path) ->
955+
khepri_adv:delete_many(?STORE_ID, Path, ?DEFAULT_COMMAND_OPTIONS).
956+
953957
put(PathPattern, Data) ->
954958
khepri:put(
955959
?STORE_ID, PathPattern, Data, ?DEFAULT_COMMAND_OPTIONS).

deps/rabbit/src/rabbit_runtime_parameters.erl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,28 @@ clear_global(Key, ActingUser) ->
203203
{user_who_performed_action, ActingUser}])
204204
end.
205205

206-
clear_vhost(VHostName, _ActingUser) when is_binary(VHostName) ->
207-
ok = rabbit_db_rtparams:delete(VHostName, '_', '_').
206+
clear_vhost(VHostName, ActingUser) when is_binary(VHostName) ->
207+
case rabbit_db_rtparams:delete_vhost(VHostName) of
208+
{ok, DeletedParams} ->
209+
lists:foreach(
210+
fun(#runtime_parameters{key = {_VHost, Component, Name}}) ->
211+
case lookup_component(Component) of
212+
{ok, Mod} ->
213+
event_notify(
214+
parameter_cleared, VHostName, Component,
215+
[{name, Name},
216+
{user_who_performed_action, ActingUser}]),
217+
Mod:notify_clear(
218+
VHostName, Component, Name, ActingUser),
219+
ok;
220+
_ ->
221+
ok
222+
end
223+
end, DeletedParams),
224+
ok;
225+
{error, _} = Err ->
226+
Err
227+
end.
208228

209229
clear_component(<<"policy">>, _) ->
210230
{error_string, "policies may not be cleared using this method"};

deps/rabbit/src/rabbit_vhost.erl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ delete(VHost, ActingUser) ->
284284
#exchange{name = Name} <- rabbit_exchange:list(VHost)],
285285
rabbit_log:info("Clearing policies and runtime parameters in vhost '~ts' because it's being deleted", [VHost]),
286286
_ = rabbit_runtime_parameters:clear_vhost(VHost, ActingUser),
287-
_ = [rabbit_policy:delete(VHost, proplists:get_value(name, Info), ActingUser)
288-
|| Info <- rabbit_policy:list(VHost)],
289287
rabbit_log:debug("Removing vhost '~ts' from the metadata storage because it's being deleted", [VHost]),
290288
case rabbit_db_vhost:delete(VHost) of
291289
true ->

deps/rabbit/test/vhost_SUITE.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,19 @@ vhost_deletion(Config) ->
456456
false
457457
end, Events))),
458458

459-
%% TODO: parameter_cleared and vhost_limits_cleared
460-
%% ?assertMatch(#{name := PolicyName, vhost := VHost},
461-
%% proplists:get_value(policy_cleared, Events)),
459+
?assertMatch(
460+
{value, {parameter_cleared, #{name := <<"limits">>,
461+
vhost := VHost}}},
462+
lists:search(
463+
fun ({parameter_cleared, #{component := <<"vhost-limits">>}}) ->
464+
true;
465+
(_Event) ->
466+
false
467+
end, Events)),
468+
?assertMatch(#{name := <<"limits">>, vhost := VHost},
469+
proplists:get_value(vhost_limits_cleared, Events)),
470+
?assertMatch(#{name := PolicyName, vhost := VHost},
471+
proplists:get_value(policy_cleared, Events)),
462472

463473
?assertMatch(#{name := VHost,
464474
user_who_performed_action := ActingUser},

0 commit comments

Comments
 (0)