π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[rust] Keep VecDB log locks through filename changes
laurenspriem68566a6351f2
diff --git a/rust/crates/ml/src/vecdb/log.rs b/rust/crates/ml/src/vecdb/log.rsindex 55aab7dbd9..c1a2772777 100644--- a/rust/crates/ml/src/vecdb/log.rs+++ b/rust/crates/ml/src/vecdb/log.rs@@ -1,4 +1,4 @@-use std::fs::{File, OpenOptions};+use std::fs::File;use std::io::{BufReader, ErrorKind, Read, Seek, SeekFrom, Write};use std::path::{Path, PathBuf};use std::sync::atomic::{AtomicU64, Ordering};@@ -75,9 +75,9 @@ pub(crate) struct LogCheckpoint {end_offset: u64,}-pub(crate) fn writer_options() -> OpenOptions {+pub(crate) fn open_writer_file(path: &Path, create_new: bool) -> Result<File, VecDbError> {let mut options = File::options();- options.read(true).write(true);+ options.read(true).write(true).create_new(create_new);#[cfg(windows)]{use std::os::windows::fs::OpenOptionsExt;@@ -86,10 +86,23 @@ pub(crate) fn writer_options() -> OpenOptions {const FILE_SHARE_DELETE: u32 = 0x00000004;options.share_mode(FILE_SHARE_READ | FILE_SHARE_DELETE);}- options+ let file = options+ .open(path)+ .map_err(|source| writer_open_error(path, source))?;+ #[cfg(unix)]+ {+ use std::fs::TryLockError;++ match file.try_lock() {+ Ok(()) => {}+ Err(TryLockError::WouldBlock) => return Err(VecDbError::Locked(path.to_path_buf())),+ Err(TryLockError::Error(source)) => return Err(VecDbError::io(path, source)),+ }+ }+ Ok(file)}-pub(crate) fn writer_open_error(path: &Path, source: std::io::Error) -> VecDbError {+fn writer_open_error(path: &Path, source: std::io::Error) -> VecDbError {#[cfg(windows)]if source.raw_os_error() == Some(32) {return VecDbError::Locked(path.to_path_buf());@@ -104,10 +117,7 @@ impl Log {storage: StorageKind,) -> Result<Self, VecDbError> {validate_dims(dims, storage)?;- let file = writer_options()- .create_new(true)- .open(path)- .map_err(|source| writer_open_error(path, source))?;+ let file = open_writer_file(path, true)?;Self::initialize(file, path, dims, storage)}@@ -1017,7 +1027,7 @@ mod tests {}fn reopen(path: &Path, dims: usize) -> Log {- let file = writer_options().open(path).unwrap();+ let file = open_writer_file(path, false).unwrap();Log::open(file, path, dims, None).unwrap()}@@ -1126,9 +1136,9 @@ mod tests {assert_eq!(reopened.current_end_offset(), HEADER_LEN as u64);}- #[cfg(windows)]+ #[cfg(any(unix, windows))]#[test]- fn writer_sharing_blocks_aliases_until_the_log_is_closed() {+ fn writer_exclusion_lasts_until_the_file_is_closed() {let dir = TempDir::new().unwrap();let path = dir.path().join("log");let alias = dir.path().join("alias");@@ -1136,18 +1146,24 @@ mod tests {std::fs::hard_link(&path, &alias).unwrap();let reader = File::open(&alias).unwrap();assert_eq!(reader.metadata().unwrap().len(), HEADER_LEN as u64);- assert_eq!(- writer_options().open(&alias).unwrap_err().raw_os_error(),- Some(32)- );- drop(created);+ assert!(matches!(+ open_writer_file(&alias, false),+ Err(VecDbError::Locked(_))+ ));+ drop(reader);+ let file = created.into_file();+ assert!(matches!(+ open_writer_file(&alias, false),+ Err(VecDbError::Locked(_))+ ));+ drop(file);let reopened = reopen(&alias, 8);- assert_eq!(- writer_options().open(&path).unwrap_err().raw_os_error(),- Some(32)- );+ assert!(matches!(+ open_writer_file(&path, false),+ Err(VecDbError::Locked(_))+ ));drop(reopened);- assert!(writer_options().open(&path).is_ok());+ assert!(open_writer_file(&path, false).is_ok());}#[cfg(windows)]diff --git a/rust/crates/ml/src/vecdb/store.rs b/rust/crates/ml/src/vecdb/store.rsindex 05a0940a74..2ebc364e5e 100644--- a/rust/crates/ml/src/vecdb/store.rs+++ b/rust/crates/ml/src/vecdb/store.rs@@ -14,8 +14,8 @@ use super::graph::{Graph, search as graph_search, search_stored};use super::kernel::{StoredVector, VectorPayload, quantize_for};use super::lock::WriterLock;use super::log::{- HEADER_LEN, Log, LogCheckpoint, LogEntry, LogRecord, header_generation, remove_if_present,- remove_stale_temp_sibling, sync_parent_dir, writer_open_error, writer_options,+ HEADER_LEN, Log, LogCheckpoint, LogEntry, LogRecord, header_generation, open_writer_file,+ remove_if_present, remove_stale_temp_sibling, sync_parent_dir,};use super::snapshot::{LoadedSnapshot, load_snapshot, remove_snapshot, snapshot_path, write_snapshot,@@ -1190,9 +1190,7 @@ fn restore_writer_mode(expected_checkpoint: LogCheckpoint,) -> Result<(), VecDbError> {let restored = (|| {- let file = writer_options()- .open(&shared.path)- .map_err(|source| writer_open_error(&shared.path, source))?;+ let file = open_writer_file(&shared.path, false)?;if file.metadata().map_err(|source| VecDbError::io(&shared.path, source))?@@ -1664,28 +1662,7 @@ fn open_existing_writer_log(dims: usize,storage: Option<StorageKind>,) -> Result<Log, VecDbError> {- let file = writer_options()- .open(path)- .map_err(|source| writer_open_error(path, source))?;- #[cfg(unix)]- {- use std::os::unix::fs::MetadataExt;-- if file- .metadata()- .map_err(|source| VecDbError::io(path, source))?- .nlink()- > 1- {- return Err(VecDbError::io(- path,- std::io::Error::new(- ErrorKind::InvalidInput,- "hard-linked vector db files cannot be opened for writing",- ),- ));- }- }+ let file = open_writer_file(path, false)?;Log::open(file, path, dims, storage)}@@ -2511,7 +2488,10 @@ mod tests {let first = open_writer(&path);fs::hard_link(&path, &alias).unwrap();let bytes = fs::read(&path).unwrap();- assert!(VecDb::open(&alias, DIMS).is_err());+ assert!(matches!(+ VecDb::open(&alias, DIMS),+ Err(VecDbError::Locked(_))+ ));assert_eq!(fs::read(&path).unwrap(), bytes);let second = open_writer(&path);let reader = VecDb::open_read_only(&path, DIMS).unwrap();@@ -2522,7 +2502,6 @@ mod tests {drop(reader);drop(second);drop(first);- fs::remove_file(alias).unwrap();let reopened = open_writer(&path);assert!(reopened.contains("first"));assert!(reopened.contains("second"));@@ -2530,21 +2509,27 @@ mod tests {#[cfg(unix)]#[test]- fn hard_link_refusal_preserves_incomplete_logs() {+ fn writer_lock_contention_preserves_incomplete_logs() {let dir = TempDir::new().unwrap();let path = dir.path().join("db");let alias = dir.path().join("alias");let partial = b"EVDB";fs::write(&path, partial).unwrap();+ let holder = File::options().read(true).write(true).open(&path).unwrap();+ holder.try_lock().unwrap();fs::hard_link(&path, &alias).unwrap();for name in [&path, &alias] {assert!(matches!(VecDb::open(name, DIMS),- Err(VecDbError::Io { source, .. }) if source.kind() == ErrorKind::InvalidInput+ Err(VecDbError::Locked(_))));}assert_eq!(fs::read(&path).unwrap(), partial);assert!(VecDb::open_read_only(&alias, DIMS).unwrap().is_empty());+ drop(holder);+ let writer = open_writer(&alias);+ assert!(writer.is_empty());+ writer.add("first", &basis_vector(0)).unwrap();}#[cfg(any(unix, windows))]@@ -2560,10 +2545,6 @@ mod tests {{"blocked" => {let result = VecDb::open(&path, DIMS);- #[cfg(unix)]- assert!(matches!(result, Err(VecDbError::Io { source, .. })- if source.kind() == ErrorKind::InvalidInput));- #[cfg(windows)]assert!(matches!(result, Err(VecDbError::Locked(_))));let reader = VecDb::open_read_only(&path, DIMS).unwrap();assert!(reader.contains("first"));@@ -2577,6 +2558,26 @@ mod tests {}}+ #[cfg(any(unix, windows))]+ fn run_hard_link_writer_process(path: &Path, action: &str) {+ let output = std::process::Command::new(std::env::current_exe().unwrap())+ .arg("--exact")+ .arg(format!(+ "{}::hard_link_writer_process",+ module_path!().split_once("::").unwrap().1+ ))+ .arg("--nocapture")+ .env("VECDB_HARD_LINK_TEST_PATH", path)+ .env("VECDB_HARD_LINK_TEST_ACTION", action)+ .output()+ .unwrap();+ assert!(output.status.success(), "{output:?}");+ assert!(+ String::from_utf8_lossy(&output.stdout).contains("1 passed"),+ "{output:?}"+ );+ }+#[cfg(any(unix, windows))]#[test]fn hard_link_exclusion_and_writer_handoff_work_across_processes() {@@ -2586,36 +2587,59 @@ mod tests {let first = open_writer(&path);first.add("first", &basis_vector(0)).unwrap();fs::hard_link(&path, &alias).unwrap();- let run = |action: &str| {- let output = std::process::Command::new(std::env::current_exe().unwrap())- .arg("--exact")- .arg(format!(- "{}::hard_link_writer_process",- module_path!().split_once("::").unwrap().1- ))- .arg("--nocapture")- .env("VECDB_HARD_LINK_TEST_PATH", &alias)- .env("VECDB_HARD_LINK_TEST_ACTION", action)- .output()- .unwrap();- assert!(output.status.success(), "{output:?}");- assert!(- String::from_utf8_lossy(&output.stdout).contains("1 passed"),- "{output:?}"- );- };- run("blocked");+ run_hard_link_writer_process(&alias, "blocked");assert_eq!(first.len(), 1);drop(first);- #[cfg(unix)]- fs::remove_file(&path).unwrap();- run("write");+ run_hard_link_writer_process(&alias, "write");let reader = VecDb::open_read_only(&alias, DIMS).unwrap();assert_eq!(reader.len(), 2);assert!(reader.contains("first"));assert!(reader.contains("next"));}+ #[cfg(unix)]+ #[test]+ fn writer_exclusion_survives_removing_or_renaming_the_original_name() {+ use std::os::unix::fs::MetadataExt;++ for reopen in [false, true] {+ for unlink in [false, true] {+ let dir = TempDir::new().unwrap();+ let path = dir.path().join("db");+ let alias = dir.path().join("alias");+ let mut first = open_writer(&path);+ first.add("first", &basis_vector(0)).unwrap();+ if reopen {+ drop(first);+ first = open_writer(&path);+ }+ if unlink {+ fs::hard_link(&path, &alias).unwrap();+ fs::remove_file(&path).unwrap();+ } else {+ fs::rename(&path, &alias).unwrap();+ }+ assert_eq!(fs::metadata(&alias).unwrap().nlink(), 1);+ assert!(matches!(+ VecDb::open(&alias, DIMS),+ Err(VecDbError::Locked(_))+ ));+ let reader = VecDb::open_read_only(&alias, DIMS).unwrap();+ assert!(reader.contains("first"));+ drop(reader);+ run_hard_link_writer_process(&alias, "blocked");+ first.add("after", &basis_vector(2)).unwrap();+ drop(first);+ run_hard_link_writer_process(&alias, "write");+ let reopened = open_writer(&alias);+ assert_eq!(reopened.len(), 3);+ assert!(reopened.contains("first"));+ assert!(reopened.contains("after"));+ assert!(reopened.contains("next"));+ }+ }+ }+#[cfg(any(unix, windows))]#[test]fn a_late_hard_link_does_not_interrupt_writer_maintenance() {@@ -2631,8 +2655,18 @@ mod tests {assert_eq!(db.len(), 10);fs::remove_file(&alias).unwrap();fs::hard_link(&path, &alias).unwrap();+ assert!(matches!(+ VecDb::open(&alias, DIMS),+ Err(VecDbError::Locked(_))+ ));db.reset().unwrap();db.add("after", &basis_vector(0)).unwrap();+ fs::remove_file(&alias).unwrap();+ fs::hard_link(&path, &alias).unwrap();+ assert!(matches!(+ VecDb::open(&alias, DIMS),+ Err(VecDbError::Locked(_))+ ));assert_eq!(db.len(), 1);assert_eq!(observer.len(), 10);}@@ -3739,7 +3773,7 @@ mod tests {assert_own_nearest(&reopened, "other", &basis_vector(1));}- #[cfg(windows)]+ #[cfg(any(unix, windows))]#[test]fn restore_writer_mode_cannot_rejoin_an_alias_writer() {let dir = TempDir::new().unwrap();