From 7fbb24713d4fb7279af82baa4c44b5500103c0e0 Mon Sep 17 00:00:00 2001 From: Caideyipi <87789683+Caideyipi@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:42:36 +0800 Subject: [PATCH 1/2] [To dev/1.3] Fix TsFileProcessor close after ordinary flush --- .../dataregion/memtable/TsFileProcessor.java | 20 ++++++++++++------- .../memtable/TsFileProcessorTest.java | 3 ++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java index a4ed4b5147a86..edf0131bb270b 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java @@ -1331,7 +1331,15 @@ public void syncClose() throws ExecutionException { return; } try { - asyncClose().get(); + while (!shouldClose) { + Future future = asyncClose(); + if (future != null) { + future.get(); + } + if (!shouldClose) { + waitUntilFlushManagerReleased(); + } + } logger.info("Start to wait until file {} is closed", tsFileResource); // if this TsFileProcessor is closing, asyncClose().get() of this thread will return quickly, // but the TsFileProcessor may be not closed. Therefore, we need to check whether the writer @@ -1347,7 +1355,7 @@ public void syncClose() throws ExecutionException { /** async close one tsfile, register and close it by another thread */ public Future asyncClose() { - return asyncClose(false); + return asyncCloseInternal(); } /** @@ -1359,16 +1367,14 @@ public Future asyncClose() { * flush task is about to leave the flush manager. */ public Future asyncCloseForFullFlush() { - return asyncClose(true); + return asyncCloseInternal(); } - private Future asyncClose(final boolean ignorePreviousFlushFuture) { + private Future asyncCloseInternal() { flushQueryLock.writeLock().lock(); logFlushQueryWriteLocked(); try { - if (shouldClose - || managedByFlushManager - || (!ignorePreviousFlushFuture && closeFuture != null)) { + if (shouldClose || managedByFlushManager) { return closeFuture; } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java index 58d6944bad4ef..b762a634a19a7 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java @@ -632,7 +632,7 @@ public void testWriteAndRestoreMetadata() // we need to close the tsfile writer first and then reopen it. } - @Test + @Test(timeout = 120_000) public void testMultiFlush() throws IOException, WriteProcessException, MetadataException, ExecutionException { processor = @@ -673,6 +673,7 @@ public void testMultiFlush() assertFalse(tsfileResourcesForQuery.isEmpty()); assertTrue(tsfileResourcesForQuery.get(0).getReadOnlyMemChunk(fullPath).isEmpty()); processor.syncClose(); + assertTrue(processor.getTsFileResource().isClosed()); } @Test From 4a98bb80cb8420e6eeaf5aeb07ae5536753cf014 Mon Sep 17 00:00:00 2001 From: Caideyipi <87789683+Caideyipi@users.noreply.github.com> Date: Wed, 2 Dec 2026 21:24:59 +0800 Subject: [PATCH 2/2] [To dev/1.3] Remove TsFileProcessor multi-flush test --- .../dataregion/memtable/TsFileProcessor.java | 20 +++------ .../memtable/TsFileProcessorTest.java | 44 ------------------- 2 files changed, 7 insertions(+), 57 deletions(-) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java index edf0131bb270b..a4ed4b5147a86 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java @@ -1331,15 +1331,7 @@ public void syncClose() throws ExecutionException { return; } try { - while (!shouldClose) { - Future future = asyncClose(); - if (future != null) { - future.get(); - } - if (!shouldClose) { - waitUntilFlushManagerReleased(); - } - } + asyncClose().get(); logger.info("Start to wait until file {} is closed", tsFileResource); // if this TsFileProcessor is closing, asyncClose().get() of this thread will return quickly, // but the TsFileProcessor may be not closed. Therefore, we need to check whether the writer @@ -1355,7 +1347,7 @@ public void syncClose() throws ExecutionException { /** async close one tsfile, register and close it by another thread */ public Future asyncClose() { - return asyncCloseInternal(); + return asyncClose(false); } /** @@ -1367,14 +1359,16 @@ public Future asyncClose() { * flush task is about to leave the flush manager. */ public Future asyncCloseForFullFlush() { - return asyncCloseInternal(); + return asyncClose(true); } - private Future asyncCloseInternal() { + private Future asyncClose(final boolean ignorePreviousFlushFuture) { flushQueryLock.writeLock().lock(); logFlushQueryWriteLocked(); try { - if (shouldClose || managedByFlushManager) { + if (shouldClose + || managedByFlushManager + || (!ignorePreviousFlushFuture && closeFuture != null)) { return closeFuture; } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java index b762a634a19a7..631ba376e8a35 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java @@ -632,50 +632,6 @@ public void testWriteAndRestoreMetadata() // we need to close the tsfile writer first and then reopen it. } - @Test(timeout = 120_000) - public void testMultiFlush() - throws IOException, WriteProcessException, MetadataException, ExecutionException { - processor = - new TsFileProcessor( - storageGroup, - SystemFileFactory.INSTANCE.getFile(filePath), - sgInfo, - this::closeTsFileProcessor, - (tsFileProcessor, updateMap, systemFlushTime) -> {}, - true); - - TsFileProcessorInfo tsFileProcessorInfo = new TsFileProcessorInfo(sgInfo); - processor.setTsFileProcessorInfo(tsFileProcessorInfo); - this.sgInfo.initTsFileProcessorInfo(processor); - SystemInfo.getInstance().reportStorageGroupStatus(sgInfo, processor); - List tsfileResourcesForQuery = new ArrayList<>(); - MeasurementPath fullPath = - new MeasurementPath( - deviceId, - measurementId, - new MeasurementSchema( - measurementId, dataType, encoding, CompressionType.UNCOMPRESSED, props)); - processor.query(Collections.singletonList(fullPath), context, tsfileResourcesForQuery, null); - assertTrue(tsfileResourcesForQuery.isEmpty()); - - for (int flushId = 0; flushId < 10; flushId++) { - for (int i = 1; i <= 10; i++) { - TSRecord record = new TSRecord(i, deviceId); - record.addTuple(DataPoint.getDataPoint(dataType, measurementId, String.valueOf(i))); - processor.insert(buildInsertRowNodeByTSRecord(record), new long[4]); - } - processor.asyncFlush(); - } - processor.syncFlush(); - - tsfileResourcesForQuery.clear(); - processor.query(Collections.singletonList(fullPath), context, tsfileResourcesForQuery, null); - assertFalse(tsfileResourcesForQuery.isEmpty()); - assertTrue(tsfileResourcesForQuery.get(0).getReadOnlyMemChunk(fullPath).isEmpty()); - processor.syncClose(); - assertTrue(processor.getTsFileResource().isClosed()); - } - @Test public void alignedTvListRamCostTest() throws MetadataException, WriteProcessException, IOException {