nickva commented on code in PR #4201:
URL: https://github.com/apache/couchdb/pull/4201#discussion_r998898093


##########
src/fabric/src/fabric_open_revs.erl:
##########
@@ -0,0 +1,455 @@
+% 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(fabric_open_revs).
+
+-export([
+    go/3
+]).
+
+-include_lib("mem3/include/mem3.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-record(req, {
+    idrevs,
+    wcnt = 0,
+    rcnt = 0,
+    responses = []
+}).
+
+-record(st, {
+    r,
+    args,
+    reqs,
+    workers
+}).
+
+go(_DbName, [], _Options) ->
+    {ok, []};
+go(DbName, IdsRevsOpts, Options) ->
+    St = init_state(DbName, IdsRevsOpts, Options),
+    WShards = maps:keys(St#st.workers),
+    RexiMon = fabric_util:create_monitors(WShards),
+    try fabric_util:recv(WShards, #shard.ref, fun handle_message/3, St) of
+        {timeout, #st{workers = #{} = Workers1}} ->
+            stop_workers(Workers1),
+            fabric_util:log_timeout(maps:keys(Workers1), "open_revs"),
+            {error, timeout};
+        Else ->
+            Else
+    after
+        rexi_monitor:stop(RexiMon)
+    end.
+
+handle_message([_ | _] = Resps, Worker, #st{} = St) ->
+    #st{workers = Workers, reqs = Reqs, r = R} = St,
+    {ArgsRefs, Workers1} = maps:take(Worker, Workers),
+    ArgsResps = lists:zip(ArgsRefs, Resps),
+    Reqs1 = lists:foldl(fun responses_fold/2, Reqs, ArgsResps),
+    case not r_met(Reqs1, R) andalso have_viable_workers(Workers1) of
+        true ->
+            {ok, St#st{workers = Workers1, reqs = Reqs1}};
+        false ->
+            stop_workers(Workers1),
+            {stop, finalize(St#st.args, Reqs1)}
+    end;
+handle_message({rexi_DOWN, _, {_, NodeRef}, _}, _Worker, #st{} = St) ->
+    #st{workers = Workers, reqs = Reqs} = St,
+    FilterFun = fun(#shard{node = N}) -> N =:= NodeRef end,
+    DeadKeys = lists:filter(FilterFun, maps:keys(Workers)),
+    Workers1 = maps:without(DeadKeys, Workers),
+    DeadWorkers = maps:with(DeadKeys, Workers),
+    FoldFun = fun(_, ArgRefs, Acc) -> update_wcnt(Acc, ArgRefs, -1) end,
+    Reqs1 = maps:fold(FoldFun, Reqs, DeadWorkers),
+    Error = {error, {nodedown, <<"progress not possible">>}},
+    handle_error(Error, St#st{workers = Workers1, reqs = Reqs1});
+handle_message({rexi_EXIT, Reason}, Worker, #st{} = St) ->
+    handle_message(Reason, Worker, St);
+handle_message({error, Reason}, Worker, #st{} = St) ->
+    handle_message(Reason, Worker, St);
+handle_message(Reason, Worker, #st{} = St) ->
+    #st{workers = Workers, reqs = Reqs} = St,
+    {DeadArgRefs, Workers1} = maps:take(Worker, Workers),
+    Reqs1 = update_wcnt(Reqs, DeadArgRefs, -1),
+    handle_error(Reason, St#st{workers = Workers1, reqs = Reqs1}).
+
+init_state(DbName, IdsRevsOpts, Options) ->
+    DefaultR = integer_to_list(mem3:quorum(DbName)),
+    R = list_to_integer(couch_util:get_value(r, Options, DefaultR)),
+    {ArgRefs, Reqs0} = build_req_map(IdsRevsOpts),
+    ShardMap = build_worker_map(DbName, Reqs0),
+    {Workers, Reqs} = spawn_workers(Reqs0, ShardMap, Options),
+    #st{r = R, args = ArgRefs, reqs = Reqs, workers = Workers}.
+
+responses_fold({ArgRef, NewResp}, #{} = Reqs) ->
+    #{ArgRef := Req} = Reqs,
+    #req{rcnt = R, wcnt = W, responses = Resps} = Req,
+    % De-duplicate identical results as we go along
+    Resps1 = lists:umerge(Resps, lists:usort(NewResp)),
+    % If responses don't match or got a "not found", don't bump rcnt so we can
+    % wait for more workers.
+    NewR =
+        case {is_not_found(NewResp), length(Resps)} of
+            {true, _} -> R;
+            {_, 0} -> R + 1;
+            {_, OldLen} when OldLen == length(Resps1) -> R + 1;

Review Comment:
   I updated this logic a bit to extract the `OldLen` and `NewLen` out of the 
case statement. It's a tiny bit less efficient but more clear to read.
   
   ```erlang
    case {any_not_found(NewResp), OldLen} of
               {true, _} -> R;
               {_, 0} -> R + 1;
               {_, L} when L == NewLen -> R + 1;
               {_, L} when L < NewLen -> R
           end,
   ```
   
   In this case `{_, 0}` would not be covered by` {_, L} when L == NewLen -> R 
+ 1` as  when the first response comes `OldLen =0` but `NewLen` would be 1. So, 
they are different, however we do want to bump the `R` count because it's the 
only response we received and don't have a reason to consider that it's a 
diverging result.
   
   As soon as we have more than one response (`OldLen = 1`) then we switch the 
logic and bump the `R` count only if we start getting the same length, that is 
we get the same response. If we get different responses (`OldLen < NewLen`) we 
stop bumping it because we're hoping to get a more worker responses.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to