Skip to content

Commit ad8e340

Browse files
authored
Optimize ttl deletion in fast compaction performer (#16367) (#16383)
* optimize ttl deletion in fast compaction performer * fix ut * check isEmpty
1 parent ae37192 commit ad8e340

10 files changed

Lines changed: 52 additions & 24 deletions

File tree

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/performer/impl/FastCompactionPerformer.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,16 @@ public void perform() throws Exception {
142142
// checked above
143143
//noinspection OptionalGetWithoutIsPresent
144144
sortedSourceFiles.sort(Comparator.comparingLong(x -> x.getStartTime(device).get()));
145+
Deletion ttlDeletion = null;
145146
if (ttl != Long.MAX_VALUE) {
146-
Deletion ttlDeletion =
147+
ttlDeletion =
147148
new Deletion(
148149
new PartialPath(device, IoTDBConstant.ONE_LEVEL_PATH_WILDCARD),
149150
Long.MAX_VALUE,
150151
Long.MIN_VALUE,
151152
deviceIterator.getTimeLowerBoundForCurrentDevice());
152-
for (TsFileResource sourceFile : sortedSourceFiles) {
153-
modificationCache
154-
.computeIfAbsent(
155-
sourceFile.getTsFile().getName(),
156-
k -> PatternTreeMapFactory.getModsPatternTreeMap())
157-
.append(ttlDeletion.getPath(), ttlDeletion);
158-
}
159153
}
154+
compactionWriter.setTTLDeletion(ttlDeletion);
160155

161156
if (sortedSourceFiles.isEmpty()) {
162157
// device is out of dated in all source files

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.iotdb.db.service.metrics.FileMetrics;
3131
import org.apache.iotdb.db.storageengine.dataregion.compaction.constant.CompactionTaskType;
3232
import org.apache.iotdb.db.storageengine.dataregion.compaction.schedule.CompactionTaskManager;
33+
import org.apache.iotdb.db.storageengine.dataregion.modification.Deletion;
3334
import org.apache.iotdb.db.storageengine.dataregion.modification.Modification;
3435
import org.apache.iotdb.db.storageengine.dataregion.modification.ModificationFile;
3536
import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
@@ -48,6 +49,7 @@
4849

4950
import java.io.File;
5051
import java.io.IOException;
52+
import java.util.ArrayList;
5153
import java.util.Collection;
5254
import java.util.Collections;
5355
import java.util.HashSet;
@@ -376,13 +378,23 @@ public static void deleteTsFileResourceWithoutLock(TsFileResource resource) {
376378
public static List<Modification> getMatchedModifications(
377379
PatternTreeMap<Modification, PatternTreeMapFactory.ModsSerializer> patternTreeMap,
378380
IDeviceID deviceID,
379-
String measurement)
381+
String measurement,
382+
Deletion ttlDeletion)
380383
throws IllegalPathException {
381-
if (patternTreeMap == null) {
382-
return Collections.emptyList();
384+
if ((patternTreeMap == null) || patternTreeMap.isEmpty()) {
385+
return ttlDeletion == null ? Collections.emptyList() : Collections.singletonList(ttlDeletion);
383386
}
384387
PartialPath path = CompactionPathUtils.getPath(deviceID, measurement);
385-
return ModificationFile.sortAndMerge(patternTreeMap.getOverlapped(path));
388+
List<Modification> modifications = patternTreeMap.getOverlapped(path);
389+
if (ttlDeletion != null) {
390+
if (!(modifications instanceof ArrayList)) {
391+
List<Modification> newModEntries = new ArrayList<>(modifications.size() + 1);
392+
newModEntries.addAll(modifications);
393+
modifications = newModEntries;
394+
}
395+
modifications.add(ttlDeletion);
396+
}
397+
return ModificationFile.sortAndMerge(modifications);
386398
}
387399

388400
public static boolean isDiskHasSpace() {

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,7 @@ private void applyModificationForAlignedChunkMetadataList(
451451
}
452452
List<Modification> modificationList =
453453
CompactionUtils.getMatchedModifications(
454-
modifications, device, valueChunkMetadata.getMeasurementUid());
455-
if (ttlDeletion != null) {
456-
modificationList.add(ttlDeletion);
457-
}
454+
modifications, device, valueChunkMetadata.getMeasurementUid(), ttlDeletion);
458455
modificationForCurDevice.add(
459456
modificationList.isEmpty() ? Collections.emptyList() : modificationList);
460457
}
@@ -666,11 +663,7 @@ public String nextSeries() throws IllegalPathException {
666663
// collect the modifications for current series
667664
List<Modification> modificationForCurrentSeries =
668665
CompactionUtils.getMatchedModifications(
669-
modificationsInThisResource, device, currentCompactingSeries);
670-
// add ttl deletion for current series
671-
if (ttlDeletion != null) {
672-
modificationForCurrentSeries.add(ttlDeletion);
673-
}
666+
modificationsInThisResource, device, currentCompactingSeries, ttlDeletion);
674667

675668
// if there are modifications of current series, apply them to the chunk metadata
676669
if (!modificationForCurrentSeries.isEmpty()) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,11 @@ protected List<Modification> getModificationsFromCache(
478478
throws IllegalPathException {
479479
PatternTreeMap<Modification, PatternTreeMapFactory.ModsSerializer> allModifications =
480480
modificationCacheMap.get(tsFileResource.getTsFile().getName());
481-
return CompactionUtils.getMatchedModifications(allModifications, deviceId, measurement);
481+
return CompactionUtils.getMatchedModifications(
482+
allModifications,
483+
deviceId,
484+
measurement,
485+
compactionWriter.getTTLLowerBoundForCurrentDevice());
482486
}
483487

484488
@SuppressWarnings("squid:S3776")

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.iotdb.db.storageengine.dataregion.compaction.execute.utils.executor.fast.element.ChunkMetadataElement;
2727
import org.apache.iotdb.db.storageengine.dataregion.compaction.execute.utils.writer.flushcontroller.AbstractCompactionFlushController;
2828
import org.apache.iotdb.db.storageengine.dataregion.compaction.io.CompactionTsFileWriter;
29+
import org.apache.iotdb.db.storageengine.dataregion.modification.Deletion;
2930

3031
import org.apache.tsfile.exception.write.PageException;
3132
import org.apache.tsfile.file.header.PageHeader;
@@ -99,10 +100,20 @@ public abstract class AbstractCompactionWriter implements AutoCloseable {
99100

100101
protected String[] measurementId = new String[subTaskNum];
101102

103+
protected Deletion ttlDeletionForCurrentDevice;
104+
102105
public abstract void startChunkGroup(IDeviceID deviceId, boolean isAlign) throws IOException;
103106

104107
public abstract void endChunkGroup() throws IOException;
105108

109+
public void setTTLDeletion(Deletion ttlDeletion) {
110+
this.ttlDeletionForCurrentDevice = ttlDeletion;
111+
}
112+
113+
public Deletion getTTLLowerBoundForCurrentDevice() {
114+
return ttlDeletionForCurrentDevice;
115+
}
116+
106117
public void startMeasurement(String measurement, IChunkWriter chunkWriter, int subTaskId) {
107118
lastCheckIndex = 0;
108119
lastTimeSet[subTaskId] = false;

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/selector/estimator/AbstractCompactionEstimator.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ public FileInfo.RoughFileInfo getRoughFileInfo(TsFileResource resource) {
246246
return roughFileInfo;
247247
}
248248

249+
public boolean supportsRoughEstimation() {
250+
return true;
251+
}
252+
249253
public static void removeFileInfoFromGlobalFileInfoCache(TsFileResource resource) {
250254
if (resource == null || resource.getTsFile() == null) {
251255
return;

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/selector/estimator/RepairUnsortedFileCompactionEstimator.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,9 @@ public long roughEstimateInnerCompactionMemory(
8989
CompactionScheduleContext context, List<TsFileResource> resources) throws IOException {
9090
throw new RuntimeException("unimplemented");
9191
}
92+
93+
@Override
94+
public boolean supportsRoughEstimation() {
95+
return false;
96+
}
9297
}

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/selector/impl/NewSizeTieredCompactionSelector.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void resetMemoryEstimationFields() {
201201
performer =
202202
sequence ? context.getSeqCompactionPerformer() : context.getUnseqCompactionPerformer();
203203
estimator = performer.getInnerSpaceEstimator().orElse(null);
204-
if (estimator == null) {
204+
if (estimator == null || !estimator.supportsRoughEstimation()) {
205205
estimateCompactionTaskMemoryDuringSelection = false;
206206
}
207207
memoryCost = 0;

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/selector/impl/SettleSelectorImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private boolean isDeviceDeletedByMods(
288288
throws IllegalPathException {
289289
List<Modification> deviceModifications =
290290
CompactionUtils.getMatchedModifications(
291-
modifications, device, AlignedPath.VECTOR_PLACEHOLDER);
291+
modifications, device, AlignedPath.VECTOR_PLACEHOLDER, null);
292292
for (Modification modification : deviceModifications) {
293293
if (((Deletion) modification).getTimeRange().contains(startTime, endTime)) {
294294
return true;

‎iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PatternTreeMap.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public PatternTreeMap(
6161
this.serializer = serializer;
6262
}
6363

64+
public boolean isEmpty() {
65+
return root.isLeaf() && root.getValues().isEmpty();
66+
}
67+
6468
/**
6569
* Append key and value to PatternTreeMap.
6670
*

0 commit comments

Comments
 (0)