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 2024/12/11 10:29:36 UTC

(couchdb) 01/02: wip

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

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

commit 331baf291ab5a1bdfb3957d66c297826f1de7068
Author: Robert Newson <rn...@apache.org>
AuthorDate: Wed Dec 11 10:21:55 2024 +0000

    wip
---
 src/couch_mrview/src/couch_mrview_cleanup.erl | 12 ++++++------
 src/couch_mrview/src/couch_mrview_util.erl    | 25 ++++++++++++++++++-------
 src/fabric/src/fabric.erl                     | 17 +++++++++--------
 3 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/src/couch_mrview/src/couch_mrview_cleanup.erl b/src/couch_mrview/src/couch_mrview_cleanup.erl
index 5b5afbdce..f1b671dd5 100644
--- a/src/couch_mrview/src/couch_mrview_cleanup.erl
+++ b/src/couch_mrview/src/couch_mrview_cleanup.erl
@@ -14,7 +14,7 @@
 
 -export([
     run/1,
-    cleanup_purges/3,
+    cleanup_checkpoints/3,
     cleanup_indices/2
 ]).
 
@@ -25,14 +25,14 @@ run(Db) ->
     Checkpoints = couch_mrview_util:get_purge_checkpoints(Db),
     {ok, Db1} = couch_db:reopen(Db),
     Sigs = couch_mrview_util:get_signatures(Db1),
-    ok = cleanup_purges(Db1, Sigs, Checkpoints),
+    ok = cleanup_checkpoints(Db1, Sigs, Checkpoints),
     ok = cleanup_indices(Sigs, Indices).
 
-cleanup_purges(DbName, Sigs, Checkpoints) when is_binary(DbName) ->
+cleanup_checkpoints(DbName, Sigs, Checkpoints) when is_binary(DbName) ->
     couch_util:with_db(DbName, fun(Db) ->
-        cleanup_purges(Db, Sigs, Checkpoints)
+        cleanup_checkpoints(Db, Sigs, Checkpoints)
     end);
