-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add CacheManager for DataFusion #19645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhita
wants to merge
5
commits into
opensearch-project:feature/datafusion
Choose a base branch
from
abhita:feature/datafusion
base: feature/datafusion
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
use std::sync::{Arc, Mutex}; | ||
|
||
|
||
use datafusion::execution::cache::cache_manager::{FileMetadataCache}; | ||
use datafusion::execution::cache::cache_unit::{DefaultFilesMetadataCache}; | ||
use datafusion::execution::cache::CacheAccessor; | ||
use object_store::ObjectMeta; | ||
|
||
// Wrapper to make Mutex<DefaultFilesMetadataCache> implement FileMetadataCache | ||
pub struct MutexFileMetadataCache { | ||
pub inner: Mutex<DefaultFilesMetadataCache>, | ||
} | ||
|
||
impl MutexFileMetadataCache { | ||
pub fn new(cache: DefaultFilesMetadataCache) -> Self { | ||
Self { | ||
inner: Mutex::new(cache), | ||
} | ||
} | ||
} | ||
|
||
// Implement CacheAccessor which is required by FileMetadataCache | ||
impl CacheAccessor<ObjectMeta, Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>> for MutexFileMetadataCache { | ||
type Extra = ObjectMeta; | ||
|
||
fn get(&self, k: &ObjectMeta) -> Option<Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>> { | ||
self.inner.lock().unwrap().get(k) | ||
} | ||
|
||
fn get_with_extra(&self, k: &ObjectMeta, extra: &Self::Extra) -> Option<Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>> { | ||
self.inner.lock().unwrap().get_with_extra(k, extra) | ||
} | ||
|
||
fn put(&self, k: &ObjectMeta, v: Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>) -> Option<Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>> { | ||
self.inner.lock().unwrap().put(k, v) | ||
} | ||
|
||
fn put_with_extra(&self, k: &ObjectMeta, v: Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>, e: &Self::Extra) -> Option<Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>> { | ||
self.inner.lock().unwrap().put_with_extra(k, v, e) | ||
} | ||
|
||
fn remove(&mut self, k: &ObjectMeta) -> Option<Arc<dyn datafusion::execution::cache::cache_manager::FileMetadata>> { | ||
self.inner.lock().unwrap().remove(k) | ||
} | ||
|
||
fn contains_key(&self, k: &ObjectMeta) -> bool { | ||
self.inner.lock().unwrap().contains_key(k) | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.inner.lock().unwrap().len() | ||
} | ||
|
||
fn clear(&self) { | ||
self.inner.lock().unwrap().clear() | ||
} | ||
|
||
fn name(&self) -> String { | ||
self.inner.lock().unwrap().name() | ||
} | ||
} | ||
|
||
impl FileMetadataCache for MutexFileMetadataCache { | ||
fn cache_limit(&self) -> usize { | ||
self.inner.lock().unwrap().cache_limit() | ||
} | ||
|
||
fn update_cache_limit(&self, limit: usize) { | ||
self.inner.lock().unwrap().update_cache_limit(limit) | ||
} | ||
|
||
fn list_entries(&self) -> std::collections::HashMap<object_store::path::Path, datafusion::execution::cache::cache_manager::FileMetadataCacheEntry> { | ||
self.inner.lock().unwrap().list_entries() | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CacheManager introduced here aims to perform periodic updates to Cache(refresh/stale files).
It is expected to have
multiple calls to CacheManager for mutating operations resulting in multiple references at a given time
for its' underlying registered Cache.DefaultFilesMetadataCache
of datafusion is internally wrapped in aMutex
and requires mut access for operations likeremove
.Refer: https://github.com/apache/datafusion/blob/main/datafusion/execution/src/cache/cache_unit.rs#L312-L315
https://github.com/apache/datafusion/blob/main/datafusion/execution/src/cache/cache_unit.rs#L402
Having multiple references to Cache, trying to acquire a mutable reference for methods like
remove
would lead to failures. Hence explicit handling of mutable references is required for whichMutexFileMetadataCache
is introducedWhy it fails in shared contexts
When we share a cache instance like this:
Rust will complain:
cannot borrow data in an
Arc
as mutableReason for above failure - Because Arc only gives shared immutable access (&self), and we can’t call a method requiring &mut self on it — even if the method internally uses a lock
Using
manually converts a raw pointer into a mutable reference, asserting exclusive ownership.
This is undefined behaviour if the cache is shared or accessed elsewhere, as it violates Rust’s aliasing guarantees.
Even though
DefaultFilesMetadataCache
has internal locks, its API requires&mut
self, and Rust still enforces exclusive access at the type level.To safely share and mutate it across threads, wrap it in Mutex or RwLock, which manage exclusive access at runtime without violating safety rules.
Why the MutexFileMetadataCache wrapper helps
By introducing this:
we can now share it safely with Arc and still perform mutations:
The outer Mutex provides mutability via runtime locking while keeping Rust’s compile-time borrow checker happy — because all operations happen through a lock guard that gives a unique mutable reference.