Search in sources :

Example 1 with IOFunction

use of org.neo4j.function.IOFunction in project neo4j by neo4j.

the class DatabaseIndexAccessorTest method shouldStopSamplingWhenIndexIsDropped.

@Test
public void shouldStopSamplingWhenIndexIsDropped() throws Exception {
    // given
    updateAndCommit(asList(add(nodeId, value), add(nodeId2, value2)));
    // when
    // needs to be acquired before drop() is called
    IndexReader indexReader = accessor.newReader();
    IndexSampler indexSampler = indexReader.createSampler();
    Future<Void> drop = threading.executeAndAwait((IOFunction<Void, Void>) nothing -> {
        accessor.drop();
        return nothing;
    }, null, waitingWhileIn(TaskCoordinator.class, "awaitCompletion"), 3, SECONDS);
    try (IndexReader reader = indexReader) /* do not inline! */
    {
        indexSampler.sampleIndex();
        fail("expected exception");
    } catch (IndexNotFoundKernelException e) {
        assertEquals("Index dropped while sampling.", e.getMessage());
    } finally {
        drop.get();
    }
}
Also used : DirectoryFactory(org.neo4j.kernel.api.impl.index.storage.DirectoryFactory) Arrays(java.util.Arrays) EphemeralFileSystemRule(org.neo4j.test.rule.fs.EphemeralFileSystemRule) Iterators.asSet(org.neo4j.helpers.collection.Iterators.asSet) RunWith(org.junit.runner.RunWith) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) TaskCoordinator(org.neo4j.helpers.TaskCoordinator) Assert.assertThat(org.junit.Assert.assertThat) Future(java.util.concurrent.Future) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) IndexQuery.exact(org.neo4j.kernel.api.schema_new.IndexQuery.exact) Assert.fail(org.junit.Assert.fail) IndexUpdateMode(org.neo4j.kernel.impl.api.index.IndexUpdateMode) ClassRule(org.junit.ClassRule) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) PrimitiveLongCollections(org.neo4j.collection.primitive.PrimitiveLongCollections) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) Collection(java.util.Collection) IndexEntryUpdate(org.neo4j.kernel.api.index.IndexEntryUpdate) NewIndexDescriptorFactory(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory) Test(org.junit.Test) IOException(java.io.IOException) IndexQuery.range(org.neo4j.kernel.api.schema_new.IndexQuery.range) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) IOFunction(org.neo4j.function.IOFunction) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) File(java.io.File) List(java.util.List) Rule(org.junit.Rule) ThreadingRule(org.neo4j.test.rule.concurrent.ThreadingRule) IndexSampler(org.neo4j.storageengine.api.schema.IndexSampler) Iterators.emptySetOf(org.neo4j.helpers.collection.Iterators.emptySetOf) ThreadingRule.waitingWhileIn(org.neo4j.test.rule.concurrent.ThreadingRule.waitingWhileIn) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) TaskCoordinator(org.neo4j.helpers.TaskCoordinator) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) IndexSampler(org.neo4j.storageengine.api.schema.IndexSampler) Test(org.junit.Test)

Example 2 with IOFunction

use of org.neo4j.function.IOFunction in project neo4j by neo4j.

the class DatabaseIndexAccessorTest method shouldStopSamplingWhenIndexIsDropped.

@Test
public void shouldStopSamplingWhenIndexIsDropped() throws Exception {
    // given
    updateAndCommit(asList(add(nodeId, value), add(nodeId2, value2)));
    // when
    var indexReader = accessor.newValueReader();
    BinaryLatch dropLatch = new BinaryLatch();
    BinaryLatch sampleLatch = new BinaryLatch();
    LuceneIndexSampler indexSampler = spy((LuceneIndexSampler) indexReader.createSampler());
    doAnswer(inv -> {
        var obj = inv.callRealMethod();
        // We have now started the sampling, let the index try to drop
        dropLatch.release();
        // Wait for the drop to be blocked
        sampleLatch.await();
        return obj;
    }).when(indexSampler).newTask();
    List<Future<?>> futures = new ArrayList<>();
    try (var reader = indexReader;
        /* do not inline! */
        IndexSampler sampler = indexSampler) /* do not inline! */
    {
        futures.add(threading.execute((IOFunction<Void, Void>) nothing -> {
            try {
                indexSampler.sampleIndex(NULL);
                fail("expected exception");
            } catch (IndexNotFoundKernelException e) {
                assertEquals("Index dropped while sampling.", e.getMessage());
            } finally {
                dropLatch.release();
            }
            return nothing;
        }, null));
        futures.add(threading.executeAndAwait((IOFunction<Void, Void>) nothing -> {
            dropLatch.await();
            accessor.drop();
            return nothing;
        }, null, waitingWhileIn(TaskCoordinator.class, "awaitCompletion"), 10, MINUTES));
    } finally {
        // drop is blocked, okay to finish sampling (will fail since index is dropped)
        sampleLatch.release();
        for (Future<?> future : futures) {
            future.get();
        }
    }
}
Also used : IOFunction(org.neo4j.function.IOFunction) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) IndexSampler(org.neo4j.kernel.api.index.IndexSampler) Test(org.junit.Test)

Aggregations

Future (java.util.concurrent.Future)2 Test (org.junit.Test)2 IOFunction (org.neo4j.function.IOFunction)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 List (java.util.List)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1 After (org.junit.After)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.fail (org.junit.Assert.fail)1 Before (org.junit.Before)1 ClassRule (org.junit.ClassRule)1 Rule (org.junit.Rule)1 RunWith (org.junit.runner.RunWith)1 Parameterized (org.junit.runners.Parameterized)1