Skip to content

Fix extra tombstones on update #197

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 17, 2014
Merged
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
45 changes: 38 additions & 7 deletions src/bitcask.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1729,15 +1729,21 @@ do_put(Key, Value, #bc_state{write_file = WriteFile} = State,
State3 = wrap_write_file(State2),
do_put(Key, Value, State3, Retries - 1, already_exists);

#bitcask_entry{file_id=OldFileId,offset=OldOffset}
when OldFileId =< WriteFileId ->
PrevTombstone = <<?TOMBSTONE2_STR, OldFileId:32>>,
{ok, WriteFile1, _, _} =
bitcask_fileops:write(WriteFile0, Key, PrevTombstone,
Tstamp),
State3 = State2#bc_state{write_file = WriteFile1},
#bitcask_entry{file_id=OldFileId,offset=OldOffset} ->
State3 =
case OldFileId < WriteFileId of
true ->
PrevTomb = <<?TOMBSTONE2_STR, OldFileId:32>>,
{ok, WriteFile1, _, _} =
bitcask_fileops:write(WriteFile0, Key,
PrevTomb, Tstamp),
State2#bc_state{write_file = WriteFile1};
false ->
State2
end,
write_and_keydir_put(State3, Key, Value, Tstamp, Retries,
bitcask_time:tstamp(), OldFileId, OldOffset);

_ ->
State3 = State2#bc_state{write_file = WriteFile0},
write_and_keydir_put(State3, Key, Value, Tstamp, Retries,
Expand Down Expand Up @@ -3439,6 +3445,31 @@ legacy_tombstones_test2() ->
bitcask:close(B),
?assertEqual([Last], AllFiles3).

update_tombstones_test() ->
Dir = "/tmp/bc.update.tombstones",
Key = <<"k">>,
Data = [{Key, integer_to_binary(N)} || N <- lists:seq(1, 10)],
B = init_dataset(Dir, [read_write, {max_file_size, 50000000}], Data),
ok = bitcask:close(B),
% Re-open to guarantee opening a second file.
% An update on the new file requires a tombstone.
B2 = bitcask:open(Dir, [read_write, {max_file_size, 50000000}]),
ok = bitcask:put(B2, Key, <<"last_val">>),
ok = bitcask:close(B2),
Files = bitcask:readable_files(Dir),
Fds = [begin
{ok, Fd} = bitcask_fileops:open_file(File),
Fd
end || File <- Files],
CountF = fun(_K, V, _Tstamp, _, Acc) ->
case bitcask:is_tombstone(V) of
true -> Acc + 1;
false -> Acc
end
end,
TombCount = bitcask:subfold(CountF, Fds, 0),
?assertEqual(1, TombCount).

make_merge_file(Dir, Seed, Probability) ->
random:seed(Seed),
case filelib:is_dir(Dir) of
Expand Down