Search in sources :

Example 11 with FileRegion

use of io.netty.channel.FileRegion in project alluxio by Alluxio.

the class RPCMessageEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, RPCMessage in, List<Object> out) throws Exception {
    RPCRequest.Type type = in.getType();
    long bodyBytes = 0;
    DataBuffer payload = null;
    if (in.hasPayload()) {
        payload = in.getPayloadDataBuffer();
        bodyBytes = payload.getLength();
    }
    int lengthBytes = Longs.BYTES;
    int typeBytes = type.getEncodedLength();
    int messageBytes = in.getEncodedLength();
    int headerBytes = lengthBytes + typeBytes + messageBytes;
    long frameBytes = headerBytes + bodyBytes;
    // Write the header info into a buffer.
    // The format is: [frame length][message type][message][(optional) data]
    ByteBuf buffer = ctx.alloc().buffer();
    buffer.writeLong(frameBytes);
    type.encode(buffer);
    in.encode(buffer);
    // Output the header buffer.
    out.add(buffer);
    if (payload != null && bodyBytes > 0) {
        Object output = payload.getNettyOutput();
        Preconditions.checkArgument(output instanceof ByteBuf || output instanceof FileRegion, "The payload must be a ByteBuf or a FileRegion.");
        out.add(output);
    }
}
Also used : FileRegion(io.netty.channel.FileRegion) ByteBuf(io.netty.buffer.ByteBuf) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 12 with FileRegion

use of io.netty.channel.FileRegion in project reactor-netty by reactor.

the class ChannelOperationsHandler method doWrite.

ChannelFuture doWrite(Object msg, ChannelPromise promise, PublisherSender inner) {
    if (// fastpath
    flushOnEach || // last drained element
    inner == null && pendingWrites.isEmpty() || !ctx.channel().isWritable()) {
        pendingBytes = 0L;
        ChannelFuture future = ctx.write(msg, promise);
        if (flushOnEachWithEventLoop && ctx.channel().isWritable()) {
            scheduleFlush();
        } else {
            ctx.flush();
        }
        return future;
    } else {
        if (msg instanceof ByteBuf) {
            pendingBytes = Operators.addCap(pendingBytes, ((ByteBuf) msg).readableBytes());
        } else if (msg instanceof ByteBufHolder) {
            pendingBytes = Operators.addCap(pendingBytes, ((ByteBufHolder) msg).content().readableBytes());
        } else if (msg instanceof FileRegion) {
            pendingBytes = Operators.addCap(pendingBytes, ((FileRegion) msg).count());
        }
        if (log.isTraceEnabled()) {
            log.trace("{} Pending write size = {}", ctx.channel(), pendingBytes);
        }
        ChannelFuture future = ctx.write(msg, promise);
        if (!ctx.channel().isWritable()) {
            pendingBytes = 0L;
            ctx.flush();
        }
        return future;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FileRegion(io.netty.channel.FileRegion) ByteBufHolder(io.netty.buffer.ByteBufHolder) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf)

Example 13 with FileRegion

use of io.netty.channel.FileRegion in project reactor-netty by reactor.

the class NettyOutboundTest method sendFileWithoutTlsUsesFileRegion.

@Test
public void sendFileWithoutTlsUsesFileRegion() throws URISyntaxException {
    List<Class<?>> messageClasses = new ArrayList<>(2);
    EmbeddedChannel channel = new EmbeddedChannel(new MessageToMessageEncoder<FileRegion>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, FileRegion msg, List<Object> out) throws Exception {
            ByteArrayOutputStream bais = new ByteArrayOutputStream();
            WritableByteChannel wbc = Channels.newChannel(bais);
            msg.transferTo(wbc, msg.position());
            out.add(new String(bais.toByteArray(), StandardCharsets.UTF_8));
        }
    }, new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
            messageClasses.add(msg.getClass());
            ReferenceCountUtil.retain(msg);
            out.add(msg);
        }
    });
    NettyContext mockContext = () -> channel;
    NettyOutbound outbound = new NettyOutbound() {

        @Override
        public NettyContext context() {
            return mockContext;
        }

        @Override
        public FileChunkedStrategy getFileChunkedStrategy() {
            return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
        }
    };
    channel.writeOneOutbound(1);
    outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then().block();
    assertThat(channel.inboundMessages()).isEmpty();
    assertThat(channel.outboundMessages()).hasSize(2);
    assertThat(messageClasses).containsExactly(Integer.class, DefaultFileRegion.class);
    assertThat(channel.outboundMessages()).element(1).asString().startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE").endsWith("GARBAGE End of File");
    assertThat(channel.finishAndReleaseAll()).isTrue();
}
Also used : ArrayList(java.util.ArrayList) WritableByteChannel(java.nio.channels.WritableByteChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) FileRegion(io.netty.channel.FileRegion) DefaultFileRegion(io.netty.channel.DefaultFileRegion) Test(org.junit.Test)

