Skip to content

Commit 68e5278

Browse files
authored
Fix aligned null fill order in fast compaction (#19386)
1 parent f3ce849 commit 68e5278

3 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/utils/executor/fast/SeriesCompactionExecutor.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ private void compactWithNonOverlapPage(PageElement pageElement)
265265
// finish writing this page
266266
break;
267267
}
268+
point = pointPriorityReader.currentPointForOutput();
268269
compactionWriter.write(point, subTaskId);
269270
pointPriorityReader.next();
270271
}
@@ -306,7 +307,7 @@ private void compactWithOverlapPages()
306307
}
307308

308309
// write data point into chunk writer
309-
compactionWriter.write(currentPoint, subTaskId);
310+
compactionWriter.write(pointPriorityReader.currentPointForOutput(), subTaskId);
310311
pointPriorityReader.next();
311312
}
312313
}

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/utils/reader/PointPriorityReader.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,22 @@ public PointPriorityReader(SeriesCompactionExecutor.RemovePage removePage, boole
6363
this.isAligned = isAligned;
6464
}
6565

66+
/** Returns the current point without aligned null filling. */
6667
public TimeValuePair currentPoint() {
6768
if (currentPointElement == null) {
6869
// if the current point is overlapped with other pages, then get the highest priority point
6970
// from queue
7071
currentPoint = pointQueue.peek().timeValuePair;
7172
lastTime = currentPoint.getTimestamp();
73+
}
74+
return currentPoint;
75+
}
7276

73-
// fill aligned null value with the same timestamp
74-
if (isAligned) {
75-
fillAlignedNullValue();
76-
}
77+
/** Fill aligned null values when the current point is ready to be written. */
78+
public TimeValuePair currentPointForOutput() {
79+
currentPoint();
80+
if (isAligned && currentPointElement == null) {
81+
fillAlignedNullValue();
7782
}
7883
return currentPoint;
7984
}

‎iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/compaction/inner/FastCompactionPerformerAlignedTest.java‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@
4343
import org.apache.tsfile.file.metadata.IDeviceID;
4444
import org.apache.tsfile.read.TimeValuePair;
4545
import org.apache.tsfile.utils.Pair;
46+
import org.apache.tsfile.write.chunk.AlignedChunkWriterImpl;
4647
import org.apache.tsfile.write.schema.IMeasurementSchema;
4748
import org.apache.tsfile.write.schema.MeasurementSchema;
49+
import org.apache.tsfile.write.writer.TsFileIOWriter;
4850
import org.junit.After;
4951
import org.junit.Assert;
5052
import org.junit.Before;
5153
import org.junit.Test;
5254

5355
import java.io.File;
56+
import java.io.IOException;
5457
import java.util.ArrayList;
58+
import java.util.Arrays;
5559
import java.util.Collections;
5660
import java.util.HashMap;
5761
import java.util.List;
@@ -295,6 +299,94 @@ public void testAlignedTsFileWithNullValueCompaction() throws Exception {
295299
CompactionCheckerUtils.validDataByValueList(originData, compactedData);
296300
}
297301

302+
@Test
303+
public void testAlignedCompactionWithLazilyLoadedIntermediatePriorityPage() throws Exception {
304+
String device = storageGroup + ".d0";
305+
List<IMeasurementSchema> schemas =
306+
Arrays.asList(
307+
new MeasurementSchema("s0", TSDataType.INT64),
308+
new MeasurementSchema("s1", TSDataType.INT64));
309+
IDeviceID deviceId = IDeviceID.Factory.DEFAULT_FACTORY.create(device);
310+
311+
TsFileResource highPriorityResource =
312+
writeAlignedTsFile(
313+
"1-3-0-0.tsfile",
314+
deviceId,
315+
schemas,
316+
new long[] {1, 2},
317+
new Long[][] {{10L, null}, {12L, null}});
318+
TsFileResource lowPriorityResource =
319+
writeAlignedTsFile(
320+
"1-1-0-0.tsfile",
321+
deviceId,
322+
schemas,
323+
new long[] {1, 2},
324+
new Long[][] {{null, 11L}, {null, 1L}});
325+
TsFileResource intermediatePriorityResource =
326+
writeAlignedTsFile(
327+
"2-2-0-0.tsfile", deviceId, schemas, new long[] {2}, new Long[][] {{null, 2L}});
328+
329+
List<TsFileResource> sourceResources =
330+
Arrays.asList(highPriorityResource, lowPriorityResource, intermediatePriorityResource);
331+
TsFileResource targetResource =
332+
TsFileNameGenerator.getInnerCompactionTargetFileResource(sourceResources, true);
333+
performer.setSourceFiles(sourceResources);
334+
performer.setTargetFiles(Collections.singletonList(targetResource));
335+
performer.setSummary(new FastCompactionTaskSummary());
336+
performer.perform();
337+
CompactionUtils.moveTargetFile(
338+
Collections.singletonList(targetResource), CompactionTaskType.INNER_SEQ, storageGroup);
339+
340+
IFullPath alignedPath = new AlignedFullPath(deviceId, Arrays.asList("s0", "s1"), schemas);
341+
List<TimeValuePair> compactedData =
342+
CompactionCheckerUtils.getDataByQuery(
343+
Collections.singletonList(alignedPath),
344+
Collections.singletonList(targetResource),
345+
Collections.emptyList())
346+
.get(alignedPath);
347+
TimeValuePair compactedPoint =
348+
compactedData.stream().filter(point -> point.getTimestamp() == 2).findFirst().orElse(null);
349+
350+
Assert.assertNotNull(compactedPoint);
351+
Assert.assertEquals(12L, compactedPoint.getValue().getVector()[0].getLong());
352+
Assert.assertEquals(2L, compactedPoint.getValue().getVector()[1].getLong());
353+
}
354+
355+
private TsFileResource writeAlignedTsFile(
356+
String fileName,
357+
IDeviceID deviceId,
358+
List<IMeasurementSchema> schemas,
359+
long[] timestamps,
360+
Long[][] values)
361+
throws IOException {
362+
TsFileResource resource = new TsFileResource(new File(dataDirectory, fileName));
363+
resource.setSeq(true);
364+
try (TsFileIOWriter writer = new TsFileIOWriter(resource.getTsFile())) {
365+
AlignedChunkWriterImpl chunkWriter = new AlignedChunkWriterImpl(schemas);
366+
for (int row = 0; row < timestamps.length; row++) {
367+
long timestamp = timestamps[row];
368+
chunkWriter.getTimeChunkWriter().write(timestamp);
369+
for (int column = 0; column < schemas.size(); column++) {
370+
Long value = values[row][column];
371+
chunkWriter
372+
.getValueChunkWriterByIndex(column)
373+
.write(timestamp, value == null ? 0 : value, value == null);
374+
}
375+
}
376+
writer.startChunkGroup(deviceId);
377+
chunkWriter.writeToFileWriter(writer);
378+
writer.endChunkGroup();
379+
writer.endFile();
380+
}
381+
for (long timestamp : timestamps) {
382+
resource.updateStartTime(deviceId, timestamp);
383+
resource.updateEndTime(deviceId, timestamp);
384+
}
385+
resource.close();
386+
resource.serialize();
387+
return resource;
388+
}
389+
298390
@Test
299391
public void testAlignedTsFileWithDifferentSchemaInDifferentTsFileCompaction() throws Exception {
300392
List<String> devices = new ArrayList<>();

0 commit comments

Comments
 (0)