Search in sources :

Example 36 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class RaftStateMachineExceptionTests method runTestRetryOnStateMachineException.

private void runTestRetryOnStateMachineException(CLUSTER cluster) throws Exception {
    RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    cluster.getLeaderAndSendFirstMessage(true);
    final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        final RaftClientRpc rpc = client.getClientRpc();
        final long callId = 999;
        final SimpleMessage message = new SimpleMessage("message");
        final RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, message);
        RaftClientReply reply = rpc.sendRequest(r);
        Assert.assertFalse(reply.isSuccess());
        Assert.assertNotNull(reply.getStateMachineException());
        // retry with the same callId
        for (int i = 0; i < 5; i++) {
            reply = rpc.sendRequest(r);
            Assert.assertEquals(client.getId(), reply.getClientId());
            Assert.assertEquals(callId, reply.getCallId());
            Assert.assertFalse(reply.isSuccess());
            Assert.assertNotNull(reply.getStateMachineException());
        }
        for (RaftServer.Division server : cluster.iterateDivisions()) {
            LOG.info("check server " + server.getId());
            JavaUtils.attemptRepeatedly(() -> {
                Assert.assertNotNull(RetryCacheTestUtil.get(server, client.getId(), callId));
                return null;
            }, 5, BaseTest.ONE_SECOND, "GetRetryEntry", LOG);
            final RaftLog log = server.getRaftLog();
            RaftTestUtil.logEntriesContains(log, oldLastApplied + 1, log.getNextIndex(), message);
        }
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 37 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class ServerPauseResumeTest method runTestPauseResume.

void runTestPauseResume(CLUSTER cluster) throws InterruptedException, IOException {
    // wait leader be elected.
    final RaftServer.Division leader = waitForLeader(cluster);
    RaftPeerId leaderId = leader.getId();
    final List<RaftServer.Division> followers = cluster.getFollowers();
    Assert.assertTrue(followers.size() >= 1);
    final RaftServerImpl follower = (RaftServerImpl) followers.get(0);
    SimpleMessage[] batch1 = SimpleMessage.create(100, "batch1");
    Thread writeThread = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch1);
    writeThread.join();
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    final RaftLog leaderLog = leader.getRaftLog();
    // leader should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(leaderLog, batch1));
    RaftLog followerLog = follower.getRaftLog();
    // follower should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch1));
    // pause follower.
    boolean isSuccess = follower.pause();
    Assert.assertTrue(isSuccess);
    Assert.assertTrue(follower.getInfo().getLifeCycleState().isPausingOrPaused());
    SimpleMessage[] batch2 = SimpleMessage.create(100, "batch2");
    Thread writeThread2 = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch2);
    writeThread2.join();
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    // paused follower should not have any batch2 message in its raftlog.
    Assert.assertTrue(RaftTestUtil.logEntriesNotContains(followerLog, batch2));
    // resume follower.
    isSuccess = follower.resume();
    Assert.assertTrue(isSuccess);
    Assert.assertFalse(follower.getInfo().getLifeCycleState().isPausingOrPaused());
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    // follower should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch2));
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 38 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class RaftSnapshotBaseTest method testBasicInstallSnapshot.

/**
 * Basic test for install snapshot: start a one node cluster and let it
 * generate a snapshot. Then delete the log and restart the node, and add more
 * nodes as followers.
 */
@Test
public void testBasicInstallSnapshot() throws Exception {
    final List<LogSegmentPath> logs;
    int i = 0;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
                RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
        LOG.info("nextIndex = {}", nextIndex);
        final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
        verifyTakeSnapshotMetric(cluster.getLeader());
        logs = LogSegmentPath.getLogSegmentPaths(cluster.getLeader().getRaftStorage());
    } finally {
        cluster.shutdown();
    }
    // delete the log segments from the leader
    for (LogSegmentPath path : logs) {
        FileUtils.delete(path.getPath());
    }
    // restart the peer
    LOG.info("Restarting the cluster");
    cluster.restart(false);
    try {
        assertLeaderContent(cluster);
        // generate some more traffic
        try (final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
            Assert.assertTrue(client.io().send(new SimpleMessage("m" + i)).isSuccess());
        }
        // add two more peers
        String[] newPeers = new String[] { "s3", "s4" };
        MiniRaftCluster.PeerChanges change = cluster.addNewPeers(newPeers, true, false);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        for (String newPeer : newPeers) {
            final RaftServer.Division s = cluster.getDivision(RaftPeerId.valueOf(newPeer));
            SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(s);
            Assert.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
        }
        // Verify installSnapshot counter on leader before restart.
        verifyInstallSnapshotMetric(cluster.getLeader());
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
        Timer timer = getTakeSnapshotTimer(cluster.getLeader());
        long count = timer.getCount();
        // restart the peer and check if it can correctly handle conf change
        cluster.restartServer(cluster.getLeader().getId(), false);
        assertLeaderContent(cluster);
        // verify that snapshot was taken when stopping the server
        Assert.assertTrue(count < timer.getCount());
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) LogSegmentPath(org.apache.ratis.server.raftlog.segmented.LogSegmentPath) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Timer(com.codahale.metrics.Timer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 39 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method testLeaderNotReadyException.

/**
 * Delay the commit of the leader placeholder log entry and see if the client
 * can correctly receive and handle the LeaderNotReadyException.
 */
@Test
public void testLeaderNotReadyException() throws Exception {
    LOG.info("Start testLeaderNotReadyException");
    final MiniRaftCluster cluster = newCluster(1).initServers();
    try {
        // delay 1s for each logSync call
        cluster.getServers().forEach(peer -> leaderPlaceHolderDelay.setDelayMs(peer.getId().toString(), 2000));
        cluster.start();
        AtomicBoolean caughtNotReady = new AtomicBoolean(false);
        AtomicBoolean success = new AtomicBoolean(false);
        final RaftPeerId leaderId = cluster.getPeers().iterator().next().getId();
        new Thread(() -> {
            try (final RaftClient client = cluster.createClient(leaderId)) {
                final RaftClientRpc sender = client.getClientRpc();
                final RaftClientRequest request = cluster.newRaftClientRequest(client.getId(), leaderId, new SimpleMessage("test"));
                while (!success.get()) {
                    try {
                        final RaftClientReply reply = sender.sendRequest(request);
                        success.set(reply.isSuccess());
                        if (reply.getException() != null && reply.getException() instanceof LeaderNotReadyException) {
                            caughtNotReady.set(true);
                        }
                    } catch (IOException e) {
                        LOG.info("Hit other IOException", e);
                    }
                    if (!success.get()) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException ignored) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            } catch (Exception ignored) {
                LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
            }
        }).start();
        RaftTestUtil.waitForLeader(cluster);
        for (int i = 0; !success.get() && i < 5; i++) {
            Thread.sleep(1000);
        }
        Assert.assertTrue(success.get());
        Assert.assertTrue(caughtNotReady.get());
    } finally {
        leaderPlaceHolderDelay.clear();
        cluster.shutdown();
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) 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) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 40 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage 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)

Aggregations

SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)52 RaftClient (org.apache.ratis.client.RaftClient)46 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)27 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)20 Test (org.junit.Test)20 RaftServer (org.apache.ratis.server.RaftServer)19 IOException (java.io.IOException)15 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)10 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)9 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 BaseTest (org.apache.ratis.BaseTest)7 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)6 File (java.io.File)5