Search in sources :

Example 81 with RaftClientReply

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

the class RaftReconfigurationBaseTest method runTestReconfTwice.

void runTestReconfTwice(CLUSTER cluster) throws Exception {
    final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        // submit some msgs before reconf
        for (int i = 0; i < STAGING_CATCHUP_GAP * 2; i++) {
            RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        final AtomicBoolean reconf1 = new AtomicBoolean(false);
        final AtomicBoolean reconf2 = new AtomicBoolean(false);
        final AtomicReference<RaftPeer[]> finalPeers = new AtomicReference<>(null);
        final AtomicReference<RaftPeer[]> deadPeers = new AtomicReference<>(null);
        CountDownLatch latch = new CountDownLatch(1);
        Thread clientThread = new Thread(() -> {
            try {
                PeerChanges c1 = cluster.addNewPeers(2, true);
                LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
                RaftClientReply reply = client.admin().setConfiguration(c1.allPeersInNewConf);
                reconf1.set(reply.isSuccess());
                PeerChanges c2 = cluster.removePeers(2, true, asList(c1.newPeers));
                finalPeers.set(c2.allPeersInNewConf);
                deadPeers.set(c2.removedPeers);
                LOG.info("Start changing the configuration again: {}", asList(c2.allPeersInNewConf));
                reply = client.admin().setConfiguration(c2.allPeersInNewConf);
                reconf2.set(reply.isSuccess());
                latch.countDown();
            } catch (Exception ignored) {
                LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
            }
        });
        clientThread.start();
        latch.await();
        Assert.assertTrue(reconf1.get());
        Assert.assertTrue(reconf2.get());
        waitAndCheckNewConf(cluster, finalPeers.get(), 2, null);
        final RaftPeerId leader2 = RaftTestUtil.waitForLeader(cluster).getId();
        // check configuration manager's internal state
        // each reconf will generate two configurations: (old, new) and (new)
        cluster.getServerAliveStream().forEach(server -> {
            final ConfigurationManager confManager = RaftServerTestUtil.getConfigurationManager(server);
            // each reconf will generate two configurations: (old, new) and (new)
            // each leader change generates one configuration.
            // expectedConf = 1 (init) + 2*2 (two conf changes) + #leader
            final int expectedConf = leader2.equals(leaderId) ? 6 : 7;
            Assert.assertEquals(server.getId() + ": " + confManager, expectedConf, confManager.numOfConf());
        });
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) PeerChanges(org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 82 with RaftClientReply

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

Example 83 with RaftClientReply

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

the class RaftAsyncTests method runTestNoRetryWaitOnNotLeaderException.

private void runTestNoRetryWaitOnNotLeaderException(MiniRaftCluster cluster) throws Exception {
    final RaftServer.Division leader = waitForLeader(cluster);
    final List<RaftServer.Division> followers = cluster.getFollowers();
    Assert.assertNotNull(followers);
    Assert.assertEquals(2, followers.size());
    Assert.assertNotSame(leader, followers.get(0));
    Assert.assertNotSame(leader, followers.get(1));
    // send a message to make sure that the leader is ready
    try (final RaftClient client = cluster.createClient(leader.getId())) {
        final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("first"));
        FIVE_SECONDS.apply(f::get);
    }
    // if sleep interval defined by retry policy is used the test will timeout
    final RetryPolicy r = event -> () -> TimeDuration.valueOf(60, TimeUnit.SECONDS);
    try (final RaftClient client = cluster.createClient(followers.get(0).getId(), cluster.getGroup(), r)) {
        final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("abc"));
        FIVE_SECONDS.apply(f::get);
    } catch (TimeoutException e) {
        throw new AssertionError("Failed to get async result", e);
    }
}
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) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftClient(org.apache.ratis.client.RaftClient) TimeoutException(java.util.concurrent.TimeoutException)

Example 84 with RaftClientReply

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

the class RequestLimitAsyncBaseTest method runTestWriteElementLimit.

