iilyak commented on code in PR #5558:
URL: https://github.com/apache/couchdb/pull/5558#discussion_r2155460403


##########
src/fabric/src/fabric_drop_seq.erl:
##########
@@ -0,0 +1,1016 @@
+-module(fabric_drop_seq).
+
+-include_lib("mem3/include/mem3.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch_mrview/include/couch_mrview.hrl").
+
+-export([go/1]).
+
+-export([
+    create_peer_checkpoint_doc_if_missing/5,
+    update_peer_checkpoint_doc/5,
+    cleanup_peer_checkpoint_docs/3,
+    peer_checkpoint_doc/4,
+    peer_id_from_sig/2
+]).
+
+%% rpc
+-export([gather_drop_seq_info_rpc/1]).
+
+-type range() :: [non_neg_integer()].
+
+-type uuid() :: binary().
+
+-type seq() :: non_neg_integer().
+
+-type uuid_map() :: #{{Range :: range(), Node :: node()} => uuid()}.
+
+-type peer_checkpoints() :: #{{range(), Node :: node()} => {Uuid :: uuid(), 
Seq :: seq()}}.
+
+-type history_item() :: {
+    SourceUuid :: uuid(), SourceSeq :: seq(), TargetUuid :: uuid(), TargetSeq 
:: seq()
+}.
+
+-type shard_sync_history() :: #{
+    {Range :: range(), SourceNode :: node(), TargetNode :: node()} => 
[history_item()]
+}.
+
+go(DbName) ->
+    Shards0 = mem3:shards(DbName),
+    case gather_drop_seq_info(Shards0) of
+        {error, Reason} ->
+            {error, Reason};
+        {ok, #{
+            uuid_map := UuidMap,
+            peer_checkpoints := PeerCheckpoints,
+            shard_sync_history := ShardSyncHistory
+        }} ->
+            Shards1 = fully_replicated_shards_only(Shards0, ShardSyncHistory),
+            DropSeqs = calculate_drop_seqs(
+                Shards0, UuidMap, PeerCheckpoints, ShardSyncHistory
+            ),
+            Workers = lists:filtermap(
+                fun(Shard) ->
+                    #shard{range = Range, node = Node, name = ShardName} = 
Shard,
+                    case maps:find({Range, Node}, DropSeqs) of
+                        {ok, {_UuidPrefix, 0}} ->
+                            false;
+                        {ok, {UuidPrefix, DropSeq}} ->
+                            Ref = rexi:cast(
+                                Node,
+                                {fabric_rpc, set_drop_seq, [
+                                    ShardName, UuidPrefix, DropSeq, 
[?ADMIN_CTX]
+                                ]}
+                            ),
+                            {true, Shard#shard{ref = Ref, opts = [{drop_seq, 
DropSeq}]}};
+                        error ->
+                            false
+                    end
+                end,
+                Shards1
+            ),
+            if
+                Workers == [] ->
+                    %% nothing to do
+                    {ok, #{}};
+                true ->
+                    RexiMon = fabric_util:create_monitors(Shards1),
+                    Acc0 = {#{}, length(Workers) - 1},
+                    try
+                        case
+                            fabric_util:recv(
+                                Workers, #shard.ref, fun 
handle_set_drop_seq_reply/3, Acc0
+                            )
+                        of
+                            {ok, Results} ->
+                                {ok, Results};
+                            {timeout, {WorkersDict, _}} ->
+                                DefunctWorkers = 
fabric_util:remove_done_workers(
+                                    WorkersDict,
+                                    nil
+                                ),
+                                fabric_util:log_timeout(
+                                    DefunctWorkers,
+                                    "set_drop_seq"
+                                ),
+                                {error, timeout};
+                            {error, Reason} ->
+                                {error, Reason}
+                        end
+                    after
+                        rexi_monitor:stop(RexiMon)
+                    end
+            end
+    end.
+
+-spec calculate_drop_seqs([#shard{}], uuid_map(), peer_checkpoints(), 
shard_sync_history()) ->
+    peer_checkpoints().
+calculate_drop_seqs(Shards, UuidMap, PeerCheckpoints0, ShardSyncHistory) ->
+    PeerCheckpoints1 = substitute_splits(Shards, UuidMap, PeerCheckpoints0),
+    PeerCheckpoints2 = crossref(PeerCheckpoints1, ShardSyncHistory),
+    ShardSyncCheckpoints = latest_shard_sync_checkpoints(ShardSyncHistory),
+    maps:merge_with(fun merge_peers/3, PeerCheckpoints2, ShardSyncCheckpoints).
+
+handle_set_drop_seq_reply(ok, Worker, {Results0, Waiting}) ->
+    DropSeq = proplists:get_value(drop_seq, Worker#shard.opts),
+    [B, E] = Worker#shard.range,
+    BHex = couch_util:to_hex(<<B:32/integer>>),
+    EHex = couch_util:to_hex(<<E:32/integer>>),
+    Range = list_to_binary([BHex, "-", EHex]),

Review Comment:
   There is an optimized version of `to_hex` which avoids extra conversion.
   
   ```erlang
   BHex = couch_util:to_hex_bin(<<B:32/integer>>),
   EHex = couch_util:to_hex_bin(<<E:32/integer>>),
   <<BHex/binary,"-",EHex/binary>>,
   ```



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