couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vatam...@apache.org
Subject [couchdb] 04/04: [fixup] avoid introducing a new metadata field
Date Sun, 09 Apr 2017 04:02:45 GMT
This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch 63012-scheduler
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 9e3a544951c506f5081f63336910c6f1156d718e
Author: Nick Vatamaniuc <vatamane@apache.org>
AuthorDate: Sun Apr 9 00:02:01 2017 -0400

    [fixup] avoid introducing a new metadata field
---
 src/couch/src/couch_doc.erl                        |  3 ---
 src/couch_replicator/src/couch_replicator.erl      | 27 ++++++++++++++------
 src/couch_replicator/src/couch_replicator.hrl      |  2 +-
 .../src/couch_replicator_doc_processor.erl         |  5 ++--
 .../src/couch_replicator_doc_processor_worker.erl  |  5 ++--
 src/couch_replicator/src/couch_replicator_docs.erl | 29 ++++++++--------------
 .../src/couch_replicator_js_functions.hrl          | 16 +++++++++---
 .../src/couch_replicator_scheduler_job.erl         |  5 ++--
 8 files changed, 50 insertions(+), 42 deletions(-)

diff --git a/src/couch/src/couch_doc.erl b/src/couch/src/couch_doc.erl
index 5e6d2b7..381ad4b 100644
--- a/src/couch/src/couch_doc.erl
+++ b/src/couch/src/couch_doc.erl
@@ -270,9 +270,6 @@ transfer_fields([{<<"_replication_state">>, _} = Field | Rest],
 transfer_fields([{<<"_replication_state_time">>, _} = Field | Rest],
     #doc{body=Fields} = Doc) ->
     transfer_fields(Rest, Doc#doc{body=[Field|Fields]});
-transfer_fields([{<<"_replication_start_time">>, _} = Field | Rest],
-    #doc{body=Fields} = Doc) ->
-    transfer_fields(Rest, Doc#doc{body=[Field|Fields]});
 transfer_fields([{<<"_replication_state_reason">>, _} = Field | Rest],
     #doc{body=Fields} = Doc) ->
     transfer_fields(Rest, Doc#doc{body=[Field|Fields]});
diff --git a/src/couch_replicator/src/couch_replicator.erl b/src/couch_replicator/src/couch_replicator.erl
index f2b0d02..345cda3 100644
--- a/src/couch_replicator/src/couch_replicator.erl
+++ b/src/couch_replicator/src/couch_replicator.erl
@@ -266,7 +266,8 @@ job(JobId0) when is_binary(JobId0) ->
 
 -spec doc(binary(), binary(), [_]) -> {ok, {[_]}} | {error, not_found}.
 doc(RepDb, DocId, UserCtx) ->
-    {Res, _Bad} = rpc:multicall(couch_replicator_doc_processor, doc, [RepDb, DocId]),
+    {Res, _Bad} = rpc:multicall(couch_replicator_doc_processor, doc,
+        [RepDb, DocId]),
     case [DocInfo || {ok, DocInfo} <- Res] of
         [DocInfo| _] ->
             {ok, DocInfo};
@@ -283,17 +284,27 @@ doc_from_db(RepDb, DocId, UserCtx) ->
             Source = get_value(<<"source">>, Props),
             Target = get_value(<<"target">>, Props),
             State = get_value(<<"_replication_state">>, Props, null),
-            StartTime = get_value(<<"_replication_start_time">>, Props, null),
             StateTime = get_value(<<"_replication_state_time">>, Props, null),
-            {StateInfo, ErrorCount} = case State of
+            {StateInfo, ErrorCount, StartTime} = case State of
                 <<"completed">> ->
-                    Info = get_value(<<"_replication_stats">>, Props, null),
-                    {Info, 0};
+                    {InfoP} = get_value(<<"_replication_stats">>, Props, {[]}),
+                    case lists:keytake(<<"start_time">>, 1, InfoP) of
+                        {value, {_, Time}, InfoP1} ->
+                            {{InfoP1}, 0, Time};
+                        false ->
+                            case lists:keytake(start_time, 1, InfoP) of
+                                {value, {_, Time}, InfoP1} ->
+                                    {{InfoP1}, 0, Time};
+                            false ->
+                                    {{InfoP}, 0, null}
+                            end
+                    end;
                 <<"failed">> ->
-                    Info = get_value(<<"_replication_state_reason">>, Props,
null),
-                    {Info, 1};
+                    Info = get_value(<<"_replication_state_reason">>, Props,
+                        null),
+                    {Info, 1, StateTime};
                 _OtherState ->
-                    {null, 0}
+                    {null, 0, null}
             end,
             {ok, {[
                 {doc_id, DocId},
diff --git a/src/couch_replicator/src/couch_replicator.hrl b/src/couch_replicator/src/couch_replicator.hrl
index b8669e8..38b5a37 100644
--- a/src/couch_replicator/src/couch_replicator.hrl
+++ b/src/couch_replicator/src/couch_replicator.hrl
@@ -22,7 +22,7 @@
     view = nil :: any() | '_',
     doc_id :: any() | '_',
     db_name = null :: null | binary() | '_',
-    start_time :: erlang:timestamp() | '_'
+    start_time = {0, 0, 0}:: erlang:timestamp() | '_'
 }).
 
 -type rep_id() :: {string(), string()}.
diff --git a/src/couch_replicator/src/couch_replicator_doc_processor.erl b/src/couch_replicator/src/couch_replicator_doc_processor.erl
index d22ac26..9063436 100644
--- a/src/couch_replicator/src/couch_replicator_doc_processor.erl
+++ b/src/couch_replicator/src/couch_replicator_doc_processor.erl
@@ -84,8 +84,7 @@ db_change(DbName, {ChangeProps} = Change, Server) ->
     _Tag:Error ->
         {RepProps} = get_json_value(doc, ChangeProps),
         DocId = get_json_value(<<"_id">>, RepProps),
-        Timestamp = os:timestamp(),
-        couch_replicator_docs:update_failed(DbName, DocId, Error, Timestamp)
+        couch_replicator_docs:update_failed(DbName, DocId, Error)
     end,
     Server.
 
@@ -830,7 +829,7 @@ setup() ->
     meck:expect(couch_replicator_doc_processor_worker, spawn_worker, 4, pid),
     meck:expect(couch_replicator_scheduler, remove_job, 1, ok),
     meck:expect(couch_replicator_docs, remove_state_fields, 2, ok),
-    meck:expect(couch_replicator_docs, update_failed, 4, ok),
+    meck:expect(couch_replicator_docs, update_failed, 3, ok),
     {ok, Pid} = start_link(),
     Pid.
 
diff --git a/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl b/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl
index 30a6988..4f6dab0 100644
--- a/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl
+++ b/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl
@@ -80,8 +80,7 @@ maybe_start_replication(Id, RepWithoutId, WRef) ->
         {temporary_error, Reason};
     {permanent_failure, Reason} ->
         {DbName, DocId} = Id,
-        StartTime = Rep#rep.start_time,
-        couch_replicator_docs:update_failed(DbName, DocId, Reason, StartTime),
+        couch_replicator_docs:update_failed(DbName, DocId, Reason),
         {permanent_failure, Reason}
     end.
 
@@ -235,7 +234,7 @@ setup() ->
     meck:expect(couch_replicator_scheduler, add_job, 1, ok),
     meck:expect(config, get, fun(_, _, Default) -> Default end),
     meck:expect(couch_server, get_uuid, 0, this_is_snek),
-    meck:expect(couch_replicator_docs, update_failed, 4, ok),
+    meck:expect(couch_replicator_docs, update_failed, 3, ok),
     meck:expect(couch_replicator_scheduler, rep_state, 1, nil),
     meck:expect(couch_replicator_doc_processor, get_worker_ref, 1, nil),
     ok.
diff --git a/src/couch_replicator/src/couch_replicator_docs.erl b/src/couch_replicator/src/couch_replicator_docs.erl
index 5bdfe92..f945c4a 100644
--- a/src/couch_replicator/src/couch_replicator_docs.erl
+++ b/src/couch_replicator/src/couch_replicator_docs.erl
@@ -19,8 +19,8 @@
 -export([ensure_cluster_rep_ddoc_exists/1]).
 -export([
     remove_state_fields/2,
-    update_doc_completed/4,
-    update_failed/4,
+    update_doc_completed/3,
+    update_failed/3,
     update_rep_id/1
 ]).
 -export([update_triggered/2, update_error/2]).
@@ -59,30 +59,26 @@ remove_state_fields(DbName, DocId) ->
         {<<"_replication_state">>, undefined},
         {<<"_replication_state_time">>, undefined},
         {<<"_replication_state_reason">>, undefined},
-        {<<"_replication_start_time">>, undefined},
         {<<"_replication_id">>, undefined},
         {<<"_replication_stats">>, undefined}]).
 
--spec update_doc_completed(binary(), binary(), [_], erlang:timestamp()) -> any().
-update_doc_completed(DbName, DocId, Stats, StartTime) ->
-    StartTimeBin = couch_replicator_utils:iso8601(StartTime),
+-spec update_doc_completed(binary(), binary(), [_]) -> any().
+update_doc_completed(DbName, DocId, Stats) ->
     update_rep_doc(DbName, DocId, [
         {<<"_replication_state">>, <<"completed">>},
         {<<"_replication_state_reason">>, undefined},
-        {<<"_replication_start_time">>,  StartTimeBin},
         {<<"_replication_stats">>, {Stats}}]),
-    couch_stats:increment_counter([couch_replicator, docs, completed_state_updates]).
+    couch_stats:increment_counter([couch_replicator, docs,
+        completed_state_updates]).
 
 
--spec update_failed(binary(), binary(), any(), erlang:timestamp()) -> any().
-update_failed(DbName, DocId, Error, StartTime) ->
+-spec update_failed(binary(), binary(), any()) -> any().
+update_failed(DbName, DocId, Error) ->
     Reason = error_reason(Error),
     couch_log:error("Error processing replication doc `~s` from `~s`: ~s",
         [DocId, DbName, Reason]),
-    StartTimeBin = couch_replicator_utils:iso8601(StartTime),
     update_rep_doc(DbName, DocId, [
         {<<"_replication_state">>, <<"failed">>},
-        {<<"_replication_start_time">>, StartTimeBin},
         {<<"_replication_stats">>, undefined},
         {<<"_replication_state_reason">>, Reason}]),
     couch_stats:increment_counter([couch_replicator, docs, failed_state_updates]).
@@ -92,15 +88,12 @@ update_failed(DbName, DocId, Error, StartTime) ->
 update_triggered(Rep, {Base, Ext}) ->
     #rep{
         db_name = DbName,
-        doc_id = DocId,
-        start_time = StartTime
+        doc_id = DocId
     } = Rep,
-    StartTimeBin = couch_replicator_utils:iso8601(StartTime),
     update_rep_doc(DbName, DocId, [
         {<<"_replication_state">>, <<"triggered">>},
         {<<"_replication_state_reason">>, undefined},
         {<<"_replication_id">>, iolist_to_binary([Base, Ext])},
-        {<<"_replication_start_time">>, StartTimeBin},
         {<<"_replication_stats">>, undefined}]),
     ok.
 
@@ -199,14 +192,14 @@ replication_design_doc_props(DDocId) ->
                 {<<"map">>, ?REP_DB_TERMINAL_STATE_VIEW_MAP_FUN},
                 {<<"reduce">>, <<"_count">>}
             ]},
-    DocProps = [
+    [
         {<<"_id">>, DDocId},
         {<<"language">>, <<"javascript">>},
         {<<"validate_doc_update">>, ?REP_DB_DOC_VALIDATE_FUN},
         {<<"views">>, {[
             {<<"terminal_states">>, TerminalViewEJson}
         ]}}
-   ].
+    ].
 
 
 % Note: parse_rep_doc can handle filtered replications. During parsing of the
diff --git a/src/couch_replicator/src/couch_replicator_js_functions.hrl b/src/couch_replicator/src/couch_replicator_js_functions.hrl
index dbad050..0c78b90 100644
--- a/src/couch_replicator/src/couch_replicator_js_functions.hrl
+++ b/src/couch_replicator/src/couch_replicator_js_functions.hrl
@@ -178,17 +178,25 @@
         if (state === 'failed') {
             source = doc.source;
             target = doc.target;
-            start_time = doc._replication_start_time;
             last_updated = doc._replication_state_time;
             state_reason = doc._replication_state_reason;
-            emit('failed', [source, target, start_time, last_updated, state_reason]);
+            emit('failed', [source, target, last_updated, last_updated, state_reason]);
         } else if (state === 'completed') {
             source = doc.source;
             target = doc.target;
-            start_time = doc._replication_start_time;
             last_updated = doc._replication_state_time;
             stats = doc._replication_stats;
-            emit('completed', [source, target, start_time, last_updated, stats]);
+            start_time = stats.start_time;
+            info = {
+                'changes_pending': stats['changes_pending'],
+                'checkpointed_source_seq': stats['checkpointed_source_seq'],
+                'doc_write_failures': stats['doc_write_failures'],
+                'docs_read': stats['docs_read'],
+                'docs_written': stats['docs_written'],
+                'missing_revisions_found': stats['missing_revisions_found'],
+                'revisions_checked': stats['revisions_checked']
+            }
+            emit('completed', [source, target, start_time, last_updated, info]);
         }
     }
 ">>).
diff --git a/src/couch_replicator/src/couch_replicator_scheduler_job.erl b/src/couch_replicator/src/couch_replicator_scheduler_job.erl
index 1c9faaf..4d80af0 100644
--- a/src/couch_replicator/src/couch_replicator_scheduler_job.erl
+++ b/src/couch_replicator/src/couch_replicator_scheduler_job.erl
@@ -493,8 +493,9 @@ doc_update_triggered(#rep{id = RepId, doc_id = DocId} = Rep) ->
 doc_update_completed(#rep{db_name = null}, _Stats) ->
     ok;
 doc_update_completed(#rep{id = RepId, doc_id = DocId, db_name = DbName,
-    start_time = StartTime}, Stats) ->
-    couch_replicator_docs:update_doc_completed(DbName, DocId, Stats, StartTime),
+    start_time = StartTime}, Stats0) ->
+    Stats = Stats0 ++ [{start_time, couch_replicator_utils:iso8601(StartTime)}],
+    couch_replicator_docs:update_doc_completed(DbName, DocId, Stats),
     couch_log:notice("Replication `~s` completed (triggered by `~s`)",
         [pp_rep_id(RepId), DocId]),
     ok.

-- 
To stop receiving notification emails like this one, please contact
"commits@couchdb.apache.org" <commits@couchdb.apache.org>.

Mime
View raw message