Add directory-level locking and introduce layered index cache
Introduces directory-level locking to prevent concurrent index operations from corrupting shared directories, along with explicit APIs for acquiring, probing, and releasing locks. Restructures the index cache crate to use a layered store architecture that eagerly initializes metadata and provides fast hierarchical lookups. Updates dependent modules, test suites, and CLI commands to align with the refactored API surface, and adds an end-to-end smoke test for validation.
This commit is contained in:
1 parent
2419a6c21d
commit
7183e3adb4
15 files changed
+509
-77
No files matched your search
+23
-8
@@ -23,18 +23,33 @@ impl DirLock {
|
||||
/// and the lock file within it if needed). Logs once if the wait is
|
||||
/// non-trivial, so a blocked command doesn't look silently hung.
|
||||
pub fn acquire(dir: &std::path::Path) -> std::io::Result<Self> {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
let lock_path = dir.join(".obikmer.lock");
|
||||
let file = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(false)
|
||||
.write(true)
|
||||
.open(&lock_path)?;
|
||||
|
||||
let file = Self::open_lock_file(dir)?;
|
||||
if file.try_lock().is_err() {
|
||||
info!(dir = %dir.display(), "waiting for another obikmer process to release this index");
|
||||
file.lock()?;
|
||||
}
|
||||
Ok(Self { _file: file })
|
||||
}
|
||||
|
||||
/// Non-blocking probe: `Ok(Some(lock))` if `dir`'s lock was free and is
|
||||
/// now held by the returned `DirLock`, `Ok(None)` if another process
|
||||
/// currently holds it (not an error — contention is the expected,
|
||||
/// common case this exists to distinguish from a genuine I/O failure).
|
||||
pub fn try_acquire(dir: &std::path::Path) -> std::io::Result<Option<Self>> {
|
||||
let file = Self::open_lock_file(dir)?;
|
||||
match file.try_lock() {
|
||||
Ok(()) => Ok(Some(Self { _file: file })),
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn open_lock_file(dir: &std::path::Path) -> std::io::Result<std::fs::File> {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
let lock_path = dir.join(".obikmer.lock");
|
||||
std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(false)
|
||||
.write(true)
|
||||
.open(&lock_path)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user