Search in sources :

Example 41 with RaftClient

use of org.apache.ratis.client.RaftClient 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 42 with RaftClient

use of org.apache.ratis.client.RaftClient 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 43 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class DataStreamAsyncClusterTests method runTestDataStream.

long runTestDataStream(CLUSTER cluster, int numStreams, int bufferSize, int bufferNum, boolean stepDownLeader) {
    final Iterable<RaftServer> servers = CollectionUtils.as(cluster.getServers(), s -> s);
    final RaftPeerId leader = cluster.getLeader().getId();
    final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
    final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
    try (RaftClient client = cluster.createClient(primaryServer)) {
        ClientId primaryClientId = getPrimaryClientId(cluster, primaryServer);
        for (int i = 0; i < numStreams; i++) {
            final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer));
            futures.add(CompletableFuture.supplyAsync(() -> DataStreamTestUtils.writeAndCloseAndAssertReplies(servers, leader, out, bufferSize, bufferNum, primaryClientId, client.getId(), stepDownLeader).join(), executor));
        }
        Assert.assertEquals(numStreams, futures.size());
        return futures.stream().map(CompletableFuture::join).map(RaftClientReply::getLogIndex).max(Long::compareTo).orElseThrow(IllegalStateException::new);
    } catch (IOException e) {
        throw new CompletionException(e);
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) CompletableFuture(java.util.concurrent.CompletableFuture) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) CompletionException(java.util.concurrent.CompletionException) ClientId(org.apache.ratis.protocol.ClientId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 44 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class DataStreamClusterTests method runTestDataStreamOutput.

void runTestDataStreamOutput(CLUSTER cluster) throws Exception {
    final RaftClientRequest request;
    final CompletableFuture<RaftClientReply> reply;
    final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
    try (RaftClient client = cluster.createClient(primaryServer)) {
        try (final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer))) {
            request = out.getHeader();
            reply = out.getRaftClientReplyFuture();
            // write using DataStreamOutput
            DataStreamTestUtils.writeAndAssertReplies(out, 1000, 10);
        }
    }
    watchOrSleep(cluster, reply.join().getLogIndex());
    assertLogEntry(cluster, request);
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 45 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method runTestKillLeaderDuringReconf.

void runTestKillLeaderDuringReconf(CLUSTER cluster) throws Exception {
    final AtomicBoolean clientRunning = new AtomicBoolean(true);
    Thread clientThread = null;
    try {
        final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
        PeerChanges c1 = cluster.addNewPeers(1, false);
        PeerChanges c2 = cluster.removePeers(1, false, asList(c1.newPeers));
        LOG.info("Start setConf: {}", asList(c2.allPeersInNewConf));
        LOG.info(cluster.printServers());
        final CompletableFuture<Void> setConf = new CompletableFuture<>();
        clientThread = new Thread(() -> {
            try (final RaftClient client = cluster.createClient(leaderId)) {
                for (int i = 0; clientRunning.get() && !setConf.isDone(); i++) {
                    final RaftClientReply reply = client.admin().setConfiguration(c2.allPeersInNewConf);
                    if (reply.isSuccess()) {
                        setConf.complete(null);
                    }
                    LOG.info("setConf attempt #{} failed, {}", i, cluster.printServers());
                }
            } catch (Exception e) {
                LOG.error("Failed to setConf", e);
                setConf.completeExceptionally(e);
            }
        });
        clientThread.start();
        TimeUnit.SECONDS.sleep(1);
        // the leader cannot generate the (old, new) conf, and it will keep
        // bootstrapping the 2 new peers since they have not started yet
        Assert.assertFalse(((RaftConfigurationImpl) cluster.getLeader().getRaftConf()).isTransitional());
        // only (0) the first conf entry, (1) the 1st setConf entry and (2) a metadata entry
        {
            final RaftLog leaderLog = cluster.getLeader().getRaftLog();
            for (LogEntryProto e : RaftTestUtil.getLogEntryProtos(leaderLog)) {
                LOG.info("{}", LogProtoUtils.toLogEntryString(e));
            }
            final long commitIndex = leaderLog.getLastCommittedIndex();
            Assert.assertTrue("commitIndex = " + commitIndex + " > 2", commitIndex <= 2);
        }
        final RaftPeerId killed = RaftTestUtil.waitAndKillLeader(cluster);
        Assert.assertEquals(leaderId, killed);
        final RaftPeerId newLeaderId = RaftTestUtil.waitForLeader(cluster).getId();
        LOG.info("newLeaderId: {}", newLeaderId);
        LOG.info("start new peers: {}", Arrays.asList(c1.newPeers));
        for (RaftPeer np : c1.newPeers) {
            cluster.restartServer(np.getId(), false);
        }
        try {
            setConf.get(10, TimeUnit.SECONDS);
        } catch (TimeoutException ignored) {
        }
        // the client fails with the first leader, and then retry the same setConfiguration request
        waitAndCheckNewConf(cluster, c2.allPeersInNewConf, 2, Collections.singletonList(leaderId));
        setConf.get(1, TimeUnit.SECONDS);
    } finally {
        if (clientThread != null) {
            clientRunning.set(false);
            clientThread.interrupt();
        }
    }
}
Also used : RaftPeer(org.apache.ratis.protocol.RaftPeer) 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) CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) 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) RaftLog(org.apache.ratis.server.raftlog.RaftLog) TimeoutException(java.util.concurrent.TimeoutException) ReconfigurationTimeoutException(org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException)

Aggregations

RaftClient (org.apache.ratis.client.RaftClient)134 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)66 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)54 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)46 RaftServer (org.apache.ratis.server.RaftServer)46 Test (org.junit.Test)44 IOException (java.io.IOException)41 RaftPeer (org.apache.ratis.protocol.RaftPeer)33 BaseTest (org.apache.ratis.BaseTest)27 RaftTestUtil (org.apache.ratis.RaftTestUtil)22 ArrayList (java.util.ArrayList)20 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)20 RaftGroup (org.apache.ratis.protocol.RaftGroup)16 CompletableFuture (java.util.concurrent.CompletableFuture)14 RaftProperties (org.apache.ratis.conf.RaftProperties)14 File (java.io.File)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)11 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)11