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

jt2594838 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 4b94bc62e29 [Test] Fix flaky LOAD restart and teardown ITs (#19103)
4b94bc62e29 is described below

commit 4b94bc62e29141058b51497fdcc3c60d6a8652aa
Author: Caideyipi <[email protected]>
AuthorDate: Tue Nov 10 17:57:24 2026 +0800

    [Test] Fix flaky LOAD restart and teardown ITs (#19103)
    
    * [Test] Skip unsealed TsFiles in LOAD restart snapshots
    
    * Fix LOAD cluster IT cleanup race
    
    * Strengthen LOAD cleanup race regression coverage
    
    * IT: verify database reuse after teardown cleanup
---
 .../iotdb/db/it/IoTDBLoadTsFileClusterIT.java      | 98 +++++++++++++++++++++-
 1 file changed, 97 insertions(+), 1 deletion(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBLoadTsFileClusterIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBLoadTsFileClusterIT.java
index dd1c1eef59d..73d3e9ccaed 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBLoadTsFileClusterIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBLoadTsFileClusterIT.java
@@ -38,6 +38,7 @@ import org.apache.iotdb.rpc.TSStatusCode;
 
 import org.apache.tsfile.common.conf.TSFileConfig;
 import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.exception.NotCompatibleTsFileException;
 import org.apache.tsfile.file.metadata.IDeviceID;
 import org.apache.tsfile.file.metadata.enums.CompressionType;
 import org.apache.tsfile.file.metadata.enums.TSEncoding;
@@ -58,9 +59,11 @@ import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
+import org.junit.runners.MethodSorters;
 
 import java.io.File;
 import java.io.IOException;
@@ -90,6 +93,8 @@ import java.util.concurrent.TimeUnit;
 import java.util.stream.Stream;
 
 @RunWith(IoTDBTestRunner.class)
+// Keep the cleanup and immediate-reuse cases consecutive at the end of this 
shared-cluster suite.
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
 @Category({ClusterIT.class})
 public class IoTDBLoadTsFileClusterIT {
 
@@ -186,6 +191,7 @@ public class IoTDBLoadTsFileClusterIT {
       statement.execute("delete database " + DATABASE);
     } catch (final Exception ignored) {
     }
+    awaitDatabaseDataRegionsDeleted();
 
     final File[] files = tmpDir.listFiles();
     if (files != null) {
@@ -326,6 +332,28 @@ public class IoTDBLoadTsFileClusterIT {
     }
   }
 
+  @Test
+  public void testZDatabaseDataRegionsDeletedByTeardown() throws Exception {
+    final long expectedPointCount = generateTsFile(tmpDir, 
"delete-database.tsfile", 10, 1L);
+    try (final Connection connection = EnvFactory.getEnv().getConnection();
+        final Statement statement = connection.createStatement()) {
+      createDatabaseAndSchema(statement);
+      statement.execute("load \"" + tmpDir.getAbsolutePath() + "\"");
+      awaitPointCount(statement, expectedPointCount);
+    }
+  }
+
+  @Test
+  public void testZZDatabaseNameCanBeReusedAfterTeardown() throws Exception {
+    final long expectedPointCount = generateTsFile(tmpDir, 
"reuse-database.tsfile", 10, 1L);
+    try (final Connection connection = EnvFactory.getEnv().getConnection();
+        final Statement statement = connection.createStatement()) {
+      createDatabaseAndSchema(statement);
+      statement.execute("load \"" + tmpDir.getAbsolutePath() + "\"");
+      awaitPointCount(statement, expectedPointCount);
+    }
+  }
+
   /**
    * A LOAD that runs while one DataNode is down is replicated by consensus to 
the surviving
    * replicas. When the stopped DataNode restarts, its replicas replay the 
LOAD commands they
@@ -479,7 +507,42 @@ public class IoTDBLoadTsFileClusterIT {
     return points.size();
   }
 
-  /** Hard-links the TsFiles below a replica directory into the snapshot 
directory. */
+  @Test
+  public void testReplicaSnapshotsSkipUnsealedTsFiles() throws Exception {
+    final File regionDir = new File(tmpDir, "region");
+    final File partitionDir = new File(regionDir, "0");
+    Assert.assertTrue(partitionDir.mkdirs());
+    final File sealed = new File(partitionDir, "1-1-0-0.tsfile");
+    final long expectedPointCount = generateTsFile(partitionDir, 
sealed.getName(), 10, 1L);
+    new TsFileResource(sealed).serialize();
+
+    // An active writer may have no bytes on disk yet, or just an incomplete 
header.
+    final File empty = new File(partitionDir, "2-1-0-0.tsfile");
+    Assert.assertTrue(empty.createNewFile());
+    final File unsealed = new File(partitionDir, "3-1-0-0.tsfile");
+    Files.write(unsealed.toPath(), 
TSFileConfig.MAGIC_STRING.getBytes(TSFileConfig.STRING_CHARSET));
+
+    final List<File> snapshots = snapshotTsFiles(regionDir, new File(tmpDir, 
"snapshots"));
+    Assert.assertEquals(1, snapshots.size());
+    final Set<String> points = new HashSet<>();
+    readPoints(snapshots.get(0), points);
+    Assert.assertEquals(expectedPointCount, points.size());
+
+    // A damaged file advertised as sealed must still be read and rejected, 
rather than skipped.
+    new TsFileResource(empty).serialize();
+    final List<File> damagedSnapshots =
+        snapshotTsFiles(regionDir, new File(tmpDir, "damaged-snapshots"));
+    Assert.assertEquals(2, damagedSnapshots.size());
+    Assert.assertThrows(
+        NotCompatibleTsFileException.class,
+        () -> {
+          for (final File snapshot : damagedSnapshots) {
+            readPoints(snapshot, new HashSet<>());
+          }
+        });
+  }
+
+  /** Hard-links only sealed TsFiles below a replica directory into the 
snapshot directory. */
   private static List<File> snapshotTsFiles(final File regionDir, final File 
snapshotDir)
       throws IOException {
     final List<File> tsFiles = new ArrayList<>();
@@ -490,6 +553,11 @@ public class IoTDBLoadTsFileClusterIT {
     Files.createDirectories(snapshotDir.toPath());
     final List<File> snapshots = new ArrayList<>();
     for (final File tsFile : tsFiles) {
+      // The resource sidecar is published after the writer finishes the file. 
A hard link to an
+      // unsealed file still shares its writes and may not even contain a 
readable header yet.
+      if (!new File(tsFile + TsFileResource.RESOURCE_SUFFIX).exists()) {
+        continue;
+      }
       // The time partition directory keeps the names unique inside one 
replica.
       final File snapshot =
           new File(snapshotDir, tsFile.getParentFile().getName() + "-" + 
tsFile.getName());
@@ -645,6 +713,34 @@ public class IoTDBLoadTsFileClusterIT {
     }
   }
 
+  /** Waits for asynchronous region deletion before the next test reuses this 
database name. */
+  private static void awaitDatabaseDataRegionsDeleted() {
+    Awaitility.await()
+        .atMost(5, TimeUnit.MINUTES)
+        .pollInterval(1, TimeUnit.SECONDS)
+        .ignoreExceptions()
+        .until(IoTDBLoadTsFileClusterIT::areDatabaseDataRegionsDeleted);
+  }
+
+  private static boolean areDatabaseDataRegionsDeleted() throws IOException {
+    for (int dataNodeIndex = 0; dataNodeIndex < DATA_NODE_NUM; 
dataNodeIndex++) {
+      final DataNodeWrapper dataNode = 
EnvFactory.getEnv().getDataNodeWrapper(dataNodeIndex);
+      for (final String order : TSFILE_DIRECTORIES) {
+        final java.nio.file.Path databaseDir =
+            Paths.get(dataNode.getDataNodeDir(), "data", order, DATABASE);
+        if (!Files.isDirectory(databaseDir)) {
+          continue;
+        }
+        try (Stream<java.nio.file.Path> regionDirs = Files.list(databaseDir)) {
+          if (regionDirs.anyMatch(Files::isDirectory)) {
+            return false;
+          }
+        }
+      }
+    }
+    return true;
+  }
+
   /** The cluster id of the given DataNode, matched by its client RPC 
endpoint. */
   private static int dataNodeId(final Statement statement, final 
DataNodeWrapper dataNode)
       throws SQLException {

Reply via email to