Disconnect bgp-rib-impl from global event loop groups
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
index 61a930055942e43edc454535aa474fb495597259..5d321ba29aa65c9c10550a87f94add47ceca4c4c 100644 (file)
@@ -13,29 +13,22 @@ import com.google.common.annotations.VisibleForTesting;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.buffer.PooledByteBufAllocator;
+import io.netty.channel.AdaptiveRecvByteBufAllocator;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelOption;
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.FixedRecvByteBufAllocator;
+import io.netty.channel.RecvByteBufAllocator;
 import io.netty.channel.WriteBufferWaterMark;
-import io.netty.channel.epoll.Epoll;
-import io.netty.channel.epoll.EpollChannelOption;
-import io.netty.channel.epoll.EpollEventLoopGroup;
-import io.netty.channel.epoll.EpollMode;
-import io.netty.channel.epoll.EpollServerSocketChannel;
-import io.netty.channel.epoll.EpollSocketChannel;
 import io.netty.channel.socket.SocketChannel;
-import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.channel.socket.nio.NioSocketChannel;
 import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.GlobalEventExecutor;
 import io.netty.util.concurrent.Promise;
 import java.net.InetSocketAddress;
-import java.util.concurrent.TimeUnit;
-import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPProtocolSessionPromise;
 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPReconnectPromise;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
@@ -44,131 +37,93 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionNegotiatorFactory;
 import org.opendaylight.protocol.concepts.KeyMapping;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of BGPDispatcher.
  */
-public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
+@Singleton
+@Component(immediate = true)
+public final class BGPDispatcherImpl implements BGPDispatcher {
     private static final Logger LOG = LoggerFactory.getLogger(BGPDispatcherImpl.class);
     private static final int SOCKET_BACKLOG_SIZE = 128;
-    private static final int FIX_BUFFER_SIZE = 1;
-    private static final long TIMEOUT = 10;
 
     private static final WriteBufferWaterMark WATER_MARK = new WriteBufferWaterMark(128 * 1024, 256 * 1024);
 
+    // An adaptive allocator, so we size our message buffers based on what we receive, but make sure we process one
+    // message at a time. This should be good enough for most cases, although we could optimize it a bit based on
+    // whether we actually negotiate use of large messages -- based on that the range of allocations can be constrained
+    // from the default 64-65536 range to 64-4096.
+    private static final RecvByteBufAllocator RECV_ALLOCATOR = new AdaptiveRecvByteBufAllocator().maxMessagesPerRead(1);
+
     private final BGPHandlerFactory handlerFactory;
-    private final EventLoopGroup bossGroup;
-    private final EventLoopGroup workerGroup;
     private final BGPPeerRegistry bgpPeerRegistry;
+    private final BGPNettyGroups nettyGroups;
 
-    public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup,
-            final EventLoopGroup workerGroup, final BGPPeerRegistry bgpPeerRegistry) {
-        if (Epoll.isAvailable()) {
-            this.bossGroup = new EpollEventLoopGroup();
-            this.workerGroup = new EpollEventLoopGroup();
-        } else {
-            this.bossGroup = requireNonNull(bossGroup);
-            this.workerGroup = requireNonNull(workerGroup);
-        }
+    @Inject
+    @Activate
+    public BGPDispatcherImpl(@Reference final BGPExtensionConsumerContext extensions,
+            @Reference final BGPNettyGroups nettyGroups, @Reference final BGPPeerRegistry bgpPeerRegistry) {
+        this.nettyGroups = requireNonNull(nettyGroups);
         this.bgpPeerRegistry = requireNonNull(bgpPeerRegistry);
-        this.handlerFactory = new BGPHandlerFactory(messageRegistry);
+        handlerFactory = new BGPHandlerFactory(extensions.getMessageRegistry());
     }
 
-    @Override
-    public synchronized Future<BGPSessionImpl> createClient(
-            final InetSocketAddress remoteAddress,
-            final int retryTimer) {
-        return createClient(remoteAddress, retryTimer,
-                createClientBootStrap(KeyMapping.getKeyMapping(), false));
-    }
-
-    private synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress,
-            final int retryTimer, final Bootstrap clientBootStrap) {
-        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.bgpPeerRegistry);
+    @VisibleForTesting
+    public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress,
+            final InetSocketAddress remoteAddress, final int retryTimer, final boolean reuseAddress) {
+        final Bootstrap clientBootStrap = createClientBootStrap(KeyMapping.of(), reuseAddress, localAddress);
+        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(bgpPeerRegistry);
         final ChannelPipelineInitializer<BGPSessionImpl> initializer = BGPChannel.createChannelPipelineInitializer(
-                this.handlerFactory, snf);
+                handlerFactory, snf);
 
         final BGPProtocolSessionPromise<BGPSessionImpl> sessionPromise = new BGPProtocolSessionPromise<>(remoteAddress,
-                retryTimer, clientBootStrap, this.bgpPeerRegistry);
+                retryTimer, clientBootStrap, bgpPeerRegistry);
         clientBootStrap.handler(BGPChannel.createClientChannelHandler(initializer, sessionPromise));
         sessionPromise.connect();
         LOG.debug("Client created.");
         return sessionPromise;
     }
 
