/
bitcask_qc_expiry.erl
179 lines (146 loc) · 6.3 KB
/
bitcask_qc_expiry.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
%% -------------------------------------------------------------------
%%
%% bitcask: Eric Brewer-inspired key/value store
%%
%% Copyright (c) 2011 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.
%%
%% -------------------------------------------------------------------
-module(bitcask_qc_expiry).
-ifdef(EQC).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("bitcask.hrl").
-compile(export_all).
-define(QC_OUT(P),
eqc:on_output(fun(Str, Args) -> io:format(user, Str, Args) end, P)).
qc(P) ->
qc(P, 100).
qc(P, NumTests) ->
?assert(eqc:quickcheck(?QC_OUT(eqc:numtests(NumTests, P)))).
keys() ->
eqc_gen:non_empty(list(eqc_gen:non_empty(binary()))).
values() ->
eqc_gen:non_empty(list(binary())).
ops(Keys, Values) ->
{oneof([put, delete]), oneof(Keys), oneof(Values)}.
apply_kv_ops([], _Ref, KVs0) ->
KVs0;
apply_kv_ops([{put, K, V} | Rest], Ref, KVs0) ->
ok = bitcask:put(Ref, K, V),
apply_kv_ops(Rest, Ref, orddict:store({K, write_file(Ref)}, {V, current_tstamp()}, KVs0));
apply_kv_ops([{delete, K, _} | Rest], Ref, KVs0) ->
ok = bitcask:delete(Ref, K),
apply_kv_ops(Rest, Ref, orddict:store({K, write_file(Ref)}, {deleted, current_tstamp()}, KVs0)).
write_file(Ref) ->
%% Extract active write_file handle from Bitcask ref
element(3, erlang:get(Ref)).
current_tstamp() ->
case erlang:get(meck_tstamp) of
undefined ->
erlang:error(uninitialized_meck_tstamp);
Value ->
Value
end.
next_tstamp() ->
Ts = case erlang:get(meck_tstamp) of
undefined ->
1;
Tstamp ->
Tstamp + erlang:get(meck_tstamp_step)
end,
erlang:put(meck_tstamp, Ts),
Ts.
set_tstamp(Tstamp) ->
erlang:put(meck_tstamp, Tstamp).
set_tstamp_step(Step) ->
erlang:put(meck_tstamp_step, Step).
prop_expiry() ->
?LET({Keys, Values}, {keys(), values()},
?FORALL({Ops, Expiry, Timestep, M1},
{eqc_gen:non_empty(list(ops(Keys, Values))),
choose(1,10), choose(5, 50), choose(5,128)},
begin
Dirname = "/tmp/bc.prop.expiry",
?cmd("rm -rf " ++ Dirname),
%% Initialize how many ticks each operation will
%% increment the clock by
set_tstamp(undefined),
set_tstamp_step(Timestep),
Bref = bitcask:open(Dirname,
[read_write,
% {log_needs_merge, true},
{frag_merge_trigger, disabled},
{dead_bytes_merge_trigger, disabled},
{small_file_threshold, disabled},
{frag_threshold, disabled},
{expiry_secs, Expiry},
{max_file_size, M1}]),
try
%% Dump the ops into the bitcask and build a model of
%% what SHOULD be in the data.
Model = apply_kv_ops(Ops, Bref, []),
%% Close the writing file to ensure that it's included
%% in the needs_merge calculation
bitcask:close_write_file(Bref),
%% Identify items in the Model that should be expired
ExpireCutoff = erlang:max(current_tstamp() + Timestep - Expiry, -1),
{Expired, _Live} = lists:partition(fun({_K, {_Value, Tstamp}}) ->
Tstamp < ExpireCutoff
end, Model),
% io:format(user, "Cutoff: ~p\nExpired: ~120p\nLive: ~120p\n",
% [ExpireCutoff, Expired, Live]),
%% Check that needs_merge has expected result
case Expired of
[] ->
?assertEqual(false, bitcask:needs_merge(Bref));
_ ->
?assertMatch({true, _}, bitcask:needs_merge(Bref))
end
after
bitcask:close(Bref)
end,
true
end)).
prop_expiry_test_() ->
{timeout, 300*60, fun() ->
try
meck:new(bitcask_time, [passthrough]),
meck:expect(bitcask_time, tstamp, fun next_tstamp/0),
qc(prop_expiry(), 500)
after
meck:unload()
end
end}.
validate_expired([], _) ->
ok;
validate_expired([{_K, {deleted, _}} | Rest], Actual) ->
validate_expired(Rest, Actual);
validate_expired([{K, {Value, _Tstamp}} | Rest], Actual) ->
% ?debugFmt("~p (~p) should be expired; actual: ~p\n", [K, Tstamp, Actual]),
?assert(not lists:member({K, Value}, Actual)),
validate_expired(Rest, Actual).
validate_live([], _) ->
ok;
validate_live([{K, {deleted, _}} | Rest], Actual) ->
% ?debugFmt("~p should be deleted; actual: ~p\n", [K, Actual]),
?assert(not lists:keymember(K, 1, Actual)),
validate_live(Rest, Actual);
validate_live([{K, {Value, _}} | Rest], Actual) ->
% ?debugFmt("~p should have value ~p; actual: ~p\n", [K, Value, Actual]),
?assert(lists:member({K, Value}, Actual)),
validate_live(Rest, Actual).
-endif.