Search in sources :

Example 1 with NoLeaderFoundException

use of org.neo4j.causalclustering.core.consensus.NoLeaderFoundException in project neo4j by neo4j.

the class GetServersProcedureV1Test method shouldReturnNoWriteEndpointsIfThereIsNoLeader.

@Test
public void shouldReturnNoWriteEndpointsIfThereIsNoLeader() throws Exception {
    // given
    final CoreTopologyService topologyService = mock(CoreTopologyService.class);
    Map<MemberId, CoreServerInfo> coreMembers = new HashMap<>();
    coreMembers.put(member(0), adressesForCore(0));
    when(topologyService.coreServers()).thenReturn(new CoreTopology(clusterId, false, coreMembers));
    when(topologyService.readReplicas()).thenReturn(new ReadReplicaTopology(emptyMap()));
    LeaderLocator leaderLocator = mock(LeaderLocator.class);
    when(leaderLocator.getLeader()).thenThrow(new NoLeaderFoundException());
    LegacyGetServersProcedure procedure = new LegacyGetServersProcedure(topologyService, leaderLocator, config, getInstance());
    // when
    ClusterView clusterView = run(procedure);
    // then
    ClusterView.Builder builder = new ClusterView.Builder();
    builder.readAddress(adressesForCore(0).connectors().boltAddress());
    builder.routeAddress(adressesForCore(0).connectors().boltAddress());
    assertEquals(builder.build(), clusterView);
}
Also used : NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) HashMap(java.util.HashMap) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) MemberId(org.neo4j.causalclustering.identity.MemberId) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) CoreTopologyService(org.neo4j.causalclustering.discovery.CoreTopologyService) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) ReadReplicaTopology(org.neo4j.causalclustering.discovery.ReadReplicaTopology) Test(org.junit.Test)

Example 2 with NoLeaderFoundException

use of org.neo4j.causalclustering.core.consensus.NoLeaderFoundException in project neo4j by neo4j.

the class RaftReplicator method replicate.

@Override
public Future<Object> replicate(ReplicatedContent command, boolean trackResult) throws InterruptedException, NoLeaderFoundException {
    OperationContext session = sessionPool.acquireSession();
    DistributedOperation operation = new DistributedOperation(command, session.globalSession(), session.localOperationId());
    Progress progress = progressTracker.start(operation);
    RetryStrategy.Timeout timeout = retryStrategy.newTimeout();
    do {
        assertDatabaseNotShutdown();
        try {
            outbound.send(leaderLocator.getLeader(), new RaftMessages.NewEntry.Request(me, operation));
            progress.awaitReplication(timeout.getMillis());
            timeout.increment();
        } catch (InterruptedException e) {
            progressTracker.abort(operation);
            throw e;
        } catch (NoLeaderFoundException e) {
            log.debug("Could not replicate operation " + operation + " because no leader was found. Retrying.", e);
        }
    } while (!progress.isReplicated());
    BiConsumer<Object, Throwable> cleanup = (ignored1, ignored2) -> sessionPool.releaseSession(session);
    if (trackResult) {
        progress.futureResult().whenComplete(cleanup);
    } else {
        cleanup.accept(null, null);
    }
    return progress.futureResult();
}
Also used : OperationContext(org.neo4j.causalclustering.core.replication.session.OperationContext) LifecycleAdapter(org.neo4j.kernel.lifecycle.LifecycleAdapter) AvailabilityGuard(org.neo4j.kernel.AvailabilityGuard) Log(org.neo4j.logging.Log) Listener(org.neo4j.kernel.impl.util.Listener) LogProvider(org.neo4j.logging.LogProvider) LocalSessionPool(org.neo4j.causalclustering.core.replication.session.LocalSessionPool) Future(java.util.concurrent.Future) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) BiConsumer(java.util.function.BiConsumer) MemberId(org.neo4j.causalclustering.identity.MemberId) Outbound(org.neo4j.causalclustering.messaging.Outbound) DatabaseShutdownException(org.neo4j.graphdb.DatabaseShutdownException) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) OperationContext(org.neo4j.causalclustering.core.replication.session.OperationContext) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) RetryStrategy(org.neo4j.causalclustering.helper.RetryStrategy) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) RetryStrategy(org.neo4j.causalclustering.helper.RetryStrategy)

Example 3 with NoLeaderFoundException

use of org.neo4j.causalclustering.core.consensus.NoLeaderFoundException in project neo4j by neo4j.

the class ReplicatedTransactionCommitProcess method commit.

@Override
public long commit(final TransactionToApply tx, final CommitEvent commitEvent, TransactionApplicationMode mode) throws TransactionFailureException {
    ReplicatedTransaction transaction = createImmutableReplicatedTransaction(tx.transactionRepresentation());
    Future<Object> futureTxId;
    try {
        futureTxId = replicator.replicate(transaction, true);
    } catch (InterruptedException e) {
        throw new TransactionFailureException("Interrupted replicating transaction.", e);
    } catch (NoLeaderFoundException e) {
        throw new TransactionFailureException("No leader found while replicating transaction.", e);
    }
    try {
        return (long) futureTxId.get();
    } catch (ExecutionException e) {
        if (e.getCause() instanceof TransactionFailureException) {
            throw (TransactionFailureException) e.getCause();
        }
        // TODO: Panic?
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        // TODO Wait for the transaction to possibly finish within a user configurable time, before aborting.
        throw new TransactionFailureException("Interrupted while waiting for txId", e);
    }
}
Also used : TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) ReplicatedTransactionFactory.createImmutableReplicatedTransaction(org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransactionFactory.createImmutableReplicatedTransaction) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with NoLeaderFoundException

use of org.neo4j.causalclustering.core.consensus.NoLeaderFoundException 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 5 with NoLeaderFoundException

use of org.neo4j.causalclustering.core.consensus.NoLeaderFoundException in project neo4j by neo4j.

the class ServerPoliciesPlugin method writeEndpoints.

private List<Endpoint> writeEndpoints(CoreTopology cores) {
    MemberId leader;
    try {
        leader = leaderLocator.getLeader();
    } catch (NoLeaderFoundException e) {
        return emptyList();
    }
    Optional<Endpoint> endPoint = cores.find(leader).map(extractBoltAddress()).map(Endpoint::write);
    return asList(endPoint);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) Endpoint(org.neo4j.causalclustering.load_balancing.Endpoint)

Aggregations

NoLeaderFoundException (org.neo4j.causalclustering.core.consensus.NoLeaderFoundException)7 MemberId (org.neo4j.causalclustering.identity.MemberId)5 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)4 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)3 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)3 Log (org.neo4j.logging.Log)3 LogProvider (org.neo4j.logging.LogProvider)3 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 ExecutionException (java.util.concurrent.ExecutionException)2 ReadReplicaTopology (org.neo4j.causalclustering.discovery.ReadReplicaTopology)2 TopologyService (org.neo4j.causalclustering.discovery.TopologyService)2 Endpoint (org.neo4j.causalclustering.load_balancing.Endpoint)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 Comparator.comparing (java.util.Comparator.comparing)1 HashMap (java.util.HashMap)1