Search in sources :

Example 1 with DataStreamPacketHeaderProto

use of org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto in project incubator-ratis by apache.

the class NettyDataStreamUtils method decodeDataStreamReplyHeader.

static DataStreamReplyHeader decodeDataStreamReplyHeader(ByteBuf buf) {
    if (DataStreamPacketHeader.getSizeOfHeaderLen() > buf.readableBytes()) {
        return null;
    }
    int headerBufLen = buf.readInt();
    if (headerBufLen > buf.readableBytes()) {
        buf.resetReaderIndex();
        return null;
    }
    try {
        ByteBuf headerBuf = buf.slice(buf.readerIndex(), headerBufLen);
        DataStreamReplyHeaderProto header = DataStreamReplyHeaderProto.parseFrom(headerBuf.nioBuffer());
        final DataStreamPacketHeaderProto h = header.getPacketHeader();
        if (header.getPacketHeader().getDataLength() + headerBufLen <= buf.readableBytes()) {
            buf.readerIndex(buf.readerIndex() + headerBufLen);
            return new DataStreamReplyHeader(ClientId.valueOf(h.getClientId()), h.getType(), h.getStreamId(), h.getStreamOffset(), h.getDataLength(), header.getBytesWritten(), header.getSuccess(), header.getCommitInfosList());
        } else {
            buf.resetReaderIndex();
            return null;
        }
    } catch (InvalidProtocolBufferException e) {
        LOG.error("Fail to decode reply header:", e);
        buf.resetReaderIndex();
        return null;
    }
}
Also used : DataStreamReplyHeader(org.apache.ratis.protocol.DataStreamReplyHeader) DataStreamPacketHeaderProto(org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) DataStreamReplyHeaderProto(org.apache.ratis.proto.RaftProtos.DataStreamReplyHeaderProto) DataStreamRequestByteBuf(org.apache.ratis.netty.server.DataStreamRequestByteBuf) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)

Example 2 with DataStreamPacketHeaderProto

use of org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto 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

DataStreamRequestByteBuf (org.apache.ratis.netty.server.DataStreamRequestByteBuf)2 DataStreamPacketHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto)2 InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)2 ByteBuf (org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)2 StandardWriteOption (org.apache.ratis.io.StandardWriteOption)1 WriteOption (org.apache.ratis.io.WriteOption)1 DataStreamReplyHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamReplyHeaderProto)1 DataStreamRequestHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamRequestHeaderProto)1 DataStreamReplyHeader (org.apache.ratis.protocol.DataStreamReplyHeader)1 DataStreamRequestHeader (org.apache.ratis.protocol.DataStreamRequestHeader)1