Search in sources :

Example 1 with RaftLogBase

use of org.apache.ratis.server.raftlog.RaftLogBase in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method runTestRevertConfigurationChange.

void runTestRevertConfigurationChange(CLUSTER cluster) throws Exception {
    RaftLogBase log2 = null;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftServer.Division leader = cluster.getLeader();
        final RaftPeerId leaderId = leader.getId();
        final RaftLog log = leader.getRaftLog();
        log2 = (RaftLogBase) log;
        Thread.sleep(1000);
        // we block the incoming msg for the leader and block its requests to
        // followers, so that we force the leader change and the old leader will
        // not know
        LOG.info("start blocking the leader");
        BlockRequestHandlingInjection.getInstance().blockReplier(leaderId.toString());
        cluster.setBlockRequestsFrom(leaderId.toString(), true);
        PeerChanges change = cluster.removePeers(1, false, new ArrayList<>());
        AtomicBoolean gotNotLeader = new AtomicBoolean(false);
        final Thread clientThread = new Thread(() -> {
            try (final RaftClient client = cluster.createClient(leaderId)) {
                LOG.info("client starts to change conf");
                final RaftClientRpc sender = client.getClientRpc();
                RaftClientReply reply = sender.sendRequest(cluster.newSetConfigurationRequest(client.getId(), leaderId, change.allPeersInNewConf));
                if (reply.getNotLeaderException() != null) {
                    gotNotLeader.set(true);
                }
            } catch (IOException e) {
                LOG.warn("Got unexpected exception when client1 changes conf", e);
            }
        });
        clientThread.start();
        // find ConfigurationEntry
        final TimeDuration sleepTime = TimeDuration.valueOf(500, TimeUnit.MILLISECONDS);
        final long confIndex = JavaUtils.attemptRepeatedly(() -> {
            final long last = log.getLastEntryTermIndex().getIndex();
            for (long i = last; i >= 1; i--) {
                if (log.get(i).hasConfigurationEntry()) {
                    return i;
                }
            }
            throw new Exception("ConfigurationEntry not found: last=" + last);
        }, 10, sleepTime, "confIndex", LOG);
        // wait till the old leader persist the new conf
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(log.getFlushIndex() >= confIndex);
            return null;
        }, 10, sleepTime, "FLUSH", LOG);
        final long committed = log.getLastCommittedIndex();
        Assert.assertTrue(committed < confIndex);
        // unblock the old leader
        BlockRequestHandlingInjection.getInstance().unblockReplier(leaderId.toString());
        cluster.setBlockRequestsFrom(leaderId.toString(), false);
        // the client should get NotLeaderException
        clientThread.join(5000);
        Assert.assertTrue(gotNotLeader.get());
        // the old leader should have truncated the setConf from the log
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(log.getLastCommittedIndex() >= confIndex);
            return null;
        }, 10, ONE_SECOND, "COMMIT", LOG);
        Assert.assertTrue(log.get(confIndex).hasConfigurationEntry());
        log2 = null;
    } finally {
        RaftStorageTestUtils.printLog(log2, s -> LOG.info(s));
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) RaftLogBase(org.apache.ratis.server.raftlog.RaftLogBase) IOException(java.io.IOException) 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) TimeDuration(org.apache.ratis.util.TimeDuration) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Aggregations

IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 RaftClient (org.apache.ratis.client.RaftClient)1 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 LeaderNotReadyException (org.apache.ratis.protocol.exceptions.LeaderNotReadyException)1 ReconfigurationInProgressException (org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException)1 ReconfigurationTimeoutException (org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException)1 RaftServer (org.apache.ratis.server.RaftServer)1 PeerChanges (org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges)1 RaftLog (org.apache.ratis.server.raftlog.RaftLog)1 RaftLogBase (org.apache.ratis.server.raftlog.RaftLogBase)1 TimeDuration (org.apache.ratis.util.TimeDuration)1