You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by mr...@apache.org on 2019/03/13 09:06:02 UTC

svn commit: r1855384 - in /jackrabbit/oak/branches/1.6/oak-core: ./ src/main/java/org/apache/jackrabbit/oak/plugins/document/ src/test/java/org/apache/jackrabbit/oak/plugins/document/

Author: mreutegg
Date: Wed Mar 13 09:06:02 2019
New Revision: 1855384

URL: http://svn.apache.org/viewvc?rev=1855384&view=rev
Log:
OAK-8106: High memory usage when large branch is reset

Merged revisions 1854827 and 1854930 from trunk

Modified:
    jackrabbit/oak/branches/1.6/oak-core/   (props changed)
    jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
    jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
    jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
    jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ResetDiff.java
    jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
    jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java

Propchange: jackrabbit/oak/branches/1.6/oak-core/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Mar 13 09:06:02 2019
@@ -3,5 +3,5 @@
 /jackrabbit/oak/trunk:1834823
 /jackrabbit/oak/trunk/oak-blob-plugins:1819950
 /jackrabbit/oak/trunk/oak-core:1781068,1781075,1781248,1781386,1781846,1781907,1782000,1782029,1782196,1782447,1782476,1782770,1782945,1782966,1782973,1782990,1783061,1783066,1783089,1783104-1783105,1783110,1783619,1783720,1783731,1783733,1783738,1783742,1783773,1783855,1783891,1784023,1784034,1784130,1784162,1784251,1784401,1784551,1784574,1784689,1785095,1785108,1785283,1785652,1785838,1785917,1785919,1785946,1786122,1787074,1787145,1787151,1787217,1787425,1788056,1788378,1788387-1788389,1788463,1788476,1788850,1789056,1789441,1789534,1789925,1789940,1789987,1790006,1790013,1790069,1790077,1790079,1790382,1792049,1792463,1792742,1792746,1793013,1793088,1793618,1793627,1793644,1794393,1794417,1794683,1795138,1795314,1795330,1795475,1795488,1795491,1795502,1795594,1795613,1795618,1796144,1796230,1796239,1796274,1796278,1796988,1797378,1798035,1798832,1798834,1799219,1799389,1799393,1799861,1799924,1800269,1800606,1800613,1800974,1801011,1801013,1801118-1801119,1801675,1802260,180226
 2,1802286,1802548,1802905,1802934,1802938,1802973,1803026,1803247-1803249,1803951,1803953-1803955,1804437,1805851-1805852,1806668,1807308,1807688,1808125,1808128,1808142,1808240,1808246,1809024,1809026,1809131,1809163,1809178-1809179,1809253,1809255-1809256,1811380,1811952,1811963,1811986,1814332,1818645,1821325,1821358,1821516,1830160,1840226,1846057
-/jackrabbit/oak/trunk/oak-store-document:1809866,1811575,1811702,1811709,1811823,1811835,1811931,1812739,1812750,1812753,1814407,1814499,1814579,1815450,1815466,1815940,1816436,1817379,1817772,1818301,1818303,1818903,1818906,1819421,1820199,1820661,1821130,1821477,1821487,1823163,1823169,1824962,1825065,1825442,1825470,1828349,1828868,1830048,1830209,1834610,1834823,1838076,1840455,1842089,1848073,1852451,1852492,1854455,1854701
+/jackrabbit/oak/trunk/oak-store-document:1809866,1811575,1811702,1811709,1811823,1811835,1811931,1812739,1812750,1812753,1814407,1814499,1814579,1815450,1815466,1815940,1816436,1817379,1817772,1818301,1818303,1818903,1818906,1819421,1820199,1820661,1821130,1821477,1821487,1823163,1823169,1824962,1825065,1825442,1825470,1828349,1828868,1830048,1830209,1834610,1834823,1838076,1840455,1842089,1848073,1852451,1852492,1854455,1854701,1854827,1854930
 /jackrabbit/trunk/oak-core:1345480