-    @VisibleForTesting
-    public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress,
-            final InetSocketAddress remoteAddress, final int retryTimer, final boolean reuseAddress) {
-        final Bootstrap clientBootStrap = createClientBootStrap(KeyMapping.getKeyMapping(), reuseAddress);
-        clientBootStrap.localAddress(localAddress);
-        return createClient(remoteAddress, retryTimer, clientBootStrap);
-    }
-
-    private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress) {
-        final Bootstrap bootstrap = new Bootstrap();
-        if (Epoll.isAvailable()) {
-            bootstrap.channel(EpollSocketChannel.class);
-            bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
-        } else {
-            bootstrap.channel(NioSocketChannel.class);
-        }
-        if (keys != null && !keys.isEmpty()) {
-            if (Epoll.isAvailable()) {
-                bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
-            } else {
-                throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
-            }
-        }
-
-        // Make sure we are doing round-robin processing
-        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(FIX_BUFFER_SIZE));
-        bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
-        bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
-        bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
-
-        if (bootstrap.config().group() == null) {
-            bootstrap.group(this.workerGroup);
-        }
-
-        return bootstrap;
-    }
-
-    @Override
-    public synchronized void close() throws InterruptedException {
-        if (Epoll.isAvailable()) {
-            LOG.debug("Closing Dispatcher");
-            this.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
-            this.bossGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
-        }
+    private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress,
+            final InetSocketAddress localAddress) {
+        return nettyGroups.createBootstrap(keys)
+            // Make sure we are doing round-robin processing
+            .option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR)
+            .option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
+            .option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK)
+            .option(ChannelOption.SO_REUSEADDR, reuseAddress)
+            .localAddress(localAddress);
     }
 
     @Override
     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
-            final int retryTimer, final KeyMapping keys) {
-        return createReconnectingClient(remoteAddress, retryTimer, keys, null, false);
+            final InetSocketAddress localAddress, final int retryTimer, final KeyMapping keys) {
+        return createReconnectingClient(remoteAddress, retryTimer, keys, localAddress, false);
     }
 
     @VisibleForTesting
     synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
             final int retryTimer, final KeyMapping keys, final InetSocketAddress localAddress,
             final boolean reuseAddress) {
-        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.bgpPeerRegistry);
-        final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress);
-        bootstrap.localAddress(localAddress);
+        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(bgpPeerRegistry);
+        final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress, localAddress);
         final BGPReconnectPromise<?> reconnectPromise = new BGPReconnectPromise<>(GlobalEventExecutor.INSTANCE,
-                remoteAddress, retryTimer, bootstrap, this.bgpPeerRegistry,
-                BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf));
+                remoteAddress, retryTimer, bootstrap, bgpPeerRegistry,
+                BGPChannel.createChannelPipelineInitializer(handlerFactory, snf));
         reconnectPromise.connect();
         return reconnectPromise;
     }
 
     @Override
     public synchronized ChannelFuture createServer(final InetSocketAddress serverAddress) {
-        final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.bgpPeerRegistry);
-        final ChannelPipelineInitializer<?> initializer = BGPChannel.
-                createChannelPipelineInitializer(this.handlerFactory, snf);
+        final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(bgpPeerRegistry);
+        final ChannelPipelineInitializer<?> initializer = BGPChannel.createChannelPipelineInitializer(
+            handlerFactory, snf);
         final ServerBootstrap serverBootstrap = createServerBootstrap(initializer);
         final ChannelFuture channelFuture = serverBootstrap.bind(serverAddress);
         LOG.debug("Initiated server {} at {}.", channelFuture, serverAddress);
@@ -177,31 +132,17 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
 
     @Override
     public BGPPeerRegistry getBGPPeerRegistry() {
-        return this.bgpPeerRegistry;
+        return bgpPeerRegistry;
     }
 
-    private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
-        final ServerBootstrap serverBootstrap = new ServerBootstrap();
-        if (Epoll.isAvailable()) {
-            serverBootstrap.channel(EpollServerSocketChannel.class);
-            serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
-        } else {
-            serverBootstrap.channel(NioServerSocketChannel.class);
-        }
-        final ChannelHandler serverChannelHandler = BGPChannel.createServerChannelHandler(initializer);
-        serverBootstrap.childHandler(serverChannelHandler);
-
-        serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
-        serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
-        serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
-
-        // Make sure we are doing round-robin processing
-        serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(FIX_BUFFER_SIZE));
-
-        if (serverBootstrap.config().group() == null) {
-            serverBootstrap.group(this.bossGroup, this.workerGroup);
-        }
-        return serverBootstrap;
+    private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer<?> initializer) {
+        return nettyGroups.createServerBootstrap()
+            .childHandler(BGPChannel.createServerChannelHandler(initializer))
+            .option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE)
+            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
+            .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK)
+            // Make sure we are doing round-robin processing
+            .option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR);
     }
 
     private static final class BGPChannel {
@@ -212,7 +153,7 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
         }
 
         static <S extends BGPSession, T extends BGPSessionNegotiatorFactory<S>> ChannelPipelineInitializer<S>
-        createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
+            createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
             return (channel, promise) -> {
                 channel.pipeline().addLast(hf.getDecoders());
                 channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
@@ -230,13 +171,12 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
             };
         }
 
-        static ChannelHandler createServerChannelHandler(final ChannelPipelineInitializer initializer) {
+        static <S extends BGPSession> ChannelHandler createServerChannelHandler(
+                final ChannelPipelineInitializer<S> initializer) {
             return new ChannelInitializer<SocketChannel>() {
                 @Override
-                @SuppressWarnings("unchecked")
                 protected void initChannel(final SocketChannel channel) {
-                    initializer.initializeChannel(channel,
-                            new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
+                    initializer.initializeChannel(channel, new DefaultPromise<>(GlobalEventExecutor.INSTANCE));
                 }
             };
         }