Search in sources :

Example 1 with RaftTestUtil.waitForLeader

use of org.apache.ratis.RaftTestUtil.waitForLeader in project incubator-ratis by apache.

the class RaftAsyncTests method testStaleReadAsync.

@Test
public void testStaleReadAsync() throws Exception {
    final int numMesssages = 10;
    final CLUSTER cluster = getFactory().newCluster(NUM_SERVERS, properties);
    try (RaftClient client = cluster.createClient()) {
        cluster.start();
        RaftTestUtil.waitForLeader(cluster);
        // submit some messages
        final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
        for (int i = 0; i < numMesssages; i++) {
            final String s = "m" + i;
            LOG.info("sendAsync " + s);
            futures.add(client.sendAsync(new RaftTestUtil.SimpleMessage(s)));
        }
        Assert.assertEquals(numMesssages, futures.size());
        RaftClientReply lastWriteReply = null;
        for (CompletableFuture<RaftClientReply> f : futures) {
            lastWriteReply = f.join();
            Assert.assertTrue(lastWriteReply.isSuccess());
        }
        futures.clear();
        // Use a follower with the max commit index
        final RaftPeerId leader = lastWriteReply.getServerId();
        LOG.info("leader = " + leader);
        final Collection<CommitInfoProto> commitInfos = lastWriteReply.getCommitInfos();
        LOG.info("commitInfos = " + commitInfos);
        final CommitInfoProto followerCommitInfo = commitInfos.stream().filter(info -> !RaftPeerId.valueOf(info.getServer().getId()).equals(leader)).max(Comparator.comparing(CommitInfoProto::getCommitIndex)).get();
        final RaftPeerId follower = RaftPeerId.valueOf(followerCommitInfo.getServer().getId());
        LOG.info("max follower = " + follower);
        // test a failure case
        testFailureCaseAsync("sendStaleReadAsync(..) with a larger commit index", () -> client.sendStaleReadAsync(new RaftTestUtil.SimpleMessage("" + (numMesssages + 1)), followerCommitInfo.getCommitIndex(), follower), StateMachineException.class, IndexOutOfBoundsException.class);
        // test sendStaleReadAsync
        for (int i = 1; i < followerCommitInfo.getCommitIndex(); i++) {
            final int query = i;
            LOG.info("sendStaleReadAsync, query=" + query);
            final Message message = new RaftTestUtil.SimpleMessage("" + query);
            final CompletableFuture<RaftClientReply> readFuture = client.sendReadOnlyAsync(message);
            final CompletableFuture<RaftClientReply> staleReadFuture = client.sendStaleReadAsync(message, followerCommitInfo.getCommitIndex(), follower);
            futures.add(readFuture.thenApply(r -> getMessageContent(r)).thenCombine(staleReadFuture.thenApply(r -> getMessageContent(r)), (expected, computed) -> {
                try {
                    LOG.info("query " + query + " returns " + LogEntryProto.parseFrom(expected).getSmLogEntry().getData().toStringUtf8());
                } catch (InvalidProtocolBufferException e) {
                    throw new CompletionException(e);
                }
                Assert.assertEquals("log entry mismatch for query=" + query, expected, computed);
                return null;
            }));
        }
        JavaUtils.allOf(futures).join();
    } finally {
        cluster.shutdown();
    }
}
Also used : ByteString(org.apache.ratis.shaded.com.google.protobuf.ByteString) InvalidProtocolBufferException(org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException) RaftServerProxy(org.apache.ratis.server.impl.RaftServerProxy) org.apache.ratis.protocol(org.apache.ratis.protocol) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(org.apache.log4j.Level) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) CommitInfoProto(org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto) JavaUtils(org.apache.ratis.util.JavaUtils) LogUtils(org.apache.ratis.util.LogUtils) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) java.util.concurrent(java.util.concurrent) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) IOException(java.io.IOException) List(java.util.List) RaftProperties(org.apache.ratis.conf.RaftProperties) org.junit(org.junit) RaftClient(org.apache.ratis.client.RaftClient) Comparator(java.util.Comparator) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) TimeDuration(org.apache.ratis.util.TimeDuration) CommitInfoProto(org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException) ByteString(org.apache.ratis.shaded.com.google.protobuf.ByteString) 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 java.util.concurrent (java.util.concurrent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Level (org.apache.log4j.Level)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 org.apache.ratis.protocol (org.apache.ratis.protocol)1 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)1 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)1 RaftServerProxy (org.apache.ratis.server.impl.RaftServerProxy)1 ByteString (org.apache.ratis.shaded.com.google.protobuf.ByteString)1 InvalidProtocolBufferException (org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException)1 CommitInfoProto (org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto)1