Search in sources :

Example 76 with MemoryNodeStore

use of org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore in project jackrabbit-oak by apache.

the class AsyncIndexUpdateTest method failOnConflict.

// OAK-1784
@Test
public void failOnConflict() throws Exception {
    final Map<Thread, Semaphore> locks = Maps.newIdentityHashMap();
    NodeStore store = new MemoryNodeStore() {

        @Nonnull
        @Override
        public NodeState merge(@Nonnull NodeBuilder builder, @Nonnull CommitHook commitHook, @Nonnull CommitInfo info) throws CommitFailedException {
            Semaphore s = locks.get(Thread.currentThread());
            if (s != null) {
                s.acquireUninterruptibly();
            }
            return super.merge(builder, commitHook, info);
        }
    };
    IndexEditorProvider provider = new PropertyIndexEditorProvider();
    NodeBuilder builder = store.getRoot().builder();
    createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "foo", false, ImmutableSet.of("foo"), null, TYPE, Collections.singletonMap(ASYNC_PROPERTY_NAME, "async"));
    builder.child("test").setProperty("foo", "a");
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    final AsyncIndexUpdate async = new AsyncIndexUpdate("async", store, provider);
    async.run();
    builder = store.getRoot().builder();
    builder.child("test").setProperty("foo", "b");
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            async.run();
        }
    });
    Semaphore s = new Semaphore(0);
    locks.put(t, s);
    t.start();
    // make some unrelated changes to trigger indexing
    builder = store.getRoot().builder();
    builder.setChildNode("dummy").setProperty("foo", "bar");
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    while (!s.hasQueuedThreads()) {
        Thread.yield();
    }
    // introduce a conflict
    builder = store.getRoot().builder();
    builder.getChildNode(INDEX_DEFINITIONS_NAME).getChildNode("foo").getChildNode(":index").child("a").remove();
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    s.release(100);
    t.join();
    builder = store.getRoot().builder();
    assertNoConflictMarker(builder);
}
Also used : Nonnull(javax.annotation.Nonnull) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) Semaphore(java.util.concurrent.Semaphore) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) 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) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) Test(org.junit.Test)

Example 77 with MemoryNodeStore

use of org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore in project jackrabbit-oak by apache.

the class AsyncIndexUpdateTest method closeWithHardLimit.

@Test
public void closeWithHardLimit() throws Exception {
    NodeStore store = new MemoryNodeStore();
    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);
    final Semaphore asyncLock = new Semaphore(1);
    final AsyncIndexUpdate async = new AsyncIndexUpdate("async", store, provider) {

        @Override
        protected AsyncUpdateCallback newAsyncUpdateCallback(NodeStore store, String name, long leaseTimeOut, String beforeCheckpoint, AsyncIndexStats indexStats, AtomicBoolean stopFlag) {
            try {
                asyncLock.acquire();
            } catch (InterruptedException ignore) {
            }
            return super.newAsyncUpdateCallback(store, name, leaseTimeOut, beforeCheckpoint, indexStats, stopFlag);
        }
    };
    //Set a 1 sec close timeout
    async.setCloseTimeOut(1);
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            async.run();
        }
    });
    Thread closer = new Thread(new Runnable() {

        @Override
        public void run() {
            async.close();
        }
    });
    //Lock to ensure that AsyncIndexUpdate waits
    asyncLock.acquire();
    t.start();
    //Wait till async gets to wait state i.e. inside run
    while (!asyncLock.hasQueuedThreads()) ;
    LogCustomizer lc = createLogCustomizer(Level.DEBUG);
    closer.start();
    //Wait till stopFlag is set
    while (!async.isClosed()) ;
    assertLogPhrase(lc.getLogs(), "[SOFT LIMIT HIT]");
    //Let indexing run complete now
    asyncLock.release();
    //Wait for both threads
    t.join();
    //Async run would have exited with log message logged
    assertLogPhrase(lc.getLogs(), "The index update interrupted");
    //Wait for close call to complete
    closer.join();
    lc.finished();
}
Also used : PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) Semaphore(java.util.concurrent.Semaphore) 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) LogCustomizer(org.apache.jackrabbit.oak.commons.junit.LogCustomizer) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) Test(org.junit.Test)

Example 78 with MemoryNodeStore

use of org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore in project jackrabbit-oak by apache.

