Search in sources :

Example 11 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project alluxio by Alluxio.

the class SnapshotReplicationManager method requestData.

private boolean requestData() {
    Preconditions.checkState(mDownloadState.get() == DownloadState.REQUEST_DATA);
    // request snapshots from the most recent to least recent
    while (!mSnapshotCandidates.isEmpty()) {
        Pair<SnapshotMetadata, RaftPeerId> candidate = mSnapshotCandidates.poll();
        SnapshotMetadata metadata = candidate.getFirst();
        RaftPeerId peerId = candidate.getSecond();
        LOG.info("Request data from follower {} for snapshot (t: {}, i: {})", peerId, metadata.getSnapshotTerm(), metadata.getSnapshotIndex());
        try {
            RaftClientReply reply = mJournalSystem.sendMessageAsync(peerId, toMessage(JournalQueryRequest.newBuilder().setSnapshotRequest(GetSnapshotRequest.getDefaultInstance()).build())).get();
            if (reply.getException() != null) {
                throw reply.getException();
            }
            return true;
        } catch (Exception e) {
            LOG.warn("Failed to request snapshot data from {}: {}", peerId, e);
        }
    }
    // return failure if there are no more candidates to ask snapshot from
    return false;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SnapshotMetadata(alluxio.grpc.SnapshotMetadata) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) CompletionException(java.util.concurrent.CompletionException) FileNotFoundException(java.io.FileNotFoundException) AbortedException(alluxio.exception.status.AbortedException) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) IOException(java.io.IOException) NotFoundException(alluxio.exception.status.NotFoundException)

Example 12 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.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) RaftClientReply(org.apache.ratis.protocol.RaftClientReply)

Example 13 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.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 14 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.StateMachineException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) IOException(java.io.IOException)

Example 15 with RaftClientReply

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

the class RaftSnapshotBaseTest method testBasicInstallSnapshot.

/**
 * Basic test for install snapshot: start a one node cluster and let it
 * generate a snapshot. Then delete the log and restart the node, and add more
 * nodes as followers.
 */
@Test
public void testBasicInstallSnapshot() throws Exception {
    List<LogPathAndIndex> logs;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        int i = 0;
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
                RaftClientReply reply = client.send(new SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        RaftStorageDirectory storageDirectory = cluster.getLeader().getState().getStorage().getStorageDir();
        final File snapshotFile = getSnapshotFile(cluster, i);
        logs = storageDirectory.getLogSegmentFiles();
        int retries = 0;
        do {
            Thread.sleep(1000);
        } while (!snapshotFile.exists() && retries++ < 10);
        Assert.assertTrue(snapshotFile + " does not exist", snapshotFile.exists());
    } finally {
        cluster.shutdown();
    }
    // delete the log segments from the leader
    for (LogPathAndIndex path : logs) {
        FileUtils.deleteFile(path.path.toFile());
    }
    // restart the peer
    LOG.info("Restarting the cluster");
    cluster.restart(false);
    try {
        assertLeaderContent(cluster);
        // generate some more traffic
        try (final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
            Assert.assertTrue(client.send(new SimpleMessage("test")).isSuccess());
        }
        // add two more peers
        MiniRaftCluster.PeerChanges change = cluster.addNewPeers(new String[] { "s3", "s4" }, true);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
    } finally {
        cluster.shutdown();
    }
}
Also used : MiniRaftCluster(org.apache.ratis.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftStorageDirectory(org.apache.ratis.server.storage.RaftStorageDirectory) LogPathAndIndex(org.apache.ratis.server.storage.RaftStorageDirectory.LogPathAndIndex) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)18 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)11 RaftClient (org.apache.ratis.client.RaftClient)9 IOException (java.io.IOException)8 InetSocketAddress (java.net.InetSocketAddress)5 ArrayList (java.util.ArrayList)5 RaftPeer (org.apache.ratis.protocol.RaftPeer)5 Test (org.junit.Test)5 File (java.io.File)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)4 ServerConfiguration (alluxio.conf.ServerConfiguration)3 JournalQueryRequest (alluxio.grpc.JournalQueryRequest)3 Map (java.util.Map)3 CompletionException (java.util.concurrent.CompletionException)3 Collectors (java.util.stream.Collectors)3 Message (org.apache.ratis.protocol.Message)3 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)3 AbortedException (alluxio.exception.status.AbortedException)2 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)2