couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [09/50] fabric commit: updated refs/heads/windsor-merge-121 to 79e6e2f
Date Fri, 01 Aug 2014 09:11:57 GMT
Validate epochs


Project: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/commit/bef303b1
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/tree/bef303b1
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/diff/bef303b1

Branch: refs/heads/windsor-merge-121
Commit: bef303b1ceb9d37e0059dff9aa069e52d234236d
Parents: a8a458b
Author: Robert Newson <robert.newson@cloudant.com>
Authored: Sat Aug 10 19:19:04 2013 +0100
Committer: Robert Newson <rnewson@apache.org>
Committed: Thu Jul 31 10:54:26 2014 +0100

----------------------------------------------------------------------
 src/fabric_rpc.erl | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fabric/blob/bef303b1/src/fabric_rpc.erl
----------------------------------------------------------------------
diff --git a/src/fabric_rpc.erl b/src/fabric_rpc.erl
index 825691b..bc0486a 100644
--- a/src/fabric_rpc.erl
+++ b/src/fabric_rpc.erl
@@ -384,6 +384,7 @@ calculate_start_seq(Db, {Seq, Uuid, Node}) ->
     end.
 
 is_owner(Node, Seq, Epochs) ->
+    validate_epochs(Epochs),
     Node =:= owner_of(Seq, Epochs).
 
 owner_of(_Seq, []) ->
@@ -393,6 +394,18 @@ owner_of(Seq, [{EpochNode, EpochSeq} | _Rest]) when Seq > EpochSeq
->
 owner_of(Seq, [_ | Rest]) ->
     owner_of(Seq, Rest).
 
+validate_epochs(Epochs) ->
+    %% Assert uniqueness.
+    case length(Epochs) == length(lists:ukeysort(2, Epochs)) of
+        true  -> ok;
+        false -> erlang:error(duplicate_epoch)
+    end,
+    %% Assert order.
+    case Epochs == lists:sort(fun({_, A}, {_, B}) -> B =< A end, Epochs) of
+        true  -> ok;
+        false -> erlang:error(epoch_order)
+    end.
+
 -ifdef(TEST).
 -include_lib("eunit/include/eunit.hrl").
 
@@ -416,6 +429,8 @@ is_owner_test() ->
     ?assert(is_owner(foo, 2, [{foo, 1}])),
     ?assert(is_owner(foo, 50, [{bar, 100}, {foo, 1}])),
     ?assert(is_owner(foo, 50, [{baz, 200}, {bar, 100}, {foo, 1}])),
-    ?assert(is_owner(bar, 150, [{baz, 200}, {bar, 100}, {foo, 1}])).
+    ?assert(is_owner(bar, 150, [{baz, 200}, {bar, 100}, {foo, 1}])),
+    ?assertError(duplicate_epoch, is_owner(foo, 1, [{foo, 1}, {bar, 1}])),
+    ?assertError(epoch_order, is_owner(foo, 1, [{foo, 100}, {bar, 200}])).
 
 -endif.


Mime
View raw message