Skip to content

Refresh efile port if gone #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/bitcask.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2865,6 +2865,25 @@ corrupt_file(Path, Offset, Data) ->
ok = file:write(FH, Data),
file:close(FH).

% Verify that if the cached efile port goes away, we can recover
% and not get stuck opening casks
efile_error_test() ->
Dir = "/tmp/bc.efile.error",
B = bitcask:open(Dir, [read_write]),
ok = bitcask:put(B, <<"k">>, <<"v">>),
ok = bitcask:close(B),
Port = get(bitcask_efile_port),
% If this fails, we stopped using the efile port trick to list
% dir contents, so remove this test
?assert(is_port(Port)),
true = erlang:port_close(Port),
case bitcask:open(Dir) of
{error, _} = Err ->
?assertEqual(ok, Err);
B2 when is_reference(B2) ->
ok = bitcask:close(B2)
end.

%% About leak_t0():
%%
%% If bitcask leaks file descriptors for the 'touch'ed files, output is:
Expand Down
20 changes: 17 additions & 3 deletions src/bitcask_fileops.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
-include_lib("eqc/include/eqc_fsm.hrl").
-endif.
-compile(export_all).
-endif.
-include_lib("eunit/include/eunit.hrl").
-endif.

%% @doc Open a new file for writing.
%% Called on a Dirname, will open a fresh file in that directory.
Expand Down Expand Up @@ -854,9 +854,20 @@ ensure_dir(F) ->
end
end.

list_dir(Directory) ->
list_dir(Dir) ->
list_dir(Dir, 1).

list_dir(_, 0) ->
{error, efile_driver_unavailable};
list_dir(Directory, Retries) when is_integer(Retries), Retries > 0 ->
Port = get_efile_port(),
prim_file:list_dir(Port, Directory).
case prim_file:list_dir(Port, Directory) of
{error, einval} ->
clear_efile_port(),
list_dir(Directory, Retries-1);
Result ->
Result
end.

get_efile_port() ->
Key = bitcask_efile_port,
Expand All @@ -875,6 +886,9 @@ get_efile_port() ->
Port
end.

clear_efile_port() ->
erase(bitcask_efile_port).

prim_file_drv_open(Driver, Portopts) ->
try erlang:open_port({spawn, Driver}, Portopts) of
Port ->
Expand Down