Search in sources :

Example 51 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class TransferLeadership method start.

CompletableFuture<RaftClientReply> start(TransferLeadershipRequest request) {
    final MemoizedSupplier<PendingRequest> supplier = JavaUtils.memoize(() -> new PendingRequest(request));
    final PendingRequest previous = pending.getAndUpdate(f -> f != null ? f : supplier.get());
    if (previous != null) {
        if (request.getNewLeader().equals(previous.getRequest().getNewLeader())) {
            final CompletableFuture<RaftClientReply> replyFuture = new CompletableFuture<>();
            previous.getReplyFuture().whenComplete((r, e) -> {
                if (e != null) {
                    replyFuture.completeExceptionally(e);
                } else {
                    replyFuture.complete(r.isSuccess() ? server.newSuccessReply(request) : server.newExceptionReply(request, r.getException()));
                }
            });
            return replyFuture;
        } else {
            final TransferLeadershipException tle = new TransferLeadershipException(server.getMemberId() + "Failed to transfer leadership to " + request.getNewLeader() + ": a previous " + previous + " exists");
            return CompletableFuture.completedFuture(server.newExceptionReply(request, tle));
        }
    }
    scheduler.onTimeout(TimeDuration.valueOf(request.getTimeoutMs(), TimeUnit.MILLISECONDS), () -> finish(server.getState().getLeaderId(), true), LOG, () -> "Timeout check failed for append entry request: " + request);
    return supplier.get().getReplyFuture();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException)

Example 52 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply 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)

Example 53 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class DataStreamManagement method replyDataStreamException.

static void replyDataStreamException(RaftServer server, Throwable cause, RaftClientRequest raftClientRequest, DataStreamRequestByteBuf request, ChannelHandlerContext ctx) {
    final RaftClientReply reply = RaftClientReply.newBuilder().setRequest(raftClientRequest).setException(new DataStreamException(server.getId(), cause)).build();
    sendDataStreamException(cause, request, reply, ctx);
}
Also used : DataStreamException(org.apache.ratis.protocol.exceptions.DataStreamException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply)

Example 54 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class DataStreamAsyncClusterTests method runTestDataStream.

void runTestDataStream(CLUSTER cluster, boolean stepDownLeader) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final List<CompletableFuture<Long>> futures = new ArrayList<>();
    futures.add(CompletableFuture.supplyAsync(() -> runTestDataStream(cluster, 5, 10, 1_000_000, 10, stepDownLeader), executor));
    futures.add(CompletableFuture.supplyAsync(() -> runTestDataStream(cluster, 2, 20, 1_000, 5_000, stepDownLeader), executor));
    final long maxIndex = futures.stream().map(CompletableFuture::join).max(Long::compareTo).orElseThrow(IllegalStateException::new);
    if (stepDownLeader) {
        final RaftPeerId oldLeader = cluster.getLeader().getId();
        final CompletableFuture<RaftPeerId> changeLeader = futures.get(0).thenApplyAsync(dummy -> {
            try {
                return RaftTestUtil.changeLeader(cluster, oldLeader);
            } catch (Exception e) {
                throw new CompletionException("Failed to change leader from " + oldLeader, e);
            }
        });
        LOG.info("Changed leader from {} to {}", oldLeader, changeLeader.join());
    }
    // wait for all servers to catch up
    try (RaftClient client = cluster.createClient()) {
        RaftClientReply reply = client.async().watch(maxIndex, ReplicationLevel.ALL).join();
        Assert.assertTrue(reply.isSuccess());
    }
    // assert all streams are linked
    for (RaftServer proxy : cluster.getServers()) {
        final RaftServer.Division impl = proxy.getDivision(cluster.getGroupId());
        final MultiDataStreamStateMachine stateMachine = (MultiDataStreamStateMachine) impl.getStateMachine();
        for (SingleDataStream s : stateMachine.getStreams()) {
            Assert.assertFalse(s.getDataChannel().isOpen());
            DataStreamTestUtils.assertLogEntry(impl, s);
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) MultiDataStreamStateMachine(org.apache.ratis.datastream.DataStreamTestUtils.MultiDataStreamStateMachine) CompletionException(java.util.concurrent.CompletionException) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) SingleDataStream(org.apache.ratis.datastream.DataStreamTestUtils.SingleDataStream) RaftClient(org.apache.ratis.client.RaftClient)

Example 55 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class DataStreamClusterTests method runTestWriteFile.

void runTestWriteFile(CLUSTER cluster, int i, CheckedConsumer<DataStreamOutputImpl, Exception> testCase) throws Exception {
    final RaftClientRequest request;
    final CompletableFuture<RaftClientReply> reply;
    final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
    try (RaftClient client = cluster.createClient(primaryServer)) {
        try (final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer))) {
            request = out.getHeader();
            reply = out.getRaftClientReplyFuture();
            final Timestamp start = Timestamp.currentTime();
            testCase.accept(out);
            LOG.info("{}: {} elapsed {}ms", i, testCase, start.elapsedTimeMs());
        }
    }
    watchOrSleep(cluster, reply.join().getLogIndex());
    assertLogEntry(cluster, request);
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftPeer(org.apache.ratis.protocol.RaftPeer) Timestamp(org.apache.ratis.util.Timestamp) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)96 RaftClient (org.apache.ratis.client.RaftClient)71 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)43 RaftServer (org.apache.ratis.server.RaftServer)40 IOException (java.io.IOException)32 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)25 CompletableFuture (java.util.concurrent.CompletableFuture)22 RaftPeer (org.apache.ratis.protocol.RaftPeer)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 TimeDuration (org.apache.ratis.util.TimeDuration)18 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)14 RaftTestUtil (org.apache.ratis.RaftTestUtil)13 RaftProperties (org.apache.ratis.conf.RaftProperties)13 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 List (java.util.List)12 RetryPolicy (org.apache.ratis.retry.RetryPolicy)12 CompletionException (java.util.concurrent.CompletionException)11 ExecutionException (java.util.concurrent.ExecutionException)11