diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/source/PipeRealtimeExtractTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/source/PipeRealtimeExtractTest.java index f5f17fc91c23..93139bd379fb 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/source/PipeRealtimeExtractTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/source/PipeRealtimeExtractTest.java @@ -122,6 +122,8 @@ public class PipeRealtimeExtractTest { @Before public void setUp() throws Exception { + PipeTsFileEpochProgressIndexKeeper.getInstance() + .clearProgressIndex(dataRegion1, TEST_PIPE_NAME); dataNodeId = IoTDBDescriptor.getInstance().getConfig().getDataNodeId(); IoTDBDescriptor.getInstance().getConfig().setDataNodeId(0); pipeTotalFloatingMemoryProportion = @@ -1255,8 +1257,8 @@ public void testTsFileSourceRegionLevelDowngradingSuppliesHeartbeatWhileTsFileIs PipeSourceConstant.SOURCE_REALTIME_REGION_LEVEL_DOWNGRADING_KEY, pipeTaskMeta)) { final SimpleProgressIndex firstProgressIndex = new SimpleProgressIndex(1, 1); final SimpleProgressIndex secondProgressIndex = new SimpleProgressIndex(1, 2); - final TsFileResource firstResource = createTsFileResource(dataRegion1, "123-1-0-0.tsfile"); - final TsFileResource secondResource = createTsFileResource(dataRegion1, "123-2-0-0.tsfile"); + final TsFileResource firstResource = createTreeTsFileResource("123-1-0-0.tsfile"); + final TsFileResource secondResource = createTreeTsFileResource("123-2-0-0.tsfile"); firstResource.updateProgressIndex(firstProgressIndex); secondResource.updateProgressIndex(secondProgressIndex); final PipeRealtimeEvent firstEvent = @@ -1558,10 +1560,7 @@ private void verifyProcessedTabletCompletion( extractor, pipeTaskMeta, createTreeTsFileResource("127-1-0-0.tsfile"), false); events[1] = extractTsFileForTest( - extractor, - pipeTaskMeta, - createTsFileResource(dataRegion1, "127-2-0-0.tsfile"), - false); + extractor, pipeTaskMeta, createTreeTsFileResource("127-2-0-0.tsfile"), false); final PipeTsFileInsertionEvent firstTsFile = (PipeTsFileInsertionEvent) extractor.supply(); final AtomicInteger completionCount = new AtomicInteger(); firstTsFile.addOnTransferredHook(completionCount::incrementAndGet); @@ -1799,6 +1798,7 @@ private TsFileResource createTreeTsFileResource(final String fileName) throws Ex "root.sg.d", Collections.singletonList(new MeasurementSchema("s0", TSDataType.INT64)), 1); + tablet.rowSize = 1; tablet.addTimestamp(0, 1); tablet.addValue("s0", 0, 1L); try (final TsFileWriter writer = new TsFileWriter(tsFile)) { diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/DataRegionTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/DataRegionTest.java index 3bdcbd96596a..90884c620831 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/DataRegionTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/DataRegionTest.java @@ -57,7 +57,6 @@ import org.apache.iotdb.db.storageengine.dataregion.compaction.selector.constant.InnerSequenceCompactionSelector; import org.apache.iotdb.db.storageengine.dataregion.compaction.selector.constant.InnerUnsequenceCompactionSelector; import org.apache.iotdb.db.storageengine.dataregion.compaction.utils.CompactionConfigRestorer; -import org.apache.iotdb.db.storageengine.dataregion.flush.FlushListener; import org.apache.iotdb.db.storageengine.dataregion.flush.FlushManager; import org.apache.iotdb.db.storageengine.dataregion.flush.TsFileFlushPolicy; import org.apache.iotdb.db.storageengine.dataregion.memtable.IMemTable; @@ -121,7 +120,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; @@ -2229,89 +2227,6 @@ public void testDeleteDataWithMetricUpdate() FileMetrics.getInstance().getModFileNum()); } - @Test - public void testFullFlushClosesProcessorAfterOrdinaryAsyncFlush() throws Exception { - final TSRecord record = new TSRecord(100, deviceId); - record.addTuple(DataPoint.getDataPoint(TSDataType.INT32, measurementId, String.valueOf(100))); - dataRegion.insert(buildInsertRowNodeByTSRecord(record)); - - final TsFileProcessor tsFileProcessor = - dataRegion.getWorkSequenceTsFileProcessors().iterator().next(); - final TsFileResource tsFileResource = tsFileProcessor.getTsFileResource(); - tsFileProcessor.asyncFlush(); - final Future ordinaryFlushFuture = tsFileProcessor.getCloseFuture(); - Assert.assertNotNull(ordinaryFlushFuture); - ordinaryFlushFuture.get(); - - Assert.assertFalse(tsFileProcessor.alreadyMarkedClosing()); - Assert.assertFalse(tsFileResource.isClosed()); - - dataRegion.syncCloseAllWorkingAndClosingTsFileProcessors(); - - Assert.assertTrue(tsFileResource.isClosed()); - Assert.assertFalse(dataRegion.getWorkSequenceTsFileProcessors().contains(tsFileProcessor)); - } - - @Test - public void testFullFlushWaitsForRunningOrdinaryAsyncFlush() throws Exception { - final CountDownLatch ordinaryFlushBlocked = new CountDownLatch(1); - final CountDownLatch allowOrdinaryFlush = new CountDownLatch(1); - dataRegion.setCustomFlushListeners( - Collections.singletonList( - new FlushListener() { - @Override - public void onMemTableFlushStarted(final IMemTable memTable) { - // Do nothing. - } - - @Override - public void onMemTableFlushed(final IMemTable memTable) { - ordinaryFlushBlocked.countDown(); - try { - allowOrdinaryFlush.await(); - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); - throw new AssertionError(e); - } - } - })); - - final TSRecord record = new TSRecord(100, deviceId); - record.addTuple(DataPoint.getDataPoint(TSDataType.INT32, measurementId, String.valueOf(100))); - dataRegion.insert(buildInsertRowNodeByTSRecord(record)); - final TsFileProcessor tsFileProcessor = - dataRegion.getWorkSequenceTsFileProcessors().iterator().next(); - final TsFileResource tsFileResource = tsFileProcessor.getTsFileResource(); - - tsFileProcessor.asyncFlush(); - Assert.assertTrue(ordinaryFlushBlocked.await(30, TimeUnit.SECONDS)); - final CompletableFuture fullFlushFuture = - CompletableFuture.runAsync( - () -> { - try { - dataRegion.syncCloseAllWorkingAndClosingTsFileProcessors(); - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); - throw new CompletionException(e); - } catch (final ExecutionException e) { - throw new CompletionException(e); - } - }); - - try { - fullFlushFuture.get(200, TimeUnit.MILLISECONDS); - Assert.fail(); - } catch (final TimeoutException expected) { - // The full flush must wait until the ordinary flush releases the processor. - } finally { - allowOrdinaryFlush.countDown(); - } - - fullFlushFuture.get(30, TimeUnit.SECONDS); - Assert.assertTrue(tsFileResource.isClosed()); - Assert.assertFalse(dataRegion.getWorkSequenceTsFileProcessors().contains(tsFileProcessor)); - } - @Test public void testFullFlushDoesNotChaseProcessorCreatedAfterSnapshot() throws Exception { final CountDownLatch closeListenerBlocked = new CountDownLatch(1); @@ -2368,30 +2283,47 @@ public void testFullFlushDoesNotChaseProcessorCreatedAfterSnapshot() throws Exce @Test @SuppressWarnings("unchecked") - public void testSyncCloseWaitsForAlreadyClosingProcessor() throws Exception { + public void testFullFlushWaitsForAlreadyClosingProcessor() throws Exception { TsFileProcessor processor = Mockito.mock(TsFileProcessor.class); Future closeFuture = Mockito.mock(Future.class); CountDownLatch waitStarted = new CountDownLatch(1); CountDownLatch allowClose = new CountDownLatch(1); + Field closingProcessorsField = + DataRegion.class.getDeclaredField("closingSequenceTsFileProcessor"); + closingProcessorsField.setAccessible(true); + Set closingProcessors = + (Set) closingProcessorsField.get(dataRegion); + Field closeStorageGroupConditionField = + DataRegion.class.getDeclaredField("closeStorageGroupCondition"); + closeStorageGroupConditionField.setAccessible(true); + Object closeStorageGroupCondition = closeStorageGroupConditionField.get(dataRegion); + Mockito.doReturn(closeFuture).when(processor).getCloseFuture(); Mockito.when(closeFuture.get()) .thenAnswer( invocation -> { waitStarted.countDown(); Assert.assertTrue(allowClose.await(10, TimeUnit.SECONDS)); + closingProcessors.remove(processor); + synchronized (closeStorageGroupCondition) { + closeStorageGroupCondition.notifyAll(); + } return null; }); - Field closingProcessorsField = - DataRegion.class.getDeclaredField("closingSequenceTsFileProcessor"); - closingProcessorsField.setAccessible(true); - Set closingProcessors = - (Set) closingProcessorsField.get(dataRegion); closingProcessors.add(processor); CompletableFuture syncCloseTask = null; try { - syncCloseTask = CompletableFuture.runAsync(dataRegion::syncCloseAllWorkingTsFileProcessors); + syncCloseTask = + CompletableFuture.runAsync( + () -> { + try { + dataRegion.syncCloseAllWorkingAndClosingTsFileProcessors(); + } catch (final InterruptedException | ExecutionException e) { + throw new CompletionException(e); + } + }); Assert.assertTrue(waitStarted.await(10, TimeUnit.SECONDS)); Assert.assertFalse(syncCloseTask.isDone()); allowClose.countDown(); @@ -2399,6 +2331,9 @@ public void testSyncCloseWaitsForAlreadyClosingProcessor() throws Exception { } finally { allowClose.countDown(); closingProcessors.remove(processor); + synchronized (closeStorageGroupCondition) { + closeStorageGroupCondition.notifyAll(); + } if (syncCloseTask != null) { syncCloseTask.get(10, TimeUnit.SECONDS); } 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 631ba376e8a3..42dd6922ec91 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 @@ -82,6 +82,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import static junit.framework.TestCase.assertTrue; import static org.apache.iotdb.db.storageengine.dataregion.DataRegionTest.buildInsertRowNodeByTSRecord; @@ -198,7 +199,7 @@ public void testWriteAndFlush() processor.query(Collections.singletonList(fullPath), context, tsfileResourcesForQuery, null); assertTrue(tsfileResourcesForQuery.get(0).getReadOnlyMemChunk(fullPath).isEmpty()); assertEquals(1, tsfileResourcesForQuery.get(0).getChunkMetadataList(fullPath).size()); - processor.syncClose(); + closeProcessorByFullFlush(); try (TsFileSequenceReader reader = new TsFileSequenceReader(filePath); TsFileReader readTsFile = new TsFileReader(reader)) { @@ -273,7 +274,7 @@ public void testFlushMultiChunks() processor.query(Collections.singletonList(fullPath), context, tsfileResourcesForQuery, null); assertTrue(tsfileResourcesForQuery.get(0).getReadOnlyMemChunk(fullPath).isEmpty()); assertEquals(3, tsfileResourcesForQuery.get(0).getChunkMetadataList(fullPath).size()); - processor.syncClose(); + closeProcessorByFullFlush(); try (TsFileSequenceReader reader = new TsFileSequenceReader(filePath); TsFileReader readTsFile = new TsFileReader(reader)) { @@ -353,7 +354,7 @@ public void testFlushMultiBinaryChunks() processor.query(Collections.singletonList(fullPath), context, tsfileResourcesForQuery, null); assertTrue(tsfileResourcesForQuery.get(0).getReadOnlyMemChunk(fullPath).isEmpty()); assertEquals(3, tsfileResourcesForQuery.get(0).getChunkMetadataList(fullPath).size()); - processor.syncClose(); + closeProcessorByFullFlush(); try (TsFileSequenceReader reader = new TsFileSequenceReader(filePath); TsFileReader readTsFile = new TsFileReader(reader)) { @@ -439,7 +440,7 @@ public void testFlushMultiAlignedChunks() processor.query(Collections.singletonList(fullPath), context, tsfileResourcesForQuery, null); assertTrue(tsfileResourcesForQuery.get(0).getReadOnlyMemChunk(fullPath).isEmpty()); assertEquals(3, tsfileResourcesForQuery.get(0).getChunkMetadataList(fullPath).size()); - processor.syncClose(); + closeProcessorByFullFlush(); try (TsFileSequenceReader reader = new TsFileSequenceReader(filePath); TsFileReader readTsFile = new TsFileReader(reader)) { @@ -528,7 +529,7 @@ public void testFlushMultiAlignedBinaryChunks() processor.query(Collections.singletonList(fullPath), context, tsfileResourcesForQuery, null); assertTrue(tsfileResourcesForQuery.get(0).getReadOnlyMemChunk(fullPath).isEmpty()); assertEquals(3, tsfileResourcesForQuery.get(0).getChunkMetadataList(fullPath).size()); - processor.syncClose(); + closeProcessorByFullFlush(); try (TsFileSequenceReader reader = new TsFileSequenceReader(filePath); TsFileReader readTsFile = new TsFileReader(reader)) { @@ -549,7 +550,11 @@ public void testFlushMultiAlignedBinaryChunks() @Test public void testWriteAndRestoreMetadata() - throws IOException, WriteProcessException, MetadataException, ExecutionException { + throws IOException, + WriteProcessException, + MetadataException, + ExecutionException, + InterruptedException { logger.info("testWriteAndRestoreMetadata begin.."); processor = new TsFileProcessor( @@ -627,8 +632,8 @@ public void testWriteAndRestoreMetadata() } } restorableTsFileIOWriter.close(); - logger.info("syncClose.."); - processor.syncClose(); + closeProcessorByFullFlush(); + assertTrue(processor.getTsFileResource().isClosed()); // we need to close the tsfile writer first and then reopen it. } @@ -1306,7 +1311,7 @@ public void testWriteAndClose() } // close synchronously - processor.syncClose(); + closeProcessorByFullFlush(); assertTrue(processor.getTsFileResource().isClosed()); } @@ -1326,6 +1331,23 @@ private void closeTsFileProcessor(TsFileProcessor unsealedTsFileProcessor) } } + private void closeProcessorByFullFlush() throws ExecutionException { + try { + while (!processor.alreadyMarkedClosing()) { + final Future closeFuture = processor.asyncCloseForFullFlush(); + if (closeFuture != null) { + closeFuture.get(); + } + if (!processor.alreadyMarkedClosing()) { + processor.waitUntilFlushManagerReleased(); + } + } + } catch (final InterruptedException e) { + Thread.currentThread().interrupt(); + throw new AssertionError(e); + } + } + private TsFileProcessor newTestProcessor(String path) throws IOException, WriteProcessException { TsFileProcessor newProcessor = new TsFileProcessor(