Search in sources :

Example 26 with SimpleMessage

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

the class RetryCacheTests method runTestRetryOnNewLeader.

void runTestRetryOnNewLeader(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        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);
        final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
        // trigger the reconfiguration, make sure the original leader is kicked out
        PeerChanges change = cluster.addNewPeers(2, true);
        RaftPeer[] allPeers = cluster.removePeers(2, true, asList(change.newPeers)).allPeersInNewConf;
        // trigger setConfiguration
        cluster.setConfiguration(allPeers);
        final RaftPeerId newLeaderId = JavaUtils.attemptRepeatedly(() -> {
            final RaftPeerId id = RaftTestUtil.waitForLeader(cluster).getId();
            Assert.assertNotEquals(leaderId, id);
            return id;
        }, 10, TimeDuration.valueOf(100, TimeUnit.MILLISECONDS), "wait for a leader different than " + leaderId, LOG);
        Assert.assertNotEquals(leaderId, newLeaderId);
        // same clientId and callId in the request
        r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, new SimpleMessage("message"));
        rpc.addRaftPeers(Arrays.asList(change.newPeers));
        for (int i = 0; i < 10; i++) {
            try {
                assertReply(rpc.sendRequest(r), client, callId);
                LOG.info("successfully sent out the retry request_" + i);
            } catch (Exception e) {
                LOG.info("hit exception while retrying the same request: " + r, e);
            }
            Thread.sleep(100);
        }
        // check the new leader and make sure the retry did not get committed
        Assert.assertEquals(0, count(cluster.getLeader().getRaftLog(), oldLastApplied + 1));
    }
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) PeerChanges(org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Example 27 with SimpleMessage

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

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

Example 29 with SimpleMessage

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

the class RaftBasicTests method testStateMachineMetrics.

public static void testStateMachineMetrics(boolean async, MiniRaftCluster cluster, Logger LOG) throws Exception {
    RaftServer.Division leader = waitForLeader(cluster);
    try (final RaftClient client = cluster.createClient()) {
        Gauge appliedIndexGauge = getStatemachineGaugeWithName(leader, STATEMACHINE_APPLIED_INDEX_GAUGE);
        Gauge smAppliedIndexGauge = getStatemachineGaugeWithName(leader, STATEMACHINE_APPLY_COMPLETED_GAUGE);
        long appliedIndexBefore = (Long) appliedIndexGauge.getValue();
        long smAppliedIndexBefore = (Long) smAppliedIndexGauge.getValue();
        checkFollowerCommitLagsLeader(cluster);
        if (async) {
            CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("abc"));
            replyFuture.get();
        } else {
            client.io().send(new SimpleMessage("abc"));
        }
        long appliedIndexAfter = (Long) appliedIndexGauge.getValue();
        long smAppliedIndexAfter = (Long) smAppliedIndexGauge.getValue();
        checkFollowerCommitLagsLeader(cluster);
        Assert.assertTrue("StateMachine Applied Index not incremented", appliedIndexAfter > appliedIndexBefore);
        Assert.assertTrue("StateMachine Apply completed Index not incremented", smAppliedIndexAfter > smAppliedIndexBefore);
    }
}
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) Gauge(com.codahale.metrics.Gauge)

Example 30 with SimpleMessage

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

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