Search in sources :

Example 1 with AnnotatingConflictHandler

use of org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler in project jackrabbit-oak by apache.

the class NodeStoreUtils method mergeWithConcurrentCheck.

public static void mergeWithConcurrentCheck(NodeStore nodeStore, NodeBuilder builder) throws CommitFailedException {
    List<EditorProvider> editorProviders = Lists.newArrayList();
    editorProviders.add(new ConflictValidatorProvider());
    CompositeHook hooks = new CompositeHook(new ConflictHook(new AnnotatingConflictHandler()), new EditorHook(CompositeEditorProvider.compose(editorProviders)));
    nodeStore.merge(builder, hooks, CommitInfo.EMPTY);
}
Also used : CompositeHook(org.apache.jackrabbit.oak.spi.commit.CompositeHook) AnnotatingConflictHandler(org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) ConflictHook(org.apache.jackrabbit.oak.plugins.commit.ConflictHook) ConflictValidatorProvider(org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider) CompositeEditorProvider(org.apache.jackrabbit.oak.spi.commit.CompositeEditorProvider) EditorProvider(org.apache.jackrabbit.oak.spi.commit.EditorProvider)

Example 2 with AnnotatingConflictHandler

use of org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler in project jackrabbit-oak by apache.

the class AsyncIndexUpdate method mergeWithConcurrencyCheck.

private static void mergeWithConcurrencyCheck(final NodeStore store, List<ValidatorProvider> validatorProviders, NodeBuilder builder, final String checkpoint, final Long lease, final String name) throws CommitFailedException {
    CommitHook concurrentUpdateCheck = new CommitHook() {

        @Override
        @Nonnull
        public NodeState processCommit(NodeState before, NodeState after, CommitInfo info) throws CommitFailedException {
            // check for concurrent updates by this async task
            NodeState async = before.getChildNode(ASYNC);
            if ((checkpoint == null || Objects.equal(checkpoint, async.getString(name))) && (lease == null || lease == async.getLong(leasify(name)))) {
                return after;
            } else {
                throw newConcurrentUpdateException();
            }
        }
    };
    List<EditorProvider> editorProviders = Lists.newArrayList();
    editorProviders.add(new ConflictValidatorProvider());
    editorProviders.addAll(validatorProviders);
    CompositeHook hooks = new CompositeHook(ResetCommitAttributeHook.INSTANCE, ConflictHook.of(new AnnotatingConflictHandler()), new EditorHook(CompositeEditorProvider.compose(editorProviders)), concurrentUpdateCheck);
    try {
        store.merge(builder, hooks, createCommitInfo());
    } catch (CommitFailedException ex) {
        // OAK-2961
        if (ex.isOfType(CommitFailedException.STATE) && ex.getCode() == 1) {
            throw newConcurrentUpdateException();
        } else {
            throw ex;
        }
    }
}
Also used : CompositeHook(org.apache.jackrabbit.oak.spi.commit.CompositeHook) AnnotatingConflictHandler(org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) ConflictValidatorProvider(org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider) EditorProvider(org.apache.jackrabbit.oak.spi.commit.EditorProvider) CompositeEditorProvider(org.apache.jackrabbit.oak.spi.commit.CompositeEditorProvider) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 3 with AnnotatingConflictHandler

use of org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler in project jackrabbit-oak by apache.

the class NodeStoreUtils method mergeWithConcurrentCheck.

static void mergeWithConcurrentCheck(NodeStore nodeStore, NodeBuilder builder) throws CommitFailedException {
    CompositeHook hooks = new CompositeHook(ResetCommitAttributeHook.INSTANCE, new ConflictHook(new AnnotatingConflictHandler()), new EditorHook(CompositeEditorProvider.compose(singletonList(new ConflictValidatorProvider()))));
    nodeStore.merge(builder, hooks, createCommitInfo());
}
Also used : CompositeHook(org.apache.jackrabbit.oak.spi.commit.CompositeHook) AnnotatingConflictHandler(org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) ConflictHook(org.apache.jackrabbit.oak.plugins.commit.ConflictHook) ConflictValidatorProvider(org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider)

Example 4 with AnnotatingConflictHandler

use of org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler in project jackrabbit-oak by apache.

the class HierarchyConflictTest method merge.

