Search in sources :

Example 66 with RaftClient

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

the class RaftAsyncTests method testAsyncRequestSemaphore.

@Test
public void testAsyncRequestSemaphore() throws InterruptedException, IOException {
    LOG.info("Running testAsyncRequestSemaphore");
    CLUSTER cluster = getFactory().newCluster(NUM_SERVERS, properties);
    Assert.assertNull(cluster.getLeader());
    cluster.start();
    waitForLeader(cluster);
    int numMessages = RaftClientConfigKeys.Async.maxOutstandingRequests(properties);
    CompletableFuture[] futures = new CompletableFuture[numMessages + 1];
    final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMessages);
    final RaftClient client = cluster.createClient();
    // Set blockTransaction flag so that transaction blocks
    for (RaftServerProxy server : cluster.getServers()) {
        ((SimpleStateMachine4Testing) server.getStateMachine()).setBlockTransaction(true);
    }
    // Send numMessages which are blocked and do not release the client semaphore permits
    AtomicInteger blockedRequestsCount = new AtomicInteger();
    for (int i = 0; i < numMessages; i++) {
        blockedRequestsCount.getAndIncrement();
        futures[i] = client.sendAsync(messages[i]);
        blockedRequestsCount.decrementAndGet();
    }
    Assert.assertTrue(blockedRequestsCount.get() == 0);
    ExecutorService threadPool = Executors.newFixedThreadPool(1);
    futures[numMessages] = CompletableFuture.supplyAsync(() -> {
        blockedRequestsCount.incrementAndGet();
        client.sendAsync(new RaftTestUtil.SimpleMessage("n1"));
        blockedRequestsCount.decrementAndGet();
        return null;
    }, threadPool);
    // Allow the last msg to be sent
    while (blockedRequestsCount.get() != 1) {
        Thread.sleep(1000);
    }
    Assert.assertTrue(blockedRequestsCount.get() == 1);
    // Since all semaphore permits are acquired the last message sent is in queue
    RaftClientTestUtil.assertAsyncRequestSemaphore(client, 0, 1);
    // Unset the blockTransaction flag so that semaphore permits can be released
    for (RaftServerProxy server : cluster.getServers()) {
        ((SimpleStateMachine4Testing) server.getStateMachine()).setBlockTransaction(false);
    }
    for (int i = 0; i <= numMessages; i++) {
        futures[i].join();
    }
    Assert.assertTrue(blockedRequestsCount.get() == 0);
    cluster.shutdown();
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftServerProxy(org.apache.ratis.server.impl.RaftServerProxy) RaftClient(org.apache.ratis.client.RaftClient)

Example 67 with RaftClient

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

the class RaftExceptionBaseTest method testGroupMismatchException.

@Test
public void testGroupMismatchException() throws Exception {
    final RaftGroup clusterGroup = cluster.getGroup();
    Assert.assertEquals(NUM_PEERS, clusterGroup.getPeers().size());
    final RaftGroup anotherGroup = new RaftGroup(RaftGroupId.randomId(), clusterGroup.getPeers());
    Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId());
    // Create client using another group
    try (RaftClient client = cluster.createClient(anotherGroup)) {
        testFailureCase("send(..) with client group being different from the server group", () -> client.send(Message.EMPTY), GroupMismatchException.class);
        testFailureCase("sendReadOnly(..) with client group being different from the server group", () -> client.sendReadOnly(Message.EMPTY), GroupMismatchException.class);
        testFailureCase("setConfiguration(..) with client group being different from the server group", () -> client.setConfiguration(RaftPeer.emptyArray()), GroupMismatchException.class);
        testFailureCase("reinitialize(..) with client group being different from the server group", () -> client.reinitialize(anotherGroup, clusterGroup.getPeers().iterator().next().getId()), GroupMismatchException.class);
    }
}
Also used : RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test)

Example 68 with RaftClient

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

the class RaftExceptionBaseTest method testHandleNotLeaderException.

