Search in sources :

Example 1 with GroupManagementApi

use of org.apache.ratis.client.api.GroupManagementApi in project incubator-ratis by apache.

the class GroupManagementBaseTest method testGroupRemoveWhenRename.

@Test
public void testGroupRemoveWhenRename() throws Exception {
    final MiniRaftCluster cluster1 = getCluster(1);
    RaftServerConfigKeys.setRemovedGroupsDir(cluster1.getProperties(), Files.createTempDirectory("groups").toFile());
    final MiniRaftCluster cluster2 = getCluster(3);
    cluster1.start();
    final RaftPeer peer1 = cluster1.getPeers().get(0);
    final RaftPeerId peerId1 = peer1.getId();
    final RaftGroup group1 = RaftGroup.valueOf(cluster1.getGroupId(), peer1);
    final RaftGroup group2 = RaftGroup.valueOf(cluster2.getGroupId(), peer1);
    try (final RaftClient client = cluster1.createClient()) {
        Assert.assertEquals(group1, cluster1.getDivision(peerId1).getGroup());
        try {
            // Group2 is added to one of the peers in Group1
            final GroupManagementApi api1 = client.getGroupManagementApi(peerId1);
            api1.add(group2);
            List<RaftGroupId> groupIds1 = cluster1.getServer(peerId1).getGroupIds();
            Assert.assertEquals(groupIds1.size(), 2);
            // Group2 is renamed from the peer1 of Group1
            api1.remove(group2.getGroupId(), false, true);
            groupIds1 = cluster1.getServer(peerId1).getGroupIds();
            Assert.assertEquals(groupIds1.size(), 1);
            cluster1.restart(false);
            List<RaftGroupId> groupIdsAfterRestart = cluster1.getServer(peerId1).getGroupIds();
            Assert.assertEquals(groupIds1.size(), groupIdsAfterRestart.size());
            File renamedGroup = new File(RaftServerConfigKeys.removedGroupsDir(cluster1.getProperties()), group2.getGroupId().getUuid().toString());
            Assert.assertTrue(renamedGroup.isDirectory());
        } catch (IOException ex) {
            Assert.fail();
        } finally {
            cluster1.shutdown();
            // Clean up
            FileUtils.deleteFully(RaftServerConfigKeys.removedGroupsDir(cluster1.getProperties()));
        }
    }
}
Also used : GroupManagementApi(org.apache.ratis.client.api.GroupManagementApi) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroup(org.apache.ratis.protocol.RaftGroup) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 2 with GroupManagementApi

use of org.apache.ratis.client.api.GroupManagementApi in project incubator-ratis by apache.

the class GroupManagementBaseTest method testGroupRemoveWhenDelete.

@Test
public void testGroupRemoveWhenDelete() throws Exception {
    final MiniRaftCluster cluster1 = getCluster(1);
    RaftServerConfigKeys.setRemovedGroupsDir(cluster1.getProperties(), Files.createTempDirectory("groups").toFile());
    final MiniRaftCluster cluster2 = getCluster(3);
    cluster1.start();
    final RaftPeer peer1 = cluster1.getPeers().get(0);
    final RaftPeerId peerId1 = peer1.getId();
    final RaftGroup group1 = RaftGroup.valueOf(cluster1.getGroupId(), peer1);
    final RaftGroup group2 = RaftGroup.valueOf(cluster2.getGroupId(), peer1);
    try (final RaftClient client = cluster1.createClient()) {
        Assert.assertEquals(group1, cluster1.getDivision(peerId1).getGroup());
        try {
            // Group2 is added again to one of the peers in Group1
            final GroupManagementApi api1 = client.getGroupManagementApi(peerId1);
            api1.add(group2);
            List<RaftGroupId> groupIds1 = cluster1.getServer(peerId1).getGroupIds();
            Assert.assertEquals(groupIds1.size(), 2);
            // Group2 is deleted from the peer1 of Group1
            api1.remove(group2.getGroupId(), true, false);
            groupIds1 = cluster1.getServer(peerId1).getGroupIds();
            Assert.assertEquals(groupIds1.size(), 1);
            cluster1.restart(false);
            List<RaftGroupId> groupIdsAfterRestart = cluster1.getServer(peerId1).getGroupIds();
            Assert.assertEquals(groupIds1.size(), groupIdsAfterRestart.size());
        } catch (IOException ex) {
            Assert.fail();
        } finally {
            cluster1.shutdown();
            FileUtils.deleteFully(RaftServerConfigKeys.removedGroupsDir(cluster1.getProperties()));
        }
    }
}
Also used : GroupManagementApi(org.apache.ratis.client.api.GroupManagementApi) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroup(org.apache.ratis.protocol.RaftGroup) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 3 with GroupManagementApi

use of org.apache.ratis.client.api.GroupManagementApi 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

BaseTest (org.apache.ratis.BaseTest)3 RaftClient (org.apache.ratis.client.RaftClient)3 GroupManagementApi (org.apache.ratis.client.api.GroupManagementApi)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 RaftGroup (org.apache.ratis.protocol.RaftGroup)2 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)2 RaftPeer (org.apache.ratis.protocol.RaftPeer)2 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)2 File (java.io.File)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Level (org.apache.log4j.Level)1 CommitInfoProto (org.apache.ratis.proto.RaftProtos.CommitInfoProto)1 org.apache.ratis.protocol (org.apache.ratis.protocol)1 RaftServer (org.apache.ratis.server.RaftServer)1 Log4jUtils (org.apache.ratis.util.Log4jUtils)1 Assert (org.junit.Assert)1