-
-
Notifications
You must be signed in to change notification settings - Fork 709
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
Fix building on windows with mmap #2070
Merged
PSeitz
merged 10 commits into
quickwit-oss:main
from
ChillFish8:chillfish8-fix-windows-build
Jun 10, 2023
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
361f9c4
Fix windows build
ChillFish8 e784bc4
Make pub
ChillFish8 413300c
Update docs
ChillFish8 56302c5
Re arrange
ChillFish8 5948f30
Fix compilation error on unix
ChillFish8 c1d94fd
Fix unix borrows
ChillFish8 6bf98bd
Revert "Fix unix borrows"
ChillFish8 5deabf5
Fix unix borrows and revert original change
ChillFish8 156d12b
Fix warning
ChillFish8 21ce6e4
Cleaner code.
fulmicoton 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 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 |
---|---|---|
|
@@ -21,6 +21,7 @@ use crate::directory::{ | |
AntiCallToken, Directory, DirectoryLock, FileHandle, Lock, OwnedBytes, TerminatingWrite, | ||
WatchCallback, WatchHandle, WritePtr, | ||
}; | ||
#[cfg(unix)] | ||
use crate::Advice; | ||
|
||
pub type ArcBytes = Arc<dyn Deref<Target = [u8]> + Send + Sync + 'static>; | ||
|
@@ -31,12 +32,26 @@ pub(crate) fn make_io_err(msg: String) -> io::Error { | |
io::Error::new(io::ErrorKind::Other, msg) | ||
} | ||
|
||
#[cfg(unix)] | ||
/// Returns `None` iff the file exists, can be read, but is empty (and hence | ||
/// cannot be mmapped) | ||
fn open_mmap( | ||
/// cannot be mmapped), this method also allows for optional [Advice] to be | ||
/// specified for the given workload. | ||
fn open_mmap_with_advice( | ||
full_path: &Path, | ||
madvice_opt: Option<Advice>, | ||
) -> result::Result<Option<Mmap>, OpenReadError> { | ||
let mmap_opt = open_mmap(full_path)?; | ||
|
||
if let (Some(mmap), Some(madvice)) = (&mmap_opt, madvice_opt) { | ||
let _ = mmap.advise(madvice); | ||
} | ||
|
||
Ok(mmap_opt) | ||
} | ||
|
||
/// Returns `None` iff the file exists, can be read, but is empty (and hence | ||
/// cannot be mmapped) | ||
fn open_mmap(full_path: &Path) -> result::Result<Option<Mmap>, OpenReadError> { | ||
let file = File::open(full_path).map_err(|io_err| { | ||
if io_err.kind() == io::ErrorKind::NotFound { | ||
OpenReadError::FileDoesNotExist(full_path.to_path_buf()) | ||
|
@@ -59,9 +74,7 @@ fn open_mmap( | |
.map(Some) | ||
.map_err(|io_err| OpenReadError::wrap_io_error(io_err, full_path.to_path_buf())) | ||
}?; | ||
if let (Some(mmap), Some(madvice)) = (&mmap_opt, madvice_opt) { | ||
let _ = mmap.advise(madvice); | ||
} | ||
|
||
Ok(mmap_opt) | ||
} | ||
|
||
|
@@ -83,18 +96,25 @@ pub struct CacheInfo { | |
struct MmapCache { | ||
counters: CacheCounters, | ||
cache: HashMap<PathBuf, WeakArcBytes>, | ||
#[cfg(unix)] | ||
madvice_opt: Option<Advice>, | ||
} | ||
|
||
impl MmapCache { | ||
fn new(madvice_opt: Option<Advice>) -> MmapCache { | ||
fn new() -> MmapCache { | ||
MmapCache { | ||
counters: CacheCounters::default(), | ||
cache: HashMap::default(), | ||
madvice_opt, | ||
#[cfg(unix)] | ||
madvice_opt: None, | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
fn with_advice(&mut self, madvice_opt: Option<Advice>) { | ||
self.madvice_opt = madvice_opt | ||
} | ||
|
||
fn get_info(&self) -> CacheInfo { | ||
let paths: Vec<PathBuf> = self.cache.keys().cloned().collect(); | ||
CacheInfo { | ||
|
@@ -125,7 +145,10 @@ impl MmapCache { | |
} | ||
self.cache.remove(full_path); | ||
self.counters.miss += 1; | ||
let mmap_opt = open_mmap(full_path, self.madvice_opt)?; | ||
#[cfg(unix)] | ||
let mmap_opt = open_mmap_with_advice(full_path, self.madvice_opt)?; | ||
#[cfg(not(unix))] | ||
let mmap_opt = open_mmap(full_path)?; | ||
Ok(mmap_opt.map(|mmap| { | ||
let mmap_arc: ArcBytes = Arc::new(mmap); | ||
let mmap_weak = Arc::downgrade(&mmap_arc); | ||
|
@@ -160,19 +183,20 @@ struct MmapDirectoryInner { | |
} | ||
|
||
impl MmapDirectoryInner { | ||
fn new( | ||
root_path: PathBuf, | ||
temp_directory: Option<TempDir>, | ||
madvice_opt: Option<Advice>, | ||
) -> MmapDirectoryInner { | ||
fn new(root_path: PathBuf, temp_directory: Option<TempDir>) -> MmapDirectoryInner { | ||
MmapDirectoryInner { | ||
mmap_cache: RwLock::new(MmapCache::new(madvice_opt)), | ||
mmap_cache: RwLock::new(MmapCache::new()), | ||
_temp_directory: temp_directory, | ||
watcher: FileWatcher::new(&root_path.join(*META_FILEPATH)), | ||
root_path, | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
fn with_advice(&self, madvice_opt: Option<Advice>) { | ||
self.mmap_cache.write().unwrap().with_advice(madvice_opt) | ||
} | ||
|
||
fn watch(&self, callback: WatchCallback) -> WatchHandle { | ||
self.watcher.watch(callback) | ||
} | ||
|
@@ -185,17 +209,21 @@ impl fmt::Debug for MmapDirectory { | |
} | ||
|
||
impl MmapDirectory { | ||
fn new( | ||
root_path: PathBuf, | ||
temp_directory: Option<TempDir>, | ||
madvice_opt: Option<Advice>, | ||
) -> MmapDirectory { | ||
let inner = MmapDirectoryInner::new(root_path, temp_directory, madvice_opt); | ||
fn new(root_path: PathBuf, temp_directory: Option<TempDir>) -> MmapDirectory { | ||
let inner = MmapDirectoryInner::new(root_path, temp_directory); | ||
MmapDirectory { | ||
inner: Arc::new(inner), | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
/// Pass specific madvice flags when opening a new mmap file. | ||
/// | ||
/// This is only supported on unix platforms. | ||
pub fn with_advice(&self, madvice_opt: Option<Advice>) { | ||
self.inner.with_advice(madvice_opt) | ||
} | ||
|
||
/// Creates a new MmapDirectory in a temporary directory. | ||
/// | ||
/// This is mostly useful to test the MmapDirectory itself. | ||
|
@@ -206,7 +234,6 @@ impl MmapDirectory { | |
Ok(MmapDirectory::new( | ||
tempdir.path().to_path_buf(), | ||
Some(tempdir), | ||
None, | ||
)) | ||
} | ||
|
||
|
@@ -215,20 +242,24 @@ impl MmapDirectory { | |
/// Returns an error if the `directory_path` does not | ||
/// exist or if it is not a directory. | ||
pub fn open<P: AsRef<Path>>(directory_path: P) -> Result<MmapDirectory, OpenDirectoryError> { | ||
Self::open_with_access_pattern_impl(directory_path.as_ref(), None) | ||
Self::open_with_access_pattern_impl(directory_path.as_ref()) | ||
} | ||
|
||
#[cfg(unix)] | ||
/// Opens a MmapDirectory in a directory, with a given access pattern. | ||
/// | ||
/// This is only supported on unix platforms. | ||
pub fn open_with_madvice<P: AsRef<Path>>( | ||
directory_path: P, | ||
madvice: Advice, | ||
) -> Result<MmapDirectory, OpenDirectoryError> { | ||
Self::open_with_access_pattern_impl(directory_path.as_ref(), Some(madvice)) | ||
let dir = Self::open_with_access_pattern_impl(directory_path.as_ref())?; | ||
dir.with_advice(Some(madvice)); | ||
Ok(dir) | ||
} | ||
|
||
fn open_with_access_pattern_impl( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can keep the helper function to avoid the monomorphization on |
||
directory_path: &Path, | ||
madvice_opt: Option<Advice>, | ||
) -> Result<MmapDirectory, OpenDirectoryError> { | ||
if !directory_path.exists() { | ||
return Err(OpenDirectoryError::DoesNotExist(PathBuf::from( | ||
|
@@ -256,7 +287,7 @@ impl MmapDirectory { | |
directory_path, | ||
))); | ||
} | ||
Ok(MmapDirectory::new(canonical_path, None, madvice_opt)) | ||
Ok(MmapDirectory::new(canonical_path, None)) | ||
} | ||
|
||
/// Joins a relative_path to the directory `root_path` | ||
|
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
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.
Shouldn't this be non optional?
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.
It could be however, I did not want to make this a breaking change so chose to leave it as optional.
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.
@PSeitz: Not necessarily. We still need one way or another to handle absence of Advice.
@ChillFish8 : No this is not public API.
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.
There's still
open_mmap
for an API without AdviceThere 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 thought it was marked as not unix.