Search in sources :

Example 11 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class FileStore method read.

CompletableFuture<ReadReplyProto> read(String relative, long offset, long length, boolean readCommitted) {
    final Supplier<String> name = () -> "read(" + relative + ", " + offset + ", " + length + ") @" + getId();
    final CheckedSupplier<ReadReplyProto, IOException> task = LogUtils.newCheckedSupplier(LOG, () -> {
        final FileInfo info = files.get(relative);
        final ReadReplyProto.Builder reply = ReadReplyProto.newBuilder().setResolvedPath(FileStoreCommon.toByteString(info.getRelativePath())).setOffset(offset);
        final ByteString bytes = info.read(this::resolve, offset, length, readCommitted);
        return reply.setData(bytes).build();
    }, name);
    return submit(task, reader);
}
Also used : ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) ReadReplyProto(org.apache.ratis.proto.ExamplesProtos.ReadReplyProto) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 12 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class MessageStreamImpl method streamAsync.

@Override
public CompletableFuture<RaftClientReply> streamAsync(Message message, SizeInBytes subSize) {
    final int n = subSize.getSizeInt();
    final MessageOutputStream out = stream();
    final ByteString bytes = message.getContent();
    for (int i = 0; i < bytes.size(); ) {
        final int j = Math.min(i + n, bytes.size());
        final ByteString sub = bytes.substring(i, j);
        out.sendAsync(Message.valueOf(sub));
        i = j;
    }
    return out.closeAsync();
}
Also used : MessageOutputStream(org.apache.ratis.client.api.MessageOutputStream) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString)

Example 13 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class RaftAsyncTests method runTestStaleReadAsync.

void runTestStaleReadAsync(CLUSTER cluster) throws Exception {
    final int numMesssages = 10;
    try (RaftClient client = cluster.createClient()) {
        RaftTestUtil.waitForLeader(cluster);
        // submit some messages
        final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
        for (int i = 0; i < numMesssages; i++) {
            final String s = "" + i;
            LOG.info("sendAsync " + s);
            futures.add(client.async().send(new SimpleMessage(s)));
        }
        Assert.assertEquals(numMesssages, futures.size());
        final List<RaftClientReply> replies = new ArrayList<>();
        for (CompletableFuture<RaftClientReply> f : futures) {
            final RaftClientReply r = f.join();
            Assert.assertTrue(r.isSuccess());
            replies.add(r);
        }
        futures.clear();
        // Use a follower with the max commit index
        final RaftClientReply lastWriteReply = replies.get(replies.size() - 1);
        final RaftPeerId leader = lastWriteReply.getServerId();
        LOG.info("leader = " + leader);
        final Collection<CommitInfoProto> commitInfos = lastWriteReply.getCommitInfos();
        LOG.info("commitInfos = " + commitInfos);
        final CommitInfoProto followerCommitInfo = commitInfos.stream().filter(info -> !RaftPeerId.valueOf(info.getServer().getId()).equals(leader)).max(Comparator.comparing(CommitInfoProto::getCommitIndex)).get();
        final RaftPeerId follower = RaftPeerId.valueOf(followerCommitInfo.getServer().getId());
        final long followerCommitIndex = followerCommitInfo.getCommitIndex();
        LOG.info("max follower = {}, commitIndex = {}", follower, followerCommitIndex);
        // test a failure case
        testFailureCaseAsync("sendStaleReadAsync(..) with a larger commit index", () -> client.async().sendStaleRead(new SimpleMessage("" + Long.MAX_VALUE), followerCommitInfo.getCommitIndex(), follower), StateMachineException.class, IndexOutOfBoundsException.class);
        // test sendStaleReadAsync
        for (int i = 0; i < numMesssages; i++) {
            final RaftClientReply reply = replies.get(i);
            final String query = "" + i;
            LOG.info("query=" + query + ", reply=" + reply);
            final Message message = new SimpleMessage(query);
            final CompletableFuture<RaftClientReply> readFuture = client.async().sendReadOnly(message);
            futures.add(readFuture.thenCompose(r -> {
                if (reply.getLogIndex() <= followerCommitIndex) {
                    LOG.info("sendStaleReadAsync, query=" + query);
                    return client.async().sendStaleRead(message, followerCommitIndex, follower);
                } else {
                    return CompletableFuture.completedFuture(null);
                }
            }).thenApply(staleReadReply -> {
                if (staleReadReply == null) {
                    return null;
                }
                final ByteString expected = readFuture.join().getMessage().getContent();
                final ByteString computed = staleReadReply.getMessage().getContent();
                try {
                    LOG.info("query " + query + " returns " + LogEntryProto.parseFrom(expected).getStateMachineLogEntry().getLogData().toStringUtf8());
                } catch (InvalidProtocolBufferException e) {
                    throw new CompletionException(e);
                }
                Assert.assertEquals("log entry mismatch for query=" + query, expected, computed);
                return null;
            }));
        }
        JavaUtils.allOf(futures).join();
    }
}
Also used : RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicies(org.apache.ratis.retry.RetryPolicies) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) ArrayList(java.util.ArrayList) Log4jUtils(org.apache.ratis.util.Log4jUtils) Message(org.apache.ratis.protocol.Message) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) CheckedRunnable(org.apache.ratis.util.function.CheckedRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) Level(org.apache.log4j.Level) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) StreamSupport(java.util.stream.StreamSupport) JavaUtils(org.apache.ratis.util.JavaUtils) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) StateMachine(org.apache.ratis.statemachine.StateMachine) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RetryLimited(org.apache.ratis.retry.RetryPolicies.RetryLimited) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) Comparator(java.util.Comparator) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) DelayLocalExecutionInjection(org.apache.ratis.server.impl.DelayLocalExecutionInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) Message(org.apache.ratis.protocol.Message) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) CompletionException(java.util.concurrent.CompletionException) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 14 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class FileChunkReader method readFileChunk.

