ColinLeeo commented on code in PR #19198:
URL: https://github.com/apache/iotdb/pull/19198#discussion_r4559473401
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/buffer/TimeSeriesMetadataCache.java:
##########
@@ -293,27 +301,327 @@ public TimeseriesMetadata get(
return new TimeseriesMetadata(timeseriesMetadata);
}
} finally {
-
queryContext.getQueryStatistics().getLoadBloomFilterTime().getAndAdd(loadBloomFilterTime);
- if (cacheHit) {
- queryContext
- .getQueryStatistics()
- .getLoadTimeSeriesMetadataFromCacheCount()
- .incrementAndGet();
- queryContext
- .getQueryStatistics()
- .getLoadTimeSeriesMetadataFromCacheTime()
- .getAndAdd(System.nanoTime() - startTime - loadBloomFilterTime);
+ long lookupTime = System.nanoTime() - startTime - loadBloomFilterTime;
+ recordLoadStatistics(
+ queryContext,
+ cacheHit ? 1 : 0,
+ cacheHit ? lookupTime : 0,
+ cacheHit ? 0 : 1,
+ cacheHit ? 0 : lookupTime,
+ loadBloomFilterTime);
+ }
+ }
+
+ /**
+ * Load the TimeseriesMetadata of {@code allSensors} of a device, reusing
the per-measurement
+ * cache and reading the device in a single batch only for the measurements
that are still
+ * missing. This is used by wide-table scans (a device with many columns) to
avoid walking the
+ * device metadata index once per column / leaf group.
+ *
+ * <p>Measurements already cached are returned without touching the file.
The remaining ones are
+ * first checked against the file's cached bloom filter, so columns absent
from this file do not
+ * force a device read; only measurements that cannot be ruled out are
loaded by the batch read.
+ *
+ * <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}.
+ *
+ * <p>Load statistics are counted per series (not per device read) so that
they stay comparable
+ * with {@link #get}; the bytes read by the batch read cannot be reported
because {@link
+ * TsFileSequenceReader#getDeviceTimeseriesMetadata(IDeviceID)} has no
IO-size recorder.
+ *
+ * @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 cachePhaseEndTime = 0;
+ long loadBloomFilterTime = 0;
+ long cacheHitCount = 0;
+ long diskLookupCount = 0;
+ boolean delegated = false;
+ boolean externalTsFile = queryContext.isExternalTsFileScan();
+ LongConsumer timeSeriesMetadataIoSizeRecorder =
+
queryContext.getQueryStatistics().getLoadTimeSeriesMetadataActualIOSize()::addAndGet;
+ LongConsumer bloomFilterIoSizeRecorder =
+
queryContext.getQueryStatistics().getLoadBloomFilterActualIOSize()::addAndGet;
+
+ Map<String, TimeseriesMetadata> result = new HashMap<>(allSensors.size());
+ List<String> missing = new ArrayList<>(allSensors.size());
+
+ try {
+ if (CACHE_ENABLE && !externalTsFile) {
+ // fast path without locking: reuse everything the per-measurement
cache already has
+ collectCachedMetadata(tsFileID, device, allSensors, result, missing);
+ cacheHitCount = result.size();
+ if (missing.isEmpty()) {
+ cachePhaseEndTime = System.nanoTime();
+ return result;
+ }
+ // serialize 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)) {
+ List<String> stillMissing = new ArrayList<>(missing.size());
+ collectCachedMetadata(tsFileID, device, missing, result,
stillMissing);
+ missing = stillMissing;
+ cacheHitCount = result.size();
+ cachePhaseEndTime = System.nanoTime();
+ if (missing.isEmpty()) {
+ return result;
+ }
+
+ // measurements the cached bloom filter already rejects are known
absent in this file, so
+ // they must not trigger a device read
+ long bloomFilterStartTime = System.nanoTime();
+ BloomFilter bloomFilter =
+ loadBloomFilter(filePath, tsFileID, queryContext,
bloomFilterIoSizeRecorder, false);
+ List<String> possiblyPresent = new ArrayList<>(missing.size());
+ for (String measurement : missing) {
+ if (bloomFilterMayContain(bloomFilter, device, measurement)) {
+ possiblyPresent.add(measurement);
+ } else {
+ result.put(measurement, null);
+ }
+ }
+ loadBloomFilterTime = System.nanoTime() - bloomFilterStartTime;
+ diskLookupCount = missing.size();
+ if (possiblyPresent.isEmpty()) {
+ return result;
+ }
+
+ // the time column is always written for an aligned device, so a
known-absent time
+ // column means the file will be skipped anyway; a bloom-filter
false positive on a field
+ // must not force a device read and a possible absent-device error
+ if (allSensors.contains("") && result.get("") == null &&
!possiblyPresent.contains("")) {
+ return result;
+ }
+
+ // for a small remainder, a per-column lookup reads one leaf group
instead of the whole
+ // device; those lookups record their own statistics
+ if (possiblyPresent.size() <= MAX_PER_COLUMN_FALLBACK) {
+ delegated = true;
+ diskLookupCount = missing.size() - possiblyPresent.size();
+ for (String measurement : possiblyPresent) {
+ result.put(
+ measurement,
+ get(
+ filePath,
+ new TimeSeriesMetadataCacheKey(tsFileID, device,
measurement),
+ allSensors,
+ ignoreNotExists,
+ false,
+ queryContext));
+ }
+ return result;
+ }
+
+ TsFileSequenceReader reader =
+ FileReaderManager.getInstance()
+ .get(filePath, tsFileID, true,
timeSeriesMetadataIoSizeRecorder, false);
+ result.putAll(
+ readDeviceTimeSeriesMetadata(
+ reader,
+ filePath,
+ tsFileID,
+ device,
+ new HashSet<>(possiblyPresent),
+ true,
+ ignoreNotExists));
+ return result;
+ }
+ }
+
+ // cache disabled / external tsfiles: probe the time column first so an
absent device does
+ // not pay for the whole-device read
+ long bloomFilterStartTime = System.nanoTime();
+ BloomFilter bloomFilter =
+ loadBloomFilter(
+ filePath, tsFileID, queryContext, bloomFilterIoSizeRecorder,
externalTsFile);
+ loadBloomFilterTime = System.nanoTime() - bloomFilterStartTime;
+ diskLookupCount = allSensors.size();
+ if (!bloomFilterMayContain(bloomFilter, device, "")) {
+ for (String measurement : allSensors) {
+ result.put(measurement, null);
+ }
+ return result;
+ }
+ TsFileSequenceReader reader =
+ FileReaderManager.getInstance()
+ .get(filePath, tsFileID, true, timeSeriesMetadataIoSizeRecorder,
externalTsFile);
+ return readDeviceTimeSeriesMetadata(
+ reader, filePath, tsFileID, device, allSensors, false,
ignoreNotExists);
+ } finally {
+ long endTime = System.nanoTime();
+ long cacheLookupTime;
+ long diskLookupTime;
+ if (delegated) {
+ // the per-column fallbacks recorded their own disk time
+ cacheLookupTime = cachePhaseEndTime == 0 ? 0 : cachePhaseEndTime -
startTime;
+ diskLookupTime = 0;
+ } else if (cacheHitCount == 0 || cachePhaseEndTime == 0) {
+ // cold or cache-disabled call: cache probes and the lock wait belong
to the disk lookup
+ cacheLookupTime = 0;
+ diskLookupTime = endTime - startTime - loadBloomFilterTime;
+ } else if (diskLookupCount == 0) {
+ cacheLookupTime = endTime - startTime - loadBloomFilterTime;
+ diskLookupTime = 0;
} else {
- queryContext
- .getQueryStatistics()
- .getLoadTimeSeriesMetadataFromDiskCount()
- .incrementAndGet();
- queryContext
- .getQueryStatistics()
- .getLoadTimeSeriesMetadataFromDiskTime()
- .getAndAdd(System.nanoTime() - startTime - loadBloomFilterTime);
+ cacheLookupTime = cachePhaseEndTime - startTime;
+ diskLookupTime = endTime - cachePhaseEndTime - loadBloomFilterTime;
+ }
+ recordLoadStatistics(
+ queryContext,
+ cacheHitCount,
+ cacheLookupTime,
+ diskLookupCount,
+ diskLookupTime,
+ loadBloomFilterTime);
+ }
+ }
+
+ private BloomFilter loadBloomFilter(
+ String filePath,
+ TsFileID tsFileID,
+ QueryContext queryContext,
+ LongConsumer bloomFilterIoSizeRecorder,
+ boolean externalTsFile)
+ throws IOException {
+ return BloomFilterCache.getInstance()
+ .get(
+ new BloomFilterCache.BloomFilterCacheKey(filePath, tsFileID),
+ false,
+ bloomFilterIoSizeRecorder,
+
queryContext.getQueryStatistics().getLoadBloomFilterFromCacheCount()::addAndGet,
+
queryContext.getQueryStatistics().getLoadBloomFilterFromDiskCount()::addAndGet,
+ externalTsFile);
+ }
+
+ /** A null filter cannot rule the measurement out. */
+ private static boolean bloomFilterMayContain(
+ BloomFilter bloomFilter, IDeviceID device, String measurement) {
+ return bloomFilter == null
+ || bloomFilter.contains(device.toString() +
IoTDBConstant.PATH_SEPARATOR + measurement);
+ }
+
+ private static void recordLoadStatistics(
+ QueryContext queryContext,
+ long cacheHitCount,
+ long cacheLookupTime,
+ long diskLookupCount,
+ long diskLookupTime,
+ long bloomFilterTime) {
+ QueryStatistics statistics = queryContext.getQueryStatistics();
+ statistics.getLoadBloomFilterTime().getAndAdd(bloomFilterTime);
+ if (cacheHitCount > 0) {
+
statistics.getLoadTimeSeriesMetadataFromCacheCount().getAndAdd(cacheHitCount);
+
statistics.getLoadTimeSeriesMetadataFromCacheTime().getAndAdd(cacheLookupTime);
+ }
+ if (diskLookupCount > 0) {
+
statistics.getLoadTimeSeriesMetadataFromDiskCount().getAndAdd(diskLookupCount);
+
statistics.getLoadTimeSeriesMetadataFromDiskTime().getAndAdd(diskLookupTime);
+ }
+ }
+
+ /**
+ * Resolve cached measurements into {@code resolved} and collect the rest
into {@code missing}.
+ */
+ private void collectCachedMetadata(
+ TsFileID tsFileID,
+ IDeviceID device,
+ Collection<String> measurements,
+ Map<String, TimeseriesMetadata> resolved,
+ List<String> missing) {
+ for (String measurement : measurements) {
+ TimeseriesMetadata metadata =
+ lruCache.getIfPresent(new TimeSeriesMetadataCacheKey(tsFileID,
device, measurement));
+ if (metadata == null) {
+ missing.add(measurement);
+ } else {
+ resolved.put(
+ measurement,
+ metadata == NULL_EXISTS_CACHE_PLACE_HOLDER ? null : new
TimeseriesMetadata(metadata));
+ }
+ }
+ }
+
+ private Map<String, TimeseriesMetadata> readDeviceTimeSeriesMetadata(
+ TsFileSequenceReader reader,
+ String filePath,
+ TsFileID tsFileID,
+ IDeviceID device,
+ Set<String> allSensors,
+ boolean cacheEnabled,
+ boolean ignoreNotExists)
+ throws IOException {
+ // Use the chunk-metadata-including variant: the returned
TimeseriesMetadata are passed to
+ // setModifications and then to DiskAlignedChunkMetadataLoader, which
dereferences the chunk
+ // metadata list, so readDeviceMetadata (which skips chunk metadata) would
NPE at query time.
+ List<TimeseriesMetadata> deviceMetadataList =
reader.getDeviceTimeseriesMetadata(device);
Review Comment:
\`TsFileSequenceReader\` is an external \`org.apache.tsfile:tsfile\`
dependency (apache/tsfile), so this PR cannot extend it. Within the existing
APIs we have: per-measurement bloom filtering so absent columns never reach the
read, cache-first reuse, and a per-column fallback for small remainders — the
measurement-level \`readTimeseriesMetadata\` is already group-selective and is
what those paths use. I documented the remaining limitation at the call site
and in the PR description; a truly selective batch API is an upstream
apache/tsfile follow-up that would also let us drop the ratio gate.
_🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
--
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]