nickva commented on a change in pull request #470: Scheduling Replicator URL: https://github.com/apache/couchdb/pull/470#discussion_r110997023
########## File path: src/couch_replicator/src/couch_replicator_scheduler.erl ########## @@ -0,0 +1,1380 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(couch_replicator_scheduler). +-behaviour(gen_server). +-behaviour(config_listener). +-vsn(1). + +-include("couch_replicator_scheduler.hrl"). +-include("couch_replicator.hrl"). +-include("couch_replicator_api_wrap.hrl"). +-include_lib("couch/include/couch_db.hrl"). + +%% public api +-export([start_link/0, add_job/1, remove_job/1, reschedule/0]). +-export([rep_state/1, find_jobs_by_dbname/1, find_jobs_by_doc/2]). +-export([job_summary/2, health_threshold/0]). +-export([jobs/0, job/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]). + +%% config_listener callback +-export([handle_config_change/5, handle_config_terminate/3]). + +%% types +-type event_type() :: added | started | stopped | {crashed, any()}. +-type event() :: {Type:: event_type(), When :: erlang:timestamp()}. +-type history() :: nonempty_list(event()). + +%% definitions +-define(MAX_BACKOFF_EXPONENT, 10). +-define(BACKOFF_INTERVAL_MICROS, 30 * 1000 * 1000). +-define(DEFAULT_HEALTH_THRESHOLD_SEC, 2 * 60). +-define(RELISTEN_DELAY, 5000). + +-define(DEFAULT_MAX_JOBS, 500). +-define(DEFAULT_MAX_CHURN, 20). +-define(DEFAULT_MAX_HISTORY, 20). +-define(DEFAULT_SCHEDULER_INTERVAL, 60000). +-record(state, {interval, timer, max_jobs, max_churn, max_history}). +-record(job, { + id :: job_id() | '$1' | '_', + rep :: #rep{} | '_', + pid :: undefined | pid() | '$1' | '_', + monitor :: undefined | reference() | '_', + history :: history() | '_'}). + +-record(stats_acc, { + now :: erlang:timestamp(), + pending_t = 0 :: non_neg_integer(), + running_t = 0 :: non_neg_integer(), + crashed_t = 0 :: non_neg_integer(), + pending_n = 0 :: non_neg_integer(), + running_n = 0 :: non_neg_integer(), + crashed_n = 0 :: non_neg_integer()}). + + +%% public functions + +-spec start_link() -> {ok, pid()} | ignore | {error, term()}. +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + + +-spec add_job(#rep{}) -> ok. +add_job(#rep{} = Rep) when Rep#rep.id /= undefined -> + Job = #job{ + id = Rep#rep.id, + rep = Rep, + history = [{added, os:timestamp()}]}, + gen_server:call(?MODULE, {add_job, Job}, infinity). + + +-spec remove_job(job_id()) -> ok. +remove_job(Id) -> + gen_server:call(?MODULE, {remove_job, Id}, infinity). + + +-spec reschedule() -> ok. +% Trigger a manual reschedule. Used for testing and/or ops. +reschedule() -> + gen_server:call(?MODULE, reschedule, infinity). + + +-spec rep_state(rep_id()) -> #rep{} | nil. +rep_state(RepId) -> + case (catch ets:lookup_element(?MODULE, RepId, #job.rep)) of + {'EXIT',{badarg, _}} -> + nil; + Rep -> + Rep + end. + + +-spec job_summary(job_id(), non_neg_integer()) -> [_] | nil. +job_summary(JobId, HealthThreshold) -> Review comment: It's called from doc processor. Doc processor is started later in the rest-for-one-supervisor https://github.com/apache/couchdb/pull/470/commits/9c22dc3276e5c46c9cc10347c57668b9f596e160 that guarantees that replication scheduler process is already running. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
