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/12 15:57:38 UTC

svn commit: r1855338 - in /jackrabbit/oak/branches/1.8: ./ oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/ oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/

Author: mreutegg
Date: Tue Mar 12 15:57:38 2019
New Revision: 1855338

URL: http://svn.apache.org/viewvc?rev=1855338&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.8/   (props changed)
    jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
    jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
    jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
    jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
    jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java

Propchange: jackrabbit/oak/branches/1.8/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Mar 12 15:57:38 2019
@@ -1,3 +1,3 @@
 /jackrabbit/oak/branches/1.0:1665962
-/jackrabbit/oak/trunk:1820660-1820661,1820729,1820734,1820859,1820861,1820878,1820888,1820947,1821027,1821130,1821140-1821141,1821178,1821237,1821240,1821249,1821258,1821325,1821358,1821361-1821362,1821370,1821375,1821393,1821477,1821487,1821516,1821617,1821663,1821665,1821668,1821681,1821847,1821975-1821983,1822121,1822182,1822201,1822207,1822527,1822642,1822723,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826833,1826932,1826957,1827423,1827472,1827486,1827816,1827977,1828349,1828439,1828502,1828529,1828827,1828868,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019,1830048,1830160,1830171,1830197,1830209,1830239
 ,1830347,1830748,1830911,1830923,1831157-1831158,1831163,1831190,1831374,1831560,1831689,1832258,1832376,1832379,1832535,1833308,1833347,1833833,1834112,1834117,1834287,1834291,1834302,1834326,1834328,1834336,1834428,1834468,1834483,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835060,1835518,1835521,1835635,1835642,1835780,1835819,1836082,1836121,1836167-1836168,1836170-1836187,1836189-1836196,1836206,1836487,1836493,1836548,1837057,1837274,1837296,1837326,1837475,1837503,1837547,1837569,1837600,1837657,1837718,1837998,1838076,1838637,1839549,1839570,1839637,1839746,1840019,1840024,1840031,1840226,1840455,1840462,1840574,1840769,1841314,1841352,1842089,1842677,1843175,1843222,1843231,1843398,1843618,1843621,1843652,1843911,1844070,1844325,1844549,1844625,1844627,1844642,1844728,1844775,1844932,1845135,1845336,1845405,1845415,1845730-1845731,1845863,1845865,1846057,1846396,1846429,1846581,1846617,1847096,1848073,1848181-1848182,1848191,1848217,1848822-1848823,1850221,1850
 837,1850874,1851533-1851535,1851619,1852120,1852451,1852492,1852528,1852582,1852584,1853141,1853229,1853393,1853429,1853433,1853866,1853870,1853893,1853969,1853997,1854044,1854455,1854461,1854466,1854515,1854701,1854848,1854859
+/jackrabbit/oak/trunk:1820660-1820661,1820729,1820734,1820859,1820861,1820878,1820888,1820947,1821027,1821130,1821140-1821141,1821178,1821237,1821240,1821249,1821258,1821325,1821358,1821361-1821362,1821370,1821375,1821393,1821477,1821487,1821516,1821617,1821663,1821665,1821668,1821681,1821847,1821975-1821983,1822121,1822182,1822201,1822207,1822527,1822642,1822723,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826833,1826932,1826957,1827423,1827472,1827486,1827816,1827977,1828349,1828439,1828502,1828529,1828827,1828868,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019,1830048,1830160,1830171,1830197,1830209,1830239
 ,1830347,1830748,1830911,1830923,1831157-1831158,1831163,1831190,1831374,1831560,1831689,1832258,1832376,1832379,1832535,1833308,1833347,1833833,1834112,1834117,1834287,1834291,1834302,1834326,1834328,1834336,1834428,1834468,1834483,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835060,1835518,1835521,1835635,1835642,1835780,1835819,1836082,1836121,1836167-1836168,1836170-1836187,1836189-1836196,1836206,1836487,1836493,1836548,1837057,1837274,1837296,1837326,1837475,1837503,1837547,1837569,1837600,1837657,1837718,1837998,1838076,1838637,1839549,1839570,1839637,1839746,1840019,1840024,1840031,1840226,1840455,1840462,1840574,1840769,1841314,1841352,1842089,1842677,1843175,1843222,1843231,1843398,1843618,1843621,1843652,1843911,1844070,1844325,1844549,1844625,1844627,1844642,1844728,1844775,1844932,1845135,1845336,1845405,1845415,1845730-1845731,1845863,1845865,1846057,1846396,1846429,1846581,1846617,1847096,1848073,1848181-1848182,1848191,1848217,1848822-1848823,1850221,1850
 837,1850874,1851533-1851535,1851619,1852120,1852451,1852492,1852528,1852582,1852584,1853141,1853229,1853393,1853429,1853433,1853866,1853870,1853893,1853969,1853997,1854044,1854455,1854461,1854466,1854515,1854701,1854827,1854848,1854859,1854930
 /jackrabbit/trunk:1345480

