Skip to content

Commit 4da170f

Browse files
authored
Faster node start with many classic queues v2 (#7676)
* Faster all_queue_directory_names/1 * Optimise writing stub files Combined, this reduces node startup time by half with many empty classic queues v2
1 parent b4871e2 commit 4da170f

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

deps/rabbit/src/rabbit_classic_queue_index_v2.erl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ init1(Name, Dir, OnSyncFun, OnSyncMsgFun) ->
205205

206206
ensure_queue_name_stub_file(#resource{virtual_host = VHost, name = QName}, Dir) ->
207207
QueueNameFile = filename:join(Dir, ?QUEUE_NAME_STUB_FILE),
208-
ok = filelib:ensure_dir(QueueNameFile),
209-
ok = file:write_file(QueueNameFile, <<"VHOST: ", VHost/binary, "\n",
208+
ok = write_file_and_ensure_dir(QueueNameFile, <<"VHOST: ", VHost/binary, "\n",
210209
"QUEUE: ", QName/binary, "\n",
211210
"INDEX: v2\n">>).
212211

@@ -1292,3 +1291,14 @@ highest_continuous_seq_id([SeqId1, SeqId2|Tail], EndSeqId)
12921291
highest_continuous_seq_id([SeqId2|Tail], EndSeqId);
12931292
highest_continuous_seq_id([SeqId|Tail], _) ->
12941293
{SeqId, Tail}.
1294+
1295+
write_file_and_ensure_dir(Name, IOData) ->
1296+
case file:write_file(Name, IOData, [raw]) of
1297+
ok -> ok;
1298+
{error, enoent} ->
1299+
case filelib:ensure_dir(Name) of
1300+
ok -> file:write_file(Name, IOData, [raw]);
1301+
Err -> Err
1302+
end;
1303+
Err -> Err
1304+
end.

deps/rabbit/src/rabbit_queue_index.erl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,12 @@ start(VHost, DurableQueueNames) ->
549549
sets:add_element(DirName, ValidDirectories)}
550550
end, {[], sets:new()}, DurableQueueNames),
551551
%% Any queue directory we've not been asked to recover is considered garbage
552-
_ = rabbit_file:recursive_delete(
553-
[DirName ||
554-
DirName <- all_queue_directory_names(VHost),
555-
not sets:is_element(filename:basename(DirName), DurableDirectories)]),
552+
ToDelete = [filename:join([rabbit_vhost:msg_store_dir_path(VHost), "queues", Dir])
553+
|| Dir <- lists:subtract(all_queue_directory_names(VHost),
554+
sets:to_list(DurableDirectories))],
555+
rabbit_log:debug("Deleting unknown files/folders: ~p", [ToDelete]),
556+
_ = rabbit_file:recursive_delete(ToDelete),
557+
556558
rabbit_recovery_terms:clear(VHost),
557559

558560
%% The backing queue interface requires that the queue recovery terms
@@ -564,8 +566,13 @@ start(VHost, DurableQueueNames) ->
564566
stop(VHost) -> rabbit_recovery_terms:stop(VHost).
565567

566568
all_queue_directory_names(VHost) ->
567-
filelib:wildcard(filename:join([rabbit_vhost:msg_store_dir_path(VHost),
568-
"queues", "*"])).
569+
VHostQueuesPath = filename:join([rabbit_vhost:msg_store_dir_path(VHost), "queues"]),
570+
case filelib:is_dir(VHostQueuesPath) of
571+
true ->
572+
{ok, Dirs} = file:list_dir(VHostQueuesPath),
573+
Dirs;
574+
false -> []
575+
end.
569576

570577
%%----------------------------------------------------------------------------
571578
%% startup and shutdown

0 commit comments

Comments
 (0)