Search in sources :

Example 66 with EventLoopGroup

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

the class NettyConnectionManagerTest method testMatchingNumberOfArenasAndThreadsAsDefault.

/**
 * Tests that the number of arenas and number of threads of the client and server are set to the
 * same number, that is the number of configured task slots.
 */
@Test
public void testMatchingNumberOfArenasAndThreadsAsDefault() throws Exception {
    // Expected number of arenas and threads
    int numberOfSlots = 2;
    NettyConnectionManager connectionManager;
    try (NetUtils.Port port = NetUtils.getAvailablePort()) {
        NettyConfig config = new NettyConfig(InetAddress.getLocalHost(), port.getPort(), 1024, numberOfSlots, new Configuration());
        connectionManager = createNettyConnectionManager(config);
        connectionManager.start();
    }
    assertNotNull("connectionManager is null due to fail to get a free port", connectionManager);
    assertEquals(numberOfSlots, connectionManager.getBufferPool().getNumberOfArenas());
    {
        // Client event loop group
        Bootstrap boostrap = connectionManager.getClient().getBootstrap();
        EventLoopGroup group = boostrap.group();
        Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
        f.setAccessible(true);
        Object[] eventExecutors = (Object[]) f.get(group);
        assertEquals(numberOfSlots, eventExecutors.length);
    }
    {
        // Server event loop group
        ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
        EventLoopGroup group = bootstrap.group();
        Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
        f.setAccessible(true);
        Object[] eventExecutors = (Object[]) f.get(group);
        assertEquals(numberOfSlots, eventExecutors.length);
    }
    {
        // Server child event loop group
        ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
        EventLoopGroup group = bootstrap.childGroup();
        Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
        f.setAccessible(true);
        Object[] eventExecutors = (Object[]) f.get(group);
        assertEquals(numberOfSlots, eventExecutors.length);
    }
}
Also used : Field(java.lang.reflect.Field) NetUtils(org.apache.flink.util.NetUtils) EventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup) Configuration(org.apache.flink.configuration.Configuration) Bootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap) ServerBootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap) ServerBootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 67 with EventLoopGroup

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

the class NettyConnectionManagerTest method testManualConfiguration.

/**
 * Tests that the number of arenas and threads can be configured manually.
 */
@Test
public void testManualConfiguration() throws Exception {
    // Expected numbers
    int numberOfArenas = 1;
    int numberOfClientThreads = 3;
    int numberOfServerThreads = 4;
    // Expected number of threads
    Configuration flinkConfig = new Configuration();
    flinkConfig.setInteger(NettyShuffleEnvironmentOptions.NUM_ARENAS, numberOfArenas);
    flinkConfig.setInteger(NettyShuffleEnvironmentOptions.NUM_THREADS_CLIENT, 3);
    flinkConfig.setInteger(NettyShuffleEnvironmentOptions.NUM_THREADS_SERVER, 4);
    NettyConnectionManager connectionManager;
    try (NetUtils.Port port = NetUtils.getAvailablePort()) {
        NettyConfig config = new NettyConfig(InetAddress.getLocalHost(), port.getPort(), 1024, 1337, flinkConfig);
        connectionManager = createNettyConnectionManager(config);
        connectionManager.start();
        assertEquals(numberOfArenas, connectionManager.getBufferPool().getNumberOfArenas());
    }
    assertNotNull("connectionManager is null due to fail to get a free port", connectionManager);
    {
        // Client event loop group
        Bootstrap boostrap = connectionManager.getClient().getBootstrap();
        EventLoopGroup group = boostrap.group();
        Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
        f.setAccessible(true);
        Object[] eventExecutors = (Object[]) f.get(group);
        assertEquals(numberOfClientThreads, eventExecutors.length);
    }
    {
        // Server event loop group
        ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
        EventLoopGroup group = bootstrap.group();
        Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
        f.setAccessible(true);
        Object[] eventExecutors = (Object[]) f.get(group);
        assertEquals(numberOfServerThreads, eventExecutors.length);
    }
    {
        // Server child event loop group
        ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
        EventLoopGroup group = bootstrap.childGroup();
        Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
        f.setAccessible(true);
        Object[] eventExecutors = (Object[]) f.get(group);
        assertEquals(numberOfServerThreads, eventExecutors.length);
    }
}
Also used : Field(java.lang.reflect.Field) NetUtils(org.apache.flink.util.NetUtils) EventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup) Configuration(org.apache.flink.configuration.Configuration) Bootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap) ServerBootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap) ServerBootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 68 with EventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project pinpoint by naver.

the class NettyIT method writeTest.

