Search in sources :

Example 31 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId 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 32 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class GroupManagementBaseTest method testSingleGroupRestart.

@Test
public void testSingleGroupRestart() throws Exception {
    final MiniRaftCluster cluster = getCluster(0);
    LOG.info("Start testMultiGroup" + cluster.printServers());
    // Start server with null group
    final List<RaftPeerId> ids = Arrays.stream(MiniRaftCluster.generateIds(3, 0)).map(RaftPeerId::valueOf).collect(Collectors.toList());
    ids.forEach(id -> cluster.putNewServer(id, null, true));
    LOG.info("putNewServer: " + cluster.printServers());
    cluster.start();
    // Make sure that there are no leaders.
    TimeUnit.SECONDS.sleep(1);
    LOG.info("start: " + cluster.printServers());
    Assert.assertNull(cluster.getLeader());
    // Add groups
    final RaftGroup newGroup = RaftGroup.valueOf(RaftGroupId.randomId(), cluster.getPeers());
    LOG.info("add new group: " + newGroup);
    try (final RaftClient client = cluster.createClient(newGroup)) {
        for (RaftPeer p : newGroup.getPeers()) {
            client.getGroupManagementApi(p.getId()).add(newGroup);
        }
    }
    Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster));
    TimeUnit.SECONDS.sleep(1);
    // restart the servers with null group
    LOG.info("restart servers");
    for (RaftPeer p : newGroup.getPeers()) {
        cluster.restartServer(p.getId(), null, false);
    }
    // the servers should retrieve the conf from the log.
    Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster));
    cluster.shutdown();
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 33 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId 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 34 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class GroupManagementBaseTest method runMultiGroupTest.

public static <T extends Throwable> void runMultiGroupTest(MiniRaftCluster cluster, int[] idIndex, int chosen, CheckedBiConsumer<MiniRaftCluster, RaftGroup, T> checker) throws IOException, InterruptedException, T {
    if (chosen < 0) {
        chosen = ThreadLocalRandom.current().nextInt(idIndex.length);
    }
    final String type = JavaUtils.getClassSimpleName(cluster.getClass()) + Arrays.toString(idIndex) + "chosen=" + chosen;
    LOG.info("\n\nrunMultiGroupTest with " + type + ": " + cluster.printServers());
    // Start server with an empty conf
    final RaftGroup emptyGroup = RaftGroup.valueOf(cluster.getGroupId());
    final List<RaftPeerId> ids = Arrays.stream(MiniRaftCluster.generateIds(idIndex[idIndex.length - 1], 0)).map(RaftPeerId::valueOf).collect(Collectors.toList());
    LOG.info("ids: " + ids);
    ids.forEach(id -> cluster.putNewServer(id, emptyGroup, true));
    LOG.info("putNewServer: " + cluster.printServers());
    TimeUnit.SECONDS.sleep(1);
    cluster.start();
    // Make sure that there are no leaders.
    TimeUnit.SECONDS.sleep(1);
    LOG.info("start: " + cluster.printServers());
    Assert.assertNull(cluster.getLeader());
    // Reinitialize servers to three groups
    final List<RaftPeer> allPeers = cluster.getPeers();
    Collections.sort(allPeers, Comparator.comparing(p -> p.getId().toString()));
    final RaftGroup[] groups = new RaftGroup[idIndex.length];
    for (int i = 0; i < idIndex.length; i++) {
        final RaftGroupId gid = RaftGroupId.randomId();
        final int previous = i == 0 ? 0 : idIndex[i - 1];
        final RaftPeer[] peers = allPeers.subList(previous, idIndex[i]).toArray(RaftPeer.emptyArray());
        groups[i] = RaftGroup.valueOf(gid, peers);
        LOG.info(i + ") starting " + groups[i]);
        for (RaftPeer p : peers) {
            try (final RaftClient client = cluster.createClient(p.getId(), emptyGroup)) {
                client.getGroupManagementApi(p.getId()).add(groups[i]);
            }
        }
        Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster, gid));
        checker.accept(cluster, groups[i]);
    }
    printThreadCount(type, "start groups");
    LOG.info("start groups: " + cluster.printServers());
    // randomly remove two of the groups
    LOG.info("chosen = " + chosen + ", " + groups[chosen]);
    for (int i = 0; i < groups.length; i++) {
        if (i != chosen) {
            final RaftGroup g = groups[i];
            LOG.info(i + ") close " + cluster.printServers(g.getGroupId()));
            for (RaftPeer p : g.getPeers()) {
                final RaftServer.Division d = cluster.getDivision(p.getId(), g.getGroupId());
                final File root = d.getRaftStorage().getStorageDir().getRoot();
                Assert.assertTrue(root.exists());
                Assert.assertTrue(root.isDirectory());
                final RaftClientReply r;
                try (final RaftClient client = cluster.createClient(p.getId(), g)) {
                    r = client.getGroupManagementApi(p.getId()).remove(g.getGroupId(), true, false);
                }
                Assert.assertTrue(r.isSuccess());
                Assert.assertFalse(root.exists());
            }
        }
    }
    printThreadCount(type, "close groups");
    LOG.info("close groups: " + cluster.printServers());
    // update chosen group to use all the peers
    final RaftGroup newGroup = RaftGroup.valueOf(groups[chosen].getGroupId());
    for (int i = 0; i < groups.length; i++) {
        if (i != chosen) {
            LOG.info(i + ") groupAdd: " + cluster.printServers(groups[i].getGroupId()));
            for (RaftPeer p : groups[i].getPeers()) {
                try (final RaftClient client = cluster.createClient(p.getId(), groups[i])) {
                    client.getGroupManagementApi(p.getId()).add(newGroup);
                }
            }
        }
    }
    LOG.info(chosen + ") setConfiguration: " + cluster.printServers(groups[chosen].getGroupId()));
    try (final RaftClient client = cluster.createClient(groups[chosen])) {
        client.admin().setConfiguration(allPeers.toArray(RaftPeer.emptyArray()));
    }
    Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster));
    checker.accept(cluster, groups[chosen]);
    LOG.info("update groups: " + cluster.printServers());
    printThreadCount(type, "update groups");
    cluster.shutdown();
    printThreadCount(type, "shutdown");
}
Also used : Arrays(java.util.Arrays) RaftGroup(org.apache.ratis.protocol.RaftGroup) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Log4jUtils(org.apache.ratis.util.Log4jUtils) GroupManagementApi(org.apache.ratis.client.api.GroupManagementApi) Level(org.apache.log4j.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CheckedBiConsumer(org.apache.ratis.util.function.CheckedBiConsumer) JavaUtils(org.apache.ratis.util.JavaUtils) Logger(org.slf4j.Logger) Files(java.nio.file.Files) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Test(org.junit.Test) IOException(java.io.IOException) BaseTest(org.apache.ratis.BaseTest) Collectors(java.util.stream.Collectors) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) File(java.io.File) RaftTestUtil(org.apache.ratis.RaftTestUtil) FileUtils(org.apache.ratis.util.FileUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) TimeDuration(org.apache.ratis.util.TimeDuration) RaftServer(org.apache.ratis.server.RaftServer) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient)

