Skip to content

Commit f3b9995

Browse files
committed
Increase performance of auto_delete queues deletion on channel down
Re-introduce previous delete guard for auto_delete queues. Undo performance enhancement introduced by #1513 for this use case. All other bulk deletions remain the same. [#159483364]
1 parent 7af8fd2 commit f3b9995

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/rabbit_amqqueue.erl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
%% internal
4848
-export([internal_declare/2, internal_delete/2, run_backing_queue/3,
4949
set_ram_duration_target/2, set_maximum_since_use/2,
50-
emit_consumers_local/3]).
50+
emit_consumers_local/3, internal_delete/3]).
5151

5252
-include("rabbit.hrl").
5353
-include_lib("stdlib/include/qlc.hrl").
@@ -969,21 +969,35 @@ notify_sent_queue_down(QPid) ->
969969
resume(QPid, ChPid) -> delegate:invoke_no_result(QPid, {gen_server2, cast, [{resume, ChPid}]}).
970970

971971
internal_delete1(QueueName, OnlyDurable) ->
972+
internal_delete1(QueueName, OnlyDurable, normal).
973+
974+
internal_delete1(QueueName, OnlyDurable, Reason) ->
972975
ok = mnesia:delete({rabbit_queue, QueueName}),
973-
mnesia:delete({rabbit_durable_queue, QueueName}),
976+
case Reason of
977+
auto_delete ->
978+
case mnesia:wread({rabbit_durable_queue, QueueName}) of
979+
[] -> ok;
980+
[_] -> ok = mnesia:delete({rabbit_durable_queue, QueueName})
981+
end;
982+
_ ->
983+
mnesia:delete({rabbit_durable_queue, QueueName})
984+
end,
974985
%% we want to execute some things, as decided by rabbit_exchange,
975986
%% after the transaction.
976987
rabbit_binding:remove_for_destination(QueueName, OnlyDurable).
977988

978989
internal_delete(QueueName, ActingUser) ->
990+
internal_delete(QueueName, ActingUser, normal).
991+
992+
internal_delete(QueueName, ActingUser, Reason) ->
979993
rabbit_misc:execute_mnesia_tx_with_tail(
980994
fun () ->
981995
case {mnesia:wread({rabbit_queue, QueueName}),
982996
mnesia:wread({rabbit_durable_queue, QueueName})} of
983997
{[], []} ->
984998
rabbit_misc:const({error, not_found});
985999
_ ->
986-
Deletions = internal_delete1(QueueName, false),
1000+
Deletions = internal_delete1(QueueName, false, Reason),
9871001
T = rabbit_binding:process_deletions(Deletions,
9881002
?INTERNAL_USER),
9891003
fun() ->

src/rabbit_amqqueue_process.erl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ terminate({shutdown, _} = R, State = #q{backing_queue = BQ}) ->
289289
terminate_shutdown(fun (BQS) -> BQ:terminate(R, BQS) end, State);
290290
terminate(normal, State) -> %% delete case
291291
terminate_shutdown(terminate_delete(true, normal, State), State);
292+
terminate(auto_delete, State) -> %% auto_delete case
293+
%% To increase performance we want to avoid a mnesia_sync:sync call
294+
%% after every transaction, as we could be deleting simultaneously
295+
%% thousands of queues. A optimisation introduced by server#1513
296+
%% needs to be reverted by this case, avoiding to guard the delete
297+
%% operation on `rabbit_durable_queue`
298+
terminate_shutdown(terminate_delete(true, auto_delete, State), State);
292299
%% If we crashed don't try to clean up the BQS, probably best to leave it.
293300
terminate(_Reason, State = #q{q = Q}) ->
294301
terminate_shutdown(fun (BQS) ->
@@ -300,12 +307,16 @@ terminate(_Reason, State = #q{q = Q}) ->
300307
BQS
301308
end, State).
302309

303-
terminate_delete(EmitStats, Reason,
310+
terminate_delete(EmitStats, Reason0,
304311
State = #q{q = #amqqueue{name = QName},
305312
backing_queue = BQ,
306313
status = Status}) ->
307314
ActingUser = terminated_by(Status),
308315
fun (BQS) ->
316+
Reason = case Reason0 of
317+
auto_delete -> normal;
318+
Any -> Any
319+
end,
309320
BQS1 = BQ:delete_and_terminate(Reason, BQS),
310321
if EmitStats -> rabbit_event:if_enabled(State, #q.stats_timer,
311322
fun() -> emit_stats(State) end);
@@ -315,7 +326,7 @@ terminate_delete(EmitStats, Reason,
315326
%% logged.
316327
try
317328
%% don't care if the internal delete doesn't return 'ok'.
318-
rabbit_amqqueue:internal_delete(QName, ActingUser)
329+
rabbit_amqqueue:internal_delete(QName, ActingUser, Reason0)
319330
catch
320331
{error, Reason} -> error(Reason)
321332
end,
@@ -1167,7 +1178,7 @@ handle_call({notify_down, ChPid}, _From, State) ->
11671178
%% gen_server2 *before* the reply is sent.
11681179
case handle_ch_down(ChPid, State) of
11691180
{ok, State1} -> reply(ok, State1);
1170-
{stop, State1} -> stop(ok, State1)
1181+
{stop, State1} -> {stop, auto_delete, ok, State1}
11711182
end;
11721183

11731184
handle_call({basic_get, ChPid, NoAck, LimiterPid}, _From,

0 commit comments

Comments
 (0)