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 06:55:27 UTC

[iotdb] branch cluster_add_snappy updated: limit shrink frequency

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 aa059e1  limit shrink frequency
aa059e1 is described below

commit aa059e1782f343b04081420dabb508b105cd47e5
Author: jt <jt...@163.com>
AuthorDate: Thu Dec 3 14:54:02 2020 +0800

    limit shrink frequency
---
 .../org/apache/iotdb/rpc/AutoResizingBuffer.java   | 34 +++++++++++++---------
 .../iotdb/rpc/TSnappyElasticFramedTransport.java   |  8 +++++
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
index c7ec4e8..f2313f4 100644
--- a/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
+++ b/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
@@ -23,25 +23,27 @@ import org.slf4j.Logger;
 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.
- *
- * 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.
+ * 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.
+ * <p>
+ * 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 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 static final long MIN_SHRINK_INTERVAL = 60_000L;
+
   private byte[] array;
   private int bufTooLargeCounter = MAX_BUFFER_OVERSIZE_TIME;
   private int initialCapacity;
+  private long lastShrinkTime;
+
 
   private static final Logger logger = LoggerFactory.getLogger(AutoResizingBuffer.class);
 
@@ -59,11 +61,15 @@ class AutoResizingBuffer {
       int newCapacity = Math.max(growCapacity, size);
       this.array = Arrays.copyOf(array, newCapacity);
       bufTooLargeCounter = MAX_BUFFER_OVERSIZE_TIME;
-      logger.debug("{} 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
+      logger
+          .debug("{} expand from {} to {}, request: {}", this, currentCapacity, newCapacity, size);
+    } else if (size > initialCapacity && currentCapacity * loadFactor > size
+        && bufTooLargeCounter-- <= 0
+        && System.currentTimeMillis() - lastShrinkTime > MIN_SHRINK_INTERVAL) {
+      // do not resize if it is reading the request size and do not shrink too often
       array = Arrays.copyOf(array, size + (currentCapacity - size) / 2);
       bufTooLargeCounter = MAX_BUFFER_OVERSIZE_TIME;
+      lastShrinkTime = System.currentTimeMillis();
       logger.debug("{} shrink from {} to {}", this, currentCapacity, size);
     }
   }
diff --git a/service-rpc/src/main/java/org/apache/iotdb/rpc/TSnappyElasticFramedTransport.java b/service-rpc/src/main/java/org/apache/iotdb/rpc/TSnappyElasticFramedTransport.java
index d938fb6..757e90c 100644
--- a/service-rpc/src/main/java/org/apache/iotdb/rpc/TSnappyElasticFramedTransport.java
+++ b/service-rpc/src/main/java/org/apache/iotdb/rpc/TSnappyElasticFramedTransport.java
@@ -25,6 +25,14 @@ import org.xerial.snappy.Snappy;
 
 public class TSnappyElasticFramedTransport extends TCompressedElasticFramedTransport {
 
+  /**
+   * How big should the default read and write buffers be?
+   */
+  public static final int DEFAULT_BUF_CAPACITY = 4 * 1024 * 1024;
+  /**
+   * How big is the largest allowable frame? Defaults to 16MB.
+   */
+  public static final int DEFAULT_MAX_LENGTH = 16384000;
 
   public static class Factory extends TTransportFactory {