private static void merge(NodeStore store, NodeBuilder root, final EditorCallback callback) throws CommitFailedException {
    CompositeHook hooks = new CompositeHook(new EditorHook(new EditorProvider() {

        private int numEdits = 0;

        @Override
        public Editor getRootEditor(NodeState before, NodeState after, NodeBuilder builder, CommitInfo info) throws CommitFailedException {
            if (callback != null) {
                if (++numEdits > 1) {
                    // this is a retry, fail the commit
                    throw new CommitFailedException(OAK, 0, "do not retry merge in this test");
                }
                callback.edit(builder);
            }
            return null;
        }
    }), ConflictHook.of(new AnnotatingConflictHandler()), new EditorHook(new ConflictValidatorProvider()));
    store.merge(root, hooks, CommitInfo.EMPTY);
}
Also used : CompositeHook(org.apache.jackrabbit.oak.spi.commit.CompositeHook) AnnotatingConflictHandler(org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) ConflictValidatorProvider(org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider) EditorProvider(org.apache.jackrabbit.oak.spi.commit.EditorProvider) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 5 with AnnotatingConflictHandler

use of org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler in project jackrabbit-oak by apache.

the class DocumentNodeStoreTest method commitRootSameAsModifiedPathWithConflicts.

@Test
public void commitRootSameAsModifiedPathWithConflicts() throws Exception {
    MemoryDocumentStore store = new MemoryDocumentStore(true);
    final DocumentNodeStore ns = builderProvider.newBuilder().setAsyncDelay(0).setDocumentStore(store).getNodeStore();
    NodeBuilder builder = ns.getRoot().builder();
    builder.child("a").child("b").setProperty("p", 0L);
    merge(ns, builder);
    final List<Throwable> exceptions = synchronizedList(Lists.newArrayList());
    Runnable task = new Runnable() {

        CommitHook hook = new CompositeHook(new ConflictHook(new AnnotatingConflictHandler()), new EditorHook(new ConflictValidatorProvider()));

        @Override
        public void run() {
            try {
                for (int i = 0; i < 100; i++) {
                    NodeBuilder builder = ns.getRoot().builder();
                    NodeBuilder b = builder.child("a").child("b");
                    PropertyState p = b.getProperty("p");
                    assertNotNull(p);
                    long value = p.getValue(Type.LONG) + 1;
                    b.setProperty(p.getName(), value);
                    try {
                        ns.merge(builder, hook, CommitInfo.EMPTY);
                    } catch (CommitFailedException e) {
                        if (e.asRepositoryException() instanceof InvalidItemStateException) {
                        // this is fine and may happen from time to
                        // time because the test updates the same
                        // property concurrently
                        } else {
                            // anything else is unexpected
                            exceptions.add(e);
                        }
                    }
                }
            } catch (AssertionError e) {
                exceptions.add(e);
            }
        }
    };
    List<Thread> threads = Lists.newArrayList();
    for (int i = 0; i < 4; i++) {
        threads.add(new Thread(task));
    }
    for (Thread t : threads) {
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    // check updates are consecutive
    NodeDocument doc = store.find(NODES, Utils.getIdFromPath("/a/b"));
    assertNotNull(doc);
    long previousValue = -1;
    List<String> values = Lists.newArrayList(doc.getLocalMap("p").values());
    for (String v : Lists.reverse(values)) {
        long currentValue = Long.parseLong(v);
        assertEquals(previousValue + 1, currentValue);
        previousValue = currentValue;
    }
    for (Throwable e : exceptions) {
        fail(e.toString());
    }
}
Also used : MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) InvalidItemStateException(javax.jcr.InvalidItemStateException) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) ConflictHook(org.apache.jackrabbit.oak.plugins.commit.ConflictHook) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) ConflictValidatorProvider(org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) CompositeHook(org.apache.jackrabbit.oak.spi.commit.CompositeHook) AnnotatingConflictHandler(org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Test(org.junit.Test)

Aggregations

AnnotatingConflictHandler (org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler)7 ConflictValidatorProvider (org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider)7 CompositeHook (org.apache.jackrabbit.oak.spi.commit.CompositeHook)7 EditorHook (org.apache.jackrabbit.oak.spi.commit.EditorHook)7 ConflictHook (org.apache.jackrabbit.oak.plugins.commit.ConflictHook)4 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)3 EditorProvider (org.apache.jackrabbit.oak.spi.commit.EditorProvider)3 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)3 CommitHook (org.apache.jackrabbit.oak.spi.commit.CommitHook)2 CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)2 CompositeEditorProvider (org.apache.jackrabbit.oak.spi.commit.CompositeEditorProvider)2 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)2 Test (org.junit.Test)2 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)1 MemoryDocumentStore (org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore)1 IndexUpdateProvider (org.apache.jackrabbit.oak.plugins.index.IndexUpdateProvider)1