You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by we...@apache.org on 2026/04/30 02:45:16 UTC

(iotdb) branch lwh/fixMemoryBlock created (now 1b9d68e0b70)

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

Wei-hao-Li pushed a change to branch lwh/fixMemoryBlock
in repository https://gitbox.apache.org/repos/asf/iotdb.git


      at 1b9d68e0b70 add queryId into account

This branch includes the following new commits:

     new 85ca0d1893d fix allocate of MemoryBlock
     new 1b9d68e0b70 add queryId into account

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(iotdb) 01/02: fix allocate of MemoryBlock

Posted by we...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

Wei-hao-Li pushed a commit to branch lwh/fixMemoryBlock
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 85ca0d1893d38b8543b28bc75338852d4867413c
Author: Weihao Li <18...@163.com>
AuthorDate: Thu Apr 30 10:38:49 2026 +0800

    fix allocate of MemoryBlock
    
    Signed-off-by: Weihao Li <18...@163.com>
---
 .../commons/memory/AtomicLongMemoryBlock.java      | 41 +++++++++++-----------
 .../iotdb/commons/memory/MemoryBlockTest.java      | 41 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 21 deletions(-)

diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/AtomicLongMemoryBlock.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/AtomicLongMemoryBlock.java
index 1376d8ddb6d..9663eb0e689 100644
--- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/AtomicLongMemoryBlock.java
+++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/memory/AtomicLongMemoryBlock.java
@@ -23,7 +23,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 
 public class AtomicLongMemoryBlock extends IMemoryBlock {
@@ -58,30 +57,30 @@ public class AtomicLongMemoryBlock extends IMemoryBlock {
 
   @Override
   public boolean allocate(long sizeInByte) {
-    AtomicBoolean result = new AtomicBoolean(false);
-    usedMemoryInBytes.updateAndGet(
-        memCost -> {
-          if (memCost + sizeInByte > totalMemorySizeInBytes) {
-            return memCost;
-          }
-          result.set(true);
-          return memCost + sizeInByte;
-        });
-    return result.get();
+    long prev;
+    long next;
+    do {
+      prev = usedMemoryInBytes.get();
+      if (prev + sizeInByte > totalMemorySizeInBytes) {
+        return false;
+      }
+      next = prev + sizeInByte;
+    } while (!usedMemoryInBytes.compareAndSet(prev, next));
+    return true;
   }
 
   @Override
   public boolean allocateIfSufficient(final long sizeInByte, final double maxRatio) {
-    AtomicBoolean result = new AtomicBoolean(false);
-    usedMemoryInBytes.updateAndGet(
-        memCost -> {
-          if (memCost + sizeInByte > totalMemorySizeInBytes * maxRatio) {
-            return memCost;
-          }
-          result.set(true);
-          return memCost + sizeInByte;
-        });
-    return result.get();
+    long prev;
+    long next;
+    do {
+      prev = usedMemoryInBytes.get();
+      if (prev + sizeInByte > totalMemorySizeInBytes * maxRatio) {
+        return false;
+      }
+      next = prev + sizeInByte;
+    } while (!usedMemoryInBytes.compareAndSet(prev, next));
+    return true;
   }
 
   @Override
diff --git a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/memory/MemoryBlockTest.java b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/memory/MemoryBlockTest.java
index dee41447aad..9291858c4a6 100644
--- a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/memory/MemoryBlockTest.java
+++ b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/memory/MemoryBlockTest.java
@@ -21,6 +21,9 @@ package org.apache.iotdb.commons.memory;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicInteger;
+
 public class MemoryBlockTest {
 
   @Test
@@ -64,4 +67,42 @@ public class MemoryBlockTest {
     memoryBlock1.close();
     memoryBlock2.close();
   }
+
+  @Test
+  public void testAllocateNoPhantomSuccessUnderContention() throws InterruptedException {
+    final int rounds = 2000;
+    for (int i = 0; i < rounds; i++) {
+      AtomicLongMemoryBlock memoryBlock = new AtomicLongMemoryBlock("ContentionBlock", null, 1);
+      AtomicInteger successCount = new AtomicInteger(0);
+      CountDownLatch startLatch = new CountDownLatch(1);
+      CountDownLatch doneLatch = new CountDownLatch(2);
+
+      Runnable allocateTask =
+          () -> {
+            try {
+              startLatch.await();
+              if (memoryBlock.allocate(1)) {
+                successCount.incrementAndGet();
+              }
+            } catch (InterruptedException e) {
+              Thread.currentThread().interrupt();
+              throw new RuntimeException(e);
+            } finally {
+              doneLatch.countDown();
+            }
+          };
+
+      Thread t1 = new Thread(allocateTask, "memory-block-test-1");
+      Thread t2 = new Thread(allocateTask, "memory-block-test-2");
+      t1.start();
+      t2.start();
+      startLatch.countDown();
+      doneLatch.await();
+
+      Assert.assertEquals(1, successCount.get());
+      Assert.assertEquals(1, memoryBlock.getUsedMemoryInBytes());
+      Assert.assertEquals(0, memoryBlock.release(1));
+      Assert.assertEquals(0, memoryBlock.getUsedMemoryInBytes());
+    }
+  }
 }


(iotdb) 02/02: add queryId into account

Posted by we...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

Wei-hao-Li pushed a commit to branch lwh/fixMemoryBlock
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 1b9d68e0b700fffe00e60bf7ed64a5afc3cf46f2
Author: Weihao Li <18...@163.com>
AuthorDate: Thu Apr 30 10:43:31 2026 +0800

    add queryId into account
    
    Signed-off-by: Weihao Li <18...@163.com>
---
 .../src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
index 9cee0e71a97..d0c256a34c3 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
@@ -1216,7 +1216,9 @@ public class Coordinator {
 
     @Override
     public long ramBytesUsed() {
+      // The result of this method is fixed
       return INSTANCE_SIZE
+          + sizeOfCharArray(queryId.length())
           + sizeOfCharArray(statement.length())
           + sizeOfCharArray(user.length())
           + sizeOfCharArray(clientHost.length());