the class AsyncIndexUpdateTest method validatorProviderInvocation.

@Test
public void validatorProviderInvocation() throws Exception {
    MemoryNodeStore store = new MemoryNodeStore();
    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");
    // merge it back in
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    AsyncIndexUpdate async = new AsyncIndexUpdate("async", store, provider);
    CollectingValidatorProvider v = new CollectingValidatorProvider();
    async.setValidatorProviders(ImmutableList.<ValidatorProvider>of(v));
    async.run();
    assertFalse(v.visitedPaths.isEmpty());
    assertThat(v.visitedPaths, hasItem("/:async"));
    assertThat(v.visitedPaths, hasItem("/oak:index/rootIndex"));
}
Also used : MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 79 with MemoryNodeStore

use of org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore in project jackrabbit-oak by apache.

the class SecondaryStoreObserverTest method setUp.

@Before
public void setUp() throws IOException {
    primary = builderProvider.newBuilder().getNodeStore();
    secondary = new MemoryNodeStore();
}
Also used : MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) Before(org.junit.Before)

Example 80 with MemoryNodeStore

use of org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore in project jackrabbit-oak by apache.

the class AsyncPropertyIndexTest method testAsyncPropertyLookup.

@Test
public void testAsyncPropertyLookup() throws Exception {
    NodeStore store = new MemoryNodeStore();
    assertTrue(Iterables.isEmpty(store.checkpoints()));
    NodeBuilder builder = store.getRoot().builder();
    //add a property index on 'foo'
    NodeBuilder def = createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "foo", true, false, of("foo"), null);
    def.setProperty(REINDEX_ASYNC_PROPERTY_NAME, true);
    // add some content
    builder.child("a").setProperty("foo", "abc");
    builder.child("b").setProperty("foo", Arrays.asList("abc", "def"), STRINGS);
    NodeState head = store.merge(builder, hook, EMPTY);
    // query the index, check it doesn't get indexed by the normal PI
    FilterImpl f = createFilter(head, NT_BASE);
    PropertyIndexLookup lookup = new PropertyIndexLookup(head);
    try {
        assertEquals(of(), find(lookup, "foo", "abc", f));
        fail();
    } catch (IllegalArgumentException e) {
    // expected: no index for "foo"
    }
    // run async first time, there are some changes
    AsyncIndexUpdate async = new AsyncIndexUpdate(ASYNC_REINDEX_VALUE, store, provider, true);
    async.run();
    assertEquals(ASYNC_REINDEX_VALUE, store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode("foo").getString(ASYNC_PROPERTY_NAME));
    // run async second time, there are no changes, should switch to sync
    async.run();
    async.close();
    assertEquals(null, store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode("foo").getString(ASYNC_PROPERTY_NAME));
    assertTrue(Iterables.isEmpty(store.checkpoints()));
    // add content, it should be indexed synchronously
    builder = store.getRoot().builder();
    builder.child("c").setProperty("foo", "def");
    head = store.merge(builder, hook, EMPTY);
    f = createFilter(head, NT_BASE);
    lookup = new PropertyIndexLookup(head);
    assertEquals(ImmutableSet.of("b", "c"), find(lookup, "foo", "def", f));
}
Also used : NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) AsyncIndexUpdate(org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdate) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Aggregations

MemoryNodeStore (org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore)92 Test (org.junit.Test)66 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)58 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)37 PropertyIndexEditorProvider (org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider)35 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)33 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)19 ProxyNodeStore (org.apache.jackrabbit.oak.spi.state.ProxyNodeStore)14 MountInfoProvider (org.apache.jackrabbit.oak.spi.mount.MountInfoProvider)13 Oak (org.apache.jackrabbit.oak.Oak)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AsyncIndexStats (org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdate.AsyncIndexStats)7 EditorHook (org.apache.jackrabbit.oak.spi.commit.EditorHook)7 OpenSecurityProvider (org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider)7 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)6 InitialContent (org.apache.jackrabbit.oak.InitialContent)6 PropertyIndexLookup (org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexLookup)6 Validator (org.apache.jackrabbit.oak.spi.commit.Validator)6 ApplyDiff (org.apache.jackrabbit.oak.spi.state.ApplyDiff)6 Semaphore (java.util.concurrent.Semaphore)5