Search in sources :

Example 6 with AsyncIndexStats

use of org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdate.AsyncIndexStats in project jackrabbit-oak by apache.

the class AsyncIndexUpdateTest method greedyLeaseReindex.

@Test
public void greedyLeaseReindex() 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");
    store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    AsyncIndexUpdate pre = new AsyncIndexUpdate("async", store, provider);
    pre.run();
    pre.close();
    // rm all cps to simulate 'missing cp scenario'
    for (String cp : store.listCheckpoints()) {
        store.release(cp);
    }
    final AtomicBoolean greedyLease = new AtomicBoolean(false);
    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 {
                    greedyLease.set(true);
                    super.initLease();
                }

                @Override
                protected void prepare(String afterCheckpoint) throws CommitFailedException {
                    assertTrue(greedyLease.get());
                    super.prepare(afterCheckpoint);
                }
            };
        }
    };
    async.run();
    async.close();
    assertTrue(greedyLease.get());
}
Also used : 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) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 7 with AsyncIndexStats

use of org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdate.AsyncIndexStats 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)

Aggregations

AsyncIndexStats (org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdate.AsyncIndexStats)7 MemoryNodeStore (org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore)7 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)7 Test (org.junit.Test)7 PropertyIndexEditorProvider (org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)5 ProxyNodeStore (org.apache.jackrabbit.oak.spi.state.ProxyNodeStore)5 Semaphore (java.util.concurrent.Semaphore)3 LogCustomizer (org.apache.jackrabbit.oak.commons.junit.LogCustomizer)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)1 CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)1 Observer (org.apache.jackrabbit.oak.spi.commit.Observer)1 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)1