Search in sources :

Example 46 with RaftClientReply

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

the class GroupManagementImpl method list.

@Override
public GroupListReply list() throws IOException {
    final long callId = CallId.getAndIncrement();
    final RaftClientReply reply = client.io().sendRequestWithRetry(() -> new GroupListRequest(client.getId(), server, client.getGroupId(), callId));
    Preconditions.assertTrue(reply instanceof GroupListReply, () -> "Unexpected reply: " + reply);
    return (GroupListReply) reply;
}
Also used : GroupListRequest(org.apache.ratis.protocol.GroupListRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) GroupListReply(org.apache.ratis.protocol.GroupListReply)

Example 47 with RaftClientReply

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

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

the class FileStoreClient method send.

static ByteString send(ByteString request, CheckedFunction<Message, RaftClientReply, IOException> sendFunction) throws IOException {
    final RaftClientReply reply = sendFunction.apply(Message.valueOf(request));
    final StateMachineException sme = reply.getStateMachineException();
    if (sme != null) {
        throw new IOException("Failed to send request " + request, sme);
    }
    Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
    return reply.getMessage().getContent();
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) IOException(java.io.IOException)

Example 49 with RaftClientReply

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

the class Get method operation.

@Override
protected void operation(RaftClient client) throws IOException {
    RaftClientReply getValue = client.io().sendReadOnly(Expression.Utils.toMessage(new Variable(name)));
    Expression response = Expression.Utils.bytes2Expression(getValue.getMessage().getContent().toByteArray(), 0);
    System.out.println(String.format("%s=%s", name, (DoubleValue) response).toString());
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Expression(org.apache.ratis.examples.arithmetic.expression.Expression)

Example 50 with RaftClientReply

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

the class Assign method operation.

@Override
protected void operation(RaftClient client) throws IOException {
    RaftClientReply send = client.io().send(new AssignmentMessage(new Variable(name), createExpression(value)));
    System.out.println("Success: " + send.isSuccess());
    System.out.println("Response: " + send.getMessage().getClass());
}
Also used : AssignmentMessage(org.apache.ratis.examples.arithmetic.AssignmentMessage) Variable(org.apache.ratis.examples.arithmetic.expression.Variable) RaftClientReply(org.apache.ratis.protocol.RaftClientReply)

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