Example 35 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method runTestOverlappedSetConfRequests.

void runTestOverlappedSetConfRequests(CLUSTER cluster) throws Exception {
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        RaftPeer[] newPeers = cluster.addNewPeers(2, true).allPeersInNewConf;
        // delay every peer's logSync so that the setConf request is delayed
        cluster.getPeers().forEach(peer -> logSyncDelay.setDelayMs(peer.getId().toString(), 1000));
        final CountDownLatch latch = new CountDownLatch(1);
        final RaftPeer[] peersInRequest2 = cluster.getPeers().toArray(new RaftPeer[0]);
        AtomicBoolean caughtException = new AtomicBoolean(false);
        new Thread(() -> {
            try (final RaftClient client2 = cluster.createClient(leaderId)) {
                latch.await();
                LOG.info("client2 starts to change conf");
                final RaftClientRpc sender2 = client2.getClientRpc();
                sender2.sendRequest(cluster.newSetConfigurationRequest(client2.getId(), leaderId, peersInRequest2));
            } catch (ReconfigurationInProgressException e) {
                caughtException.set(true);
            } catch (Exception e) {
                LOG.warn("Got unexpected exception when client2 changes conf", e);
            }
        }).start();
        AtomicBoolean confChanged = new AtomicBoolean(false);
        new Thread(() -> {
            try (final RaftClient client1 = cluster.createClient(leaderId)) {
                LOG.info("client1 starts to change conf");
                confChanged.set(client1.admin().setConfiguration(newPeers).isSuccess());
            } catch (IOException e) {
                LOG.warn("Got unexpected exception when client1 changes conf", e);
            }
        }).start();
        Thread.sleep(100);
        latch.countDown();
        for (int i = 0; i < 10 && !confChanged.get(); i++) {
            Thread.sleep(1000);
        }
        Assert.assertTrue(confChanged.get());
        Assert.assertTrue(caughtException.get());
    } finally {
        logSyncDelay.clear();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) CountDownLatch(java.util.concurrent.CountDownLatch) ReconfigurationInProgressException(org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException) RaftClient(org.apache.ratis.client.RaftClient) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) TimeoutException(java.util.concurrent.TimeoutException) ReconfigurationInProgressException(org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException) ReconfigurationTimeoutException(org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException) IOException(java.io.IOException) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)101 RaftClient (org.apache.ratis.client.RaftClient)58 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)45 IOException (java.io.IOException)36 Test (org.junit.Test)32 RaftPeer (org.apache.ratis.protocol.RaftPeer)31 RaftServer (org.apache.ratis.server.RaftServer)31 BaseTest (org.apache.ratis.BaseTest)21 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)20 CompletableFuture (java.util.concurrent.CompletableFuture)18 RaftProperties (org.apache.ratis.conf.RaftProperties)18 File (java.io.File)17 ArrayList (java.util.ArrayList)15 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)15 RaftGroup (org.apache.ratis.protocol.RaftGroup)14 TimeDuration (org.apache.ratis.util.TimeDuration)14 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Collectors (java.util.stream.Collectors)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)11