Search in sources :

Example 1 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project java by wavefrontHQ.

the class StreamIngester method run.

public void run() {
    activeListeners.inc();
    // Configure the server.
    ServerBootstrap b = new ServerBootstrap();
    EventLoopGroup parentGroup;
    EventLoopGroup childGroup;
    Class<? extends ServerChannel> socketChannelClass;
    if (Epoll.isAvailable()) {
        logger.fine("Using native socket transport for port " + listeningPort);
        parentGroup = new EpollEventLoopGroup(1);
        childGroup = new EpollEventLoopGroup();
        socketChannelClass = EpollServerSocketChannel.class;
    } else {
        logger.fine("Using NIO socket transport for port " + listeningPort);
        parentGroup = new NioEventLoopGroup(1);
        childGroup = new NioEventLoopGroup();
        socketChannelClass = NioServerSocketChannel.class;
    }
    try {
        b.group(parentGroup, childGroup).channel(socketChannelClass).option(ChannelOption.SO_BACKLOG, 1024).localAddress(listeningPort).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("frame decoder", frameDecoderFactory.getDecoder());
                pipeline.addLast("byte array decoder", new ByteArrayDecoder());
                pipeline.addLast(commandHandler);
            }
        });
        if (parentChannelOptions != null) {
            for (Map.Entry<ChannelOption<?>, ?> entry : parentChannelOptions.entrySet()) {
                b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
            }
        }
        if (childChannelOptions != null) {
            for (Map.Entry<ChannelOption<?>, ?> entry : childChannelOptions.entrySet()) {
                b.childOption((ChannelOption<Object>) entry.getKey(), entry.getValue());
            }
        }
        // Start the server.
        ChannelFuture f = b.bind().sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } catch (final InterruptedException e) {
        logger.log(Level.WARNING, "Interrupted");
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
        logger.info("Listener on port " + String.valueOf(listeningPort) + " shut down");
    } catch (Exception e) {
        // ChannelFuture throws undeclared checked exceptions, so we need to handle it
        if (e instanceof BindException) {
            logger.severe("Unable to start listener - port " + String.valueOf(listeningPort) + " is already in use!");
        } else {
            logger.log(Level.SEVERE, "StreamIngester exception: ", e);
        }
    } finally {
        activeListeners.dec();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ChannelOption(io.netty.channel.ChannelOption) BindException(java.net.BindException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) BindException(java.net.BindException) ChannelPipeline(io.netty.channel.ChannelPipeline) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ByteArrayDecoder(io.netty.handler.codec.bytes.ByteArrayDecoder)

Example 2 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project netty by netty.

the class AbstractBootstrapConfig method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder().append(StringUtil.simpleClassName(this)).append('(');
    EventLoopGroup group = group();
    if (group != null) {
        buf.append("group: ").append(StringUtil.simpleClassName(group)).append(", ");
    }
    @SuppressWarnings("deprecation") ChannelFactory<? extends C> factory = channelFactory();
    if (factory != null) {
        buf.append("channelFactory: ").append(factory).append(", ");
    }
    SocketAddress localAddress = localAddress();
    if (localAddress != null) {
        buf.append("localAddress: ").append(localAddress).append(", ");
    }
    Map<ChannelOption<?>, Object> options = options();
    if (!options.isEmpty()) {
        buf.append("options: ").append(options).append(", ");
    }
    Map<AttributeKey<?>, Object> attrs = attrs();
    if (!attrs.isEmpty()) {
        buf.append("attrs: ").append(attrs).append(", ");
    }
    ChannelHandler handler = handler();
    if (handler != null) {
        buf.append("handler: ").append(handler).append(", ");
    }
    if (buf.charAt(buf.length() - 1) == '(') {
        buf.append(')');
    } else {
        buf.setCharAt(buf.length() - 2, ')');
        buf.setLength(buf.length() - 1);
    }
    return buf.toString();
}
Also used : AttributeKey(io.netty.util.AttributeKey) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelOption(io.netty.channel.ChannelOption) ChannelHandler(io.netty.channel.ChannelHandler) SocketAddress(java.net.SocketAddress)

Example 3 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project zuul by Netflix.

the class Server method setupServerBootstrap.

