jt2594838 commented on code in PR #13773:
URL: https://github.com/apache/iotdb/pull/13773#discussion_r1802225262


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/utils/executor/batch/utils/BatchCompactionPlan.java:
##########
@@ -21,18 +21,53 @@
 
 import 
org.apache.iotdb.db.storageengine.dataregion.compaction.execute.utils.executor.ModifiedStatus;
 
+import org.apache.tsfile.file.metadata.ChunkMetadata;
+import org.apache.tsfile.read.TsFileSequenceReader;
+import org.apache.tsfile.read.common.Chunk;
 import org.apache.tsfile.read.common.TimeRange;
+import org.apache.tsfile.utils.Pair;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 public class BatchCompactionPlan {
+  public static final long MAX_CACHED_TIME_CHUNKS_SIZE = 2 * 1024 * 1024;
   private final List<CompactChunkPlan> compactChunkPlans = new ArrayList<>();
   private final Map<String, Map<TimeRange, ModifiedStatus>> 
alignedPageModifiedStatusCache =
       new HashMap<>();
+  private final Map<Pair<String, Long>, Chunk> cachedTimeChunks = new 
HashMap<>();

Review Comment:
   Add a comment to explain the meaning of the key.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/utils/executor/batch/BatchedReadChunkAlignedSeriesCompactionExecutor.java:
##########
@@ -249,6 +251,17 @@ protected boolean isAllValuePageEmpty(PageLoader timePage, 
List<PageLoader> valu
       return modifiedStatus == ModifiedStatus.ALL_DELETED;
     }
 
+    @Override
+    protected ChunkLoader getChunkLoader(TsFileSequenceReader reader, 
ChunkMetadata chunkMetadata)
+        throws IOException {
+      ChunkLoader chunkLoader = super.getChunkLoader(reader, chunkMetadata);
+      if (!chunkLoader.isEmpty() && 
chunkLoader.getChunkMetadata().getMeasurementUid().isEmpty()) {

Review Comment:
   May abstract `getChunkMetadata().getMeasurementUid().isEmpty()` as 
SomeUtil.isTimeChunk(ChunkMetadata).



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/utils/executor/batch/utils/BatchCompactionPlan.java:
##########
@@ -21,18 +21,53 @@
 
 import 
org.apache.iotdb.db.storageengine.dataregion.compaction.execute.utils.executor.ModifiedStatus;
 
+import org.apache.tsfile.file.metadata.ChunkMetadata;
+import org.apache.tsfile.read.TsFileSequenceReader;
+import org.apache.tsfile.read.common.Chunk;
 import org.apache.tsfile.read.common.TimeRange;
+import org.apache.tsfile.utils.Pair;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 public class BatchCompactionPlan {
+  public static final long MAX_CACHED_TIME_CHUNKS_SIZE = 2 * 1024 * 1024;
   private final List<CompactChunkPlan> compactChunkPlans = new ArrayList<>();
   private final Map<String, Map<TimeRange, ModifiedStatus>> 
alignedPageModifiedStatusCache =
       new HashMap<>();
+  private final Map<Pair<String, Long>, Chunk> cachedTimeChunks = new 
HashMap<>();
+  private long cachedTimeChunkSize = 0;
+
+  public Chunk getTimeChunkFromCache(TsFileSequenceReader reader, 
ChunkMetadata chunkMetadata)
+      throws IOException {
+    Pair<String, Long> key =
+        new Pair<>(reader.getFileName(), 
chunkMetadata.getOffsetOfChunkHeader());
+    Chunk chunk = cachedTimeChunks.get(key);
+    if (chunk == null) {
+      chunk = reader.readMemChunk(chunkMetadata);
+    }
+    chunk.getData().flip();
+    return chunk;
+  }
+
+  public void addTimeChunkToCache(String file, long offset, Chunk chunk) {
+    if (cachedTimeChunkSize >= MAX_CACHED_TIME_CHUNKS_SIZE) {
+      return;
+    }
+    cachedTimeChunks.put(
+        new Pair<>(file, offset),
+        new Chunk(
+            chunk.getHeader(),
+            chunk.getData(),
+            chunk.getDeleteIntervalList(),
+            chunk.getChunkStatistic(),
+            chunk.getDecryptor()));
+    cachedTimeChunkSize += chunk.getHeader().getDataSize();
+  }

Review Comment:
   So this cache only caches the first chunks satisfying 
MAX_CACHED_TIME_CHUNKS_SIZE. How about using an LRUCache?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/utils/executor/batch/BatchedFastAlignedSeriesCompactionExecutor.java:
##########
@@ -375,6 +388,15 @@ protected List<AlignedChunkMetadata> 
getAlignedChunkMetadataList(TsFileResource
       return getAlignedChunkMetadataListBySelectedValueColumn(resource, 
measurementSchemas);
     }
 
+    @Override
+    protected Chunk readChunk(TsFileSequenceReader reader, ChunkMetadata 
chunkMetadata)
+        throws IOException {
+      if (chunkMetadata != null && 
chunkMetadata.getMeasurementUid().isEmpty()) {
+        return batchCompactionPlan.getTimeChunkFromCache(reader, 
chunkMetadata);
+      }
+      return super.readChunk(reader, chunkMetadata);
+    }

Review Comment:
   What is the behavior of super.readChunk(reader, chunkMetadata) when 
chunkMetadata is null?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/utils/executor/batch/utils/BatchCompactionPlan.java:
##########
@@ -21,18 +21,53 @@
 
 import 
org.apache.iotdb.db.storageengine.dataregion.compaction.execute.utils.executor.ModifiedStatus;
 
+import org.apache.tsfile.file.metadata.ChunkMetadata;
+import org.apache.tsfile.read.TsFileSequenceReader;
+import org.apache.tsfile.read.common.Chunk;
 import org.apache.tsfile.read.common.TimeRange;
+import org.apache.tsfile.utils.Pair;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 public class BatchCompactionPlan {
+  public static final long MAX_CACHED_TIME_CHUNKS_SIZE = 2 * 1024 * 1024;
   private final List<CompactChunkPlan> compactChunkPlans = new ArrayList<>();
   private final Map<String, Map<TimeRange, ModifiedStatus>> 
alignedPageModifiedStatusCache =
       new HashMap<>();
+  private final Map<Pair<String, Long>, Chunk> cachedTimeChunks = new 
HashMap<>();
+  private long cachedTimeChunkSize = 0;
+
+  public Chunk getTimeChunkFromCache(TsFileSequenceReader reader, 
ChunkMetadata chunkMetadata)
+      throws IOException {
+    Pair<String, Long> key =
+        new Pair<>(reader.getFileName(), 
chunkMetadata.getOffsetOfChunkHeader());
+    Chunk chunk = cachedTimeChunks.get(key);
+    if (chunk == null) {
+      chunk = reader.readMemChunk(chunkMetadata);
+    }
+    chunk.getData().flip();
+    return chunk;
+  }
+
+  public void addTimeChunkToCache(String file, long offset, Chunk chunk) {
+    if (cachedTimeChunkSize >= MAX_CACHED_TIME_CHUNKS_SIZE) {
+      return;
+    }
+    cachedTimeChunks.put(
+        new Pair<>(file, offset),
+        new Chunk(
+            chunk.getHeader(),
+            chunk.getData(),
+            chunk.getDeleteIntervalList(),
+            chunk.getChunkStatistic(),
+            chunk.getDecryptor()));

Review Comment:
   Why is this Chunk copied?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to