Search in sources :

Example 6 with CommitInfoProto

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

the class GroupInfoBaseTest method runTest.

private void runTest(CLUSTER cluster) throws Exception {
    // all the peers in the cluster are in the same group, get it.
    RaftGroup group = cluster.getGroup();
    List<RaftPeer> peers = cluster.getPeers();
    // Multi-raft with the second group
    RaftGroup group2 = RaftGroup.valueOf(RaftGroupId.randomId(), peers);
    for (RaftPeer peer : peers) {
        try (final RaftClient client = cluster.createClient(peer.getId())) {
            client.getGroupManagementApi(peer.getId()).add(group2);
        }
    }
    // for each of them.
    for (RaftPeer peer : peers) {
        try (final RaftClient client = cluster.createClient(peer.getId())) {
            final GroupManagementApi api = client.getGroupManagementApi(peer.getId());
            final GroupListReply info = api.list();
            List<RaftGroupId> groupList = info.getGroupIds().stream().filter(id -> group.getGroupId().equals(id)).collect(Collectors.toList());
            assert (groupList.size() == 1);
            final GroupInfoReply gi = api.info(groupList.get(0));
            assert (sameGroup(group, gi.getGroup()));
            groupList = info.getGroupIds().stream().filter(id -> group2.getGroupId().equals(id)).collect(Collectors.toList());
            assert (groupList.size() == 1);
            final GroupInfoReply gi2 = api.info(groupList.get(0));
            assert (sameGroup(group2, gi2.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);
            }
        }
    }
    // check getGroupList
    for (RaftPeer peer : peers) {
        if (peer.getId().equals(killedFollower)) {
            continue;
        }
        try (final RaftClient client = cluster.createClient(peer.getId())) {
            final GroupListReply info = client.getGroupManagementApi(peer.getId()).list();
            Assert.assertEquals(1, info.getGroupIds().stream().filter(id -> group.getGroupId().equals(id)).count());
            for (CommitInfoProto i : info.getCommitInfos()) {
                if (RaftPeerId.valueOf(i.getServer().getId()).equals(killedFollower)) {
                    Assert.assertTrue(i.getCommitIndex() <= maxCommit);
                } else {
                    Assert.assertTrue(i.getCommitIndex() > maxCommit);
                }
            }
        }
    }
}
Also used : Collection(java.util.Collection) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest) org.apache.ratis.protocol(org.apache.ratis.protocol) Collectors(java.util.stream.Collectors) Log4jUtils(org.apache.ratis.util.Log4jUtils) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) GroupManagementApi(org.apache.ratis.client.api.GroupManagementApi) Level(org.apache.log4j.Level) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) GroupManagementApi(org.apache.ratis.client.api.GroupManagementApi) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

CommitInfoProto (org.apache.ratis.proto.RaftProtos.CommitInfoProto)6 Collection (java.util.Collection)4 List (java.util.List)4 Level (org.apache.log4j.Level)4 RaftClient (org.apache.ratis.client.RaftClient)4 RaftServer (org.apache.ratis.server.RaftServer)4 Log4jUtils (org.apache.ratis.util.Log4jUtils)4 Assert (org.junit.Assert)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3 RaftClientConfigKeys (org.apache.ratis.client.RaftClientConfigKeys)3 RaftProperties (org.apache.ratis.conf.RaftProperties)3 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)3 AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)3 RaftRetryFailureException (org.apache.ratis.protocol.exceptions.RaftRetryFailureException)3 RetryPolicies (org.apache.ratis.retry.RetryPolicies)3