Search in sources :

Example 6 with InvalidProtocolBufferException

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

the class FileStoreStateMachine method write.

@Override
public CompletableFuture<Integer> write(LogEntryProto entry) {
    final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
    final ByteString data = smLog.getLogData();
    final FileStoreRequestProto proto;
    try {
        proto = FileStoreRequestProto.parseFrom(data);
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally(entry.getIndex(), "Failed to parse data, entry=" + entry, e);
    }
    if (proto.getRequestCase() != FileStoreRequestProto.RequestCase.WRITEHEADER) {
        return null;
    }
    final WriteRequestHeaderProto h = proto.getWriteHeader();
    final CompletableFuture<Integer> f = files.write(entry.getIndex(), h.getPath().toStringUtf8(), h.getClose(), h.getSync(), h.getOffset(), smLog.getStateMachineEntry().getStateMachineData());
    // sync only if closing the file
    return h.getClose() ? f : null;
}
Also used : FileStoreRequestProto(org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) WriteRequestHeaderProto(org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)

Example 7 with InvalidProtocolBufferException

use of org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException 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 8 with InvalidProtocolBufferException

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

the class NettyDataStreamUtils method decodeDataStreamRequestHeader.

static DataStreamRequestHeader decodeDataStreamRequestHeader(ByteBuf buf) {
    if (DataStreamPacketHeader.getSizeOfHeaderBodyLen() > buf.readableBytes()) {
        return null;
    }
    long headerBodyBufLen = buf.readLong();
    if (headerBodyBufLen > buf.readableBytes()) {
        buf.resetReaderIndex();
        return null;
    }
    int headerBufLen = buf.readInt();
    if (headerBufLen > buf.readableBytes()) {
        buf.resetReaderIndex();
        return null;
    }
    try {
        ByteBuf headerBuf = buf.slice(buf.readerIndex(), headerBufLen);
        DataStreamRequestHeaderProto header = DataStreamRequestHeaderProto.parseFrom(headerBuf.nioBuffer());
        final DataStreamPacketHeaderProto h = header.getPacketHeader();
        if (h.getDataLength() + headerBufLen <= buf.readableBytes()) {
            buf.readerIndex(buf.readerIndex() + headerBufLen);
            WriteOption[] options = new WriteOption[h.getOptionsCount()];
            for (int i = 0; i < options.length; i++) {
                options[i] = StandardWriteOption.values()[h.getOptions(i).ordinal()];
            }
            return new DataStreamRequestHeader(ClientId.valueOf(h.getClientId()), h.getType(), h.getStreamId(), h.getStreamOffset(), h.getDataLength(), options);
        } else {
            buf.resetReaderIndex();
            return null;
        }
    } catch (InvalidProtocolBufferException e) {
        LOG.error("Fail to decode request header:", e);
        buf.resetReaderIndex();
        return null;
    }
}
Also used : DataStreamPacketHeaderProto(org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto) DataStreamRequestHeaderProto(org.apache.ratis.proto.RaftProtos.DataStreamRequestHeaderProto) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) DataStreamRequestHeader(org.apache.ratis.protocol.DataStreamRequestHeader) DataStreamRequestByteBuf(org.apache.ratis.netty.server.DataStreamRequestByteBuf) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf) StandardWriteOption(org.apache.ratis.io.StandardWriteOption) WriteOption(org.apache.ratis.io.WriteOption)

Aggregations

InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)8 ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)5 FileStoreRequestProto (org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto)4 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)3 DataStreamRequestByteBuf (org.apache.ratis.netty.server.DataStreamRequestByteBuf)2 WriteRequestHeaderProto (org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto)2 DataStreamPacketHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto)2 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 StreamSupport (java.util.stream.StreamSupport)1