From 009c47495f9ab02a66a49d06f365aaefc8907f52 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 11 Nov 2026 15:31:16 -0500 Subject: [PATCH 1/2] fix: stop the view walk hanging on its shared span context Repository::materialize_view_entries renders files on rayon workers that share one lazily built span context. Building that context runs rayon jobs as well, so the worker building it could end up waiting for a worker that had picked up another file of the same walk and was parked on the context still being built. Neither could finish, and the rest of the pool parked behind them. - SharedSpanContext builds outside its cell and keeps the first result, so no caller waits for another caller's build. - The view walk builds the context before it fans out when the view has a span-rooted file, so a walk still builds exactly one context. --- atomic-repository/src/repository/content.rs | 60 +++- .../src/repository/materialize.rs | 11 +- atomic-repository/src/repository/tests/mod.rs | 1 + .../tests/view_entries_span_context_tests.rs | 278 ++++++++++++++++++ 4 files changed, 336 insertions(+), 14 deletions(-) create mode 100644 atomic-repository/src/repository/tests/view_entries_span_context_tests.rs diff --git a/atomic-repository/src/repository/content.rs b/atomic-repository/src/repository/content.rs index e5a23ac4..f592c52f 100644 --- a/atomic-repository/src/repository/content.rs +++ b/atomic-repository/src/repository/content.rs @@ -1092,8 +1092,13 @@ where /// transaction and visibility closure. `retrieve_span_if_present` builds a /// fresh context per call — which decodes and verifies the whole visible /// closure — so a per-file caller on a large view pays the closure decode -/// thousands of times; holders of this struct pay it once. Safe to share -/// across rayon workers: first use builds, the rest reuse. +/// thousands of times; holders of this struct pay it once. +/// +/// Building the context runs rayon jobs, so a caller never waits for another +/// caller's build: the worker building it may itself be waiting for a worker +/// that needs the context for another file of the same walk. A caller that +/// finds none builds its own, and the first one finished is kept. Parallel +/// walks call [`Self::prime`] first so that only one is ever built. pub(super) struct SharedSpanContext<'a, T, C> where T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn @@ -1135,22 +1140,51 @@ where if !has_visible_span_root(self.txn, self.store, inode, self.visibility)? { return Ok(None); } - let context = self - .context - .get_or_init(|| { - atomic_core::pristine::span_patch_storage::SpanRenderContext::new( + self.context()? + .render(inode) + .map(Some) + .map_err(|error| RepositoryError::Database(error.to_string())) + } + + /// Build the context on the calling thread when any of `inodes` is + /// span-rooted on this view, so that a parallel walk over them shares one + /// build instead of every worker that arrives early making its own. A + /// failure is left for the walk to report at the path that hits it. + pub(super) fn prime(&self, inodes: impl IntoIterator) { + let any_span_root = inodes.into_iter().any(|inode| { + matches!( + has_visible_span_root(self.txn, self.store, inode, self.visibility), + Ok(true) + ) + }); + if any_span_root { + // The outcome, an error included, is stored for the renders. + let _stored = self.context(); + } + } + + fn context( + &self, + ) -> Result<&atomic_core::pristine::span_patch_storage::SpanRenderContext<'a, T>, RepositoryError> + { + let stored = match self.context.get() { + Some(stored) => stored, + None => { + // Built outside the cell: initialising it in place would park + // every other caller behind a build that may be waiting for + // one of them. + let built = atomic_core::pristine::span_patch_storage::SpanRenderContext::new( self.txn, self.store, self.visibility, ) - .map_err(|error| error.to_string()) - }) + .map_err(|error| error.to_string()); + self.context.get_or_init(|| built) + } + }; + stored .as_ref() - .map_err(|error| RepositoryError::Database(error.clone()))?; - context - .render(inode) - .map(Some) - .map_err(|error| RepositoryError::Database(error.to_string())) + .map_err(|error| RepositoryError::Database(error.clone())) } } diff --git a/atomic-repository/src/repository/materialize.rs b/atomic-repository/src/repository/materialize.rs index e969b622..14a03733 100644 --- a/atomic-repository/src/repository/materialize.rs +++ b/atomic-repository/src/repository/materialize.rs @@ -2605,9 +2605,18 @@ impl Repository { let filter = crate::content_filter::GitAttributesFilter::for_repository(&self.root); // One span context per walk: per-file contexts each decode and // verify the whole visible closure, which on a large span-backed - // view costs more than the render itself. + // view costs more than the render itself. It is built here rather + // than by whichever workers reach a span-rooted file first. let shared_spans = super::content::SharedSpanContext::new(&txn, &self.change_store, &visibility); + shared_spans.prime( + paths + .iter() + .filter(|path| !conflict_entries.contains_key(*path)) + .map(|path| &projection.present[path]) + .filter(|item| !item.is_directory) + .map(|item| item.inode), + ); for chunk in paths.chunks(256) { let rendered: Vec> = chunk .par_iter() diff --git a/atomic-repository/src/repository/tests/mod.rs b/atomic-repository/src/repository/tests/mod.rs index 0a505b6f..ac5f33dd 100644 --- a/atomic-repository/src/repository/tests/mod.rs +++ b/atomic-repository/src/repository/tests/mod.rs @@ -57,6 +57,7 @@ mod tag_projection_tests; mod tracking_tests; mod tree_projection_tests; mod verify_tests; +mod view_entries_span_context_tests; mod view_tests; mod working_copy_identity_tests; mod working_copy_reconcile_tests; diff --git a/atomic-repository/src/repository/tests/view_entries_span_context_tests.rs b/atomic-repository/src/repository/tests/view_entries_span_context_tests.rs new file mode 100644 index 00000000..2a53d5cd --- /dev/null +++ b/atomic-repository/src/repository/tests/view_entries_span_context_tests.rs @@ -0,0 +1,278 @@ +use super::super::content::SharedSpanContext; +use super::super::filter::graph_visibility_closure; +use super::*; +use atomic_core::change::{VerifiedChange, VerifiedLoadError}; +use atomic_core::pristine::{ + CapabilityMutTxnT, MutTxnT, TreeTxnT, ViewTxnT, SPAN_RANGE_CAPABILITY, +}; +use atomic_core::types::GraphNode; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Condvar, Mutex}; +use std::time::Duration; + +/// Far longer than a render takes; only a render that is stuck behind the +/// build it is holding up ever waits this long. +const SECOND_RENDER_DEADLINE: Duration = Duration::from_secs(60); + +fn record_all(repo: &TestRepository, message: &str) { + repo.record( + ChangeHeader::new(message), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + .sync_vault(false) + .enrich_kg(false), + ) + .unwrap(); +} + +fn enable_spans(repo: &TestRepository) { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); +} + +fn write_and_add(temp: &tempfile::TempDir, repo: &TestRepository, path: &str, bytes: &[u8]) { + std::fs::write(temp.path().join(path), bytes).unwrap(); + repo.add(path, TrackingOptions::default()).unwrap(); +} + +/// `legacy.txt` stays graph-origin; `a.txt` and `b.txt` are span-rooted. +fn record_mixed_fixture(temp: &tempfile::TempDir, repo: &TestRepository) { + write_and_add(temp, repo, "legacy.txt", b"legacy\n"); + record_all(repo, "graph origin"); + enable_spans(repo); + write_and_add(temp, repo, "a.txt", b"alpha\n"); + write_and_add(temp, repo, "b.txt", b"beta\n"); + record_all(repo, "span files"); + std::fs::write(temp.path().join("a.txt"), b"alpha edited\n").unwrap(); + record_all(repo, "edit a"); +} + +/// Lets a test run a second render while the first span context build is +/// still inside the change store. +#[derive(Default)] +struct BuildGate { + state: Mutex, + changed: Condvar, +} + +#[derive(Default)] +struct BuildGateState { + build_started: bool, + second_render_finished: bool, + build_gave_up: bool, +} + +impl BuildGate { + /// Called from inside the first build: stay there until the second + /// render has finished, or give up when it never does. + fn hold_build(&self) { + let mut state = self.state.lock().unwrap(); + state.build_started = true; + self.changed.notify_all(); + let (mut state, wait) = self + .changed + .wait_timeout_while(state, SECOND_RENDER_DEADLINE, |state| { + !state.second_render_finished + }) + .unwrap(); + if wait.timed_out() { + state.build_gave_up = true; + } + } + + fn wait_for_build(&self) { + let state = self.state.lock().unwrap(); + let (_state, wait) = self + .changed + .wait_timeout_while(state, SECOND_RENDER_DEADLINE, |state| !state.build_started) + .unwrap(); + assert!(!wait.timed_out(), "the first render never built a context"); + } + + fn second_render_finished(&self) { + self.state.lock().unwrap().second_render_finished = true; + self.changed.notify_all(); + } + + fn build_gave_up(&self) -> bool { + self.state.lock().unwrap().build_gave_up + } +} + +/// The repository's change store, counting the verified loads that only a +/// span context build performs and optionally holding the first one up. +struct ObservedStore<'a> { + inner: &'a ChangeStore, + verified_loads: AtomicUsize, + hold_first_build: Option<&'a BuildGate>, +} + +impl<'a> ObservedStore<'a> { + fn new(inner: &'a ChangeStore) -> Self { + Self { + inner, + verified_loads: AtomicUsize::new(0), + hold_first_build: None, + } + } + + fn holding_first_build(inner: &'a ChangeStore, gate: &'a BuildGate) -> Self { + Self { + hold_first_build: Some(gate), + ..Self::new(inner) + } + } +} + +impl atomic_core::change::ChangeStore for ObservedStore<'_> { + type Error = ChangeStoreError; + + fn get_change(&self, hash: &Hash) -> Result { + self.inner.load_change(hash) + } + + fn get_verified_change( + &self, + hash: &Hash, + ) -> Result> { + if self.verified_loads.fetch_add(1, Ordering::SeqCst) == 0 { + if let Some(gate) = self.hold_first_build { + gate.hold_build(); + } + } + atomic_core::change::ChangeStore::get_verified_change(self.inner, hash) + } + + fn get_contents( + &self, + hash_fn: F, + span: GraphNode, + buf: &mut [u8], + ) -> Result + where + F: Fn(NodeId) -> Option, + { + atomic_core::change::ChangeStore::get_contents(self.inner, hash_fn, span, buf) + } + + fn get_contents_ext( + &self, + span: GraphNode>, + buf: &mut [u8], + ) -> Result { + atomic_core::change::ChangeStore::get_contents_ext(self.inner, span, buf) + } +} + +/// Building a span context runs rayon jobs, so the worker building it can end +/// up waiting for another worker — one that has meanwhile picked up a file of +/// the same walk and needs the context too. That second render has to finish +/// while the first build is still in progress; if it waits for the build, the +/// two wait on each other and the walk never returns. +#[test] +fn render_does_not_wait_for_a_span_context_build_in_progress() { + let (temp, repo) = create_temp_repo(); + record_mixed_fixture(&temp, &repo); + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view(repo.current_view()).unwrap().unwrap(); + let visibility = graph_visibility_closure(&txn, &view).unwrap(); + let a = txn.get_inode("a.txt").unwrap().unwrap(); + let b = txn.get_inode("b.txt").unwrap().unwrap(); + + let gate = BuildGate::default(); + let store = ObservedStore::holding_first_build(repo.change_store(), &gate); + let spans = SharedSpanContext::new(&txn, &store, &visibility); + let (first, second) = std::thread::scope(|scope| { + let first = scope.spawn(|| spans.render_if_present(a)); + gate.wait_for_build(); + let second = spans.render_if_present(b); + gate.second_render_finished(); + (first.join().unwrap(), second) + }); + + assert!( + !gate.build_gave_up(), + "the second render waited for the context build that was waiting for it" + ); + assert_eq!( + first.unwrap().as_deref(), + Some(b"alpha edited\n".as_slice()) + ); + assert_eq!(second.unwrap().as_deref(), Some(b"beta\n".as_slice())); +} + +/// A parallel walk primes the context before it fans out. Priming builds it +/// once when the walk covers a span-rooted file, and not at all for a walk +/// over graph-origin files only. +#[test] +fn prime_builds_the_span_context_once_and_only_for_span_rooted_files() { + let (temp, repo) = create_temp_repo(); + record_mixed_fixture(&temp, &repo); + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view(repo.current_view()).unwrap().unwrap(); + let visibility = graph_visibility_closure(&txn, &view).unwrap(); + let legacy = txn.get_inode("legacy.txt").unwrap().unwrap(); + let a = txn.get_inode("a.txt").unwrap().unwrap(); + let b = txn.get_inode("b.txt").unwrap().unwrap(); + + let store = ObservedStore::new(repo.change_store()); + let spans = SharedSpanContext::new(&txn, &store, &visibility); + spans.prime([legacy]); + assert_eq!( + store.verified_loads.load(Ordering::SeqCst), + 0, + "graph-origin files need no span context" + ); + assert_eq!(spans.render_if_present(legacy).unwrap(), None); + + spans.prime([legacy, a]); + let one_build = store.verified_loads.load(Ordering::SeqCst); + assert!(one_build > 0, "a span-rooted file needs the context built"); + + spans.prime([a, b]); + assert_eq!( + spans.render_if_present(a).unwrap().as_deref(), + Some(b"alpha edited\n".as_slice()) + ); + assert_eq!( + spans.render_if_present(b).unwrap().as_deref(), + Some(b"beta\n".as_slice()) + ); + assert_eq!( + store.verified_loads.load(Ordering::SeqCst), + one_build, + "priming again and rendering reuse the one context" + ); +} + +/// The view walk renders on rayon workers; span-rooted and graph-origin files +/// alike must come back with their recorded bytes. +#[test] +fn view_entries_walk_renders_mixed_views_on_a_worker_pool() { + let (temp, repo) = create_temp_repo(); + record_mixed_fixture(&temp, &repo); + let view = repo.current_view().to_string(); + let pool = rayon::ThreadPoolBuilder::new() + .num_threads(8) + .build() + .unwrap(); + for _ in 0..20 { + let mut rendered = std::collections::BTreeMap::new(); + pool.install(|| { + repo.materialize_view_entries::<()>(&view, |entry| { + if entry.kind == ViewEntryKind::File { + rendered.insert(entry.path, entry.content); + } + Ok(()) + }) + }) + .unwrap() + .unwrap(); + assert_eq!(rendered["legacy.txt"], b"legacy\n"); + assert_eq!(rendered["a.txt"], b"alpha edited\n"); + assert_eq!(rendered["b.txt"], b"beta\n"); + } +} From a610ea161f78c6293def9ebd69dcf4c4287095f8 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 11 Nov 2026 17:57:03 -0500 Subject: [PATCH 2/2] fix: prime the span context without loading graph-origin changes Priming asked has_visible_span_root about every file of the walk. For a file without a span root that loads its origin change, which the walk already does per file on its workers, so a view of mostly graph-origin files paid for the check a second time on one thread. Look for an indexed root first. The regression test held up a worker of the global rayon pool, so with a single worker the second render had nothing to run on and the test failed. It now uses a two-worker pool of its own. --- atomic-repository/src/repository/content.rs | 12 +++++--- .../tests/view_entries_span_context_tests.rs | 28 +++++++++++++++---- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/atomic-repository/src/repository/content.rs b/atomic-repository/src/repository/content.rs index f592c52f..d913e7e6 100644 --- a/atomic-repository/src/repository/content.rs +++ b/atomic-repository/src/repository/content.rs @@ -1152,10 +1152,14 @@ where /// failure is left for the walk to report at the path that hits it. pub(super) fn prime(&self, inodes: impl IntoIterator) { let any_span_root = inodes.into_iter().any(|inode| { - matches!( - has_visible_span_root(self.txn, self.store, inode, self.visibility), - Ok(true) - ) + // Only an indexed root can need the context. Asking about a file + // without one would load its origin change, which the walk does + // for each file on its own workers. + matches!(self.txn.span_root(inode), Ok(Some(_))) + && matches!( + has_visible_span_root(self.txn, self.store, inode, self.visibility), + Ok(true) + ) }); if any_span_root { // The outcome, an error included, is stored for the renders. diff --git a/atomic-repository/src/repository/tests/view_entries_span_context_tests.rs b/atomic-repository/src/repository/tests/view_entries_span_context_tests.rs index 2a53d5cd..fcd6e3d2 100644 --- a/atomic-repository/src/repository/tests/view_entries_span_context_tests.rs +++ b/atomic-repository/src/repository/tests/view_entries_span_context_tests.rs @@ -102,10 +102,12 @@ impl BuildGate { } } -/// The repository's change store, counting the verified loads that only a -/// span context build performs and optionally holding the first one up. +/// The repository's change store, counting plain loads and the verified +/// loads that only a span context build performs, and optionally holding the +/// first build up. struct ObservedStore<'a> { inner: &'a ChangeStore, + plain_loads: AtomicUsize, verified_loads: AtomicUsize, hold_first_build: Option<&'a BuildGate>, } @@ -114,6 +116,7 @@ impl<'a> ObservedStore<'a> { fn new(inner: &'a ChangeStore) -> Self { Self { inner, + plain_loads: AtomicUsize::new(0), verified_loads: AtomicUsize::new(0), hold_first_build: None, } @@ -131,6 +134,7 @@ impl atomic_core::change::ChangeStore for ObservedStore<'_> { type Error = ChangeStoreError; fn get_change(&self, hash: &Hash) -> Result { + self.plain_loads.fetch_add(1, Ordering::SeqCst); self.inner.load_change(hash) } @@ -185,10 +189,16 @@ fn render_does_not_wait_for_a_span_context_build_in_progress() { let gate = BuildGate::default(); let store = ObservedStore::holding_first_build(repo.change_store(), &gate); let spans = SharedSpanContext::new(&txn, &store, &visibility); + // The held build keeps one worker inside the store, so the second render + // needs a worker of its own whatever size the global pool is. + let pool = rayon::ThreadPoolBuilder::new() + .num_threads(2) + .build() + .unwrap(); let (first, second) = std::thread::scope(|scope| { - let first = scope.spawn(|| spans.render_if_present(a)); + let first = scope.spawn(|| pool.install(|| spans.render_if_present(a))); gate.wait_for_build(); - let second = spans.render_if_present(b); + let second = pool.install(|| spans.render_if_present(b)); gate.second_render_finished(); (first.join().unwrap(), second) }); @@ -205,8 +215,9 @@ fn render_does_not_wait_for_a_span_context_build_in_progress() { } /// A parallel walk primes the context before it fans out. Priming builds it -/// once when the walk covers a span-rooted file, and not at all for a walk -/// over graph-origin files only. +/// once when the walk covers a span-rooted file. For graph-origin files it +/// builds nothing and loads nothing: the walk checks each of those itself, on +/// its workers, and priming must not repeat that on one thread. #[test] fn prime_builds_the_span_context_once_and_only_for_span_rooted_files() { let (temp, repo) = create_temp_repo(); @@ -226,6 +237,11 @@ fn prime_builds_the_span_context_once_and_only_for_span_rooted_files() { 0, "graph-origin files need no span context" ); + assert_eq!( + store.plain_loads.load(Ordering::SeqCst), + 0, + "priming must not load the origin change of a graph-origin file" + ); assert_eq!(spans.render_if_present(legacy).unwrap(), None); spans.prime([legacy, a]);