Skip to content
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

Adds "grace period" to stop just-written files from expiring. #54

Merged
merged 6 commits into from Sep 13, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion rebar.config
Expand Up @@ -30,4 +30,7 @@
% {d, 'PULSE'},
% {pulse_side_effect, [{bitcask, get_filestate, '_'},
% {bitcask_nifs, keydir_get, '_'}]}
]}.

]}.

{eunit_opts, [verbose]}.
38 changes: 29 additions & 9 deletions src/bitcask.erl
Expand Up @@ -73,6 +73,7 @@
hint_keydir,
del_keydir,
expiry_time,
expiry_grace_time,
opts }).

%% A bitcask is a directory containing:
Expand Down Expand Up @@ -503,6 +504,7 @@ merge1(Dirname, Opts, FilesToMerge) ->
live_keydir = LiveKeyDir,
del_keydir = DelKeyDir,
expiry_time = expiry_time(Opts),
expiry_grace_time = expiry_grace_time(Opts),
opts = Opts },

%% Finally, start the merge process
Expand All @@ -523,6 +525,16 @@ merge1(Dirname, Opts, FilesToMerge) ->
end || F <- State#mstate.input_files],
ok = bitcask_lockops:release(Lock).

%% @doc Predicate which determines whether or not a file should be considered for a merge.
consider_for_merge(FragTrigger, DeadBytesTrigger, ExpirationGraceTime) ->
fun (F) ->
(F#file_status.fragmented >= FragTrigger)
orelse (F#file_status.dead_bytes >= DeadBytesTrigger)
orelse ( (F#file_status.oldest_tstamp > 0) %% means that the file has data
andalso (F#file_status.oldest_tstamp < ExpirationGraceTime)
)
end.

-spec needs_merge(reference()) -> {true, [string()]} | false.
needs_merge(Ref) ->
State = get_state(Ref),
Expand All @@ -547,18 +559,18 @@ needs_merge(Ref) ->
%%
%% frag_merge_trigger - Any file exceeds this % fragmentation
%% dead_bytes_merge_trigger - Any file has more than this # of dead bytes
%% expiry_secs - Any file has an expired key
%% expiry_time - Any file has an expired key
%% expiry_grace_time - avoid expiring in the case of continuous writes
%%
FragTrigger = get_opt(frag_merge_trigger, State#bc_state.opts),
DeadBytesTrigger = get_opt(dead_bytes_merge_trigger, State#bc_state.opts),
ExpirationTime = expiry_time(State#bc_state.opts),

NeedsMerge = lists:any(fun(F) ->
(F#file_status.fragmented >= FragTrigger)
or (F#file_status.dead_bytes >= DeadBytesTrigger)
or (F#file_status.oldest_tstamp < ExpirationTime)
end, Summary),
ExpirationTime =
max(expiry_time(State#bc_state.opts), 0),
ExpirationGraceTime =
max(expiry_time(State#bc_state.opts) - expiry_grace_time(State#bc_state.opts), 0),

NeedsMerge = lists:any(consider_for_merge(FragTrigger, DeadBytesTrigger, ExpirationGraceTime),
Summary),
case NeedsMerge of
true ->
%% Build a list of threshold checks; a file which meets ANY
Expand Down Expand Up @@ -711,6 +723,15 @@ expiry_time(Opts) ->
false -> 0
end.

to_lower_grace_time_bound(undefined) -> 0;
to_lower_grace_time_bound(X) ->
case X > 0 of
true -> X;
false -> 0
end.

expiry_grace_time(Opts) -> to_lower_grace_time_bound(get_opt(expiry_grace_time, Opts)).

start_app() ->
case application:start(?MODULE) of
ok ->
Expand Down Expand Up @@ -1641,4 +1662,3 @@ truncate_file(Path, Offset) ->
file:close(FH).

-endif.

8 changes: 5 additions & 3 deletions test/bitcask_qc_expiry.erl
Expand Up @@ -86,9 +86,9 @@ set_tstamp_step(Step) ->

prop_expiry() ->
?LET({Keys, Values}, {keys(), values()},
?FORALL({Ops, Expiry, Timestep, M1},
?FORALL({Ops, Expiry, ExpiryGrace, Timestep, M1},
{eqc_gen:non_empty(list(ops(Keys, Values))),
choose(1,10), choose(5, 50), choose(5,128)},
choose(1,10), choose(1, 10), choose(5, 50), choose(5,128)},
begin
Dirname = "/tmp/bc.prop.expiry",
?cmd("rm -rf " ++ Dirname),
Expand All @@ -106,6 +106,7 @@ prop_expiry() ->
{small_file_threshold, disabled},
{frag_threshold, disabled},
{expiry_secs, Expiry},
{expiry_grace_secs, ExpiryGrace},
{max_file_size, M1}]),

try
Expand All @@ -118,7 +119,7 @@ prop_expiry() ->
bitcask:close_write_file(Bref),

%% Identify items in the Model that should be expired
ExpireCutoff = erlang:max(current_tstamp() + Timestep - Expiry, -1),
ExpireCutoff = erlang:max(current_tstamp() + Timestep - erlang:max(Expiry - ExpiryGrace, 0), -1),

{Expired, _Live} = lists:partition(fun({_K, {_Value, Tstamp}}) ->
Tstamp < ExpireCutoff
Expand All @@ -133,6 +134,7 @@ prop_expiry() ->
_ ->
?assertMatch({true, _}, bitcask:needs_merge(Bref))
end
catch X:Y -> io:format(user, "exception: ~p ~p @ ~p\n", [X,Y, erlang:get_stacktrace()])
after
bitcask:close(Bref)
end,
Expand Down