Skip to content
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

Fix ordering issue in vector_orddict #488

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion apps/antidote/rebar.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{erl_opts, [debug_info]}.
{plugins, [rebar3_path_deps]}.
{project_plugins, [rebar3_proper]}.

{deps, [
{antidote_crdt, {path, "../antidote_crdt"}},
Expand All @@ -19,7 +20,7 @@
{test, [
{extra_src_dirs, [{"test", [{recursive, true}]}]},
{erl_opts, [warnings_as_errors, debug_info, no_inline_list_funcs]},
{deps, [meck]}
{deps, [meck, proper]}
]}
]}.

Expand Down
48 changes: 47 additions & 1 deletion apps/antidote/src/vector_orddict.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
-include("antidote.hrl").

-ifdef(TEST).
-include_lib("proper/include/proper.hrl").
-include_lib("eunit/include/eunit.hrl").
-endif.

Expand Down Expand Up @@ -121,7 +122,7 @@ insert(Vector, Val, {List, Size}) ->
insert_internal(Vector, Val, [], Size, PrevList) ->
{lists:reverse([{Vector, Val} | PrevList]), Size};
insert_internal(Vector, Val, [{FirstClock, FirstVal} | Rest], Size, PrevList) ->
case vectorclock:all_dots_greater(Vector, FirstClock) of
case vectorclock:all_dots(Vector, FirstClock, fun erlang:'>='/2) of
true ->
{lists:reverse(PrevList, [{Vector, Val} | [{FirstClock, FirstVal} | Rest]]), Size};
false ->
Expand Down Expand Up @@ -287,4 +288,49 @@ vector_orddict_conc_test() ->
?assertEqual(is_concurrent_with_any(VDict, CT1), false),
?assertEqual(is_concurrent_with_any(VDict, CT2), true).

vector_orddict_simple_ordering_test() ->
Q0 = vector_orddict:new(),
Q1 = vector_orddict:insert(#{dc1 => 1, dc2 => 1}, val1, Q0),
Q2 = vector_orddict:insert(#{dc1 => 1, dc2 => 2}, val2, Q1),
?assertEqual({#{dc1 => 1, dc2 => 2}, val2},
vector_orddict:first(Q2)).

vector_orddict_simple_ordering2_test() ->
Q0 = vector_orddict:new(),
Q1 = vector_orddict:insert(#{dc1 => 1, dc2 => 2}, val2, Q0),
Q2 = vector_orddict:insert(#{dc1 => 1, dc2 => 1}, val1, Q1),
?assertEqual({#{dc1 => 1, dc2 => 2}, val2},
vector_orddict:first(Q2)).

vv_gen() ->
vector(3, range(1, 10)).

prop_vector_orddict_ordering() ->
?FORALL(VVs0, non_empty(list(vv_gen())),
begin
VVs1 = lists:foldl(
fun(VVlist, Acc) ->
VV0 = lists:zip(lists:seq(1, 3), VVlist),
VV1 = vectorclock:from_list(VV0),
vector_orddict:insert(VV1, none, Acc)
end, vector_orddict:new(), VVs0),
%% Verify that monotonically decreasing value
{InitVV, _} = vector_orddict:first(VVs1),
_ = lists:foldl(fun({VV, _}, MonIncreaseVV) ->
case vectorclock:ge(MonIncreaseVV, VV) of
true -> VV;
false ->
case vectorclock:ge(VV, MonIncreaseVV) of
true -> erlang:error(order_failure);
false -> MonIncreaseVV
end
end
end, InitVV, vector_orddict:to_list(VVs1)),
true
end).

vector_orddict_ordering_test() ->
?assertEqual(true, proper:quickcheck(
prop_vector_orddict_ordering(), [1000, {to_file, user}])).

-endif.
6 changes: 6 additions & 0 deletions apps/vectorclock/src/vectorclock.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
-export([
all_dots_greater/2,
all_dots_smaller/2,
all_dots/3,
conc/2,
eq/2,
fold/3,
Expand Down Expand Up @@ -180,6 +181,11 @@ all_dots_smaller(V1, V2) ->
all_dots_greater(V1, V2) ->
for_all_keys(fun(A, B) -> A > B end, V1, V2).

-spec all_dots(vectorclock(), vectorclock(), fun((integer(), integer()) -> boolean())) ->
boolean().
all_dots(V1, V2, CompFun) ->
for_all_keys(fun(A, B) -> CompFun(A, B) end, V1, V2).

-spec gt(vectorclock(), vectorclock()) -> boolean().
gt(V1, V2) -> lt(V2, V1).

Expand Down