shuwenwei commented on code in PR #19198:
URL: https://github.com/apache/iotdb/pull/19198#discussion_r4558865769


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/FileLoaderUtils.java:
##########
@@ -72,6 +73,12 @@ public class FileLoaderUtils {
   private static final SeriesScanCostMetricSet SERIES_SCAN_COST_METRIC_SET =
       SeriesScanCostMetricSet.getInstance();
 
+  /**
+   * Fraction of a table's field columns that a scan must select for it to use 
the batch
+   * device-metadata read instead of the per-column path.
+   */
+  private static final double DEVICE_METADATA_BATCH_SCAN_RATIO = 0.6;

Review Comment:
   **Consider a selective batch reader instead of a fixed ratio**
   
   The `0.6` threshold appears to compensate for the current batch reader 
loading the entire device, including unrequested measurements. In principle, a 
selective batch reader could visit only the metadata blocks needed by the 
requested measurements, read each block once, and share index traversal across 
measurements. A single-measurement lookup would then be a one-element batch.
   
   Assuming collection-management overhead is negligible, this could match the 
per-column path’s required I/O while reducing repeated index traversal. The 
ratio check might therefore be unnecessary once selective reading is 
implemented. This would require improving the reader’s selectivity; simply 
removing the current gate could cause unnecessary full-device reads.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/buffer/TimeSeriesMetadataCache.java:
##########
@@ -316,6 +317,216 @@ public TimeseriesMetadata get(
     }
   }
 
