Search in sources :

Example 31 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage 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)

Example 32 with SimpleMessage

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

the class ServerRestartTests method runTestRestartFollower.

void runTestRestartFollower(MiniRaftCluster cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    // write some messages
    final AtomicInteger messageCount = new AtomicInteger();
    final Supplier<Message> newMessage = () -> new SimpleMessage("m" + messageCount.getAndIncrement());
    writeSomething(newMessage, cluster);
    // restart a follower
    RaftPeerId followerId = cluster.getFollowers().get(0).getId();
    LOG.info("Restart follower {}", followerId);
    cluster.restartServer(followerId, false);
    // write some more messages
    writeSomething(newMessage, cluster);
    final int truncatedMessageIndex = messageCount.get() - 1;
    final long leaderLastIndex = cluster.getLeader().getRaftLog().getLastEntryTermIndex().getIndex();
    // make sure the restarted follower can catchup
    final RaftServer.Division followerState = cluster.getDivision(followerId);
    JavaUtils.attemptRepeatedly(() -> {
        Assert.assertTrue(followerState.getInfo().getLastAppliedIndex() >= leaderLastIndex);
        return null;
    }, 10, ONE_SECOND, "follower catchup", LOG);
    // make sure the restarted peer's log segments is correct
    final RaftServer.Division follower = cluster.restartServer(followerId, false);
    final RaftLog followerLog = follower.getRaftLog();
    final long followerLastIndex = followerLog.getLastEntryTermIndex().getIndex();
    Assert.assertTrue(followerLastIndex >= leaderLastIndex);
    final File followerOpenLogFile = getOpenLogFile(follower);
    final File leaderOpenLogFile = getOpenLogFile(cluster.getDivision(leaderId));
    // shutdown all servers
    for (RaftServer s : cluster.getServers()) {
        s.close();
    }
    // truncate log and
    assertTruncatedLog(followerId, followerOpenLogFile, followerLastIndex, cluster);
    assertTruncatedLog(leaderId, leaderOpenLogFile, leaderLastIndex, cluster);
    // restart and write something.
    cluster.restart(false);
    writeSomething(newMessage, cluster);
    // restart again and check messages.
    cluster.restart(false);
    try (final RaftClient client = cluster.createClient()) {
        for (int i = 0; i < messageCount.get(); i++) {
            if (i != truncatedMessageIndex) {
                final Message m = new SimpleMessage("m" + i);
                final RaftClientReply reply = client.io().sendReadOnly(m);
                Assert.assertTrue(reply.isSuccess());
                LOG.info("query {}: {} {}", m, reply, LogEntryProto.parseFrom(reply.getMessage().getContent()));
            }
        }
    }
}
Also used : Message(org.apache.ratis.protocol.Message) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog) TestSegmentedRaftLog(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog)

Example 33 with SimpleMessage

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

the class TestRaftServerWithGrpc method testRaftClientRequestMetrics.

