Search in sources :

Example 26 with CommitInfo

use of org.apache.jackrabbit.oak.spi.commit.CommitInfo in project jackrabbit-oak by apache.

the class CommitQueueTest method concurrentCommits.

@Test
public void concurrentCommits() throws Exception {
    final DocumentNodeStore store = builderProvider.newBuilder().getNodeStore();
    AtomicBoolean running = new AtomicBoolean(true);
    Closeable observer = store.addObserver(new Observer() {

        private RevisionVector before = new RevisionVector(new Revision(0, 0, store.getClusterId()));

        @Override
        public void contentChanged(@Nonnull NodeState root, @Nullable CommitInfo info) {
            DocumentNodeState after = (DocumentNodeState) root;
            RevisionVector r = after.getRootRevision();
            LOG.debug("seen: {}", r);
            if (r.compareTo(before) < 0) {
                exceptions.add(new Exception("Inconsistent revision sequence. Before: " + before + ", after: " + r));
            }
            before = r;
        }
    });
    // perform commits with multiple threads
    List<Thread> writers = new ArrayList<Thread>();
    for (int i = 0; i < NUM_WRITERS; i++) {
        final Random random = new Random(i);
        writers.add(new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < COMMITS_PER_WRITER; i++) {
                        Commit commit = store.newCommit(null, null);
                        try {
                            Thread.sleep(0, random.nextInt(1000));
                        } catch (InterruptedException e) {
                        // ignore
                        }
                        if (random.nextInt(5) == 0) {
                            // cancel 20% of the commits
                            store.canceled(commit);
                        } else {
                            boolean isBranch = random.nextInt(5) == 0;
                            store.done(commit, isBranch, CommitInfo.EMPTY);
                        }
                    }
                } catch (Exception e) {
                    exceptions.add(e);
                }
            }
        }));
    }
    for (Thread t : writers) {
        t.start();
    }
    for (Thread t : writers) {
        t.join();
    }
    running.set(false);
    observer.close();
    store.dispose();
    assertNoExceptions();
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) Observer(org.apache.jackrabbit.oak.spi.commit.Observer) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) Test(org.junit.Test)

Example 27 with CommitInfo

use of org.apache.jackrabbit.oak.spi.commit.CommitInfo in project jackrabbit-oak by apache.

the class DocumentNodeStoreStatsCollectorIT method failedMerge.

@Test
public void failedMerge() throws Exception {
    CommitHook failingHook = new CommitHook() {

        @Override
        public NodeState processCommit(NodeState before, NodeState after, CommitInfo info) throws CommitFailedException {
            throw new CommitFailedException(CommitFailedException.MERGE, 0, "");
        }
    };
    NodeBuilder nb1 = nodeStore.getRoot().builder();
    nb1.child("a");
    try {
        nodeStore.merge(nb1, failingHook, CommitInfo.EMPTY);
        fail();
    } catch (CommitFailedException ignore) {
    }
    verify(statsCollector).failedMerge(anyInt(), anyLong(), eq(false), eq(false));
    //Should be called once more with exclusive lock
    verify(statsCollector).failedMerge(anyInt(), anyLong(), eq(false), eq(true));
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Test(org.junit.Test)

Example 28 with CommitInfo

use of org.apache.jackrabbit.oak.spi.commit.CommitInfo in project jackrabbit-oak by apache.

the class DocumentNodeStoreTest method nonBlockingReset.

// OAK-2620
@Test
public void nonBlockingReset() throws Exception {
    final List<String> failure = Lists.newArrayList();
    final AtomicReference<ReentrantReadWriteLock> mergeLock = new AtomicReference<ReentrantReadWriteLock>();
    MemoryDocumentStore store = new MemoryDocumentStore() {

        @Override
        public <T extends Document> T findAndUpdate(Collection<T> collection, UpdateOp update) {
            for (Map.Entry<Key, Operation> entry : update.getChanges().entrySet()) {
                if (entry.getKey().getName().equals(NodeDocument.COLLISIONS)) {
                    ReentrantReadWriteLock rwLock = mergeLock.get();
                    if (rwLock.getReadHoldCount() > 0 || rwLock.getWriteHoldCount() > 0) {
                        failure.add("Branch reset still holds merge lock");
                        break;
                    }
                }
            }
            return super.findAndUpdate(collection, update);
        }
    };
    DocumentNodeStore ds = builderProvider.newBuilder().setDocumentStore(store).setAsyncDelay(0).getNodeStore();
    // do not retry merges
    ds.setMaxBackOffMillis(0);
    DocumentNodeState root = ds.getRoot();
    final DocumentNodeStoreBranch b = ds.createBranch(root);
    // branch state is now Unmodified
    assertTrue(b.getMergeLock() instanceof ReentrantReadWriteLock);
    mergeLock.set((ReentrantReadWriteLock) b.getMergeLock());
    NodeBuilder builder = root.builder();
    builder.child("foo");
    b.setRoot(builder.getNodeState());
    // branch state is now InMemory
    builder.child("bar");
    b.setRoot(builder.getNodeState());
    try {
        b.merge(new CommitHook() {

            @Nonnull
            @Override
            public NodeState processCommit(NodeState before, NodeState after, CommitInfo info) throws CommitFailedException {
                NodeBuilder foo = after.builder().child("foo");
                for (int i = 0; i <= DocumentMK.UPDATE_LIMIT; i++) {
                    foo.setProperty("prop", i);
                }
                throw new CommitFailedException("Fail", 0, "");
            }
        }, CommitInfo.EMPTY);
    } catch (CommitFailedException e) {
    // expected
    }
    for (String s : failure) {
        fail(s);
    }
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) Nonnull(javax.annotation.Nonnull) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) AtomicReference(java.util.concurrent.atomic.AtomicReference) Operation(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Operation) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key) Test(org.junit.Test)

