Search in sources :

Example 11 with Observer

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

the class RepositoryFixture method configureLuceneProvider.

private void configureLuceneProvider(Oak oak) throws IOException {
    LuceneIndexEditorProvider ep = new LuceneIndexEditorProvider();
    LuceneIndexProvider provider = new LuceneIndexProvider();
    oak.with((QueryIndexProvider) provider).with((Observer) provider).with(ep);
}
Also used : LuceneIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditorProvider) Observer(org.apache.jackrabbit.oak.spi.commit.Observer) LuceneIndexProvider(org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexProvider)

Example 12 with Observer

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

the class Oak method createNewContentRepository.

private ContentRepository createNewContentRepository() {
    final RepoStateCheckHook repoStateCheckHook = new RepoStateCheckHook();
    final List<Registration> regs = Lists.newArrayList();
    regs.add(whiteboard.register(Executor.class, getExecutor(), Collections.emptyMap()));
    IndexEditorProvider indexEditors = CompositeIndexEditorProvider.compose(indexEditorProviders);
    OakInitializer.initialize(store, new CompositeInitializer(initializers), indexEditors);
    QueryIndexProvider indexProvider = CompositeQueryIndexProvider.compose(queryIndexProviders);
    commitHooks.add(repoStateCheckHook);
    List<CommitHook> initHooks = new ArrayList<CommitHook>(commitHooks);
    initHooks.add(new EditorHook(CompositeEditorProvider.compose(editorProviders)));
    if (asyncTasks != null) {
        IndexMBeanRegistration indexRegistration = new IndexMBeanRegistration(whiteboard);
        regs.add(indexRegistration);
        for (Entry<String, Long> t : asyncTasks.entrySet()) {
            AsyncIndexUpdate task = new AsyncIndexUpdate(t.getKey(), store, indexEditors);
            indexRegistration.registerAsyncIndexer(task, t.getValue());
            closer.register(task);
        }
        PropertyIndexAsyncReindex asyncPI = new PropertyIndexAsyncReindex(new AsyncIndexUpdate(IndexConstants.ASYNC_REINDEX_VALUE, store, indexEditors, true), getExecutor());
        regs.add(registerMBean(whiteboard, PropertyIndexAsyncReindexMBean.class, asyncPI, PropertyIndexAsyncReindexMBean.TYPE, "async"));
    }
    regs.add(registerMBean(whiteboard, NodeCounterMBean.class, new NodeCounter(store), NodeCounterMBean.TYPE, "nodeCounter"));
    regs.add(registerMBean(whiteboard, QueryEngineSettingsMBean.class, queryEngineSettings, QueryEngineSettingsMBean.TYPE, "settings"));
    // FIXME: OAK-810 move to proper workspace initialization
    // initialize default workspace
    Iterable<WorkspaceInitializer> workspaceInitializers = Iterables.transform(securityProvider.getConfigurations(), new Function<SecurityConfiguration, WorkspaceInitializer>() {

        @Override
        public WorkspaceInitializer apply(SecurityConfiguration sc) {
            return sc.getWorkspaceInitializer();
        }
    });
    OakInitializer.initialize(workspaceInitializers, store, defaultWorkspaceName, indexEditors);
    // add index hooks later to prevent the OakInitializer to do excessive indexing
    with(new IndexUpdateProvider(indexEditors, failOnMissingIndexProvider));
    withEditorHook();
    // Register observer last to prevent sending events while initialising
    for (Observer observer : observers) {
        regs.add(whiteboard.register(Observer.class, observer, emptyMap()));
    }
    RepositoryManager repositoryManager = new RepositoryManager(whiteboard);
    regs.add(registerMBean(whiteboard, RepositoryManagementMBean.class, repositoryManager, RepositoryManagementMBean.TYPE, repositoryManager.getName()));
    CommitHook composite = CompositeHook.compose(commitHooks);
    regs.add(whiteboard.register(CommitHook.class, composite, Collections.emptyMap()));
    final Tracker<Descriptors> t = whiteboard.track(Descriptors.class);
    return new ContentRepositoryImpl(store, composite, defaultWorkspaceName, queryEngineSettings.unwrap(), indexProvider, securityProvider, new AggregatingDescriptors(t)) {

        @Override
        public void close() throws IOException {
            super.close();
            repoStateCheckHook.close();
            new CompositeRegistration(regs).unregister();
            closer.close();
        }
    };
}
Also used : ContentRepositoryImpl(org.apache.jackrabbit.oak.core.ContentRepositoryImpl) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) NodeCounter(org.apache.jackrabbit.oak.plugins.index.counter.jmx.NodeCounter) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) IndexMBeanRegistration(org.apache.jackrabbit.oak.plugins.index.IndexMBeanRegistration) CompositeRegistration(org.apache.jackrabbit.oak.spi.whiteboard.CompositeRegistration) IndexMBeanRegistration(org.apache.jackrabbit.oak.plugins.index.IndexMBeanRegistration) Registration(org.apache.jackrabbit.oak.spi.whiteboard.Registration) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) Observer(org.apache.jackrabbit.oak.spi.commit.Observer) AggregatingDescriptors(org.apache.jackrabbit.oak.spi.descriptors.AggregatingDescriptors) IndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.IndexEditorProvider) CompositeIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.CompositeIndexEditorProvider) RepositoryManager(org.apache.jackrabbit.oak.management.RepositoryManager) PropertyIndexAsyncReindex(org.apache.jackrabbit.oak.plugins.index.property.jmx.PropertyIndexAsyncReindex) AggregatingDescriptors(org.apache.jackrabbit.oak.spi.descriptors.AggregatingDescriptors) Descriptors(org.apache.jackrabbit.oak.api.Descriptors) PropertyIndexAsyncReindexMBean(org.apache.jackrabbit.oak.plugins.index.property.jmx.PropertyIndexAsyncReindexMBean) IndexUpdateProvider(org.apache.jackrabbit.oak.plugins.index.IndexUpdateProvider) CompositeInitializer(org.apache.jackrabbit.oak.spi.lifecycle.CompositeInitializer) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) NodeCounterMBean(org.apache.jackrabbit.oak.plugins.index.counter.jmx.NodeCounterMBean) AsyncIndexUpdate(org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdate) CompositeQueryIndexProvider(org.apache.jackrabbit.oak.spi.query.CompositeQueryIndexProvider) QueryIndexProvider(org.apache.jackrabbit.oak.spi.query.QueryIndexProvider) QueryEngineSettingsMBean(org.apache.jackrabbit.oak.api.jmx.QueryEngineSettingsMBean) RepositoryManagementMBean(org.apache.jackrabbit.oak.api.jmx.RepositoryManagementMBean) WorkspaceInitializer(org.apache.jackrabbit.oak.spi.lifecycle.WorkspaceInitializer) SecurityConfiguration(org.apache.jackrabbit.oak.spi.security.SecurityConfiguration) CompositeRegistration(org.apache.jackrabbit.oak.spi.whiteboard.CompositeRegistration)

