use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project atomix by atomix.
the class NettyMessagingService method initEventLoopGroup.
private void initEventLoopGroup() {
// try Epoll first and if that does work, use nio.
try {
clientGroup = new EpollEventLoopGroup(0, namedThreads("netty-messaging-event-epoll-client-%d", log));
serverGroup = new EpollEventLoopGroup(0, namedThreads("netty-messaging-event-epoll-server-%d", log));
serverChannelClass = EpollServerSocketChannel.class;
clientChannelClass = EpollSocketChannel.class;
return;
} catch (Throwable e) {
log.debug("Failed to initialize native (epoll) transport. " + "Reason: {}. Proceeding with nio.", e.getMessage());
}
clientGroup = new NioEventLoopGroup(0, namedThreads("netty-messaging-event-nio-client-%d", log));
serverGroup = new NioEventLoopGroup(0, namedThreads("netty-messaging-event-nio-server-%d", log));
serverChannelClass = NioServerSocketChannel.class;
clientChannelClass = NioSocketChannel.class;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup 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();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project herddb by diennea.
the class NettyConnector method connect.
public static NettyChannel connect(String host, int port, boolean ssl, int connectTimeout, int socketTimeout, ChannelEventListener receiver, final ExecutorService callbackExecutor, final MultithreadEventLoopGroup networkGroup, final DefaultEventLoopGroup localEventsGroup) throws IOException {
try {
final SslContext sslCtx = !ssl ? null : SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
Class<? extends Channel> channelType;
InetSocketAddress inet = new InetSocketAddress(host, port);
SocketAddress address;
String hostAddress = NetworkUtils.getAddress(inet);
MultithreadEventLoopGroup group;
if (LocalServerRegistry.isLocalServer(hostAddress, port, ssl)) {
channelType = LocalChannel.class;
address = new LocalAddress(hostAddress + ":" + port + ":" + ssl);
group = localEventsGroup;
} else {
channelType = networkGroup instanceof EpollEventLoopGroup ? EpollSocketChannel.class : NioSocketChannel.class;
address = inet;
group = networkGroup;
}
Bootstrap b = new Bootstrap();
AtomicReference<NettyChannel> result = new AtomicReference<>();
b.group(group).channel(channelType).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout).handler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel ch) throws Exception {
NettyChannel channel = new NettyChannel(host + ":" + port, ch, callbackExecutor);
result.set(channel);
channel.setMessagesReceiver(receiver);
if (ssl) {
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port));
}
if (socketTimeout > 0) {
ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(socketTimeout));
}
ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4));
ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
//
ch.pipeline().addLast("messageencoder", new DataMessageEncoder());
ch.pipeline().addLast("messagedecoder", new DataMessageDecoder());
ch.pipeline().addLast(new InboundMessageHandler(channel));
}
});
LOGGER.log(Level.FINE, "connecting to {0}:{1} ssl={2} address={3}", new Object[] { host, port, ssl, address });
b.connect(address).sync();
return result.get();
} catch (InterruptedException ex) {
throw new IOException(ex);
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project herddb by diennea.
the class HDBClient method init.
private void init() {
LOG.log(Level.SEVERE, "init {0}", this);
this.thredpool = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "hdb-client");
t.setDaemon(true);
return t;
}
});
this.networkGroup = NetworkUtils.isEnableEpoolNative() ? new EpollEventLoopGroup(0, thredpool) : new NioEventLoopGroup(0, thredpool);
this.localEventsGroup = new DefaultEventLoopGroup();
String mode = configuration.getString(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_LOCAL);
switch(mode) {
case ClientConfiguration.PROPERTY_MODE_LOCAL:
case ClientConfiguration.PROPERTY_MODE_STANDALONE:
this.clientSideMetadataProvider = new StaticClientSideMetadataProvider(configuration.getString(ClientConfiguration.PROPERTY_SERVER_ADDRESS, ClientConfiguration.PROPERTY_SERVER_ADDRESS_DEFAULT), configuration.getInt(ClientConfiguration.PROPERTY_SERVER_PORT, ClientConfiguration.PROPERTY_SERVER_PORT_DEFAULT), configuration.getBoolean(ClientConfiguration.PROPERTY_SERVER_SSL, ClientConfiguration.PROPERTY_SERVER_SSL_DEFAULT));
break;
case ClientConfiguration.PROPERTY_MODE_CLUSTER:
this.clientSideMetadataProvider = new ZookeeperClientSideMetadataProvider(configuration.getString(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS_DEFAULT), configuration.getInt(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT), configuration.getString(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, ClientConfiguration.PROPERTY_ZOOKEEPER_PATH_DEFAULT));
break;
default:
throw new IllegalStateException(mode);
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project herddb by diennea.
the class NetworkChannelTest method test.
@Test
public void test() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
if (NetworkUtils.isEnableEpoolNative()) {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new EpollEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
}
}
Aggregations