Skip to content

Commit 5690cec

Browse files
authored
[Test] Use consistent time units for asynchronous assertion flushes (#18905)
* [Test] Use consistent time units for asynchronous assertion flushes * [Fix] Preserve table schemas in consensus LOAD TsFiles Register table schemas before sealing consensus LOAD files, including recovered tasks, so Pipe can enumerate their table data. Cover normal loads, recovery, and null tags with regression tests.
1 parent ad1bd47 commit 5690cec

5 files changed

Lines changed: 243 additions & 11 deletions

File tree

‎integration-test/src/test/java/org/apache/iotdb/db/it/utils/IoTDBTestUtilsIT.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.function.Consumer;
3838

3939
import static org.junit.Assert.assertEquals;
40+
import static org.junit.Assert.assertFalse;
4041
import static org.junit.Assert.assertThrows;
4142
import static org.junit.Assert.assertTrue;
4243
import static org.junit.Assert.fail;
@@ -66,6 +67,12 @@ public void testAssertAlwaysFail() {
6667
() -> TestUtils.assertAlwaysFail(EnvFactory.getEnv(), "show databases", 1));
6768
}
6869

70+
@Test
71+
public void testHalfTimeoutElapsedUsesSeconds() {
72+
assertFalse(TestUtils.isHalfTimeoutElapsed(299_999, 600));
73+
assertTrue(TestUtils.isHalfTimeoutElapsed(300_001, 600));
74+
}
75+
6976
@Test
7077
public void testAssertDataAlwaysFailsOnMismatch() {
7178
final BaseEnv env = EnvFactory.getEnv();

‎integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public class TestUtils {
8585

8686
public static final String TIME_PRECISION_IN_NS = "ns";
8787

88+
static boolean isHalfTimeoutElapsed(final long elapsedTimeInMillis, final long timeoutSeconds) {
89+
return elapsedTimeInMillis > TimeUnit.SECONDS.toMillis(timeoutSeconds) / 2;
90+
}
91+
8892
/**
8993
* Creates a statement on the write node without comparing results across DataNodes.
9094
*
@@ -1702,7 +1706,9 @@ public static void assertDataEventuallyOnEnv(
17021706
// For IoTV2 batch mode, the pipe receiver may need to flush because the replica
17031707
// sync requires tsFile to process. We flush in the middle of assertion because we
17041708
// don't know when the data reaches the receiver in general cases
1705-
if (!flushed[0] && System.currentTimeMillis() - startTime > timeoutSeconds >> 1) {
1709+
if (!flushed[0]
1710+
&& isHalfTimeoutElapsed(
1711+
System.currentTimeMillis() - startTime, timeoutSeconds)) {
17061712
flushed[0] = true;
17071713
statement.execute("flush");
17081714
}
@@ -1783,7 +1789,9 @@ public static void assertDataSizeEventuallyOnEnv(
17831789
// For IoTV2 batch mode, the pipe receiver may need to flush because the replica
17841790
// sync requires tsFile to process. We flush in the middle of assertion because we
17851791
// don't know when the data reaches the receiver in general cases
1786-
if (!flushed[0] && System.currentTimeMillis() - startTime > timeoutSeconds >> 1) {
1792+
if (!flushed[0]
1793+
&& isHalfTimeoutElapsed(
1794+
System.currentTimeMillis() - startTime, timeoutSeconds)) {
17871795
flushed[0] = true;
17881796
statement.execute("flush");
17891797
}
@@ -1822,7 +1830,9 @@ public static void assertDataEventuallyOnEnv(
18221830
// For IoTV2 batch mode, the pipe receiver may need to flush because the replica
18231831
// sync requires tsFile to process. We flush in the middle of assertion because we
18241832
// don't know when the data reaches the receiver in general cases
1825-
if (!flushed[0] && System.currentTimeMillis() - startTime > timeoutSeconds >> 1) {
1833+
if (!flushed[0]
1834+
&& isHalfTimeoutElapsed(
1835+
System.currentTimeMillis() - startTime, timeoutSeconds)) {
18261836
flushed[0] = true;
18271837
statement.execute("flush");
18281838
}
@@ -1904,7 +1914,9 @@ public static void assertDataEventuallyOnEnv(
19041914
// For IoTV2 batch mode, the pipe receiver may need to flush because the replica
19051915
// sync requires tsFile to process. We flush in the middle of assertion because we
19061916
// don't know when the data reaches the receiver in general cases
1907-
if (!flushed[0] && System.currentTimeMillis() - startTime > timeoutSeconds >> 1) {
1917+
if (!flushed[0]
1918+
&& isHalfTimeoutElapsed(
1919+
System.currentTimeMillis() - startTime, timeoutSeconds)) {
19081920
flushed[0] = true;
19091921
statement.execute("flush");
19101922
}
@@ -2024,7 +2036,9 @@ public static void assertDataEventuallyOnEnv(
20242036
// For IoTV2 batch mode, the pipe receiver may need to flush because the replica
20252037
// sync requires tsFile to process. We flush in the middle of assertion because we
20262038
// don't know when the data reaches the receiver in general cases
2027-
if (!flushed[0] && System.currentTimeMillis() - startTime > timeoutSeconds >> 1) {
2039+
if (!flushed[0]
2040+
&& isHalfTimeoutElapsed(
2041+
System.currentTimeMillis() - startTime, timeoutSeconds)) {
20282042
flushed[0] = true;
20292043
statement.execute("flush");
20302044
}
@@ -2095,8 +2109,8 @@ public static void assertDataAlwaysOnEnv(
20952109
// sync requires tsFile to process. We flush in the middle of assertion because we
20962110
// don't know when the data reaches the receiver in general cases
20972111
if (!flushed[0]
2098-
&& System.currentTimeMillis() - startTime
2099-
> TimeUnit.SECONDS.toMillis(consistentSeconds) / 2) {
2112+
&& isHalfTimeoutElapsed(
2113+
System.currentTimeMillis() - startTime, consistentSeconds)) {
21002114
flushed[0] = true;
21012115
statement.execute("flush");
21022116
}
@@ -2137,8 +2151,8 @@ public static void assertDataAlwaysOnEnv(
21372151
// sync requires tsFile to process. We flush in the middle of assertion because we
21382152
// don't know when the data reaches the receiver in general cases
21392153
if (!flushed[0]
2140-
&& System.currentTimeMillis() - startTime
2141-
> TimeUnit.SECONDS.toMillis(consistentSeconds) / 2) {
2154+
&& isHalfTimeoutElapsed(
2155+
System.currentTimeMillis() - startTime, consistentSeconds)) {
21422156
flushed[0] = true;
21432157
statement.execute("flush");
21442158
}
@@ -2192,8 +2206,8 @@ public static void assertDataAlwaysOnEnv(
21922206
// sync requires tsFile to process. We flush in the middle of assertion because we
21932207
// don't know when the data reaches the receiver in general cases
21942208
if (!flushed[0]
2195-
&& System.currentTimeMillis() - startTime
2196-
> TimeUnit.SECONDS.toMillis(consistentSeconds) / 2) {
2209+
&& isHalfTimeoutElapsed(
2210+
System.currentTimeMillis() - startTime, consistentSeconds)) {
21972211
flushed[0] = true;
21982212
statement.execute("flush");
21992213
}

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/TsFileWriterManager.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
import org.apache.iotdb.commons.consensus.index.ProgressIndex;
2525
import org.apache.iotdb.commons.consensus.index.impl.MinimumProgressIndex;
2626
import org.apache.iotdb.commons.file.SystemFileFactory;
27+
import org.apache.iotdb.commons.schema.table.TsFileTableSchemaUtil;
28+
import org.apache.iotdb.commons.schema.table.TsTable;
29+
import org.apache.iotdb.commons.utils.PathUtils;
2730
import org.apache.iotdb.commons.utils.RetryUtils;
2831
import org.apache.iotdb.db.conf.IoTDBDescriptor;
2932
import org.apache.iotdb.db.exception.load.LoadFileException;
3033
import org.apache.iotdb.db.i18n.StorageEngineMessages;
3134
import org.apache.iotdb.db.queryengine.plan.planner.plan.node.load.LoadTsFileConsensusNode;
3235
import org.apache.iotdb.db.queryengine.plan.planner.plan.node.load.LoadTsFileConsensusOp;
36+
import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
3337
import org.apache.iotdb.db.storageengine.dataregion.DataRegion;
3438
import org.apache.iotdb.db.storageengine.dataregion.modification.ModificationFile;
3539
import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
@@ -502,6 +506,18 @@ void prepare(
502506
fileLength));
503507
}
504508
}
509+
// Rebuild from the staged devices so recovered writers also retain the table schemas that
510+
// Pipe needs to enumerate table data in the sealed file.
511+
if (PathUtils.isTableModelDatabase(dataRegion.getDatabaseName())) {
512+
for (final String tableName : writer.getTableNames()) {
513+
final TsTable table =
514+
DataNodeTableCache.getInstance()
515+
.getTable(dataRegion.getDatabaseName(), tableName, false);
516+
if (table != null) {
517+
writer.registerTableSchema(TsFileTableSchemaUtil.toTsFileTableSchemaNoAttribute(table));
518+
}
519+
}
520+
}
505521
writer.close();
506522

507523
final TsFileResource tsFileResource = timePartition2Resource.get(entry.getKey());

‎iotdb-core/datanode/src/main/java/org/apache/tsfile/write/TsFilePrecalculatedChunkWriter.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.tsfile.file.metadata.IDeviceID;
3232
import org.apache.tsfile.file.metadata.MeasurementMetadataIndexEntry;
3333
import org.apache.tsfile.file.metadata.MetadataIndexNode;
34+
import org.apache.tsfile.file.metadata.TableSchema;
3435
import org.apache.tsfile.file.metadata.TimeseriesMetadata;
3536
import org.apache.tsfile.file.metadata.TsFileMetadata;
3637
import org.apache.tsfile.file.metadata.enums.MetadataIndexNodeType;
@@ -53,6 +54,7 @@
5354
import java.util.List;
5455
import java.util.Map;
5556
import java.util.Objects;
57+
import java.util.Set;
5658
import java.util.TreeMap;
5759
import java.util.stream.Collectors;
5860

@@ -80,6 +82,7 @@ public class TsFilePrecalculatedChunkWriter implements AutoCloseable {
8082
private final File file;
8183
private final Map<IDeviceID, Map<String, List<IChunkMetadata>>> device2MetadataMap =
8284
new TreeMap<>();
85+
private final Map<String, TableSchema> tableSchemaMap = new TreeMap<>();
8386

8487
private long maxPhysicalFileSize;
8588
private boolean sealed;
@@ -385,6 +388,7 @@ public void close() throws IOException {
385388
}
386389

387390
tsFileMetadata.setTableMetadataIndexNodeMap(tableIndexNodeMap);
391+
tsFileMetadata.setTableSchemaMap(tableSchemaMap);
388392
tsFileMetadata.setMetaOffset(metaOffset);
389393
tsFileMetadata.setBloomFilter(bloomFilter);
390394

@@ -446,6 +450,16 @@ public Map<IDeviceID, List<IChunkMetadata>> getChunkMetadataListMap() {
446450
return result;
447451
}
448452

453+
public Set<String> getTableNames() {
454+
return device2MetadataMap.keySet().stream()
455+
.map(IDeviceID::getTableName)
456+
.collect(Collectors.toSet());
457+
}
458+
459+
public void registerTableSchema(final TableSchema tableSchema) {
460+
tableSchemaMap.put(tableSchema.getTableName(), tableSchema);
461+
}
462+
449463
private String getFileForLog() {
450464
return file == null ? "<memory-output>" : file.getAbsolutePath();
451465
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.db.storageengine.load;
21+
22+
import org.apache.iotdb.commons.pipe.datastructure.pattern.TablePattern;
23+
import org.apache.iotdb.commons.schema.table.TsFileTableSchemaUtil;
24+
import org.apache.iotdb.commons.schema.table.TsTable;
25+
import org.apache.iotdb.commons.schema.table.column.FieldColumnSchema;
26+
import org.apache.iotdb.commons.schema.table.column.TagColumnSchema;
27+
import org.apache.iotdb.db.conf.IoTDBDescriptor;
28+
import org.apache.iotdb.db.pipe.event.common.tablet.PipeRawTabletInsertionEvent;
29+
import org.apache.iotdb.db.pipe.event.common.tsfile.parser.table.TsFileInsertionEventTableParser;
30+
import org.apache.iotdb.db.queryengine.plan.scheduler.load.ChunkOffsetCalculator;
31+
import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
32+
import org.apache.iotdb.db.storageengine.dataregion.DataRegion;
33+
import org.apache.iotdb.db.storageengine.load.splitter.ChunkData;
34+
import org.apache.iotdb.db.storageengine.load.splitter.TsFileData;
35+
import org.apache.iotdb.db.storageengine.load.splitter.TsFileSplitter;
36+
import org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent;
37+
38+
import org.apache.tsfile.enums.ColumnCategory;
39+
import org.apache.tsfile.enums.TSDataType;
40+
import org.apache.tsfile.file.metadata.TableSchema;
41+
import org.apache.tsfile.file.metadata.enums.CompressionType;
42+
import org.apache.tsfile.file.metadata.enums.TSEncoding;
43+
import org.apache.tsfile.read.TsFileSequenceReader;
44+
import org.apache.tsfile.write.TsFileWriter;
45+
import org.apache.tsfile.write.record.Tablet;
46+
import org.junit.After;
47+
import org.junit.Before;
48+
import org.junit.Rule;
49+
import org.junit.Test;
50+
import org.junit.rules.TemporaryFolder;
51+
import org.mockito.Mockito;
52+
53+
import java.io.File;
54+
import java.util.ArrayList;
55+
import java.util.Arrays;
56+
import java.util.Collections;
57+
import java.util.List;
58+
import java.util.Optional;
59+
60+
import static org.junit.Assert.assertEquals;
61+
62+
public class LoadTsFileTableSchemaTest {
63+
64+
private static final String DATABASE = "load_table_schema_test";
65+
private static final String TABLE = "measurements";
66+
private static final int ROW_COUNT = 20;
67+
68+
@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
69+
70+
private String[] originalLoadDirs;
71+
private DataRegion dataRegion;
72+
private TsTable table;
73+
74+
@Before
75+
public void setUp() throws Exception {
76+
originalLoadDirs = IoTDBDescriptor.getInstance().getConfig().getLoadTsFileDirs();
77+
IoTDBDescriptor.getInstance()
78+
.getConfig()
79+
.setLoadTsFileDirs(new String[] {temporaryFolder.getRoot().getAbsolutePath()});
80+
dataRegion = Mockito.mock(DataRegion.class);
81+
Mockito.when(dataRegion.getDatabaseName()).thenReturn(DATABASE);
82+
Mockito.when(dataRegion.getDataRegionIdString()).thenReturn("0");
83+
Mockito.when(dataRegion.getNonSystemDatabaseName()).thenReturn(Optional.of(DATABASE));
84+
85+
table = new TsTable(TABLE);
86+
table.addColumnSchema(new TagColumnSchema("device", TSDataType.STRING));
87+
table.addColumnSchema(
88+
new FieldColumnSchema("value", TSDataType.INT32, TSEncoding.PLAIN, CompressionType.LZ4));
89+
DataNodeTableCache.getInstance().preUpdateTable(DATABASE, table, null);
90+
DataNodeTableCache.getInstance().commitUpdateTable(DATABASE, TABLE, null);
91+
}
92+
93+
@After
94+
public void tearDown() {
95+
DataNodeTableCache.getInstance().invalid(DATABASE);
96+
IoTDBDescriptor.getInstance().getConfig().setLoadTsFileDirs(originalLoadDirs);
97+
}
98+
99+
@Test
100+
public void testLoadedTableRemainsReadableByPipe() throws Exception {
101+
assertLoadedTableReadable(false, false);
102+
}
103+
104+
@Test
105+
public void testRecoveredTableRemainsReadableByPipe() throws Exception {
106+
assertLoadedTableReadable(true, false);
107+
}
108+
109+
@Test
110+
public void testLoadedTableWithNullTagsRemainsReadableByPipe() throws Exception {
111+
assertLoadedTableReadable(false, true);
112+
}
113+
114+
private void assertLoadedTableReadable(final boolean recover, final boolean nullTags)
115+
throws Exception {
116+
final File source = new File(temporaryFolder.getRoot(), "source.tsfile");
117+
final TableSchema schema = TsFileTableSchemaUtil.toTsFileTableSchemaNoAttribute(table);
118+
try (final TsFileWriter writer = new TsFileWriter(source)) {
119+
writer.registerTableSchema(schema);
120+
final Tablet tablet =
121+
new Tablet(
122+
TABLE,
123+
Arrays.asList("device", "value"),
124+
Arrays.asList(TSDataType.STRING, TSDataType.INT32),
125+
Arrays.asList(ColumnCategory.TAG, ColumnCategory.FIELD),
126+
ROW_COUNT);
127+
for (int i = 0; i < ROW_COUNT; i++) {
128+
tablet.addTimestamp(i, i);
129+
tablet.addValue("device", i, nullTags ? null : "d" + i % 2);
130+
tablet.addValue("value", i, i);
131+
}
132+
writer.writeTable(tablet);
133+
}
134+
135+
final File taskDir = temporaryFolder.newFolder("task");
136+
TsFileWriterManager manager = new TsFileWriterManager(dataRegion, taskDir);
137+
try {
138+
final ChunkOffsetCalculator calculator = new ChunkOffsetCalculator();
139+
final List<TsFileData> pieces = new ArrayList<>();
140+
new TsFileSplitter(
141+
source,
142+
data -> {
143+
calculator.assign((ChunkData) data);
144+
pieces.add(data);
145+
return true;
146+
})
147+
.splitTsFileByDataPartition();
148+
manager.writePiece(pieces);
149+
if (recover) {
150+
manager.close(true);
151+
manager = new TsFileWriterManager(dataRegion, taskDir).recoverFromDisk();
152+
}
153+
manager.prepare(false, Collections.emptyMap(), Collections.emptyMap());
154+
155+
final File staged = new File(taskDir, DATABASE + "-0-0.tsfile");
156+
int rowCount = 0;
157+
try (final TsFileInsertionEventTableParser parser =
158+
new TsFileInsertionEventTableParser(
159+
staged,
160+
new TablePattern(true, null, null),
161+
Long.MIN_VALUE,
162+
Long.MAX_VALUE,
163+
null,
164+
null,
165+
null,
166+
false)) {
167+
for (final TabletInsertionEvent event : parser.toTabletInsertionEvents()) {
168+
final PipeRawTabletInsertionEvent tabletEvent = (PipeRawTabletInsertionEvent) event;
169+
rowCount += tabletEvent.convertToTablet().getRowSize();
170+
tabletEvent.clearReferenceCount(getClass().getName());
171+
}
172+
}
173+
assertEquals(ROW_COUNT, rowCount);
174+
try (final TsFileSequenceReader reader = new TsFileSequenceReader(staged.getPath())) {
175+
assertEquals(schema, reader.getTableSchemaMap().get(TABLE));
176+
}
177+
} finally {
178+
manager.close();
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)