Search in sources :

Example 1 with LeaderSteppingDownException

use of org.apache.ratis.protocol.exceptions.LeaderSteppingDownException 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;
}
Also used : NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) CacheEntry(org.apache.ratis.server.impl.RetryCacheImpl.CacheEntry) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)

Example 2 with LeaderSteppingDownException

use of org.apache.ratis.protocol.exceptions.LeaderSteppingDownException 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 3 with LeaderSteppingDownException

use of org.apache.ratis.protocol.exceptions.LeaderSteppingDownException in project incubator-ratis by apache.

the class ClientProtoUtils method toRaftClientReply.

static RaftClientReply toRaftClientReply(RaftClientReplyProto replyProto) {
    final RaftRpcReplyProto rp = replyProto.getRpcReply();
    final RaftGroupMemberId serverMemberId = ProtoUtils.toRaftGroupMemberId(rp.getReplyId(), rp.getRaftGroupId());
    final RaftException e;
    if (replyProto.getExceptionDetailsCase().equals(NOTLEADEREXCEPTION)) {
        NotLeaderExceptionProto nleProto = replyProto.getNotLeaderException();
        final RaftPeer suggestedLeader = nleProto.hasSuggestedLeader() ? ProtoUtils.toRaftPeer(nleProto.getSuggestedLeader()) : null;
        final List<RaftPeer> peers = ProtoUtils.toRaftPeers(nleProto.getPeersInConfList());
        e = new NotLeaderException(serverMemberId, suggestedLeader, peers);
    } else if (replyProto.getExceptionDetailsCase() == NOTREPLICATEDEXCEPTION) {
        final NotReplicatedExceptionProto nre = replyProto.getNotReplicatedException();
        e = new NotReplicatedException(nre.getCallId(), nre.getReplication(), nre.getLogIndex());
    } else if (replyProto.getExceptionDetailsCase().equals(STATEMACHINEEXCEPTION)) {
        e = toStateMachineException(serverMemberId, replyProto.getStateMachineException());
    } else if (replyProto.getExceptionDetailsCase().equals(DATASTREAMEXCEPTION)) {
        e = ProtoUtils.toThrowable(replyProto.getDataStreamException(), DataStreamException.class);
    } else if (replyProto.getExceptionDetailsCase().equals(LEADERNOTREADYEXCEPTION)) {
        LeaderNotReadyExceptionProto lnreProto = replyProto.getLeaderNotReadyException();
        e = new LeaderNotReadyException(ProtoUtils.toRaftGroupMemberId(lnreProto.getServerId()));
    } else if (replyProto.getExceptionDetailsCase().equals(ALREADYCLOSEDEXCEPTION)) {
        e = toAlreadyClosedException(replyProto.getAlreadyClosedException());
    } else if (replyProto.getExceptionDetailsCase().equals(LEADERSTEPPINGDOWNEXCEPTION)) {
        e = ProtoUtils.toThrowable(replyProto.getLeaderSteppingDownException(), LeaderSteppingDownException.class);
    } else if (replyProto.getExceptionDetailsCase().equals(TRANSFERLEADERSHIPEXCEPTION)) {
        e = ProtoUtils.toThrowable(replyProto.getTransferLeadershipException(), TransferLeadershipException.class);
    } else {
        e = null;
    }
    return RaftClientReply.newBuilder().setClientId(ClientId.valueOf(rp.getRequestorId())).setServerId(serverMemberId).setCallId(rp.getCallId()).setSuccess(rp.getSuccess()).setMessage(toMessage(replyProto.getMessage())).setException(e).setLogIndex(replyProto.getLogIndex()).setCommitInfos(replyProto.getCommitInfosList()).build();
}
Also used : RaftException(org.apache.ratis.protocol.exceptions.RaftException) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)

Example 4 with LeaderSteppingDownException

use of org.apache.ratis.protocol.exceptions.LeaderSteppingDownException 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)

Aggregations

LeaderSteppingDownException (org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)4 IOException (java.io.IOException)2 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)2 GroupMismatchException (org.apache.ratis.protocol.exceptions.GroupMismatchException)2 LeaderNotReadyException (org.apache.ratis.protocol.exceptions.LeaderNotReadyException)2 NotLeaderException (org.apache.ratis.protocol.exceptions.NotLeaderException)2 TransferLeadershipException (org.apache.ratis.protocol.exceptions.TransferLeadershipException)2 InterruptedIOException (java.io.InterruptedIOException)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 NotReplicatedException (org.apache.ratis.protocol.exceptions.NotReplicatedException)1 RaftException (org.apache.ratis.protocol.exceptions.RaftException)1 StateMachineException (org.apache.ratis.protocol.exceptions.StateMachineException)1 RetryPolicy (org.apache.ratis.retry.RetryPolicy)1