Modified: jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java?rev=1855384&r1=1855383&r2=1855384&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java Wed Mar 13 09:06:02 2019
@@ -94,7 +94,7 @@ class Branch {
     }
 
     /**
-     * @return the initial base of this branch.
+     * @return the initial base of this branch. This is a trunk revision.
      */
     @Nonnull
     RevisionVector getBase() {
@@ -302,6 +302,9 @@ class Branch {
             this.commit = commit;
         }
 
+        /**
+         * @return the branch base for this branch commit.
+         */
         RevisionVector getBase() {
             return base;
         }

Modified: jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1855384&r1=1855383&r2=1855384&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java Wed Mar 13 09:06:02 2019
@@ -22,7 +22,6 @@ import static com.google.common.base.Pre
 import static com.google.common.collect.Iterables.filter;
 import static com.google.common.collect.Iterables.toArray;
 import static com.google.common.collect.Iterables.transform;
-import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Lists.reverse;
 import static java.util.Collections.singletonList;
 import static org.apache.jackrabbit.oak.commons.PathUtils.concat;
@@ -43,6 +42,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.ref.WeakReference;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashSet;
@@ -58,6 +58,7 @@ import java.util.concurrent.Executor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
@@ -1538,44 +1539,52 @@ public final class DocumentNodeStore
             throw new DocumentStoreException(branchHead + " is not the head " +
                     "of a branch");
         }