@Test
public void writeTest() throws Exception {
    final CountDownLatch awaitLatch = new CountDownLatch(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(2);
    Bootstrap bootstrap = client(workerGroup);
    final ChannelFuture connect = bootstrap.connect(webServer.getHostname(), webServer.getListeningPort());
    connect.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                Channel channel = future.channel();
                channel.pipeline().addLast(new SimpleChannelInboundHandler() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
                        awaitLatch.countDown();
                    }
                });
                HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
                future.channel().writeAndFlush(request);
            }
        }
    });
    boolean await = awaitLatch.await(3000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(await);
    final Channel channel = connect.channel();
    try {
        PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
        verifier.printCache();
        verifier.verifyTrace(event("NETTY", Bootstrap.class.getMethod("connect", SocketAddress.class), annotation("netty.address", webServer.getHostAndPort())));
        verifier.verifyTrace(event("NETTY", "io.netty.channel.DefaultChannelPromise.addListener(io.netty.util.concurrent.GenericFutureListener)"));
        verifier.verifyTrace(event("ASYNC", "Asynchronous Invocation"));
        verifier.verifyTrace(event("NETTY_INTERNAL", "io.netty.util.concurrent.DefaultPromise.notifyListenersNow()"));
        verifier.verifyTrace(event("NETTY_INTERNAL", "io.netty.util.concurrent.DefaultPromise.notifyListener0(io.netty.util.concurrent.Future, io.netty.util.concurrent.GenericFutureListener)"));
        verifier.verifyTrace(event("NETTY", "io.netty.channel.DefaultChannelPipeline.writeAndFlush(java.lang.Object)"));
        verifier.verifyTrace(event("NETTY_HTTP", "io.netty.handler.codec.http.HttpObjectEncoder.encode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)", annotation("http.url", "/")));
    } finally {
        channel.close().sync();
        workerGroup.shutdown();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelFutureListener(io.netty.channel.ChannelFutureListener) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 69 with EventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project alluxio by Alluxio.

the class GrpcConnectionPool method applyGroupDefaults.

/**
 * It updates and returns the given {@link NettyChannelBuilder} based on network group settings.
 */
private NettyChannelBuilder applyGroupDefaults(GrpcChannelKey key, NettyChannelBuilder channelBuilder, AlluxioConfiguration conf) {
    long keepAliveTimeMs = conf.getMs(PropertyKey.Template.USER_NETWORK_KEEPALIVE_TIME_MS.format(key.getNetworkGroup().getPropertyCode()));
    long keepAliveTimeoutMs = conf.getMs(PropertyKey.Template.USER_NETWORK_KEEPALIVE_TIMEOUT_MS.format(key.getNetworkGroup().getPropertyCode()));
    long inboundMessageSizeBytes = conf.getBytes(PropertyKey.Template.USER_NETWORK_MAX_INBOUND_MESSAGE_SIZE.format(key.getNetworkGroup().getPropertyCode()));
    long flowControlWindow = conf.getBytes(PropertyKey.Template.USER_NETWORK_FLOWCONTROL_WINDOW.format(key.getNetworkGroup().getPropertyCode()));
    Class<? extends Channel> channelType = NettyUtils.getChannelClass(!(key.getServerAddress().getSocketAddress() instanceof InetSocketAddress), PropertyKey.Template.USER_NETWORK_NETTY_CHANNEL.format(key.getNetworkGroup().getPropertyCode()), conf);
    EventLoopGroup eventLoopGroup = acquireNetworkEventLoop(key, conf);
    // Update the builder.
    channelBuilder.keepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS);
    channelBuilder.keepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS);
    channelBuilder.maxInboundMessageSize((int) inboundMessageSizeBytes);
    channelBuilder.flowControlWindow((int) flowControlWindow);
    channelBuilder.channelType(channelType);
    channelBuilder.eventLoopGroup(eventLoopGroup);
    // Use plaintext
    channelBuilder.usePlaintext();
    if (key.getNetworkGroup() == GrpcNetworkGroup.SECRET) {
        // Use self-signed for SECRET network group.
        channelBuilder.sslContext(getSslContextProvider(conf).getSelfSignedClientSslContext());
        channelBuilder.useTransportSecurity();
    } else if (conf.getBoolean(alluxio.conf.PropertyKey.NETWORK_TLS_ENABLED)) {
        // Use shared TLS config for other network groups if enabled.
        channelBuilder.sslContext(getSslContextProvider(conf).getClientSslContext());
        channelBuilder.useTransportSecurity();
    }
    return channelBuilder;
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) InetSocketAddress(java.net.InetSocketAddress)

Example 70 with EventLoopGroup

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

the class HttpNativeServer method main.

public static void main(String[] args) throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    // Control status.
    boolean serverStartSucess = false;
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpNativeServerInitializer());
        Channel channel = b.bind(0).sync().channel();
        System.err.println("Server started, will shutdown now.");
        channel.close().sync();
        serverStartSucess = true;
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
    // return the right system exit code to signal success
    System.exit(serverStartSucess ? 0 : 1);
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

EventLoopGroup (io.netty.channel.EventLoopGroup)367 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)271 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)162 Bootstrap (io.netty.bootstrap.Bootstrap)137 Channel (io.netty.channel.Channel)131 ChannelFuture (io.netty.channel.ChannelFuture)129 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)109 SocketChannel (io.netty.channel.socket.SocketChannel)94 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)92 InetSocketAddress (java.net.InetSocketAddress)69 Test (org.junit.jupiter.api.Test)67 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)60 LoggingHandler (io.netty.handler.logging.LoggingHandler)55 ChannelPipeline (io.netty.channel.ChannelPipeline)51 SslContext (io.netty.handler.ssl.SslContext)51 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)50 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)49 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)46 LocalServerChannel (io.netty.channel.local.LocalServerChannel)42 LocalChannel (io.netty.channel.local.LocalChannel)40