Skip to content
This repository
Newer
Older
100644 296 lines (262 sloc) 10.687 kb
90f6ce85 »
2010-07-21 initial import
1 %% -------------------------------------------------------------------
2 %%
3 %% riak_core: Core Riak Application
4 %%
5 %% Copyright (c) 2007-2010 Basho Technologies, Inc. All Rights Reserved.
6 %%
7 %% This file is provided to you under the Apache License,
8 %% Version 2.0 (the "License"); you may not use this file
9 %% except in compliance with the License. You may obtain
10 %% a copy of the License at
11 %%
12 %% http://www.apache.org/licenses/LICENSE-2.0
13 %%
14 %% Unless required by applicable law or agreed to in writing,
15 %% software distributed under the License is distributed on an
16 %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 %% KIND, either express or implied. See the License for the
18 %% specific language governing permissions and limitations
19 %% under the License.
20 %%
21 %% -------------------------------------------------------------------
22
23 %% @doc A simple Erlang implementation of vector clocks as inspired by Lamport logical clocks.
24 %%
25 %% @reference Leslie Lamport (1978). "Time, clocks, and the ordering of events
26 %% in a distributed system". Communications of the ACM 21 (7): 558-565.
27 %%
28 %% @reference Friedemann Mattern (1988). "Virtual Time and Global States of
29 %% Distributed Systems". Workshop on Parallel and Distributed Algorithms:
30 %% pp. 215-226
31
32 -module(vclock).
33
34 -author('Justin Sheehy <justin@basho.com>').
35 -author('Andy Gross <andy@basho.com>').
36
37 -export([fresh/0,descends/2,merge/1,get_counter/2,get_timestamp/2,
97231e05 »
2011-03-16 Add vclock:increment variant that accepts a timestamp.
38 increment/2,increment/3,all_nodes/1,equal/2,prune/3,timestamp/0]).
90f6ce85 »
2010-07-21 initial import
39
ec82f3a7 »
2011-04-04 dialyzer: riak_core fixes
40 -ifdef(TEST).
90f6ce85 »
2010-07-21 initial import
41 -include_lib("eunit/include/eunit.hrl").
ec82f3a7 »
2011-04-04 dialyzer: riak_core fixes
42 -endif.
90f6ce85 »
2010-07-21 initial import
43
ec82f3a7 »
2011-04-04 dialyzer: riak_core fixes
44 -export_type([vclock/0, timestamp/0, vclock_node/0]).
45
46 -opaque vclock() :: [vc_entry()].
90f6ce85 »
2010-07-21 initial import
47 % The timestamp is present but not used, in case a client wishes to inspect it.
48 -type vc_entry() :: {vclock_node(), {counter(), timestamp()}}.
49
50 % Nodes can have any term() as a name, but they must differ from each other.
51 -type vclock_node() :: term().
52 -type counter() :: integer().
53 -type timestamp() :: integer().
54
55 % @doc Create a brand new vclock.
56 -spec fresh() -> vclock().
57 fresh() ->
58 [].
59
60 % @doc Return true if Va is a direct descendant of Vb, else false -- remember, a vclock is its own descendant!
ec82f3a7 »
2011-04-04 dialyzer: riak_core fixes
61 -spec descends(Va :: vclock()|[], Vb :: vclock()|[]) -> boolean().
90f6ce85 »
2010-07-21 initial import
62 descends(_, []) ->
63 % all vclocks descend from the empty vclock
64 true;
65 descends(Va, Vb) ->
66 [{NodeB, {CtrB, _T}}|RestB] = Vb,
e58c769f »
2011-04-08 vclock: simplification of descends/2
67 case lists:keyfind(NodeB, 1, Va) of
68 false ->
69 false;
70 {_, {CtrA, _TSA}} ->
71 (CtrA >= CtrB) andalso descends(Va,RestB)
72 end.
90f6ce85 »
2010-07-21 initial import
73
74 % @doc Combine all VClocks in the input list into their least possible
75 % common descendant.
ec82f3a7 »
2011-04-04 dialyzer: riak_core fixes
76 -spec merge(VClocks :: [vclock()]) -> vclock() | [].
90f6ce85 »
2010-07-21 initial import
77 merge([]) -> [];
78 merge([SingleVclock]) -> SingleVclock;
79 merge([First|Rest]) -> merge(Rest, lists:keysort(1, First)).
80
81 merge([], NClock) -> NClock;
82 merge([AClock|VClocks],NClock) ->
83 merge(VClocks, merge(lists:keysort(1, AClock), NClock, [])).
84
85 merge([], [], AccClock) -> lists:reverse(AccClock);
86 merge([], [Left|Rest], AccClock) -> merge([], Rest, [Left|AccClock]);
87 merge(Left, [], AccClock) -> merge([], Left, AccClock);
88 merge(V=[{Node1,{Ctr1,TS1}}|VClock],
89 N=[{Node2,{Ctr2,TS2}}|NClock], AccClock) ->
90 if Node1 < Node2 ->
91 merge(VClock, N, [{Node1,{Ctr1,TS1}}|AccClock]);
92 Node1 > Node2 ->
93 merge(V, NClock, [{Node2,{Ctr2,TS2}}|AccClock]);
94 true ->
95 ({_Ctr,_TS} = C1) = if Ctr1 > Ctr2 -> {Ctr1,TS1};
96 true -> {Ctr2,TS2}
97 end,
98 merge(VClock, NClock, [{Node1,C1}|AccClock])
99 end.
100
101 % @doc Get the counter value in VClock set from Node.
102 -spec get_counter(Node :: vclock_node(), VClock :: vclock()) -> counter() | undefined.
103 get_counter(Node, VClock) ->
c4fe87f5 »
2011-04-08 vclock: Replacing proplists:get_value() calls with far faster lists:k…
104 case lists:keyfind(Node, 1, VClock) of
105 {_, {Ctr, _TS}} -> Ctr;
106 false -> undefined
90f6ce85 »
2010-07-21 initial import
107 end.
108
109 % @doc Get the timestamp value in a VClock set from Node.
110 -spec get_timestamp(Node :: vclock_node(), VClock :: vclock()) -> timestamp() | undefined.
111 get_timestamp(Node, VClock) ->
c4fe87f5 »
2011-04-08 vclock: Replacing proplists:get_value() calls with far faster lists:k…
112 case lists:keyfind(Node, 1, VClock) of
113 {_, {_Ctr, TS}} -> TS;
114 false -> undefined
90f6ce85 »
2010-07-21 initial import
115 end.
116
117 % @doc Increment VClock at Node.
118 -spec increment(Node :: vclock_node(), VClock :: vclock()) -> vclock().
119 increment(Node, VClock) ->
97231e05 »
2011-03-16 Add vclock:increment variant that accepts a timestamp.
120 increment(Node, timestamp(), VClock).
121
122 % @doc Increment VClock at Node.
123 -spec increment(Node :: vclock_node(), IncTs :: timestamp(),
124 VClock :: vclock()) -> vclock().
125 increment(Node, IncTs, VClock) ->
90f6ce85 »
2010-07-21 initial import
126 {{_Ctr, _TS}=C1,NewV} = case lists:keytake(Node, 1, VClock) of
127 false ->
97231e05 »
2011-03-16 Add vclock:increment variant that accepts a timestamp.
128 {{1, IncTs}, VClock};
90f6ce85 »
2010-07-21 initial import
129 {value, {_N, {C, _T}}, ModV} ->
97231e05 »
2011-03-16 Add vclock:increment variant that accepts a timestamp.
130 {{C + 1, IncTs}, ModV}
90f6ce85 »
2010-07-21 initial import
131 end,
132 [{Node,C1}|NewV].
133
97231e05 »
2011-03-16 Add vclock:increment variant that accepts a timestamp.
134
90f6ce85 »
2010-07-21 initial import
135 % @doc Return the list of all nodes that have ever incremented VClock.
136 -spec all_nodes(VClock :: vclock()) -> [vclock_node()].
137 all_nodes(VClock) ->
138 [X || {X,{_,_}} <- VClock].
139
97231e05 »
2011-03-16 Add vclock:increment variant that accepts a timestamp.
140 % @doc Return a timestamp for a vector clock
141 -spec timestamp() -> timestamp().
90f6ce85 »
2010-07-21 initial import
142 timestamp() ->
143 calendar:datetime_to_gregorian_seconds(erlang:universaltime()).
144
145 % @doc Compares two VClocks for equality.
146 % Not very fast.
147 -spec equal(VClockA :: vclock(), VClockB :: vclock()) -> boolean().
148 equal(VA,VB) ->
149 VSet1 = sets:from_list(VA),
150 VSet2 = sets:from_list(VB),
151 case sets:size(sets:subtract(VSet1,VSet2)) > 0 of
152 true -> false;
153 false ->
154 case sets:size(sets:subtract(VSet2,VSet1)) > 0 of
155 true -> false;
156 false -> true
157 end
158 end.
159
160 % @doc Possibly shrink the size of a vclock, depending on current age and size.
161 -spec prune(V::vclock(), Now::integer(), BucketProps::term()) -> vclock().
162 prune(V,Now,BucketProps) ->
724b9511 »
2011-03-03 vclock:prune(): In case of oldish vclock entries with identical times…
163 %% This sort need to be deterministic, to avoid spurious merge conflicts later.
164 %% We achieve this by using the node ID as secondary key.
165 SortV = lists:sort(fun({N1,{_,T1}},{N2,{_,T2}}) -> {T1,N1} < {T2,N2} end, V),
90f6ce85 »
2010-07-21 initial import
166 prune_vclock1(SortV,Now,BucketProps).
167 % @private
168 prune_vclock1(V,Now,BProps) ->
c4fe87f5 »
2011-04-08 vclock: Replacing proplists:get_value() calls with far faster lists:k…
169 case length(V) =< get_property(small_vclock, BProps) of
90f6ce85 »
2010-07-21 initial import
170 true -> V;
171 false ->
172 {_,{_,HeadTime}} = hd(V),
c4fe87f5 »
2011-04-08 vclock: Replacing proplists:get_value() calls with far faster lists:k…
173 case (Now - HeadTime) < get_property(young_vclock,BProps) of
90f6ce85 »
2010-07-21 initial import
174 true -> V;
175 false -> prune_vclock1(V,Now,BProps,HeadTime)
176 end
177 end.
178 % @private
179 prune_vclock1(V,Now,BProps,HeadTime) ->
180 % has a precondition that V is longer than small and older than young
c4fe87f5 »
2011-04-08 vclock: Replacing proplists:get_value() calls with far faster lists:k…
181 case length(V) > get_property(big_vclock,BProps) of
90f6ce85 »
2010-07-21 initial import
182 true -> prune_vclock1(tl(V),Now,BProps);
183 false ->
c4fe87f5 »
2011-04-08 vclock: Replacing proplists:get_value() calls with far faster lists:k…
184 case (Now - HeadTime) > get_property(old_vclock,BProps) of
90f6ce85 »
2010-07-21 initial import
185 true -> prune_vclock1(tl(V),Now,BProps);
186 false -> V
187 end
188 end.
189
c4fe87f5 »
2011-04-08 vclock: Replacing proplists:get_value() calls with far faster lists:k…
190 get_property(Key, PairList) ->
191 {_Key, Value} = lists:keyfind(Key, 1, PairList),
192 Value.
193
ec82f3a7 »
2011-04-04 dialyzer: riak_core fixes
194 %% ===================================================================
195 %% EUnit tests
196 %% ===================================================================
197 -ifdef(TEST).
198
199 % @doc Serves as both a trivial test and some example code.
200 example_test() ->
201 A = vclock:fresh(),
202 B = vclock:fresh(),
203 A1 = vclock:increment(a, A),
204 B1 = vclock:increment(b, B),
205 true = vclock:descends(A1,A),
206 true = vclock:descends(B1,B),
207 false = vclock:descends(A1,B1),
208 A2 = vclock:increment(a, A1),
209 C = vclock:merge([A2, B1]),
210 C1 = vclock:increment(c, C),
211 true = vclock:descends(C1, A2),
212 true = vclock:descends(C1, B1),
213 false = vclock:descends(B1, C1),
214 false = vclock:descends(B1, A1),
215 ok.
216
90f6ce85 »
2010-07-21 initial import
217 prune_small_test() ->
218 % vclock with less entries than small_vclock will be untouched
219 Now = riak_core_util:moment(),
220 OldTime = Now - 32000000,
221 SmallVC = [{<<"1">>, {1, OldTime}},
222 {<<"2">>, {2, OldTime}},
223 {<<"3">>, {3, OldTime}}],
224 Props = [{small_vclock,4}],
225 ?assertEqual(lists:sort(SmallVC), lists:sort(prune(SmallVC, Now, Props))).
226
227 prune_young_test() ->
228 % vclock with all entries younger than young_vclock will be untouched
229 Now = riak_core_util:moment(),
230 NewTime = Now - 1,
231 VC = [{<<"1">>, {1, NewTime}},
232 {<<"2">>, {2, NewTime}},
233 {<<"3">>, {3, NewTime}}],
234 Props = [{small_vclock,1},{young_vclock,1000}],
235 ?assertEqual(lists:sort(VC), lists:sort(prune(VC, Now, Props))).
236
237 prune_big_test() ->
238 % vclock not preserved by small or young will be pruned down to
239 % no larger than big_vclock entries
240 Now = riak_core_util:moment(),
241 NewTime = Now - 1000,
242 VC = [{<<"1">>, {1, NewTime}},
243 {<<"2">>, {2, NewTime}},
244 {<<"3">>, {3, NewTime}}],
245 Props = [{small_vclock,1},{young_vclock,1},
246 {big_vclock,2},{old_vclock,100000}],
247 ?assert(length(prune(VC, Now, Props)) =:= 2).
248
249 prune_old_test() ->
250 % vclock not preserved by small or young will be pruned down to
251 % no larger than big_vclock and no entries more than old_vclock ago
252 Now = riak_core_util:moment(),
253 NewTime = Now - 1000,
254 OldTime = Now - 100000,
255 VC = [{<<"1">>, {1, NewTime}},
256 {<<"2">>, {2, OldTime}},
257 {<<"3">>, {3, OldTime}}],
258 Props = [{small_vclock,1},{young_vclock,1},
259 {big_vclock,2},{old_vclock,10000}],
260 ?assert(length(prune(VC, Now, Props)) =:= 1).
261
7ce0ed9c »
2011-03-03 Added test for determinstic vclock pruning for nodes with same timest…
262 prune_order_test() ->
263 % vclock with two nodes of the same timestamp will be pruned down
264 % to the same node
265 Now = riak_core_util:moment(),
266 OldTime = Now - 100000,
267 VC1 = [{<<"1">>, {1, OldTime}},
268 {<<"2">>, {2, OldTime}}],
269 VC2 = lists:reverse(VC1),
270 Props = [{small_vclock,1},{young_vclock,1},
271 {big_vclock,2},{old_vclock,10000}],
272 ?assertEqual(prune(VC1, Now, Props), prune(VC2, Now, Props)).
273
90f6ce85 »
2010-07-21 initial import
274 accessor_test() ->
275 VC = [{<<"1">>, {1, 1}},
276 {<<"2">>, {2, 2}}],
277 ?assertEqual(1, get_counter(<<"1">>, VC)),
278 ?assertEqual(1, get_timestamp(<<"1">>, VC)),
279 ?assertEqual(2, get_counter(<<"2">>, VC)),
280 ?assertEqual(2, get_timestamp(<<"2">>, VC)),
281 ?assertEqual(undefined, get_counter(<<"3">>, VC)),
282 ?assertEqual(undefined, get_timestamp(<<"3">>, VC)),
283 ?assertEqual([<<"1">>, <<"2">>], all_nodes(VC)).
284
285 merge_test() ->
286 VC1 = [{<<"1">>, {1, 1}},
287 {<<"2">>, {2, 2}},
288 {<<"4">>, {4, 4}}],
289 VC2 = [{<<"3">>, {3, 3}},
290 {<<"4">>, {3, 3}}],
291 ?assertEqual([], merge(vclock:fresh())),
292 ?assertEqual([{<<"1">>,{1,1}},{<<"2">>,{2,2}},{<<"3">>,{3,3}},{<<"4">>,{4,4}}],
293 merge([VC1, VC2])).
ec82f3a7 »
2011-04-04 dialyzer: riak_core fixes
294
295 -endif.
Something went wrong with that request. Please try again.