Example 13 with Observer

use of org.apache.jackrabbit.oak.spi.commit.Observer 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 14 with Observer

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

the class AsyncIndexUpdateTest method checkpointLostEventualConsistent.

@Test
public void checkpointLostEventualConsistent() throws Exception {
    MemoryNodeStore store = new MemoryNodeStore();
    final List<NodeState> rootStates = Lists.newArrayList();
    store.addObserver(new Observer() {

        @Override
        public void contentChanged(@Nonnull NodeState root, @Nullable CommitInfo info) {
            rootStates.add(root);
        }
    });
    IndexEditorProvider provider = new PropertyIndexEditorProvider();
    NodeBuilder builder = store.getRoot().builder();
    createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "rootIndex", true, false, ImmutableSet.of("foo"), null).setProperty(ASYNC_PROPERTY_NAME, "async");
    builder.child("testRoot").setProperty("foo", "abc");
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    AsyncIndexUpdate pre = new AsyncIndexUpdate("async", store, provider);
    pre.run();
    //Create another commit so that we have two checkpoints
    builder = store.getRoot().builder();
    builder.child("testRoot2").setProperty("foo", "abc");
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    pre.run();
    pre.close();
    //Look for the nodestate just before the final merge in AsyncIndexUpdate
    //i.e. where older checkpoint was still referred and which has been "released"
    //post last run
    Collections.reverse(rootStates);
    final AtomicReference<NodeState> oldRootState = new AtomicReference<NodeState>();
    for (NodeState ns : rootStates) {
        NodeState async = ns.getChildNode(ASYNC);
        String checkpointName = async.getString("async");
        if (store.retrieve(checkpointName) == null && async.getProperty(AsyncIndexUpdate.leasify("async")) == null) {
            oldRootState.set(ns);
            break;
        }
    }
    assertNotNull(oldRootState.get());
    final AtomicBoolean intiLeaseCalled = new AtomicBoolean(false);
    //Here for the call to read existing NodeState we would return the old
    //"stale" state where we have a stale checkpoint
    store = new MemoryNodeStore(store.getRoot()) {

        @Override
        public NodeState getRoot() {
            //Keep returning stale view untill initlease is not invoked
            if (!intiLeaseCalled.get()) {
                return oldRootState.get();
            }
            return super.getRoot();
        }
    };
    final AsyncIndexUpdate async = new AsyncIndexUpdate("async", store, provider) {

        @Override
        protected AsyncUpdateCallback newAsyncUpdateCallback(NodeStore store, String name, long leaseTimeOut, String beforeCheckpoint, AsyncIndexStats indexStats, AtomicBoolean stopFlag) {
            return new AsyncUpdateCallback(store, name, leaseTimeOut, beforeCheckpoint, indexStats, stopFlag) {

                @Override
                protected void initLease() throws CommitFailedException {
                    intiLeaseCalled.set(true);
                    super.initLease();
                }
            };
        }
    };
    async.run();
    //This run should fail
    assertTrue(async.getIndexStats().isFailing());
    async.close();
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProxyNodeStore(org.apache.jackrabbit.oak.spi.state.ProxyNodeStore) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) AsyncIndexStats(org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdate.AsyncIndexStats) Observer(org.apache.jackrabbit.oak.spi.commit.Observer) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) Test(org.junit.Test)