void runTestWriteElementLimit(CLUSTER cluster) throws Exception {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    try (RaftClient c1 = cluster.createClient(leader.getId())) {
        {
            // send first message to make sure the cluster is working
            final SimpleMessage message = new SimpleMessage("first");
            final CompletableFuture<RaftClientReply> future = c1.async().send(message);
            final RaftClientReply reply = getWithDefaultTimeout(future);
            Assert.assertTrue(reply.isSuccess());
        }
        // collecting futures returned from StateMachine.applyTransaction
        final BlockingQueue<Runnable> toBeCompleted = SimpleStateMachine4Testing.get(leader).collecting().enable(SimpleStateMachine4Testing.Collecting.Type.APPLY_TRANSACTION);
        // send write requests up to the limit
        final List<CompletableFuture<RaftClientReply>> writeFutures = new ArrayList<>();
        for (int i = 0; i < writeElementLimit; i++) {
            final SimpleMessage message = new SimpleMessage("m" + i);
            writeFutures.add(c1.async().send(message));
        }
        // send watch requests up to the limit
        // watch a large index so that it won't complete
        final long watchBase = 1000;
        for (int i = 0; i < watchElementLimit; i++) {
            c1.async().watch(watchBase + i, ReplicationLevel.ALL);
        }
        // sleep to make sure that all the request were sent
        HUNDRED_MILLIS.sleep();
        try (RaftClient c2 = cluster.createClient(leader.getId(), RetryPolicies.noRetry())) {
            // more write requests should get ResourceUnavailableException
            final SimpleMessage message = new SimpleMessage("err");
            testFailureCase("send should fail", () -> c2.io().send(message), ResourceUnavailableException.class);
            testFailureCase("sendAsync should fail", () -> c2.async().send(message).get(), ExecutionException.class, ResourceUnavailableException.class);
            // more watch requests should get ResourceUnavailableException
            final long watchIndex = watchBase + watchElementLimit;
            testFailureCase("sendWatch should fail", () -> c2.io().watch(watchIndex, ReplicationLevel.ALL), ResourceUnavailableException.class);
            testFailureCase("sendWatchAsync should fail", () -> c2.async().watch(watchIndex, ReplicationLevel.ALL).get(), ExecutionException.class, ResourceUnavailableException.class);
        }
        // complete futures from applyTransaction
        toBeCompleted.forEach(Runnable::run);
        // check replies
        for (CompletableFuture<RaftClientReply> f : writeFutures) {
            final RaftClientReply reply = getWithDefaultTimeout(f);
            Assert.assertTrue(reply.isSuccess());
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 85 with RaftClientReply

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

the class WatchRequestTests method runTestWatchRequestAsyncChangeLeader.

static void runTestWatchRequestAsyncChangeLeader(TestParameters p) throws Exception {
    final Logger LOG = p.log;
    final MiniRaftCluster cluster = p.cluster;
    final int numMessages = p.numMessages;
    // blockFlushStateMachineData a follower so that no transaction can be ALL_COMMITTED
    final List<RaftServer.Division> followers = cluster.getFollowers();
    final RaftServer.Division blockedFollower = followers.get(ThreadLocalRandom.current().nextInt(followers.size()));
    LOG.info("block follower {}", blockedFollower.getId());
    SimpleStateMachine4Testing.get(blockedFollower).blockFlushStateMachineData();
    final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
    final List<CompletableFuture<WatchReplies>> watches = new ArrayList<>();
    p.sendRequests(replies, watches);
    Assert.assertEquals(numMessages, replies.size());
    Assert.assertEquals(numMessages, watches.size());
    // since only one follower is blocked commit, requests can be committed MAJORITY and ALL but not ALL_COMMITTED.
    checkMajority(replies, watches, LOG);
    TimeUnit.SECONDS.sleep(1);
    assertNotDone(watches.stream().map(CompletableFuture::join).map(w -> w.allCommitted));
    // Now change leader
    RaftTestUtil.changeLeader(cluster, cluster.getLeader().getId());
    // unblock follower so that the transaction can be replicated and committed to all.
    SimpleStateMachine4Testing.get(blockedFollower).unblockFlushStateMachineData();
    LOG.info("unblock follower {}", blockedFollower.getId());
    checkAll(watches, LOG);
}
Also used : RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) CheckedSupplier(org.apache.ratis.util.function.CheckedSupplier) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicies(org.apache.ratis.retry.RetryPolicies) ArrayList(java.util.ArrayList) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) Log4jUtils(org.apache.ratis.util.Log4jUtils) ReplicationLevel(org.apache.ratis.proto.RaftProtos.ReplicationLevel) ProtoUtils(org.apache.ratis.util.ProtoUtils) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) CheckedConsumer(org.apache.ratis.util.function.CheckedConsumer) Level(org.apache.log4j.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) Logger(org.slf4j.Logger) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) CompletableFuture(java.util.concurrent.CompletableFuture) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)96 RaftClient (org.apache.ratis.client.RaftClient)71 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)43 RaftServer (org.apache.ratis.server.RaftServer)40 IOException (java.io.IOException)32 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)25 CompletableFuture (java.util.concurrent.CompletableFuture)22 RaftPeer (org.apache.ratis.protocol.RaftPeer)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 TimeDuration (org.apache.ratis.util.TimeDuration)18 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)14 RaftTestUtil (org.apache.ratis.RaftTestUtil)13 RaftProperties (org.apache.ratis.conf.RaftProperties)13 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 List (java.util.List)12 RetryPolicy (org.apache.ratis.retry.RetryPolicy)12 CompletionException (java.util.concurrent.CompletionException)11 ExecutionException (java.util.concurrent.ExecutionException)11