Search in sources :

Example 46 with SimpleMessage

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

the class RaftExceptionBaseTest method runTestLogAppenderBufferCapacity.

void runTestLogAppenderBufferCapacity(CLUSTER cluster) throws Exception {
    final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    byte[] bytes = new byte[8192];
    Arrays.fill(bytes, (byte) 1);
    SimpleMessage msg = new SimpleMessage(new String(bytes));
    try (RaftClient client = cluster.createClient(leaderId)) {
        testFailureCase("testLogAppenderBufferCapacity", () -> client.io().send(msg), StateMachineException.class, RaftLogIOException.class);
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient)

Example 47 with SimpleMessage

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

the class RaftExceptionBaseTest method assertNotLeaderException.

RaftClientReply assertNotLeaderException(RaftPeerId expectedSuggestedLeader, String messageId, RaftPeerId server, RaftClientRpc rpc, CLUSTER cluster) throws IOException {
    final SimpleMessage message = new SimpleMessage(messageId);
    final RaftClientReply reply = rpc.sendRequest(cluster.newRaftClientRequest(ClientId.randomId(), server, message));
    Assert.assertNotNull(reply);
    Assume.assumeFalse(reply.isSuccess());
    final NotLeaderException nle = reply.getNotLeaderException();
    Objects.requireNonNull(nle);
    Assert.assertEquals(expectedSuggestedLeader, nle.getSuggestedLeader().getId());
    return reply;
}
Also used : NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage)

Example 48 with SimpleMessage

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

the class RequestLimitAsyncBaseTest method runTestWriteElementLimit.

void runTestWriteElementLimit(CLUSTER cluster) throws Exception {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    try (RaftClient c1 = cluster.createClient(leader.getId())) {
        {
            // send first message to make sure the cluster is working
            final SimpleMessage message = new SimpleMessage("first");
            final CompletableFuture<RaftClientReply> future = c1.async().send(message);
            final RaftClientReply reply = getWithDefaultTimeout(future);
            Assert.assertTrue(reply.isSuccess());
        }
        // collecting futures returned from StateMachine.applyTransaction
        final BlockingQueue<Runnable> toBeCompleted = SimpleStateMachine4Testing.get(leader).collecting().enable(SimpleStateMachine4Testing.Collecting.Type.APPLY_TRANSACTION);
        // send write requests up to the limit
        final List<CompletableFuture<RaftClientReply>> writeFutures = new ArrayList<>();
        for (int i = 0; i < writeElementLimit; i++) {
            final SimpleMessage message = new SimpleMessage("m" + i);
            writeFutures.add(c1.async().send(message));
        }
        // send watch requests up to the limit
        // watch a large index so that it won't complete
        final long watchBase = 1000;
        for (int i = 0; i < watchElementLimit; i++) {
            c1.async().watch(watchBase + i, ReplicationLevel.ALL);
        }
        // sleep to make sure that all the request were sent
        HUNDRED_MILLIS.sleep();
        try (RaftClient c2 = cluster.createClient(leader.getId(), RetryPolicies.noRetry())) {
            // more write requests should get ResourceUnavailableException
            final SimpleMessage message = new SimpleMessage("err");
            testFailureCase("send should fail", () -> c2.io().send(message), ResourceUnavailableException.class);
            testFailureCase("sendAsync should fail", () -> c2.async().send(message).get(), ExecutionException.class, ResourceUnavailableException.class);
            // more watch requests should get ResourceUnavailableException
            final long watchIndex = watchBase + watchElementLimit;
            testFailureCase("sendWatch should fail", () -> c2.io().watch(watchIndex, ReplicationLevel.ALL), ResourceUnavailableException.class);
            testFailureCase("sendWatchAsync should fail", () -> c2.async().watch(watchIndex, ReplicationLevel.ALL).get(), ExecutionException.class, ResourceUnavailableException.class);
        }
        // complete futures from applyTransaction
        toBeCompleted.forEach(Runnable::run);
        // check replies
        for (CompletableFuture<RaftClientReply> f : writeFutures) {
            final RaftClientReply reply = getWithDefaultTimeout(f);
            Assert.assertTrue(reply.isSuccess());
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 49 with SimpleMessage

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

the class RaftBasicTests method runTestBasicAppendEntries.

static void runTestBasicAppendEntries(boolean async, boolean killLeader, int numMessages, MiniRaftCluster cluster, Logger LOG) throws Exception {
    LOG.info("runTestBasicAppendEntries: async? {}, killLeader={}, numMessages={}", async, killLeader, numMessages);
    for (RaftServer s : cluster.getServers()) {
        cluster.restartServer(s.getId(), false);
    }
    RaftServer.Division leader = waitForLeader(cluster);
    final long term = leader.getInfo().getCurrentTerm();
    final CompletableFuture<Void> killAndRestartFollower = killAndRestartServer(cluster.getFollowers().get(0).getId(), 0, 1000, cluster, LOG);
    final CompletableFuture<Void> killAndRestartLeader;
    if (killLeader) {
        LOG.info("killAndRestart leader " + leader.getId());
        killAndRestartLeader = killAndRestartServer(leader.getId(), 2000, 4000, cluster, LOG);
    } else {
        killAndRestartLeader = CompletableFuture.completedFuture(null);
    }
    LOG.info(cluster.printServers());
    final SimpleMessage[] messages = SimpleMessage.create(numMessages);
    try (final RaftClient client = cluster.createClient()) {
        final AtomicInteger asyncReplyCount = new AtomicInteger();
        final CompletableFuture<Void> f = new CompletableFuture<>();
        for (SimpleMessage message : messages) {
            if (async) {
                client.async().send(message).thenAcceptAsync(reply -> {
                    if (!reply.isSuccess()) {
                        f.completeExceptionally(new AssertionError("Failed with reply " + reply));
                    } else if (asyncReplyCount.incrementAndGet() == messages.length) {
                        f.complete(null);
                    }
                });
            } else {
                final RaftClientReply reply = client.io().send(message);
                Assert.assertTrue(reply.isSuccess());
            }
        }
        if (async) {
            f.join();
            Assert.assertEquals(messages.length, asyncReplyCount.get());
        }
    }
    Thread.sleep(cluster.getTimeoutMax().toIntExact(TimeUnit.MILLISECONDS) + 100);
    LOG.info(cluster.printAllLogs());
    killAndRestartFollower.join();
    killAndRestartLeader.join();
    final List<RaftServer.Division> divisions = cluster.getServerAliveStream().collect(Collectors.toList());
    for (RaftServer.Division impl : divisions) {
        JavaUtils.attempt(() -> RaftTestUtil.assertLogEntries(impl, term, messages), 5, TimeDuration.valueOf(1, TimeUnit.SECONDS), impl.getId() + " assertLogEntries", LOG);
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftClient(org.apache.ratis.client.RaftClient)

Example 50 with SimpleMessage

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

the class RetryCacheTests method runTestBasicRetry.

void runTestBasicRetry(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId();
    final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        final RaftClientRpc rpc = client.getClientRpc();
        final long callId = 999;
        RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, new SimpleMessage("message"));
        assertReply(rpc.sendRequest(r), client, callId);
        // retry with the same callId
        for (int i = 0; i < 5; i++) {
            assertReply(rpc.sendRequest(r), client, callId);
        }
        assertServer(cluster, client.getId(), callId, oldLastApplied);
    }
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

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