From a7f2d6bb43703c7d843125427a4de6998191a688 Mon Sep 17 00:00:00 2001 From: Zhenyu Luo Date: Thu, 21 Nov 2024 21:24:40 +0800 Subject: [PATCH] Load: Fix locally load v3 tsfile with PlainDeviceTimeIndex (#14098) Co-authored-by: Steve Yurong Su --- .../scheduler/load/LoadTsFileScheduler.java | 22 +++++++++++++++++++ .../dataregion/tsfile/TsFileResource.java | 4 +++- .../timeindex/ArrayDeviceTimeIndex.java | 12 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/scheduler/load/LoadTsFileScheduler.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/scheduler/load/LoadTsFileScheduler.java index 5fc847b2b9b7..c112eb344905 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/scheduler/load/LoadTsFileScheduler.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/scheduler/load/LoadTsFileScheduler.java @@ -57,6 +57,8 @@ import org.apache.iotdb.db.storageengine.dataregion.DataRegion; import org.apache.iotdb.db.storageengine.dataregion.flush.MemTableFlushTask; import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource; +import org.apache.iotdb.db.storageengine.dataregion.tsfile.timeindex.ArrayDeviceTimeIndex; +import org.apache.iotdb.db.storageengine.dataregion.tsfile.timeindex.PlainDeviceTimeIndex; import org.apache.iotdb.db.storageengine.load.memory.LoadTsFileDataCacheMemoryBlock; import org.apache.iotdb.db.storageengine.load.memory.LoadTsFileMemoryManager; import org.apache.iotdb.db.storageengine.load.metrics.LoadTsFileCostMetricsSet; @@ -70,6 +72,7 @@ import io.airlift.units.Duration; import org.apache.tsfile.file.metadata.IDeviceID; +import org.apache.tsfile.file.metadata.StringArrayDeviceID; import org.apache.tsfile.utils.Pair; import org.apache.tsfile.utils.PublicBAOS; import org.slf4j.Logger; @@ -89,6 +92,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.CancellationException; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -414,6 +418,24 @@ private boolean loadLocally(LoadSingleTsFileNode node) throws IoTDBException { throw new LoadReadOnlyException(); } + // if the time index is PlainDeviceTimeIndex, convert it to ArrayDeviceTimeIndex + if (node.getTsFileResource().getTimeIndex() instanceof PlainDeviceTimeIndex) { + final PlainDeviceTimeIndex timeIndex = + (PlainDeviceTimeIndex) node.getTsFileResource().getTimeIndex(); + final Map convertedDeviceToIndex = new ConcurrentHashMap<>(); + for (final Map.Entry entry : timeIndex.getDeviceToIndex().entrySet()) { + convertedDeviceToIndex.put( + entry.getKey() instanceof StringArrayDeviceID + ? entry.getKey() + : new StringArrayDeviceID(entry.getKey().toString()), + entry.getValue()); + } + node.getTsFileResource() + .setTimeIndex( + new ArrayDeviceTimeIndex( + convertedDeviceToIndex, timeIndex.getStartTimes(), timeIndex.getEndTimes())); + } + try { FragmentInstance instance = new FragmentInstance( diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java index 912c593f8418..88796349bfbd 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java @@ -646,7 +646,9 @@ public ArrayDeviceTimeIndex buildDeviceTimeIndex() throws IOException { } } - /** Only used for compaction to validate tsfile. */ + /** + * Used for compaction to verify tsfile, also used to verify TimeIndex version when loading tsfile + */ public ITimeIndex getTimeIndex() { return timeIndex; } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/timeindex/ArrayDeviceTimeIndex.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/timeindex/ArrayDeviceTimeIndex.java index f0f52d3b5082..31fd9d385e62 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/timeindex/ArrayDeviceTimeIndex.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/timeindex/ArrayDeviceTimeIndex.java @@ -165,6 +165,18 @@ public Set getDevices(String tsFilePath, TsFileResource tsFileResourc return deviceToIndex.keySet(); } + public Map getDeviceToIndex() { + return deviceToIndex; + } + + public long[] getEndTimes() { + return endTimes; + } + + public long[] getStartTimes() { + return startTimes; + } + /** * Deserialize TimeIndex and get devices only. *