Example 29 with CommitInfo

use of org.apache.jackrabbit.oak.spi.commit.CommitInfo in project jackrabbit-oak by apache.

the class ExternalChangesTest method changeSetForExternalChanges.

@Test
public void changeSetForExternalChanges() throws Exception {
    NodeBuilder b1 = ns1.getRoot().builder();
    b1.child("a");
    b1.setProperty("foo1", "bar");
    ns1.merge(b1, newCollectingHook(), newCommitInfo());
    NodeBuilder b2 = ns1.getRoot().builder();
    b2.child("b");
    b2.setProperty("foo2", "bar");
    ns1.merge(b2, newCollectingHook(), newCommitInfo());
    ns1.runBackgroundUpdateOperations();
    c2.reset();
    ns2.runBackgroundReadOperations();
    CommitInfo ci = c2.getExternalChange();
    CommitContext cc = (CommitContext) ci.getInfo().get(CommitContext.NAME);
    assertNotNull(cc);
    ChangeSet cs = (ChangeSet) cc.get(ChangeCollectorProvider.COMMIT_CONTEXT_OBSERVATION_CHANGESET);
    assertNotNull(cs);
    assertFalse(cs.anyOverflow());
    assertThat(cs.getPropertyNames(), containsInAnyOrder("foo1", "foo2"));
}
Also used : CommitContext(org.apache.jackrabbit.oak.spi.commit.CommitContext) SimpleCommitContext(org.apache.jackrabbit.oak.core.SimpleCommitContext) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) ChangeSet(org.apache.jackrabbit.oak.plugins.observation.ChangeSet) Test(org.junit.Test)

Example 30 with CommitInfo

use of org.apache.jackrabbit.oak.spi.commit.CommitInfo in project jackrabbit-oak by apache.

the class DocumentBundlingTest method recreatedBundledNode2.

@Test
public void recreatedBundledNode2() throws Exception {
    NodeBuilder builder = store.getRoot().builder();
    NodeBuilder fileNode = newNode("nt:file");
    fileNode.child("jcr:content").setProperty("jcr:data", "foo");
    builder.child("test").setChildNode("book.jpg", fileNode.getNodeState());
    merge(builder);
    builder = store.getRoot().builder();
    builder.child("a");
    //In this case we recreate the node in CommitHook
    store.merge(builder, new EditorHook(new EditorProvider() {

        @Override
        public Editor getRootEditor(NodeState before, NodeState after, NodeBuilder builder, CommitInfo info) throws CommitFailedException {
            return new BookRecreatingEditor(builder);
        }
    }), CommitInfo.EMPTY);
}
Also used : DocumentNodeState(org.apache.jackrabbit.oak.plugins.document.DocumentNodeState) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) AbstractNodeState(org.apache.jackrabbit.oak.spi.state.AbstractNodeState) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) EditorProvider(org.apache.jackrabbit.oak.spi.commit.EditorProvider) Test(org.junit.Test)

Aggregations

CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)39 Test (org.junit.Test)29 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)23 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)16 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)11 CommitHook (org.apache.jackrabbit.oak.spi.commit.CommitHook)9 Nonnull (javax.annotation.Nonnull)7 Observer (org.apache.jackrabbit.oak.spi.commit.Observer)7 SimpleCommitContext (org.apache.jackrabbit.oak.core.SimpleCommitContext)6 CommitContext (org.apache.jackrabbit.oak.spi.commit.CommitContext)6 OakBaseTest (org.apache.jackrabbit.oak.OakBaseTest)4 EditorHook (org.apache.jackrabbit.oak.spi.commit.EditorHook)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 PropertyIndexEditorProvider (org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider)3 MemoryNodeStore (org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore)3 ChangeSet (org.apache.jackrabbit.oak.plugins.observation.ChangeSet)3 EditorProvider (org.apache.jackrabbit.oak.spi.commit.EditorProvider)3 Observable (org.apache.jackrabbit.oak.spi.commit.Observable)3 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)3