Search in sources :

Example 6 with LeaderNotReadyException

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

the class RaftReconfigurationBaseTest method testLeaderNotReadyException.

/**
 * Delay the commit of the leader placeholder log entry and see if the client
 * can correctly receive and handle the LeaderNotReadyException.
 */
@Test
public void testLeaderNotReadyException() throws Exception {
    LOG.info("Start testLeaderNotReadyException");
    final MiniRaftCluster cluster = newCluster(1).initServers();
    try {
        // delay 1s for each logSync call
        cluster.getServers().forEach(peer -> leaderPlaceHolderDelay.setDelayMs(peer.getId().toString(), 2000));
        cluster.start();
        AtomicBoolean caughtNotReady = new AtomicBoolean(false);
        AtomicBoolean success = new AtomicBoolean(false);
        final RaftPeerId leaderId = cluster.getPeers().iterator().next().getId();
        new Thread(() -> {
            try (final RaftClient client = cluster.createClient(leaderId)) {
                final RaftClientRpc sender = client.getClientRpc();
                final RaftClientRequest request = cluster.newRaftClientRequest(client.getId(), leaderId, new SimpleMessage("test"));
                while (!success.get()) {
                    try {
                        final RaftClientReply reply = sender.sendRequest(request);
                        success.set(reply.isSuccess());
                        if (reply.getException() != null && reply.getException() instanceof LeaderNotReadyException) {
                            caughtNotReady.set(true);
                        }
                    } catch (IOException e) {
                        LOG.info("Hit other IOException", e);
                    }
                    if (!success.get()) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException ignored) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            } catch (Exception ignored) {
                LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
            }
        }).start();
        RaftTestUtil.waitForLeader(cluster);
        for (int i = 0; !success.get() && i < 5; i++) {
            Thread.sleep(1000);
        }
        Assert.assertTrue(success.get());
        Assert.assertTrue(caughtNotReady.get());
    } finally {
        leaderPlaceHolderDelay.clear();
        cluster.shutdown();
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) IOException(java.io.IOException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) TimeoutException(java.util.concurrent.TimeoutException) ReconfigurationInProgressException(org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException) ReconfigurationTimeoutException(org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

LeaderNotReadyException (org.apache.ratis.protocol.exceptions.LeaderNotReadyException)6 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)3 NotLeaderException (org.apache.ratis.protocol.exceptions.NotLeaderException)3 IOException (java.io.IOException)2 RaftPeer (org.apache.ratis.protocol.RaftPeer)2 LeaderSteppingDownException (org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)2 Constants (alluxio.Constants)1 PropertyKey (alluxio.conf.PropertyKey)1 ServerConfiguration (alluxio.conf.ServerConfiguration)1 ExceptionMessage (alluxio.exception.ExceptionMessage)1 CancelledException (alluxio.exception.status.CancelledException)1 UnavailableException (alluxio.exception.status.UnavailableException)1 AddQuorumServerRequest (alluxio.grpc.AddQuorumServerRequest)1 GrpcService (alluxio.grpc.GrpcService)1 JournalQueryRequest (alluxio.grpc.JournalQueryRequest)1 NetAddress (alluxio.grpc.NetAddress)1 QuorumServerInfo (alluxio.grpc.QuorumServerInfo)1 QuorumServerState (alluxio.grpc.QuorumServerState)1 ServiceType (alluxio.grpc.ServiceType)1 TransferLeaderMessage (alluxio.grpc.TransferLeaderMessage)1