Search in sources :

Example 76 with RaftClient

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

the class TestExceptionDependentRetry method runTestExceptionRetryAttempts.

void runTestExceptionRetryAttempts(MiniRaftClusterWithGrpc cluster) throws Exception {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final ExceptionDependentRetry policy = ExceptionDependentRetry.newBuilder().setExceptionToPolicy(TimeoutIOException.class, MultipleLinearRandomRetry.parseCommaSeparated("1ms, 5")).setDefaultPolicy(RetryPolicies.retryForeverNoSleep()).build();
    // create a client with the exception dependent policy
    try (final RaftClient client = cluster.createClient(policy)) {
        client.async().send(new RaftTestUtil.SimpleMessage("1")).get();
    }
    try (final RaftClient client = cluster.createClient(policy)) {
        SimpleStateMachine4Testing.get(leader).blockWriteStateMachineData();
        client.async().send(new RaftTestUtil.SimpleMessage("2")).get();
        Assert.fail("Test should have failed.");
    } catch (ExecutionException e) {
        RaftRetryFailureException rrfe = (RaftRetryFailureException) e.getCause();
        Assert.assertEquals(16, rrfe.getAttemptCount());
    } finally {
        SimpleStateMachine4Testing.get(leader).unblockWriteStateMachineData();
        cluster.shutdown();
    }
}
Also used : RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) RaftServer(org.apache.ratis.server.RaftServer) ExecutionException(java.util.concurrent.ExecutionException) RaftClient(org.apache.ratis.client.RaftClient)

Example 77 with RaftClient

use of org.apache.ratis.client.RaftClient 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 78 with RaftClient

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

the class TestStateMachine method runTestTransactionContextIsPassedBack.

static void runTestTransactionContextIsPassedBack(MiniRaftCluster cluster) throws Throwable {
    // tests that the TrxContext set by the StateMachine in Leader is passed back to the SM
    int numTrx = 100;
    final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numTrx);
    try (final RaftClient client = cluster.createClient()) {
        for (RaftTestUtil.SimpleMessage message : messages) {
            client.io().send(message);
        }
    }
    // TODO: there eshould be a better way to ensure all data is replicated and applied
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
    for (RaftServer.Division raftServer : cluster.iterateDivisions()) {
        final SMTransactionContext sm = SMTransactionContext.get(raftServer);
        sm.rethrowIfException();
        assertEquals(numTrx, sm.numApplied.get());
    }
    // check leader
    RaftServer.Division raftServer = cluster.getLeader();
    // assert every transaction has obtained context in leader
    final SMTransactionContext sm = SMTransactionContext.get(raftServer);
    final List<Long> ll = new ArrayList<>(sm.applied);
    Collections.sort(ll);
    assertEquals(ll.toString(), ll.size(), numTrx);
    for (int i = 0; i < numTrx; i++) {
        assertEquals(ll.toString(), Long.valueOf(i + 1), ll.get(i));
    }
}
Also used : RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) RaftClient(org.apache.ratis.client.RaftClient)

Example 79 with RaftClient

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

the class TestStateMachine method testStateMachineRegistry.

@Test
public void testStateMachineRegistry() throws Throwable {
    final Map<RaftGroupId, StateMachine> registry = new ConcurrentHashMap<>();
    registry.put(RaftGroupId.randomId(), new SimpleStateMachine4Testing());
    registry.put(RaftGroupId.randomId(), new SMTransactionContext());
    try (MiniRaftClusterWithSimulatedRpc cluster = newCluster(0)) {
        cluster.setStateMachineRegistry(registry::get);
        final RaftPeerId id = RaftPeerId.valueOf("s0");
        cluster.putNewServer(id, null, true);
        cluster.start();
        for (RaftGroupId gid : registry.keySet()) {
            final RaftGroup newGroup = RaftGroup.valueOf(gid, cluster.getPeers());
            LOG.info("add new group: " + newGroup);
            try (final RaftClient client = cluster.createClient(newGroup)) {
                for (RaftPeer p : newGroup.getPeers()) {
                    client.getGroupManagementApi(p.getId()).add(newGroup);
                }
            }
        }
        final RaftServer server = cluster.getServer(id);
        for (Map.Entry<RaftGroupId, StateMachine> e : registry.entrySet()) {
            Assert.assertSame(e.getValue(), server.getDivision(e.getKey()).getStateMachine());
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) MiniRaftClusterWithSimulatedRpc(org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RaftClient(org.apache.ratis.client.RaftClient) BaseTest(org.apache.ratis.BaseTest)

Example 80 with RaftClient

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

the class TestCounter method testSeveralCounter.

@Test
public void testSeveralCounter() throws IOException, InterruptedException {
    setAndStart(cluster);
    try (final RaftClient client = cluster.createClient()) {
        for (int i = 0; i < 10; i++) {
            client.io().send(Message.valueOf("INCREMENT"));
        }
        RaftClientReply reply1 = client.io().sendReadOnly(Message.valueOf("GET"));
        Assert.assertEquals("10", reply1.getMessage().getContent().toString(Charset.defaultCharset()));
        for (int i = 0; i < 10; i++) {
            client.io().send(Message.valueOf("INCREMENT"));
        }
        RaftClientReply reply2 = client.io().sendReadOnly(Message.valueOf("GET"));
        Assert.assertEquals("20", reply2.getMessage().getContent().toString(Charset.defaultCharset()));
        for (int i = 0; i < 10; i++) {
            client.io().send(Message.valueOf("INCREMENT"));
        }
        RaftClientReply reply3 = client.io().sendReadOnly(Message.valueOf("GET"));
        Assert.assertEquals("30", reply3.getMessage().getContent().toString(Charset.defaultCharset()));
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) 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