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

Caideyipi pushed a commit to branch dev/1.3
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/dev/1.3 by this push:
     new 3df2e9a5430 [To dev/1.3] Fix schema region snapshot memory accounting 
(#18750)
3df2e9a5430 is described below

commit 3df2e9a543027b69bd269e0f812a44a3f81acca9
Author: Caideyipi <[email protected]>
AuthorDate: Wed Nov 4 16:30:14 2026 +0800

    [To dev/1.3] Fix schema region snapshot memory accounting (#18750)
---
 .../schemaregion/impl/SchemaRegionMemoryImpl.java  | 10 ++++--
 .../schemaregion/impl/SchemaRegionPBTreeImpl.java  | 22 ++++++++----
 .../schemaRegion/SchemaRegionManagementTest.java   | 40 ++++++++++++++++++++++
 3 files changed, 64 insertions(+), 8 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionMemoryImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionMemoryImpl.java
index 65e8e2f95ee..21b3de4315c 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionMemoryImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionMemoryImpl.java
@@ -147,6 +147,7 @@ public class SchemaRegionMemoryImpl implements 
ISchemaRegion {
 
   private boolean isRecovering = true;
   private volatile boolean initialized = false;
+  private boolean ratisLogAppenderBufferReserved = false;
 
   private final String storageGroupDirPath;
   private final String schemaRegionDirPath;
@@ -197,7 +198,10 @@ public class SchemaRegionMemoryImpl implements 
ISchemaRegion {
       return;
     }
 
-    if 
(config.getSchemaRegionConsensusProtocolClass().equals(ConsensusFactory.RATIS_CONSENSUS))
 {
+    if (!ratisLogAppenderBufferReserved
+        && config
+            .getSchemaRegionConsensusProtocolClass()
+            .equals(ConsensusFactory.RATIS_CONSENSUS)) {
       final long memCost = 
config.getSchemaRatisConsensusLogAppenderBufferSizeMax();
       if (!SystemInfo.getInstance().addDirectBufferMemoryCost(memCost)) {
         throw new MetadataException(
@@ -206,6 +210,7 @@ public class SchemaRegionMemoryImpl implements 
ISchemaRegion {
                 + ", which is greater than limit mem cost: "
                 + 
SystemInfo.getInstance().getTotalDirectBufferMemorySizeLimit());
       }
+      ratisLogAppenderBufferReserved = true;
     }
 
     initDir();
@@ -424,9 +429,10 @@ public class SchemaRegionMemoryImpl implements 
ISchemaRegion {
 
     // delete all the schema region files
     SchemaRegionUtils.deleteSchemaRegionFolder(schemaRegionDirPath, logger);
-    if 
(config.getSchemaRegionConsensusProtocolClass().equals(ConsensusFactory.RATIS_CONSENSUS))
 {
+    if (ratisLogAppenderBufferReserved) {
       SystemInfo.getInstance()
           
.decreaseDirectBufferMemoryCost(config.getSchemaRatisConsensusLogAppenderBufferSizeMax());
+      ratisLogAppenderBufferReserved = false;
     }
   }
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionPBTreeImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionPBTreeImpl.java
index b78f570125a..02cdb960e24 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionPBTreeImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionPBTreeImpl.java
@@ -149,6 +149,7 @@ public class SchemaRegionPBTreeImpl implements 
ISchemaRegion {
   private boolean isRecovering = true;
   private volatile boolean initialized = false;
   private boolean isClearing = false;
+  private boolean ratisLogAppenderBufferReserved = false;
 
   private final String storageGroupDirPath;
   private final String schemaRegionDirPath;
@@ -193,7 +194,10 @@ public class SchemaRegionPBTreeImpl implements 
ISchemaRegion {
       return;
     }
 
-    if 
(config.getSchemaRegionConsensusProtocolClass().equals(ConsensusFactory.RATIS_CONSENSUS))
 {
+    if (!ratisLogAppenderBufferReserved
+        && config
+            .getSchemaRegionConsensusProtocolClass()
+            .equals(ConsensusFactory.RATIS_CONSENSUS)) {
       long memCost = config.getSchemaRatisConsensusLogAppenderBufferSizeMax();
       if (!SystemInfo.getInstance().addDirectBufferMemoryCost(memCost)) {
         throw new MetadataException(
@@ -202,6 +206,7 @@ public class SchemaRegionPBTreeImpl implements 
ISchemaRegion {
                 + ", which is greater than limit mem cost: "
                 + 
SystemInfo.getInstance().getTotalDirectBufferMemorySizeLimit());
       }
+      ratisLogAppenderBufferReserved = true;
     }
 
     initDir();
@@ -481,9 +486,10 @@ public class SchemaRegionPBTreeImpl implements 
ISchemaRegion {
     // delete all the schema region files
     SchemaRegionUtils.deleteSchemaRegionFolder(schemaRegionDirPath, logger);
 
-    if 
(config.getSchemaRegionConsensusProtocolClass().equals(ConsensusFactory.RATIS_CONSENSUS))
 {
+    if (ratisLogAppenderBufferReserved) {
       SystemInfo.getInstance()
           
.decreaseDirectBufferMemoryCost(config.getSchemaRatisConsensusLogAppenderBufferSizeMax());
+      ratisLogAppenderBufferReserved = false;
     }
   }
 
@@ -496,18 +502,20 @@ public class SchemaRegionPBTreeImpl implements 
ISchemaRegion {
       return false;
     }
     logger.info("Start create snapshot of schemaRegion {}", schemaRegionId);
-    boolean isSuccess = true;
+    boolean isSuccess;
+    boolean currentResult;
     long startTime = System.currentTimeMillis();
 
     long mtreeSnapshotStartTime = System.currentTimeMillis();
-    isSuccess = isSuccess && mtree.createSnapshot(snapshotDir);
+    isSuccess = mtree.createSnapshot(snapshotDir);
     logger.info(
         "MTree snapshot creation of schemaRegion {} costs {}ms.",
         schemaRegionId,
         System.currentTimeMillis() - mtreeSnapshotStartTime);
 
     long tagSnapshotStartTime = System.currentTimeMillis();
-    isSuccess = isSuccess && tagManager.createSnapshot(snapshotDir);
+    currentResult = tagManager.createSnapshot(snapshotDir);
+    isSuccess = isSuccess && currentResult;
     logger.info(
         "Tag snapshot creation of schemaRegion {} costs {}ms.",
         schemaRegionId,
@@ -517,7 +525,9 @@ public class SchemaRegionPBTreeImpl implements 
ISchemaRegion {
         "Snapshot creation of schemaRegion {} costs {}ms.",
         schemaRegionId,
         System.currentTimeMillis() - startTime);
-    logger.info("Successfully create snapshot of schemaRegion {}", 
schemaRegionId);
+    if (isSuccess) {
+      logger.info("Successfully create snapshot of schemaRegion {}", 
schemaRegionId);
+    }
 
     return isSuccess;
   }
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionManagementTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionManagementTest.java
index 2d79a6a4766..d638e66b344 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionManagementTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionManagementTest.java
@@ -26,12 +26,14 @@ import org.apache.iotdb.commons.schema.SchemaConstant;
 import org.apache.iotdb.consensus.ConsensusFactory;
 import org.apache.iotdb.db.conf.IoTDBConfig;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.schemaengine.SchemaEngine;
 import org.apache.iotdb.db.schemaengine.schemaregion.ISchemaRegion;
 import org.apache.iotdb.db.schemaengine.schemaregion.SchemaRegionPlanType;
 import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.info.ISchemaInfo;
 import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.info.ITimeSeriesSchemaInfo;
 import 
org.apache.iotdb.db.schemaengine.schemaregion.write.req.SchemaRegionWritePlanFactory;
 import org.apache.iotdb.db.schemaengine.template.Template;
+import org.apache.iotdb.db.storageengine.rescon.memory.SystemInfo;
 
 import org.apache.tsfile.enums.TSDataType;
 import org.apache.tsfile.file.metadata.enums.CompressionType;
@@ -192,6 +194,44 @@ public class SchemaRegionManagementTest extends 
AbstractSchemaRegionTest {
     }
   }
 
+  @Test
+  public void testLoadSnapshotDoesNotReserveDirectBufferMemoryRepeatedly() 
throws Exception {
+    String schemaRegionConsensusProtocolClass = 
config.getSchemaRegionConsensusProtocolClass();
+    
config.setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS);
+    try {
+      long directBufferMemoryCostBeforeRegion =
+          SystemInfo.getInstance().getDirectBufferMemoryCost();
+      ISchemaRegion schemaRegion = getSchemaRegion("root.sg", 0);
+      long expectedDirectBufferMemoryCost =
+          directBufferMemoryCostBeforeRegion
+              + config.getSchemaRatisConsensusLogAppenderBufferSizeMax();
+      Assert.assertEquals(
+          expectedDirectBufferMemoryCost, 
SystemInfo.getInstance().getDirectBufferMemoryCost());
+
+      try {
+        // A failed snapshot load re-initializes the region. Repeated 
initialization must not
+        // reserve
+        // the Ratis log appender buffer more than once.
+        File missingSnapshotDir =
+            new File(config.getSchemaDir() + File.separator + 
"non-existent-snapshot");
+        Assert.assertFalse(missingSnapshotDir.exists());
+
+        for (int i = 0; i < 2; i++) {
+          schemaRegion.loadSnapshot(missingSnapshotDir);
+          Assert.assertEquals(
+              expectedDirectBufferMemoryCost, 
SystemInfo.getInstance().getDirectBufferMemoryCost());
+        }
+      } finally {
+        
SchemaEngine.getInstance().deleteSchemaRegion(schemaRegion.getSchemaRegionId());
+        Assert.assertEquals(
+            directBufferMemoryCostBeforeRegion,
+            SystemInfo.getInstance().getDirectBufferMemoryCost());
+      }
+    } finally {
+      
config.setSchemaRegionConsensusProtocolClass(schemaRegionConsensusProtocolClass);
+    }
+  }
+
   @Test
   @Ignore
   public void testSnapshotPerformance() throws Exception {

Reply via email to