use of org.apache.ratis.protocol.exceptions.GroupMismatchException 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.GroupMismatchException in project incubator-ratis by apache.
the class BlockingImpl method sendRequest.
private RaftClientReply sendRequest(RaftClientRequest request) throws IOException {
LOG.debug("{}: send {}", client.getId(), request);
RaftClientReply reply;
try {
reply = client.getClientRpc().sendRequest(request);
} catch (GroupMismatchException gme) {
throw gme;
} catch (IOException ioe) {
client.handleIOException(request, ioe);
throw ioe;
}
LOG.debug("{}: receive {}", client.getId(), reply);
reply = client.handleLeaderException(request, reply);
reply = RaftClientImpl.handleRaftException(reply, Function.identity());
return reply;
}
use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.
the class RaftServerProxy method groupAddAsync.
private CompletableFuture<RaftClientReply> groupAddAsync(GroupManagementRequest request, RaftGroup newGroup) {
if (!request.getRaftGroupId().equals(newGroup.getGroupId())) {
return JavaUtils.completeExceptionally(new GroupMismatchException(getId() + ": Request group id (" + request.getRaftGroupId() + ") does not match the new group " + newGroup));
}
return impls.addNew(newGroup).thenApplyAsync(newImpl -> {
LOG.debug("{}: newImpl = {}", getId(), newImpl);
final boolean started = newImpl.start();
Preconditions.assertTrue(started, () -> getId() + ": failed to start a new impl: " + newImpl);
return newImpl.newSuccessReply(request);
}, implExecutor).whenComplete((raftClientReply, throwable) -> {
if (throwable != null) {
if (!(throwable.getCause() instanceof AlreadyExistsException)) {
impls.remove(newGroup.getGroupId());
LOG.warn(getId() + ": Failed groupAdd* " + request, throwable);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug(getId() + ": Failed groupAdd* " + request, throwable);
}
}
}
});
}
use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.
the class RaftServerImpl method checkLeaderState.
/**
* @return null if the server is in leader state.
*/
private CompletableFuture<RaftClientReply> checkLeaderState(RaftClientRequest request, CacheEntry entry, boolean isWrite) {
try {
assertGroup(request.getRequestorId(), request.getRaftGroupId());
} catch (GroupMismatchException e) {
return RetryCacheImpl.failWithException(e, entry);
}
if (!getInfo().isLeader()) {
NotLeaderException exception = generateNotLeaderException();
final RaftClientReply reply = newExceptionReply(request, exception);
return RetryCacheImpl.failWithReply(reply, entry);
}
if (!getInfo().isLeaderReady()) {
final CacheEntry cacheEntry = retryCache.getIfPresent(ClientInvocationId.valueOf(request));
if (cacheEntry != null && cacheEntry.isCompletedNormally()) {
return cacheEntry.getReplyFuture();
}
final LeaderNotReadyException lnre = new LeaderNotReadyException(getMemberId());
final RaftClientReply reply = newExceptionReply(request, lnre);
return RetryCacheImpl.failWithReply(reply, entry);
}
if (isWrite && isSteppingDown()) {
final LeaderSteppingDownException lsde = new LeaderSteppingDownException(getMemberId() + " is stepping down");
final RaftClientReply reply = newExceptionReply(request, lsde);
return RetryCacheImpl.failWithReply(reply, entry);
}
return null;
}
use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.
the class OrderedAsync method sendRequest.
private CompletableFuture<RaftClientReply> sendRequest(PendingOrderedRequest pending) {
final RetryPolicy retryPolicy = client.getRetryPolicy();
final CompletableFuture<RaftClientReply> f;
final RaftClientRequest request;
if (getSlidingWindow((RaftPeerId) null).isFirst(pending.getSeqNum())) {
pending.setFirstRequest();
}
request = pending.newRequest();
LOG.debug("{}: send* {}", client.getId(), request);
f = client.getClientRpc().sendRequestAsync(request);
return f.thenApply(reply -> {
LOG.debug("{}: receive* {}", client.getId(), reply);
getSlidingWindow(request).receiveReply(request.getSlidingWindowEntry().getSeqNum(), reply, this::sendRequestWithRetry);
return reply;
}).exceptionally(e -> {
if (LOG.isTraceEnabled()) {
LOG.trace(client.getId() + ": Failed* " + request, e);
} else {
LOG.debug("{}: Failed* {} with {}", client.getId(), request, e);
}
e = JavaUtils.unwrapCompletionException(e);
if (e instanceof IOException && !(e instanceof GroupMismatchException)) {
pending.incrementExceptionCount(e);
final ClientRetryEvent event = new ClientRetryEvent(request, e, pending);
if (!retryPolicy.handleAttemptFailure(event).shouldRetry()) {
handleAsyncRetryFailure(event);
} else {
if (e instanceof NotLeaderException) {
NotLeaderException nle = (NotLeaderException) e;
client.handleNotLeaderException(request, nle, this::resetSlidingWindow);
} else {
client.handleIOException(request, (IOException) e, null, this::resetSlidingWindow);
}
}
throw new CompletionException(e);
}
failAllAsyncRequests(request, e);
return null;
});
}
Aggregations