Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 029DD200C7A for ; Sun, 9 Apr 2017 06:02:45 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 00391160B96; Sun, 9 Apr 2017 04:02:45 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 2603C160B9E for ; Sun, 9 Apr 2017 06:02:43 +0200 (CEST) Received: (qmail 6786 invoked by uid 500); 9 Apr 2017 04:02:43 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 6693 invoked by uid 99); 9 Apr 2017 04:02:43 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 09 Apr 2017 04:02:43 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 212E38653B; Sun, 9 Apr 2017 04:02:42 +0000 (UTC) Date: Sun, 09 Apr 2017 04:02:43 +0000 To: "commits@couchdb.apache.org" Subject: [couchdb] 02/04: [fixup] move rate limiter tables to separate module MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: vatamane@apache.org Reply-To: "commits@couchdb.apache.org" In-Reply-To: <149171056196.18974.6959902671364707386@gitbox.apache.org> References: <149171056196.18974.6959902671364707386@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: couchdb X-Git-Refname: refs/heads/63012-scheduler X-Git-Reftype: branch X-Git-Rev: a5d03007e2ab143f3fd208b6a1b7a7a85c543abc X-Git-NotificationType: diff X-Git-Multimail-Version: 1.3.dev Auto-Submitted: auto-generated Message-Id: <20170409040242.212E38653B@gitbox.apache.org> archived-at: Sun, 09 Apr 2017 04:02:45 -0000 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 a5d03007e2ab143f3fd208b6a1b7a7a85c543abc Author: Nick Vatamaniuc AuthorDate: Sat Apr 8 02:44:51 2017 -0400 [fixup] move rate limiter tables to separate module --- .../src/couch_replicator_rate_limiter.erl | 32 ++++--------- .../src/couch_replicator_rate_limiter_tables.erl | 54 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/src/couch_replicator/src/couch_replicator_rate_limiter.erl b/src/couch_replicator/src/couch_replicator_rate_limiter.erl index f01f7f9..011372b 100644 --- a/src/couch_replicator/src/couch_replicator_rate_limiter.erl +++ b/src/couch_replicator/src/couch_replicator_rate_limiter.erl @@ -61,7 +61,6 @@ % Definitions --define(SHARDS_N, 16). % Main parameters of the algorithm. The factor is the multiplicative part and % base interval is the additive. @@ -128,8 +127,7 @@ success(Key) -> % gen_server callbacks init([]) -> - Opts = [named_table, public, {keypos, #rec.id}, {read_concurrency, true}], - [ets:new(list_to_atom(TableName), Opts) || TableName <- table_names()], + couch_replicator_rate_limiter_tables:create(#rec.id), {ok, #state{timer = new_timer()}}. @@ -147,7 +145,8 @@ handle_cast(_, State) -> handle_info(cleanup, #state{timer = Timer}) -> timer:cancel(Timer), - TIds = [list_to_existing_atom(TableName) || TableName <- table_names()], + TableNames = couch_replicator_rate_limiter_tables:table_names(), + TIds = [list_to_existing_atom(TableName) || TableName <- TableNames], [cleanup_table(TId, now_msec() - ?MAX_INTERVAL) || TId <- TIds], {noreply, #state{timer = new_timer()}}. @@ -172,7 +171,8 @@ update_success(Key, Interval, Timestamp, Now) -> NewInterval = DecayedInterval - AdditiveFactor, if NewInterval =< 0 -> - ets:delete(term_to_table(Key), Key), + Table = couch_replicator_rate_limiter_tables:term_to_table(Key), + ets:delete(Table, Key), 0; NewInterval =< ?BASE_INTERVAL -> insert(Key, ?BASE_INTERVAL, Now); @@ -196,13 +196,15 @@ update_failure(Key, Interval, _Timestamp, Now) -> -spec insert(any(), interval(), msec()) -> interval(). insert(Key, Interval, Timestamp) -> Entry = #rec{id = Key, backoff = Interval, ts = Timestamp}, - ets:insert(term_to_table(Key), Entry), + Table = couch_replicator_rate_limiter_tables:term_to_table(Key), + ets:insert(Table, Entry), Interval. -spec interval_and_timestamp(key()) -> {interval(), msec()}. interval_and_timestamp(Key) -> - case ets:lookup(term_to_table(Key), Key) of + Table = couch_replicator_rate_limiter_tables:term_to_table(Key), + case ets:lookup(Table, Key) of [] -> {0, 0}; [#rec{backoff = Interval, ts = Timestamp}] -> @@ -239,22 +241,6 @@ additive_factor(_Interval) -> ?BASE_INTERVAL. --spec table_name(non_neg_integer()) -> string(). -table_name(Id) when is_integer(Id), Id >= 0 andalso Id < ?SHARDS_N -> - atom_to_list(?MODULE) ++ "_" ++ integer_to_list(Id). - - --spec table_names() -> [string()]. -table_names() -> - [table_name(N) || N <- lists:seq(0, ?SHARDS_N - 1)]. - - --spec term_to_table(any()) -> atom(). -term_to_table(Term) -> - PHash = erlang:phash2(Term), - list_to_existing_atom(table_name(PHash rem ?SHARDS_N)). - - -spec new_timer() -> timer:tref(). new_timer() -> {ok, Timer} = timer:send_after(?MAX_INTERVAL * 2, cleanup), diff --git a/src/couch_replicator/src/couch_replicator_rate_limiter_tables.erl b/src/couch_replicator/src/couch_replicator_rate_limiter_tables.erl new file mode 100644 index 0000000..aa087d6 --- /dev/null +++ b/src/couch_replicator/src/couch_replicator_rate_limiter_tables.erl @@ -0,0 +1,54 @@ +% 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. + + +% Maintain cluster membership and stability notifications for replications. +% On changes to cluster membership, broadcast events to `replication` gen_event. +% Listeners will get `{cluster, stable}` or `{cluster, unstable}` events. +% +% Cluster stability is defined as "there have been no nodes added or removed in +% last `QuietPeriod` seconds". QuietPeriod value is configurable. To ensure a +% speedier startup, during initialization there is a shorter StartupQuietPeriod in +% effect (also configurable). +% +% This module is also in charge of calculating ownership of replications based on +% where their _repicator db documents shards live. + +-module(couch_replicator_rate_limiter_tables). + +-export([create/1]). +-export([table_names/0]). +-export([term_to_table/1]). + +-define(SHARDS_N, 16). + + +-spec create(non_neg_integer()) -> ok. +create(KeyPos) -> + Opts = [named_table, public, {keypos, KeyPos}, {read_concurrency, true}], + [ets:new(list_to_atom(TableName), Opts) || TableName <- table_names()], + ok. + +-spec table_names() -> [string()]. +table_names() -> + [table_name(N) || N <- lists:seq(0, ?SHARDS_N - 1)]. + + +-spec term_to_table(any()) -> atom(). +term_to_table(Term) -> + PHash = erlang:phash2(Term), + list_to_existing_atom(table_name(PHash rem ?SHARDS_N)). + + +-spec table_name(non_neg_integer()) -> string(). +table_name(Id) when is_integer(Id), Id >= 0 andalso Id < ?SHARDS_N -> + atom_to_list(?MODULE) ++ "_" ++ integer_to_list(Id). -- To stop receiving notification emails like this one, please contact "commits@couchdb.apache.org" .