Search in sources :

Example 1 with RetryLimited

use of org.apache.ratis.retry.RetryPolicies.RetryLimited 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

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 ExecutionException (java.util.concurrent.ExecutionException)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 RaftClient (org.apache.ratis.client.RaftClient)1 RaftClientConfigKeys (org.apache.ratis.client.RaftClientConfigKeys)1 RaftClientTestUtil (org.apache.ratis.client.impl.RaftClientTestUtil)1 RaftProperties (org.apache.ratis.conf.RaftProperties)1 CommitInfoProto (org.apache.ratis.proto.RaftProtos.CommitInfoProto)1