Search in sources :

Example 91 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project cassandra by apache.

the class ProxyHandlerTest method test.

public void test(DoTest test) throws Throwable {
    EventLoopGroup serverGroup = new NioEventLoopGroup(1);
    EventLoopGroup clientGroup = new NioEventLoopGroup(1);
    InboundProxyHandler.Controller controller = new InboundProxyHandler.Controller();
    InboundProxyHandler proxyHandler = new InboundProxyHandler(controller);
    TestHandler testHandler = new TestHandler();
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(serverGroup).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) {
            ch.pipeline().addLast(proxyHandler).addLast(testHandler);
        }
    }).childOption(ChannelOption.AUTO_READ, false);
    Bootstrap cb = new Bootstrap();
    cb.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
        }
    });
    final LocalAddress addr = new LocalAddress("test");
    Channel serverChannel = sb.bind(addr).sync().channel();
    Channel clientChannel = cb.connect(addr).sync().channel();
    test.doTest(controller, testHandler, clientChannel);
    clientChannel.close();
    serverChannel.close();
    serverGroup.shutdownGracefully();
    clientGroup.shutdownGracefully();
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 92 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project flink by apache.

the class NettyClient method connect.

// ------------------------------------------------------------------------
// Client connections
// ------------------------------------------------------------------------
ChannelFuture connect(final InetSocketAddress serverSocketAddress) {
    checkState(bootstrap != null, "Client has not been initialized yet.");
    // --------------------------------------------------------------------
    // Child channel pipeline for accepted connections
    // --------------------------------------------------------------------
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel channel) throws Exception {
            // SSL handler should be added first in the pipeline
            if (clientSSLFactory != null) {
                SslHandler sslHandler = clientSSLFactory.createNettySSLHandler(channel.alloc(), serverSocketAddress.getAddress().getCanonicalHostName(), serverSocketAddress.getPort());
                channel.pipeline().addLast("ssl", sslHandler);
            }
            channel.pipeline().addLast(protocol.getClientChannelHandlers());
        }
    });
    try {
        return bootstrap.connect(serverSocketAddress);
    } catch (ChannelException e) {
        if ((e.getCause() instanceof java.net.SocketException && e.getCause().getMessage().equals("Too many open files")) || (e.getCause() instanceof ChannelException && e.getCause().getCause() instanceof java.net.SocketException && e.getCause().getCause().getMessage().equals("Too many open files"))) {
            throw new ChannelException("The operating system does not offer enough file handles to open the network connection. " + "Please increase the number of available file handles.", e.getCause());
        } else {
            throw e;
        }
    }
}
Also used : EpollSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) IOException(java.io.IOException) ChannelException(org.apache.flink.shaded.netty4.io.netty.channel.ChannelException) SslHandler(org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler) ChannelException(org.apache.flink.shaded.netty4.io.netty.channel.ChannelException)

Example 93 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project flink by apache.

the class AbstractServerBase method shutdownServer.

/**
 * Shuts down the server and all related thread pools.
 *
 * @return A {@link CompletableFuture} that will be completed upon termination of the shutdown
 *     process.
 */
public CompletableFuture<Void> shutdownServer() {
    CompletableFuture<Void> shutdownFuture = new CompletableFuture<>();
    if (serverShutdownFuture.compareAndSet(null, shutdownFuture)) {
        log.info("Shutting down {} @ {}", serverName, serverAddress);
        final CompletableFuture<Void> groupShutdownFuture = new CompletableFuture<>();
        if (bootstrap != null) {
            EventLoopGroup group = bootstrap.group();
            if (group != null && !group.isShutdown()) {
                group.shutdownGracefully(0L, 0L, TimeUnit.MILLISECONDS).addListener(finished -> {
                    if (finished.isSuccess()) {
                        groupShutdownFuture.complete(null);
                    } else {
                        groupShutdownFuture.completeExceptionally(finished.cause());
                    }
                });
            } else {
                groupShutdownFuture.complete(null);
            }
        } else {
            groupShutdownFuture.complete(null);
        }
        final CompletableFuture<Void> handlerShutdownFuture = new CompletableFuture<>();
        if (handler == null) {
            handlerShutdownFuture.complete(null);
        } else {
            handler.shutdown().whenComplete((result, throwable) -> {
                if (throwable != null) {
                    handlerShutdownFuture.completeExceptionally(throwable);
                } else {
                    handlerShutdownFuture.complete(null);
                }
            });
        }
        final CompletableFuture<Void> queryExecShutdownFuture = CompletableFuture.runAsync(() -> {
            if (queryExecutor != null) {
                ExecutorUtils.gracefulShutdown(10L, TimeUnit.MINUTES, queryExecutor);
            }
        });
        CompletableFuture.allOf(queryExecShutdownFuture, groupShutdownFuture, handlerShutdownFuture).whenComplete((result, throwable) -> {
            if (throwable != null) {
                shutdownFuture.completeExceptionally(throwable);
            } else {
                shutdownFuture.complete(null);
            }
        });
    }
    return serverShutdownFuture.get();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) EventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup) NioEventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup)

Example 94 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap 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 95 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap 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)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)429 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)207 Channel (io.netty.channel.Channel)194 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)189 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)169 ChannelFuture (io.netty.channel.ChannelFuture)152 EventLoopGroup (io.netty.channel.EventLoopGroup)139 SocketChannel (io.netty.channel.socket.SocketChannel)123 InetSocketAddress (java.net.InetSocketAddress)119 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)108 Test (org.junit.jupiter.api.Test)89 ChannelPipeline (io.netty.channel.ChannelPipeline)76 LocalChannel (io.netty.channel.local.LocalChannel)74 LocalServerChannel (io.netty.channel.local.LocalServerChannel)71 LocalAddress (io.netty.channel.local.LocalAddress)66 CountDownLatch (java.util.concurrent.CountDownLatch)62 IOException (java.io.IOException)60 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)56 ChannelInitializer (io.netty.channel.ChannelInitializer)45