Correct input buffer sizing
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
index 077fa1b117cea880e41e3ae2ec8942fea688b1eb..13b31c7e084f125b7946fc1afa7111f192f37735 100644 (file)
@@ -7,9 +7,9 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.buffer.PooledByteBufAllocator;
@@ -18,6 +18,7 @@ import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelOption;
 import io.netty.channel.EventLoopGroup;
+import io.netty.channel.WriteBufferWaterMark;
 import io.netty.channel.epoll.Epoll;
 import io.netty.channel.epoll.EpollChannelOption;
 import io.netty.channel.epoll.EpollEventLoopGroup;
@@ -51,55 +52,46 @@ import org.slf4j.LoggerFactory;
 public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(BGPDispatcherImpl.class);
     private static final int SOCKET_BACKLOG_SIZE = 128;
-    private static final int HIGH_WATER_MARK = 256 * 1024;
-    private static final int LOW_WATER_MARK = 128 * 1024;
     private static final long TIMEOUT = 10;
 
+    private static final WriteBufferWaterMark WATER_MARK = new WriteBufferWaterMark(128 * 1024, 256 * 1024);
+
     private final BGPHandlerFactory handlerFactory;
     private final EventLoopGroup bossGroup;
     private final EventLoopGroup workerGroup;
     private final BGPPeerRegistry bgpPeerRegistry;
 
     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup,
-        final EventLoopGroup workerGroup, final BGPPeerRegistry bgpPeerRegistry) {
+            final EventLoopGroup workerGroup, final BGPPeerRegistry bgpPeerRegistry) {
         if (Epoll.isAvailable()) {
             this.bossGroup = new EpollEventLoopGroup();
             this.workerGroup = new EpollEventLoopGroup();
         } else {
-            this.bossGroup = Preconditions.checkNotNull(bossGroup);
-            this.workerGroup = Preconditions.checkNotNull(workerGroup);
+            this.bossGroup = requireNonNull(bossGroup);
+            this.workerGroup = requireNonNull(workerGroup);
         }
-        this.bgpPeerRegistry = Preconditions.checkNotNull(bgpPeerRegistry);
+        this.bgpPeerRegistry = requireNonNull(bgpPeerRegistry);
         this.handlerFactory = new BGPHandlerFactory(messageRegistry);
     }
 
-    @Override
-    public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress, final int retryTimer) {
-        return createClient(remoteAddress, retryTimer, createClientBootStrap(Optional.absent(), false));
-    }
-
-    private synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress,
-        final int retryTimer, final Bootstrap clientBootStrap) {
+    @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, localAddress);
         final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.bgpPeerRegistry);
-        final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf);
+        final ChannelPipelineInitializer<BGPSessionImpl> initializer = BGPChannel.createChannelPipelineInitializer(
+                this.handlerFactory, snf);
 
-        final BGPProtocolSessionPromise sessionPromise = new BGPProtocolSessionPromise(remoteAddress, retryTimer,
-            clientBootStrap, this.bgpPeerRegistry);
+        final BGPProtocolSessionPromise<BGPSessionImpl> sessionPromise = new BGPProtocolSessionPromise<>(remoteAddress,
+                retryTimer, clientBootStrap, this.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(Optional.absent(), reuseAddress);
-        clientBootStrap.localAddress(localAddress);
-        return createClient(remoteAddress, retryTimer, clientBootStrap);
-    }
-
-    private synchronized Bootstrap createClientBootStrap(final Optional<KeyMapping> keys, final boolean reuseAddress) {
+    private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress,
+            final InetSocketAddress localAddress) {
         final Bootstrap bootstrap = new Bootstrap();
         if (Epoll.isAvailable()) {
             bootstrap.channel(EpollSocketChannel.class);
@@ -107,30 +99,30 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
         } else {
             bootstrap.channel(NioSocketChannel.class);
         }
-        if (keys.isPresent()) {
+        if (keys != null && !keys.isEmpty()) {
             if (Epoll.isAvailable()) {
-                bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys.get());
+                bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
             } else {
                 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
             }
         }
 
         // Make sure we are doing round-robin processing
-        bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, BGPMessageHeaderDecoder.getRecvAllocator());
         bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
