You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2020/09/08 16:54:10 UTC

[GitHub] [couchdb] nickva commented on a change in pull request #3127: Prototype/fdb layer replace couch rate

nickva commented on a change in pull request #3127:
URL: https://github.com/apache/couchdb/pull/3127#discussion_r485052752



##########
File path: src/couch_views/src/couch_views_indexer.erl
##########
@@ -165,48 +171,60 @@ add_error(Error, Reason, Data) ->
 
 
 update(#{} = Db, Mrst0, State0) ->
-    Limiter = maps:get(limiter, State0),
-    case couch_rate:budget(Limiter) of
-        0 ->
-            couch_rate:wait(Limiter),
-            update(Db, Mrst0, State0);
-        Limit ->
-            {Mrst1, State1} = do_update(Db, Mrst0, State0#{limit => Limit, limiter => Limiter}),
-            case State1 of
-                finished ->
-                    couch_eval:release_map_context(Mrst1#mrst.qserver);
-                _ ->
-                    couch_rate:wait(Limiter),
-                    update(Db, Mrst1, State1)
+    Limit = couch_views_batch:start(),
+    try
+        {Mrst1, State1} = do_update(Db, Mrst0, State0#{limit => Limit}),
+        case State1 of
+            finished ->
+                couch_eval:release_map_context(Mrst1#mrst.qserver);
+            _ ->
+                #{
+                    docs_read := DocsRead,
+                    tx_size := TxSize
+                } = State1,
+                couch_views_batch:success(TxSize, DocsRead),
+                update(Db, Mrst1, State1)
+        end
+    catch
+        error:{erlfdb_error, Error} ->
+            case lists:member(Error, ?RETRY_FAILURES) of
+                true ->
+                    couch_views_batch:failure(),
+                    update(Db, Mrst0, State0);
+                false ->
+                    erlang:error({erlfdb_error, Error})

Review comment:
       Will this loose the original stacktrace? It might not be as nice but we could defined ?RETRY_FAILURES as a guard maybe. Would this work:
   
   ```erlang
   -define(RETRY_FAILURE(E), (E =:= 1007 orelse E =:= 1031 ...)).
   ```
   And then say `catch error:{erlfdb_error, E} when ?RETRY_FAILURE(E) ...` ?

##########
File path: src/couch_views/src/couch_views_indexer.erl
##########
@@ -165,48 +171,60 @@ add_error(Error, Reason, Data) ->
 
 
 update(#{} = Db, Mrst0, State0) ->
-    Limiter = maps:get(limiter, State0),
-    case couch_rate:budget(Limiter) of
-        0 ->
-            couch_rate:wait(Limiter),
-            update(Db, Mrst0, State0);
-        Limit ->
-            {Mrst1, State1} = do_update(Db, Mrst0, State0#{limit => Limit, limiter => Limiter}),
-            case State1 of
-                finished ->
-                    couch_eval:release_map_context(Mrst1#mrst.qserver);
-                _ ->
-                    couch_rate:wait(Limiter),
-                    update(Db, Mrst1, State1)
+    Limit = couch_views_batch:start(),
+    try
+        {Mrst1, State1} = do_update(Db, Mrst0, State0#{limit => Limit}),
+        case State1 of
+            finished ->
+                couch_eval:release_map_context(Mrst1#mrst.qserver);
+            _ ->
+                #{
+                    docs_read := DocsRead,
+                    tx_size := TxSize
+                } = State1,
+                couch_views_batch:success(TxSize, DocsRead),
+                update(Db, Mrst1, State1)

Review comment:
       Because of the catch that wraps it, would this still be tail recursive? Say if we call this 1M times would it have to keep the stack frames around to unwind after the call?

##########
File path: src/couch_views/src/couch_views_indexer.erl
##########
@@ -78,10 +86,10 @@ init() ->
         fail_job(Job, Data, sig_changed, "Design document was modified")
     end,
 
-    Limiter = couch_rate:create_if_missing({DbName, DDocId}, "views"),
-
     State = #{
         tx_db => undefined,
+        tx_size => 0,

Review comment:
       Minor nit: would these make sense emitted as metrics. It could a separate PR though

##########
File path: src/couch_views/src/couch_views_batch.erl
##########
@@ -0,0 +1,68 @@
+% 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_views_batch).
+
+
+-export([
+    start/0,
+    success/2,
+    failure/0
+]).
+
+
+-callback start(State::term()) -> {NewState::term(), BatchSize::pos_integer()}.
+-callback success(
+            TxSize::non_neg_integer(),
+            DocsRead::non_neg_integer(),
+            State::term()
+        ) -> NewState::term().
+-callback failure(State::term()) -> NewState::term().
+
+
+-define(DEFAULT_MOD, "couch_views_batch_impl").
+
+
+start() ->
+    {Mod, State} = case load_state() of
+        {M, S} ->
+            {M, S};
+        undefined ->
+            ModStr = config:get("couch_views", "batch_module", ?DEFAULT_MOD),
+            ModAtom = list_to_existing_atom(ModStr),
+            {ModAtom, undefined}
+    end,
+    {NewState, BatchSize} = Mod:start(State),

Review comment:
       Wonder if it makes sense to pass in dbname and/or ddoc id to the `Mod:start/1` so the batch maximizer could apply different rules to different databases or design docs perhaps. For example, like we pass in the DocsRead below even though we don't actually use it in the basic implementation.
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org