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();
}
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;
}
}
}
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();
}
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);
}
}
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);
}
}
Aggregations