private ChannelFuture setupServerBootstrap(NamedSocketAddress listenAddress, ChannelInitializer<?> channelInitializer) throws InterruptedException {
    ServerBootstrap serverBootstrap = new ServerBootstrap().group(serverGroup.clientToProxyBossPool, serverGroup.clientToProxyWorkerPool);
    // Choose socket options.
    Map<ChannelOption<?>, Object> channelOptions = new HashMap<>();
    channelOptions.put(ChannelOption.SO_BACKLOG, 128);
    channelOptions.put(ChannelOption.SO_LINGER, -1);
    channelOptions.put(ChannelOption.TCP_NODELAY, true);
    channelOptions.put(ChannelOption.SO_KEEPALIVE, true);
    LOG.info("Proxy listening with " + serverGroup.channelType);
    serverBootstrap.channel(serverGroup.channelType);
    // Apply socket options.
    for (Map.Entry<ChannelOption<?>, ?> optionEntry : channelOptions.entrySet()) {
        serverBootstrap = serverBootstrap.option((ChannelOption) optionEntry.getKey(), optionEntry.getValue());
    }
    // Apply transport specific socket options.
    for (Map.Entry<ChannelOption<?>, ?> optionEntry : serverGroup.transportChannelOptions.entrySet()) {
        serverBootstrap = serverBootstrap.option((ChannelOption) optionEntry.getKey(), optionEntry.getValue());
    }
    serverBootstrap.handler(new NewConnHandler());
    serverBootstrap.childHandler(channelInitializer);
    serverBootstrap.validate();
    LOG.info("Binding to : " + listenAddress);
    if (MANUAL_DISCOVERY_STATUS.get()) {
        // Flag status as UP just before binding to the port.
        serverStatusManager.localStatus(InstanceInfo.InstanceStatus.UP);
    }
    // Bind and start to accept incoming connections.
    ChannelFuture bindFuture = serverBootstrap.bind(listenAddress.unwrap());
    try {
        return bindFuture.sync();
    } catch (Exception e) {
        // failure, which is typically an IOException.  Just chain it and rethrow.
        throw new RuntimeException("Failed to bind on addr " + listenAddress, e);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOption(io.netty.channel.ChannelOption) EpollChannelOption(io.netty.channel.epoll.EpollChannelOption) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.

the class NettyClientTransport method start.

@SuppressWarnings("unchecked")
@Override
public Runnable start(Listener transportListener) {
    lifecycleManager = new ClientTransportLifecycleManager(Preconditions.checkNotNull(transportListener, "listener"));
    EventLoop eventLoop = group.next();
    if (keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED) {
        keepAliveManager = new KeepAliveManager(new ClientKeepAlivePinger(this), eventLoop, keepAliveTimeNanos, keepAliveTimeoutNanos, keepAliveWithoutCalls);
    }
    handler = NettyClientHandler.newHandler(lifecycleManager, keepAliveManager, autoFlowControl, flowControlWindow, maxHeaderListSize, GrpcUtil.STOPWATCH_SUPPLIER, tooManyPingsRunnable, transportTracer, eagAttributes, authorityString, channelLogger);
    ChannelHandler negotiationHandler = negotiator.newHandler(handler);
    Bootstrap b = new Bootstrap();
    b.option(ALLOCATOR, Utils.getByteBufAllocator(false));
    b.group(eventLoop);
    b.channelFactory(channelFactory);
    // For non-socket based channel, the option will be ignored.
    b.option(SO_KEEPALIVE, true);
    // For non-epoll based channel, the option will be ignored.
    if (keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED) {
        ChannelOption<Integer> tcpUserTimeout = Utils.maybeGetTcpUserTimeoutOption();
        if (tcpUserTimeout != null) {
            b.option(tcpUserTimeout, (int) TimeUnit.NANOSECONDS.toMillis(keepAliveTimeoutNanos));
        }
    }
    for (Map.Entry<ChannelOption<?>, ?> entry : channelOptions.entrySet()) {
        // Every entry in the map is obtained from
        // NettyChannelBuilder#withOption(ChannelOption<T> option, T value)
        // so it is safe to pass the key-value pair to b.option().
        b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
    }
    ChannelHandler bufferingHandler = new WriteBufferingAndExceptionHandler(negotiationHandler);
    /**
     * We don't use a ChannelInitializer in the client bootstrap because its "initChannel" method
     * is executed in the event loop and we need this handler to be in the pipeline immediately so
     * that it may begin buffering writes.
     */
    b.handler(bufferingHandler);
    ChannelFuture regFuture = b.register();
    if (regFuture.isDone() && !regFuture.isSuccess()) {
        channel = null;
        // Initialization has failed badly. All new streams should be made to fail.
        Throwable t = regFuture.cause();
        if (t == null) {
            t = new IllegalStateException("Channel is null, but future doesn't have a cause");
        }
        statusExplainingWhyTheChannelIsNull = Utils.statusFromThrowable(t);
        // Use a Runnable since lifecycleManager calls transportListener
        return new Runnable() {

            @Override
            public void run() {
                // NOTICE: we not are calling lifecycleManager from the event loop. But there isn't really
                // an event loop in this case, so nothing should be accessing the lifecycleManager. We
                // could use GlobalEventExecutor (which is what regFuture would use for notifying
                // listeners in this case), but avoiding on-demand thread creation in an error case seems
                // a good idea and is probably clearer threading.
                lifecycleManager.notifyTerminated(statusExplainingWhyTheChannelIsNull);
            }
        };
    }
    channel = regFuture.channel();
    // Start the write queue as soon as the channel is constructed
    handler.startWriteQueue(channel);
    // This write will have no effect, yet it will only complete once the negotiationHandler
    // flushes any pending writes. We need it to be staged *before* the `connect` so that
    // the channel can't have been closed yet, removing all handlers. This write will sit in the
    // AbstractBufferingHandler's buffer, and will either be flushed on a successful connection,
    // or failed if the connection fails.
    channel.writeAndFlush(NettyClientHandler.NOOP_MESSAGE).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                // Need to notify of this failure, because NettyClientHandler may not have been added to
                // the pipeline before the error occurred.
                lifecycleManager.notifyTerminated(Utils.statusFromThrowable(future.cause()));
            }
        }
    });
    // Start the connection operation to the server.
    SocketAddress localAddress = localSocketPicker.createSocketAddress(remoteAddress, eagAttributes);
    if (localAddress != null) {
        channel.connect(remoteAddress, localAddress);
    } else {
        channel.connect(remoteAddress);
    }
    if (keepAliveManager != null) {
        keepAliveManager.onTransportStarted();
    }
    return null;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOption(io.netty.channel.ChannelOption) ChannelHandler(io.netty.channel.ChannelHandler) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Http2ChannelClosedException(io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException) ClosedChannelException(java.nio.channels.ClosedChannelException) EventLoop(io.netty.channel.EventLoop) ClientKeepAlivePinger(io.grpc.internal.KeepAliveManager.ClientKeepAlivePinger) Bootstrap(io.netty.bootstrap.Bootstrap) KeepAliveManager(io.grpc.internal.KeepAliveManager) SocketAddress(java.net.SocketAddress) Map(java.util.Map)