private void testHandleNotLeaderException(boolean killNewLeader) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    final RaftClient client = cluster.createClient(leaderId);
    RaftClientReply reply = client.send(new SimpleMessage("m1"));
    Assert.assertTrue(reply.isSuccess());
    // enforce leader change
    RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId);
    if (killNewLeader) {
        // kill the new leader
        cluster.killServer(newLeader);
    }
    RaftClientRpc rpc = client.getClientRpc();
    reply = null;
    for (int i = 0; reply == null && i < 10; i++) {
        try {
            reply = rpc.sendRequest(cluster.newRaftClientRequest(ClientId.randomId(), leaderId, new SimpleMessage("m2")));
        } catch (IOException ignored) {
            Thread.sleep(1000);
        }
    }
    Assert.assertNotNull(reply);
    Assert.assertFalse(reply.isSuccess());
    final NotLeaderException nle = reply.getNotLeaderException();
    Objects.requireNonNull(nle);
    Assert.assertEquals(newLeader, nle.getSuggestedLeader().getId());
    reply = client.send(new SimpleMessage("m3"));
    Assert.assertTrue(reply.isSuccess());
    client.close();
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) IOException(java.io.IOException) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Example 69 with RaftClient

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

the class TestRaftLogMetrics method testFlushMetric.

@Test
public void testFlushMetric() throws Exception {
    int numMsg = 2;
    final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMsg);
    try (final RaftClient client = cluster.createClient()) {
        for (RaftTestUtil.SimpleMessage message : messages) {
            client.send(message);
        }
    }
    for (RaftServerProxy rsp : cluster.getServers()) {
        String flushTimeMetric = getLogFlushTimeMetric(rsp.getId().toString());
        Timer tm = RatisMetricsRegistry.getRegistry().getTimers().get(flushTimeMetric);
        Assert.assertNotNull(tm);
        // Number of log entries expected = numMsg + 1 entry for start-log-segment
        int numExpectedLogEntries = numMsg + 1;
        Assert.assertEquals(numExpectedLogEntries, tm.getCount());
        Assert.assertTrue(tm.getMeanRate() > 0);
        // Test jmx
        ObjectName oname = new ObjectName("metrics", "name", flushTimeMetric);
        Assert.assertEquals(numExpectedLogEntries, ((Long) ManagementFactory.getPlatformMBeanServer().getAttribute(oname, "Count")).intValue());
    }
}
Also used : Timer(com.codahale.metrics.Timer) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServerProxy(org.apache.ratis.server.impl.RaftServerProxy) RaftClient(org.apache.ratis.client.RaftClient) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 70 with RaftClient

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

the class TestRestartRaftPeer method restartFollower.

@Test
public void restartFollower() throws Exception {
    cluster.start();
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    final RaftClient client = cluster.createClient(leaderId);
    // write some messages
    final byte[] content = new byte[1024];
    Arrays.fill(content, (byte) 1);
    final SimpleMessage message = new SimpleMessage(new String(content));
    for (int i = 0; i < 10; i++) {
        Assert.assertTrue(client.send(message).isSuccess());
    }
    // restart a follower
    RaftPeerId followerId = cluster.getFollowers().get(0).getId();
    LOG.info("Restart follower {}", followerId);
    cluster.restartServer(followerId, false);
    // write some more messages
    for (int i = 0; i < 10; i++) {
        Assert.assertTrue(client.send(message).isSuccess());
    }
    client.close();
    // make sure the restarted follower can catchup
    boolean catchup = false;
    long lastAppliedIndex = 0;
    for (int i = 0; i < 10 && !catchup; i++) {
        Thread.sleep(500);
        lastAppliedIndex = cluster.getServer(followerId).getImpl().getState().getLastAppliedIndex();
        catchup = lastAppliedIndex >= 20;
    }
    Assert.assertTrue("lastAppliedIndex=" + lastAppliedIndex, catchup);
    // make sure the restarted peer's log segments is correct
    cluster.restartServer(followerId, false);
    Assert.assertTrue(cluster.getServer(followerId).getImpl().getState().getLog().getLastEntryTermIndex().getIndex() >= 20);
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) ParameterizedBaseTest(org.apache.ratis.examples.ParameterizedBaseTest) Test(org.junit.Test)

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