nickva commented on a change in pull request #470: Scheduling Replicator URL: https://github.com/apache/couchdb/pull/470#discussion_r112500979
########## File path: src/couch_replicator/src/couch_replicator.erl ########## @@ -11,167 +11,100 @@ % the License. -module(couch_replicator). --behaviour(gen_server). --vsn(1). -% public API --export([replicate/2]). - -% meant to be used only by the replicator database listener --export([async_replicate/1]). --export([cancel_replication/1]). - -% gen_server callbacks --export([init/1, terminate/2, code_change/3]). --export([handle_call/3, handle_cast/2, handle_info/2]). --export([format_status/2]). - --export([details/1]). +-export([ + replicate/2, + ensure_rep_db_exists/0, + replication_states/0, + job/1, + doc/3, + active_doc/2, + info_from_doc/2 +]). -include_lib("couch/include/couch_db.hrl"). --include("couch_replicator_api_wrap.hrl"). -include("couch_replicator.hrl"). - --define(LOWEST_SEQ, 0). - --define(DEFAULT_CHECKPOINT_INTERVAL, 30000). +-include("couch_replicator_api_wrap.hrl"). +-include_lib("couch_mrview/include/couch_mrview.hrl"). +-include_lib("mem3/include/mem3.hrl"). + +-define(DESIGN_DOC_CREATION_DELAY_MSEC, 1000). +-define(REPLICATION_STATES, [ + initializing, % Just added to scheduler + error, % Could not be turned into a replication job + running, % Scheduled and running + pending, % Scheduled and waiting to run + crashing, % Scheduled but crashing, backed off by the scheduler + completed, % Non-continuous (normal) completed replication + failed % Terminal failure, will not be retried anymore +]). -import(couch_util, [ get_value/2, - get_value/3, - to_binary/1 + get_value/3 ]). --import(couch_replicator_utils, [ - start_db_compaction_notifier/2, - stop_db_compaction_notifier/1 -]). - --record(rep_state, { - rep_details, - source_name, - target_name, - source, - target, - history, - checkpoint_history, - start_seq, - committed_seq, - current_through_seq, - seqs_in_progress = [], - highest_seq_done = {0, ?LOWEST_SEQ}, - source_log, - target_log, - rep_starttime, - src_starttime, - tgt_starttime, - timer, % checkpoint timer - changes_queue, - changes_manager, - changes_reader, - workers, - stats = couch_replicator_stats:new(), - session_id, - source_db_compaction_notifier = nil, - target_db_compaction_notifier = nil, - source_monitor = nil, - target_monitor = nil, - source_seq = nil, - use_checkpoints = true, - checkpoint_interval = ?DEFAULT_CHECKPOINT_INTERVAL, - type = db, - view = nil -}). - +-spec replicate({[_]}, #user_ctx{}) -> + {ok, {continuous, binary()}} | + {ok, {[_]}} | + {ok, {cancelled, binary()}} | + {error, any()}. replicate(PostBody, Ctx) -> - {ok, #rep{id = RepId, options = Options, user_ctx = UserCtx} = Rep} = - couch_replicator_utils:parse_rep_doc(PostBody, Ctx), + {ok, Rep0} = couch_replicator_utils:parse_rep_doc(PostBody, Ctx), + Rep = Rep0#rep{start_time = os:timestamp()}, + #rep{id = RepId, options = Options, user_ctx = UserCtx} = Rep, case get_value(cancel, Options, false) of true -> - case get_value(id, Options, nil) of + CancelRepId = case get_value(id, Options, nil) of nil -> - cancel_replication(RepId); + RepId; RepId2 -> - cancel_replication(RepId2, UserCtx) + RepId2 + end, + case check_authorization(CancelRepId, UserCtx) of + ok -> + cancel_replication(CancelRepId); + not_found -> + {error, not_found} end; false -> + check_authorization(RepId, UserCtx), {ok, Listener} = rep_result_listener(RepId), Result = do_replication_loop(Rep), couch_replicator_notifier:stop(Listener), Result end. -do_replication_loop(#rep{id = {BaseId, Ext} = Id, options = Options} = Rep) -> - case async_replicate(Rep) of - {ok, _Pid} -> - case get_value(continuous, Options, false) of - true -> - {ok, {continuous, ?l2b(BaseId ++ Ext)}}; - false -> - wait_for_result(Id) - end; - Error -> - Error - end. +% This is called from supervisor. Must respect supervisor protocol so +% it returns `ignore`. +-spec ensure_rep_db_exists() -> ignore. +ensure_rep_db_exists() -> + {ok, _Db} = couch_replicator_docs:ensure_rep_db_exists(), + couch_log:notice("~p : created local _replicator database", [?MODULE]), Review comment: Agreed. Will fix it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services