Search in sources :

Example 6 with CommitInfo

use of org.apache.jackrabbit.oak.spi.commit.CommitInfo 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, new ConflictHook(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) ConflictHook(org.apache.jackrabbit.oak.plugins.commit.ConflictHook) 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 7 with CommitInfo

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

the class AsyncIndexUpdate method updateIndex.

protected boolean updateIndex(NodeState before, String beforeCheckpoint, NodeState after, String afterCheckpoint, String afterTime, AsyncUpdateCallback callback) throws CommitFailedException {
    Stopwatch watch = Stopwatch.createStarted();
    boolean updatePostRunStatus = true;
    boolean progressLogged = false;
    // prepare the update callback for tracking index updates
    // and maintaining the update lease
    callback.prepare(afterCheckpoint);
    // check for index tasks split requests, if a split happened, make
    // sure to not delete the reference checkpoint, as the other index
    // task will take care of it
    taskSplitter.maybeSplit(beforeCheckpoint, callback.lease);
    IndexUpdate indexUpdate = null;
    try {
        NodeBuilder builder = store.getRoot().builder();
        markFailingIndexesAsCorrupt(builder);
        CommitInfo info = new CommitInfo(CommitInfo.OAK_UNKNOWN, CommitInfo.OAK_UNKNOWN, ImmutableMap.of(IndexConstants.CHECKPOINT_CREATION_TIME, afterTime));
        indexUpdate = new IndexUpdate(provider, name, after, builder, callback, callback, info, corruptIndexHandler).withMissingProviderStrategy(missingStrategy);
        configureRateEstimator(indexUpdate);
        CommitFailedException exception = EditorDiff.process(VisibleEditor.wrap(indexUpdate), before, after);
        if (exception != null) {
            throw exception;
        }
        builder.child(ASYNC).setProperty(name, afterCheckpoint);
        builder.child(ASYNC).setProperty(PropertyStates.createProperty(lastIndexedTo, afterTime, Type.DATE));
        if (callback.isDirty() || before == MISSING_NODE) {
            if (switchOnSync) {
                reindexedDefinitions.addAll(indexUpdate.getReindexedDefinitions());
                updatePostRunStatus = false;
            } else {
                updatePostRunStatus = true;
            }
        } else {
            if (switchOnSync) {
                log.debug("[{}] No changes detected after diff; will try to switch to synchronous updates on {}", name, reindexedDefinitions);
                // no changes after diff, switch to sync on the async defs
                for (String path : reindexedDefinitions) {
                    NodeBuilder c = builder;
                    for (String p : elements(path)) {
                        c = c.getChildNode(p);
                    }
                    if (c.exists() && !c.getBoolean(REINDEX_PROPERTY_NAME)) {
                        c.removeProperty(ASYNC_PROPERTY_NAME);
                    }
                }
                reindexedDefinitions.clear();
                if (store.release(afterCheckpoint)) {
                    builder.child(ASYNC).removeProperty(name);
                    builder.child(ASYNC).removeProperty(lastIndexedTo);
                } else {
                    log.debug("[{}] Unable to release checkpoint {}", name, afterCheckpoint);
                }
            }
            updatePostRunStatus = true;
        }
        mergeWithConcurrencyCheck(store, validatorProviders, builder, beforeCheckpoint, callback.lease, name);
        if (indexUpdate.isReindexingPerformed()) {
            log.info("[{}] Reindexing completed for indexes: {} in {} ({} ms)", name, indexUpdate.getReindexStats(), watch, watch.elapsed(TimeUnit.MILLISECONDS));
            progressLogged = true;
        }
        corruptIndexHandler.markWorkingIndexes(indexUpdate.getUpdatedIndexPaths());
    } finally {
        if (indexUpdate != null) {
            if (updatePostRunStatus) {
                indexUpdate.commitProgress(IndexCommitCallback.IndexProgress.COMMIT_SUCCEDED);
            } else {
                indexUpdate.commitProgress(IndexCommitCallback.IndexProgress.COMMIT_FAILED);
            }
        }
        callback.close();
    }
    if (!progressLogged) {
        String msg = "[{}] AsyncIndex update run completed in {}. Indexed {} nodes, {}";
        //Log at info level if time taken is more than 5 min
        if (watch.elapsed(TimeUnit.MINUTES) >= 5) {
            log.info(msg, name, watch, indexStats.getUpdates(), indexUpdate.getIndexingStats());
        } else {
            log.debug(msg, name, watch, indexStats.getUpdates(), indexUpdate.getIndexingStats());
        }
    }
    return updatePostRunStatus;
}
Also used : Stopwatch(com.google.common.base.Stopwatch) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 8 with CommitInfo

use of org.apache.jackrabbit.oak.spi.commit.CommitInfo 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;
        }
    }), new ConflictHook(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) ConflictHook(org.apache.jackrabbit.oak.plugins.commit.ConflictHook) 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 9 with CommitInfo

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

the class AsyncIndexUpdateTest method startTimePresentInCommitInfo.

@Test
public void startTimePresentInCommitInfo() throws Exception {
    MemoryNodeStore store = new MemoryNodeStore();
    NodeBuilder builder = store.getRoot().builder();
    createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "fooIndex", true, false, ImmutableSet.of("foo"), null).setProperty(ASYNC_PROPERTY_NAME, "async");
    builder.child("testRoot1").setProperty("foo", "abc");
    // merge it back in
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    IndexingContextCapturingProvider provider = new IndexingContextCapturingProvider();
    AsyncIndexUpdate async = new AsyncIndexUpdate("async", store, provider);
    async.run();
    assertNotNull(provider.lastIndexingContext);
    CommitInfo info = provider.lastIndexingContext.getCommitInfo();
    String indexStartTime = (String) info.getInfo().get(IndexConstants.CHECKPOINT_CREATION_TIME);
    assertNotNull(indexStartTime);
}
Also used : MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 10 with CommitInfo

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

the class IndexUpdateTest method testMissingProviderFailsCommit.

/**
     * OAK-3505 Provide an optionally stricter policy for missing synchronous
     * index editor providers
     */
@Test
public void testMissingProviderFailsCommit() throws Exception {
    final IndexUpdateCallback noop = new IndexUpdateCallback() {

        @Override
        public void indexUpdate() {
        }
    };
    final MissingIndexProviderStrategy mips = new MissingIndexProviderStrategy();
    mips.setFailOnMissingIndexProvider(true);
    EditorHook hook = new EditorHook(new EditorProvider() {

        @Override
        public Editor getRootEditor(NodeState before, NodeState after, NodeBuilder builder, CommitInfo info) throws CommitFailedException {
            return new IndexUpdate(emptyProvider(), null, after, builder, noop).withMissingProviderStrategy(mips);
        }
    });
    NodeState before = builder.getNodeState();
    createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "rootIndex", true, false, ImmutableSet.of("foo"), null);
    builder.child(INDEX_DEFINITIONS_NAME).child("azerty");
    builder.child("testRoot").setProperty("foo", "abc");
    NodeState after = builder.getNodeState();
    try {
        hook.processCommit(before, after, CommitInfo.EMPTY);
        fail("commit should fail on missing index provider");
    } catch (CommitFailedException ex) {
    // expected
    }
}
Also used : MissingIndexProviderStrategy(org.apache.jackrabbit.oak.plugins.index.IndexUpdate.MissingIndexProviderStrategy) EmptyNodeState(org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState) 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) Editor(org.apache.jackrabbit.oak.spi.commit.Editor) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) EditorProvider(org.apache.jackrabbit.oak.spi.commit.EditorProvider) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) 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