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


##########
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:
   Sorry, it's my mistake and this method is not for this pr, and I will remove 
this



##########
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:
   Sorry, it's my mistake and this method is not for this pr, and I will remove 
this



##########
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:
   Sorry, it's my mistake and this method is not for this pr, and I will remove 
this



##########
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:
   Sorry, it's my mistake and this method is not for this pr, and I will remove 
this



-- 
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