jiangphcn commented on a change in pull request #1370: [5/5] Clustered Purge Implementation URL: https://github.com/apache/couchdb/pull/1370#discussion_r195361091
########## File path: src/fabric/src/fabric_doc_purge.erl ########## @@ -0,0 +1,572 @@ +% 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_doc_purge). + + +-export([ + go/3 +]). + + +-include_lib("fabric/include/fabric.hrl"). +-include_lib("mem3/include/mem3.hrl"). + + +-record(acc, { + worker_uuids, + resps, + uuid_counts, + w +}). + + +go(_, [], _) -> + {ok, []}; +go(DbName, IdsRevs, Options) -> + % Generate our purge requests of {UUID, DocId, Revs} + {UUIDs, Reqs} = create_reqs(IdsRevs, [], []), + + % Fire off rexi workers for each shard. + {Workers, WorkerUUIDs} = dict:fold(fun(Shard, ShardReqs, {Ws, WUUIDs}) -> + #shard{name = ShardDbName, node = Node} = Shard, + Args = [ShardDbName, ShardReqs, Options], + Ref = rexi:cast(Node, {fabric_rpc, purge_docs, Args}), + Worker = Shard#shard{ref=Ref}, + ShardUUIDs = [UUID || {UUID, _Id, _Revs} <- ShardReqs], + {[Worker | Ws], [{Worker, ShardUUIDs} | WUUIDs]} + end, {[], []}, group_reqs_by_shard(DbName, Reqs)), + + UUIDCounts = lists:foldl(fun({_Worker, WUUIDs}, CountAcc) -> + lists:foldl(fun(UUID, InnerCountAcc) -> + dict:update_counter(UUID, 1, InnerCountAcc) + end, CountAcc, WUUIDs) + end, dict:new(), WorkerUUIDs), + + RexiMon = fabric_util:create_monitors(Workers), + Timeout = fabric_util:request_timeout(), + Acc0 = #acc{ + worker_uuids = WorkerUUIDs, + resps = dict:from_list([{UUID, []} || UUID <- UUIDs]), + uuid_counts = UUIDCounts, + w = w(DbName, Options) + }, + Acc2 = try rexi_utils:recv(Workers, #shard.ref, + fun handle_message/3, Acc0, infinity, Timeout) of + {ok, Acc1} -> + Acc1; + {timeout, Acc1} -> + #acc{ + worker_uuids = WorkerUUIDs, + resps = Resps + } = Acc1, + DefunctWorkers = [Worker || {Worker, _} <- WorkerUUIDs], + fabric_util:log_timeout(DefunctWorkers, "purge_docs"), + NewResps = append_errors(timeout, WorkerUUIDs, Resps), + Acc1#acc{worker_uuids = [], resps = NewResps}; + Else -> + Else + after + rexi_monitor:stop(RexiMon) + end, + + FinalResps = format_resps(UUIDs, Acc2), + {resp_health(FinalResps), FinalResps}. + + +handle_message({rexi_DOWN, _, {_, Node}, _}, _Worker, Acc) -> + #acc{ + worker_uuids = WorkerUUIDs, + resps = Resps + } = Acc, + Pred = fun({#shard{node = N}, _}) -> N == Node end, + {Failed, Rest} = lists:partition(Pred, WorkerUUIDs), + NewResps = append_errors(internal_server_error, Failed, Resps), + maybe_stop(Acc#acc{worker_uuids = Rest, resps = NewResps}); + +handle_message({rexi_EXIT, _}, Worker, Acc) -> + #acc{ + worker_uuids = WorkerUUIDs, + resps = Resps + } = Acc, + {value, WorkerPair, Rest} = lists:keytake(Worker, 1, WorkerUUIDs), + NewResps = append_errors(internal_server_error, [WorkerPair], Resps), + maybe_stop(Acc#acc{worker_uuids = Rest, resps = NewResps}); + +handle_message({ok, Replies}, Worker, Acc) -> + #acc{ + worker_uuids = WorkerUUIDs, + resps = Resps + } = Acc, + {value, {_W, UUIDs}, Rest} = lists:keytake(Worker, 1, WorkerUUIDs), + NewResps = append_resps(UUIDs, Replies, Resps), + maybe_stop(Acc#acc{worker_uuids = Rest, resps = NewResps}); + +handle_message({bad_request, Msg}, _, _) -> + throw({bad_request, Msg}). + + +create_reqs([], UUIDs, Reqs) -> + {lists:reverse(UUIDs), lists:reverse(Reqs)}; + +create_reqs([{Id, Revs} | RestIdsRevs], UUIDs, Reqs) -> + UUID = couch_uuids:new(), + NewUUIDs = [UUID | UUIDs], + NewReqs = [{UUID, Id, Revs} | Reqs], + create_reqs(RestIdsRevs, NewUUIDs, NewReqs). + + +group_reqs_by_shard(DbName, Reqs) -> + lists:foldl(fun({_UUID, Id, _Revs} = Req, D0) -> + lists:foldl(fun(Shard, D1) -> + dict:append(Shard, Req, D1) + end, D0, mem3:shards(DbName, Id)) + end, dict:new(), Reqs). + + +w(DbName, Options) -> + try + list_to_integer(couch_util:get_value(w, Options)) + catch _:_ -> + mem3:quorum(DbName) + end. + + +append_errors(Type, WorkerUUIDs, Resps) -> + lists:foldl(fun({_Worker, UUIDs}, RespAcc) -> + Errors = [{error, Type} || _UUID <- UUIDs], + append_resps(UUIDs, Errors, RespAcc) + end, Resps, WorkerUUIDs). + + +append_resps([], [], Resps) -> + Resps; +append_resps([UUID | RestUUIDs], [Reply | RestReplies], Resps) -> + NewResps = dict:append(UUID, Reply, Resps), + append_resps(RestUUIDs, RestReplies, NewResps). + + +maybe_stop(#acc{worker_uuids = []} = Acc) -> + {stop, Acc}; +maybe_stop(#acc{resps = Resps, uuid_counts = Counts, w = W} = Acc) -> + try + dict:fold(fun(UUID, UUIDResps, _) -> + UUIDCount = dict:fetch(UUID, Counts), + case has_quorum(UUIDResps, UUIDCount, W) of + true -> ok; + false -> throw(keep_going) + end + end, nil, Resps), + {stop, Acc} + catch throw:keep_going -> + {ok, Acc} + end. + + +format_resps(UUIDs, #acc{} = Acc) -> + #acc{ + resps = Resps, + w = W + } = Acc, + FoldFun = fun(UUID, Replies, ReplyAcc) -> + OkReplies = [Reply || {ok, Reply} <- Replies], + case OkReplies of + [] -> + [Error | _] = lists:usort(Replies), + [{UUID, Error} | ReplyAcc]; + _ -> + AllRevs = lists:usort(lists:flatten(OkReplies)), + IsOk = length(OkReplies) >= W + andalso length(lists:usort(OkReplies)) == 1, + Health = if IsOk -> ok; true -> accepted end, + [{UUID, {Health, AllRevs}} | ReplyAcc] + end + end, + FinalReplies = dict:fold(FoldFun, {ok, []}, Resps), + couch_util:reorder_results(UUIDs, FinalReplies); + +format_resps(_UUIDs, Else) -> + Else. + + +resp_health(Resps) -> + Healths = lists:usort([H || {H, _} <- Resps]), + HasError = lists:member(error, Healths), + HasAccepted = lists:member(accepted, Healths), + AllOk = Healths == [ok], + if + HasError -> error; + HasAccepted -> accepted; + AllOk -> ok; + true -> error + end. + + +has_quorum(Resps, Count, W) -> + OkResps = [R || {ok, _} = R <- Resps], + OkCounts = lists:foldl(fun(R, Acc) -> + dict:update_counter(R, 1, Acc) + end, dict:new(), OkResps), + MaxOk = lists:max([0 | element(2, lists:unzip(dict:to_list(OkCounts)))]), Review comment: addressed in commit 9038557 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
