Search in sources :

Example 56 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project LogHub by fbacchella.

the class AbstractHttpServer method addHandlers.

@Override
public void addHandlers(ChannelPipeline p) {
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpContentCompressor(9, 15, 8));
    p.addLast(new ChunkedWriteHandler());
    p.addLast(new HttpObjectAggregator(1048576));
    addModelHandlers(p);
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec)

Example 57 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project flink by apache.

the class RestServerEndpoint method start.

/**
 * Starts this REST server endpoint.
 *
 * @throws Exception if we cannot start the RestServerEndpoint
 */
public final void start() throws Exception {
    synchronized (lock) {
        Preconditions.checkState(state == State.CREATED, "The RestServerEndpoint cannot be restarted.");
        log.info("Starting rest endpoint.");
        final Router router = new Router();
        final CompletableFuture<String> restAddressFuture = new CompletableFuture<>();
        handlers = initializeHandlers(restAddressFuture);
        /* sort the handlers such that they are ordered the following:
             * /jobs
             * /jobs/overview
             * /jobs/:jobid
             * /jobs/:jobid/config
             * /:*
             */
        Collections.sort(handlers, RestHandlerUrlComparator.INSTANCE);
        checkAllEndpointsAndHandlersAreUnique(handlers);
        handlers.forEach(handler -> registerHandler(router, handler, log));
        ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws ConfigurationException {
                RouterHandler handler = new RouterHandler(router, responseHeaders);
                // SSL should be the first handler in the pipeline
                if (isHttpsEnabled()) {
                    ch.pipeline().addLast("ssl", new RedirectingSslHandler(restAddress, restAddressFuture, sslHandlerFactory));
                }
                ch.pipeline().addLast(new HttpServerCodec()).addLast(new FileUploadHandler(uploadDir)).addLast(new FlinkHttpObjectAggregator(maxContentLength, responseHeaders));
                for (InboundChannelHandlerFactory factory : inboundChannelHandlerFactories) {
                    Optional<ChannelHandler> channelHandler = factory.createHandler(configuration, responseHeaders);
                    if (channelHandler.isPresent()) {
                        ch.pipeline().addLast(channelHandler.get());
                    }
                }
                ch.pipeline().addLast(new ChunkedWriteHandler()).addLast(handler.getName(), handler).addLast(new PipelineErrorHandler(log, responseHeaders));
            }
        };
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, new ExecutorThreadFactory("flink-rest-server-netty-boss"));
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, new ExecutorThreadFactory("flink-rest-server-netty-worker"));
        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(initializer);
        Iterator<Integer> portsIterator;
        try {
            portsIterator = NetUtils.getPortRangeFromString(restBindPortRange);
        } catch (IllegalConfigurationException e) {
            throw e;
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid port range definition: " + restBindPortRange);
        }
        int chosenPort = 0;
        while (portsIterator.hasNext()) {
            try {
                chosenPort = portsIterator.next();
                final ChannelFuture channel;
                if (restBindAddress == null) {
                    channel = bootstrap.bind(chosenPort);
                } else {
                    channel = bootstrap.bind(restBindAddress, chosenPort);
                }
                serverChannel = channel.syncUninterruptibly().channel();
                break;
            } catch (final Exception e) {
                // otherwise
                if (!(e instanceof java.net.BindException)) {
                    throw e;
                }
            }
        }
        if (serverChannel == null) {
            throw new BindException("Could not start rest endpoint on any port in port range " + restBindPortRange);
        }
        log.debug("Binding rest endpoint to {}:{}.", restBindAddress, chosenPort);
        final InetSocketAddress bindAddress = (InetSocketAddress) serverChannel.localAddress();
        final String advertisedAddress;
        if (bindAddress.getAddress().isAnyLocalAddress()) {
            advertisedAddress = this.restAddress;
        } else {
            advertisedAddress = bindAddress.getAddress().getHostAddress();
        }
        port = bindAddress.getPort();
        log.info("Rest endpoint listening at {}:{}", advertisedAddress, port);
        restBaseUrl = new URL(determineProtocol(), advertisedAddress, port, "").toString();
        restAddressFuture.complete(restBaseUrl);
        state = State.RUNNING;
        startInternal();
    }
}
Also used : SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) NioServerSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) RedirectingSslHandler(org.apache.flink.runtime.net.RedirectingSslHandler) ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) BindException(java.net.BindException) URL(java.net.URL) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelInitializer(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer) NioEventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) NioServerSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel) InboundChannelHandlerFactory(org.apache.flink.runtime.io.network.netty.InboundChannelHandlerFactory) Router(org.apache.flink.runtime.rest.handler.router.Router) BindException(java.net.BindException) ServerBootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) ConfigurationException(org.apache.flink.util.ConfigurationException) BindException(java.net.BindException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) ExecutorThreadFactory(org.apache.flink.util.concurrent.ExecutorThreadFactory) PipelineErrorHandler(org.apache.flink.runtime.rest.handler.PipelineErrorHandler) RouterHandler(org.apache.flink.runtime.rest.handler.router.RouterHandler) ChunkedWriteHandler(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec)

