Skip to content

Commit 76ee654

Browse files
author
Loïc Hoguin
committed
Keep fd open in msg store client single reads
Let's see if this helps performance of single reads.
1 parent af21e80 commit 76ee654

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

deps/rabbit/src/rabbit_msg_store.erl

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
-record(client_msstate,
138138
{ server,
139139
client_ref,
140+
current_file,
140141
index_state,
141142
index_module,
142143
dir,
@@ -182,6 +183,7 @@
182183
-type client_msstate() :: #client_msstate {
183184
server :: server(),
184185
client_ref :: client_ref(),
186+
current_file :: undefined | {non_neg_integer(), file:fd()},
185187
index_state :: any(),
186188
index_module :: atom(),
187189
%% Stored as binary() as opposed to file:filename() to save memory.
@@ -435,6 +437,7 @@ client_init(Server, Ref, MsgOnDiskFun) when is_pid(Server); is_atom(Server) ->
435437
?CREDIT_DISC_BOUND),
436438
#client_msstate { server = Server,
437439
client_ref = Ref,
440+
current_file = undefined,
438441
index_state = IState,
439442
index_module = IModule,
440443
dir = rabbit_file:filename_to_binary(Dir),
@@ -446,13 +449,21 @@ client_init(Server, Ref, MsgOnDiskFun) when is_pid(Server); is_atom(Server) ->
446449
-spec client_terminate(client_msstate()) -> 'ok'.
447450

448451
client_terminate(CState = #client_msstate { client_ref = Ref }) ->
449-
ok = server_call(CState, {client_terminate, Ref}).
452+
ok = server_call(CState, {client_terminate, Ref}),
453+
client_maybe_close_current_file(CState).
450454

451455
-spec client_delete_and_terminate(client_msstate()) -> 'ok'.
452456

453457
client_delete_and_terminate(CState = #client_msstate { client_ref = Ref }) ->
454458
ok = server_cast(CState, {client_dying, Ref}),
455-
ok = server_cast(CState, {client_delete, Ref}).
459+
ok = server_cast(CState, {client_delete, Ref}),
460+
client_maybe_close_current_file(CState).
461+
462+
client_maybe_close_current_file(#client_msstate{ current_file = CurrentFile }) ->
463+
case CurrentFile of
464+
undefined -> ok;
465+
{_File, Fd} -> ok = file:close(Fd)
466+
end.
456467

457468
-spec client_ref(client_msstate()) -> client_ref().
458469

@@ -1348,9 +1359,21 @@ write_message(MsgId, Msg,
13481359
current_file_offset = CurOffset + TotalSize }).
13491360

13501361
read_from_disk(#msg_location { msg_id = MsgId, file = File, offset = Offset,
1351-
total_size = TotalSize }, State = #client_msstate{ dir = Dir }) ->
1352-
{ok, Hdl} = file:open(form_filename(Dir, filenum_to_name(File)),
1353-
[binary, read, raw]),
1362+
total_size = TotalSize }, State = #client_msstate{ current_file = CurrentFile0, dir = Dir }) ->
1363+
{ok, Hdl} = case CurrentFile0 of
1364+
{File, Fd} ->
1365+
{ok, Fd};
1366+
{_AnotherFile, Fd} ->
1367+
ok = file:close(Fd),
1368+
file:open(form_filename(Dir, filenum_to_name(File)),
1369+
[binary, read, raw]);
1370+
undefined ->
1371+
file:open(form_filename(Dir, filenum_to_name(File)),
1372+
[binary, read, raw])
1373+
end,
1374+
1375+
% {ok, Hdl} = file:open(form_filename(Dir, filenum_to_name(File)),
1376+
% [binary, read, raw]),
13541377
{ok, {MsgId, Msg}} =
13551378
case rabbit_msg_file:pread(Hdl, Offset, TotalSize) of
13561379
{ok, {MsgId, _}} = Obj ->
@@ -1364,8 +1387,8 @@ read_from_disk(#msg_location { msg_id = MsgId, file = File, offset = Offset,
13641387
{proc_dict, get()}
13651388
]}}
13661389
end,
1367-
ok = file:close(Hdl),
1368-
{Msg, State}.
1390+
% ok = file:close(Hdl),
1391+
{Msg, State#client_msstate{ current_file = {File, Hdl}}}.
13691392

13701393
contains_message(MsgId, From, State) ->
13711394
MsgLocation = index_lookup_positive_ref_count(MsgId, State),

0 commit comments

Comments
 (0)