Use adaptive allocators again 06/83306/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 29 Jul 2019 16:30:38 +0000 (18:30 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 29 Jul 2019 19:49:35 +0000 (19:49 +0000)
This is a fixup of I865f8c2c949c226d69be8981e1070e96089b3ccb,
which restores the use of adaptive buffer allocators. While the
previous fix was a notable improvement in performance, it could
end up wasting memory on small input messages.

Change-Id: I74b5b5ddfa74b52b86ecd5ba9b9bd8779b4a9e8d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit cafb1376e7b8e8a239d3bcb08f4ee5c592e5c214)

bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPMessageHeaderDecoder.java

index 13b31c7e084f125b7946fc1afa7111f192f37735..dee8a88b1ab669ca5775a13e601359332a53c0a7 100644 (file)
@@ -13,11 +13,13 @@ 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.RecvByteBufAllocator;
 import io.netty.channel.WriteBufferWaterMark;
 import io.netty.channel.epoll.Epoll;
 import io.netty.channel.epoll.EpollChannelOption;
@@ -56,6 +58,12 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
 
     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;
@@ -108,7 +116,7 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
         }
 
         // Make sure we are doing round-robin processing
-        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, BGPMessageHeaderDecoder.getRecvAllocator());
+        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR);
         bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
         bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
         bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
@@ -181,7 +189,7 @@ public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
         serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
 
         // Make sure we are doing round-robin processing
-        serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, BGPMessageHeaderDecoder.getRecvAllocator());
+        serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR);
 
         if (serverBootstrap.config().group() == null) {
             serverBootstrap.group(this.bossGroup, this.workerGroup);
index bc1cf5b9c6ea32492eb7be9321ef3d42e137e863..403e1022540aacc9e5f1fc3fd9805d318f0d8efe 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
-import io.netty.channel.FixedRecvByteBufAllocator;
 import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
 
 /**
@@ -27,11 +26,6 @@ final class BGPMessageHeaderDecoder extends LengthFieldBasedFrameDecoder {
 
     private static final int EXTENDED_MAX_FRAME_SIZE = 65535;
 
-    // Allocators
-    private static final FixedRecvByteBufAllocator RECV_ALLOCATOR = new FixedRecvByteBufAllocator(MAX_FRAME_SIZE);
-    private static final FixedRecvByteBufAllocator EXTENDED_RECV_ALLOCATOR =
-            new FixedRecvByteBufAllocator(EXTENDED_MAX_FRAME_SIZE);
-
     /*
 
      0                   1                   2                   3
@@ -55,14 +49,6 @@ final class BGPMessageHeaderDecoder extends LengthFieldBasedFrameDecoder {
         super(maxFrameSize, MARKER_SIZE, LENGTH_SIZE, -MARKER_SIZE - LENGTH_SIZE, 0);
     }
 
-    static FixedRecvByteBufAllocator getRecvAllocator() {
-        return RECV_ALLOCATOR;
-    }
-
-    static FixedRecvByteBufAllocator getExtendedRecvAllocator() {
-        return EXTENDED_RECV_ALLOCATOR;
-    }
-
     static BGPMessageHeaderDecoder getBGPMessageHeaderDecoder() {
         return new BGPMessageHeaderDecoder(MAX_FRAME_SIZE);
     }