From 81576a0eb76b4e56422721182e92214303cab9a4 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 20:49:40 +0100 Subject: [PATCH] fix(store): bound what reading each object reads ahead `read_each` read a whole chunk, up to 4096 objects, before handing any to its callback, sized from the previous chunk: thousands of small objects followed by large blobs could load gigabytes before a snapshot part saw it was full. And one object it couldn't read failed the whole chunk, even when the part filled before reaching it, losing the valid part. The cores reading a chunk now share a byte count and stop once it reaches the target, the chunk handing over objects up to the first part cut short, and each object's read error is kept until the callback reaches it. --- apps/origin-src/src/store/mod.rs | 85 ++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 14 deletions(-) diff --git a/apps/origin-src/src/store/mod.rs b/apps/origin-src/src/store/mod.rs index b98246c..87b9d28 100644 --- a/apps/origin-src/src/store/mod.rs +++ b/apps/origin-src/src/store/mod.rs @@ -30,6 +30,7 @@ use std::io; use std::num::NonZero; use std::panic; use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, PoisonError}; use std::thread; use std::time::{Duration, SystemTime}; @@ -597,11 +598,13 @@ impl Store

{ /// Reads the objects `ids` names, in order, handing each to `each` until /// it returns `false`. They're read from every core a chunk at a time, /// chunks growing while their objects are small and shrinking while - /// they're large, so a chunk holds a few MiB. + /// they're large. The cores stop reading a chunk once it holds a few + /// MiB, so it never holds much more, however large its objects. /// /// # Errors /// - /// Fails like [`Store::read`], or as `each` does. + /// Fails like [`Store::read`] once `each` reaches an object that cannot + /// be read, or as `each` does. pub fn read_each>( &self, ids: &[ObjectId], @@ -612,20 +615,50 @@ impl Store

{ let mut size = 16; let mut rest = ids; while !rest.is_empty() { - let (chunk, after) = rest.split_at(size.min(rest.len())); - rest = after; - let objects = self.read_all(chunk)?; - let bytes: usize = objects.iter().map(|object| object.bytes().len()).sum(); - if bytes < TARGET / 2 { - size = (size * 2).min(4096); - } else if bytes > TARGET * 2 { - size = (size / 2).max(1); - } - for object in objects { - if !each(object)? { - return Ok(()); + let chunk = &rest[..size.min(rest.len())]; + let read = AtomicUsize::new(0); + // Each core reads its part of the chunk, in order, until it + // fails or the chunk holds its target, though always one object, + // so the first part moves on. + let parts = Self::on_every_core(chunk, |part| { + let mut objects = Vec::new(); + for &id in part { + if !objects.is_empty() && read.load(Ordering::Relaxed) >= TARGET { + break; + } + let object = self.read(id); + let failed = object.is_err(); + if let Ok(object) = &object { + read.fetch_add(object.bytes().len(), Ordering::Relaxed); + } + objects.push(object); + if failed { + break; + } + } + (objects.len() == part.len(), objects) + }); + // In order, up to the first part that stopped short: the rest + // comes with the next chunk. + let mut handed = 0; + for (whole, objects) in parts { + for object in objects { + handed += 1; + if !each(object?)? { + return Ok(()); + } + } + if !whole { + break; } } + rest = &rest[handed..]; + let bytes = read.into_inner(); + if handed < chunk.len() || bytes > TARGET * 2 { + size = (size / 2).max(1); + } else if bytes < TARGET / 2 { + size = (size * 2).min(4096); + } } Ok(()) } @@ -1102,6 +1135,30 @@ pub(crate) mod tests { assert!(matches!(store.read(blob.id()), Err(StoreError::Corrupt(_)))); } + #[test] + fn reading_each_fails_only_on_reaching_an_unreadable_object() { + let (_dir, store) = store(); + let blobs: Vec<_> = (0..8_u8).map(|i| EncodedObject::blob(&[i])).collect(); + store.write_all(&blobs).unwrap(); + let corrupt = zstd::bulk::compress(EncodedObject::blob(b"evil").bytes(), 1).unwrap(); + fs::write(store.path(blobs[4].id()), corrupt).unwrap(); + let ids: Vec<_> = blobs.iter().map(EncodedObject::id).collect(); + + let mut read = Vec::new(); + store + .read_each(&ids, |object| { + read.push(object); + Ok::<_, StoreError>(read.len() < 4) + }) + .unwrap(); + assert_eq!(read, blobs[..4], "stopped before it"); + let reached = store.read_each(&ids, |_| Ok::<_, StoreError>(true)); + assert!( + matches!(reached, Err(StoreError::Corrupt(_))), + "{reached:?}" + ); + } + #[test] fn a_frame_claiming_a_huge_size_fails_to_read() { let (_dir, store) = store();