-        if (!b.containsCommit(ancestor.getBranchRevision())
+        Revision ancestorRev = ancestor.getBranchRevision();
+        if (!b.containsCommit(ancestorRev)
                 && !b.getBase().asBranchRevision(getClusterId()).equals(ancestor)) {
             throw new DocumentStoreException(ancestor + " is not " +
                     "an ancestor revision of " + branchHead);
         }
-        // tailSet is inclusive -> use an ancestorRev with a
-        // counter incremented by one to make the call exclusive
-        Revision ancestorRev = ancestor.getBranchRevision();
-        ancestorRev = new Revision(ancestorRev.getTimestamp(),
-                ancestorRev.getCounter() + 1, ancestorRev.getClusterId(), true);
-        List<Revision> revs = newArrayList(b.getCommits().tailSet(ancestorRev));
-        if (revs.isEmpty()) {
-            // trivial
-            return branchHead;
+        // tailSet is inclusive and will contain the ancestorRev, unless
+        // the ancestorRev is the base revision of the branch
+        List<Revision> revs = new ArrayList<>();
+        if (!b.containsCommit(ancestorRev)) {
+            // add before all other branch revisions
+            revs.add(ancestorRev);
         }
+        revs.addAll(b.getCommits().tailSet(ancestorRev));
         UpdateOp rootOp = new UpdateOp(Utils.getIdFromPath("/"), false);
         // reset each branch commit in reverse order
         Map<String, UpdateOp> operations = Maps.newHashMap();
+        AtomicReference<Revision> currentRev = new AtomicReference<>();
         for (Revision r : reverse(revs)) {
-            NodeDocument.removeCollision(rootOp, r.asTrunkRevision());
-            NodeDocument.removeRevision(rootOp, r.asTrunkRevision());
             operations.clear();
-            BranchCommit bc = b.getCommit(r);
+            Revision previous = currentRev.getAndSet(r);
+            if (previous == null) {
+                continue;
+            }
+            NodeDocument.removeCollision(rootOp, previous.asTrunkRevision());
+            NodeDocument.removeRevision(rootOp, previous.asTrunkRevision());
+            BranchCommit bc = b.getCommit(previous);
             if (bc.isRebase()) {
                 continue;
             }
-            getRoot(bc.getBase().update(r))
-                    .compareAgainstBaseState(getRoot(bc.getBase()),
-                            new ResetDiff(r.asTrunkRevision(), operations));
+            DocumentNodeState branchState = getRoot(bc.getBase().update(previous));
+            DocumentNodeState baseState = getRoot(bc.getBase().update(r));
+            LOG.debug("reset: comparing branch {} with base {}",
+                    branchState.getRootRevision(), baseState.getRootRevision());
+            branchState.compareAgainstBaseState(baseState,
+                    new ResetDiff(previous.asTrunkRevision(), operations));
+            LOG.debug("reset: applying {} operations", operations.size());
             // apply reset operations
-            for (UpdateOp op : operations.values()) {
-                store.findAndUpdate(Collection.NODES, op);
-            }
+            store.createOrUpdate(NODES, new ArrayList<>(operations.values()));
         }
-        store.findAndUpdate(Collection.NODES, rootOp);
+        store.findAndUpdate(NODES, rootOp);
         // clean up in-memory branch data
         for (Revision r : revs) {
-            b.removeCommit(r);
+            if (!r.equals(ancestorRev)) {
+                b.removeCommit(r);
+            }
         }
         return ancestor;
     }

Modified: jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java?rev=1855384&r1=1855383&r2=1855384&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java Wed Mar 13 09:06:02 2019
@@ -508,19 +508,38 @@ class DocumentNodeStoreBranch implements
             Lock lock = acquireMergeLock(exclusive);
             try {
                 rebase();
-                NodeState toCommit = hook.processCommit(base, head, info);
+                boolean success = false;
+                NodeState previousHead = head;
                 try {
-                    NodeState newHead = DocumentNodeStoreBranch.this.persist(toCommit, base, info);
-                    branchState = new Merged(base);
-                    return newHead;
-                } catch (ConflictException e) {
-                    throw e.asCommitFailedException();
-                } catch(DocumentStoreException e) {
-                    throw new CommitFailedException(MERGE, 1,
-                            "Failed to merge changes to the underlying store", e);
-                } catch (Exception e) {
-                    throw new CommitFailedException(OAK, 1,
-                            "Failed to merge changes to the underlying store", e);
+                    NodeState toCommit = hook.processCommit(base, head, info);
+                    try {
+                        NodeState newHead = DocumentNodeStoreBranch.this.persist(toCommit, base, info);
+                        branchState = new Merged(base);
+                        success = true;
+                        return newHead;
+                    } catch (ConflictException e) {
+                        throw e.asCommitFailedException();
+                    } catch(DocumentStoreException e) {
+                        throw new CommitFailedException(MERGE, 1,
+                                "Failed to merge changes to the underlying store", e);
+                    } catch (Exception e) {
+                        throw new CommitFailedException(OAK, 1,
+                                "Failed to merge changes to the underlying store", e);
+                    }
+                } finally {
+                    if (!success) {
+                        this.head = previousHead;
+                        if (this != branchState) {
+                            // the branch state transitioned to persisted while
+                            // processing the commit hook and then failed.
+                            // remember the persisted branch state
+                            BranchState currentState = branchState;
+                            // reset branch state back to in-memory
+                            branchState = this;
+                            // reset the entire persisted branch state
+                            reset(currentState.persist());
+                        }
+                    }
                 }
             } finally {
                 if (lock != null) {
@@ -528,6 +547,25 @@ class DocumentNodeStoreBranch implements
                 }
             }
         }