Modified: jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java?rev=1855338&r1=1855337&r2=1855338&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java (original)
+++ jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java Tue Mar 12 15:57:38 2019
@@ -93,7 +93,7 @@ class Branch {
     }
 
     /**
-     * @return the initial base of this branch.
+     * @return the initial base of this branch. This is a trunk revision.
      */
     @NotNull
     RevisionVector getBase() {
@@ -301,6 +301,9 @@ class Branch {
             this.commit = commit;
         }
 
+        /**
+         * @return the branch base for this branch commit.
+         */
         RevisionVector getBase() {
             return base;
         }

Modified: jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1855338&r1=1855337&r2=1855338&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java (original)
+++ jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java Tue Mar 12 15:57:38 2019
@@ -46,6 +46,7 @@ import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.ref.WeakReference;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -60,6 +61,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;
 
@@ -1651,45 +1653,53 @@ 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());
-            NodeDocument.removeBranchCommit(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());
+            NodeDocument.removeBranchCommit(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.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java?rev=1855338&r1=1855337&r2=1855338&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java (original)
+++ jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java Tue Mar 12 15:57:38 2019
@@ -511,19 +511,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) {
@@ -531,6 +550,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);
+                }
+            }
+        }
     }
 
     /**
@@ -574,7 +612,7 @@ class DocumentNodeStoreBranch implements
 
         @Override
         @NotNull
-        NodeState getHead() {
+        DocumentNodeState getHead() {
             return head;
         }
 

Modified: jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java?rev=1855338&r1=1855337&r2=1855338&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java (original)
+++ jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java Tue Mar 12 15:57:38 2019
@@ -44,6 +44,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.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java?rev=1855338&r1=1855337&r2=1855338&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java (original)
+++ jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java Tue Mar 12 15:57:38 2019
@@ -16,15 +16,26 @@
  */
 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.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;
@@ -37,7 +48,6 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.stats.Clock;
 import org.jetbrains.annotations.Nullable;
-import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Rule;
@@ -127,6 +137,247 @@ public class DocumentNodeStoreBranchesTe
         }
     }
 
+    // OAK-8106
+    @Test
+    public void resetBranch() throws Exception {
+        final long branchCommits = 5;
+        final int updateLimit = 100;
+        final CountingDocumentStore store = new CountingDocumentStore(new MemoryDocumentStore());
+        DocumentNodeStore ns = builderProvider.newBuilder()
+                .setUpdateLimit(updateLimit).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()));
+                assertThat(path, doc.getLocalBranchCommits(), is(empty()));
+                assertTrue(path, doc.wasDeletedOnce());
+            }
+        }
+    }
+
+    // OAK-8106
+    @Test
+    public void resetBranchCreatedByCommitHook() {
+        final long branchCommits = 5;
+        final int updateLimit = 100;
+        DocumentNodeStore ns = builderProvider.newBuilder()
+                .setUpdateLimit(updateLimit).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()));
+                assertThat(path, doc.getLocalBranchCommits(), is(empty()));
+                assertTrue(path, doc.wasDeletedOnce());
+            }
+        }
+    }
+
+    // OAK-8106
+    @Test
+    public void resetBranchWithFinalRebaseBranchCommit()
+            throws CommitFailedException {
+        final long branchCommits = 5;
+        final int updateLimit = 100;
+        final DocumentNodeStore ns = builderProvider.newBuilder()
+                .setUpdateLimit(updateLimit).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 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()));
+                assertThat(path, doc.getLocalBranchCommits(), is(empty()));
+                assertTrue(path, doc.wasDeletedOnce());
+            }
+        }
+    }
+
+    // OAK-8106
+    @Test
+    public void resetBranchWithFirstRebaseBranchCommit()
+            throws CommitFailedException {
+        final long branchCommits = 5;
+        final int updateLimit = 100;
+        final DocumentNodeStore ns = builderProvider.newBuilder()
+                .setUpdateLimit(updateLimit).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 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()));
+                assertThat(path, doc.getLocalBranchCommits(), 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;