mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
vector: a key goes ready only after writes that raced its add are done
A PutVectors that opened the table before a key's column add and committed after it wrote its rows' key column as of the version it read: NULL for new rows, the old value for updated ones. It derives them again itself, but when that failed, or the write was still running when the build finished, the key went ready with those rows wrong, and maintenance only filled NULLs. PutVectors now commits within WRITE_DEADLINE (120s) of opening the table or fails. finish waits until RACE_GRACE (180s) after the add, then derives again the fragments written since it first looked, comparing values, not only NULLs, before it marks the key ready. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
This commit is contained in:
1 parent
982a114db4
commit
854a49ccc3
5 files changed
+171
-36
No files matched your search
@@ -671,7 +671,7 @@ async fn keys_are_requested_and_listed_through_the_engine() {
|
||||
let key = metadata_index::add_column(&mut dataset, "author")
|
||||
.await
|
||||
.unwrap();
|
||||
metadata_index::finish(&mut dataset, &key, 1_000_000)
|
||||
metadata_index::finish_after(&mut dataset, &key, 1_000_000, std::time::Duration::ZERO)
|
||||
.await
|
||||
.unwrap();
|
||||
let listed = fixture
|
||||
|
||||
@@ -985,13 +985,34 @@ impl VectorService for VectorServer {
|
||||
let table = req
|
||||
.table
|
||||
.ok_or_else(|| Status::invalid_argument("missing table"))?;
|
||||
let dataset = self.open(&table).await?;
|
||||
if req.vectors.is_empty() {
|
||||
return Ok(Response::new(pb::PutVectorsResponse {
|
||||
version: dataset.version().version,
|
||||
}));
|
||||
}
|
||||
let dataset = write_vectors(dataset, &req.vectors).await?;
|
||||
// A write commits within its deadline of opening the table or not
|
||||
// at all: a key's column add waits longer than that before the key
|
||||
// is ready, so a write that opened before the add has committed,
|
||||
// and been derived again, before any query uses the key.
|
||||
let merged = tokio::time::timeout(metadata_index::WRITE_DEADLINE, async {
|
||||
let dataset = self.open(&table).await?;
|
||||
if req.vectors.is_empty() {
|
||||
return Ok::<_, Status>((dataset, None));
|
||||
}
|
||||
let written = Arc::new(Into::<arrow_schema::Schema>::into(dataset.schema()));
|
||||
Ok((merge_vectors(dataset, &req.vectors).await?, Some(written)))
|
||||
})
|
||||
.await
|
||||
.map_err(|_| {
|
||||
Status::deadline_exceeded(format!(
|
||||
"PutVectors took longer than {:?} to commit",
|
||||
metadata_index::WRITE_DEADLINE
|
||||
))
|
||||
})??;
|
||||
let dataset = match merged {
|
||||
(dataset, Some(written)) => {
|
||||
let keys: Vec<String> = req.vectors.iter().map(|v| v.key.clone()).collect();
|
||||
heal_columns(dataset, &written, &keys)
|
||||
.await
|
||||
.map_err(internal)?
|
||||
}
|
||||
(dataset, None) => dataset,
|
||||
};
|
||||
Ok(Response::new(pb::PutVectorsResponse {
|
||||
version: dataset.version().version,
|
||||
}))
|
||||
|
||||
@@ -230,7 +230,7 @@ async fn a_job_on_a_dropped_table_fails_and_leaves_it_dropped() {
|
||||
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)
|
||||
metadata_index::finish_after(&mut d, &key, 1_000, std::time::Duration::ZERO)
|
||||
.await
|
||||
.map(|_| ())
|
||||
},
|
||||
@@ -241,7 +241,9 @@ async fn a_job_on_a_dropped_table_fails_and_leaves_it_dropped() {
|
||||
during,
|
||||
async |d| {
|
||||
let key = metadata_index::add_column(d, "author").await.unwrap();
|
||||
metadata_index::finish(d, &key, 1_000).await.unwrap();
|
||||
metadata_index::finish_after(d, &key, 1_000, std::time::Duration::ZERO)
|
||||
.await
|
||||
.unwrap();
|
||||
*d = upsert(d.clone(), 4_000..4_500).await;
|
||||
},
|
||||
async |mut d| {
|
||||
@@ -257,7 +259,9 @@ async fn a_job_on_a_dropped_table_fails_and_leaves_it_dropped() {
|
||||
during,
|
||||
async |d| {
|
||||
let key = metadata_index::add_column(d, "author").await.unwrap();
|
||||
metadata_index::finish(d, &key, 1_000).await.unwrap();
|
||||
metadata_index::finish_after(d, &key, 1_000, std::time::Duration::ZERO)
|
||||
.await
|
||||
.unwrap();
|
||||
metadata_index::set_state(d, "_mk_0", State::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -124,6 +124,7 @@ pub async fn run(
|
||||
action: Action,
|
||||
segment_rows: u64,
|
||||
min_unindexed_rows: u64,
|
||||
grace: std::time::Duration,
|
||||
) -> Result<String> {
|
||||
let column = metadata_index::keys_of(dataset)
|
||||
.into_iter()
|
||||
@@ -140,7 +141,7 @@ pub async fn run(
|
||||
added.column
|
||||
));
|
||||
}
|
||||
let filled = metadata_index::finish(dataset, &added, segment_rows).await?;
|
||||
let filled = metadata_index::finish_after(dataset, &added, segment_rows, grace).await?;
|
||||
Ok(format!(
|
||||
"{key} indexed in {} ({filled} rows a racing write left derived again)",
|
||||
added.column
|
||||
@@ -156,7 +157,8 @@ pub async fn run(
|
||||
state.map_or("unknown", State::as_str)
|
||||
));
|
||||
}
|
||||
let filled = metadata_index::finish(dataset, &column, segment_rows).await?;
|
||||
let filled =
|
||||
metadata_index::finish_after(dataset, &column, segment_rows, grace).await?;
|
||||
Ok(format!(
|
||||
"{key} indexed in {} ({filled} rows a racing write left derived again)",
|
||||
column.column
|
||||
@@ -412,6 +414,7 @@ impl JobHandler for MetadataIndexHandler {
|
||||
action,
|
||||
segment_rows,
|
||||
min_unindexed_rows,
|
||||
metadata_index::RACE_GRACE,
|
||||
)
|
||||
.await?;
|
||||
sender.send_completed(JobCompleted {
|
||||
@@ -509,13 +512,27 @@ mod tests {
|
||||
metadata_index::request_removal(&mut dataset, "genre")
|
||||
.await
|
||||
.unwrap();
|
||||
let skipped = run(&mut dataset, "genre", Action::Add, 1_000, 0)
|
||||
.await
|
||||
.unwrap();
|
||||
let skipped = run(
|
||||
&mut dataset,
|
||||
"genre",
|
||||
Action::Add,
|
||||
1_000,
|
||||
0,
|
||||
std::time::Duration::ZERO,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(skipped, "genre is no longer requested");
|
||||
run(&mut dataset, "author", Action::Add, 1_000, 0)
|
||||
.await
|
||||
.unwrap();
|
||||
run(
|
||||
&mut dataset,
|
||||
"author",
|
||||
Action::Add,
|
||||
1_000,
|
||||
0,
|
||||
std::time::Duration::ZERO,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let keys = metadata_index::keys_of(&dataset);
|
||||
assert_eq!(keys.len(), 1);
|
||||
assert_eq!(keys[0].state, Some(State::Ready));
|
||||
@@ -534,9 +551,16 @@ mod tests {
|
||||
pending(&dataset, 500).await.unwrap(),
|
||||
[("author".to_string(), Action::Maintain)]
|
||||
);
|
||||
run(&mut dataset, "author", Action::Maintain, 1_000, 500)
|
||||
.await
|
||||
.unwrap();
|
||||
run(
|
||||
&mut dataset,
|
||||
"author",
|
||||
Action::Maintain,
|
||||
1_000,
|
||||
500,
|
||||
std::time::Duration::ZERO,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(pending(&dataset, 0).await.unwrap().is_empty());
|
||||
|
||||
// A raced write that left NULL rows is maintained at once.
|
||||
@@ -548,13 +572,27 @@ mod tests {
|
||||
pending(&dataset, 1_000_000).await.unwrap(),
|
||||
[("author".to_string(), Action::Maintain)]
|
||||
);
|
||||
run(&mut dataset, "author", Action::Maintain, 1_000, 1_000_000)
|
||||
.await
|
||||
.unwrap();
|
||||
run(
|
||||
&mut dataset,
|
||||
"author",
|
||||
Action::Maintain,
|
||||
1_000,
|
||||
1_000_000,
|
||||
std::time::Duration::ZERO,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(pending(&dataset, 1_000_000).await.unwrap().is_empty());
|
||||
let noop = run(&mut dataset, "author", Action::Remove, 1_000, 0)
|
||||
.await
|
||||
.unwrap();
|
||||
let noop = run(
|
||||
&mut dataset,
|
||||
"author",
|
||||
Action::Remove,
|
||||
1_000,
|
||||
0,
|
||||
std::time::Duration::ZERO,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(noop, "author needs no remove: ready");
|
||||
|
||||
metadata_index::request_removal(&mut dataset, "author")
|
||||
@@ -564,9 +602,16 @@ mod tests {
|
||||
pending(&dataset, 0).await.unwrap(),
|
||||
[("author".to_string(), Action::Remove)]
|
||||
);
|
||||
run(&mut dataset, "author", Action::Remove, 1_000, 0)
|
||||
.await
|
||||
.unwrap();
|
||||
run(
|
||||
&mut dataset,
|
||||
"author",
|
||||
Action::Remove,
|
||||
1_000,
|
||||
0,
|
||||
std::time::Duration::ZERO,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(metadata_index::keys_of(&dataset).is_empty());
|
||||
assert!(pending(&dataset, 0).await.unwrap().is_empty());
|
||||
}
|
||||
|
||||
@@ -36,6 +36,13 @@ pub const STATE_FIELD_META: &str = "weed:metadata_key_state";
|
||||
pub const MAX_KEYS: usize = 8;
|
||||
/// Field metadata naming the version a key's column add read.
|
||||
pub const READ_VERSION_FIELD_META: &str = "weed:metadata_key_read_version";
|
||||
/// Longest an engine's PutVectors may take from opening the table to
|
||||
/// committing; a write that would take longer fails instead.
|
||||
pub const WRITE_DEADLINE: std::time::Duration = std::time::Duration::from_secs(120);
|
||||
/// How long after a key's column add [`finish`] waits before marking the
|
||||
/// key ready: past [`WRITE_DEADLINE`], so every write that opened the table
|
||||
/// before the add has committed, and its rows are derived again, first.
|
||||
pub const RACE_GRACE: std::time::Duration = std::time::Duration::from_secs(180);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum State {
|
||||
@@ -698,13 +705,33 @@ pub async fn index_fragments(
|
||||
|
||||
/// Completes adding `key`: derives its column again where a write that
|
||||
/// raced the add left it NULL or stale and did not fix it itself, builds
|
||||
/// the first segments, checks the fragments written meanwhile, and marks
|
||||
/// the key ready.
|
||||
/// the first segments, waits until [`RACE_GRACE`] after the add, checks the
|
||||
/// fragments written meanwhile, and marks the key ready.
|
||||
pub async fn finish(dataset: &mut Dataset, key: &IndexedKey, segment_rows: u64) -> Result<u64> {
|
||||
finish_after(dataset, key, segment_rows, RACE_GRACE).await
|
||||
}
|
||||
|
||||
/// [`finish`] with the wait after the add given.
|
||||
pub async fn finish_after(
|
||||
dataset: &mut Dataset,
|
||||
key: &IndexedKey,
|
||||
segment_rows: u64,
|
||||
grace: std::time::Duration,
|
||||
) -> Result<u64> {
|
||||
let columns = std::slice::from_ref(key);
|
||||
let added_at = added_version(dataset, &key.column)
|
||||
.await?
|
||||
.map(|(at, _)| at.version().timestamp);
|
||||
let checked = dataset.fragments().iter().map(|f| f.id).max();
|
||||
let mut fixed = rederive(dataset, columns, None).await?;
|
||||
index_fragments(dataset, &key.column, segment_rows, 0).await?;
|
||||
if let Some(added_at) = added_at {
|
||||
let until = added_at + chrono::Duration::from_std(grace)?;
|
||||
if let Ok(wait) = (until - chrono::Utc::now()).to_std() {
|
||||
tokio::time::sleep(wait).await;
|
||||
}
|
||||
}
|
||||
dataset.checkout_latest().await?;
|
||||
let since: Vec<u64> = dataset
|
||||
.fragments()
|
||||
.iter()
|
||||
@@ -1073,7 +1100,9 @@ pub(crate) mod tests {
|
||||
let mut dataset = table(&dir, 2).await;
|
||||
request(&mut dataset, "author").await.unwrap();
|
||||
let author = add_column(&mut dataset, "author").await.unwrap();
|
||||
finish(&mut dataset, &author, 1_000).await.unwrap();
|
||||
finish_after(&mut dataset, &author, 1_000, std::time::Duration::ZERO)
|
||||
.await
|
||||
.unwrap();
|
||||
request(&mut dataset, "n").await.unwrap();
|
||||
request(&mut dataset, "genre").await.unwrap();
|
||||
let mut adding = dataset.clone();
|
||||
@@ -1133,7 +1162,9 @@ pub(crate) mod tests {
|
||||
.unwrap();
|
||||
dataset.checkout_latest().await.unwrap();
|
||||
|
||||
let fixed = finish(&mut dataset, &key, 4_000).await.unwrap();
|
||||
let fixed = finish_after(&mut dataset, &key, 4_000, std::time::Duration::ZERO)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(fixed as usize >= raced_nulls + 500 - stale_moved, "{fixed}");
|
||||
assert_eq!(
|
||||
dataset
|
||||
@@ -1257,7 +1288,9 @@ pub(crate) mod tests {
|
||||
.keys()
|
||||
.any(|k| k.starts_with(REQUEST_SCHEMA_META_PREFIX))
|
||||
);
|
||||
finish(&mut dataset, &key, 1_000).await.unwrap();
|
||||
finish_after(&mut dataset, &key, 1_000, std::time::Duration::ZERO)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(keys_of(&dataset)[0].state, Some(State::Off));
|
||||
remove_column(&mut dataset, "_mk_0").await.unwrap();
|
||||
assert!(keys_of(&dataset).is_empty());
|
||||
@@ -1297,5 +1330,37 @@ pub(crate) mod tests {
|
||||
assert!(!adder.schema().metadata.contains_key(&entry));
|
||||
assert_eq!(keys_of(&adder)[0].state, Some(State::Off));
|
||||
}
|
||||
|
||||
/// A write that opened the table before the add and commits while
|
||||
/// finish waits out the grace is derived again before the key is
|
||||
/// ready; without the wait it lands after, stale, on a ready key.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn finish_waits_out_writes_that_raced_the_add() {
|
||||
for grace in [std::time::Duration::from_secs(3), std::time::Duration::ZERO] {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let mut dataset = table(&dir, 2).await;
|
||||
let stale = dataset.clone();
|
||||
let key = add_column(&mut dataset, "author").await.unwrap();
|
||||
let moved = |_: usize| serde_json::json!({"author": "moved"});
|
||||
let racer = async move {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(1_500)).await;
|
||||
upsert_with(stale, 0..50, &moved).await;
|
||||
};
|
||||
let (finished, _) =
|
||||
tokio::join!(finish_after(&mut dataset, &key, 1_000, grace), racer);
|
||||
finished.unwrap();
|
||||
dataset.checkout_latest().await.unwrap();
|
||||
assert_eq!(keys_of(&dataset)[0].state, Some(State::Ready));
|
||||
let found = dataset
|
||||
.count_rows(Some("array_has(_mk_0, 'moved')".to_string()))
|
||||
.await
|
||||
.unwrap();
|
||||
if grace.is_zero() {
|
||||
assert_eq!(found, 0, "the race did not land after ready");
|
||||
} else {
|
||||
assert_eq!(found, 50, "grace {grace:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user