Search in sources :

Example 1 with CommitInfoProto

use of org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto 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)

Example 2 with CommitInfoProto

use of org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto in project incubator-ratis by apache.

the class ServerInformationBaseTest method runTest.

private void runTest(int num) throws Exception {
    LOG.info("Running server info test with " + num);
    final MiniRaftCluster cluster = newCluster(num);
    cluster.start();
    // all the peers in the cluster are in the same group, get it.
    RaftGroup group = cluster.getGroup();
    List<RaftPeer> peers = cluster.getPeers();
    // is requested.
    for (RaftPeer peer : peers) {
        try (final RaftClient client = cluster.createClient(peer.getId())) {
            RaftClientReply reply = client.serverInformation(peer.getId());
            assertTrue(reply instanceof ServerInformationReply);
            ServerInformationReply info = (ServerInformationReply) reply;
            assertTrue(sameGroup(group, info.getGroup()));
        }
    }
    final int numMessages = 5;
    final long maxCommit;
    {
        // send some messages and get max commit from the last reply
        final RaftClientReply reply = sendMessages(numMessages, cluster);
        maxCommit = reply.getCommitInfos().stream().mapToLong(CommitInfoProto::getCommitIndex).max().getAsLong();
    }
    // kill a follower
    final RaftPeerId killedFollower = cluster.getFollowers().iterator().next().getId();
    cluster.killServer(killedFollower);
    {
        // send more messages and check last reply
        final RaftClientReply reply = sendMessages(numMessages, cluster);
        for (CommitInfoProto i : reply.getCommitInfos()) {
            if (RaftPeerId.valueOf(i.getServer().getId()).equals(killedFollower)) {
                Assert.assertTrue(i.getCommitIndex() <= maxCommit);
            } else {
                Assert.assertTrue(i.getCommitIndex() > maxCommit);
            }
        }
    }
    // check serverInformation
    for (RaftPeer peer : peers) {
        if (peer.getId().equals(killedFollower)) {
            continue;
        }
        try (final RaftClient client = cluster.createClient(peer.getId())) {
            RaftClientReply reply = client.serverInformation(peer.getId());
            assertTrue(reply instanceof ServerInformationReply);
            ServerInformationReply info = (ServerInformationReply) reply;
            assertTrue(sameGroup(group, info.getGroup()));
            for (CommitInfoProto i : info.getCommitInfos()) {
                if (RaftPeerId.valueOf(i.getServer().getId()).equals(killedFollower)) {
                    Assert.assertTrue(i.getCommitIndex() <= maxCommit);
                } else {
                    Assert.assertTrue(i.getCommitIndex() > maxCommit);
                }
            }
        }
    }
    cluster.shutdown();
}
Also used : MiniRaftCluster(org.apache.ratis.MiniRaftCluster) CommitInfoProto(org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftClient (org.apache.ratis.client.RaftClient)2 CommitInfoProto (org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto)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 java.util.concurrent (java.util.concurrent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Level (org.apache.log4j.Level)1 MiniRaftCluster (org.apache.ratis.MiniRaftCluster)1 RaftTestUtil.waitForLeader (org.apache.ratis.RaftTestUtil.waitForLeader)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