+  /**
+   * Load the TimeseriesMetadata of {@code allSensors} of a device, reading 
the device in a single
+   * batch when the per-measurement cache does not already hold all of them. 
This is used by
+   * wide-table scans (a device with many columns) to avoid walking the device 
metadata index once
+   * per column / leaf node.
+   *
+   * <p>The returned map always holds copies of the cached (or freshly read) 
metadata, so the caller
+   * may apply {@code setModified} and {@code setChunkMetadataLoader} without 
corrupting cached
+   * entries — matching {@link #get}.
+   *
+   * @param filePath path of the tsfile
+   * @param tsFileID id of the tsfile
+   * @param device target device
+   * @param allSensors measurements the caller needs, including the time 
column keyed by ""
+   * @param queryContext query context used for metrics and to detect external 
tsfiles
+   * @param ignoreNotExists whether a device missing from a file that claims a 
device-level time
+   *     index should be skipped instead of reported as an inconsistency
+   * @return map from measurementId (including the time column, keyed by "") 
to TimeseriesMetadata;
+   *     entries whose series is empty (count == 0) are mapped to null
+   * @throws IOException if the metadata cannot be read from disk or the 
device is unexpectedly
+   *     absent
+   */
+  public Map<String, TimeseriesMetadata> getDeviceTimeSeriesMetadata(
+      String filePath,
+      TsFileID tsFileID,
+      IDeviceID device,
+      Set<String> allSensors,
+      QueryContext queryContext,
+      boolean ignoreNotExists)
+      throws IOException {
+    long startTime = System.nanoTime();
+    long loadBloomFilterTime = 0;
+    boolean externalTsFile = queryContext.isExternalTsFileScan();
+    LongConsumer timeSeriesMetadataIoSizeRecorder =
+        
queryContext.getQueryStatistics().getLoadTimeSeriesMetadataActualIOSize()::addAndGet;
+    LongConsumer bloomFilterIoSizeRecorder =
+        
queryContext.getQueryStatistics().getLoadBloomFilterActualIOSize()::addAndGet;
+    boolean cacheHit = true;
+
+    try {
+      if (CACHE_ENABLE && !externalTsFile) {
+        Map<String, TimeseriesMetadata> cached =
+            getCachedDeviceTimeSeriesMetadata(tsFileID, device, allSensors);
+        if (cached != null) {
+          return cached;
+        }
+        // serialize concurrent reads of the same device+file (same lock key 
as get) so threads
+        // don't each deserialize the whole device and race on the shared 
reader
+        synchronized (
+            devices.computeIfAbsent(device.toString() + SEPARATOR + filePath, 
WeakReference::new)) {
+          cached = getCachedDeviceTimeSeriesMetadata(tsFileID, device, 
allSensors);
+          if (cached != null) {
+            return cached;
+          }
+          cacheHit = false;
+          long bloomFilterStartTime = System.nanoTime();
+          boolean devicePossiblyPresent =
+              isDevicePossiblyPresent(
+                  filePath, tsFileID, device, queryContext, 
bloomFilterIoSizeRecorder);
+          loadBloomFilterTime = System.nanoTime() - bloomFilterStartTime;
+          if (!devicePossiblyPresent) {
+            return Collections.emptyMap();
+          }
+          TsFileSequenceReader reader =
+              FileReaderManager.getInstance()
+                  .get(filePath, tsFileID, true, 
timeSeriesMetadataIoSizeRecorder, false);
+          return readDeviceTimeSeriesMetadata(
+              reader, tsFileID, device, allSensors, true, ignoreNotExists);
+        }
+      }
+      cacheHit = false;
+      long bloomFilterStartTime = System.nanoTime();
+      boolean devicePossiblyPresent =
+          isDevicePossiblyPresent(
+              filePath, tsFileID, device, queryContext, 
bloomFilterIoSizeRecorder);
+      loadBloomFilterTime = System.nanoTime() - bloomFilterStartTime;
+      if (!devicePossiblyPresent) {
+        return Collections.emptyMap();
+      }
+      TsFileSequenceReader reader =
+          FileReaderManager.getInstance()
+              .get(filePath, tsFileID, true, timeSeriesMetadataIoSizeRecorder, 
externalTsFile);
+      return readDeviceTimeSeriesMetadata(
+          reader, tsFileID, device, allSensors, false, ignoreNotExists);
+    } finally {
+      
queryContext.getQueryStatistics().getLoadBloomFilterTime().getAndAdd(loadBloomFilterTime);
+      if (cacheHit) {
+        queryContext
+            .getQueryStatistics()
+            .getLoadTimeSeriesMetadataFromCacheCount()
+            .incrementAndGet();
+        queryContext
+            .getQueryStatistics()
+            .getLoadTimeSeriesMetadataFromCacheTime()
+            .getAndAdd(System.nanoTime() - startTime - loadBloomFilterTime);
+      } else {
+        queryContext
+            .getQueryStatistics()
+            .getLoadTimeSeriesMetadataFromDiskCount()
+            .incrementAndGet();
+        queryContext
+            .getQueryStatistics()
+            .getLoadTimeSeriesMetadataFromDiskTime()
+            .getAndAdd(System.nanoTime() - startTime - loadBloomFilterTime);
+      }
+    }
+  }
+
+  /**
+   * The time column (keyed by "") is written for every aligned device, so its 
bloom filter entry is
+   * a cheap per-file presence probe. A null filter cannot rule the device out.
+   */
+  private boolean isDevicePossiblyPresent(
+      String filePath,
+      TsFileID tsFileID,
+      IDeviceID device,
+      QueryContext queryContext,
+      LongConsumer bloomFilterIoSizeRecorder)
+      throws IOException {
+    BloomFilter bloomFilter =
+        BloomFilterCache.getInstance()
+            .get(
+                new BloomFilterCache.BloomFilterCacheKey(filePath, tsFileID),
+                false,
+                bloomFilterIoSizeRecorder,
+                
queryContext.getQueryStatistics().getLoadBloomFilterFromCacheCount()::addAndGet,
+                
queryContext.getQueryStatistics().getLoadBloomFilterFromDiskCount()::addAndGet,
+                false);
+    return bloomFilter == null
+        || bloomFilter.contains(device.toString() + 
IoTDBConstant.PATH_SEPARATOR);
+  }
+
+  /**
+   * @return copies of the cached metadata for every measurement in {@code 
allSensors}, or null as
+   *     soon as one of them is not cached and the device therefore has to be 
read from disk
+   */
+  private Map<String, TimeseriesMetadata> getCachedDeviceTimeSeriesMetadata(
+      TsFileID tsFileID, IDeviceID device, Set<String> allSensors) {
+    Map<String, TimeseriesMetadata> result = new HashMap<>(allSensors.size());
+    for (String measurement : allSensors) {
+      TimeseriesMetadata metadata =
+          lruCache.getIfPresent(new TimeSeriesMetadataCacheKey(tsFileID, 
device, measurement));
+      if (metadata == null) {
+        return null;

Review Comment:
   **Preserve cache hits and batch-read only missing measurements**
   
   `getCachedDeviceTimeSeriesMetadata()` currently returns `null` as soon as 
one measurement is missing, and the subsequent reader call reloads the entire 
device. Consequently, even if almost all requested measurements are already 
cached, a single miss triggers a full-device read.
   
   Could we retain the cached results, collect the missing measurements, 
recheck those misses under the existing device lock, and pass only the 
remaining measurements to a selective reader? The freshly loaded metadata could 
then be cached and merged with the retained results. The filtering needs to 
happen before selecting metadata blocks in the reader; filtering the results 
after a full-device read would not save I/O.



-- 
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