Madan Jampani

Netty epoll support. Now with updated pom.xml and features.xml to bring in the dependencies

...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 <bundle>mvn:io.netty/netty-transport/4.0.23.Final</bundle> 15 <bundle>mvn:io.netty/netty-transport/4.0.23.Final</bundle>
16 <bundle>mvn:io.netty/netty-handler/4.0.23.Final</bundle> 16 <bundle>mvn:io.netty/netty-handler/4.0.23.Final</bundle>
17 <bundle>mvn:io.netty/netty-codec/4.0.23.Final</bundle> 17 <bundle>mvn:io.netty/netty-codec/4.0.23.Final</bundle>
18 + <bundle>io.netty/netty-transport-native-epoll/4.0.23.Final</bundle>
18 <bundle>mvn:commons-pool/commons-pool/1.6</bundle> 19 <bundle>mvn:commons-pool/commons-pool/1.6</bundle>
19 20
20 <bundle>mvn:com.hazelcast/hazelcast/3.3</bundle> 21 <bundle>mvn:com.hazelcast/hazelcast/3.3</bundle>
......
...@@ -312,6 +312,11 @@ ...@@ -312,6 +312,11 @@
312 <artifactId>netty-codec</artifactId> 312 <artifactId>netty-codec</artifactId>
313 <version>${netty4.version}</version> 313 <version>${netty4.version}</version>
314 </dependency> 314 </dependency>
315 + <dependency>
316 + <groupId>io.netty</groupId>
317 + <artifactId>netty-transport-native-epoll</artifactId>
318 + <version>${netty4.version}</version>
319 + </dependency>
315 </dependencies> 320 </dependencies>
316 </dependencyManagement> 321 </dependencyManagement>
317 322
......
...@@ -55,6 +55,10 @@ ...@@ -55,6 +55,10 @@
55 <groupId>io.netty</groupId> 55 <groupId>io.netty</groupId>
56 <artifactId>netty-codec</artifactId> 56 <artifactId>netty-codec</artifactId>
57 </dependency> 57 </dependency>
58 + <dependency>
59 + <groupId>io.netty</groupId>
60 + <artifactId>netty-transport-native-epoll</artifactId>
61 + </dependency>
58 </dependencies> 62 </dependencies>
59 63
60 </project> 64 </project>
......
...@@ -16,7 +16,12 @@ import io.netty.channel.ChannelHandlerContext; ...@@ -16,7 +16,12 @@ import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.ChannelInitializer; 16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.ChannelOption; 17 import io.netty.channel.ChannelOption;
18 import io.netty.channel.EventLoopGroup; 18 import io.netty.channel.EventLoopGroup;
19 +import io.netty.channel.ServerChannel;
19 import io.netty.channel.SimpleChannelInboundHandler; 20 import io.netty.channel.SimpleChannelInboundHandler;
21 +import io.netty.channel.epoll.Epoll;
22 +import io.netty.channel.epoll.EpollEventLoopGroup;
23 +import io.netty.channel.epoll.EpollServerSocketChannel;
24 +import io.netty.channel.epoll.EpollSocketChannel;
20 import io.netty.channel.nio.NioEventLoopGroup; 25 import io.netty.channel.nio.NioEventLoopGroup;
21 import io.netty.channel.socket.SocketChannel; 26 import io.netty.channel.socket.SocketChannel;
22 import io.netty.channel.socket.nio.NioServerSocketChannel; 27 import io.netty.channel.socket.nio.NioServerSocketChannel;
...@@ -40,9 +45,6 @@ public class NettyMessagingService implements MessagingService { ...@@ -40,9 +45,6 @@ public class NettyMessagingService implements MessagingService {
40 45
41 private final int port; 46 private final int port;
42 private final Endpoint localEp; 47 private final Endpoint localEp;
43 - private final EventLoopGroup bossGroup = new NioEventLoopGroup();
44 - private EventLoopGroup workerGroup;
45 - private Class<? extends Channel> channelClass;
46 private final ConcurrentMap<String, MessageHandler> handlers = new ConcurrentHashMap<>(); 48 private final ConcurrentMap<String, MessageHandler> handlers = new ConcurrentHashMap<>();
47 private final Cache<Long, AsyncResponse> responseFutures = CacheBuilder.newBuilder() 49 private final Cache<Long, AsyncResponse> responseFutures = CacheBuilder.newBuilder()
48 .maximumSize(100000) 50 .maximumSize(100000)
...@@ -53,10 +55,32 @@ public class NettyMessagingService implements MessagingService { ...@@ -53,10 +55,32 @@ public class NettyMessagingService implements MessagingService {
53 private final GenericKeyedObjectPool<Endpoint, Channel> channels 55 private final GenericKeyedObjectPool<Endpoint, Channel> channels
54 = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory()); 56 = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory());
55 57
56 - // TODO: make this configurable. 58 + private EventLoopGroup serverGroup;
59 + private EventLoopGroup clientGroup;
60 + private Class<? extends ServerChannel> serverChannelClass;
61 + private Class<? extends Channel> clientChannelClass;
62 +
57 private void initEventLoopGroup() { 63 private void initEventLoopGroup() {
58 - workerGroup = new NioEventLoopGroup(); 64 + // try Epoll first and if that does work, use nio.
59 - channelClass = NioSocketChannel.class; 65 + // TODO: make this configurable.
66 + try {
67 + if (Epoll.isAvailable()) {
68 + clientGroup = new EpollEventLoopGroup();
69 + serverGroup = new EpollEventLoopGroup();
70 + serverChannelClass = EpollServerSocketChannel.class;
71 + clientChannelClass = EpollSocketChannel.class;
72 + return;
73 + } else {
74 + log.info("Netty epoll support is not available. Proceeding with nio.");
75 + }
76 +
77 + } catch (Throwable t) {
78 + log.warn("Failed to initialize epoll sockets. Proceeding with nio.", t);
79 + }
80 + clientGroup = new NioEventLoopGroup();
81 + serverGroup = new NioEventLoopGroup();
82 + serverChannelClass = NioServerSocketChannel.class;
83 + clientChannelClass = NioSocketChannel.class;
60 } 84 }
61 85
62 public NettyMessagingService() { 86 public NettyMessagingService() {
...@@ -84,8 +108,8 @@ public class NettyMessagingService implements MessagingService { ...@@ -84,8 +108,8 @@ public class NettyMessagingService implements MessagingService {
84 108
85 public void deactivate() throws Exception { 109 public void deactivate() throws Exception {
86 channels.close(); 110 channels.close();
87 - bossGroup.shutdownGracefully(); 111 + serverGroup.shutdownGracefully();
88 - workerGroup.shutdownGracefully(); 112 + clientGroup.shutdownGracefully();
89 } 113 }
90 114
91 @Override 115 @Override
...@@ -149,8 +173,8 @@ public class NettyMessagingService implements MessagingService { ...@@ -149,8 +173,8 @@ public class NettyMessagingService implements MessagingService {
149 b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); 173 b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
150 // TODO: Need JVM options to configure PooledByteBufAllocator. 174 // TODO: Need JVM options to configure PooledByteBufAllocator.
151 b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); 175 b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
152 - b.group(bossGroup, workerGroup) 176 + b.group(serverGroup, clientGroup)
153 - .channel(NioServerSocketChannel.class) 177 + .channel(serverChannelClass)
154 .childHandler(new OnosCommunicationChannelInitializer()) 178 .childHandler(new OnosCommunicationChannelInitializer())
155 .option(ChannelOption.SO_BACKLOG, 128) 179 .option(ChannelOption.SO_BACKLOG, 128)
156 .childOption(ChannelOption.SO_KEEPALIVE, true); 180 .childOption(ChannelOption.SO_KEEPALIVE, true);
...@@ -178,10 +202,10 @@ public class NettyMessagingService implements MessagingService { ...@@ -178,10 +202,10 @@ public class NettyMessagingService implements MessagingService {
178 bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); 202 bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
179 bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); 203 bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
180 bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); 204 bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
181 - bootstrap.group(workerGroup); 205 + bootstrap.group(clientGroup);
182 // TODO: Make this faster: 206 // TODO: Make this faster:
183 // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0 207 // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0
184 - bootstrap.channel(channelClass); 208 + bootstrap.channel(clientChannelClass);
185 bootstrap.option(ChannelOption.SO_KEEPALIVE, true); 209 bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
186 bootstrap.handler(new OnosCommunicationChannelInitializer()); 210 bootstrap.handler(new OnosCommunicationChannelInitializer());
187 // Start the client. 211 // Start the client.
......