Skip to content

Commit 518d9db

Browse files
BenHuddlestonPeter-Searby
authored andcommitted
MB-67857[BP]: Enhance fake_chronicle_kv with seqno support for
... more tests. chronicle_metakv uses seqnos to detect CAS issues. Rather than meck a chunk of chronicle_kv in those test, we can enhance fake_chronicle_kv with seqno support and use this. Additionally add a few more mecks that will be required in future changes. Change-Id: Ie109e85c2a90ca114260918689748f899a68adf6 Reviewed-on: https://review.couchbase.org/c/ns_server/+/231992 Tested-by: Peter Searby <[email protected]> Well-Formed: Restriction Checker Reviewed-by: Ben Huddleston <[email protected]> Well-Formed: Build Bot <[email protected]>
1 parent ba549c9 commit 518d9db

File tree

1 file changed

+60
-12
lines changed

1 file changed

+60
-12
lines changed

test/fake_helpers/fake_chronicle_kv.erl

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
-export([setup_cluster_compat_version/1]).
5151

5252
-define(TABLE_NAME, fake_chronicle_kv).
53+
-define(FAKE_HISTORY_REV, <<"fake">>).
5354

5455
%% --------------------
5556
%% API - Setup/Teardown
@@ -122,12 +123,25 @@ meck_setup_chronicle_kv_getters() ->
122123
end),
123124

124125

126+
meck:expect(chronicle_kv, ro_txn,
127+
fun(_Name, Fun) ->
128+
{ok,
129+
{Fun(get_ets_snapshot()),
130+
make_rev(get_snapshot_seqno())}}
131+
end),
125132
meck:expect(chronicle_kv, ro_txn,
126133
fun(_Name, _Fun, _Opts) ->
127-
{ok, {get_ets_snapshot(), no_rev}}
134+
{ok,
135+
{get_ets_snapshot(),
136+
make_rev(get_snapshot_seqno())}}
128137
end),
129138

130139

140+
meck:expect(chronicle_kv, txn_get,
141+
fun(Key, Txn) ->
142+
fetch_from_snapshot(Txn, Key, [])
143+
end),
144+
131145
meck:expect(chronicle_kv, txn_get_many,
132146
fun(Keys, Txn) ->
133147
get_keys_for_txn(Keys, Txn)
@@ -141,6 +155,12 @@ meck_setup_chronicle_kv_getters() ->
141155
meck:expect(chronicle_kv, get,
142156
fun(_Name, Key, #{}) ->
143157
fetch_from_latest_snapshot(Key)
158+
end),
159+
160+
meck:expect(chronicle_kv, get_full_snapshot,
161+
fun(_Name) ->
162+
{ok,
163+
{get_ets_snapshot(), make_rev(get_snapshot_seqno())}}
144164
end).
145165

146166
meck_setup_chronicle_kv_setters() ->
@@ -159,6 +179,10 @@ meck_setup_chronicle_kv_setters() ->
159179
transaction(Name, [], Fun, Opts)
160180
end),
161181

182+
meck:expect(chronicle_kv, txn,
183+
fun(Name, Fun) ->
184+
transaction(Name, [], Fun, [])
185+
end),
162186

163187
meck:expect(chronicle_kv, set,
164188
fun(_Name, Key, Value) ->
@@ -175,13 +199,32 @@ get_ets_snapshot() ->
175199
[] -> maps:new()
176200
end.
177201

202+
get_snapshot_seqno() ->
203+
case ets:lookup(?TABLE_NAME, snapshot_seqno) of
204+
[{snapshot_seqno, Value}] -> Value;
205+
[] -> 0
206+
end.
207+
178208
store_ets_snapshot(Snapshot) ->
179-
ets:insert(?TABLE_NAME, {snapshot, Snapshot}).
209+
Seqno = maps:fold(
210+
fun(_Key, {_Value, {_, Seqno}}, Acc) ->
211+
max(Seqno, Acc)
212+
end, 0, Snapshot),
213+
store_ets_snapshot(Snapshot, Seqno).
214+
store_ets_snapshot(Snapshot, Seqno) ->
215+
ets:insert(?TABLE_NAME, {snapshot, Snapshot}),
216+
ets:insert(?TABLE_NAME, {snapshot_seqno, Seqno}).
217+
218+
add_rev_to_value(Value, Seqno) ->
219+
{Value, make_rev(Seqno)}.
180220

181221
add_rev_to_value(Value) ->
182-
%% Chronicle stores revs, we won't bother with the actual values here,
183-
%% but we need the correct format.
184-
{Value, 1}.
222+
%% Chronicle stores revs, we won't bother with the actual history rev here,
223+
%% but we do use the seqno in some places/tests.
224+
{Value, make_rev(get_snapshot_seqno() + 1)}.
225+
226+
make_rev(Seqno) ->
227+
{?FAKE_HISTORY_REV, Seqno}.
185228

186229
fetch_from_latest_snapshot(Key) ->
187230
fetch_from_latest_snapshot(Key, #{}).
@@ -205,11 +248,16 @@ get_keys_for_txn(_Keys, _Snapshot) ->
205248
get_ets_snapshot().
206249

207250
do_commits(Commits) ->
208-
NewMap = lists:foldl(
209-
fun({set, Key, Value}, Acc) ->
210-
Acc#{Key => Value}
211-
end, #{}, Commits),
212-
update_snapshot(NewMap).
251+
{NewMap, NewSeqno} = lists:foldl(
252+
fun({set, Key, Value}, {Snapshot, Seqno}) ->
253+
NewSeqno = Seqno + 1,
254+
{Snapshot#{Key => add_rev_to_value(Value, NewSeqno)}, NewSeqno};
255+
({delete, Key}, {Snapshot, Seqno}) ->
256+
NewSeqno = Seqno + 1,
257+
{maps:remove(Key, Snapshot), NewSeqno}
258+
end, {get_ets_snapshot(), get_snapshot_seqno()}, Commits),
259+
260+
store_ets_snapshot(NewMap, NewSeqno).
213261

214262
transaction(_Name, Keys, Fun, _Opts) ->
215263
Snapshot = get_keys_for_txn(Keys, {txn_slow, get_ets_snapshot()}),
@@ -220,10 +268,10 @@ transaction(_Name, Keys, Fun, _Opts) ->
220268
%% expanded in the future if necessary
221269
{commit, Commits} ->
222270
do_commits(Commits),
223-
{ok, 1};
271+
{ok, {?FAKE_HISTORY_REV, get_snapshot_seqno()}};
224272
{commit, Commits, Results} ->
225273
do_commits(Commits),
226-
{ok, 1, Results};
274+
{ok, {?FAKE_HISTORY_REV, get_snapshot_seqno()}, Results};
227275
{abort, Error} ->
228276
Error
229277
end.

0 commit comments

Comments
 (0)