Search in sources :

Example 1 with DeadlockDetectedException

use of org.neo4j.kernel.DeadlockDetectedException in project neo4j by neo4j.

the class Neo4jErrorTest method shouldCombineErrors.

@Test
public void shouldCombineErrors() {
    // Given
    Neo4jError error1 = Neo4jError.from(new DeadlockDetectedException("In my life"));
    Neo4jError error2 = Neo4jError.from(new DeadlockDetectedException("Why do I give valuable time"));
    Neo4jError error3 = Neo4jError.from(new DeadlockDetectedException("To people who don't care if I live or die?"));
    // When
    Neo4jError combine = Neo4jError.combine(asList(error1, error2, error3));
    // Then
    assertThat(combine.status(), equalTo(Status.Transaction.DeadlockDetected));
    assertThat(combine.message(), equalTo(String.format("The following errors has occurred:%n%n" + "In my life%n" + "Why do I give valuable time%n" + "To people who don't care if I live or die?")));
}
Also used : DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) Test(org.junit.Test)

Example 2 with DeadlockDetectedException

use of org.neo4j.kernel.DeadlockDetectedException in project neo4j by neo4j.

the class RWLockTest method testThreadRemovedFromWaitingListOnDeadlock.

@Test(timeout = 1000)
public void testThreadRemovedFromWaitingListOnDeadlock() throws InterruptedException {
    RagManager ragManager = Mockito.mock(RagManager.class);
    LockResource resource = new LockResource(ResourceTypes.NODE, 1L);
    final RWLock lock = createRWLock(ragManager, resource);
    final LockTransaction lockTransaction = new LockTransaction();
    final LockTransaction anotherTransaction = new LockTransaction();
    final CountDownLatch exceptionLatch = new CountDownLatch(1);
    final CountDownLatch completionLatch = new CountDownLatch(1);
    Mockito.doNothing().doAnswer(invocation -> {
        exceptionLatch.countDown();
        throw new DeadlockDetectedException("Deadlock");
    }).when(ragManager).checkWaitOn(lock, lockTransaction);
    lock.mark();
    lock.mark();
    lock.acquireReadLock(LockTracer.NONE, lockTransaction);
    lock.acquireReadLock(LockTracer.NONE, anotherTransaction);
    // writer will be added to a waiting list
    // then spurious wake up will be simulated
    // and deadlock will be detected
    Runnable writer = () -> {
        try {
            lock.mark();
            lock.acquireWriteLock(LockTracer.NONE, lockTransaction);
        } catch (DeadlockDetectedException ignored) {
        // ignored
        }
        completionLatch.countDown();
    };
    executor.execute(writer);
    waitWaitingThreads(lock, 1);
    // sending notify for all threads till our writer will not cause deadlock exception
    do {
        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (lock) {
            lock.notifyAll();
        }
    } while (exceptionLatch.getCount() == 1);
    // waiting for writer to finish
    completionLatch.await();
    assertEquals("In case of deadlock caused by spurious wake up " + "thread should be removed from waiting list", 0, lock.getWaitingThreadsCount());
}
Also used : DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with DeadlockDetectedException

use of org.neo4j.kernel.DeadlockDetectedException in project neo4j by neo4j.

the class TransactionConstraintsIT method deadlockDetectionBetween.

private void deadlockDetectionBetween(HighlyAvailableGraphDatabase slave1, final HighlyAvailableGraphDatabase slave2) throws Exception {
    // GIVEN
    // -- two members acquiring a read lock on the same entity
    final Node commonNode;
    try (Transaction tx = slave1.beginTx()) {
        commonNode = slave1.createNode();
        tx.success();
    }
    OtherThreadExecutor<HighlyAvailableGraphDatabase> thread2 = new OtherThreadExecutor<>("T2", slave2);
    Transaction tx1 = slave1.beginTx();
    Transaction tx2 = thread2.execute(new BeginTx());
    tx1.acquireReadLock(commonNode);
    thread2.execute(state -> tx2.acquireReadLock(commonNode));
    // -- and one of them wanting (and awaiting) to upgrade its read lock to a write lock
    Future<Lock> writeLockFuture = thread2.executeDontWait(state -> {
        try (Transaction ignored = tx2) {
            return tx2.acquireWriteLock(commonNode);
        }
    });
    for (int i = 0; i < 10; i++) {
        thread2.waitUntilThreadState(Thread.State.TIMED_WAITING, Thread.State.WAITING);
        Thread.sleep(2);
    }
    try (// Close transaction no matter what happens
    Transaction ignored = tx1) {
        // WHEN
        tx1.acquireWriteLock(commonNode);
        // -- Deadlock detection is non-deterministic, so either the slave or the master will detect it
        writeLockFuture.get();
        fail("Deadlock exception should have been thrown");
    } catch (DeadlockDetectedException e) {
    // THEN -- deadlock should be avoided with this exception
    } catch (ExecutionException e) {
        // OR -- the tx2 thread fails with executionexception, caused by deadlock on its end
        assertThat(e.getCause(), instanceOf(DeadlockDetectedException.class));
    }
    thread2.close();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) Node(org.neo4j.graphdb.Node) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) ExecutionException(java.util.concurrent.ExecutionException) Lock(org.neo4j.graphdb.Lock)