+
+        /**
+         * Reset the entire persisted branch.
+         *
+         * @param p the persisted branch.
+         */
+        private void reset(Persisted p) {
+            RevisionVector branchHeadRev = p.getHead().getRootRevision();
+            // get the branch that belongs to this persisted branch state
+            Branch b = store.getBranches().getBranch(branchHeadRev);
+            if (b != null) {
+                try {
+                    store.reset(branchHeadRev,
+                            b.getBase().asBranchRevision(store.getClusterId()));
+                } catch (Exception e) {
+                    LOG.warn("Resetting persisted branch failed", e);
+                }
+            }
+        }
     }
 
     /**
@@ -566,7 +604,7 @@ class DocumentNodeStoreBranch implements
 
         @Override
         @Nonnull
-        NodeState getHead() {
+        DocumentNodeState getHead() {
             return head;
         }
 

Modified: jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ResetDiff.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ResetDiff.java?rev=1855384&r1=1855383&r2=1855384&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ResetDiff.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ResetDiff.java Wed Mar 13 09:06:02 2019
@@ -79,6 +79,7 @@ class ResetDiff implements NodeStateDiff
         ResetDiff diff = new ResetDiff(revision, p, operations);
         UpdateOp op = diff.getUpdateOp();
         NodeDocument.removeDeleted(op, revision);
+        NodeDocument.setDeletedOnce(op);
         return after.compareAgainstBaseState(EMPTY_NODE, diff);
     }
 

Modified: jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java?rev=1855384&r1=1855383&r2=1855384&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java Wed Mar 13 09:06:02 2019
@@ -43,6 +43,37 @@ public class DocumentMKResetTest extends
     }
 
     @Test
+    public void resetEmptyBranch() {
+        String rev = mk.branch(null);
+        try {
+            mk.reset(rev, rev);
+            fail("DocumentStoreException expected");
+        } catch (DocumentStoreException expected) {}
+    }
+
+    @Test
+    public void resetNonBranchHead() {
+        String base = mk.branch(null);
+        String rev = addNodes(base, "/foo");
+        addNodes(rev, "/bar");
+        try {
+            mk.reset(rev, base);
+            fail("DocumentStoreException expected");
+        } catch (DocumentStoreException expected) {}
+    }
+
+    @Test
+    public void resetWithForeignAncestor() {
+        String rev = mk.branch(null);
+        rev = addNodes(rev, "/foo");
+        addNodes(null, "/bar");
+        try {
+            mk.reset(rev, mk.branch(null));
+            fail("DocumentStoreException expected");
+        } catch (DocumentStoreException expected) {}
+    }
+
+    @Test
     public void resetTrunk() {
         String rev = addNodes(null, "/foo");
         try {

Modified: jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java?rev=1855384&r1=1855383&r2=1855384&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java Wed Mar 13 09:06:02 2019
@@ -16,17 +16,27 @@
  */
 package org.apache.jackrabbit.oak.plugins.document;
 
+import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.Arrays;
 
 import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
 
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.plugins.document.DocumentMK.Builder;
+import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore;
+import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.apache.jackrabbit.oak.spi.commit.CommitHook;
 import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
 import org.apache.jackrabbit.oak.spi.commit.CompositeHook;
@@ -133,6 +143,243 @@ public class DocumentNodeStoreBranchesTe
         }
     }
 
