Search in sources :

Example 6 with RaftClientReply

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

the class RaftSnapshotBaseTest method testInstallSnapshotDuringBootstrap.

/**
 * Test for install snapshot during a peer bootstrap: start a one node cluster
 * and let it generate a snapshot. Add another node and verify that the new
 * node installs a snapshot from the old node.
 */
@Test
public void testInstallSnapshotDuringBootstrap() throws Exception {
    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());
        assertLeaderContent(cluster);
        // 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
        verifyInstallSnapshotMetric(cluster.getLeader());
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) 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 7 with RaftClientReply

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

the class RaftSnapshotBaseTest method testRestartPeer.

/**
 * Keep generating writing traffic and make sure snapshots are taken.
 * We then restart the whole raft peer and check if it can correctly load
 * snapshots + raft log.
 */
@Test
public void testRestartPeer() throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    int i = 0;
    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());
        }
    }
    final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
    LOG.info("nextIndex = {}", nextIndex);
    // wait for the snapshot to be done
    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);
    // restart the peer and check if it can correctly load snapshot
    cluster.restart(false);
    try {
        // 200 messages + two leader elections --> last committed = 201
        assertLeaderContent(cluster);
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) 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 8 with RaftClientReply

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

the class SnapshotManagementTest method runTestTakeSnapshotOnSpecificServer.

void runTestTakeSnapshotOnSpecificServer(CLUSTER cluster) throws Exception {
    final RaftClientReply snapshotReply;
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftServer.Division follower = cluster.getFollowers().get(0);
    final RaftPeerId followerId = follower.getId();
    Assert.assertTrue(follower.getInfo().isFollower());
    try (final RaftClient client = cluster.createClient(followerId)) {
        for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()); i++) {
            RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        snapshotReply = client.getSnapshotManagementApi(followerId).create(3000);
    }
    Assert.assertTrue(snapshotReply.isSuccess());
    final long snapshotIndex = snapshotReply.getLogIndex();
    LOG.info("snapshotIndex = {} on {} server {}", snapshotIndex, follower.getInfo().getCurrentRole(), follower.getId());
    final File snapshotFile = SimpleStateMachine4Testing.get(follower).getStateMachineStorage().getSnapshotFile(follower.getInfo().getCurrentTerm(), snapshotIndex);
    Assert.assertTrue(snapshotFile.exists());
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient)

Example 9 with RaftClientReply

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

the class SnapshotManagementTest method runTestTakeSnapshot.

void runTestTakeSnapshot(CLUSTER cluster) throws Exception {
    final RaftClientReply snapshotReply;
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = leader.getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()); i++) {
            RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        snapshotReply = client.getSnapshotManagementApi().create(3000);
    }
    Assert.assertTrue(snapshotReply.isSuccess());
    final long snapshotIndex = snapshotReply.getLogIndex();
    LOG.info("snapshotIndex = {}", snapshotIndex);
    final File snapshotFile = SimpleStateMachine4Testing.get(leader).getStateMachineStorage().getSnapshotFile(leader.getInfo().getCurrentTerm(), snapshotIndex);
    Assert.assertTrue(snapshotFile.exists());
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient)

Example 10 with RaftClientReply

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

the class SnapshotManagementTest method runTestTakeSnapshotWithConfigurableGap.

void runTestTakeSnapshotWithConfigurableGap(CLUSTER cluster) throws Exception {
    RaftClientReply snapshotReply;
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = leader.getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()) / 2 - 1; i++) {
            RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        Assert.assertTrue(leader.getStateMachine().getLastAppliedTermIndex().getIndex() < RaftServerConfigKeys.Snapshot.creationGap(getProperties()));
        snapshotReply = client.getSnapshotManagementApi(leaderId).create(3000);
        Assert.assertTrue(snapshotReply.isSuccess());
        Assert.assertEquals(0, snapshotReply.getLogIndex());
        for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()) / 2 - 1; i++) {
            RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        final SnapshotManagementRequest r1 = SnapshotManagementRequest.newCreate(client.getId(), leaderId, cluster.getGroupId(), CallId.getAndIncrement(), 3000);
        snapshotReply = client.getSnapshotManagementApi(leaderId).create(3000);
    }
    Assert.assertTrue(snapshotReply.isSuccess());
    final long snapshotIndex = snapshotReply.getLogIndex();
    LOG.info("snapshotIndex = {}", snapshotIndex);
    final File snapshotFile = SimpleStateMachine4Testing.get(leader).getStateMachineStorage().getSnapshotFile(leader.getInfo().getCurrentTerm(), snapshotIndex);
    Assert.assertTrue(snapshotFile.exists());
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) SnapshotManagementRequest(org.apache.ratis.protocol.SnapshotManagementRequest) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)96 RaftClient (org.apache.ratis.client.RaftClient)71 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)43 RaftServer (org.apache.ratis.server.RaftServer)40 IOException (java.io.IOException)32 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)25 CompletableFuture (java.util.concurrent.CompletableFuture)22 RaftPeer (org.apache.ratis.protocol.RaftPeer)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 TimeDuration (org.apache.ratis.util.TimeDuration)18 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)14 RaftTestUtil (org.apache.ratis.RaftTestUtil)13 RaftProperties (org.apache.ratis.conf.RaftProperties)13 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 List (java.util.List)12 RetryPolicy (org.apache.ratis.retry.RetryPolicy)12 CompletionException (java.util.concurrent.CompletionException)11 ExecutionException (java.util.concurrent.ExecutionException)11