Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

vecdb: attach the snapshot graph mid-scan so tail replay applies against historical arena state; stage compaction appends unsynced ahead of the single pre-rename sync; sync the parent dir after index and data file removal

laurenspriemfc179a54bf9b
diff --git a/rust/crates/ml/src/vecdb/log.rs b/rust/crates/ml/src/vecdb/log.rs
index 1373694eff..41e5c97da0 100644
--- a/rust/crates/ml/src/vecdb/log.rs
+++ b/rust/crates/ml/src/vecdb/log.rs
@@ -154,13 +154,24 @@ impl Log {
}
pub(crate) fn append(&mut self, entries: &[LogEntry<'_>]) -> Result<(), VecDbError> {
+ self.append_records(entries, true)
+ }
+
+ pub(crate) fn append_unsynced_for_staging(
+ &mut self,
+ entries: &[LogEntry<'_>],
+ ) -> Result<(), VecDbError> {
+ self.append_records(entries, false)
+ }
+
+ fn append_records(&mut self, entries: &[LogEntry<'_>], sync: bool) -> Result<(), VecDbError> {
if entries.is_empty() {
return Ok(());
}
for entry in entries {
validate_entry(entry, self.dims)?;
}
- match self.write_records(entries) {
+ match self.write_records(entries, sync) {
Ok(written) => {
self.end_offset += written;
Ok(())
@@ -172,7 +183,7 @@ impl Log {
}
}
- fn write_records(&mut self, entries: &[LogEntry<'_>]) -> Result<u64, VecDbError> {
+ fn write_records(&mut self, entries: &[LogEntry<'_>], sync: bool) -> Result<u64, VecDbError> {
self.file
.seek(SeekFrom::Start(self.end_offset))
.map_err(|source| VecDbError::io(&self.path, source))?;
@@ -185,9 +196,11 @@ impl Log {
}
}
written += self.drain_encode_buffer()?;
- self.file
- .sync_all()
- .map_err(|source| VecDbError::io(&self.path, source))?;
+ if sync {
+ self.file
+ .sync_all()
+ .map_err(|source| VecDbError::io(&self.path, source))?;
+ }
Ok(written)
}
@@ -1364,6 +1377,58 @@ mod tests {
assert_eq!(recoverable_end, acked_end);
}
+ #[test]
+ fn unsynced_staging_appends_produce_bytes_identical_to_synced_appends() {
+ let dir = TempDir::new().unwrap();
+ let synced_path = dir.path().join("synced");
+ let staged_path = dir.path().join("staged");
+ let mut synced = Log::create(&synced_path, 8).unwrap();
+ let mut staged = Log::create(&staged_path, 8).unwrap();
+ let vectors: Vec<Vec<f32>> = (0..3).map(|seed| seeded_vector(seed, 8)).collect();
+ let attrs = [Attribute {
+ name: "model".to_string(),
+ value: AttrValue::Str("v1".to_string()),
+ }];
+ let batches: Vec<Vec<LogEntry<'_>>> = vec![
+ vec![
+ LogEntry::Add {
+ key: "a",
+ vector: &vectors[0],
+ attrs: &[],
+ },
+ LogEntry::Add {
+ key: "b",
+ vector: &vectors[1],
+ attrs: &attrs,
+ },
+ ],
+ vec![
+ LogEntry::Tombstone { key: "a" },
+ LogEntry::Add {
+ key: "c",
+ vector: &vectors[2],
+ attrs: &[],
+ },
+ ],
+ ];
+ for batch in &batches {
+ synced.append(batch).unwrap();
+ staged.append_unsynced_for_staging(batch).unwrap();
+ }
+ assert_eq!(staged.current_end_offset(), synced.current_end_offset());
+ staged.into_file().sync_all().unwrap();
+ drop(synced);
+ let synced_bytes = std::fs::read(&synced_path).unwrap();
+ let staged_bytes = std::fs::read(&staged_path).unwrap();
+ assert_eq!(staged_bytes.len(), synced_bytes.len());
+ assert_eq!(staged_bytes[HEADER_LEN..], synced_bytes[HEADER_LEN..]);
+ let (synced_records, synced_end) = scan_all(&mut reopen(&synced_path, 8));
+ let (staged_records, staged_end) = scan_all(&mut reopen(&staged_path, 8));
+ assert_eq!(staged_records.len(), 4);
+ assert_eq!(staged_records, synced_records);
+ assert_eq!(staged_end, synced_end);
+ }
+
#[test]
fn append_failure_rolls_back_without_disturbing_acked_records() {
let dir = TempDir::new().unwrap();
diff --git a/rust/crates/ml/src/vecdb/store.rs b/rust/crates/ml/src/vecdb/store.rs
index 888cba0c87..5541a9c677 100644
--- a/rust/crates/ml/src/vecdb/store.rs
+++ b/rust/crates/ml/src/vecdb/store.rs
@@ -16,7 +16,9 @@ use super::log::{
HEADER_LEN, Log, LogEntry, LogRecord, header_generation, remove_if_present,
remove_stale_temp_sibling, sync_parent_dir,
};
-use super::snapshot::{load_snapshot, remove_snapshot, snapshot_path, write_snapshot};
+use super::snapshot::{
+ LoadedSnapshot, load_snapshot, remove_snapshot, snapshot_path, write_snapshot,
+};
use super::{AttrValue, Attribute, KeyMatches, Match, SearchParams, VecDbError};
const SNAPSHOT_HARD_CAP: usize = 5000;
@@ -1283,7 +1285,7 @@ struct ReplayedState {
}
fn replay(log: &mut Log, dims: usize) -> Result<ReplayedState, VecDbError> {
- let snapshot = load_snapshot(log.path(), log.generation());
+ let mut snapshot = load_snapshot(log.path(), log.generation());
if let Some(loaded) = &snapshot
&& loaded.covered_log_offset > log.current_end_offset()
{
@@ -1294,23 +1296,21 @@ fn replay(log: &mut Log, dims: usize) -> Result<ReplayedState, VecDbError> {
log.current_end_offset()
)));
}
- let covered = snapshot
- .as_ref()
- .map_or(u64::MAX, |loaded| loaded.covered_log_offset);
+ let snapshot_covered = snapshot.as_ref().map(|loaded| loaded.covered_log_offset);
+ let covered = snapshot_covered.unwrap_or(u64::MAX);
+ let path = log.path().to_path_buf();
let mut arena = VectorArena::new(dims)?;
let mut attrs = AttrTable::default();
- let mut tail_outcomes: Vec<UpsertOutcome> = Vec::new();
+ let mut graph: Option<Graph> = None;
let mut tail_records = 0u64;
let mut total_records = 0u64;
- let mut covered_slot_count: Option<usize> = None;
let recoverable_end;
{
let mut scanner = log.scan()?;
while let Some((record, offset)) = scanner.next_record()? {
total_records += 1;
- let in_tail = offset >= covered;
- if in_tail {
- covered_slot_count.get_or_insert(arena.slot_count());
+ if offset >= covered {
+ attach_snapshot_graph(&mut graph, &mut snapshot, &arena, &path);
tail_records += 1;
}
match record {
@@ -1321,8 +1321,8 @@ fn replay(log: &mut Log, dims: usize) -> Result<ReplayedState, VecDbError> {
} => {
let outcome = arena.upsert(&key, &vector)?;
attrs.set(slot_of_outcome(outcome), &record_attrs);
- if in_tail {
- tail_outcomes.push(outcome);
+ if let Some(graph) = &mut graph {
+ apply_outcome(graph, &arena, outcome);
}
}
LogRecord::Tombstone { key } => {
@@ -1336,13 +1336,12 @@ fn replay(log: &mut Log, dims: usize) -> Result<ReplayedState, VecDbError> {
}
let physical_end = log.current_end_offset();
if recoverable_end < physical_end {
- if let Some(loaded) = &snapshot
- && loaded.covered_log_offset > recoverable_end
+ if let Some(snapshot_covered) = snapshot_covered
+ && snapshot_covered > recoverable_end
{
return Err(VecDbError::Corrupt(format!(
- "{}: log unreadable past offset {recoverable_end}, but its snapshot covers {} of {physical_end} bytes; preserving the file",
- log.path().display(),
- loaded.covered_log_offset
+ "{}: log unreadable past offset {recoverable_end}, but its snapshot covers {snapshot_covered} of {physical_end} bytes; preserving the file",
+ log.path().display()
)));
}
if log.tail_contains_valid_record(recoverable_end)? {
@@ -1352,29 +1351,9 @@ fn replay(log: &mut Log, dims: usize) -> Result<ReplayedState, VecDbError> {
)));
}
}
- let graph = match snapshot {
- Some(loaded) if loaded.covered_log_offset <= recoverable_end => {
- let snapshot_slots = covered_slot_count.unwrap_or(arena.slot_count());
- match Graph::from_parts(
- loaded.entry_point,
- loaded.parts,
- snapshot_slots,
- loaded.insert_ordinal,
- ) {
- Ok(mut graph) => {
- for outcome in tail_outcomes {
- apply_outcome(&mut graph, &arena, outcome);
- }
- Some(graph)
- }
- Err(error) => {
- log::debug!("discarding snapshot for {}: {error}", log.path().display());
- None
- }
- }
- }
- _ => None,
- };
+ if covered <= recoverable_end {
+ attach_snapshot_graph(&mut graph, &mut snapshot, &arena, &path);
+ }
Ok(ReplayedState {
arena,
graph,
@@ -1385,6 +1364,28 @@ fn replay(log: &mut Log, dims: usize) -> Result<ReplayedState, VecDbError> {
})
}
+fn attach_snapshot_graph(
+ graph: &mut Option<Graph>,
+ snapshot: &mut Option<LoadedSnapshot>,
+ arena: &VectorArena,
+ path: &Path,
+) {
+ let Some(loaded) = snapshot.take() else {
+ return;
+ };
+ match Graph::from_parts(
+ loaded.entry_point,
+ loaded.parts,
+ arena.slot_count(),
+ loaded.insert_ordinal,
+ ) {
+ Ok(attached) => *graph = Some(attached),
+ Err(error) => {
+ log::debug!("discarding snapshot for {}: {error}", path.display());
+ }
+ }
+}
+
fn apply_outcome(graph: &mut Graph, arena: &VectorArena, outcome: UpsertOutcome) {
match outcome {
UpsertOutcome::NewSlot(slot) => graph.insert(slot, arena),
@@ -1419,13 +1420,15 @@ fn promote_compacted_log(temp_path: &Path, path: &Path) -> Result<(), VecDbError
fn remove_index_files(path: &Path) -> Result<(), VecDbError> {
remove_data_files(path)?;
- remove_if_present(&lock_path(path))
+ remove_if_present(&lock_path(path))?;
+ sync_parent_dir(path)
}
fn remove_data_files(path: &Path) -> Result<(), VecDbError> {
remove_if_present(path)?;
remove_snapshot(path)?;
- remove_stale_temp_sibling(path)
+ remove_stale_temp_sibling(path)?;
+ sync_parent_dir(path)
}
fn stage_live_entries(
@@ -1469,7 +1472,7 @@ fn flush_stage_batch(
.iter()
.map(|(key, vector, attrs)| LogEntry::Add { key, vector, attrs })
.collect();
- log.append(&records)?;
+ log.append_unsynced_for_staging(&records)?;
for (key, vector, attrs) in batch.drain(..) {
let outcome = target.upsert(&key, &vector)?;
target_attrs.set(slot_of_outcome(outcome), &attrs);
@@ -1487,6 +1490,7 @@ mod tests {
use super::super::crc::crc32;
use super::super::kernel::splitmix64;
+ use super::super::test_support;
use super::*;
const DIMS: usize = 16;
@@ -2574,10 +2578,20 @@ mod tests {
}
#[test]
- fn interrupted_sessions_draw_the_same_levels_as_one_uninterrupted_session() {
+ fn interrupted_sessions_build_the_same_graph_as_one_uninterrupted_session() {
let dir = TempDir::new().unwrap();
let split_path = dir.path().join("split");
let whole_path = dir.path().join("whole");
+ let tail_new = seeded_unit_vector(4242, DIMS);
+ let older_upsert = {
+ let mut values = tail_new.clone();
+ values[0] += 0.05;
+ let norm = values.iter().map(|value| value * value).sum::<f32>().sqrt();
+ for value in &mut values {
+ *value /= norm;
+ }
+ values
+ };
{
let db = open_writer(&split_path);
bulk_add(&db, &bulk_entries(0, 40, 500)).unwrap();
@@ -2591,41 +2605,36 @@ mod tests {
);
{
let db = open_writer(&split_path);
- bulk_add(&db, &bulk_entries(40, 30, 500)).unwrap();
- db.add("key-3", &seeded_unit_vector(999, DIMS)).unwrap();
- }
- {
- let db = open_writer(&split_path);
- bulk_add(&db, &bulk_entries(70, 30, 500)).unwrap();
- db.flush().unwrap();
+ db.add("tail-new", &tail_new).unwrap();
+ db.add("key-3", &older_upsert).unwrap();
}
let split = open_writer(&split_path);
let whole = open_writer(&whole_path);
bulk_add(&whole, &bulk_entries(0, 40, 500)).unwrap();
- bulk_add(&whole, &bulk_entries(40, 30, 500)).unwrap();
- whole.add("key-3", &seeded_unit_vector(999, DIMS)).unwrap();
- bulk_add(&whole, &bulk_entries(70, 30, 500)).unwrap();
+ whole.add("tail-new", &tail_new).unwrap();
+ whole.add("key-3", &older_upsert).unwrap();
let split_state = split.shared.state_read();
let whole_state = whole.shared.state_read();
let split_graph = split_state.graph.as_ref().unwrap();
let whole_graph = whole_state.graph.as_ref().unwrap();
- assert_eq!(split_graph.insert_ordinal(), 101);
- assert_eq!(whole_graph.insert_ordinal(), 101);
- let mut upper_levels = 0usize;
- for index in 0..100 {
- let key = format!("key-{index}");
- let split_slot = split_state.arena.slot_of_key(&key).unwrap();
- let whole_slot = whole_state.arena.slot_of_key(&key).unwrap();
- assert_eq!(split_slot, whole_slot);
- let level = split_graph.level_of(split_slot).unwrap();
+ assert_eq!(split_graph.insert_ordinal(), 42);
+ assert_eq!(whole_graph.insert_ordinal(), 42);
+ for key in (0..40)
+ .map(|index| format!("key-{index}"))
+ .chain(["tail-new".to_string()])
+ {
assert_eq!(
- level,
- whole_graph.level_of(whole_slot).unwrap(),
- "level diverges for {key}"
+ split_state.arena.slot_of_key(&key),
+ whole_state.arena.slot_of_key(&key),
+ "slot diverges for {key}"
);
- upper_levels += usize::from(level > 0);
}
- assert!(upper_levels > 0);
+ assert!(
+ split_graph
+ .slots()
+ .any(|slot| split_graph.level_of(slot).unwrap() > 0)
+ );
+ test_support::assert_identical_graphs(split_graph, whole_graph);
}
#[test]