viirya commented on code in PR #7082:
URL: https://github.com/apache/datafusion-comet/pull/7082#discussion_r4357001845


##########
native/core/src/parquet/shared_metadata_cache.rs:
##########
@@ -0,0 +1,634 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! A registry that lets the tasks of one Spark stage share a Parquet 
`FileMetadataCache`.
+//!
+//! Comet builds a `RuntimeEnv`, and with it a metadata cache, for every Spark 
task, so two tasks
+//! that read splits of the same file each fetch and parse its footer and page 
index. This
+//! registry hands every task of a stage on this executor the same cache 
instead.
+//!
+//! The scope is the stage, not the executor, on purpose. A cache that 
outlives the query would
+//! answer a later query from metadata of a file that has since been deleted 
or rewritten, and
+//! `(size, modification time)` validation cannot catch that: Spark's cached 
file index lists the
+//! same size and time as the cached footer. Spark's `HiveMetadataCacheSuite` 
reads a table,
+//! deletes one data file, reads again, and expects `FILE_NOT_EXIST`. Every 
new query is a new
+//! stage, so it opens its files and sees the deletion, as it does without the 
registry.
+//!
+//! A stage attempt is part of the key because a retried stage may read a 
changed file. So is the
+//! Spark application: stage ids restart at zero in every `SparkContext`, and 
this registry is
+//! process-wide, so a context started after another one stopped in the same 
JVM must not match
+//! the stages the old one left behind.
+//!
+//! The registry holds a weak reference to the cache of every stage with a 
running task, since the
+//! tasks own it through their `RuntimeEnv`. It additionally holds strong 
references to the last
+//! [`MAX_IDLE_CACHES`] caches it handed out. Those matter when an executor 
runs one task at a
+//! time: the next task of the stage starts after the previous one released 
its plan. Whatever the
+//! tasks do, at most [`MAX_IDLE_CACHES`] caches outlive the tasks that used 
them. An executor is
+//! not told that a stage has finished, so a retained cache is freed once no 
task of its stage has
+//! run for [`IDLE_CACHE_TIMEOUT`], once tasks of [`MAX_IDLE_CACHES`] other 
stages have asked for
+//! theirs, or when the plugin shuts down and [`clear_shared_metadata_caches`] 
runs, whichever comes
+//! first. An executor that goes idle after a query frees them within the 
timeout.
+//!
+//! A stage's cache holds DataFusion's per-task limit for each of its tasks 
that is running, and one
+//! task's limit once none is, so that sharing does not shrink the room each 
task had with a cache
+//! of its own: tasks that read different files would otherwise evict each 
other's entries, among
+//! them the footers the Delta deletion vector planning loads before the scan 
opens them. Each task
+//! takes its share when it asks for the cache and gives it back when its 
`RuntimeEnv` drops it, so
+//! the limit follows the tasks that actually run, whatever the executor's 
configured cores say.
+//! Shared entries are stored once, so the tasks of a stage hold no more than 
they did, and a
+//! retained cache holds no more than one task's.
+//!
+//! The stage scopes the cache; it does not identify the files in it. 
DataFusion keys an entry by
+//! the file's path within its object store, which drops the bucket, so 
[`scope_to_store`] gives
+//! each scan a view of the cache that also keys by the scan's object store.
+
+use crate::execution::jni_api::get_runtime;
+use crate::execution::spark_config::{
+    SparkConfig, COMET_APP_ID, COMET_PARQUET_SHARED_METADATA_CACHE_ENABLED, 
COMET_STAGE_ATTEMPT,
+    COMET_STAGE_ID,
+};
+use datafusion::common::TableReference;
+use datafusion::datasource::object_store::ObjectStoreUrl;
+use datafusion::execution::cache::cache_manager::{
+    CachedFileMetadataEntry, FileMetadataCache, DEFAULT_METADATA_CACHE_LIMIT,
+};
+use datafusion::execution::cache::default_cache::DefaultCache;
+use datafusion::execution::cache::{Cache, CacheEntryInfo};
+use object_store::path::Path;
+use std::collections::{HashMap, VecDeque};
+use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::{Arc, LazyLock, Mutex, Weak};
+use std::time::{Duration, Instant};
+
+/// How many caches of stages with no running task the registry holds on to.
+const MAX_IDLE_CACHES: usize = 2;

Review Comment:
   Retention now carries a fair amount of machinery: the `retained` queue, the 
expiry task on the runtime, the `release_runtime` hook, and the "two caches, 10 
seconds" wording in the module doc, the config doc and `memory_management.md`. 
I'd like to check that it pays for itself.
   
   As I read it, retention only matters when an executor has no running task of 
the stage between two of its tasks. With several slots, the stage's other 
running tasks keep the cache alive through their `TaskCache` handles. So the 
case is mainly single-slot executors, which is YARN's default. There, the tasks 
one executor runs are about E indices apart, where E is the number of 
executors, since Spark hands out pending tasks in ascending index order. So the 
next task only hits when a file has more splits than there are executors. The 
measurements used 8-core and 4-core executors, where retention shouldn't change 
the result.
   
   Could you share hit counts from `local-cluster` with single-core executors, 
with retention on and off? If it rarely hits there, dropping retention would 
leave only the weak registry and the running-task count. Caches would then be 
freed with their plans as before, and sunchao's restart finding would no longer 
apply. If it does pay off, the timer already covers what the shutdown hook in 
`d80bb2195` was added for, so the hook could probably go.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to