use of org.apache.ratis.protocol.exceptions.AlreadyClosedException in project incubator-ratis by apache.
the class BlockingImpl method sendRequestWithRetry.
RaftClientReply sendRequestWithRetry(Supplier<RaftClientRequest> supplier) throws IOException {
RaftClientImpl.PendingClientRequest pending = new RaftClientImpl.PendingClientRequest() {
@Override
public RaftClientRequest newRequestImpl() {
return supplier.get();
}
};
while (true) {
final RaftClientRequest request = pending.newRequest();
IOException ioe = null;
try {
final RaftClientReply reply = sendRequest(request);
if (reply != null) {
return client.handleReply(request, reply);
}
} catch (GroupMismatchException | StateMachineException | TransferLeadershipException | LeaderSteppingDownException | AlreadyClosedException | AlreadyExistsException e) {
throw e;
} catch (IOException e) {
ioe = e;
}
pending.incrementExceptionCount(ioe);
ClientRetryEvent event = new ClientRetryEvent(request, ioe, pending);
final RetryPolicy retryPolicy = client.getRetryPolicy();
final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
TimeDuration sleepTime = client.getEffectiveSleepTime(ioe, action.getSleepTime());
if (!action.shouldRetry()) {
throw (IOException) client.noMoreRetries(event);
}
try {
sleepTime.sleep();
} catch (InterruptedException e) {
throw new InterruptedIOException("retry policy=" + retryPolicy);
}
}
}
use of org.apache.ratis.protocol.exceptions.AlreadyClosedException in project incubator-ratis by apache.
the class ClientProtoUtils method toAlreadyClosedException.
static AlreadyClosedException toAlreadyClosedException(String className, String errorMsg, ByteString stackTraceBytes) {
AlreadyClosedException ace;
if (className == null) {
ace = new AlreadyClosedException(errorMsg);
} else {
try {
Class<?> clazz = Class.forName(className);
final Exception e = ReflectionUtils.instantiateException(clazz.asSubclass(Exception.class), errorMsg);
ace = new AlreadyClosedException(errorMsg, e);
} catch (Exception e) {
ace = new AlreadyClosedException(className + ": " + errorMsg);
}
}
StackTraceElement[] stacktrace = (StackTraceElement[]) ProtoUtils.toObject(stackTraceBytes);
ace.setStackTrace(stacktrace);
return ace;
}
use of org.apache.ratis.protocol.exceptions.AlreadyClosedException in project incubator-ratis by apache.
the class GrpcClientRpc method sendRequest.
private CompletableFuture<RaftClientReply> sendRequest(RaftClientRequest request, GrpcClientProtocolClient proxy) throws IOException {
final RaftClientRequestProto requestProto = toRaftClientRequestProto(request);
final CompletableFuture<RaftClientReplyProto> replyFuture = new CompletableFuture<>();
// create a new grpc stream for each non-async call.
final StreamObserver<RaftClientRequestProto> requestObserver = proxy.unorderedWithTimeout(new StreamObserver<RaftClientReplyProto>() {
@Override
public void onNext(RaftClientReplyProto value) {
replyFuture.complete(value);
}
@Override
public void onError(Throwable t) {
replyFuture.completeExceptionally(GrpcUtil.unwrapIOException(t));
}
@Override
public void onCompleted() {
if (!replyFuture.isDone()) {
replyFuture.completeExceptionally(new AlreadyClosedException(clientId + ": Stream completed but no reply for request " + request));
}
}
});
requestObserver.onNext(requestProto);
requestObserver.onCompleted();
return replyFuture.thenApply(ClientProtoUtils::toRaftClientReply);
}
Aggregations