+    // OAK-8106
+    @Test
+    public void resetBranch() throws Exception {
+        final long branchCommits = 5;
+        final int updateLimit = DocumentRootBuilder.UPDATE_LIMIT;
+        final CountingDocumentStore store = new CountingDocumentStore(new MemoryDocumentStore());
+        DocumentNodeStore ns = builderProvider.newBuilder()
+                .setAsyncDelay(0)
+                .setDocumentStore(store).getNodeStore();
+        ns.setMaxBackOffMillis(0); // do not retry merges
+        NodeBuilder nb  = ns.getRoot().builder();
+        for (int i = 0; i < branchCommits; i++) {
+            NodeBuilder child = nb.child("foo").child("node-" + i);
+            for (int j = 0; j < updateLimit; j++) {
+                child.child("node-" + j).setProperty("p", "v");
+            }
+        }
+
+        // add some other node to force a rebase
+        NodeBuilder nb2 = ns.getRoot().builder();
+        nb2.setProperty("foo", "v");
+        TestUtils.merge(ns, nb2);
+
+        try {
+            ns.merge(nb, new CommitHook() {
+                @Nonnull
+                @Override
+                public NodeState processCommit(NodeState before,
+                                               NodeState after,
+                                               CommitInfo info)
+                        throws CommitFailedException {
+                    // add more nodes and then fail the commit to trigger a reset
+                    NodeBuilder nb = after.builder().child("bar");
+                    for (int i = 0; i < branchCommits; i++) {
+                        NodeBuilder child = nb.child("node-" + i);
+                        for (int j = 0; j < updateLimit; j++) {
+                            child.child("node-" + j);
+                        }
+                    }
+                    // reset counter before throwing the exception to measure
+                    // the number of calls for the reset
+                    store.resetCounters();
+                    throw new CommitFailedException(CommitFailedException.OAK, 1, "failure");
+                }
+            }, CommitInfo.EMPTY);
+            fail("Merge must fail with CommitFailedException");
+        } catch (CommitFailedException e) {
+            // expected
+        }
+
+        long numCreateOrUpdate = store.getNumCreateOrUpdateCalls(NODES);
+        assertThat(numCreateOrUpdate, lessThanOrEqualTo(branchCommits + 1));
+
+        // verify reset cleaned up properly
+        for (NodeDocument doc : Utils.getAllDocuments(store)) {
+            String path = doc.getPath();
+            if (path.startsWith("/bar")) {
+                assertThat(path, doc.getLocalRevisions().keySet(), is(empty()));
+                assertThat(path, doc.getLocalCommitRoot().keySet(), is(empty()));
+                assertThat(path, doc.getDeleted().keySet(), is(empty()));
+                assertTrue(path, doc.wasDeletedOnce());
+            }
+        }
+    }
+
+    // OAK-8106
+    @Test
+    public void resetBranchCreatedByCommitHook() {
+        final long branchCommits = 5;
+        final int updateLimit = DocumentRootBuilder.UPDATE_LIMIT;
+        DocumentNodeStore ns = builderProvider.newBuilder()
+                .setAsyncDelay(0)
+                .getNodeStore();
+        ns.setMaxBackOffMillis(0); // do not retry merges
+        NodeBuilder nb  = ns.getRoot().builder();
+        nb.child("foo");
+        try {
+            ns.merge(nb, new CommitHook() {
+                @Nonnull
+                @Override
+                public NodeState processCommit(NodeState before,
+                                               NodeState after,
+                                               CommitInfo info)
+                        throws CommitFailedException {
+                    // add more nodes and then fail the commit to trigger a reset
+                    NodeBuilder nb = after.builder().child("bar");
+                    for (int i = 0; i < branchCommits; i++) {
+                        NodeBuilder child = nb.child("node-" + i);
+                        for (int j = 0; j < updateLimit; j++) {
+                            child.child("node-" + j);
+                        }
+                    }
+                    throw new CommitFailedException(CommitFailedException.OAK, 1, "failure");
+                }
+            }, CommitInfo.EMPTY);
+            fail("Merge must fail with CommitFailedException");
+        } catch (CommitFailedException e) {
+            // expected
+        }
+
+        // verify reset cleaned up properly
+        for (NodeDocument doc : Utils.getAllDocuments(ns.getDocumentStore())) {
+            String path = doc.getPath();
+            if (path.startsWith("/bar")) {
+                assertThat(path, doc.getLocalRevisions().keySet(), is(empty()));
+                assertThat(path, doc.getLocalCommitRoot().keySet(), is(empty()));
+                assertThat(path, doc.getDeleted().keySet(), is(empty()));
+                assertTrue(path, doc.wasDeletedOnce());
+            }
+        }
+    }
+
+    // OAK-8106
+    @Test
+    public void resetBranchWithFinalRebaseBranchCommit()
+            throws CommitFailedException {
+        final long branchCommits = 5;
+        final int updateLimit = DocumentRootBuilder.UPDATE_LIMIT;
+        final DocumentNodeStore ns = builderProvider.newBuilder()
+                .setAsyncDelay(0)
+                .getNodeStore();
+        ns.setMaxBackOffMillis(0); // do not retry merges
+        final NodeBuilder nb  = ns.getRoot().builder();
+        nb.child("foo");
+        try {
+            ns.merge(nb, new CommitHook() {
+                @Nonnull
+                @Override
+                public NodeState processCommit(NodeState before,
+                                               NodeState after,
+                                               CommitInfo info)
+                        throws CommitFailedException {
+                    // add more nodes to create branch commits
+                    NodeBuilder builder = after.builder();
+                    for (int i = 0; i < branchCommits; i++) {
+                        NodeBuilder child = builder.child("bar").child("node-" + i);
+                        for (int j = 0; j < updateLimit; j++) {
+                            child.child("node-" + j);
+                        }
+                        // add a node with a different merge
+                        addNodes(ns, "/baz/node-" + i);
+                        // now force a rebase
+                        ns.rebase(nb);
+                    }
+                    // eventually fail the commit to trigger a reset
+                    throw new CommitFailedException(CommitFailedException.OAK, Integer.MAX_VALUE, "failure");
+                }
+            }, CommitInfo.EMPTY);
+            fail("Merge must fail with CommitFailedException");
+        } catch (CommitFailedException e) {
+            if (e.getCode() != Integer.MAX_VALUE) {
+                throw e;
+            }
+            // otherwise expected
+        }
+
+        // verify reset cleaned up properly
+        for (NodeDocument doc : Utils.getAllDocuments(ns.getDocumentStore())) {
+            String path = doc.getPath();
+            if (path.startsWith("/bar")) {
+                assertThat(path, doc.getLocalRevisions().keySet(), is(empty()));
+                assertThat(path, doc.getLocalCommitRoot().keySet(), is(empty()));
+                assertThat(path, doc.getDeleted().keySet(), is(empty()));
+                assertTrue(path, doc.wasDeletedOnce());
+            }
+        }
+    }
+
+    // OAK-8106
+    @Test
+    public void resetBranchWithFirstRebaseBranchCommit()
+            throws CommitFailedException {
+        final long branchCommits = 5;
+        final int updateLimit = DocumentRootBuilder.UPDATE_LIMIT;
+        final DocumentNodeStore ns = builderProvider.newBuilder()
+                .setAsyncDelay(0)
+                .getNodeStore();
+        ns.setMaxBackOffMillis(0); // do not retry merges
+        final NodeBuilder nb  = ns.getRoot().builder();
+        nb.child("foo");
+        try {
+            ns.merge(nb, new CommitHook() {
+                @Nonnull
+                @Override
+                public NodeState processCommit(NodeState before,
+                                               NodeState after,
+                                               CommitInfo info)
+                        throws CommitFailedException {
+                    // add more nodes to create branch commits
+                    NodeBuilder builder = after.builder();
+                    for (int i = 0; i < branchCommits; i++) {
+                        // add a node with a different merge
+                        addNodes(ns, "/baz/node-" + i);
+                        // now force a rebase
+                        ns.rebase(nb);
+                        // and add nodes until a branch commit is created
+                        NodeBuilder child = builder.child("bar").child("node-" + i);
+                        for (int j = 0; j < updateLimit; j++) {
+                            child.child("node-" + j);
+                        }
+                    }
+                    // eventually fail the commit to trigger a reset
+                    throw new CommitFailedException(CommitFailedException.OAK, Integer.MAX_VALUE, "failure");
+                }
+            }, CommitInfo.EMPTY);
+            fail("Merge must fail with CommitFailedException");
+        } catch (CommitFailedException e) {
+            if (e.getCode() != Integer.MAX_VALUE) {
+                throw e;
+            }
+            // otherwise expected
+        }
+
+        // verify reset cleaned up properly
+        for (NodeDocument doc : Utils.getAllDocuments(ns.getDocumentStore())) {
+            String path = doc.getPath();
+            if (path.startsWith("/bar")) {
+                assertThat(path, doc.getLocalRevisions().keySet(), is(empty()));
+                assertThat(path, doc.getLocalCommitRoot().keySet(), is(empty()));
+                assertThat(path, doc.getDeleted().keySet(), is(empty()));
+                assertTrue(path, doc.wasDeletedOnce());
+            }
+        }
+    }
+
+    private void addNodes(DocumentNodeStore ns, String... paths)
+            throws CommitFailedException {
+        NodeBuilder nb = ns.getRoot().builder();
+        for (String p : paths) {
+            NodeBuilder b = nb;
+            for (String name : PathUtils.elements(p)) {
+                b = b.child(name);
+            }
+        }
+        TestUtils.merge(ns, nb);
+    }
+
     private static class TestEditor extends DefaultEditor {
 
         private final NodeBuilder builder;