You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2025/03/03 21:00:39 UTC

(couchdb) 02/11: wip

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch auto-delete-3
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 2124fe35ad6813ddac4bea3c5caeb72a57ea1abb
Author: Robert Newson <rn...@apache.org>
AuthorDate: Tue Feb 4 13:14:08 2025 +0000

    wip
---
 src/couch/src/couch_bt_engine_compactor.erl        |  5 +-
 .../src/couch_replicator_scheduler_job.erl         |  2 +
 src/fabric/src/fabric.erl                          |  8 ++-
 src/fabric/src/fabric_drop_seq.erl                 | 81 ++++++++++++++++++++++
 4 files changed, 93 insertions(+), 3 deletions(-)

diff --git a/src/couch/src/couch_bt_engine_compactor.erl b/src/couch/src/couch_bt_engine_compactor.erl
index 5e9289d71..619200bce 100644
--- a/src/couch/src/couch_bt_engine_compactor.erl
+++ b/src/couch/src/couch_bt_engine_compactor.erl
@@ -322,14 +322,15 @@ copy_compact(#comp_st{} = CompSt) ->
             Deleted =
                 case DocInfo of
                     #full_doc_info{} -> DocInfo#full_doc_info.deleted;
-                    #doc_info{} -> false % Older versions stored #doc_info records in the seq_tree.
+                    % Older versions stored #doc_info records in the seq_tree.
+                    #doc_info{} -> false
                 end,
 
             AccUncopiedSize2 = AccUncopiedSize + ?term_size(DocInfo),
             if
                 Deleted andalso Seq =< DropSeq ->
                     %% drop this document completely
-                    {ok,  {AccNewSt, AccUncopied, AccUncopiedSize, AccCopiedSize}};
+                    {ok, {AccNewSt, AccUncopied, AccUncopiedSize, AccCopiedSize}};
                 AccUncopiedSize2 >= BufferSize ->
                     NewSt2 = copy_docs(
                         St, AccNewSt, lists:reverse([DocInfo | AccUncopied]), Retry
diff --git a/src/couch_replicator/src/couch_replicator_scheduler_job.erl b/src/couch_replicator/src/couch_replicator_scheduler_job.erl
index 544c5602a..21f5360d9 100644
--- a/src/couch_replicator/src/couch_replicator_scheduler_job.erl
+++ b/src/couch_replicator/src/couch_replicator_scheduler_job.erl
@@ -854,6 +854,8 @@ do_checkpoint(State) ->
             BaseHistory =
                 [
                     {<<"session_id">>, SessionId},
+                    {<<"source">>, ?l2b(SourceName)},
+                    {<<"target">>, ?l2b(TargetName)},
                     {<<"source_last_seq">>, NewSeq},
                     {<<"replication_id_version">>, ?REP_ID_VERSION}
                 ] ++
diff --git a/src/fabric/src/fabric.erl b/src/fabric/src/fabric.erl
index 93a71b035..9f3fbb767 100644
--- a/src/fabric/src/fabric.erl
+++ b/src/fabric/src/fabric.erl
@@ -33,7 +33,8 @@
     set_purge_infos_limit/3,
     get_purged_infos/1,
     compact/1, compact/2,
-    get_partition_info/2
+    get_partition_info/2,
+    calculate_drop_seq/1
 ]).
 
 % Documents
@@ -119,6 +120,11 @@ get_db_info(DbName) ->
 get_partition_info(DbName, Partition) ->
     fabric_db_partition_info:go(dbname(DbName), Partition).
 
+-spec calculate_drop_seq(dbname()) ->
+    {ok, [{node(), binary(), non_neg_integer()}]}.
+calculate_drop_seq(DbName) ->
+    fabric_drop_seq:go(dbname(DbName)).
+
 %% @doc the number of docs in a database
 %% @equiv get_doc_count(DbName, <<"_all_docs">>)
 get_doc_count(DbName) ->
diff --git a/src/fabric/src/fabric_drop_seq.erl b/src/fabric/src/fabric_drop_seq.erl
new file mode 100644
index 000000000..08562d815
--- /dev/null
+++ b/src/fabric/src/fabric_drop_seq.erl
@@ -0,0 +1,81 @@
+% 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_drop_seq).
+
+-include_lib("mem3/include/mem3.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch_mrview/include/couch_mrview.hrl").
+-include_lib("couch_replicator/src/couch_replicator.hrl").
+
+-export([go/1]).
+
+go(DbName) ->
+    MrArgs = #mrargs{
+        view_type = map,
+        include_docs = true,
+        extra = [
+            {include_system, true},
+            {namespace, <<"_local">>}
+        ]
+    },
+    {ok, LocalDocs} = fabric:all_docs(DbName, fun all_docs_cb/2, [], MrArgs),
+%    1. find lowest update seq per {node,uuid} pair in all checkpoints excluding mem3_sync
+%    2. use mem3_sync docs to find lowest update seq on any other {node,uuids}
+%    3. issue rpc to matching shards and ask them to set drop seq if uuid matches
+
+    lists:foldl(fun parse_replication_doc/2, [], LocalDocs).
+
+all_docs_cb({row, Row}, Acc0) ->
+    Acc1 =
+        case lists:keyfind(doc, 1, Row) of
+            {doc, Doc} ->
+                [couch_doc:from_json_obj(Doc) | Acc0];
+            false ->
+                Acc0
+        end,
+    {ok, Acc1};
+all_docs_cb(_Else, Acc) ->
+    {ok, Acc}.
+
+parse_replication_doc(#doc{} = Doc, Acc) ->
+    {Props} = Doc#doc.body,
+    ReplicationIdVersion = couch_util:get_value(<<"replication_id_version">>, Props),
+    case ReplicationIdVersion of
+        ?REP_ID_VERSION ->
+            SourceLastSeq = couch_util:get_value(<<"source_last_seq">>, Props),
+            [decode_seq(SourceLastSeq) | Acc];
+        _Else ->
+            Acc
+    end.
+
+parse_shard_sync_doc(#doc{id = <<"_local/shard-sync-", _/binary>>} = Doc, Acc) ->
+    {Props} = Doc#doc.body,
+    History = couch_util:get_value(<<"history">>, Props),
+    [History | Acc];
+parse_shard_sync_doc(_Doc, Acc) ->
+    Acc.
+
+
+decode_seq(OpaqueSeq) ->
+    Decoded = fabric_view_changes:decode_seq(OpaqueSeq),
+    lists:foldl(
+        fun
+            ({_Node, Range, {Seq, Uuid, Node}}, Acc) ->
+                Acc#{{Node, Range, Uuid} => Seq};
+            ({_Node, _Range, _Seq}, Acc) ->
+                Acc
+        end,
+        #{},
+        Decoded
+    ).
+