Search in sources :

Example 1 with TransferLeadershipException

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);
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TimeDuration(org.apache.ratis.util.TimeDuration) RetryPolicy(org.apache.ratis.retry.RetryPolicy)

Example 2 with TransferLeadershipException

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();
    }
}
Also used : TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 3 with TransferLeadershipException

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();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)3 TransferLeadershipException (org.apache.ratis.protocol.exceptions.TransferLeadershipException)3 IOException (java.io.IOException)2 LeaderSteppingDownException (org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)2 InterruptedIOException (java.io.InterruptedIOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 BaseTest (org.apache.ratis.BaseTest)1 RaftTestUtil (org.apache.ratis.RaftTestUtil)1 RaftClient (org.apache.ratis.client.RaftClient)1 ClientRetryEvent (org.apache.ratis.client.retry.ClientRetryEvent)1 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)1 RaftPeer (org.apache.ratis.protocol.RaftPeer)1 AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)1 AlreadyExistsException (org.apache.ratis.protocol.exceptions.AlreadyExistsException)1 GroupMismatchException (org.apache.ratis.protocol.exceptions.GroupMismatchException)1 StateMachineException (org.apache.ratis.protocol.exceptions.StateMachineException)1 RetryPolicy (org.apache.ratis.retry.RetryPolicy)1 RaftServer (org.apache.ratis.server.RaftServer)1 TimeDuration (org.apache.ratis.util.TimeDuration)1 Test (org.junit.Test)1