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

nickva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/main by this push:
     new eafed9939 Handle merging same rev but with a different parent
eafed9939 is described below

commit eafed9939fe88de296d8875cf3b2ace5be3293cb
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Wed Nov 4 17:57:43 2026 -0500

    Handle merging same rev but with a different parent
    
    Treat it as a new revision instead creating a broken rev tree.
    
    Since #6168 when we replicated a revision we already had locally, we kept 
its
    stored value (just updated the path, since it may have connected some 
ancestry
    paths together). However, since then, if a new rev's parent disagrees with 
the
    stored version's parent, for example, if we have stored 1-a -> 2-x -> 3-c, 
and
    got 1-z -> 2-x, the path couldn't join the existing branch and became a new 
one
    with 2-x as a leaf that had nothing stored. That's an invalid rev tree, and
    after that, opening the doc (including _changes, _all_docs) would fail with 
a
    function clause.
    
    This was found thanks to Bryan Green.
    
    Related: #6170
---
 src/couch/src/couch_db_updater.erl                 | 43 ++++++++++++++----
 .../test/eunit/couchdb_update_conflicts_tests.erl  | 52 +++++++++++++++++++++-
 2 files changed, 85 insertions(+), 10 deletions(-)

diff --git a/src/couch/src/couch_db_updater.erl 
b/src/couch/src/couch_db_updater.erl
index 5312e189a..76f8fd338 100644
--- a/src/couch/src/couch_db_updater.erl
+++ b/src/couch/src/couch_db_updater.erl
@@ -21,6 +21,8 @@
 
 % 10 GiB
 -define(DEFAULT_MAX_PARTITION_SIZE, 16#280000000).
+% Stem depth to check for duplicate rev parents
+-define(STEM_DUP_CHECK_DEPTH, 16#7FFFFFFF).
 
 -record(merge_acc, {
     revs_limit,
@@ -624,9 +626,8 @@ merge_rev_tree(_Db, OldInfo, NewDoc, _Client, true) ->
     % We're merging in revisions without caring about
     % conflicts. Most likely this is a replication update.
     OldTree = OldInfo#full_doc_info.rev_tree,
-    case replicated_path(OldTree, NewDoc) of
-        {ok, NewTree0} ->
-            {NewTree, _} = couch_key_tree:merge(OldTree, NewTree0),
+    case replicated_merge(OldTree, NewDoc) of
+        {ok, NewTree} ->
             OldInfo#full_doc_info{rev_tree = NewTree};
         ignore ->
             OldInfo
@@ -640,8 +641,16 @@ merge_rev_tree(_Db, OldInfo, NewDoc, _Client, true) ->
 % node of a longer path in the same request) there is nothing to store and it
 % is ignored, otherwise it's any revision not in the tree is a new revision.
 % revision not in the tree is new.
-replicated_path(OldTree, #doc{revs = {Pos, [RevId | _]}, meta = Meta} = Doc) ->
+%
+% When we merge, we may get a strange case of a revision parent being different
+% than what we already have in the tree. In that case after the merge we could
+% end up with a ?REV_MISSIING leaf (since merge aborts). In that case we merge
+% the sent Path body into the tree. It will create a conflict but that is the
+% best we can do in such cases and is the behavior we always had.
+%
+replicated_merge(OldTree, #doc{revs = {Pos, [RevId | _]}, meta = Meta} = Doc) 
->
     Path = couch_doc:to_path(Doc),
+    RevsOnly = lists:keymember(revs_only, 1, Meta),
     case couch_key_tree:get_full_key_paths(OldTree, [{Pos, RevId}]) of
         {[{Pos, [{RevId, Value} | _]} | _], []} ->
             Fun = fun
@@ -649,14 +658,30 @@ replicated_path(OldTree, #doc{revs = {Pos, [RevId | _]}, 
meta = Meta} = Doc) ->
                 (_Rev, V, branch) -> V
             end,
             [Path1] = couch_key_tree:map(Fun, [Path]),
-            {ok, Path1};
+            case couch_key_tree:merge(OldTree, Path1) of
+                {NewTree, internal_node} ->
+                    {ok, NewTree};
+                {NewTree, _} ->
+                    Folded = couch_key_tree:stem(NewTree, 
?STEM_DUP_CHECK_DEPTH),
+                    Leafs = couch_key_tree:get_all_leafs(Folded),
+                    % If we're creating a new branch/leaf and have a
+                    % REV_MISSING at a leaf and it's not a RevOnly update, put
+                    % the incoming body (Path) in that position.
+                    case lists:keymember(?REV_MISSING, 1, Leafs) of
+                        false -> {ok, Folded};
+                        true -> replicated_new_rev(OldTree, Path, RevsOnly)
+                    end
+            end;
         _ ->
-            case lists:keymember(revs_only, 1, Meta) of
-                true -> ignore;
-                false -> {ok, Path}
-            end
+            replicated_new_rev(OldTree, Path, RevsOnly)
     end.
 
+replicated_new_rev(_Tree, _Path, true = _RevsOnly) ->
+    ignore;
+replicated_new_rev(Tree, Path, false = _RevsOnly) ->
+    {Tree1, _} = couch_key_tree:merge(Tree, Path),
+    {ok, Tree1}.
+
 % Revpos were set before the document was grafted onto the deleted revision and
 % point to rev 1. Update them to the new revpos. This is needed when we
 % re-create a document which was previously deleted.
diff --git a/src/couch/test/eunit/couchdb_update_conflicts_tests.erl 
b/src/couch/test/eunit/couchdb_update_conflicts_tests.erl
index 199a79c14..3146ae13a 100644
--- a/src/couch/test/eunit/couchdb_update_conflicts_tests.erl
+++ b/src/couch/test/eunit/couchdb_update_conflicts_tests.erl
@@ -382,7 +382,10 @@ replicated_mismatched_revslimit_test_() ->
                     ?TDEF_FE(t_att_data),
                     ?TDEF_FE(t_internal_node_stemmed),
                     ?TDEF_FE(t_internal_node_full_path_patches_rev_tree),
-                    ?TDEF_FE(t_multiple_revs_path_in_one_request)
+                    ?TDEF_FE(t_multiple_revs_path_in_one_request),
+                    ?TDEF_FE(t_internal_node_bad_parent),
+                    ?TDEF_FE(t_revs_only_internal_node_bad_parent),
+                    ?TDEF_FE(t_internal_node_other_root)
                 ]
             }
         }
@@ -496,6 +499,40 @@ t_multiple_revs_path_in_one_request(DbName) ->
     % 5-e internal never go through
     ?assertMatch([{{not_found, missing}, {5, ~"e"}}], revs(DbName, [{5, 
~"e"}])).
 
+t_internal_node_bad_parent(DbName) ->
+    update(DbName, [doc(4, [~"d", ~"c", ~"x"], [{~"val", ~"d"}])]),
+    % 3-c is an internal rev with parent 2-x. It's sent with a path that says
+    % its parent is 2-b (oops!), so we cannot fold it into 4-d's path. It
+    % should be a new rev (as opposed leaving a leaf behind with no body).
+    update(DbName, [doc(3, [~"c", ~"b", ~"a"], [{~"val", ~"c"}])]),
+    ?assertEqual([], i_got_no_body_and_nobody_cares(DbName)),
+    ?assertEqual([{3, ~"c", false, 3}, {4, ~"d", false, 3}], leafs(DbName)),
+    ?assertEqual(ok, compact(DbName)).
+
+t_revs_only_internal_node_bad_parent(DbName) ->
+    update(DbName, [doc(4, [~"d", ~"c", ~"x"], [{~"val", ~"d"}])]),
+    % As in t_internal_node_bad_parent but it's a prep_and_validate as 
revs_only case.
+    % Since we don't store anything anyway we expect it to be ignored.
+    Doc = doc(3, [~"c", ~"b", ~"a"], []),
+    update(DbName, [Doc#doc{meta = [{revs_only, true}]}]),
+    ?assertEqual([], i_got_no_body_and_nobody_cares(DbName)),
+    ?assertEqual([{4, ~"d", false, 3}], leafs(DbName)).
+
+t_internal_node_other_root(DbName) ->
+    update(DbName, [doc(3, [~"c", ~"x", ~"a"], [])]),
+    % 2-x is an internal rev under 1-a. Send it again as a leaf under 1-z
+    % (different root but same position). Paths can't be folded. We still
+    % shouldn't get a body-less leaf.
+    update(DbName, [doc(2, [~"x", ~"z"], [])]),
+    ?assertEqual([], i_got_no_body_and_nobody_cares(DbName)),
+    ?assertEqual([{2, ~"x", false, 2}, {3, ~"c", false, 3}], leafs(DbName)),
+    % Make sure we can open the doc and compact it
+    ?assertMatch(
+        {ok, #doc{revs = {3, [~"c" | _]}}},
+        couch_util:with_db(DbName, fun(Db) -> couch_db:open_doc(Db, ?DOC_ID, 
[]) end)
+    ),
+    ?assertEqual(ok, compact(DbName)).
+
 two_roots(DbName) ->
     update(DbName, [doc(1, [~"a"], [{~"val", ~"a"}])]),
     update(DbName, [d_4_stemmed()]),
@@ -562,6 +599,19 @@ update(DbName, Docs) ->
         couch_db:update_docs(Db, Docs, [], ?REPLICATED_CHANGES)
     end).
 
+% Make sure we never have a bodyless leaf
+i_got_no_body_and_nobody_cares(DbName) ->
+    couch_util:with_db(DbName, fun(Db) ->
+        #full_doc_info{rev_tree = Tree} = couch_db:get_full_doc_info(Db, 
?DOC_ID),
+        [{P, R} || {?REV_MISSING, {P, [R | _]}} <- 
couch_key_tree:get_all_leafs(Tree)]
+    end).
+
+compact(DbName) ->
+    couch_util:with_db(DbName, fun(Db) ->
+        {ok, _} = couch_db:start_compact(Db),
+        couch_db:wait_for_compaction(Db)
+    end).
+
 % Get a list of {Pos, RevId, Deleted, RevPathLength} for all leafs
 leafs(DbName) ->
     couch_util:with_db(DbName, fun(Db) ->

Reply via email to