Example 4 with DeadlockDetectedException

use of org.neo4j.kernel.DeadlockDetectedException in project graphdb by neo4j-attic.

the class SlaveLockManager method getWriteLock.

@Override
public void getWriteLock(Object resource) throws DeadlockDetectedException, IllegalResourceException {
    // Code copied from getReadLock. Fix!
    try {
        Node node = resource instanceof Node ? (Node) resource : null;
        Relationship relationship = resource instanceof Relationship ? (Relationship) resource : null;
        if (node == null && relationship == null) {
            // This is a "fake" resource, only grab the lock locally
            super.getWriteLock(resource);
            return;
        }
        //          if ( hasAlreadyGotLock() )
        //          {
        //              return;
        //          }
        LockResult result = null;
        do {
            int eventIdentifier = getLocalTxId();
            result = node != null ? receiver.receive(broker.getMaster().first().acquireNodeWriteLock(receiver.getSlaveContext(eventIdentifier), node.getId())) : receiver.receive(broker.getMaster().first().acquireRelationshipWriteLock(receiver.getSlaveContext(eventIdentifier), relationship.getId()));
            switch(result.getStatus()) {
                case OK_LOCKED:
                    super.getWriteLock(resource);
                    return;
                case DEAD_LOCKED:
                    throw new DeadlockDetectedException(result.getDeadlockMessage());
            }
        } while (result.getStatus() == LockStatus.NOT_LOCKED);
    } catch (ZooKeeperException e) {
        receiver.newMaster(null, e);
        throw e;
    } catch (ComException e) {
        receiver.newMaster(null, e);
        throw e;
    }
}
Also used : ZooKeeperException(org.neo4j.kernel.ha.zookeeper.ZooKeeperException) ComException(org.neo4j.com.ComException) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException)

Example 5 with DeadlockDetectedException

use of org.neo4j.kernel.DeadlockDetectedException in project neo4j by neo4j.

the class MasterImplTest method lockResultMustHaveMessageWhenAcquiringExclusiveLockDeadlocks.

@Test
public void lockResultMustHaveMessageWhenAcquiringExclusiveLockDeadlocks() throws Exception {
    MasterImpl.SPI spi = mockedSpi();
    DefaultConversationSPI conversationSpi = mockedConversationSpi();
    Config config = config();
    ConversationManager conversationManager = new ConversationManager(conversationSpi, config);
    conversationManager.start();
    Client locks = mock(Client.class);
    MasterImpl master = new MasterImpl(spi, conversationManager, null, config);
    RequestContext context = createRequestContext(master);
    when(conversationSpi.acquireClient()).thenReturn(locks);
    ResourceTypes type = ResourceTypes.NODE;
    doThrow(new DeadlockDetectedException("")).when(locks).acquireExclusive(LockTracer.NONE, type, 1);
    master.acquireExclusiveLock(context, type, 1);
    ArgumentCaptor<LockResult> captor = ArgumentCaptor.forClass(LockResult.class);
    verify(spi).packTransactionObligationResponse(argThat(is(context)), captor.capture());
    assertThat(captor.getValue().getMessage(), is(not(nullValue())));
}
Also used : SPI(org.neo4j.kernel.ha.com.master.MasterImpl.SPI) ResourceTypes(org.neo4j.kernel.impl.locking.ResourceTypes) DefaultConversationSPI(org.neo4j.kernel.ha.cluster.DefaultConversationSPI) LockResult(org.neo4j.kernel.ha.lock.LockResult) Config(org.neo4j.kernel.configuration.Config) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) RequestContext(org.neo4j.com.RequestContext) Client(org.neo4j.kernel.impl.locking.Locks.Client) Test(org.junit.Test)

Aggregations

DeadlockDetectedException (org.neo4j.kernel.DeadlockDetectedException)14 Test (org.junit.Test)6 LockResult (org.neo4j.kernel.ha.lock.LockResult)4 Node (org.neo4j.graphdb.Node)3 IllegalResourceException (org.neo4j.kernel.impl.transaction.IllegalResourceException)3 ComException (org.neo4j.com.ComException)2 RequestContext (org.neo4j.com.RequestContext)2 Relationship (org.neo4j.graphdb.Relationship)2 Config (org.neo4j.kernel.configuration.Config)2 DefaultConversationSPI (org.neo4j.kernel.ha.cluster.DefaultConversationSPI)2 SPI (org.neo4j.kernel.ha.com.master.MasterImpl.SPI)2 ZooKeeperException (org.neo4j.kernel.ha.zookeeper.ZooKeeperException)2 Locks (org.neo4j.kernel.impl.locking.Locks)2 Client (org.neo4j.kernel.impl.locking.Locks.Client)2 ResourceTypes (org.neo4j.kernel.impl.locking.ResourceTypes)2 TransactionalContext (org.neo4j.kernel.impl.query.TransactionalContext)2 ConcurrentAccessException (org.neo4j.kernel.impl.util.collection.ConcurrentAccessException)2 NoSuchEntryException (org.neo4j.kernel.impl.util.collection.NoSuchEntryException)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1