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()));
}
}
}
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()));
}
}
}
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);
}
}
}
}
}
Aggregations