Skip to content

Commit 9a965a5

Browse files
committed
Fix issue where merge attempts to sync deleted hint
wday-contrib 1702
1 parent f380840 commit 9a965a5

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

src/bitcask.app.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{application, bitcask, [
22
{description, "Yet another key/value storage engine"},
3-
{vsn, "2.1.0.3"},
3+
{vsn, "2.1.0.4"},
44
{modules, []},
55
{registered, []},
66
{applications, [kernel, stdlib]},

src/bitcask.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%% -------------------------------------------------------------------
22
%%
33
%% Copyright (c) 2010-2017 Basho Technologies, Inc.
4-
%% Copyright (c) 2018-2023 Workday, Inc.
4+
%% Copyright (c) 2018-2024 Workday, Inc.
55
%%
66
%% This file is provided to you under the Apache License,
77
%% Version 2.0 (the "License"); you may not use this file
@@ -3019,14 +3019,14 @@ no_pending_delete_bottleneck_test2() ->
30193019
[begin
30203020
?assertEqual(ok, bitcask:delete(B1, K))
30213021
end || {K, _} <- Data],
3022-
bitcask:merge(Dir1),
3022+
ok = bitcask:merge(Dir1),
30233023

30243024
bitcask:iterator(B2, -1, -1),
30253025
put_till_frozen(B2),
30263026
[begin
30273027
?assertEqual(ok, bitcask:delete(B2, K))
30283028
end || {K, _} <- Data],
3029-
bitcask:merge(Dir2),
3029+
ok = bitcask:merge(Dir2),
30303030
bitcask:iterator_release(B2),
30313031

30323032
%% Timing is everything. This test will timeout in 60 seconds.
@@ -3282,7 +3282,7 @@ leak_t1() ->
32823282
[bitcask:delete(Ref, <<X:32>>) || X <- lists:seq(1, DelKeys)],
32833283
io:format("After deleting ~p keys, lsof says: ~s", [DelKeys, Used()]),
32843284

3285-
bitcask:merge(Dir),
3285+
ok = bitcask:merge(Dir),
32863286
io:format("After merging, lsof says: ~s", [Used()]),
32873287

32883288
io:format("Q: Are all keys fetchable? ..."),
@@ -3618,7 +3618,7 @@ no_crash_on_key_transform_test() ->
36183618
default_dataset()),
36193619
bitcask:list_keys(B),
36203620
bitcask:fold(B, fun(K, _V, Acc0) -> [K|Acc0] end, [], -1, -1, true),
3621-
bitcask:merge(Dir, [{key_transform, CrashTx}]),
3621+
ok = bitcask:merge(Dir, [{key_transform, CrashTx}]),
36223622
ok = bitcask:close(B),
36233623
B2 = bitcask:open(Dir, [{key_transform, CrashTx}]),
36243624
ok = bitcask:close(B2),

src/bitcask_fileops.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%% -------------------------------------------------------------------
22
%%
33
%% Copyright (c) 2010-2017 Basho Technologies, Inc.
4-
%% Copyright (c) 2022-2023 Workday, Inc.
4+
%% Copyright (c) 2022-2024 Workday, Inc.
55
%%
66
%% This file is provided to you under the Apache License,
77
%% Version 2.0 (the "License"); you may not use this file
@@ -407,6 +407,8 @@ read(#filestate { fd = FD }, Offset, Size) ->
407407

408408
%% @doc Call the OS's fsync(2) system call on the cask and hint files.
409409
-spec sync(#filestate{}) -> ok.
410+
sync(#filestate { mode = read_write, fd = Fd, hintfd = undefined }) ->
411+
ok = bitcask_io:file_sync(Fd);
410412
sync(#filestate { mode = read_write, fd = Fd, hintfd = HintFd }) ->
411413
ok = bitcask_io:file_sync(Fd),
412414
ok = bitcask_io:file_sync(HintFd).

src/bitcask_merge_delete.erl

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
%% -------------------------------------------------------------------
22
%%
33
%% Copyright (c) 2012-2017 Basho Technologies, Inc.
4+
%% Copyright (c) 2024 Workday, Inc.
45
%%
56
%% This file is provided to you under the Apache License,
67
%% Version 2.0 (the "License"); you may not use this file
@@ -182,7 +183,8 @@ delete_files(Files) ->
182183
-ifdef(TEST).
183184

184185
multiple_merges_during_fold_test_() ->
185-
{timeout, 60, fun multiple_merges_during_fold_body/0}.
186+
[{timeout, 60, fun multiple_merges_during_fold_body/0},
187+
{timeout, 60, fun multiple_merges_during_fold_body_with_corrupt_hintfiles/0}].
186188

187189
multiple_merges_during_fold_body() ->
188190
Dir = filename:join(?TEST_FILEPATH, "bc.multiple-merges-fold"),
@@ -212,7 +214,7 @@ multiple_merges_during_fold_body() ->
212214
PutSome(),
213215
Count1 = merge_until(Dir, 0, CountSetuids),
214216
PutSome(),
215-
bitcask:merge(Dir),
217+
ok = bitcask:merge(Dir),
216218
PutSome(),
217219
merge_until(Dir, Count1, CountSetuids),
218220

@@ -223,8 +225,44 @@ multiple_merges_during_fold_body() ->
223225

224226
ok.
225227

228+
multiple_merges_during_fold_body_with_corrupt_hintfiles() ->
229+
Dir = filename:join(?TEST_FILEPATH, "bc.multiple-merges-fold-hintfile"),
230+
B = bitcask:open(Dir, [read_write, {max_file_size, 50}]),
231+
PutSome = fun() ->
232+
[bitcask:put(B, <<X:32>>, <<"yo this is a value">>) ||
233+
X <- lists:seq(1,5)]
234+
end,
235+
PutSome(),
236+
PutSome(),
237+
Bstuff = get(B),
238+
FoldFun = fun(_K, _V, 0) ->
239+
receive go_ahead -> ok end,
240+
1;
241+
(_K, _V, 1) ->
242+
1
243+
end,
244+
_SlowPid = spawn(fun() ->
245+
put(B, Bstuff),
246+
bitcask:fold(B, FoldFun, 0)
247+
end),
248+
CountSetuids = fun() ->
249+
Fs = filelib:wildcard(Dir ++ "/*"),
250+
length([F || F <- Fs,
251+
bitcask:has_pending_delete_bit(F)])
252+
end,
253+
PutSome(),
254+
_DeletedHints = [file:delete(H) || H <- filelib:wildcard(Dir ++ "/*.hint")],
255+
_Count1 = merge_until(Dir, 0, CountSetuids),
256+
PutSome(),
257+
ok = bitcask:merge(Dir),
258+
259+
ok = ?MODULE:testonly__delete_trigger(),
260+
0 = CountSetuids(),
261+
262+
ok.
263+
226264
merge_until(Dir, MinCount, CountSetuids) ->
227-
bitcask:merge(Dir),
265+
ok = bitcask:merge(Dir),
228266
Count = CountSetuids(),
229267
if (Count > MinCount) ->
230268
Count;
@@ -571,7 +609,7 @@ fork_merge(H, Dir) ->
571609
merge_these(_H, TestDir, Ids) ->
572610
Files = [lists:flatten(io_lib:format("~s/~w.bitcask.data", [TestDir,Id])) ||
573611
Id <- Ids],
574-
bitcask:merge(TestDir, [], Files).
612+
ok = bitcask:merge(TestDir, [], Files).
575613

576614
incr_clock() ->
577615
bitcask_time:test__incr_fudge(1).

0 commit comments

Comments
 (0)