couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [04/50] fabric commit: updated refs/heads/windsor-merge-121 to 79e6e2f
Date Fri, 01 Aug 2014 09:11:52 GMT
Revert "Revert uuid in changes feeds temporarily"

This reverts commit 0796c6714978fd8302192c24f652c53844373773.


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

Branch: refs/heads/windsor-merge-121
Commit: 400bce8475b80abf16d982ebb1528d998d8ea28f
Parents: 9aafe7b
Author: Robert Newson <robert.newson@cloudant.com>
Authored: Wed Jul 31 00:11:29 2013 +0100
Committer: Robert Newson <rnewson@apache.org>
Committed: Thu Jul 31 10:51:19 2014 +0100

----------------------------------------------------------------------
 src/fabric_rpc.erl | 70 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 63 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fabric/blob/400bce84/src/fabric_rpc.erl
----------------------------------------------------------------------
diff --git a/src/fabric_rpc.erl b/src/fabric_rpc.erl
index c6ac263..870ac57 100644
--- a/src/fabric_rpc.erl
+++ b/src/fabric_rpc.erl
@@ -29,18 +29,19 @@
 
 changes(DbName, #changes_args{} = Args, StartSeq) ->
     changes(DbName, [Args], StartSeq);
-changes(DbName, Options, StartSeq) ->
+changes(DbName, Options, StartVector) ->
     erlang:put(io_priority, {interactive, DbName}),
     #changes_args{dir=Dir} = Args = lists:keyfind(changes_args, 1, Options),
     case get_or_create_db(DbName, []) of
     {ok, Db} ->
+        StartSeq = calculate_start_seq(Db, StartVector),
         Enum = fun changes_enumerator/2,
         Opts = [{dir,Dir}],
         Acc0 = {Db, StartSeq, Args, Options},
         try
             {ok, {_, LastSeq, _, _}} =
                 couch_db:changes_since(Db, StartSeq, Enum, Opts, Acc0),
-            rexi:reply({complete, LastSeq})
+            rexi:reply({complete, {LastSeq, couch_db:get_uuid(Db), node()}})
         after
             couch_db:close(Db)
         end;
@@ -278,11 +279,14 @@ changes_enumerator(DocInfo, {Db, _Seq, Args, Options}) ->
 
 changes_row(Db, #doc_info{id=Id, high_seq=Seq}=DI, Results, Del, true, Opts) ->
     Doc = doc_member(Db, DI, Opts),
-    #change{key=Seq, id=Id, value=Results, doc=Doc, deleted=Del};
-changes_row(_, #doc_info{id=Id, high_seq=Seq}, Results, true, _, _) ->
-    #change{key=Seq, id=Id, value=Results, deleted=true};
-changes_row(_, #doc_info{id=Id, high_seq=Seq}, Results, _, _, _) ->
-    #change{key=Seq, id=Id, value=Results}.
+    Uuid = couch_db:get_uuid(Db),
+    #change{key={Seq, Uuid, node()}, id=Id, value=Results, doc=Doc, deleted=Del};
+changes_row(Db, #doc_info{id=Id, high_seq=Seq}, Results, true, _, _) ->
+    Uuid = couch_db:get_uuid(Db),
+    #change{key={Seq, Uuid, node()}, id=Id, value=Results, deleted=true};
+changes_row(Db, #doc_info{id=Id, high_seq=Seq}, Results, _, _, _) ->
+    Uuid = couch_db:get_uuid(Db),
+    #change{key={Seq, Uuid, node()}, id=Id, value=Results}.
 
 doc_member(Shard, DocInfo, Opts) ->
     case couch_db:open_doc(Shard, DocInfo, [deleted | Opts]) of
@@ -363,3 +367,55 @@ set_io_priority(DbName, Options) ->
         _ ->
             ok
     end.
+
+calculate_start_seq(_Db, Seq) when is_integer(Seq) ->
+    Seq;
+calculate_start_seq(Db, {Seq, Uuid, Node}) ->
+    case couch_db:get_uuid(Db) == Uuid of
+        true ->
+            case owner(Node, Seq, couch_db:get_epochs(Db)) of
+                true -> Seq;
+                false -> 0
+            end;
+        false ->
+            %% The file was rebuilt, most likely in a different
+            %% order, so rewind.
+            0
+    end.
+
+owner(Node, Seq, Epochs) ->
+    owner(Node, Seq, Epochs, infinity).
+
+owner(_Node, _Seq, [], _HighSeq) ->
+    false;
+owner(Node, Seq, [{EpochNode, EpochSeq} | _Rest], HighSeq)
+  when Node =:= EpochNode andalso Seq < HighSeq andalso Seq >= EpochSeq ->
+    true;
+owner(Node, Seq, [{_EpochNode, EpochSeq} | Rest], _HighSeq) ->
+    owner(Node, Seq, Rest, EpochSeq).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+calculate_start_seq_test() ->
+    %% uuid mismatch is always a rewind.
+    Hdr1 = couch_db_header:new(),
+    Hdr2 = couch_db_header:set(Hdr1, [{uuid, uuid1}]),
+    ?assertEqual(0, calculate_start_seq(#db{header=Hdr2}, {1, uuid2, node1})),
+    %% uuid matches and seq is owned by node.
+    Hdr3 = couch_db_header:set(Hdr2, [{epochs, [{node1, 1}]}]),
+    ?assertEqual(2, calculate_start_seq(#db{header=Hdr3}, {2, uuid1, node1})),
+    %% uuids match but seq is not owned by node.
+    Hdr4 = couch_db_header:set(Hdr2, [{epochs, [{node2, 2}, {node1, 1}]}]),
+    ?assertEqual(0, calculate_start_seq(#db{header=Hdr4}, {3, uuid1, node1})),
+    %% return integer if we didn't get a vector.
+    ?assertEqual(4, calculate_start_seq(#db{}, 4)).
+
+owner_test() ->
+    ?assertNot(owner(foo, 1, [])),
+    ?assert(owner(foo, 1, [{foo, 1}])),
+    ?assert(owner(foo, 50, [{bar, 100}, {foo, 1}])),
+    ?assert(owner(foo, 50, [{baz, 200}, {bar, 100}, {foo, 1}])),
+    ?assert(owner(bar, 150, [{baz, 200}, {bar, 100}, {foo, 1}])).
+
+-endif.


Mime
View raw message