nickva commented on code in PR #4703: URL: https://github.com/apache/couchdb/pull/4703#discussion_r1313230483
########## src/fabric/src/fabric_db_purged_infos.erl: ########## @@ -0,0 +1,85 @@ +% 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_db_purged_infos). + +-export([go/1]). + +-include_lib("fabric/include/fabric.hrl"). +-include_lib("mem3/include/mem3.hrl"). + +-record(pacc, { + counters, + replies, + ring_opts +}). + +go(DbName) -> + Shards = mem3:shards(DbName), + Workers = fabric_util:submit_jobs(Shards, get_purged_infos, []), + RexiMon = fabric_util:create_monitors(Shards), + Fun = fun handle_message/3, + Acc0 = #pacc{ + counters = fabric_dict:init(Workers, nil), + replies = [], + ring_opts = [{any, Shards}] + }, + try + case fabric_util:recv(Workers, #shard.ref, Fun, Acc0) of + {ok, Res} -> + {ok, Res}; + {timeout, {WorkersDict, _}} -> + DefunctWorkers = fabric_util:remove_done_workers(WorkersDict, nil), + fabric_util:log_timeout(DefunctWorkers, "get_purged_infos"), + {error, timeout}; + {error, Error} -> + throw(Error) + end + after + rexi_monitor:stop(RexiMon) + end. + +handle_message({rexi_DOWN, _, {_, NodeRef}, _}, _Shard, #pacc{} = Acc) -> + #pacc{counters = Counters, ring_opts = RingOpts} = Acc, + case fabric_util:remove_down_workers(Counters, NodeRef, RingOpts) of + {ok, NewCounters} -> + {ok, Acc#pacc{counters = NewCounters}}; + error -> + {error, {nodedown, <<"progress not possible">>}} + end; +handle_message({rexi_EXIT, Reason}, Shard, #pacc{} = Acc) -> + #pacc{counters = Counters, ring_opts = RingOpts} = Acc, + NewCounters = fabric_dict:erase(Shard, Counters), + case fabric_ring:is_progress_possible(NewCounters, RingOpts) of + true -> + {ok, Acc#pacc{counters = NewCounters}}; + false -> + {error, Reason} + end; +handle_message({ok, Info}, #shard{} = Shard, #pacc{} = Acc) -> + #pacc{counters = Counters, replies = Replies} = Acc, + Replies1 = [Info | Replies], + Counters1 = fabric_dict:erase(Shard, Counters), + case fabric_dict:size(Counters1) =:= 0 of + true -> + {stop, [{purged_infos, format_purged_infos(Replies1)}]}; + false -> + {ok, Acc#pacc{counters = Counters1, replies = Replies1}} + end; +handle_message(_, _, #pacc{} = Acc) -> + {ok, Acc}. + +format_purged_infos(Info) -> + [ + {[{id, Id}, {revs, [couch_doc:rev_to_str(Rev) || Rev <- Revs]}]} + || {Id, Revs} <- lists:flatten(Info) + ]. Review Comment: I don't think fabric should try too hard to format the response into a json form. It should return values as if we would use them from Erlang (internally) that means keeping ids and `#doc` records etc, as is. Any rev transformations or building json objects would be done in the `chttpd_db`, so I think might work to return `{ok, lists:flatten(Infos)}` here ,skipping the format_purged_infos altogether perhaps. Then do the transform in chttpd_db. -- 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]
