Search in sources :

Example 1 with ByteBuf

use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf 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 ByteBuf

use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf in project incubator-ratis by apache.

the class DataStreamManagement method readImpl.

private void readImpl(DataStreamRequestByteBuf request, ChannelHandlerContext ctx, ByteBuf buf, CheckedBiFunction<RaftClientRequest, Set<RaftPeer>, Set<DataStreamOutputRpc>, IOException> getStreams) {
    boolean close = WriteOption.containsOption(request.getWriteOptions(), StandardWriteOption.CLOSE);
    ClientInvocationId key = ClientInvocationId.valueOf(request.getClientId(), request.getStreamId());
    final StreamInfo info;
    if (request.getType() == Type.STREAM_HEADER) {
        final MemoizedSupplier<StreamInfo> supplier = JavaUtils.memoize(() -> newStreamInfo(buf, getStreams));
        info = streams.computeIfAbsent(key, id -> supplier.get());
        if (!supplier.isInitialized()) {
            streams.remove(key);
            throw new IllegalStateException("Failed to create a new stream for " + request + " since a stream already exists Key: " + key + " StreamInfo:" + info);
        }
        getMetrics().onRequestCreate(RequestType.HEADER);
    } else if (close) {
        info = Optional.ofNullable(streams.remove(key)).orElseThrow(() -> new IllegalStateException("Failed to remove StreamInfo for " + request));
    } else {
        info = Optional.ofNullable(streams.get(key)).orElseThrow(() -> {
            streams.remove(key);
            return new IllegalStateException("Failed to get StreamInfo for " + request);
        });
    }
    final CompletableFuture<Long> localWrite;
    final List<CompletableFuture<DataStreamReply>> remoteWrites;
    if (request.getType() == Type.STREAM_HEADER) {
        localWrite = CompletableFuture.completedFuture(0L);
        remoteWrites = Collections.emptyList();
    } else if (request.getType() == Type.STREAM_DATA) {
        localWrite = info.getLocal().write(buf, request.getWriteOptions(), writeExecutor);
        remoteWrites = info.applyToRemotes(out -> out.write(request, requestExecutor));
    } else {
        throw new IllegalStateException(this + ": Unexpected type " + request.getType() + ", request=" + request);
    }
    composeAsync(info.getPrevious(), requestExecutor, n -> JavaUtils.allOf(remoteWrites).thenCombineAsync(localWrite, (v, bytesWritten) -> {
        if (request.getType() == Type.STREAM_HEADER || (request.getType() == Type.STREAM_DATA && !close)) {
            sendReply(remoteWrites, request, bytesWritten, info.getCommitInfos(), ctx);
        } else if (close) {
            if (info.isPrimary()) {
                // after all server close stream, primary server start transaction
                startTransaction(info, request, bytesWritten, ctx);
            } else {
                sendReply(remoteWrites, request, bytesWritten, info.getCommitInfos(), ctx);
            }
        } else {
            throw new IllegalStateException(this + ": Unexpected type " + request.getType() + ", request=" + request);
        }
        return null;
    }, requestExecutor)).whenComplete((v, exception) -> {
        try {
            if (exception != null) {
                streams.remove(key);
                replyDataStreamException(server, exception, info.getRequest(), request, ctx);
            }
        } finally {
            buf.release();
        }
    });
}
Also used : RoutingTable(org.apache.ratis.protocol.RoutingTable) RequestContext(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) RaftConfiguration(org.apache.ratis.server.RaftConfiguration) DataStreamReplyByteBuffer(org.apache.ratis.datastream.impl.DataStreamReplyByteBuffer) WriteOption(org.apache.ratis.io.WriteOption) JavaUtils(org.apache.ratis.util.JavaUtils) MemoizedSupplier(org.apache.ratis.util.MemoizedSupplier) RaftPeer(org.apache.ratis.protocol.RaftPeer) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) DataChannel(org.apache.ratis.statemachine.StateMachine.DataChannel) ClientProtoUtils(org.apache.ratis.client.impl.ClientProtoUtils) List(java.util.List) ClientId(org.apache.ratis.protocol.ClientId) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) NettyServerStreamRpcMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics) Optional(java.util.Optional) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DataStreamOutputRpc(org.apache.ratis.client.DataStreamOutputRpc) Preconditions(org.apache.ratis.util.Preconditions) ChannelHandlerContext(org.apache.ratis.thirdparty.io.netty.channel.ChannelHandlerContext) StandardWriteOption(org.apache.ratis.io.StandardWriteOption) RequestMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DataStreamException(org.apache.ratis.protocol.exceptions.DataStreamException) ConcurrentMap(java.util.concurrent.ConcurrentMap) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftClientRequestProto(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) AsyncRpcApi(org.apache.ratis.client.AsyncRpcApi) ConcurrentUtils(org.apache.ratis.util.ConcurrentUtils) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf) DataStream(org.apache.ratis.statemachine.StateMachine.DataStream) Logger(org.slf4j.Logger) CheckedBiFunction(org.apache.ratis.util.function.CheckedBiFunction) Executor(java.util.concurrent.Executor) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) IOException(java.io.IOException) Type(org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto.Type) Division(org.apache.ratis.server.RaftServer.Division) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) RequestType(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestType) RaftServer(org.apache.ratis.server.RaftServer) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId)