-cleanup_purges(Db, #{} = Sigs, #{} = CheckpointsMap) ->
+cleanup_checkpoints(Db, #{} = Sigs, #{} = CheckpointsMap) ->
     InactiveMap = maps:without(maps:keys(Sigs), CheckpointsMap),
     InactiveCheckpoints = maps:values(InactiveMap),
     DeleteFun = fun(DocId) -> delete_checkpoint(Db, DocId) end,
@@ -57,7 +57,7 @@ delete_file(File) ->
 
 delete_checkpoint(Db, DocId) ->
     DbName = couch_db:name(Db),
-    LogMsg = "~p : deleting inactive purge checkpoint ~s : ~s",
+    LogMsg = "~p : deleting inactive checkpoint ~s : ~s",
     couch_log:debug(LogMsg, [?MODULE, DbName, DocId]),
     try couch_db:open_doc(Db, DocId, []) of
         {ok, Doc = #doc{}} ->
diff --git a/src/couch_mrview/src/couch_mrview_util.erl b/src/couch_mrview/src/couch_mrview_util.erl
index a478685da..c27150453 100644
--- a/src/couch_mrview/src/couch_mrview_util.erl
+++ b/src/couch_mrview/src/couch_mrview_util.erl
@@ -36,6 +36,7 @@
 -export([get_collator_versions/1]).
 -export([compact_on_collator_upgrade/0]).
 -export([commit_on_header_upgrade/0]).
+-export([get_mrview_checkpoints/1]).
 
 -define(MOD, couch_mrview_index).
 -define(GET_VIEW_RETRY_COUNT, 1).
@@ -114,18 +115,28 @@ get_signatures(Db) ->
 % Returns a map of `Sig => DocId` elements for all the purge view
 % checkpoint docs. Sig is a hex-encoded binary.
 %
-get_purge_checkpoints(DbName) when is_binary(DbName) ->
-    couch_util:with_db(DbName, fun get_purge_checkpoints/1);
-get_purge_checkpoints(Db) ->
+get_purge_checkpoints(DbOrName) ->
+    get_registered_peers(DbOrName, <<?LOCAL_DOC_PREFIX, "purge-mrview-">>).
+
+% Returns a map of `Sig => DocId` elements for all the mrview
+% checkpoint docs. Sig is a hex-encoded binary.
+%
+get_mrview_checkpoints(DbOrName) ->
+    get_registered_peers(DbOrName, <<?LOCAL_DOC_PREFIX, "mrview-">>).
+
+get_registered_peers(DbName, Prefix) when is_binary(DbName), is_binary(Prefix) ->
+    couch_util:with_db(DbName, fun(Db) -> get_registered_peers(Db, Prefix) end);
+get_registered_peers(Db, Prefix) ->
     FoldFun = fun(#doc{id = Id}, Acc) ->
-        case Id of
-            <<?LOCAL_DOC_PREFIX, "purge-mrview-", Sig/binary>> ->
-                {ok, Acc#{Sig => Id}};
+        case binary:match(Id, Prefix) of
+            {0, Len} ->
+                Sig = binary:part(Id, byte_size(Id) - Len),
+                    {ok, Acc#{Sig => Id}};
             _ ->
                 {stop, Acc}
         end
     end,
-    Opts = [{start_key, <<?LOCAL_DOC_PREFIX, "purge-mrview-">>}],
+    Opts = [{start_key, Prefix}],
     {ok, Signatures = #{}} = couch_db:fold_local_docs(Db, FoldFun, #{}, Opts),
     Signatures.
 
diff --git a/src/fabric/src/fabric.erl b/src/fabric/src/fabric.erl
index 343b12563..c717e0040 100644
--- a/src/fabric/src/fabric.erl
+++ b/src/fabric/src/fabric.erl
@@ -574,10 +574,10 @@ reset_validation_funs(DbName) ->
     ].
 
 %% @doc clean up index files for all Dbs
--spec cleanup_index_files() -> [ok].
+-spec cleanup_index_files() -> ok.
 cleanup_index_files() ->
     {ok, Dbs} = fabric:all_dbs(),
-    [cleanup_index_files(Db) || Db <- Dbs].
+    lists:foreach(fun cleanup_index_files/1, Dbs).
 
 %% @doc clean up index files for a specific db
 -spec cleanup_index_files(dbname()) -> ok.
@@ -595,15 +595,16 @@ cleanup_local_indices_and_purge_checkpoints([]) ->
 cleanup_local_indices_and_purge_checkpoints([_ | _] = Dbs) ->
     AllIndices = lists:map(fun couch_mrview_util:get_index_files/1, Dbs),
     AllPurges = lists:map(fun couch_mrview_util:get_purge_checkpoints/1, Dbs),
+    AllCheckpoints = lists:map(fun couch_mrview_util:get_mrview_checkpoints/1, Dbs),
     Sigs = couch_mrview_util:get_signatures(hd(Dbs)),
-    ok = cleanup_purges(Sigs, AllPurges, Dbs),
+    ok = cleanup_checkpoints(Sigs, AllPurges ++ AllCheckpoints, Dbs),
     ok = cleanup_indices(Sigs, AllIndices).
 
-cleanup_purges(Sigs, AllPurges, Dbs) ->
-    Fun = fun(DbPurges, Db) ->
-        couch_mrview_cleanup:cleanup_purges(Db, Sigs, DbPurges)
+cleanup_checkpoints(Sigs, AllCheckpoints, Dbs) ->
+    Fun = fun(Checkpoints, Db) ->
+        couch_mrview_cleanup:cleanup_checkpoints(Db, Sigs, Checkpoints)
     end,
-    lists:zipwith(Fun, AllPurges, Dbs),
+    lists:zipwith(Fun, AllCheckpoints, Dbs),
     ok.
 
 cleanup_indices(Sigs, AllIndices) ->
@@ -613,7 +614,7 @@ cleanup_indices(Sigs, AllIndices) ->
     lists:foreach(Fun, AllIndices).
 
 %% @doc clean up index files for a specific db on all nodes
--spec cleanup_index_files_all_nodes(dbname()) -> [reference()].
+-spec cleanup_index_files_all_nodes(dbname()) -> ok.
 cleanup_index_files_all_nodes(DbName) ->
     lists:foreach(
         fun(Node) ->