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