diff --git a/seaweed-worker/crates/lance/src/drop_tests.rs b/seaweed-worker/crates/lance/src/drop_tests.rs new file mode 100644 index 0000000000..fd6ef9e208 --- /dev/null +++ b/seaweed-worker/crates/lance/src/drop_tests.rs @@ -0,0 +1,269 @@ +//! A table dropped while a worker job runs on it: the job must fail without +//! writing into the dropped path, and a table created again there starts +//! empty. + +use std::fmt; +use std::ops::Range; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +use bytes::Bytes; +use futures::stream::BoxStream; +use lance::Dataset; +use lance::io::WrappingObjectStore; +use object_store::path::Path; +use object_store::{ + CopyOptions, GetOptions, GetResult, ListResult, MultipartUpload, ObjectMeta, ObjectStore, + PutMultipartOptions, PutOptions, PutPayload, PutResult, RenameOptions, Result, +}; + +use crate::metadata_index::tests::lifecycle::{table, upsert}; +use crate::metadata_index::{self, State}; + +/// Removes the table directory just before the first write through it, the +/// way DeleteIndex purging the table between a job's open and its writes +/// would. +#[derive(Debug)] +struct DropOnFirstWrite { + dir: PathBuf, + fired: Arc, +} + +impl WrappingObjectStore for DropOnFirstWrite { + fn wrap(&self, _prefix: &str, original: Arc) -> Arc { + Arc::new(Dropping { + inner: original, + dir: self.dir.clone(), + fired: self.fired.clone(), + }) + } +} + +#[derive(Debug)] +struct Dropping { + inner: Arc, + dir: PathBuf, + fired: Arc, +} + +impl Dropping { + fn drop_once(&self) { + if !self.fired.swap(true, Ordering::SeqCst) { + std::fs::remove_dir_all(&self.dir).unwrap(); + } + } +} + +impl fmt::Display for Dropping { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Dropping({})", self.inner) + } +} + +#[async_trait::async_trait] +impl ObjectStore for Dropping { + async fn put_opts( + &self, + location: &Path, + payload: PutPayload, + opts: PutOptions, + ) -> Result { + self.drop_once(); + self.inner.put_opts(location, payload, opts).await + } + async fn put_multipart_opts( + &self, + location: &Path, + opts: PutMultipartOptions, + ) -> Result> { + self.drop_once(); + self.inner.put_multipart_opts(location, opts).await + } + async fn get_opts(&self, location: &Path, options: GetOptions) -> Result { + self.inner.get_opts(location, options).await + } + async fn get_ranges(&self, location: &Path, ranges: &[Range]) -> Result> { + self.inner.get_ranges(location, ranges).await + } + fn delete_stream( + &self, + locations: BoxStream<'static, Result>, + ) -> BoxStream<'static, Result> { + self.inner.delete_stream(locations) + } + fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, Result> { + self.inner.list(prefix) + } + fn list_with_offset( + &self, + prefix: Option<&Path>, + offset: &Path, + ) -> BoxStream<'static, Result> { + self.inner.list_with_offset(prefix, offset) + } + async fn list_with_delimiter(&self, prefix: Option<&Path>) -> Result { + self.inner.list_with_delimiter(prefix).await + } + async fn copy_opts(&self, from: &Path, to: &Path, options: CopyOptions) -> Result<()> { + self.inner.copy_opts(from, to, options).await + } + async fn rename_opts(&self, from: &Path, to: &Path, options: RenameOptions) -> Result<()> { + self.inner.rename_opts(from, to, options).await + } +} + +/// Opens the table the way a job does; `during` drops it at the job's first +/// write, otherwise it is dropped right after the open. +async fn open_then_drop(dir: &tempfile::TempDir, during: bool) -> Dataset { + let description = crate::catalog::TableDescription { + location: dir.path().to_str().unwrap().to_string(), + storage_options: Default::default(), + managed_versioning: false, + }; + let dropping: Option> = during.then(|| { + Arc::new(DropOnFirstWrite { + dir: dir.path().to_path_buf(), + fired: Arc::new(AtomicBool::new(false)), + }) as Arc + }); + let dataset = crate::dataset::open_at(&description, dropping, None) + .await + .unwrap(); + if !during { + std::fs::remove_dir_all(dir.path()).unwrap(); + } + dataset +} + +fn files(dir: &std::path::Path) -> Vec { + let mut out = Vec::new(); + if let Ok(entries) = std::fs::read_dir(dir) { + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + out.extend(files(&path)); + } else { + out.push(path.display().to_string()); + } + } + } + out +} + +/// Runs `job` on a table dropped right after the job opens it, or at the +/// job's first write. The job must fail, leave no manifest (nor, dropped +/// before it wrote, any file) in the dropped path, and a table created +/// there again must start empty. +async fn dropped_during( + label: &str, + during: bool, + prepare: impl AsyncFnOnce(&mut Dataset), + job: F, +) where + F: FnOnce(Dataset) -> Fut, + Fut: std::future::Future>, +{ + let dir = tempfile::TempDir::new().unwrap(); + let mut dataset = table(&dir, 4).await; + prepare(&mut dataset).await; + let result = job(open_then_drop(&dir, during).await).await; + let left = files(dir.path()); + let error = result.expect_err(&format!( + "{label}: a job on a dropped table succeeded, leaving {left:?}" + )); + eprintln!( + "{label} (dropped {}): {error:#}", + if during { + "at its first write" + } else { + "after the open" + } + ); + assert!( + !left.iter().any(|f| f.contains("_versions")), + "{label}: a manifest was written into the dropped table: {left:?}" + ); + if !during { + assert!( + left.is_empty(), + "{label}: files were written into the dropped table: {left:?}" + ); + } + let recreated = Dataset::write( + arrow_array::RecordBatchIterator::new( + Vec::>::new(), + Arc::new(crate::metadata_index::tests::lifecycle::base_schema()), + ), + dir.path().to_str().unwrap(), + None, + ) + .await + .unwrap_or_else(|e| panic!("{label}: the table could not be created again: {e}")); + assert_eq!(recreated.version().version, 1, "{label}"); + assert_eq!(recreated.count_rows(None).await.unwrap(), 0, "{label}"); +} + +/// Every job, with the table dropped after its open and at its first write. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_job_on_a_dropped_table_fails_and_leaves_it_dropped() { + for during in [false, true] { + dropped_during( + "vector index build", + during, + async |_| {}, + async |mut d| { + crate::vector_index::build( + &mut d, + "vector", + crate::vector_index::INDEX_NAME, + 4, + false, + ) + .await + }, + ) + .await; + dropped_during( + "metadata key add", + during, + async |d| metadata_index::request(d, "author").await.unwrap(), + async |mut d| { + let key = metadata_index::add_column(&mut d, "author").await?; + metadata_index::finish(&mut d, &key, 1_000) + .await + .map(|_| ()) + }, + ) + .await; + dropped_during( + "metadata key maintenance", + during, + async |d| { + let key = metadata_index::add_column(d, "author").await.unwrap(); + metadata_index::finish(d, &key, 1_000).await.unwrap(); + *d = upsert(d.clone(), 4_000..4_500).await; + }, + async |mut d| { + let key = metadata_index::keys_of(&d).remove(0); + metadata_index::maintain(&mut d, &key, 1_000, 0) + .await + .map(|_| ()) + }, + ) + .await; + dropped_during( + "metadata key remove", + during, + async |d| { + let key = metadata_index::add_column(d, "author").await.unwrap(); + metadata_index::finish(d, &key, 1_000).await.unwrap(); + metadata_index::set_state(d, "_mk_0", State::Off) + .await + .unwrap(); + }, + async |mut d| metadata_index::remove_column(&mut d, "_mk_0").await, + ) + .await; + } +} diff --git a/seaweed-worker/crates/lance/src/lib.rs b/seaweed-worker/crates/lance/src/lib.rs index 52fef49799..fe868e18f2 100644 --- a/seaweed-worker/crates/lance/src/lib.rs +++ b/seaweed-worker/crates/lance/src/lib.rs @@ -35,3 +35,5 @@ pub mod volume_server_pb { pub mod remote_pb { tonic::include_proto!("remote_pb"); } +#[cfg(test)] +mod drop_tests;