Search in sources :

Example 6 with PeerChanges

use of org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method runTestBootstrapReconf.

void runTestBootstrapReconf(int numNewPeer, boolean startNewPeer, CLUSTER cluster) throws Exception {
    LOG.info("Originally {} peer(s), add {} more, startNewPeer={}", cluster.getNumServers(), numNewPeer, startNewPeer);
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        // submit some msgs before reconf
        for (int i = 0; i < STAGING_CATCHUP_GAP * 2; i++) {
            RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        final PeerChanges c1 = cluster.addNewPeers(numNewPeer, startNewPeer);
        LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
        final AtomicReference<Boolean> success = new AtomicReference<>();
        Thread clientThread = new Thread(() -> {
            try {
                RaftClientReply reply = client.admin().setConfiguration(c1.allPeersInNewConf);
                success.set(reply.isSuccess());
            } catch (IOException ioe) {
                LOG.error("FAILED", ioe);
            }
        });
        clientThread.start();
        if (!startNewPeer) {
            // Make sure that set configuration is run inside the thread
            RaftTestUtil.waitFor(() -> clientThread.isAlive(), 300, 5000);
            ONE_SECOND.sleep();
            LOG.info("start new peer(s): {}", c1.newPeers);
            for (RaftPeer p : c1.newPeers) {
                cluster.restartServer(p.getId(), true);
            }
        }
        RaftTestUtil.waitFor(() -> success.get() != null && success.get(), 300, 15000);
        LOG.info(cluster.printServers());
        final RaftLog leaderLog = cluster.getLeader().getRaftLog();
        for (RaftPeer newPeer : c1.newPeers) {
            final RaftServer.Division d = cluster.getDivision(newPeer.getId());
            RaftTestUtil.waitFor(() -> leaderLog.getEntries(0, Long.MAX_VALUE).length == d.getRaftLog().getEntries(0, Long.MAX_VALUE).length, 300, 15000);
            Assert.assertArrayEquals(leaderLog.getEntries(0, Long.MAX_VALUE), d.getRaftLog().getEntries(0, Long.MAX_VALUE));
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) PeerChanges(org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 7 with PeerChanges

use of org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method runTestReconfTwice.

void runTestReconfTwice(CLUSTER cluster) throws Exception {
    final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        // submit some msgs before reconf
        for (int i = 0; i < STAGING_CATCHUP_GAP * 2; i++) {
            RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        final AtomicBoolean reconf1 = new AtomicBoolean(false);
        final AtomicBoolean reconf2 = new AtomicBoolean(false);
        final AtomicReference<RaftPeer[]> finalPeers = new AtomicReference<>(null);
        final AtomicReference<RaftPeer[]> deadPeers = new AtomicReference<>(null);
        CountDownLatch latch = new CountDownLatch(1);
        Thread clientThread = new Thread(() -> {
            try {
                PeerChanges c1 = cluster.addNewPeers(2, true);
                LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
                RaftClientReply reply = client.admin().setConfiguration(c1.allPeersInNewConf);
                reconf1.set(reply.isSuccess());
                PeerChanges c2 = cluster.removePeers(2, true, asList(c1.newPeers));
                finalPeers.set(c2.allPeersInNewConf);
                deadPeers.set(c2.removedPeers);
                LOG.info("Start changing the configuration again: {}", asList(c2.allPeersInNewConf));
                reply = client.admin().setConfiguration(c2.allPeersInNewConf);
                reconf2.set(reply.isSuccess());
                latch.countDown();
            } catch (Exception ignored) {
                LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
            }
        });
        clientThread.start();
        latch.await();
        Assert.assertTrue(reconf1.get());
        Assert.assertTrue(reconf2.get());
        waitAndCheckNewConf(cluster, finalPeers.get(), 2, null);
        final RaftPeerId leader2 = RaftTestUtil.waitForLeader(cluster).getId();
        // check configuration manager's internal state
        // each reconf will generate two configurations: (old, new) and (new)
        cluster.getServerAliveStream().forEach(server -> {
            final ConfigurationManager confManager = RaftServerTestUtil.getConfigurationManager(server);
            // each reconf will generate two configurations: (old, new) and (new)
            // each leader change generates one configuration.
            // expectedConf = 1 (init) + 2*2 (two conf changes) + #leader
            final int expectedConf = leader2.equals(leaderId) ? 6 : 7;
            Assert.assertEquals(server.getId() + ": " + confManager, expectedConf, confManager.numOfConf());
        });
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) PeerChanges(org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

PeerChanges (org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges)7 RaftClient (org.apache.ratis.client.RaftClient)6 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)6 IOException (java.io.IOException)5 RaftPeer (org.apache.ratis.protocol.RaftPeer)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)4 ReconfigurationTimeoutException (org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException)4 TimeoutException (java.util.concurrent.TimeoutException)3 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)3 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)3 LeaderNotReadyException (org.apache.ratis.protocol.exceptions.LeaderNotReadyException)3 ReconfigurationInProgressException (org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException)3 RaftLog (org.apache.ratis.server.raftlog.RaftLog)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RaftServer (org.apache.ratis.server.RaftServer)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)1 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)1