-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: Support cloud storage in scan_csv
#16674
Merged
Merged
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
aba8b12
c
nameexhaustion 07bfbfd
fix test and features
nameexhaustion cdce48c
fix features
nameexhaustion addcfcd
fix features
nameexhaustion d0635d0
fix features
nameexhaustion f20c201
try ignore wasm
nameexhaustion 4d9aba1
fix features
nameexhaustion 36bb536
try wasm
nameexhaustion b8b2b11
add eviction logic
nameexhaustion 793930d
#[cfg(feature = "async")] -> #[cfg(feature = "cloud")]
nameexhaustion 96bb6a9
introduce `file_cache` feature, make `blake3`, `fs4` optional
nameexhaustion 741e7a5
refactor + add comments to `file_fetcher`
nameexhaustion 9c202c6
consolidate path-finding to `static POLARS_TEMP_DIR_BASE_PATH` and `s…
nameexhaustion 312056f
increase minimum sleep interval for gc to 60 secs
nameexhaustion f10c04f
fix directory init
nameexhaustion da40630
code style
nameexhaustion 7392d2b
refactor + comment
nameexhaustion 3ca891c
use notify in background unlock, and add comments
nameexhaustion 62afca6
inaccuracy in try_unlock
nameexhaustion b131a2b
nit
nameexhaustion 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,153 @@ | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::{Arc, RwLock}; | ||
use std::time::Duration; | ||
|
||
use once_cell::sync::Lazy; | ||
use polars_core::config; | ||
use polars_error::PolarsResult; | ||
use polars_utils::aliases::PlHashMap; | ||
|
||
use super::entry::FileCacheEntry; | ||
use super::eviction::EvictionManager; | ||
use super::file_fetcher::FileFetcher; | ||
use crate::file_cache::entry::{DATA_PREFIX, METADATA_PREFIX}; | ||
use crate::prelude::is_cloud_url; | ||
|
||
pub static FILE_CACHE: Lazy<FileCache> = Lazy::new(|| { | ||
let prefix = std::env::var("POLARS_TEMP_DIR") | ||
.unwrap_or_else(|_| std::env::temp_dir().to_string_lossy().into_owned()); | ||
let prefix = PathBuf::from(prefix).join("polars/file-cache/"); | ||
let prefix = Arc::<Path>::from(prefix.as_path()); | ||
|
||
if config::verbose() { | ||
eprintln!("file cache prefix: {}", prefix.to_str().unwrap()); | ||
} | ||
|
||
EvictionManager { | ||
prefix: prefix.clone(), | ||
files_to_remove: None, | ||
limit_since_last_access: Duration::from_secs( | ||
std::env::var("POLARS_FILE_CACHE_TTL") | ||
.map(|x| x.parse::<u64>().expect("integer")) | ||
.unwrap_or(60 * 60), | ||
), | ||
} | ||
.run_in_background(); | ||
|
||
FileCache::new(prefix) | ||
}); | ||
|
||
pub struct FileCache { | ||
prefix: Arc<Path>, | ||
entries: Arc<RwLock<PlHashMap<Arc<str>, Arc<FileCacheEntry>>>>, | ||
} | ||
|
||
impl FileCache { | ||
fn new(prefix: Arc<Path>) -> Self { | ||
let path = &prefix | ||
.as_ref() | ||
.join(std::str::from_utf8(&[METADATA_PREFIX]).unwrap()); | ||
let _ = std::fs::create_dir_all(path); | ||
assert!( | ||
path.is_dir(), | ||
"failed to create file cache metadata directory: {}", | ||
path.to_str().unwrap(), | ||
); | ||
|
||
let path = &prefix | ||
.as_ref() | ||
.join(std::str::from_utf8(&[DATA_PREFIX]).unwrap()); | ||
let _ = std::fs::create_dir_all(path); | ||
assert!( | ||
path.is_dir(), | ||
"failed to create file cache data directory: {}", | ||
path.to_str().unwrap(), | ||
); | ||
|
||
Self { | ||
prefix, | ||
entries: Default::default(), | ||
} | ||
} | ||
|
||
/// If `uri` is a local path, it must be an absolute path. | ||
pub fn init_entry<F: Fn() -> PolarsResult<Arc<dyn FileFetcher>>>( | ||
&self, | ||
uri: Arc<str>, | ||
get_file_fetcher: F, | ||
) -> PolarsResult<Arc<FileCacheEntry>> { | ||
let verbose = config::verbose(); | ||
|
||
#[cfg(debug_assertions)] | ||
{ | ||
// Local paths must be absolute or else the cache would be wrong. | ||
if !crate::utils::is_cloud_url(uri.as_ref()) { | ||
let path = Path::new(uri.as_ref()); | ||
assert_eq!(path, std::fs::canonicalize(path).unwrap().as_path()); | ||
} | ||
} | ||
|
||
{ | ||
let entries = self.entries.read().unwrap(); | ||
|
||
if let Some(entry) = entries.get(uri.as_ref()) { | ||
if verbose { | ||
eprintln!( | ||
"[file_cache] init_entry: return existing entry for uri = {}", | ||
uri.clone() | ||
); | ||
} | ||
return Ok(entry.clone()); | ||
} | ||
} | ||
|
||
let uri_hash = blake3::hash(uri.as_bytes()) | ||
.to_hex() | ||
.get(..32) | ||
.unwrap() | ||
.to_string(); | ||
|
||
{ | ||
let mut entries = self.entries.write().unwrap(); | ||
|
||
// May have been raced | ||
if let Some(entry) = entries.get(uri.as_ref()) { | ||
if verbose { | ||
eprintln!("[file_cache] init_entry: return existing entry for uri = {} (lost init race)", uri.clone()); | ||
} | ||
return Ok(entry.clone()); | ||
} | ||
|
||
if verbose { | ||
eprintln!( | ||
"[file_cache] init_entry: creating new entry for uri = {}, hash = {}", | ||
uri.clone(), | ||
uri_hash.clone() | ||
); | ||
} | ||
|
||
let entry = Arc::new(FileCacheEntry::new( | ||
uri.clone(), | ||
uri_hash, | ||
self.prefix.clone(), | ||
get_file_fetcher()?, | ||
)); | ||
entries.insert_unique_unchecked(uri, entry.clone()); | ||
Ok(entry.clone()) | ||
} | ||
} | ||
|
||
/// This function can accept relative local paths. | ||
pub fn get_entry(&self, uri: &str) -> Option<Arc<FileCacheEntry>> { | ||
if is_cloud_url(uri) { | ||
self.entries.read().unwrap().get(uri).map(Arc::clone) | ||
} else { | ||
let uri = std::fs::canonicalize(uri).unwrap(); | ||
self.entries | ||
.read() | ||
.unwrap() | ||
.get(uri.to_str().unwrap()) | ||
.map(Arc::clone) | ||
} | ||
} | ||
} |
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.
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.
Can we make this an optional dependency? Only activated when we activate the caching?
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.
I put them behind a new
file_cache
feature flag(although maybe we could also just use thecloud
feature flag if you want, let me know)*edit: nvm, I think I prefer the new feature flag because it also specifies
"dep:blake3", "dep:fs4"