Example 3 with ByteBuf

use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf in project incubator-ratis by apache.

the class DataStreamManagement method read.

void read(DataStreamRequestByteBuf request, ChannelHandlerContext ctx, CheckedBiFunction<RaftClientRequest, Set<RaftPeer>, Set<DataStreamOutputRpc>, IOException> getStreams) {
    LOG.debug("{}: read {}", this, request);
    final ByteBuf buf = request.slice();
    try {
        readImpl(request, ctx, buf, getStreams);
    } catch (Throwable t) {
        buf.release();
        throw t;
    }
}
Also used : ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)

Example 4 with ByteBuf

use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf in project incubator-ratis by apache.

the class LoadGen method write.

long write(FileChannel in, long offset, FileStoreClient fileStoreClient, String path, List<CompletableFuture<Long>> futures) throws IOException {
    final int bufferSize = getBufferSizeInBytes();
    final ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(bufferSize);
    final int bytesRead = buf.writeBytes(in, bufferSize);
    if (bytesRead < 0) {
        throw new IllegalStateException("Failed to read " + bufferSize + " byte(s) from " + this + ". The channel has reached end-of-stream at " + offset);
    } else if (bytesRead > 0) {
        final CompletableFuture<Long> f = fileStoreClient.writeAsync(path, offset, offset + bytesRead == getFileSizeInBytes(), buf.nioBuffer(), sync == 1);
        f.thenRun(buf::release);
        futures.add(f);
    }
    return bytesRead;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)

Example 5 with ByteBuf

use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf in project incubator-ratis by apache.

the class RequestDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    if (msg.readableBytes() >= 8) {
        int id = msg.readInt();
        int buflen = msg.readInt();
        if (msg.readableBytes() >= buflen) {
            RequestData req = new RequestData();
            req.setDataId(id);
            // System.out.printf("msg id and buflen %d and %d bytes\n", id, buflen, msg.readableBytes());
            try {
                ByteBuf bf = msg.slice(msg.readerIndex(), buflen);
                req.setBuff(bf.nioBuffer());
            } catch (Exception e) {
                System.out.println(e);
            }
            msg.readerIndex(msg.readerIndex() + buflen);
            msg.markReaderIndex();
            out.add(req);
        } else {
            msg.resetReaderIndex();
            return;
        }
    } else {
        return;
    }
}
Also used : RequestData(org.apache.ratis.experiments.nettyzerocopy.objects.RequestData) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)9 DataStreamRequestByteBuf (org.apache.ratis.netty.server.DataStreamRequestByteBuf)4 ByteBuffer (java.nio.ByteBuffer)3 DataStreamReplyByteBuffer (org.apache.ratis.datastream.impl.DataStreamReplyByteBuffer)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 DataStreamRequestByteBuffer (org.apache.ratis.datastream.impl.DataStreamRequestByteBuffer)2 StandardWriteOption (org.apache.ratis.io.StandardWriteOption)2 WriteOption (org.apache.ratis.io.WriteOption)2 DataStreamPacketHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 CompletionException (java.util.concurrent.CompletionException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Executor (java.util.concurrent.Executor)1