Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 1 | package org.onlab.netty; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.net.UnknownHostException; |
| 5 | import java.util.concurrent.ConcurrentHashMap; |
| 6 | import java.util.concurrent.ConcurrentMap; |
| 7 | import java.util.concurrent.TimeUnit; |
| 8 | |
| 9 | import io.netty.bootstrap.Bootstrap; |
| 10 | import io.netty.bootstrap.ServerBootstrap; |
| 11 | import io.netty.buffer.PooledByteBufAllocator; |
| 12 | import io.netty.channel.Channel; |
| 13 | import io.netty.channel.ChannelFuture; |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 14 | import io.netty.channel.ChannelHandler; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 15 | import io.netty.channel.ChannelHandlerContext; |
| 16 | import io.netty.channel.ChannelInitializer; |
| 17 | import io.netty.channel.ChannelOption; |
| 18 | import io.netty.channel.EventLoopGroup; |
| 19 | import io.netty.channel.SimpleChannelInboundHandler; |
| 20 | import io.netty.channel.nio.NioEventLoopGroup; |
| 21 | import io.netty.channel.socket.SocketChannel; |
| 22 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 23 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 24 | |
| 25 | import org.apache.commons.lang.math.RandomUtils; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 26 | import org.apache.commons.pool.KeyedPoolableObjectFactory; |
| 27 | import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import com.google.common.cache.Cache; |
| 32 | import com.google.common.cache.CacheBuilder; |
| 33 | |
| 34 | /** |
| 35 | * A Netty based implementation of MessagingService. |
| 36 | */ |
| 37 | public class NettyMessagingService implements MessagingService { |
| 38 | |
| 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 41 | private final int port; |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 42 | private final Endpoint localEp; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 43 | private final EventLoopGroup bossGroup = new NioEventLoopGroup(); |
| 44 | private final EventLoopGroup workerGroup = new NioEventLoopGroup(); |
| 45 | private final ConcurrentMap<String, MessageHandler> handlers = new ConcurrentHashMap<>(); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 46 | private final Cache<Long, AsyncResponse> responseFutures = CacheBuilder.newBuilder() |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 47 | .maximumSize(100000) |
| 48 | .weakValues() |
| 49 | // TODO: Once the entry expires, notify blocking threads (if any). |
| 50 | .expireAfterWrite(10, TimeUnit.MINUTES) |
| 51 | .build(); |
| 52 | private final GenericKeyedObjectPool<Endpoint, Channel> channels |
| 53 | = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 54 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 55 | public NettyMessagingService() { |
| 56 | // TODO: Default port should be configurable. |
| 57 | this(8080); |
| 58 | } |
| 59 | |
| 60 | // FIXME: Constructor should not throw exceptions. |
| 61 | public NettyMessagingService(int port) { |
| 62 | this.port = port; |
| 63 | try { |
| 64 | localEp = new Endpoint(java.net.InetAddress.getLocalHost().getHostName(), port); |
| 65 | } catch (UnknownHostException e) { |
| 66 | // bailing out. |
| 67 | throw new RuntimeException(e); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void activate() throws Exception { |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 72 | channels.setTestOnBorrow(true); |
| 73 | channels.setTestOnReturn(true); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 74 | startAcceptingConnections(); |
| 75 | } |
| 76 | |
| 77 | public void deactivate() throws Exception { |
| 78 | channels.close(); |
| 79 | bossGroup.shutdownGracefully(); |
| 80 | workerGroup.shutdownGracefully(); |
| 81 | } |
| 82 | |
| 83 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 84 | public void sendAsync(Endpoint ep, String type, byte[] payload) throws IOException { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 85 | InternalMessage message = new InternalMessage.Builder(this) |
| 86 | .withId(RandomUtils.nextLong()) |
| 87 | .withSender(localEp) |
| 88 | .withType(type) |
| 89 | .withPayload(payload) |
| 90 | .build(); |
| 91 | sendAsync(ep, message); |
| 92 | } |
| 93 | |
| 94 | protected void sendAsync(Endpoint ep, InternalMessage message) throws IOException { |
| 95 | Channel channel = null; |
| 96 | try { |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 97 | try { |
| 98 | channel = channels.borrowObject(ep); |
| 99 | channel.eventLoop().execute(new WriteTask(channel, message)); |
| 100 | } finally { |
| 101 | channels.returnObject(ep, channel); |
| 102 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 103 | } catch (Exception e) { |
| 104 | throw new IOException(e); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 109 | public Response sendAndReceive(Endpoint ep, String type, byte[] payload) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 110 | throws IOException { |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 111 | AsyncResponse futureResponse = new AsyncResponse(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 112 | Long messageId = RandomUtils.nextLong(); |
| 113 | responseFutures.put(messageId, futureResponse); |
| 114 | InternalMessage message = new InternalMessage.Builder(this) |
| 115 | .withId(messageId) |
| 116 | .withSender(localEp) |
| 117 | .withType(type) |
| 118 | .withPayload(payload) |
| 119 | .build(); |
| 120 | sendAsync(ep, message); |
| 121 | return futureResponse; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void registerHandler(String type, MessageHandler handler) { |
| 126 | // TODO: Is this the right semantics for handler registration? |
| 127 | handlers.putIfAbsent(type, handler); |
| 128 | } |
| 129 | |
| 130 | public void unregisterHandler(String type) { |
| 131 | handlers.remove(type); |
| 132 | } |
| 133 | |
| 134 | private MessageHandler getMessageHandler(String type) { |
| 135 | return handlers.get(type); |
| 136 | } |
| 137 | |
| 138 | private void startAcceptingConnections() throws InterruptedException { |
| 139 | ServerBootstrap b = new ServerBootstrap(); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 140 | b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 141 | b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
| 142 | // TODO: Need JVM options to configure PooledByteBufAllocator. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 143 | b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
| 144 | b.group(bossGroup, workerGroup) |
| 145 | .channel(NioServerSocketChannel.class) |
| 146 | .childHandler(new OnosCommunicationChannelInitializer()) |
| 147 | .option(ChannelOption.SO_BACKLOG, 128) |
| 148 | .childOption(ChannelOption.SO_KEEPALIVE, true); |
| 149 | |
| 150 | // Bind and start to accept incoming connections. |
| 151 | b.bind(port).sync(); |
| 152 | } |
| 153 | |
| 154 | private class OnosCommunicationChannelFactory |
| 155 | implements KeyedPoolableObjectFactory<Endpoint, Channel> { |
| 156 | |
| 157 | @Override |
| 158 | public void activateObject(Endpoint endpoint, Channel channel) |
| 159 | throws Exception { |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void destroyObject(Endpoint ep, Channel channel) throws Exception { |
| 164 | channel.close(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Channel makeObject(Endpoint ep) throws Exception { |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 169 | Bootstrap bootstrap = new Bootstrap(); |
| 170 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
| 171 | bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
| 172 | bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
| 173 | bootstrap.group(workerGroup); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 174 | // TODO: Make this faster: |
| 175 | // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0 |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 176 | bootstrap.channel(NioSocketChannel.class); |
| 177 | bootstrap.option(ChannelOption.SO_KEEPALIVE, true); |
| 178 | bootstrap.handler(new OnosCommunicationChannelInitializer()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 179 | // Start the client. |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 180 | ChannelFuture f = bootstrap.connect(ep.host(), ep.port()).sync(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 181 | return f.channel(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void passivateObject(Endpoint ep, Channel channel) |
| 186 | throws Exception { |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public boolean validateObject(Endpoint ep, Channel channel) { |
| 191 | return channel.isOpen(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private class OnosCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
| 196 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 197 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 198 | private final ChannelHandler encoder = new MessageEncoder(); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 199 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 200 | @Override |
| 201 | protected void initChannel(SocketChannel channel) throws Exception { |
| 202 | channel.pipeline() |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 203 | .addLast("encoder", encoder) |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 204 | .addLast("decoder", new MessageDecoder(NettyMessagingService.this)) |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 205 | .addLast("handler", dispatcher); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | private class WriteTask implements Runnable { |
| 210 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 211 | private final InternalMessage message; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 212 | private final Channel channel; |
| 213 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 214 | public WriteTask(Channel channel, InternalMessage message) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 215 | this.channel = channel; |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 216 | this.message = message; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void run() { |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 221 | channel.writeAndFlush(message, channel.voidPromise()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 225 | @ChannelHandler.Sharable |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 226 | private class InboundMessageDispatcher extends SimpleChannelInboundHandler<InternalMessage> { |
| 227 | |
| 228 | @Override |
| 229 | protected void channelRead0(ChannelHandlerContext ctx, InternalMessage message) throws Exception { |
| 230 | String type = message.type(); |
| 231 | if (type.equals(InternalMessage.REPLY_MESSAGE_TYPE)) { |
| 232 | try { |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 233 | AsyncResponse futureResponse = |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 234 | NettyMessagingService.this.responseFutures.getIfPresent(message.id()); |
| 235 | if (futureResponse != null) { |
| 236 | futureResponse.setResponse(message.payload()); |
Madan Jampani | f1d425a | 2014-10-07 09:52:36 -0700 | [diff] [blame] | 237 | } else { |
| 238 | log.warn("Received a reply. But was unable to locate the request handle"); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 239 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 240 | } finally { |
| 241 | NettyMessagingService.this.responseFutures.invalidate(message.id()); |
| 242 | } |
| 243 | return; |
| 244 | } |
| 245 | MessageHandler handler = NettyMessagingService.this.getMessageHandler(type); |
| 246 | handler.handle(message); |
| 247 | } |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 248 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 249 | @Override |
| 250 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame^] | 251 | log.error("Exception inside channel handling pipeline.", cause); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 252 | context.close(); |
| 253 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 254 | } |
| 255 | } |