use of java.util.concurrent.CompletionException in project pravega by pravega.
the class ClientAdapterBase method delete.
@Override
public CompletableFuture<Void> delete(String streamName, Duration timeout) {
ensureRunning();
String parentName = StreamSegmentNameUtils.getParentStreamSegmentName(streamName);
if (isTransaction(streamName, parentName)) {
// We have a transaction to abort.
return abortTransaction(streamName, timeout);
} else {
return CompletableFuture.runAsync(() -> {
if (getStreamManager().deleteStream(SCOPE, streamName)) {
closeWriters(streamName);
} else {
throw new CompletionException(new StreamingException(String.format("Unable to delete stream '%s'.", streamName)));
}
}, this.testExecutor);
}
}
use of java.util.concurrent.CompletionException in project atomix by atomix.
the class LeaderRole method onTransfer.
@Override
public CompletableFuture<TransferResponse> onTransfer(final TransferRequest request) {
logRequest(request);
RaftMemberContext member = raft.getCluster().getMemberState(request.member());
if (member == null) {
return CompletableFuture.completedFuture(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.ILLEGAL_MEMBER_STATE).build()));
}
transferring = true;
CompletableFuture<TransferResponse> future = new CompletableFuture<>();
appender.appendEntries(raft.getLogWriter().getLastIndex()).whenComplete((result, error) -> {
if (isRunning()) {
if (error == null) {
log.debug("Transferring leadership to {}", request.member());
raft.transition(RaftServer.Role.FOLLOWER);
future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.OK).build()));
} else if (error instanceof CompletionException && error.getCause() instanceof RaftException) {
future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(((RaftException) error.getCause()).getType(), error.getMessage()).build()));
} else if (error instanceof RaftException) {
future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(((RaftException) error).getType(), error.getMessage()).build()));
} else {
future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.PROTOCOL_ERROR, error.getMessage()).build()));
}
} else {
future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.ILLEGAL_MEMBER_STATE).build()));
}
});
return future;
}
use of java.util.concurrent.CompletionException in project atomix by atomix.
the class LeaderRole method onKeepAlive.
@Override
public CompletableFuture<KeepAliveResponse> onKeepAlive(KeepAliveRequest request) {
final long term = raft.getTerm();
final long timestamp = System.currentTimeMillis();
raft.checkThread();
logRequest(request);
CompletableFuture<KeepAliveResponse> future = new CompletableFuture<>();
appendAndCompact(new KeepAliveEntry(term, timestamp, request.sessionIds(), request.commandSequenceNumbers(), request.eventIndexes())).whenCompleteAsync((entry, error) -> {
if (error != null) {
future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(RaftError.Type.PROTOCOL_ERROR).build()));
return;
}
appender.appendEntries(entry.index()).whenComplete((commitIndex, commitError) -> {
raft.checkThread();
if (isRunning()) {
if (commitError == null) {
raft.getServiceManager().<long[]>apply(entry.index()).whenCompleteAsync((sessionResult, sessionError) -> {
if (sessionError == null) {
future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.OK).withLeader(raft.getCluster().getMember().nodeId()).withMembers(raft.getCluster().getMembers().stream().map(RaftMember::nodeId).filter(m -> m != null).collect(Collectors.toList())).withSessionIds(sessionResult).build()));
} else if (sessionError instanceof CompletionException && sessionError.getCause() instanceof RaftException) {
future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(((RaftException) sessionError.getCause()).getType(), sessionError.getMessage()).build()));
} else if (sessionError instanceof RaftException) {
future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(((RaftException) sessionError).getType(), sessionError.getMessage()).build()));
} else {
future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(RaftError.Type.PROTOCOL_ERROR, sessionError.getMessage()).build()));
}
}, raft.getThreadContext());
} else {
future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(RaftError.Type.PROTOCOL_ERROR).build()));
}
} else {
RaftMember leader = raft.getLeader();
future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(leader != null ? leader.nodeId() : null).withError(RaftError.Type.ILLEGAL_MEMBER_STATE).build()));
}
});
}, raft.getThreadContext());
return future;
}
use of java.util.concurrent.CompletionException in project assertj-core by joel-costigliola.
the class StandardRepresentation method toStringOf.
protected String toStringOf(CompletableFuture<?> future) {
String className = future.getClass().getSimpleName();
if (!future.isDone())
return concat(className, "[Incomplete]");
try {
Object joinResult = future.join();
// avoid stack overflow error if future join on itself or another future that cycles back to the first
Object joinResultRepresentation = joinResult instanceof CompletableFuture ? joinResult : toStringOf(joinResult);
return concat(className, "[Completed: ", joinResultRepresentation, "]");
} catch (CompletionException e) {
return concat(className, "[Failed: ", toStringOf(e.getCause()), "]", String.format("%n%s", getStackTrace(e)));
} catch (CancellationException e) {
return concat(className, "[Cancelled]");
}
}
use of java.util.concurrent.CompletionException in project photon-model by vmware.
the class TestAWSClientManagement method testAWSClientManagementArnRefresh.
/**
* This test requires a minimum of 15 minutes waiting to ensure the ARN credentials refresh
* occurs.
*/
@Ignore
@Test
public void testAWSClientManagementArnRefresh() throws Throwable {
this.ec2ClientReferenceCount = getClientReferenceCount(AwsClientType.EC2);
this.host.setTimeoutSeconds(1200);
// Getting a reference to client managers in the test
AWSClientManager ec2ClientManager = getClientManager(AwsClientType.EC2);
ec2ClientManager.cleanUpArnCache();
assertEquals(this.ec2ClientReferenceCount + 1, getClientReferenceCount(AwsClientType.EC2));
this.creds = new AuthCredentialsServiceState();
this.creds.customProperties = new HashMap<>();
this.creds.customProperties.put(ARN_KEY, this.arn);
this.creds.customProperties.put(EXTERNAL_ID_KEY, this.externalId);
TestContext waitContext = new TestContext(1, Duration.ofSeconds(30L));
ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
waitContext.fail(t);
throw new CompletionException(t);
}).thenAccept(ec2Client -> {
this.client = ec2Client;
waitContext.complete();
});
waitContext.await();
this.clientCacheCount = ec2ClientManager.getCacheCount();
host.log(Level.INFO, "Waiting 16 minutes for the current set of credentials to expire.");
Thread.sleep(TimeUnit.MINUTES.toMillis(16));
host.log(Level.INFO, "Retrieving the ec2 client with a refreshed set of credentials.");
// Requesting the EC2 client will generate a new client as the original client's credentials
// are now expired.
AmazonEC2AsyncClient oldClient = this.client;
TestContext nextContext = new TestContext(1, Duration.ofSeconds(30L));
ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
nextContext.fail(t);
throw new CompletionException(t);
}).thenAccept(ec2Client -> {
this.client = ec2Client;
this.clientCacheCount++;
nextContext.complete();
});
nextContext.await();
assertNotEquals(oldClient, this.client);
assertEquals(this.clientCacheCount, ec2ClientManager.getCacheCount());
}
Aggregations