couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vatam...@apache.org
Subject [couchdb] 02/04: [fixup] move rate limiter tables to separate module
Date Sun, 09 Apr 2017 04:02:43 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 a5d03007e2ab143f3fd208b6a1b7a7a85c543abc
Author: Nick Vatamaniuc <vatamane@apache.org>
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" <commits@couchdb.apache.org>.

Mime
View raw message