use of org.neo4j.storageengine.api.lock.AcquireLockTimeoutException in project neo4j by neo4j.
the class LeaderOnlyLockManager method acquireTokenOrThrow.
/**
* Acquires a valid token id owned by us or throws.
*/
private synchronized int acquireTokenOrThrow() {
LockToken currentToken = lockTokenStateMachine.currentToken();
if (myself.equals(currentToken.owner())) {
return currentToken.id();
}
/* If we are not the leader then we will not even attempt to get the token,
since only the leader should take locks. */
ensureLeader();
ReplicatedLockTokenRequest lockTokenRequest = new ReplicatedLockTokenRequest(myself, LockToken.nextCandidateId(currentToken.id()));
Future<Object> future;
try {
future = replicator.replicate(lockTokenRequest, true);
} catch (InterruptedException e) {
throw new AcquireLockTimeoutException(e, "Interrupted acquiring token.", Interrupted);
} catch (NoLeaderFoundException e) {
throw new AcquireLockTimeoutException(e, "Could not acquire lock token because no leader was found.", NoLeaderAvailable);
}
try {
boolean success = (boolean) future.get();
if (success) {
return lockTokenRequest.id();
} else {
throw new AcquireLockTimeoutException("Failed to acquire lock token. Was taken by another candidate.", NotALeader);
}
} catch (ExecutionException e) {
throw new AcquireLockTimeoutException(e, "Failed to acquire lock token.", NotALeader);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AcquireLockTimeoutException(e, "Failed to acquire lock token.", Interrupted);
}
}
use of org.neo4j.storageengine.api.lock.AcquireLockTimeoutException in project neo4j by neo4j.
the class RestartIT method restartWhileDoingTransactions.
@Test
public void restartWhileDoingTransactions() throws Exception {
// given
Cluster cluster = clusterRule.startCluster();
// when
final GraphDatabaseService coreDB = cluster.getCoreMemberById(0).database();
ExecutorService executor = Executors.newCachedThreadPool();
final AtomicBoolean done = new AtomicBoolean(false);
executor.execute(() -> {
while (!done.get()) {
try (Transaction tx = coreDB.beginTx()) {
Node node = coreDB.createNode(label("boo"));
node.setProperty("foobar", "baz_bat");
tx.success();
} catch (AcquireLockTimeoutException | WriteOperationsNotAllowedException e) {
// expected sometimes
}
}
});
Thread.sleep(500);
cluster.removeCoreMemberWithMemberId(1);
cluster.addCoreMemberWithId(1).start();
Thread.sleep(500);
// then
done.set(true);
executor.shutdown();
}
use of org.neo4j.storageengine.api.lock.AcquireLockTimeoutException in project neo4j by neo4j.
the class LeaderOnlyLockManagerTest method shouldNotIssueLocksOnNonLeader.
@Test
public void shouldNotIssueLocksOnNonLeader() throws Exception {
// given
MemberId me = member(0);
MemberId leader = member(1);
ReplicatedLockTokenStateMachine replicatedLockStateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage(new ReplicatedLockTokenState()));
DirectReplicator replicator = new DirectReplicator(replicatedLockStateMachine);
LeaderLocator leaderLocator = mock(LeaderLocator.class);
when(leaderLocator.getLeader()).thenReturn(leader);
Locks locks = mock(Locks.class);
when(locks.newClient()).thenReturn(mock(Locks.Client.class));
LeaderOnlyLockManager lockManager = new LeaderOnlyLockManager(me, replicator, leaderLocator, locks, replicatedLockStateMachine);
// when
Locks.Client lockClient = lockManager.newClient();
try {
lockClient.acquireExclusive(LockTracer.NONE, ResourceTypes.NODE, 0L);
fail("Should have thrown exception");
} catch (AcquireLockTimeoutException e) {
// expected
}
}
Aggregations