diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-03-27 13:17:21 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-03-27 13:17:21 +0200 |
commit | 2e05ea2d5302dba75ca131379cf9c75a31c41b2d (patch) | |
tree | 94728f9e327b1b43982338de95fbcf2f43f2447d | |
parent | ed5cf1060888038078b7c015acc9684b0f752cea (diff) |
Use a bufferpool to avoid expensive creation of buffers.
4 files changed, 28 insertions, 16 deletions
diff --git a/container-search/src/main/java/com/yahoo/fs4/mplex/Backend.java b/container-search/src/main/java/com/yahoo/fs4/mplex/Backend.java index 202ee94383f..12f8e9e387d 100644 --- a/container-search/src/main/java/com/yahoo/fs4/mplex/Backend.java +++ b/container-search/src/main/java/com/yahoo/fs4/mplex/Backend.java @@ -1,19 +1,29 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.fs4.mplex; - -import com.yahoo.fs4.*; +import com.yahoo.fs4.BasicPacket; +import com.yahoo.fs4.Packet; +import com.yahoo.fs4.PacketDumper; +import com.yahoo.fs4.PacketListener; +import com.yahoo.fs4.PacketNotificationsBroadcaster; +import com.yahoo.fs4.PacketQueryTracer; import com.yahoo.io.Connection; import com.yahoo.io.ConnectionFactory; import com.yahoo.io.Listener; import com.yahoo.vespa.defaults.Defaults; import com.yahoo.yolean.Exceptions; +import com.yahoo.yolean.concurrent.ConcurrentResourcePool; +import com.yahoo.yolean.concurrent.ResourceFactory; +import com.yahoo.yolean.concurrent.ResourcePool; import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; +import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; -import java.util.*; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -23,6 +33,8 @@ import java.util.logging.Logger; */ public class Backend implements ConnectionFactory { + private static int DEFAULT_BUFFER_SIZE = 0x8000; + public static final class BackendStatistics { public final int activeConnections; @@ -61,6 +73,12 @@ public class Backend implements ConnectionFactory { private final ConnectionPool connectionPool; private final PacketDumper packetDumper; private final AtomicInteger connectionCount = new AtomicInteger(0); + private final ConcurrentResourcePool<ByteBuffer> byteBufferResourcePool = new ConcurrentResourcePool<>(new ResourceFactory<>() { + @Override + public ByteBuffer create() { + return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE); + } + }); /** * For unit testing. do not use @@ -116,6 +134,10 @@ public class Backend implements ConnectionFactory { return connection; } + ConcurrentResourcePool<ByteBuffer> getBufferPool() { + return byteBufferResourcePool; + } + /** * Return a connection to the connection pool. If the * connection is not valid anymore we drop it, ie. do not @@ -416,10 +438,6 @@ public class Backend implements ConnectionFactory { } } - public void dumpPackets(final PacketDumper.PacketType packetType, final boolean on) throws IOException { - packetDumper.dumpPackets(packetType, on); - } - public String getHost() { return host; } diff --git a/container-search/src/main/java/com/yahoo/fs4/mplex/FS4Connection.java b/container-search/src/main/java/com/yahoo/fs4/mplex/FS4Connection.java index 599fe0a7cf9..7dcbefde9fa 100644 --- a/container-search/src/main/java/com/yahoo/fs4/mplex/FS4Connection.java +++ b/container-search/src/main/java/com/yahoo/fs4/mplex/FS4Connection.java @@ -36,7 +36,6 @@ public class FS4Connection implements Connection private static int idCounter = 1; private int idNumber; - private int maxInitialSize = 1024; // outbound data private ByteBuffer writeBuffer; @@ -69,7 +68,7 @@ public class FS4Connection implements Connection * Packet sending interface. */ public void sendPacket (BasicPacket packet, Integer channelId) throws IOException { - ByteBuffer buffer = packet.grantEncodingBuffer(channelId.intValue(), ByteBuffer.allocate(maxInitialSize)); + ByteBuffer buffer = packet.grantEncodingBuffer(channelId.intValue(), backend.getBufferPool().alloc()); ByteBuffer viewForPacketListener = buffer.slice(); synchronized (this) { if (!(valid && channel.isOpen())) { @@ -79,9 +78,6 @@ public class FS4Connection implements Connection ", isOpen = " + channel.isOpen()); } - if (buffer.capacity() > maxInitialSize) { - maxInitialSize = buffer.limit(); - } if (writeBuffer == null) { writeBuffer = buffer; } else { @@ -131,6 +127,8 @@ public class FS4Connection implements Connection // buffer drained so we forget it and see what happens when we // go around. if indeed we go around if (!writeBuffer.hasRemaining()) { + writeBuffer.clear(); + backend.getBufferPool().free(writeBuffer); writeBuffer = null; } } while (bytesWritten > 0); diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java index 6f5fb591ba6..24d4cfe4318 100644 --- a/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java +++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java @@ -1,8 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.yolean.concurrent; -import com.yahoo.yolean.concurrent.ResourceFactory; - import java.util.Iterator; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java index 62d5d749604..40c5ca3b6c2 100644 --- a/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java +++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java @@ -1,8 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.yolean.concurrent; -import com.yahoo.yolean.concurrent.ResourceFactory; - import java.util.ArrayDeque; import java.util.Deque; import java.util.Iterator; |