Search in sources :

Example 11 with Resource

use of org.neo4j.graphdb.Resource in project neo4j by neo4j.

the class StoreCopyCheckPointMutexTest method shouldPropagateStoreCopyActionFailureToOtherStoreCopyRequests.

@Test
public void shouldPropagateStoreCopyActionFailureToOtherStoreCopyRequests() throws Exception {
    // GIVEN
    Barrier.Control barrier = new Barrier.Control();
    IOException controlledFailure = new IOException("My own fault");
    AtomicReference<Future<Object>> secondRequest = new AtomicReference<>();
    ThrowingAction<IOException> controllableAndFailingAction = new ThrowingAction<IOException>() {

        @Override
        public void apply() throws IOException {
            // Now that we know we're first, start the second request...
            secondRequest.set(t3.execute(state -> mutex.storeCopy(ASSERT_NOT_CALLED)));
            // ...and wait for it to reach its destination
            barrier.awaitUninterruptibly();
            try {
                // OK, second request has made progress into the request, so we can now produce our failure
                throw controlledFailure;
            } finally {
                barrier.release();
            }
        }
    };
    Future<Object> firstRequest = t2.execute(state -> mutex.storeCopy(controllableAndFailingAction));
    while (secondRequest.get() == null) {
        parkARandomWhile();
    }
    t3.get().waitUntilWaiting(details -> details.isAt(StoreCopyCheckPointMutex.class, "waitForFirstStoreCopyActionToComplete"));
    // WHEN
    barrier.reached();
    // THEN
    try {
        firstRequest.get();
    } catch (ExecutionException e) {
        assertSame(controlledFailure, e.getCause());
    }
    try {
        secondRequest.get().get();
    } catch (ExecutionException e) {
        Throwable cooperativeActionFailure = e.getCause();
        assertThat(cooperativeActionFailure.getMessage(), containsString("Co-operative"));
        assertSame(controlledFailure, cooperativeActionFailure.getCause());
    }
    // WHEN afterwards trying another store-copy
    CountingAction action = new CountingAction();
    try (Resource lock = mutex.storeCopy(action)) {
        // THEN
        assertEquals(1, action.count());
    }
}
Also used : ThrowingAction(org.neo4j.function.ThrowingAction) Resource(org.neo4j.graphdb.Resource) OtherThreadRule(org.neo4j.test.rule.concurrent.OtherThreadRule) AtomicReference(java.util.concurrent.atomic.AtomicReference) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Matchers.lessThan(org.hamcrest.Matchers.lessThan) ThrowingAction(org.neo4j.function.ThrowingAction) Assert.fail(org.junit.Assert.fail) Barrier(org.neo4j.test.Barrier) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) LockSupport(java.util.concurrent.locks.LockSupport) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Race.throwing(org.neo4j.test.Race.throwing) Matchers.containsString(org.hamcrest.Matchers.containsString) Race(org.neo4j.test.Race) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Resource(org.neo4j.graphdb.Resource) Barrier(org.neo4j.test.Barrier) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 12 with Resource

use of org.neo4j.graphdb.Resource in project neo4j by neo4j.

the class StoreCopyCheckPointMutexTest method shouldHandleMultipleConcurrentStoreCopyRequests.

@Test
public void shouldHandleMultipleConcurrentStoreCopyRequests() throws Throwable {
    // GIVEN
    Race race = new Race();
    CountingAction action = new CountingAction();
    int threads = Runtime.getRuntime().availableProcessors() * 10;
    race.addContestants(threads, throwing(() -> {
        parkARandomWhile();
        try (Resource lock = mutex.storeCopy(action)) {
            parkARandomWhile();
        }
    }));
    race.go();
    // THEN
    // It's hard to make predictions about what should have been seen. Most importantly is that
    // The lock doesn't hang any requests and that number of calls to the action less than number of threads
    assertThat(action.count(), lessThan(threads));
}
Also used : Race(org.neo4j.test.Race) Resource(org.neo4j.graphdb.Resource) Test(org.junit.Test)

Aggregations

Resource (org.neo4j.graphdb.Resource)12 Test (org.junit.Test)8 StoreFileMetadata (org.neo4j.storageengine.api.StoreFileMetadata)3 File (java.io.File)2 IOException (java.io.IOException)2 PagedFile (org.neo4j.io.pagecache.PagedFile)2 SimpleTriggerInfo (org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo)2 Race (org.neo4j.test.Race)2 ByteBuffer (java.nio.ByteBuffer)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LockSupport (java.util.concurrent.locks.LockSupport)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.lessThan (org.hamcrest.Matchers.lessThan)1