jt2594838 commented on code in PR #14710:
URL: https://github.com/apache/iotdb/pull/14710#discussion_r1978582183


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/MemoryManager.java:
##########
@@ -471,6 +487,121 @@ public long getUsedMemorySizeInBytes() {
     return memorySize;
   }
 
+  /** Get static allocated memory size in bytes of memory manager */
+  public long getStaticAllocatedMemorySizeInBytes() {
+    long memorySize = staticAllocatedMemorySizeInBytes;
+    for (MemoryManager child : children.values()) {
+      memorySize += child.getStaticAllocatedMemorySizeInBytes();
+    }
+    return memorySize;
+  }
+
+  /** Get used memory ratio */
+  public double getUsedMemoryRatio() {
+    return (double) getUsedMemorySizeInBytes() / totalMemorySizeInBytes;
+  }
+
+  // endregion
+
+  // region auto adapt memory
+  /**
+   * Whether this memory manager is available to shrink
+   *
+   * @return true if available to shrink, otherwise false
+   */
+  public boolean isAvailableToShrink() {
+    return totalAllocatedMemorySizeInBytes - totalMemorySizeInBytes
+            < totalAllocatedMemorySizeInBytes / 10
+        && totalMemorySizeInBytes != allocatedMemorySizeInBytes;
+  }
+
+  /**
+   * Try to shrink this memory manager
+   *
+   * @return actual shrink size
+   */
+  public synchronized long shrink() {
+    long shrinkSize =
+        Math.min(
+            getAvailableMemorySizeInBytes() / 10,
+            totalMemorySizeInBytes - totalAllocatedMemorySizeInBytes * 9 / 10);
+    totalMemorySizeInBytes -= shrinkSize;
+    return shrinkSize;
+  }
+
+  /**
+   * Whether this memory manager is available to expand. If there are one 
child memory manager or
+   * memory block available to expand, return true.
+   *
+   * @return true if available to expand, otherwise false
+   */
+  public boolean isAvailableToExpand() {
+    for (MemoryManager memoryManager : children.values()) {
+      if (memoryManager.isAvailableToExpand()) {
+        return true;
+      }
+    }
+    for (IMemoryBlock memoryBlock : allocatedMemoryBlocks.values()) {
+      if (memoryBlock.getMemoryBlockType() != MemoryBlockType.STATIC) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  public double getScore() {
+    return getUsedMemoryRatio();
+  }

Review Comment:
   This method needs some comments.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/MemoryManager.java:
##########
@@ -46,9 +46,15 @@ public class MemoryManager {
   /** Whether memory management is enabled */
   private final boolean enable;
 
+  /** The total allocate memory size in byte of memory manager */
+  private final long totalAllocatedMemorySizeInBytes;
+
   /** The total memory size in byte of memory manager */
   private long totalMemorySizeInBytes;
 
+  /** The static allocated memory size */
+  private long staticAllocatedMemorySizeInBytes = 0L;
+
   /** The allocated memory size */
   private long allocatedMemorySizeInBytes = 0L;

Review Comment:
   `totalAllocatedMemorySizeInBytes` and `allocatedMemorySizeInBytes` are just 
too confusing, use better names or add more comments.
   Maybe `totalAllocatedMemorySizeInBytes` should be `initiallyAllocated...`?



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/MemoryManager.java:
##########
@@ -471,6 +487,121 @@ public long getUsedMemorySizeInBytes() {
     return memorySize;
   }
 
+  /** Get static allocated memory size in bytes of memory manager */
+  public long getStaticAllocatedMemorySizeInBytes() {
+    long memorySize = staticAllocatedMemorySizeInBytes;
+    for (MemoryManager child : children.values()) {
+      memorySize += child.getStaticAllocatedMemorySizeInBytes();
+    }
+    return memorySize;
+  }
+
+  /** Get used memory ratio */
+  public double getUsedMemoryRatio() {
+    return (double) getUsedMemorySizeInBytes() / totalMemorySizeInBytes;
+  }
+
+  // endregion
+
+  // region auto adapt memory
+  /**
+   * Whether this memory manager is available to shrink
+   *
+   * @return true if available to shrink, otherwise false
+   */
+  public boolean isAvailableToShrink() {
+    return totalAllocatedMemorySizeInBytes - totalMemorySizeInBytes
+            < totalAllocatedMemorySizeInBytes / 10
+        && totalMemorySizeInBytes != allocatedMemorySizeInBytes;
+  }
+
+  /**
+   * Try to shrink this memory manager
+   *
+   * @return actual shrink size
+   */
+  public synchronized long shrink() {
+    long shrinkSize =
+        Math.min(
+            getAvailableMemorySizeInBytes() / 10,
+            totalMemorySizeInBytes - totalAllocatedMemorySizeInBytes * 9 / 10);
+    totalMemorySizeInBytes -= shrinkSize;
+    return shrinkSize;
+  }
+
+  /**
+   * Whether this memory manager is available to expand. If there are one 
child memory manager or
+   * memory block available to expand, return true.
+   *
+   * @return true if available to expand, otherwise false
+   */
+  public boolean isAvailableToExpand() {
+    for (MemoryManager memoryManager : children.values()) {
+      if (memoryManager.isAvailableToExpand()) {
+        return true;
+      }
+    }
+    for (IMemoryBlock memoryBlock : allocatedMemoryBlocks.values()) {
+      if (memoryBlock.getMemoryBlockType() != MemoryBlockType.STATIC) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  public double getScore() {
+    return getUsedMemoryRatio();
+  }
+
+  /** Try to update allocation */
+  public void updateAllocate() {
+    if (children.isEmpty()) {
+      long staticAllocatedMemorySizeInBytes = 
getStaticAllocatedMemorySizeInBytes();
+      double ratio =
+          (double) (totalMemorySizeInBytes - staticAllocatedMemorySizeInBytes)
+              / (totalAllocatedMemorySizeInBytes - 
staticAllocatedMemorySizeInBytes);
+      for (IMemoryBlock memoryBlock : allocatedMemoryBlocks.values()) {
+        if (!memoryBlock.getMemoryBlockType().equals(MemoryBlockType.STATIC)) {
+          memoryBlock.resizeByRatio(ratio);
+        }
+      }
+    } else {
+      // Try to find memory manager with highest and lowest memory usage
+      MemoryManager highestMemoryManager = null;
+      MemoryManager lowestMemoryManager = null;
+      for (MemoryManager child : children.values()) {
+        if (highestMemoryManager == null) {
+          highestMemoryManager = child;
+          lowestMemoryManager = child;
+        } else {
+          if (highestMemoryManager.isAvailableToExpand()
+              && child.getScore() > highestMemoryManager.getScore()) {
+            highestMemoryManager = child;
+          }
+          if (lowestMemoryManager.isAvailableToShrink()
+              && child.getScore() < lowestMemoryManager.getScore()) {
+            lowestMemoryManager = child;
+          }

Review Comment:
   Seeing this, you may just rename `getScore` to `getUsedRatio`.
   Avoid introducing new concepts (score) without any explanation.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/MemoryManager.java:
##########
@@ -471,6 +487,121 @@ public long getUsedMemorySizeInBytes() {
     return memorySize;
   }
 
+  /** Get static allocated memory size in bytes of memory manager */
+  public long getStaticAllocatedMemorySizeInBytes() {
+    long memorySize = staticAllocatedMemorySizeInBytes;
+    for (MemoryManager child : children.values()) {
+      memorySize += child.getStaticAllocatedMemorySizeInBytes();
+    }
+    return memorySize;
+  }
+
+  /** Get used memory ratio */
+  public double getUsedMemoryRatio() {
+    return (double) getUsedMemorySizeInBytes() / totalMemorySizeInBytes;
+  }
+
+  // endregion
+
+  // region auto adapt memory
+  /**
+   * Whether this memory manager is available to shrink
+   *
+   * @return true if available to shrink, otherwise false
+   */
+  public boolean isAvailableToShrink() {
+    return totalAllocatedMemorySizeInBytes - totalMemorySizeInBytes
+            < totalAllocatedMemorySizeInBytes / 10
+        && totalMemorySizeInBytes != allocatedMemorySizeInBytes;
+  }

Review Comment:
   Not fully understand this.
   I thought if this can be shrunk, then it has been expended before, and 
`totalMemorySizeInBytes` should be larger than 
`totalAllocatedMemorySizeInBytes`.
   Please give some further explanation about this method. (Do not only reply 
here, also add in the method comment)



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/IMemoryBlock.java:
##########
@@ -106,6 +106,10 @@ public void setTotalMemorySizeInBytes(final long 
totalMemorySizeInBytes) {
     this.totalMemorySizeInBytes = totalMemorySizeInBytes;
   }
 
+  public void resizeByRatio(double ratio) {
+    totalMemorySizeInBytes = (long) (totalMemorySizeInBytes * ratio);
+  }

Review Comment:
   Make sure the range of `ratio` is checked.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/MemoryPeriodicalJobExecutor.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.commons.memory;
+
+import org.apache.iotdb.commons.concurrent.WrappedRunnable;
+import org.apache.iotdb.commons.concurrent.threadpool.ScheduledExecutorUtil;
+import org.apache.iotdb.commons.utils.TestOnly;
+
+import org.apache.tsfile.utils.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+public class MemoryPeriodicalJobExecutor {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(MemoryPeriodicalJobExecutor.class);
+
+  private final ScheduledExecutorService executorService;
+  private final long minIntervalSeconds;
+
+  private long rounds;
+  private Future<?> executorFuture;
+
+  private final List<Pair<WrappedRunnable, Long>> periodicalJobs = new 
CopyOnWriteArrayList<>();

Review Comment:
   Add some comments explaining what the `Long` is. It is not very 
straightforward.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to