Search in sources :

Example 16 with ChannelOption

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

the class NettyServerTest method connectionSettingsPropagated.

@Test(timeout = 60000)
public void connectionSettingsPropagated() throws Exception {
    final int originalLowWaterMark = 2097169;
    final int originalHighWaterMark = 2097211;
    Map<ChannelOption<?>, Object> childChannelOptions = new HashMap<>();
    childChannelOptions.put(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(originalLowWaterMark, originalHighWaterMark));
    class TestChannelHandler extends ChannelHandlerAdapter {

        CountDownLatch countDownLatch = new CountDownLatch(1);

        int lowWaterMark;

        int highWaterMark;

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) {
            Channel channel = ctx.channel();
            WriteBufferWaterMark writeBufferWaterMark = channel.config().getOption(ChannelOption.WRITE_BUFFER_WATER_MARK);
            lowWaterMark = writeBufferWaterMark.low();
            highWaterMark = writeBufferWaterMark.high();
            countDownLatch.countDown();
        }
    }
    final TestChannelHandler channelHandler = new TestChannelHandler();
    class TestProtocolNegotiator implements ProtocolNegotiator {

        Attributes eagAttributes;

        @Override
        public ChannelHandler newHandler(GrpcHttp2ConnectionHandler handler) {
            eagAttributes = handler.getEagAttributes();
            return channelHandler;
        }

        @Override
        public void close() {
        }

        @Override
        public AsciiString scheme() {
            return Utils.HTTP;
        }
    }
    Attributes eagAttributes = Attributes.newBuilder().set(Attributes.Key.create("foo"), "bar").build();
    TestProtocolNegotiator protocolNegotiator = new TestProtocolNegotiator();
    InetSocketAddress addr = new InetSocketAddress(0);
    NettyServer ns = new NettyServer(Arrays.asList(addr), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), childChannelOptions, new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, protocolNegotiator, 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, eagAttributes, channelz);
    ns.start(new ServerListener() {

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

        @Override
        public void serverShutdown() {
        }
    });
    Socket socket = new Socket();
    socket.connect(ns.getListenSocketAddress(), /* timeout= */
    8000);
    channelHandler.countDownLatch.await();
    socket.close();
    assertThat(protocolNegotiator.eagAttributes).isSameInstanceAs(eagAttributes);
    assertThat(channelHandler.lowWaterMark).isEqualTo(originalLowWaterMark);
    assertThat(channelHandler.highWaterMark).isEqualTo(originalHighWaterMark);
    ns.shutdown();
}
Also used : ChannelOption(io.netty.channel.ChannelOption) HashMap(java.util.HashMap) ServerTransport(io.grpc.internal.ServerTransport) InetSocketAddress(java.net.InetSocketAddress) Attributes(io.grpc.Attributes) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ServerListener(io.grpc.internal.ServerListener) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerStreamTracer(io.grpc.ServerStreamTracer) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) ServerTransportListener(io.grpc.internal.ServerTransportListener) Socket(java.net.Socket) Test(org.junit.Test)

Example 17 with ChannelOption

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

the class NettyServerTest method channelzListenSocket.

@Test
public void channelzListenSocket() throws Exception {
    InetSocketAddress addr = new InetSocketAddress(0);
    NettyServer ns = new NettyServer(Arrays.asList(addr), 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);
        }
    });
    assertThat(((InetSocketAddress) ns.getListenSocketAddress()).getPort()).isGreaterThan(0);
    // 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);
    InternalInstrumented<SocketStats> listenSocket = ns.getListenSocketStats();
    assertSame(listenSocket, channelz.getSocket(id(listenSocket)));
    // very basic sanity check of the contents
    SocketStats socketStats = listenSocket.getStats().get();
    assertEquals(ns.getListenSocketAddress(), socketStats.local);
    assertNull(socketStats.remote);
    // by default, there are some socket options set on the listen socket
    assertThat(socketStats.socketOptions.others).isNotEmpty();
    // Cleanup
    ns.shutdown();
    shutdownCompleted.get();
    // listen socket is removed
    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) ServerListener(io.grpc.internal.ServerListener) Test(org.junit.Test)

Example 18 with ChannelOption

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

the class BootstrapTest method testChannelOptionOrderPreserve.

@Test
public void testChannelOptionOrderPreserve() throws InterruptedException {
    final BlockingQueue<ChannelOption<?>> options = new LinkedBlockingQueue<ChannelOption<?>>();
    class ChannelConfigValidator extends DefaultChannelConfig {

        ChannelConfigValidator(Channel channel) {
            super(channel);
        }

        @Override
        public <T> boolean setOption(ChannelOption<T> option, T value) {
            options.add(option);
            return super.setOption(option, value);
        }
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final Bootstrap bootstrap = new Bootstrap().handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            latch.countDown();
        }
    }).group(groupA).channelFactory(new ChannelFactory<Channel>() {

        @Override
        public Channel newChannel() {
            return new LocalChannel() {

                private ChannelConfigValidator config;

                @Override
                public synchronized ChannelConfig config() {
                    if (config == null) {
                        config = new ChannelConfigValidator(this);
                    }
                    return config;
                }
            };
        }
    }).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1).option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 2);
    bootstrap.register().syncUninterruptibly();
    latch.await();
    // Check the order is the same as what we defined before.
    assertSame(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, options.take());
    assertSame(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, options.take());
}
Also used : ChannelOption(io.netty.channel.ChannelOption) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) ChannelConfig(io.netty.channel.ChannelConfig) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) Test(org.junit.jupiter.api.Test)

Example 19 with ChannelOption

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

the class ServerBootstrapConfig method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder(super.toString());
    buf.setLength(buf.length() - 1);
    buf.append(", ");
    EventLoopGroup childGroup = childGroup();
    if (childGroup != null) {
        buf.append("childGroup: ");
        buf.append(StringUtil.simpleClassName(childGroup));
        buf.append(", ");
    }
    Map<ChannelOption<?>, Object> childOptions = childOptions();
    if (!childOptions.isEmpty()) {
        buf.append("childOptions: ");
        buf.append(childOptions);
        buf.append(", ");
    }
    Map<AttributeKey<?>, Object> childAttrs = childAttrs();
    if (!childAttrs.isEmpty()) {
        buf.append("childAttrs: ");
        buf.append(childAttrs);
        buf.append(", ");
    }
    ChannelHandler childHandler = childHandler();
    if (childHandler != null) {
        buf.append("childHandler: ");
        buf.append(childHandler);
        buf.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)

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