Example 58 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project reactor-netty by reactor.

the class NettyOutboundTest method sendFileWithTlsUsesChunkedFile.

@Test
void sendFileWithTlsUsesChunkedFile() throws URISyntaxException, SSLException {
    SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    final SslHandler sslHandler = sslCtx.newHandler(ByteBufAllocator.DEFAULT);
    List<Class<?>> messageWritten = new ArrayList<>(2);
    List<Object> clearMessages = new ArrayList<>(2);
    EmbeddedChannel channel = new EmbeddedChannel(// bytes are encrypted
    sslHandler, // capture the chunks unencrypted, transform as Strings:
    new MessageToMessageEncoder<ByteBuf>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) {
            clearMessages.add(msg.readCharSequence(msg.readableBytes(), CharsetUtil.UTF_8));
            // the encoder will release the buffer, make sure it is retained for SslHandler
            out.add(msg.retain());
        }
    }, // transform the ChunkedFile into ByteBuf chunks:
    new ChunkedWriteHandler(), // helps to ensure a ChunkedFile was written outs
    new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
            messageWritten.add(msg.getClass());
            // passing the ChunkedFile through this method releases it, which is undesired
            ReferenceCountUtil.retain(msg);
            out.add(msg);
        }
    });
    Connection mockContext = () -> channel;
    NettyOutbound outbound = new NettyOutbound() {

        @Override
        public NettyOutbound sendObject(Publisher<?> dataStream, Predicate<Object> predicate) {
            return this;
        }

        @Override
        public NettyOutbound sendObject(Object message) {
            return this;
        }

        @Override
        public NettyOutbound send(Publisher<? extends ByteBuf> dataStream, Predicate<ByteBuf> predicate) {
            return this;
        }

        @Override
        public ByteBufAllocator alloc() {
            return ByteBufAllocator.DEFAULT;
        }

        @Override
        public <S> NettyOutbound sendUsing(Callable<? extends S> sourceInput, BiFunction<? super Connection, ? super S, ?> mappedInput, Consumer<? super S> sourceCleanup) {
            return then(mockSendUsing(mockContext, sourceInput, mappedInput, sourceCleanup));
        }

        @Override
        public NettyOutbound withConnection(Consumer<? super Connection> withConnection) {
            withConnection.accept(mockContext);
            return this;
        }
    };
    ChannelFuture f = channel.writeOneOutbound(1);
    try {
        outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then().block(// TODO investigate why this hangs
        Duration.ofSeconds(1));
    } catch (IllegalStateException e) {
        if (!"Timeout on blocking read for 1000 MILLISECONDS".equals(e.getMessage())) {
            throw e;
        }
        e.printStackTrace();
    }
    assertThat(messageWritten).containsExactly(Integer.class, ChunkedNioFile.class);
    assertThat(clearMessages).hasSize(2).element(0).asString().startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE").endsWith("1024 mark here ->");
    assertThat(clearMessages).element(1).asString().startsWith("<- 1024 mark here").endsWith("End of File");
    assertThat(f.isSuccess()).isFalse();
    assertThat(channel.finishAndReleaseAll()).isTrue();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Publisher(org.reactivestreams.Publisher) ByteBuf(io.netty.buffer.ByteBuf) SslHandler(io.netty.handler.ssl.SslHandler) Callable(java.util.concurrent.Callable) Predicate(java.util.function.Predicate) Consumer(java.util.function.Consumer) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) BiFunction(java.util.function.BiFunction) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.jupiter.api.Test)

Aggregations

ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)57 ChannelPipeline (io.netty.channel.ChannelPipeline)28 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)18 SslHandler (io.netty.handler.ssl.SslHandler)13 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)12 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)12 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)11 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)11 ByteBuf (io.netty.buffer.ByteBuf)10 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)10 LoggingHandler (io.netty.handler.logging.LoggingHandler)10 SocketChannel (io.netty.channel.socket.SocketChannel)8 ChannelFuture (io.netty.channel.ChannelFuture)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 Test (org.junit.Test)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)5 SslContext (io.netty.handler.ssl.SslContext)5 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)4