void testRaftClientRequestMetrics(MiniRaftClusterWithGrpc cluster) throws IOException, ExecutionException, InterruptedException {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftServerMetricsImpl raftServerMetrics = getRaftServerMetrics(leader);
    try (final RaftClient client = cluster.createClient()) {
        final CompletableFuture<RaftClientReply> f1 = client.async().send(new SimpleMessage("testing"));
        Assert.assertTrue(f1.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(RAFT_CLIENT_WRITE_REQUEST).getCount() > 0);
        final CompletableFuture<RaftClientReply> f2 = client.async().sendReadOnly(new SimpleMessage("testing"));
        Assert.assertTrue(f2.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(RAFT_CLIENT_READ_REQUEST).getCount() > 0);
        final CompletableFuture<RaftClientReply> f3 = client.async().sendStaleRead(new SimpleMessage("testing"), 0, leader.getId());
        Assert.assertTrue(f3.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(RAFT_CLIENT_STALE_READ_REQUEST).getCount() > 0);
        final CompletableFuture<RaftClientReply> f4 = client.async().watch(0, RaftProtos.ReplicationLevel.ALL);
        Assert.assertTrue(f4.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(String.format(RAFT_CLIENT_WATCH_REQUEST, "-ALL")).getCount() > 0);
        final CompletableFuture<RaftClientReply> f5 = client.async().watch(0, RaftProtos.ReplicationLevel.MAJORITY);
        Assert.assertTrue(f5.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(String.format(RAFT_CLIENT_WATCH_REQUEST, "")).getCount() > 0);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl)

Example 34 with SimpleMessage

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

the class TestRaftServerWithGrpc method testRequestMetrics.

void testRequestMetrics(MiniRaftClusterWithGrpc cluster) throws Exception {
    try (RaftClient client = cluster.createClient()) {
        // send a request to make sure leader is ready
        final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("testing"));
        Assert.assertTrue(f.get().isSuccess());
    }
    SimpleStateMachine4Testing stateMachine = SimpleStateMachine4Testing.get(cluster.getLeader());
    stateMachine.blockFlushStateMachineData();
    // Block stateMachine flush data, so that 2nd request will not be
    // completed, and so it will not be removed from pending request map.
    List<RaftClient> clients = new ArrayList<>();
    try {
        RaftClient client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
        clients.add(client);
        client.async().send(new SimpleMessage("2nd Message"));
        final SortedMap<String, Gauge> gaugeMap = getRaftServerMetrics(cluster.getLeader()).getRegistry().getGauges((s, metric) -> s.contains(REQUEST_MEGA_BYTE_SIZE));
        for (int i = 0; i < 10; i++) {
            client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
            clients.add(client);
            client.async().send(new SimpleMessage("message " + i));
        }
        // Because we have passed 11 requests, and the element queue size is 10.
        RaftTestUtil.waitFor(() -> getRaftServerMetrics(cluster.getLeader()).getCounter(REQUEST_QUEUE_LIMIT_HIT_COUNTER).getCount() == 1, 300, 5000);
        stateMachine.unblockFlushStateMachineData();
        // Send a message with 1025kb , our byte size limit is 1024kb (1mb) , so it should fail
        // and byte size counter limit will be hit.
        client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
        final SizeInBytes size = SizeInBytes.valueOf("1025kb");
        final ByteString bytes = randomByteString(size.getSizeInt());
        Assert.assertEquals(size.getSizeInt(), bytes.size());
        client.async().send(new SimpleMessage(size + "-message", bytes));
        clients.add(client);
        RaftTestUtil.waitFor(() -> getRaftServerMetrics(cluster.getLeader()).getCounter(REQUEST_BYTE_SIZE_LIMIT_HIT_COUNTER).getCount() == 1, 300, 5000);
        Assert.assertEquals(2, getRaftServerMetrics(cluster.getLeader()).getCounter(RESOURCE_LIMIT_HIT_COUNTER).getCount());
    } finally {
        for (RaftClient client : clients) {
            client.close();
        }
    }
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) SizeInBytes(org.apache.ratis.util.SizeInBytes) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) Gauge(com.codahale.metrics.Gauge) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 35 with SimpleMessage

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

the class RaftStateMachineExceptionTests method runTestRetryOnExceptionDuringReplication.

private void runTestRetryOnExceptionDuringReplication(CLUSTER cluster) throws Exception {
    final RaftServer.Division oldLeader = RaftTestUtil.waitForLeader(cluster);
    cluster.getLeaderAndSendFirstMessage(true);
    // turn on the preAppend failure switch
    failPreAppend = true;
    try (final RaftClient client = cluster.createClient(oldLeader.getId())) {
        final RaftClientRpc rpc = client.getClientRpc();
        final long callId = 999;
        final SimpleMessage message = new SimpleMessage("message");
        RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), oldLeader.getId(), callId, message);
        RaftClientReply reply = rpc.sendRequest(r);
        Objects.requireNonNull(reply.getStateMachineException());
        final RetryCache.Entry oldEntry = RetryCacheTestUtil.get(oldLeader, client.getId(), callId);
        Assert.assertNotNull(oldEntry);
        Assert.assertTrue(RetryCacheTestUtil.isFailed(oldEntry));
        // At this point of time the old leader would have stepped down. wait for leader election to complete
        final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
        // retry
        r = cluster.newRaftClientRequest(client.getId(), leader.getId(), callId, message);
        reply = rpc.sendRequest(r);
        Objects.requireNonNull(reply.getStateMachineException());
        final RetryCache.Entry currentEntry = RetryCacheTestUtil.get(leader, client.getId(), callId);
        Assert.assertNotNull(currentEntry);
        Assert.assertTrue(RetryCacheTestUtil.isFailed(currentEntry));
        Assert.assertNotEquals(oldEntry, currentEntry);
        failPreAppend = false;
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) RetryCache(org.apache.ratis.server.RetryCache) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) 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