-
Notifications
You must be signed in to change notification settings - Fork 169
/
bitcask_fileops.erl
559 lines (500 loc) · 20.5 KB
/
bitcask_fileops.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
%% -------------------------------------------------------------------
%%
%% bitcask: Eric Brewer-inspired key/value store
%%
%% Copyright (c) 2007-2010 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
%% @doc Basic file i/o operations for bitcask.
-module(bitcask_fileops).
-author('Dave Smith <dizzyd@basho.com>').
-author('Justin Sheehy <justin@basho.com>').
-author('Andy Gross <andy@basho.com>').
-export([create_file/2,
open_file/1,
close/1,
close_for_writing/1,
data_file_tstamps/1,
write/4,
read/3,
sync/1,
delete/1,
fold/3,
fold_keys/3, fold_keys/4,
create_hintfile/1,
mk_filename/2,
filename/1,
hintfile_name/1,
file_tstamp/1,
check_write/4]).
-include_lib("kernel/include/file.hrl").
-include("bitcask.hrl").
-define(HINT_RECORD_SZ, 18). % Tstamp(4) + KeySz(2) + TotalSz(4) + Offset(8)
-ifdef(TEST).
-ifdef(EQC).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eqc/include/eqc_fsm.hrl").
-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.
-spec create_file(Dirname :: string(), Opts :: [any()]) -> {ok, #filestate{}}.
create_file(DirName, Opts) ->
create_file_loop(DirName, [create] ++ Opts, most_recent_tstamp(DirName) + 1).
%% @doc Open an existing file for reading.
%% Called with fully-qualified filename.
-spec open_file(Filename :: string()) -> {ok, #filestate{}} | {error, any()}.
open_file(Filename) ->
case bitcask_nifs:file_open(Filename, [readonly]) of
{ok, FD} ->
{ok, #filestate{mode = read_only,
filename = Filename, tstamp = file_tstamp(Filename),
fd = FD, ofs = 0 }};
{error, Reason} ->
{error, Reason}
end.
%% @doc Use when done writing a file. (never open for writing again)
-spec close(#filestate{} | fresh | undefined) -> ok.
close(fresh) -> ok;
close(undefined) -> ok;
close(#filestate{ fd = FD, hintfd = HintFd }) ->
bitcask_nifs:file_close(FD),
case HintFd of
undefined ->
ok;
_ ->
bitcask_nifs:file_close(HintFd)
end,
ok.
%% @doc Close a file for writing, but leave it open for reads.
-spec close_for_writing(#filestate{} | fresh | undefined) -> #filestate{} | ok.
close_for_writing(fresh) -> ok;
close_for_writing(undefined) -> ok;
close_for_writing(State =
#filestate{ mode = read_write, fd = Fd, hintfd = HintFd }) ->
bitcask_nifs:file_sync(Fd),
bitcask_nifs:file_sync(HintFd),
bitcask_nifs:file_close(HintFd),
State#filestate { mode = read_only, hintfd = undefined }.
%% Build a list of {tstamp, filename} for all files in the directory that
%% match our regex.
-spec data_file_tstamps(Dirname :: string()) -> [{integer(), string()}].
data_file_tstamps(Dirname) ->
filelib:fold_files(Dirname, "[0-9]+.bitcask.data", false,
fun(F, Acc) ->
[{file_tstamp(F), F} | Acc]
end, []).
%% @doc Use only after merging, to permanently delete a data file.
-spec delete(#filestate{}) -> ok | {error, atom()}.
delete(#filestate{ filename = FN } = State) ->
file:delete(FN),
case has_hintfile(State) of
true ->
file:delete(hintfile_name(State));
false ->
ok
end.
%% @doc Write a Key-named binary data field ("Value") to the Filestate.
-spec write(#filestate{},
Key :: binary(), Value :: binary(), Tstamp :: integer()) ->
{ok, #filestate{}, Offset :: integer(), Size :: integer()} |
{error, read_only}.
write(#filestate { mode = read_only }, _K, _V, _Tstamp) ->
{error, read_only};
write(Filestate=#filestate{fd = FD, hintfd = HintFD, ofs = Offset},
Key, Value, Tstamp) ->
KeySz = size(Key),
true = (KeySz =< ?MAXKEYSIZE),
ValueSz = size(Value),
true = (ValueSz =< ?MAXVALSIZE),
%% Setup io_list for writing -- avoid merging binaries if we can help it
Bytes0 = [<<Tstamp:?TSTAMPFIELD>>, <<KeySz:?KEYSIZEFIELD>>,
<<ValueSz:?VALSIZEFIELD>>, Key, Value],
Bytes = [<<(erlang:crc32(Bytes0)):?CRCSIZEFIELD>> | Bytes0],
%% Store the full entry in the data file
ok = bitcask_nifs:file_pwrite(FD, Offset, Bytes),
%% Create and store the corresponding hint entry
TotalSz = KeySz + ValueSz + ?HEADER_SIZE,
Iolist = hintfile_entry(Key, Tstamp, {Offset, TotalSz}),
ok = bitcask_nifs:file_write(HintFD, Iolist),
%% Record our final offset
TotalSz = iolist_size(Bytes),
{ok, Filestate#filestate{ofs = Offset + TotalSz}, Offset, TotalSz}.
%% @doc Given an Offset and Size, get the corresponding k/v from Filename.
-spec read(Filename :: string() | #filestate{}, Offset :: integer(),
Size :: integer()) ->
{ok, Key :: binary(), Value :: binary()} |
{error, bad_crc} | {error, atom()}.
read(Filename, Offset, Size) when is_list(Filename) ->
case open_file(Filename) of
{ok, Fstate} ->
read(Fstate, Offset, Size);
{error, Reason} ->
{error, Reason}
end;
read(#filestate { fd = FD }, Offset, Size) ->
case bitcask_nifs:file_pread(FD, Offset, Size) of
{ok, <<Crc32:?CRCSIZEFIELD/unsigned, Bytes/binary>>} ->
%% Unpack the actual data
<<_Tstamp:?TSTAMPFIELD, KeySz:?KEYSIZEFIELD, ValueSz:?VALSIZEFIELD,
Key:KeySz/bytes, Value:ValueSz/bytes>> = Bytes,
%% Verify the CRC of the data
case erlang:crc32(Bytes) of
Crc32 ->
{ok, Key, Value};
_BadCrc ->
{error, bad_crc}
end;
eof ->
{error, eof};
{error, Reason} ->
{error, Reason}
end.
%% @doc Call the OS's fsync(2) system call on the cask and hint files.
-spec sync(#filestate{}) -> ok.
sync(#filestate { mode = read_write, fd = Fd, hintfd = HintFd }) ->
ok = bitcask_nifs:file_sync(Fd),
ok = bitcask_nifs:file_sync(HintFd).
-spec fold(fresh | #filestate{},
fun((binary(), binary(), integer(),
{list(), integer(), integer(), integer()}, any()) -> any()),
any()) ->
any() | {error, any()}.
fold(fresh, _Fun, Acc) -> Acc;
fold(#filestate { fd=Fd, filename=Filename, tstamp=FTStamp }, Fun, Acc) ->
%% TODO: Add some sort of check that this is a read-only file
%% TODO: Need to position+read?!
ok = bitcask_nifs:file_seekbof(Fd),
case bitcask_nifs:file_read(Fd, ?HEADER_SIZE) of
{ok, <<_Crc:?CRCSIZEFIELD, _Tstamp:?TSTAMPFIELD, _KeySz:?KEYSIZEFIELD,
_ValueSz:?VALSIZEFIELD>> = H} ->
fold_loop(Fd, Filename, FTStamp, H, 0, Fun, Acc);
{ok, Bytes} ->
error_logger:error_msg("~s:fold: ~s: expected ~p bytes but got "
"only ~p bytes, skipping\n",
[?MODULE, Filename, ?HEADER_SIZE,
size(Bytes)]),
Acc;
eof ->
Acc;
{error, Reason} ->
{error, Reason}
end.
-spec fold_keys(fresh | #filestate{}, fun((binary(), integer(), {integer(), integer()}, any()) -> any()), any()) ->
any() | {error, any()}.
fold_keys(fresh, _Fun, Acc) -> Acc;
fold_keys(State, Fun, Acc) ->
fold_keys(State, Fun, Acc, default).
-spec fold_keys(fresh | #filestate{}, fun((binary(), integer(), {integer(), integer()}, any()) -> any()), any(), datafile | hintfile | default) ->
any() | {error, any()}.
fold_keys(#filestate { fd = Fd } = State, Fun, Acc, Mode) ->
case Mode of
datafile ->
fold_keys_loop(Fd, 0, Fun, Acc);
hintfile ->
fold_hintfile(State, Fun, Acc);
default ->
case has_hintfile(State) of
true ->
fold_hintfile(State, Fun, Acc);
false ->
fold_keys_loop(Fd, 0, Fun, Acc)
end
end.
-spec create_hintfile(string() | #filestate{}) -> ok | {error, any()}.
create_hintfile(Filename) when is_list(Filename) ->
case open_file(Filename) of
{ok, Fstate} ->
try
create_hintfile(Fstate)
after
close(Fstate)
end;
{error, Reason} ->
{error, Reason}
end;
create_hintfile(State) when is_record(State, filestate) ->
F = fun(K, Tstamp, {Offset, TotalSz}, HintFd) ->
Iolist = hintfile_entry(K, Tstamp, {Offset, TotalSz}),
case bitcask_nifs:file_write(HintFd, Iolist) of
ok ->
HintFd;
{error, Reason} ->
throw({error, Reason})
end
end,
generate_hintfile(hintfile_name(State),
{?MODULE, fold_keys_loop, [State#filestate.fd, 0, F]}).
-spec mk_filename(string(), integer()) -> string().
mk_filename(Dirname, Tstamp) ->
filename:join(Dirname,
lists:concat([integer_to_list(Tstamp),".bitcask.data"])).
-spec filename(#filestate{}) -> string().
filename(#filestate { filename = Fname }) ->
Fname.
-spec hintfile_name(string() | #filestate{}) -> string().
hintfile_name(Filename) when is_list(Filename) ->
filename:rootname(Filename, ".data") ++ ".hint";
hintfile_name(#filestate { filename = Fname }) ->
hintfile_name(Fname).
-spec file_tstamp(#filestate{} | string()) -> integer().
file_tstamp(#filestate{tstamp=Tstamp}) ->
Tstamp;
file_tstamp(Filename) when is_list(Filename) ->
list_to_integer(filename:basename(Filename, ".bitcask.data")).
-spec check_write(fresh | #filestate{}, binary(), binary(), integer()) ->
fresh | wrap | ok.
check_write(fresh, _Key, _Value, _MaxSize) ->
%% for the very first write, special-case
fresh;
check_write(#filestate { ofs = Offset }, Key, Value, MaxSize) ->
Size = ?HEADER_SIZE + size(Key) + size(Value),
case (Offset + Size) > MaxSize of
true ->
wrap;
false ->
ok
end.
has_hintfile(#filestate { filename = Fname }) ->
filelib:is_file(hintfile_name(Fname)).
%% ===================================================================
%% Internal functions
%% ===================================================================
fold_loop(Fd, Filename, FTStamp, Header, Offset, Fun, Acc0) ->
<<Crc32:?CRCSIZEFIELD, Tstamp:?TSTAMPFIELD, KeySz:?KEYSIZEFIELD,
ValueSz:?VALSIZEFIELD>> = Header,
<<_:4/binary, HeaderMinusCRC/binary>> = Header,
TotalSz = KeySz + ValueSz + ?HEADER_SIZE,
case bitcask_nifs:file_read(Fd, TotalSz) of
{ok, <<Key:KeySz/bytes, Value:ValueSz/bytes, Rest/binary>>} ->
case erlang:crc32([HeaderMinusCRC, Key, Value]) of
Crc32 ->
PosInfo = {Filename, FTStamp, Offset, TotalSz},
Acc = Fun(Key, Value, Tstamp, PosInfo, Acc0),
case Rest of
<<NextHeader:?HEADER_SIZE/bytes>> ->
fold_loop(Fd, Filename, FTStamp, NextHeader,
Offset + TotalSz, Fun, Acc);
<<>> ->
Acc;
Tail ->
error_logger:error_msg("Trailing data, discarding"
" (~p bytes)\n", [size(Tail)]),
Acc
end;
_ ->
{error, {bad_crc, Fd, Offset}}
end;
{ok, X} ->
error_logger:error_msg("Bad datafile entry, discarding"
"(~p/~p bytes)\n", [size(X),TotalSz]),
Acc0;
eof ->
error_logger:error_msg("Unexpected EOF, ignore (~p bytes)\n",
[TotalSz]),
Acc0;
{error, Reason} ->
{error, Reason}
end.
fold_keys_loop(Fd, Offset, Fun, Acc0) ->
case bitcask_nifs:file_pread(Fd, Offset, ?HEADER_SIZE) of
{ok, Header} when erlang:size(Header) =:= ?HEADER_SIZE ->
<<_Crc32:?CRCSIZEFIELD, Tstamp:?TSTAMPFIELD, KeySz:?KEYSIZEFIELD,
ValueSz:?VALSIZEFIELD>> = Header,
TotalSz = KeySz + ValueSz + ?HEADER_SIZE,
PosInfo = {Offset, TotalSz},
%% NOTE: We are intentionally *not* reading the value blob,
%% so we cannot check the checksum.
case bitcask_nifs:file_pread(Fd, Offset + ?HEADER_SIZE, KeySz) of
{ok, Key} when erlang:size(Key) =:= KeySz ->
Acc = Fun(Key, Tstamp, PosInfo, Acc0),
fold_keys_loop(Fd, Offset + TotalSz, Fun, Acc);
eof ->
Acc0;
{error, Reason} ->
{error, Reason};
X ->
error_logger:error_msg("Bad datafile entry 1: ~p\n", [X]),
Acc0
end;
eof ->
Acc0;
{error, Reason} ->
{error, Reason};
X ->
error_logger:error_msg("Bad datafile entry 2: ~p\n", [X]),
Acc0
end.
fold_hintfile(State, Fun, Acc) ->
case bitcask_nifs:file_open(hintfile_name(State), [readonly]) of
{ok, HintFd} ->
try
{ok, DataI} = file:read_file_info(State#filestate.filename),
DataSize = DataI#file_info.size,
case bitcask_nifs:file_read(HintFd, ?HINT_RECORD_SZ) of
{ok, <<H:?HINT_RECORD_SZ/bytes>>} ->
fold_hintfile_loop(DataSize, hintfile_name(State),
HintFd, H, Fun, Acc);
{ok, Bytes} ->
error_logger:error_msg("~s:fold_hintfile: ~s: expected "
"~p bytes but got only ~p "
"bytes, skipping\n",
[?MODULE, hintfile_name(State),
?HINT_RECORD_SZ, size(Bytes)]),
Acc;
eof ->
Acc;
{error, Reason} ->
{error, {fold_hintfile, Reason}}
end
after
bitcask_nifs:file_close(HintFd)
end;
{error, Reason} ->
{error, {fold_hintfile, Reason}}
end.
fold_hintfile_loop(DataSize, HintFile, Fd, HintRecord, Fun, Acc0) ->
<<Tstamp:?TSTAMPFIELD, KeySz:?KEYSIZEFIELD,
TotalSz:?TOTALSIZEFIELD, Offset:?OFFSETFIELD>> = HintRecord,
ReadSz = KeySz + ?HINT_RECORD_SZ,
case bitcask_nifs:file_read(Fd, ReadSz) of
{ok, <<Key:KeySz/bytes, Rest/binary>>} when
Offset + TotalSz =< DataSize ->
PosInfo = {Offset, TotalSz},
Acc = Fun(Key, Tstamp, PosInfo, Acc0),
case Rest of
<<NextRecord:?HINT_RECORD_SZ/bytes>> ->
fold_hintfile_loop(DataSize, HintFile,
Fd, NextRecord, Fun, Acc);
<<>> ->
Acc;
X ->
error_logger:error_msg("Bad hintfile data 1: ~p\n", [X]),
Acc
end;
{ok, _} ->
error_logger:error_msg("Hintfile '~s' contains pointer ~p ~p "
"that is greater than total data size ~p\n",
[HintFile, Offset, TotalSz, DataSize]),
Acc0;
eof ->
{error, incomplete_key};
{error, Reason} ->
{error, Reason}
end.
create_file_loop(DirName, Opts, Tstamp) ->
Filename = mk_filename(DirName, Tstamp),
ok = filelib:ensure_dir(Filename),
%% Check for o_sync strategy and add to opts
FinalOpts = case bitcask:get_opt(sync_strategy, Opts) of
o_sync ->
[o_sync | Opts];
_ ->
Opts
end,
case bitcask_nifs:file_open(Filename, FinalOpts) of
{ok, FD} ->
{ok, HintFD} = bitcask_nifs:file_open(hintfile_name(Filename), FinalOpts),
{ok, #filestate{mode = read_write,
filename = Filename,
tstamp = file_tstamp(Filename),
hintfd = HintFD, fd = FD, ofs = 0}};
{error, _} ->
%% Couldn't create a new file with the requested name,
%% check for the more recent timestamp and increment by
%% 1 and try again.
%% This introduces some drift into the actual creation
%% time, but given that we only have at most 2 writers
%% (writer + merger) for a given bitcask, it shouldn't be
%% more than a few seconds. The alternative it to sleep
%% until the next second rolls around -- but this
%% introduces lengthy, unnecessary delays.
%% This also handles the possibility of clock skew
MostRecentTS = most_recent_tstamp(DirName),
create_file_loop(DirName, Opts, MostRecentTS + 1)
end.
generate_hintfile(Filename, {FolderMod, FolderFn, FolderArgs}) ->
%% Create the temp file that we will write records out to.
{ok, Fd, TmpFilename} = temp_file(Filename ++ ".~w"),
%% Run the provided fold function over whatever the dataset is. The function
%% is passed the Fd as the accumulator argument, and must return the same
%% Fd on success. Otherwise, we expect an {error, Reason}.
try apply(FolderMod, FolderFn, FolderArgs ++ [Fd]) of
Fd ->
%% All done writing -- rename to the actual hintfile
ok = file:rename(TmpFilename, Filename);
{error, Reason} ->
{error, Reason}
after
file:delete(TmpFilename),
bitcask_nifs:file_close(Fd)
end.
hintfile_entry(Key, Tstamp, {Offset, TotalSz}) ->
KeySz = size(Key),
[<<Tstamp:?TSTAMPFIELD>>, <<KeySz:?KEYSIZEFIELD>>,
<<TotalSz:?TOTALSIZEFIELD>>, <<Offset:?OFFSETFIELD>>, Key].
%% Find the most recent timestamp for a .data file
most_recent_tstamp(DirName) ->
lists:foldl(fun({TS,_},Acc) ->
erlang:max(TS, Acc)
end, 0, data_file_tstamps(DirName)).
temp_file(Template) ->
temp_file(Template, now()).
temp_file(Template, Seed0) ->
{Int, Seed} = random:uniform_s(65536, Seed0),
Filename = ?FMT(Template, [Int]),
case bitcask_nifs:file_open(Filename, [create]) of
{ok, FD} ->
{ok, FD, Filename};
{error, eexists} ->
temp_file(Template, Seed)
end.
%% ===================================================================
%% EUnit tests
%% ===================================================================
-ifdef(TEST).
-ifdef(EQC).
-define(QC_OUT(P), eqc:on_output(fun(Str, Args) -> ?debugFmt(Str, Args) end, P)).
init_file(Dirname, Kvs) ->
os:cmd(?FMT("rm -rf ~s", [Dirname])),
{ok, F} = create_file(Dirname, []),
{ok, _} = write_kvs(Kvs, F, 0).
write_kvs([], F, _Tstamp) ->
{ok, F};
write_kvs([{K, V} | Rest], F, Tstamp) ->
{ok, F2, _, _} = write(F, K, V, Tstamp),
write_kvs(Rest, F2, Tstamp + 1).
key_value() ->
{eqc_gen:non_empty(binary()), binary()}.
prop_hintfile() ->
?FORALL(Kvs, eqc_gen:non_empty(list(key_value())),
begin
{ok, F} = init_file("/tmp/bc.fop.hintfile", Kvs),
ok = create_hintfile(F),
true = has_hintfile(F),
AccFn = fun(K, Tstamp, Pos, Acc0) ->
[{K, Tstamp, Pos} | Acc0]
end,
fold_keys(F, AccFn, [], datafile) ==
fold_keys(F, AccFn, [], hintfile)
end).
prop_hintfile_test_() ->
{timeout, 60, fun() -> ?assert(eqc:quickcheck(prop_hintfile())) end}.
-endif.
-endif.