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?")));
}
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());
}
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();
}
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;
}
}
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())));
}
Aggregations