Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2229,89 +2227,6 @@
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<Void> 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);
Expand Down Expand Up @@ -2368,37 +2283,57 @@

@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);

Check warning on line 2289 in iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/DataRegionTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Distance between variable 'waitStarted' declaration and its first usage is 4, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value).

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaJA4c9dsu55uBUg9tKQ&open=AaJA4c9dsu55uBUg9tKQ&pullRequest=19529
CountDownLatch allowClose = new CountDownLatch(1);

Check warning on line 2290 in iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/DataRegionTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Distance between variable 'allowClose' declaration and its first usage is 4, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value).

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaJA4c9dsu55uBUg9tKR&open=AaJA4c9dsu55uBUg9tKR&pullRequest=19529
Field closingProcessorsField =
DataRegion.class.getDeclaredField("closingSequenceTsFileProcessor");
closingProcessorsField.setAccessible(true);
Set<TsFileProcessor> closingProcessors =
(Set<TsFileProcessor>) 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<TsFileProcessor> closingProcessors =
(Set<TsFileProcessor>) closingProcessorsField.get(dataRegion);
closingProcessors.add(processor);

CompletableFuture<Void> 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();
syncCloseTask.get(10, TimeUnit.SECONDS);
} finally {
allowClose.countDown();
closingProcessors.remove(processor);
synchronized (closeStorageGroupCondition) {
closeStorageGroupCondition.notifyAll();
}
if (syncCloseTask != null) {
syncCloseTask.get(10, TimeUnit.SECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -198,7 +199,7 @@
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)) {
Expand Down Expand Up @@ -273,7 +274,7 @@
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)) {
Expand Down Expand Up @@ -353,7 +354,7 @@
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)) {
Expand Down Expand Up @@ -439,7 +440,7 @@
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)) {
Expand Down Expand Up @@ -528,7 +529,7 @@
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)) {
Expand All @@ -549,7 +550,11 @@

@Test
public void testWriteAndRestoreMetadata()
throws IOException, WriteProcessException, MetadataException, ExecutionException {
throws IOException,
WriteProcessException,
MetadataException,
ExecutionException,
InterruptedException {

Check warning on line 557 in iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the declaration of thrown exception 'java.lang.InterruptedException', as it cannot be thrown from method's body.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaJA4c62su55uBUg9tKP&open=AaJA4c62su55uBUg9tKP&pullRequest=19529
logger.info("testWriteAndRestoreMetadata begin..");
processor =
new TsFileProcessor(
Expand Down Expand Up @@ -627,8 +632,8 @@
}
}
restorableTsFileIOWriter.close();
logger.info("syncClose..");
processor.syncClose();
closeProcessorByFullFlush();
assertTrue(processor.getTsFileResource().isClosed());
// we need to close the tsfile writer first and then reopen it.
}

Expand Down Expand Up @@ -1306,7 +1311,7 @@
}

// close synchronously
processor.syncClose();
closeProcessorByFullFlush();

assertTrue(processor.getTsFileResource().isClosed());
}
Expand All @@ -1326,6 +1331,23 @@
}
}

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(
Expand Down
Loading