You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ji...@apache.org on 2020/12/03 03:29:12 UTC

[iotdb] branch cluster_add_snappy updated: buffer will not shrink benethe initial capacity

This is an automated email from the ASF dual-hosted git repository.

jiangtian pushed a commit to branch cluster_add_snappy
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/cluster_add_snappy by this push:
     new 369fb6f  buffer will not shrink benethe initial capacity
369fb6f is described below

commit 369fb6f0eec91ef03f2d434c3d8da00c8a4c8647
Author: jt <jt...@163.com>
AuthorDate: Thu Dec 3 11:27:56 2020 +0800

    buffer will not shrink benethe initial capacity
---
 ...xpandingBuffer.java => AutoResizingBuffer.java} | 25 ++++++++++++++--------
 .../iotdb/rpc/AutoScalingBufferReadTransport.java  |  4 ++--
 .../iotdb/rpc/AutoScalingBufferWriteTransport.java |  6 +++---
 3 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoExpandingBuffer.java b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
similarity index 72%
rename from service-rpc/src/main/java/org/apache/iotdb/rpc/AutoExpandingBuffer.java
rename to service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
index 35a74d8..620b7eb 100644
--- a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoExpandingBuffer.java
+++ b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
@@ -25,20 +25,27 @@ import org.slf4j.LoggerFactory;
 /**
  * Helper class that wraps a byte[] so that it can expand and be reused. Users
  * should call resizeIfNecessary to make sure the buffer has suitable capacity,
- * and then use the array as needed. Note that the internal array will grow at a
- * rate slightly faster than the requested capacity with the (untested)
- * objective of avoiding expensive buffer allocations and copies.
+ * and then use the array as needed.
+ *
+ * Resizing policies:
+ * Expanding:
+ *  If the required size > current capacity * 1.5, expand to the required size, otherwise expand
+ *  to current capacity * 1.5.
+ * Shrinking:
+ *  If initial size < the required size < current capacity * 0.6, and such small requests last
+ *  for more than 5 times, shrink to the middle of the required size and current capacity.
  */
-class AutoExpandingBuffer {
+class AutoResizingBuffer {
   // if resizeIfNecessary is called continuously with a small size for more than
   // MAX_BUFFER_OVERSIZE_TIME times, we will shrink the buffer to reclaim space
   private static final int MAX_BUFFER_OVERSIZE_TIME = 5;
   private byte[] array;
   private int bufTooLargeCounter = MAX_BUFFER_OVERSIZE_TIME;
+  private int initialCapacity;
 
-  private static final Logger logger = LoggerFactory.getLogger(AutoExpandingBuffer.class);
+  private static final Logger logger = LoggerFactory.getLogger(AutoResizingBuffer.class);
 
-  public AutoExpandingBuffer(int initialCapacity) {
+  public AutoResizingBuffer(int initialCapacity) {
     this.array = new byte[initialCapacity];
   }
 
@@ -51,10 +58,10 @@ class AutoExpandingBuffer {
       int newCapacity = Math.max(growCapacity, size);
       this.array = Arrays.copyOf(array, newCapacity);
       bufTooLargeCounter = MAX_BUFFER_OVERSIZE_TIME;
-      logger.info("{} expand from {} to {}", this, currentCapacity, newCapacity);
-    } else if (size > 8 && currentCapacity * loadFactor > size && bufTooLargeCounter-- <= 0) {
+      logger.info("{} expand from {} to {}, request: {}", this, currentCapacity, newCapacity, size);
+    } else if (size > initialCapacity && currentCapacity * loadFactor > size && bufTooLargeCounter-- <= 0) {
       // do not resize if it is reading the request size
-      array = Arrays.copyOf(array, size);
+      array = Arrays.copyOf(array, size + (currentCapacity - size) / 2);
       bufTooLargeCounter = MAX_BUFFER_OVERSIZE_TIME;
       logger.info("{} shrink from {} to {}", this, currentCapacity, size);
     }
diff --git a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java
index d94dbf8..a428343 100644
--- a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java
+++ b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java
@@ -24,12 +24,12 @@ import org.apache.thrift.transport.TTransportException;
 
 public class AutoScalingBufferReadTransport extends TTransport {
 
-  private final AutoExpandingBuffer buf;
+  private final AutoResizingBuffer buf;
   private int pos = 0;
   private int limit = 0;
 
   public AutoScalingBufferReadTransport(int initialCapacity) {
-    this.buf = new AutoExpandingBuffer(initialCapacity);
+    this.buf = new AutoResizingBuffer(initialCapacity);
   }
 
   public void fill(TTransport inTrans, int length) throws TTransportException {
diff --git a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java
index 26807fb..e6eab8f 100644
--- a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java
+++ b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java
@@ -27,11 +27,11 @@ import org.apache.thrift.transport.TTransport;
  */
 public class AutoScalingBufferWriteTransport extends TTransport {
 
-  private final AutoExpandingBuffer buf;
+  private final AutoResizingBuffer buf;
   private int pos;
 
   public AutoScalingBufferWriteTransport(int initialCapacity) {
-    this.buf = new AutoExpandingBuffer(initialCapacity);
+    this.buf = new AutoResizingBuffer(initialCapacity);
     this.pos = 0;
   }
 
@@ -74,7 +74,7 @@ public class AutoScalingBufferWriteTransport extends TTransport {
     buf.resizeIfNecessary(size);
   }
 
-  public AutoExpandingBuffer getBuf() {
+  public AutoResizingBuffer getBuf() {
     return buf;
   }
 }