use of org.apache.ratis.protocol.exceptions.TransferLeadershipException 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.TransferLeadershipException in project incubator-ratis by apache.
the class LeaderElectionTests method testTransferLeaderTimeout.
@Test
public void testTransferLeaderTimeout() throws Exception {
try (final MiniRaftCluster cluster = newCluster(3)) {
cluster.start();
final RaftServer.Division leader = waitForLeader(cluster);
try (RaftClient client = cluster.createClient(leader.getId())) {
List<RaftServer.Division> followers = cluster.getFollowers();
Assert.assertEquals(followers.size(), 2);
RaftServer.Division newLeader = followers.get(0);
// isolate new leader, so that transfer leadership will timeout
isolate(cluster, newLeader.getId());
List<RaftPeer> peers = cluster.getPeers();
List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
Assert.assertTrue(reply.isSuccess());
CompletableFuture<Boolean> transferTimeoutFuture = CompletableFuture.supplyAsync(() -> {
try {
long timeoutMs = 5000;
long start = System.currentTimeMillis();
try {
client.admin().transferLeadership(newLeader.getId(), timeoutMs);
} catch (TransferLeadershipException e) {
long cost = System.currentTimeMillis() - start;
Assert.assertTrue(cost > timeoutMs);
Assert.assertTrue(e.getMessage().contains("Failed to transfer leadership to"));
Assert.assertTrue(e.getMessage().contains("timed out"));
}
return true;
} catch (IOException e) {
return false;
}
});
// before transfer timeout, leader should in steppingDown
JavaUtils.attemptRepeatedly(() -> {
try {
client.io().send(new RaftTestUtil.SimpleMessage("message"));
} catch (LeaderSteppingDownException e) {
Assert.assertTrue(e.getMessage().contains("is stepping down"));
}
return null;
}, 5, TimeDuration.ONE_SECOND, "check leader steppingDown", RaftServer.LOG);
Assert.assertTrue(transferTimeoutFuture.get());
// after transfer timeout, leader should accept request
reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
Assert.assertTrue(reply.getReplierId().equals(leader.getId().toString()));
Assert.assertTrue(reply.isSuccess());
deIsolate(cluster, newLeader.getId());
}
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.exceptions.TransferLeadershipException in project incubator-ratis by apache.
the class TransferLeadership method start.
CompletableFuture<RaftClientReply> start(TransferLeadershipRequest request) {
final MemoizedSupplier<PendingRequest> supplier = JavaUtils.memoize(() -> new PendingRequest(request));
final PendingRequest previous = pending.getAndUpdate(f -> f != null ? f : supplier.get());
if (previous != null) {
if (request.getNewLeader().equals(previous.getRequest().getNewLeader())) {
final CompletableFuture<RaftClientReply> replyFuture = new CompletableFuture<>();
previous.getReplyFuture().whenComplete((r, e) -> {
if (e != null) {
replyFuture.completeExceptionally(e);
} else {
replyFuture.complete(r.isSuccess() ? server.newSuccessReply(request) : server.newExceptionReply(request, r.getException()));
}
});
return replyFuture;
} else {
final TransferLeadershipException tle = new TransferLeadershipException(server.getMemberId() + "Failed to transfer leadership to " + request.getNewLeader() + ": a previous " + previous + " exists");
return CompletableFuture.completedFuture(server.newExceptionReply(request, tle));
}
}
scheduler.onTimeout(TimeDuration.valueOf(request.getTimeoutMs(), TimeUnit.MILLISECONDS), () -> finish(server.getState().getLeaderId(), true), LOG, () -> "Timeout check failed for append entry request: " + request);
return supplier.get().getReplyFuture();
}
Aggregations