Author: mreutegg
Date: Wed Mar 6 16:05:27 2019
New Revision: 1854930
URL: http://svn.apache.org/viewvc?rev=1854930&view=rev
Log:
OAK-8106: High memory usage when large branch is reset
Modified:
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchInCommitHookTest.java
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java
Modified:
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java?rev=1854930&r1=1854929&r2=1854930&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
(original)
+++
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
Wed Mar 6 16:05:27 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/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1854930&r1=1854929&r2=1854930&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
(original)
+++
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
Wed Mar 6 16:05:27 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;
@@ -1660,45 +1662,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/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java?rev=1854930&r1=1854929&r2=1854930&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
(original)
+++
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
Wed Mar 6 16:05:27 2019
@@ -556,8 +556,16 @@ class DocumentNodeStoreBranch implements
} finally {
if (!success) {
this.head = previousHead;
- // make sure branch state is reset
- branchState = this;
+ 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 {
@@ -566,6 +574,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);
+ }
+ }
+ }
private ModifiedDocumentNodeState
newModifiedDocumentNodeState(NodeState modified) {
return new ModifiedDocumentNodeState(store,
DocumentNodeStoreBranch.this, base, modified);
Modified:
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java?rev=1854930&r1=1854929&r2=1854930&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
(original)
+++
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKResetTest.java
Wed Mar 6 16:05:27 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/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchInCommitHookTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchInCommitHookTest.java?rev=1854930&r1=1854929&r2=1854930&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchInCommitHookTest.java
(original)
+++
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchInCommitHookTest.java
Wed Mar 6 16:05:27 2019
@@ -132,14 +132,13 @@ public class DocumentNodeStoreBranchInCo
hook.assertChanges(ns.getRoot());
- // must have left behind unmerged branch commits
+ // must not leave behind unmerged branch commits (OAK-8106)
root = Utils.getRootDocument(ns.getDocumentStore());
for (String value : root.getLocalRevisions().values()) {
if (!Utils.isCommitted(value)) {
- return;
+ fail("Must not leave unmerged branch commits behind");
}
}
- fail("Must have created unmerged branch commits");
}
private int numBranchCommits(NodeDocument root) {
Modified:
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java?rev=1854930&r1=1854929&r2=1854930&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java
(original)
+++
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranchesTest.java
Wed Mar 6 16:05:27 2019
@@ -32,6 +32,7 @@ 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;
@@ -49,7 +50,6 @@ import org.apache.jackrabbit.oak.stats.C
import org.jetbrains.annotations.Nullable;
import org.junit.AfterClass;
import org.junit.BeforeClass;
-import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
@@ -138,7 +138,6 @@ public class DocumentNodeStoreBranchesTe
}
// OAK-8106
- @Ignore("OAK-8106")
@Test
public void resetBranch() throws Exception {
final long branchCommits = 5;
@@ -195,15 +194,190 @@ public class DocumentNodeStoreBranchesTe
for (NodeDocument doc : Utils.getAllDocuments(store)) {
String path = doc.getPath();
if (path.startsWith("/bar")) {
- assertThat(doc.getLocalRevisions().keySet(), is(empty()));
- assertThat(doc.getLocalCommitRoot().keySet(), is(empty()));
- assertThat(doc.getDeleted().keySet(), is(empty()));
- assertThat(doc.getLocalBranchCommits(), is(empty()));
- assertTrue(doc.wasDeletedOnce());
+ 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
+ DocumentRootBuilder nb = (DocumentRootBuilder)
after.builder();
+ for (int i = 0; i < branchCommits; i++) {
+ NodeBuilder child = nb.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
+ nb.rebase();
+ }
+ // 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
+ DocumentRootBuilder nb = (DocumentRootBuilder)
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
+ nb.rebase();
+ // and add nodes until a branch commit is created
+ NodeBuilder child = nb.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;