Search in sources :

Example 1 with RaftRetryFailureException

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

the class TestExceptionDependentRetry method runTestExceptionRetryAttempts.

void runTestExceptionRetryAttempts(MiniRaftClusterWithGrpc cluster) throws Exception {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final ExceptionDependentRetry policy = ExceptionDependentRetry.newBuilder().setExceptionToPolicy(TimeoutIOException.class, MultipleLinearRandomRetry.parseCommaSeparated("1ms, 5")).setDefaultPolicy(RetryPolicies.retryForeverNoSleep()).build();
    // create a client with the exception dependent policy
    try (final RaftClient client = cluster.createClient(policy)) {
        client.async().send(new RaftTestUtil.SimpleMessage("1")).get();
    }
    try (final RaftClient client = cluster.createClient(policy)) {
        SimpleStateMachine4Testing.get(leader).blockWriteStateMachineData();
        client.async().send(new RaftTestUtil.SimpleMessage("2")).get();
        Assert.fail("Test should have failed.");
    } catch (ExecutionException e) {
        RaftRetryFailureException rrfe = (RaftRetryFailureException) e.getCause();
        Assert.assertEquals(16, rrfe.getAttemptCount());
    } finally {
        SimpleStateMachine4Testing.get(leader).unblockWriteStateMachineData();
        cluster.shutdown();
    }
}
Also used : RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) RaftServer(org.apache.ratis.server.RaftServer) ExecutionException(java.util.concurrent.ExecutionException) RaftClient(org.apache.ratis.client.RaftClient)

Example 2 with RaftRetryFailureException

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

the class RaftAsyncTests method runTestRequestAsyncWithRetryFailure.

void runTestRequestAsyncWithRetryFailure(boolean initialMessages, CLUSTER cluster) throws Exception {
    final TimeDuration sleepTime = HUNDRED_MILLIS;
    final RetryLimited retryPolicy = RetryPolicies.retryUpToMaximumCountWithFixedSleep(10, sleepTime);
    try (final RaftClient client = cluster.createClient(null, retryPolicy)) {
        RaftPeerId leader = null;
        if (initialMessages) {
            // cluster is already started, send a few success messages
            leader = RaftTestUtil.waitForLeader(cluster).getId();
            final SimpleMessage[] messages = SimpleMessage.create(10, "initial-");
            final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
            for (int i = 0; i < messages.length; i++) {
                replies.add(client.async().send(messages[i]));
            }
            for (int i = 0; i < messages.length; i++) {
                RaftTestUtil.assertSuccessReply(replies.get(i));
            }
            // kill the only server
            cluster.killServer(leader);
        }
        // now, either the cluster is not yet started or the server is killed.
        final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
        {
            final SimpleMessage[] messages = SimpleMessage.create(10);
            int i = 0;
            // send half of the calls without starting the cluster
            for (; i < messages.length / 2; i++) {
                replies.add(client.async().send(messages[i]));
            }
            // sleep most of the retry time
            sleepTime.apply(t -> t * (retryPolicy.getMaxAttempts() - 1)).sleep();
            // send another half of the calls without starting the cluster
            for (; i < messages.length; i++) {
                replies.add(client.async().send(messages[i]));
            }
            Assert.assertEquals(messages.length, replies.size());
        }
        // sleep again so that the first half calls will fail retries.
        // the second half still have retry time remaining.
        sleepTime.apply(t -> t * 2).sleep();
        if (leader != null) {
            cluster.restartServer(leader, false);
        } else {
            cluster.start();
        }
        // all the calls should fail for ordering guarantee
        for (int i = 0; i < replies.size(); i++) {
            final CheckedRunnable<Exception> getReply = replies.get(i)::get;
            final String name = "retry-failure-" + i;
            if (i == 0) {
                final Throwable t = testFailureCase(name, getReply, ExecutionException.class, RaftRetryFailureException.class);
                assertRaftRetryFailureException((RaftRetryFailureException) t.getCause(), retryPolicy, name);
            } else {
                testFailureCase(name, getReply, ExecutionException.class, AlreadyClosedException.class, RaftRetryFailureException.class);
            }
        }
        testFailureCaseAsync("last-request", () -> client.async().send(new SimpleMessage("last")), AlreadyClosedException.class, RaftRetryFailureException.class);
    }
}
Also used : RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicies(org.apache.ratis.retry.RetryPolicies) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) ArrayList(java.util.ArrayList) Log4jUtils(org.apache.ratis.util.Log4jUtils) Message(org.apache.ratis.protocol.Message) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) CheckedRunnable(org.apache.ratis.util.function.CheckedRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) Level(org.apache.log4j.Level) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) StreamSupport(java.util.stream.StreamSupport) JavaUtils(org.apache.ratis.util.JavaUtils) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) StateMachine(org.apache.ratis.statemachine.StateMachine) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RetryLimited(org.apache.ratis.retry.RetryPolicies.RetryLimited) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) Comparator(java.util.Comparator) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) DelayLocalExecutionInjection(org.apache.ratis.server.impl.DelayLocalExecutionInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) TimeoutException(java.util.concurrent.TimeoutException) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) TimeDuration(org.apache.ratis.util.TimeDuration) RetryLimited(org.apache.ratis.retry.RetryPolicies.RetryLimited) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)2 RaftClient (org.apache.ratis.client.RaftClient)2 RaftRetryFailureException (org.apache.ratis.protocol.exceptions.RaftRetryFailureException)2 RaftServer (org.apache.ratis.server.RaftServer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 StreamSupport (java.util.stream.StreamSupport)1 Level (org.apache.log4j.Level)1 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)1 RaftTestUtil.waitForLeader (org.apache.ratis.RaftTestUtil.waitForLeader)1 RaftClientConfigKeys (org.apache.ratis.client.RaftClientConfigKeys)1 RaftClientTestUtil (org.apache.ratis.client.impl.RaftClientTestUtil)1