Example 14 with FileRegion

use of io.netty.channel.FileRegion in project rocketmq by apache.

the class QueryMessageProcessor method viewMessageById.

public RemotingCommand viewMessageById(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ViewMessageRequestHeader requestHeader = (ViewMessageRequestHeader) request.decodeCommandCustomHeader(ViewMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    final SelectMappedBufferResult selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
    if (selectMappedBufferResult != null) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new OneMessageTransfer(response.encodeHeader(selectMappedBufferResult.getSize()), selectMappedBufferResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    selectMappedBufferResult.release();
                    if (!future.isSuccess()) {
                        log.error("Transfer one message from page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            selectMappedBufferResult.release();
        }
        return null;
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
    }
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ViewMessageRequestHeader(org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader) ChannelFuture(io.netty.channel.ChannelFuture) OneMessageTransfer(org.apache.rocketmq.broker.pagecache.OneMessageTransfer) FileRegion(io.netty.channel.FileRegion) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 15 with FileRegion

use of io.netty.channel.FileRegion in project rocketmq by apache.

the class FileRegionEncoderTest method testEncode.

/**
 * This unit test case ensures that {@link FileRegionEncoder} indeed wraps {@link FileRegion} to
 * {@link ByteBuf}.
 * @throws IOException if there is an error.
 */
@Test
public void testEncode() throws IOException {
    FileRegionEncoder fileRegionEncoder = new FileRegionEncoder();
    EmbeddedChannel channel = new EmbeddedChannel(fileRegionEncoder);
    File file = File.createTempFile(UUID.randomUUID().toString(), ".data");
    file.deleteOnExit();
    Random random = new Random(System.currentTimeMillis());
    int dataLength = 1 << 10;
    byte[] data = new byte[dataLength];
    random.nextBytes(data);
    write(file, data);
    FileRegion fileRegion = new DefaultFileRegion(file, 0, dataLength);
    Assert.assertEquals(0, fileRegion.transfered());
    Assert.assertEquals(dataLength, fileRegion.count());
    Assert.assertTrue(channel.writeOutbound(fileRegion));
    ByteBuf out = (ByteBuf) channel.readOutbound();
    byte[] arr = new byte[out.readableBytes()];
    out.getBytes(0, arr);
    Assert.assertArrayEquals("Data should be identical", data, arr);
}
Also used : Random(java.util.Random) DefaultFileRegion(io.netty.channel.DefaultFileRegion) FileRegion(io.netty.channel.FileRegion) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultFileRegion(io.netty.channel.DefaultFileRegion) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) Test(org.junit.Test)

Aggregations

FileRegion (io.netty.channel.FileRegion)21 ByteBuf (io.netty.buffer.ByteBuf)10 ChannelFuture (io.netty.channel.ChannelFuture)10 ChannelFutureListener (io.netty.channel.ChannelFutureListener)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)6 DefaultFileRegion (io.netty.channel.DefaultFileRegion)5 IOException (java.io.IOException)5 WritableByteChannel (java.nio.channels.WritableByteChannel)4 OneMessageTransfer (org.apache.rocketmq.broker.pagecache.OneMessageTransfer)4 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 Random (java.util.Random)2 ConsumerGroupInfo (org.apache.rocketmq.broker.client.ConsumerGroupInfo)2 ConsumerFilterData (org.apache.rocketmq.broker.filter.ConsumerFilterData)2 ExpressionForRetryMessageFilter (org.apache.rocketmq.broker.filter.ExpressionForRetryMessageFilter)2