Example 15 with Observer

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

the class NodeStoreTest method afterCommitHook.

@Test
public void afterCommitHook() throws CommitFailedException, InterruptedException {
    assumeTrue(store instanceof Observable);
    final AtomicReference<NodeState> observedRoot = new AtomicReference<NodeState>(null);
    final CountDownLatch latch = new CountDownLatch(2);
    ((Observable) store).addObserver(new Observer() {

        @Override
        public void contentChanged(@Nonnull NodeState root, @Nonnull CommitInfo info) {
            if (root.getChildNode("test").hasChildNode("newNode")) {
                observedRoot.set(checkNotNull(root));
                latch.countDown();
            }
        }
    });
    NodeState root = store.getRoot();
    NodeBuilder rootBuilder = root.builder();
    NodeBuilder testBuilder = rootBuilder.child("test");
    NodeBuilder newNodeBuilder = testBuilder.child("newNode");
    newNodeBuilder.setProperty("n", 42);
    testBuilder.getChildNode("a").remove();
    store.merge(rootBuilder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    // triggers the observer
    NodeState newRoot = store.getRoot();
    latch.await(2, TimeUnit.SECONDS);
    NodeState after = observedRoot.get();
    assertNotNull(after);
    assertTrue(after.getChildNode("test").getChildNode("newNode").exists());
    assertFalse(after.getChildNode("test").getChildNode("a").exists());
    assertEquals(42, (long) after.getChildNode("test").getChildNode("newNode").getProperty("n").getValue(LONG));
    assertEquals(newRoot, after);
}
Also used : Observer(org.apache.jackrabbit.oak.spi.commit.Observer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(org.apache.jackrabbit.oak.spi.commit.Observable) Test(org.junit.Test) OakBaseTest(org.apache.jackrabbit.oak.OakBaseTest)

Aggregations

Observer (org.apache.jackrabbit.oak.spi.commit.Observer)21 Test (org.junit.Test)8 CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)7 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)6 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)5 SimpleCredentials (javax.jcr.SimpleCredentials)4 Jcr (org.apache.jackrabbit.oak.jcr.Jcr)4 PropertyIndexEditorProvider (org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider)4 QueryIndexProvider (org.apache.jackrabbit.oak.spi.query.QueryIndexProvider)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Oak (org.apache.jackrabbit.oak.Oak)3 LuceneIndexProvider (org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexProvider)3 MemoryNodeStore (org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore)3 Observable (org.apache.jackrabbit.oak.spi.commit.Observable)3 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)3 Before (org.junit.Before)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 InitialContent (org.apache.jackrabbit.oak.InitialContent)2