-
-
Couldn't load subscription status.
- Fork 817
Add soft-commit and NRT overlay: IndexWriter::soft_commit, IndexWriter::nrt_searcher, and NrtDirectory #2682
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
MagellaX
wants to merge
5
commits into
quickwit-oss:main
Choose a base branch
from
MagellaX:feat/nrt-soft-commit
base: main
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
c84146e
feat(query): make empty-query behavior configurable in QueryParser (#…
MagellaX bf560ac
feat(query): decide empty-query at user input AST level; add set/get_…
MagellaX 5faf5a1
feat(nrt): add soft-commit API, IndexWriter::nrt_searcher, and NrtDir…
MagellaX 1e435c4
fix(nrt): publish meta.json only after data persisted; avoid early ba…
MagellaX ce38d1e
Merge branch 'main' into feat/nrt-soft-commit
MagellaX 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,156 @@ | ||
| use std::collections::HashSet; | ||
| use std::fmt; | ||
| use std::io::{self, Write}; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::sync::{Arc, RwLock}; | ||
|
|
||
| use crate::core::META_FILEPATH; | ||
| use crate::directory::error::{DeleteError, OpenReadError, OpenWriteError}; | ||
| use crate::directory::{Directory, DirectoryLock, FileHandle, FileSlice, Lock, TerminatingWrite, | ||
| WatchCallback, WatchHandle, WritePtr}; | ||
| use crate::directory::RamDirectory; | ||
|
|
||
| /// A Directory that overlays a `RamDirectory` on top of a base `Directory`. | ||
| /// | ||
| /// - Writes (open_write and atomic_write) go to the in-memory overlay. | ||
| /// - Reads first check the overlay, then fallback to the base directory. | ||
| /// - sync_directory() persists overlay files that do not yet exist in the base directory. | ||
| #[derive(Clone)] | ||
| pub struct NrtDirectory { | ||
| base: Box<dyn Directory>, | ||
| overlay: RamDirectory, | ||
| /// Tracks files written into the overlay to decide what to persist on sync. | ||
| overlay_paths: Arc<RwLock<HashSet<PathBuf>>>, | ||
| } | ||
|
|
||
| impl NrtDirectory { | ||
| /// Wraps a base directory with an NRT overlay. | ||
| pub fn wrap(base: Box<dyn Directory>) -> NrtDirectory { | ||
| NrtDirectory { | ||
| base, | ||
| overlay: RamDirectory::default(), | ||
| overlay_paths: Arc::new(RwLock::new(HashSet::new())), | ||
| } | ||
| } | ||
|
|
||
| /// Persist overlay files into the base directory if missing there. | ||
| fn persist_overlay_into_base(&self) -> crate::Result<()> { | ||
| let snapshot_paths: Vec<PathBuf> = { | ||
| let guard = self.overlay_paths.read().unwrap(); | ||
| guard.iter().cloned().collect() | ||
| }; | ||
| // First copy all non-meta files. `meta.json` must be written last atomically. | ||
| for path in snapshot_paths { | ||
| if path == *META_FILEPATH { | ||
| continue; | ||
| } | ||
| // Skip if base already has the file | ||
| if self.base.exists(&path).unwrap_or(false) { | ||
| continue; | ||
| } | ||
| // Read bytes from overlay | ||
| let file_slice: FileSlice = match self.overlay.open_read(&path) { | ||
| Ok(slice) => slice, | ||
| Err(OpenReadError::FileDoesNotExist(_)) => continue, // was removed meanwhile | ||
| Err(e) => return Err(e.into()), | ||
| }; | ||
| let bytes = file_slice | ||
| .read_bytes() | ||
| .map_err(|io_err| OpenReadError::IoError { | ||
| io_error: Arc::new(io_err), | ||
| filepath: path.clone(), | ||
| })?; | ||
| // Write to base | ||
| let mut dest_wrt: WritePtr = self.base.open_write(&path)?; | ||
| dest_wrt.write_all(bytes.as_slice())?; | ||
| dest_wrt.terminate()?; | ||
| } | ||
| // Then, if present, write `meta.json` atomically to the base directory. | ||
| if self.overlay.exists(&*META_FILEPATH).unwrap_or(false) { | ||
| // Read meta from overlay atomically to a buffer and then write to base atomically. | ||
| if let Ok(meta_bytes) = self.overlay.atomic_read(&*META_FILEPATH) { | ||
| self.base.atomic_write(&*META_FILEPATH, &meta_bytes)?; | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Debug for NrtDirectory { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "NrtDirectory") | ||
| } | ||
| } | ||
|
|
||
| impl Directory for NrtDirectory { | ||
| fn get_file_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>, OpenReadError> { | ||
| if self.overlay.exists(path).unwrap_or(false) { | ||
| return self.overlay.get_file_handle(path); | ||
| } | ||
| self.base.get_file_handle(path) | ||
| } | ||
|
|
||
| fn open_read(&self, path: &Path) -> Result<FileSlice, OpenReadError> { | ||
| if self.overlay.exists(path).unwrap_or(false) { | ||
| return self.overlay.open_read(path); | ||
| } | ||
| self.base.open_read(path) | ||
| } | ||
|
|
||
| fn delete(&self, path: &Path) -> Result<(), DeleteError> { | ||
| let _ = self.overlay.delete(path); // best-effort | ||
| self.base.delete(path) | ||
| } | ||
|
|
||
| fn exists(&self, path: &Path) -> Result<bool, OpenReadError> { | ||
| if self.overlay.exists(path).unwrap_or(false) { | ||
| return Ok(true); | ||
| } | ||
| self.base.exists(path) | ||
| } | ||
|
|
||
| fn open_write(&self, path: &Path) -> Result<WritePtr, OpenWriteError> { | ||
| { | ||
| let mut guard = self.overlay_paths.write().unwrap(); | ||
| guard.insert(path.to_path_buf()); | ||
| } | ||
| self.overlay.open_write(path) | ||
| } | ||
|
|
||
| fn atomic_read(&self, path: &Path) -> Result<Vec<u8>, OpenReadError> { | ||
| if self.overlay.exists(path).unwrap_or(false) { | ||
| return self.overlay.atomic_read(path); | ||
| } | ||
| self.base.atomic_read(path) | ||
| } | ||
|
|
||
| fn atomic_write(&self, path: &Path, data: &[u8]) -> io::Result<()> { | ||
| { | ||
| let mut guard = self.overlay_paths.write().unwrap(); | ||
| guard.insert(path.to_path_buf()); | ||
| } | ||
| // Always write to the overlay first. We do not write meta.json to base here, | ||
| // to ensure meta is published only after all files are persisted in sync_directory(). | ||
| self.overlay.atomic_write(path, data)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn acquire_lock(&self, lock: &Lock) -> Result<DirectoryLock, crate::directory::error::LockError> { | ||
| self.base.acquire_lock(lock) | ||
| } | ||
|
|
||
| fn watch(&self, watch_callback: WatchCallback) -> crate::Result<WatchHandle> { | ||
| // Watch meta.json changes on the base directory | ||
| self.base.watch(watch_callback) | ||
| } | ||
|
|
||
| fn sync_directory(&self) -> io::Result<()> { | ||
| // Best effort: persist overlay, then sync base directory | ||
| if let Err(err) = self.persist_overlay_into_base() { | ||
| return Err(io::Error::new(io::ErrorKind::Other, format!("{err}"))); | ||
| } | ||
| self.base.sync_directory() | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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
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.
ok, but if we update the meta json file before a segment file, and this function has an io Error in the middle, the index will be corrupted right now?
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’ve moved the meta publication to the end of sync_directory() and removed the early base write in atomic_write. We now snapshot overlay paths, persist all data files first, and atomically write meta.json last to avoid partial states. Build is green; please have another look.