Example 5 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.

the class NettyServerTest method multiPortStartStopGet.

@Test
public void multiPortStartStopGet() throws Exception {
    InetSocketAddress addr1 = new InetSocketAddress(0);
    InetSocketAddress addr2 = new InetSocketAddress(0);
    NettyServer ns = new NettyServer(Arrays.asList(addr1, addr2), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), new HashMap<ChannelOption<?>, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, ProtocolNegotiators.plaintext(), Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), // ignore
    1, // ignore
    false, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    true, // ignore
    0, Attributes.EMPTY, channelz);
    final SettableFuture<Void> shutdownCompleted = SettableFuture.create();
    ns.start(new ServerListener() {

        @Override
        public ServerTransportListener transportCreated(ServerTransport transport) {
            return new NoopServerTransportListener();
        }

        @Override
        public void serverShutdown() {
            shutdownCompleted.set(null);
        }
    });
    // SocketStats won't be available until the event loop task of adding SocketStats created by
    // ns.start() complete. So submit a noop task and await until it's drained.
    eventLoop.submit(new Runnable() {

        @Override
        public void run() {
        }
    }).await(5, TimeUnit.SECONDS);
    assertEquals(2, ns.getListenSocketAddresses().size());
    for (SocketAddress address : ns.getListenSocketAddresses()) {
        assertThat(((InetSocketAddress) address).getPort()).isGreaterThan(0);
    }
    List<InternalInstrumented<SocketStats>> stats = ns.getListenSocketStatsList();
    assertEquals(2, ns.getListenSocketStatsList().size());
    for (InternalInstrumented<SocketStats> listenSocket : stats) {
        assertSame(listenSocket, channelz.getSocket(id(listenSocket)));
        // very basic sanity check of the contents
        SocketStats socketStats = listenSocket.getStats().get();
        assertThat(ns.getListenSocketAddresses()).contains(socketStats.local);
        assertNull(socketStats.remote);
    }
    // Cleanup
    ns.shutdown();
    shutdownCompleted.get();
    // listen socket is removed
    for (InternalInstrumented<SocketStats> listenSocket : stats) {
        assertNull(channelz.getSocket(id(listenSocket)));
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ChannelOption(io.netty.channel.ChannelOption) ServerStreamTracer(io.grpc.ServerStreamTracer) ServerTransport(io.grpc.internal.ServerTransport) InetSocketAddress(java.net.InetSocketAddress) SocketStats(io.grpc.InternalChannelz.SocketStats) ServerTransportListener(io.grpc.internal.ServerTransportListener) InternalInstrumented(io.grpc.InternalInstrumented) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ServerListener(io.grpc.internal.ServerListener) Test(org.junit.Test)

Aggregations

ChannelOption (io.netty.channel.ChannelOption)19 Test (org.junit.Test)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 InetSocketAddress (java.net.InetSocketAddress)7 ServerStreamTracer (io.grpc.ServerStreamTracer)6 ServerTransportListener (io.grpc.internal.ServerTransportListener)6 Map (java.util.Map)6 ServerListener (io.grpc.internal.ServerListener)5 ServerTransport (io.grpc.internal.ServerTransport)5 ChannelFuture (io.netty.channel.ChannelFuture)5 SocketAddress (java.net.SocketAddress)5 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 HashMap (java.util.HashMap)4 SocketStats (io.grpc.InternalChannelz.SocketStats)3 TransportTracer (io.grpc.internal.TransportTracer)3 LocalSocketPicker (io.grpc.netty.NettyChannelBuilder.LocalSocketPicker)3 Channel (io.netty.channel.Channel)3 ChannelConfig (io.netty.channel.ChannelConfig)3 ChannelHandler (io.netty.channel.ChannelHandler)3