Search in sources :

Example 56 with RaftClient

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

the class GroupInfoBaseTest method sendMessages.

RaftClientReply sendMessages(int n, MiniRaftCluster cluster) throws Exception {
    LOG.info("sendMessages: " + n);
    RaftClientReply reply = null;
    try (final RaftClient client = cluster.createClient()) {
        for (int i = 0; i < n; i++) {
            reply = client.io().send(Message.valueOf("m" + i));
        }
    }
    return reply;
}
Also used : RaftClient(org.apache.ratis.client.RaftClient)

Example 57 with RaftClient

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

the class LeaderElectionTests method testTransferLeaderTimeout.

@Test
public void testTransferLeaderTimeout() throws Exception {
    try (final MiniRaftCluster cluster = newCluster(3)) {
        cluster.start();
        final RaftServer.Division leader = waitForLeader(cluster);
        try (RaftClient client = cluster.createClient(leader.getId())) {
            List<RaftServer.Division> followers = cluster.getFollowers();
            Assert.assertEquals(followers.size(), 2);
            RaftServer.Division newLeader = followers.get(0);
            // isolate new leader, so that transfer leadership will timeout
            isolate(cluster, newLeader.getId());
            List<RaftPeer> peers = cluster.getPeers();
            List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
            RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
            Assert.assertTrue(reply.isSuccess());
            CompletableFuture<Boolean> transferTimeoutFuture = CompletableFuture.supplyAsync(() -> {
                try {
                    long timeoutMs = 5000;
                    long start = System.currentTimeMillis();
                    try {
                        client.admin().transferLeadership(newLeader.getId(), timeoutMs);
                    } catch (TransferLeadershipException e) {
                        long cost = System.currentTimeMillis() - start;
                        Assert.assertTrue(cost > timeoutMs);
                        Assert.assertTrue(e.getMessage().contains("Failed to transfer leadership to"));
                        Assert.assertTrue(e.getMessage().contains("timed out"));
                    }
                    return true;
                } catch (IOException e) {
                    return false;
                }
            });
            // before transfer timeout, leader should in steppingDown
            JavaUtils.attemptRepeatedly(() -> {
                try {
                    client.io().send(new RaftTestUtil.SimpleMessage("message"));
                } catch (LeaderSteppingDownException e) {
                    Assert.assertTrue(e.getMessage().contains("is stepping down"));
                }
                return null;
            }, 5, TimeDuration.ONE_SECOND, "check leader steppingDown", RaftServer.LOG);
            Assert.assertTrue(transferTimeoutFuture.get());
            // after transfer timeout, leader should accept request
            reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
            Assert.assertTrue(reply.getReplierId().equals(leader.getId().toString()));
            Assert.assertTrue(reply.isSuccess());
            deIsolate(cluster, newLeader.getId());
        }
        cluster.shutdown();
    }
}
Also used : TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 58 with RaftClient

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

the class InstallSnapshotNotificationTests method testInstallSnapshotDuringBootstrap.

private void testInstallSnapshotDuringBootstrap(CLUSTER cluster) throws Exception {
    leaderSnapshotInfoRef.set(null);
    numSnapshotRequests.set(0);
    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 RaftTestUtil.SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        final RaftServer.Division leader = cluster.getLeader();
        final long nextIndex = leader.getRaftLog().getNextIndex();
        LOG.info("nextIndex = {}", nextIndex);
        final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
        RaftSnapshotBaseTest.assertLeaderContent(cluster);
        final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
        final boolean set = leaderSnapshotInfoRef.compareAndSet(null, leaderSnapshotInfo);
        Assert.assertTrue(set);
        // add two more peers
        final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true, true);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
        // leader snapshot.
        for (RaftServer.Division follower : cluster.getFollowers()) {
            Assert.assertEquals(leaderSnapshotInfo.getIndex(), RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
        }
        // Make sure each new peer got one snapshot notification.
        Assert.assertEquals(2, numSnapshotRequests.get());
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) 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)

Example 59 with RaftClient

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

the class RaftAsyncExceptionTests method runTestTimeoutException.

private void runTestTimeoutException(CLUSTER cluster) throws Exception {
    // send a message to make sure the cluster is working
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.io().send(new SimpleMessage("m0"));
        Assert.assertTrue(reply.isSuccess());
        RaftClientConfigKeys.Rpc.setRequestTimeout(properties.get(), ONE_SECOND);
        // Block StartTransaction
        StreamSupport.stream(cluster.getServers().spliterator(), false).map(cluster::getDivision).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockStartTransaction);
        final CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("m1"));
        FIVE_SECONDS.sleep();
        // Unblock StartTransaction
        StreamSupport.stream(cluster.getServers().spliterator(), false).map(cluster::getDivision).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockStartTransaction);
        // The request should succeed after start transaction is unblocked
        Assert.assertTrue(replyFuture.get(FIVE_SECONDS.getDuration(), FIVE_SECONDS.getUnit()).isSuccess());
    }
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient)

Example 60 with RaftClient

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

the class RaftAsyncExceptionTests method runTestGroupMismatchException.

private void runTestGroupMismatchException(CLUSTER cluster) throws Exception {
    // send a message to make sure the cluster is working
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.async().send(new SimpleMessage("first")).get();
        Assert.assertTrue(reply.isSuccess());
    }
    // create another group
    final RaftGroup clusterGroup = cluster.getGroup();
    final RaftGroup anotherGroup = RaftGroup.valueOf(RaftGroupId.randomId(), clusterGroup.getPeers());
    Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId());
    // create another client using another group
    final SimpleMessage[] messages = SimpleMessage.create(5);
    try (RaftClient client = cluster.createClient(anotherGroup)) {
        // send a few messages
        final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
        for (SimpleMessage m : messages) {
            futures.add(client.async().send(m));
        }
        Assert.assertEquals(messages.length, futures.size());
        // check replies
        final Iterator<CompletableFuture<RaftClientReply>> i = futures.iterator();
        testFailureCase("First reply is GroupMismatchException", () -> i.next().get(), ExecutionException.class, GroupMismatchException.class);
        for (; i.hasNext(); ) {
            testFailureCase("Following replies are AlreadyClosedException caused by GroupMismatchException", () -> i.next().get(), ExecutionException.class, AlreadyClosedException.class, GroupMismatchException.class);
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftClient(org.apache.ratis.client.RaftClient)

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