You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by fd...@apache.org on 2010/09/02 12:27:46 UTC

svn commit: r991883 - /couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl

Author: fdmanana
Date: Thu Sep  2 10:27:45 2010
New Revision: 991883

URL: http://svn.apache.org/viewvc?rev=991883&view=rev
Log:
New replicator: add retry logic.

Modified:
    couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl

Modified: couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl?rev=991883&r1=991882&r2=991883&view=diff
==============================================================================
--- couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl (original)
+++ couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl Thu Sep  2 10:27:45 2010
@@ -74,17 +74,55 @@ replicate(Src, Tgt, Options, UserCtx) ->
         end_replication(RepId);
     false ->
         {ok, Listener} = rep_result_listener(RepId),
-        Result = case start_replication(RepId, Src, Tgt, Options, UserCtx) of
-        {ok, _RepPid} ->
-            wait_for_result(RepId);
-        Error ->
-            Error
-        end,
+        Result = do_replication_loop(RepId, Src, Tgt, Options, UserCtx),
         couch_replication_notifier:stop(Listener),
         Result
     end.
 
 
+do_replication_loop(RepId, Src, Tgt, Options, UserCtx) ->
+    DocIds = couch_util:get_value(doc_ids, Options),
+    Continuous = couch_util:get_value(continuous, Options, false),
+    Seq = case {DocIds, Continuous} of
+    {undefined, false} ->
+        last_seq(Src);
+    _ ->
+        undefined
+    end,
+    do_replication_loop(RepId, Src, Tgt, Options, UserCtx, Seq).
+
+do_replication_loop(RepId, Src, Tgt, Options, UserCtx, FinalSeq) ->
+    case start_replication(RepId, Src, Tgt, Options, UserCtx) of
+    {ok, _Pid} ->
+        Result = wait_for_result(RepId),
+        maybe_retry(Result, RepId, Src, Tgt, Options, UserCtx, FinalSeq);
+    Error ->
+        Error
+    end.
+
+
+maybe_retry(RepResult, _RepId, _Src, _Tgt, _Options, _UserCtx, undefined) ->
+    RepResult;
+maybe_retry({ok, {Props}} = Result, RepId, Src, Tgt, Options, UserCtx, Seq) ->
+    case couch_util:get_value(source_last_seq, Props) >= Seq of
+    true ->
+        Result;
+    false ->
+        do_replication_loop(RepId, Src, Tgt, Options, UserCtx, Seq)
+    end;
+maybe_retry(RepResult, _RepId, _Src, _Tgt, _Options, _UserCtx, _Seq) ->
+    RepResult.
+
+
+last_seq(DbName) ->
+    {ok, Db} = couch_api_wrap:db_open(
+        DbName, [{user_ctx, #user_ctx{roles = [<<"_admin">>]}}]),
+    {ok, DbInfo} = couch_api_wrap:get_db_info(Db),
+    Seq = couch_util:get_value(update_seq, DbInfo),
+    couch_api_wrap:db_close(Db),
+    Seq.
+
+
 start_replication({BaseId, Extension} = RepId, Src, Tgt, Options, UserCtx) ->
     RepChildId = BaseId ++ Extension,
     ChildSpec = {