-        bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
-        bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
+        bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
         bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
 
-        if (bootstrap.group() == null) {
+        if (bootstrap.config().group() == null) {
             bootstrap.group(this.workerGroup);
         }
+        bootstrap.localAddress(localAddress);
 
         return bootstrap;
     }
 
     @Override
-    public synchronized void close() throws InterruptedException {
+    public synchronized void close() {
         if (Epoll.isAvailable()) {
             LOG.debug("Closing Dispatcher");
             this.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
@@ -140,20 +132,19 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
 
     @Override
     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
-            final int retryTimer, final Optional<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
-    protected synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
-        final int retryTimer, final Optional<KeyMapping> keys, final InetSocketAddress localAddress,
-        final boolean reuseAddress) {
+    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 BGPReconnectPromise reconnectPromise = new BGPReconnectPromise(GlobalEventExecutor.INSTANCE,
-            remoteAddress, retryTimer, bootstrap, this.bgpPeerRegistry,
-            BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf));
+        final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress, localAddress);
+        final BGPReconnectPromise<?> reconnectPromise = new BGPReconnectPromise<>(GlobalEventExecutor.INSTANCE,
+                remoteAddress, retryTimer, bootstrap, this.bgpPeerRegistry,
+                BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf));
         reconnectPromise.connect();
         return reconnectPromise;
     }
@@ -161,7 +152,8 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
     @Override
     public synchronized ChannelFuture createServer(final InetSocketAddress serverAddress) {
         final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.bgpPeerRegistry);
-        final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf);
+        final ChannelPipelineInitializer<?> initializer = BGPChannel.createChannelPipelineInitializer(
+            this.handlerFactory, snf);
         final ServerBootstrap serverBootstrap = createServerBootstrap(initializer);
         final ChannelFuture channelFuture = serverBootstrap.bind(serverAddress);
         LOG.debug("Initiated server {} at {}.", channelFuture, serverAddress);
@@ -173,7 +165,7 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
         return this.bgpPeerRegistry;
     }
 
-    private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
+    private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer<?> initializer) {
         final ServerBootstrap serverBootstrap = new ServerBootstrap();
         if (Epoll.isAvailable()) {
             serverBootstrap.channel(EpollServerSocketChannel.class);
@@ -186,13 +178,12 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
 
         serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
         serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
-        serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
-        serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
+        serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
 
         // Make sure we are doing round-robin processing
-        serverBootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+        serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, BGPMessageHeaderDecoder.getRecvAllocator());
 
-        if (serverBootstrap.group() == null) {
+        if (serverBootstrap.config().group() == null) {
             serverBootstrap.group(this.bossGroup, this.workerGroup);
         }
         return serverBootstrap;
@@ -205,8 +196,8 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
 
         }
 
-        static <T extends BGPSessionNegotiatorFactory> ChannelPipelineInitializer
-        createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
+        static <S extends BGPSession, T extends BGPSessionNegotiatorFactory<S>> ChannelPipelineInitializer<S>
+            createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
             return (channel, promise) -> {
                 channel.pipeline().addLast(hf.getDecoders());
                 channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
@@ -214,7 +205,8 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
             };
         }
 
-        static <S extends BGPSession> ChannelHandler createClientChannelHandler(final ChannelPipelineInitializer initializer, final Promise<S> promise) {
+        static <S extends BGPSession> ChannelHandler createClientChannelHandler(
+                final ChannelPipelineInitializer<S> initializer, final Promise<S> promise) {
             return new ChannelInitializer<SocketChannel>() {
                 @Override
                 protected void initChannel(final SocketChannel channel) {
@@ -223,11 +215,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
                 protected void initChannel(final SocketChannel channel) {
-                    initializer.initializeChannel(channel, new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
+                    initializer.initializeChannel(channel, new DefaultPromise<>(GlobalEventExecutor.INSTANCE));
                 }
             };
         }