Search in sources :

Example 1 with AcquireLockTimeoutException

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);
    }
}
Also used : AcquireLockTimeoutException(org.neo4j.storageengine.api.lock.AcquireLockTimeoutException) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with AcquireLockTimeoutException

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();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AcquireLockTimeoutException(org.neo4j.storageengine.api.lock.AcquireLockTimeoutException) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ExecutorService(java.util.concurrent.ExecutorService) Cluster(org.neo4j.causalclustering.discovery.Cluster) WriteOperationsNotAllowedException(org.neo4j.graphdb.security.WriteOperationsNotAllowedException) Test(org.junit.Test)

Example 3 with AcquireLockTimeoutException

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
    }
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) InMemoryStateStorage(org.neo4j.causalclustering.core.state.storage.InMemoryStateStorage) AcquireLockTimeoutException(org.neo4j.storageengine.api.lock.AcquireLockTimeoutException) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) DirectReplicator(org.neo4j.causalclustering.core.replication.DirectReplicator) Locks(org.neo4j.kernel.impl.locking.Locks) Test(org.junit.Test)

Aggregations

AcquireLockTimeoutException (org.neo4j.storageengine.api.lock.AcquireLockTimeoutException)3 Test (org.junit.Test)2 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)1 NoLeaderFoundException (org.neo4j.causalclustering.core.consensus.NoLeaderFoundException)1 DirectReplicator (org.neo4j.causalclustering.core.replication.DirectReplicator)1 InMemoryStateStorage (org.neo4j.causalclustering.core.state.storage.InMemoryStateStorage)1 Cluster (org.neo4j.causalclustering.discovery.Cluster)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Node (org.neo4j.graphdb.Node)1 Transaction (org.neo4j.graphdb.Transaction)1 WriteOperationsNotAllowedException (org.neo4j.graphdb.security.WriteOperationsNotAllowedException)1 Locks (org.neo4j.kernel.impl.locking.Locks)1