/**
 * Read the next chunk.
 *
 * @param chunkMaxSize maximum chunk size
 * @return the chunk read from the file.
 * @throws IOException if it failed to read the file.
 */
public FileChunkProto readFileChunk(int chunkMaxSize) throws IOException {
    final long remaining = info.getFileSize() - offset;
    final int chunkLength = remaining < chunkMaxSize ? (int) remaining : chunkMaxSize;
    final ByteString data = ByteString.readFrom(in, chunkLength);
    final FileChunkProto proto = FileChunkProto.newBuilder().setFilename(relativePath.toString()).setOffset(offset).setChunkIndex(chunkIndex).setDone(offset + chunkLength == info.getFileSize()).setData(data).setFileDigest(ByteString.copyFrom(info.getFileDigest().getDigest())).build();
    chunkIndex++;
    offset += chunkLength;
    return proto;
}
Also used : ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) FileChunkProto(org.apache.ratis.proto.RaftProtos.FileChunkProto)

Example 15 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class DataStreamClientImpl method stream.

@Override
public DataStreamOutputRpc stream(ByteBuffer headerMessage, RoutingTable routingTable) {
    final Message message = Optional.ofNullable(headerMessage).map(ByteString::copyFrom).map(Message::valueOf).orElse(null);
    RaftClientRequest request = RaftClientRequest.newBuilder().setClientId(clientId).setServerId(dataStreamServer.getId()).setGroupId(groupId).setCallId(CallId.getAndIncrement()).setMessage(message).setType(RaftClientRequest.dataStreamRequestType()).setRoutingTable(routingTable).build();
    return new DataStreamOutputImpl(request);
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString)

Aggregations

ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)19 FileStoreRequestProto (org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto)4 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)4 InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)4 Test (org.junit.Test)4 BaseTest (org.apache.ratis.BaseTest)3 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)3 RaftClient (org.apache.ratis.client.RaftClient)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 WriteRequestHeaderProto (org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto)2 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)2 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)2 TimeDuration (org.apache.ratis.util.TimeDuration)2 Gauge (com.codahale.metrics.Gauge)1 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Random (java.util.Random)1