Unverified Commit e8ed1445 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks-backend: parent_count reference counting (#95976)

Adds the reference counting that lets the backend know when a task has
become unreachable.

### What this adds

Two inline counts on `TaskStorage`, maintained incrementally so a task's
reachability is known the moment its last parent drops — with no DB
scan:

- **`parent_count`** (meta, persisted) — how many persistent parents
list this task in their `children` set. Reaching 0 means the task is
unreachable from persistent roots.
- **`transient_ref_count`** (transient) — session-only references that
aren't persistent parent edges: a transient parent's child edge, or a
detached handle held outside the tracked graph. Never persisted;
re-established on restart.

Maintenance happens at the two `children`-edge mutation sites:

- `connect_children` applies the `+1` directly under the child guard it
already takes. It has to land before the first `operation_suspend_point`
so the new child edge and the `+1` are atomically observable.
- `CleanupOldEdges` queues the `-1` as an aggregation-update job, which
takes the child guards itself rather than nesting a second guard under
the parent's.

`AdjustParentCount` is durably encoded so an in-flight delta replays
after a crash and the persisted count stays consistent.
`AdjustTransientRefCount` is `bincode(skip)` and collapses to `Noop`,
like the other transient active-count jobs.

### Two things worth a closer look

**`output_dependent`'s inline capacity drops from 6 to 4.**
`test_schema_size` asserts `TaskStorage` is exactly 128 bytes, and the
two new `u32`s add 8. `AutoSet<TaskId, 6>` is 32 B and `AutoSet<TaskId,
4>` is 24 B, freeing exactly the 8 needed (capacity 5 is still 32 B, so
4 is the first that fits). This is a real behavior change, not just a
layout tweak: a task with 5 or 6 output dependents now spills to the
heap where it previously stayed inline.

**`task_execution_completed_unfinished_children_dirty` is gone.** Since
`connect_children` now takes a guard on every new child anyway, the
"mark children with no output dirty" pass folds into that same loop —
one guard acquisition instead of two. Behavior-preserving relocation,
but it does mean the dirty-marking now reads as part of the connect
path.

### Testing

`tests/parent_count.rs` covers the count reaching the right value
through connect/disconnect, the parallel wide-fanout `connect_children`
path, revalidation not double-counting, survival across a snapshot +
eviction, and survival across a full DB reopen (the count is persisted
meta, and an in-flight `AdjustParentCount` replays via the durable
queue).

Each of the four commits builds and tests green on its own.
parent bc80691c
...@@ -330,6 +330,23 @@ impl TurboTasksBackend { ...@@ -330,6 +330,23 @@ impl TurboTasksBackend {
(had_new_data, counts) (had_new_data, counts)
} }
/// The persistent `parent_count` of a resident task (0 if absent or not resident). Test-only
/// hook for verifying incremental refcount maintenance.
#[doc(hidden)]
pub fn parent_count_for_testing(&self, task: TaskId) -> u32 {
self.storage
.with_task(task, |t| t.gc_parent_count())
.unwrap_or(0)
}
/// The transient `transient_ref_count` of a resident task (0 if absent or not resident).
#[doc(hidden)]
pub fn transient_ref_count_for_testing(&self, task: TaskId) -> u32 {
self.storage
.with_task(task, |t| t.gc_transient_ref_count())
.unwrap_or(0)
}
/// Opens `task` with the must-exist [`ExecuteContext::task`] and drops the guard. Test-only /// Opens `task` with the must-exist [`ExecuteContext::task`] and drops the guard. Test-only
/// hook to exercise the non-fabricating existence guarantee: this panics (debug builds) if /// hook to exercise the non-fabricating existence guarantee: this panics (debug builds) if
/// `task` exists in neither memory nor persistent storage (rather than fabricating a /// `task` exists in neither memory nor persistent storage (rather than fabricating a
...@@ -2119,10 +2136,6 @@ impl TurboTasksBackend { ...@@ -2119,10 +2136,6 @@ impl TurboTasksBackend {
let has_new_children = !new_children.is_empty(); let has_new_children = !new_children.is_empty();
span.record("new_children", new_children.len()); span.record("new_children", new_children.len());
if has_new_children {
self.task_execution_completed_unfinished_children_dirty(&mut ctx, &new_children)
}
if has_new_children if has_new_children
&& let Some(stale_priority) = && let Some(stale_priority) =
self.task_execution_completed_connect(&mut ctx, task_id, new_children) self.task_execution_completed_connect(&mut ctx, task_id, new_children)
...@@ -2580,36 +2593,6 @@ impl TurboTasksBackend { ...@@ -2580,36 +2593,6 @@ impl TurboTasksBackend {
} }
} }
fn task_execution_completed_unfinished_children_dirty(
&self,
ctx: &mut impl ExecuteContext<'_>,
new_children: &FxHashSet<TaskId>,
) {
debug_assert!(!new_children.is_empty());
let mut queue = AggregationUpdateQueue::new();
ctx.for_each_task_all(
new_children.iter().copied(),
"unfinished children dirty",
|child_task, ctx| {
if !child_task.has_output() {
let child_id = child_task.id();
make_task_dirty_internal(
child_task,
child_id,
false,
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::InitialDirty,
&mut queue,
ctx,
);
}
},
);
queue.execute(ctx);
}
fn task_execution_completed_connect( fn task_execution_completed_connect(
&self, &self,
ctx: &mut impl ExecuteContext<'_>, ctx: &mut impl ExecuteContext<'_>,
......
...@@ -280,6 +280,15 @@ pub enum AggregationUpdateJob { ...@@ -280,6 +280,15 @@ pub enum AggregationUpdateJob {
lost_follower_ids: TaskIdVec, lost_follower_ids: TaskIdVec,
retry: u16, retry: u16,
}, },
/// Adjust the persistent `parent_count` of each task in `task_ids` by `delta`
AdjustParentCount { task_ids: TaskIdVec, delta: i32 },
/// Adjust the session-only `transient_ref_count` of each task in `task_ids` by `delta`
AdjustTransientRefCount {
#[bincode(skip, default = "unreachable_decode")]
task_ids: TaskIdVec,
#[bincode(skip, default = "unreachable_decode")]
delta: i32,
},
/// Notifies an upper task about changed data from an inner task. /// Notifies an upper task about changed data from an inner task.
AggregatedDataUpdate(Box<AggregatedDataUpdateJob>), AggregatedDataUpdate(Box<AggregatedDataUpdateJob>),
/// Invalidates tasks that are dependent on a collectible type. /// Invalidates tasks that are dependent on a collectible type.
...@@ -880,7 +889,8 @@ mod encode_jobs { ...@@ -880,7 +889,8 @@ mod encode_jobs {
AggregationUpdateJob::IncreaseActiveCount { .. } AggregationUpdateJob::IncreaseActiveCount { .. }
| AggregationUpdateJob::IncreaseActiveCounts { .. } | AggregationUpdateJob::IncreaseActiveCounts { .. }
| AggregationUpdateJob::DecreaseActiveCount { .. } | AggregationUpdateJob::DecreaseActiveCount { .. }
| AggregationUpdateJob::DecreaseActiveCounts { .. } => { | AggregationUpdateJob::DecreaseActiveCounts { .. }
| AggregationUpdateJob::AdjustTransientRefCount { .. } => {
AggregationUpdateJobItem { AggregationUpdateJobItem {
job: AggregationUpdateJob::Noop, job: AggregationUpdateJob::Noop,
#[cfg(feature = "trace_aggregation_update_queue")] #[cfg(feature = "trace_aggregation_update_queue")]
...@@ -1446,6 +1456,20 @@ impl AggregationUpdateQueue { ...@@ -1446,6 +1456,20 @@ impl AggregationUpdateQueue {
); );
} }
} }
AggregationUpdateJob::AdjustParentCount { task_ids, delta } => {
ctx.for_each_task_meta(task_ids, "AdjustParentCount", |mut task, _ctx| {
task.update_and_get_parent_count(delta);
});
}
AggregationUpdateJob::AdjustTransientRefCount { task_ids, delta } => {
ctx.for_each_task_meta(
task_ids,
"AdjustTransientRefCount",
|mut task, _ctx| {
task.update_and_get_transient_ref_count(delta);
},
);
}
AggregationUpdateJob::DecreaseActiveCount { task } => { AggregationUpdateJob::DecreaseActiveCount { task } => {
self.decrease_active_count(ctx, task); self.decrease_active_count(ctx, task);
} }
......
...@@ -96,10 +96,31 @@ impl CleanupOldEdgesOperation { ...@@ -96,10 +96,31 @@ impl CleanupOldEdgesOperation {
_ => true, _ => true,
}); });
let mut task = ctx.task(task_id, TaskDataCategory::All); let mut task = ctx.task(task_id, TaskDataCategory::All);
for task_id in children.iter() {
task.remove_children(task_id); let mut removed_persistent_children =
SmallVec::<[TaskId; 4]>::new();
for child_id in children.iter() {
if task.remove_children(child_id) && !child_id.is_transient() {
removed_persistent_children.push(*child_id);
}
}
// Each removed persistent child loses a parent.
if !removed_persistent_children.is_empty() {
let job = if task_id.is_transient() {
AggregationUpdateJob::AdjustTransientRefCount {
task_ids: removed_persistent_children,
delta: -1,
}
} else {
AggregationUpdateJob::AdjustParentCount {
task_ids: removed_persistent_children,
delta: -1,
}
};
queue.push(job);
} }
if is_aggregating_node(get_aggregation_number(&task)) { if is_aggregating_node(get_aggregation_number(&task)) {
drop(task);
queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers { queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers {
upper_id: task_id, upper_id: task_id,
lost_follower_ids: children, lost_follower_ids: children,
......
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use smallvec::SmallVec; use smallvec::SmallVec;
#[cfg(feature = "task_dirty_cause")]
use turbo_tasks::TaskDirtyCause;
use turbo_tasks::{ use turbo_tasks::{
TaskId, TaskId,
scope_bounded::scope_bounded, scope_bounded::scope_bounded,
util::{good_chunk_size, into_chunks}, util::{good_chunk_size, into_chunks},
}; };
use crate::backend::operation::{ use crate::backend::{
AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext, Operation, operation::{
TaskGuard, aggregation_update::InnerOfUppersHasNewFollowersJob, get_aggregation_number, AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext,
get_uppers, is_aggregating_node, Operation, TaskGuard, aggregation_update::InnerOfUppersHasNewFollowersJob,
get_aggregation_number, get_uppers, invalidate::make_task_dirty_internal,
is_aggregating_node,
},
storage_schema::TaskStorageAccessors,
}; };
pub fn connect_children( pub fn connect_children(
...@@ -52,6 +58,41 @@ pub fn connect_children( ...@@ -52,6 +58,41 @@ pub fn connect_children(
let mut queue = AggregationUpdateQueue::new(); let mut queue = AggregationUpdateQueue::new();
// Single pass over the newly-connected children, two things per child under one guard:
//
// 1. Bump the child-side parent reference count. it is important to do this before any
// suspend points persistence/GC cannot run
//
// 2. Make any child that has not produced output yet dirty, so it gets scheduled and
// computes.
let parent_is_transient = parent_task_id.is_transient();
ctx.for_each_task_all(
new_follower_ids.iter().copied(),
"connect_children parent_count + dirty",
|mut child, ctx| {
// Bump before `make_task_dirty_internal`, which consumes the guard.
if !child.id().is_transient() {
if parent_is_transient {
child.update_and_get_transient_ref_count(1);
} else {
child.update_and_get_parent_count(1);
}
}
if !child.has_output() {
let child_id = child.id();
make_task_dirty_internal(
child,
child_id,
false,
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::InitialDirty,
&mut queue,
ctx,
);
}
},
);
if let Some(upper_ids) = upper_ids { if let Some(upper_ids) = upper_ids {
// We need to add new followers when there are upper ids as the parent is a leaf node // We need to add new followers when there are upper ids as the parent is a leaf node
// and new children are new followers. // and new children are new followers.
...@@ -133,10 +174,10 @@ pub fn connect_children( ...@@ -133,10 +174,10 @@ pub fn connect_children(
// This avoids long pauses of more than 30µs * 10k = 300ms. // This avoids long pauses of more than 30µs * 10k = 300ms.
// We don't want to parallelize too eagerly as spawning tasks and the temporary allocations have // We don't want to parallelize too eagerly as spawning tasks and the temporary allocations have
// a cost as well. // a cost as well.
const CONNECT_CHILDREN_PARALLIZATION_THRESHOLD: usize = 10000; const CONNECT_CHILDREN_PARALLELIZATION_THRESHOLD: usize = 10000;
let len = new_follower_ids.len(); let len = new_follower_ids.len();
if len >= CONNECT_CHILDREN_PARALLIZATION_THRESHOLD { if len >= CONNECT_CHILDREN_PARALLELIZATION_THRESHOLD {
let new_follower_ids = new_follower_ids.into_vec(); let new_follower_ids = new_follower_ids.into_vec();
let chunk_size = good_chunk_size(len); let chunk_size = good_chunk_size(len);
let _ = scope_bounded(len.div_ceil(chunk_size), |scope| { let _ = scope_bounded(len.div_ceil(chunk_size), |scope| {
......
...@@ -1270,6 +1270,30 @@ pub trait TaskGuard: Debug + TaskStorageAccessors { ...@@ -1270,6 +1270,30 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
new_value new_value
} }
/// Adjust the count of persistent parents referencing this task by `delta`
///
/// Panics on underflow/overflow
fn update_and_get_parent_count(&mut self, delta: i32) -> u32 {
let current = self.get_parent_count().copied().unwrap_or(0);
let new_value = current
.checked_add_signed(delta)
.expect("parent_count underflow: decremented below the number of persistent parents");
self.set_parent_count(new_value);
new_value
}
/// Like [`Self::update_and_get_parent_count`], but for the transient (session-only) parent
/// reference count
/// Panics on underflow/overflow.
fn update_and_get_transient_ref_count(&mut self, delta: i32) -> u32 {
let current = self.get_transient_ref_count().copied().unwrap_or(0);
let new_value = current
.checked_add_signed(delta)
.expect("transient_ref_count underflow");
self.set_transient_ref_count(new_value);
new_value
}
fn invalidate_serialization(&mut self); fn invalidate_serialization(&mut self);
/// Determine which tasks to prefetch for a task. /// Determine which tasks to prefetch for a task.
/// Only returns Some once per task. /// Only returns Some once per task.
...@@ -1657,6 +1681,7 @@ impl TaskStorageAccessors for TaskGuardImpl<'_> { ...@@ -1657,6 +1681,7 @@ impl TaskStorageAccessors for TaskGuardImpl<'_> {
self.task.undo_track_modification(outcome); self.task.undo_track_modification(outcome);
} }
#[track_caller]
fn check_access(&self, category: crate::backend::storage::SpecificTaskDataCategory) { fn check_access(&self, category: crate::backend::storage::SpecificTaskDataCategory) {
self.check_access(category); self.check_access(category);
} }
......
...@@ -518,6 +518,14 @@ impl Storage { ...@@ -518,6 +518,14 @@ impl Storage {
} }
} }
/// Read-only access to an already resident task. Returns `None` if the task isnt in memory
/// resident. The closure runs while a shard read lock is held, so it must be cheap and must
/// not re-enter the map.
pub fn with_task<R>(&self, key: TaskId, f: impl FnOnce(&TaskStorage) -> R) -> Option<R> {
let task = self.map.get(&key)?;
Some(f(task.value()))
}
pub fn access_pair_mut( pub fn access_pair_mut(
&self, &self,
key1: TaskId, key1: TaskId,
......
...@@ -84,7 +84,7 @@ struct TaskStorageSchema { ...@@ -84,7 +84,7 @@ struct TaskStorageSchema {
filter_transient, filter_transient,
drop_on_completion_if_immutable drop_on_completion_if_immutable
)] )]
output_dependent: AutoSet<TaskId, 6>, output_dependent: AutoSet<TaskId, 4>,
/// The task's output value. /// The task's output value.
/// Filtered during serialization to skip transient outputs (referencing transient tasks). /// Filtered during serialization to skip transient outputs (referencing transient tasks).
...@@ -142,6 +142,19 @@ struct TaskStorageSchema { ...@@ -142,6 +142,19 @@ struct TaskStorageSchema {
#[field(storage = "counter_map", category = "transient")] #[field(storage = "counter_map", category = "transient")]
aggregated_current_session_clean_containers: CounterMap<TaskId, i32, 3>, aggregated_current_session_clean_containers: CounterMap<TaskId, i32, 3>,
/// Number of **persistent** parent tasks that list this task in their `children` set.
#[field(storage = "direct", category = "meta", inline, default)]
parent_count: u32,
/// Number of **transient** in-session references to this task. Two sources bump it:
/// - a **transient parent** connecting this task as a child (transient parents are never
/// persisted, so their edge can't count toward the durable `parent_count`), and
/// - a **detached handle** that holds this task's `OperationVc` outside the tracked graph
/// (e.g. a `DetachedVc` passed to JS across the NAPI boundary), which pins it like a GC
/// root.
#[field(storage = "direct", category = "transient", inline, default)]
transient_ref_count: u32,
// ========================================================================= // =========================================================================
// FLAGS (meta) - Boolean flags stored in TaskFlags bitfield // FLAGS (meta) - Boolean flags stored in TaskFlags bitfield
// Persisted flags come first, then transient flags. // Persisted flags come first, then transient flags.
...@@ -829,6 +842,17 @@ impl TaskStorage { ...@@ -829,6 +842,17 @@ impl TaskStorage {
aggregated_dirty_containers: self.aggregated_dirty_containers().map_or(0, |c| c.len()), aggregated_dirty_containers: self.aggregated_dirty_containers().map_or(0, |c| c.len()),
} }
} }
/// The number of persistent parents referencing this task (0 when the field is absent).
pub fn gc_parent_count(&self) -> u32 {
self.get_parent_count().copied().unwrap_or(0)
}
/// The number of transient (session-only) references to this task (0 when absent). See the
/// `transient_ref_count` field for what counts as one.
pub fn gc_transient_ref_count(&self) -> u32 {
self.get_transient_ref_count().copied().unwrap_or(0)
}
} }
/// Counts for aggregation tree and collectibles fields. /// Counts for aggregation tree and collectibles fields.
...@@ -1178,6 +1202,9 @@ mod tests { ...@@ -1178,6 +1202,9 @@ mod tests {
original original
.aggregated_dirty_containers_mut() .aggregated_dirty_containers_mut()
.insert(TaskId::new(50).unwrap(), 2); .insert(TaskId::new(50).unwrap(), 2);
original.set_parent_count(3);
// Transient ref count (should NOT be serialized).
original.set_transient_ref_count(9);
// Set transient flag (should NOT be serialized) // Set transient flag (should NOT be serialized)
original.flags.set_current_session_clean(true); original.flags.set_current_session_clean(true);
...@@ -1223,6 +1250,9 @@ mod tests { ...@@ -1223,6 +1250,9 @@ mod tests {
decoded.aggregated_dirty_containers(), decoded.aggregated_dirty_containers(),
original.aggregated_dirty_containers() original.aggregated_dirty_containers()
); );
assert_eq!(decoded.get_parent_count(), Some(&3));
// Transient parent count is NOT serialized; it stays at its default (absent == 0).
assert_eq!(decoded.get_transient_ref_count(), None);
// Note: invalidator and immutable are data category flags, not meta // Note: invalidator and immutable are data category flags, not meta
// They should NOT have changed during meta encode/decode // They should NOT have changed during meta encode/decode
...@@ -1720,13 +1750,13 @@ mod tests { ...@@ -1720,13 +1750,13 @@ mod tests {
assert_eq!( assert_eq!(
size_of::<TaskStorage>(), size_of::<TaskStorage>(),
128, 128,
"TaskStorage size changed! Run print_schema_sizes and update this test." "TaskStorage size changed! Update this test."
); );
// `LazyField` is 40 B = 32 B largest payload + 8 B discriminant. // `LazyField` is 40 B = 32 B largest payload + 8 B discriminant.
assert_eq!( assert_eq!(
size_of::<LazyField>(), size_of::<LazyField>(),
40, 40,
"LazyField size changed! Run print_schema_sizes and update this test." "LazyField size changed! Update this test."
); );
} }
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#![feature(arbitrary_self_types_pointers)] #![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this #![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
mod util;
use std::sync::{ use std::sync::{
Arc, Arc,
atomic::{AtomicBool, AtomicU64, Ordering}, atomic::{AtomicBool, AtomicU64, Ordering},
...@@ -9,62 +11,10 @@ use std::sync::{ ...@@ -9,62 +11,10 @@ use std::sync::{
use anyhow::Result; use anyhow::Result;
use turbo_tasks::{ use turbo_tasks::{
ResolvedVc, State, TurboTasks, Vc, unmark_top_level_task_may_leak_eventually_consistent_state, ResolvedVc, State, Vc, unmark_top_level_task_may_leak_eventually_consistent_state,
};
use turbo_tasks_backend::{
BackendOptions, BackingStorageOptions, EvictionMode, GitVersionInfo, TurboTasksBackend,
}; };
/// Creates a fresh per-call persistence directory in the OS temp dir, with the test `name` as a use crate::util::{create_tt, create_tt_with_workers};
/// prefix so a leaked directory from a failed run is identifiable. The unique suffix from
/// `tempfile` lets repeated or concurrent runs coexist without trampling each other's database.
///
/// The returned [`tempfile::TempDir`] cleans up its contents on drop, so callers should keep it
/// alive at least until the `TurboTasks` it backs has finished shutting down (so the final snapshot
/// can flush to disk).
fn create_test_persistence_dir(name: &str) -> tempfile::TempDir {
tempfile::Builder::new()
.prefix(&format!("{name}-"))
.tempdir()
.unwrap()
}
fn create_tt_with_workers(
name: &str,
num_workers: usize,
) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
let dir = create_test_persistence_dir(name);
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
num_workers: Some(num_workers),
small_preallocation: true,
// Avoid racing with the background snapshot loop; the test drives
// snapshot_and_evict_for_testing manually.
storage_mode: Some(turbo_tasks_backend::StorageMode::ReadWriteOnShutdown),
eviction_mode: EvictionMode::Full,
..Default::default()
},
turbo_tasks_backend::turbo_backing_storage(
dir.path(),
&GitVersionInfo {
describe: "test-unversioned",
dirty: false,
},
BackingStorageOptions {
is_short_session: true,
skip_compaction: true,
..Default::default()
},
)
.unwrap()
.0,
));
(tt, dir)
}
fn create_tt(name: &str) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
create_tt_with_workers(name, 2)
}
/// Verify that after eviction, task re-execution produces correct results. /// Verify that after eviction, task re-execution produces correct results.
/// This tests the snapshot → evict → invalidate → restore → re-execute cycle. /// This tests the snapshot → evict → invalidate → restore → re-execute cycle.
......
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
mod util;
use anyhow::Result;
use turbo_tasks::{
ResolvedVc, State, TaskId, Vc, unmark_top_level_task_may_leak_eventually_consistent_state,
};
use crate::util::create_tt;
/// The `TaskId` backing a resolved `Vc` (its `TaskOutput` node).
fn task_id_of<T>(vc: Vc<T>) -> TaskId {
Vc::into_raw(vc)
.try_get_task_id()
.expect("a resolved Vc should be backed by a task")
}
#[turbo_tasks::value(transparent)]
struct Selector(State<bool>);
#[turbo_tasks::function(operation, root)]
fn create_selector(initial: bool) -> Vc<Selector> {
Selector(State::new(initial)).cell()
}
#[turbo_tasks::function]
fn leaf(n: u32) -> Vc<u32> {
Vc::cell(n)
}
#[turbo_tasks::function]
async fn branch_a() -> Result<Vc<u32>> {
Ok(Vc::cell(1 + *leaf(10).await?))
}
#[turbo_tasks::function]
async fn branch_b() -> Result<Vc<u32>> {
Ok(Vc::cell(2 + *leaf(20).await?))
}
/// Reads exactly one branch depending on the selector; flipping it re-executes and disconnects the
/// previously-read branch (and its subtree), which should drop that branch's `parent_count` to 0.
#[turbo_tasks::function(operation, root)]
async fn select(selector: ResolvedVc<Selector>) -> Result<Vc<u32>> {
let use_b = *selector.await?.get();
let value = if use_b {
*branch_b().await?
} else {
*branch_a().await?
};
Ok(Vc::cell(value))
}
/// `parent_count` must track the number of persistent parents, incremented when a parent connects a
/// child and decremented when it disconnects it.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn parent_count_tracks_connect_and_disconnect() {
let (tt, _persistence_dir) = create_tt("parent_count_tracks_connect_and_disconnect");
let tt2 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
unmark_top_level_task_may_leak_eventually_consistent_state();
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
let selector = selector_op.read_strongly_consistent().await?;
// Build the connected graph: select -> branch_a -> leaf(10).
let output = select(selector_vc);
assert_eq!(*output.read_strongly_consistent().await?, 11);
// Resolve the branch task ids (calling the cached functions returns the same tasks).
let branch_a_id = task_id_of(branch_a().resolve().await?);
let leaf10_id = task_id_of(leaf(10).resolve().await?);
let branch_b_id = task_id_of(branch_b().resolve().await?);
assert_eq!(
tt2.backend().parent_count_for_testing(branch_a_id),
1,
"branch_a has one parent (select) while connected"
);
assert_eq!(
tt2.backend().parent_count_for_testing(leaf10_id),
1,
"leaf(10) has one parent (branch_a) while connected"
);
// Flip: select re-executes reading branch_b, disconnecting branch_a + leaf(10).
selector.set(true);
assert_eq!(*output.read_strongly_consistent().await?, 22);
assert_eq!(
tt2.backend().parent_count_for_testing(branch_a_id),
0,
"branch_a lost its only parent (select) after the flip"
);
// leaf(10) is still listed as a child by the (now-garbage) branch_a — nothing re-executed
// branch_a to drop that edge — so its parent_count stays 1. It only drops to 0 once
// branch_a is torn down by the GC cascade.
assert_eq!(
tt2.backend().parent_count_for_testing(leaf10_id),
1,
"leaf(10)'s parent branch_a still lists it (branch_a is garbage but not yet torn down)"
);
assert_eq!(
tt2.backend().parent_count_for_testing(branch_b_id),
1,
"branch_b gained a parent (select) after the flip"
);
// Flip back: branch_a reconnects (recomputed), branch_b disconnects.
selector.set(false);
assert_eq!(*output.read_strongly_consistent().await?, 11);
assert_eq!(
tt2.backend().parent_count_for_testing(branch_b_id),
0,
"branch_b lost its parent after flipping back"
);
anyhow::Ok(())
})
.await;
tt.stop_and_wait().await;
result.unwrap();
}
/// Re-executes when the selector flips while keeping the same child edge.
#[turbo_tasks::function(operation, root)]
async fn stable_child_parent(selector: ResolvedVc<Selector>) -> Result<Vc<u32>> {
// Read the selector so we re-execute when it flips...
let bump = if *selector.await?.get() { 100 } else { 0 };
// ...but always connect the SAME child regardless.
let child = *leaf(30).await?;
Ok(Vc::cell(bump + child))
}
/// Re-validation must not double-count. When a parent re-executes and connects a child it was
/// *already* connected to (the child is still in its persistent `children` set), the child must NOT
/// gain a second `parent_count`: `connect_children` only bumps the *genuinely-new* children (those
/// not already present in `children`), so the count stays at exactly 1 across arbitrarily many
/// re-executions that keep the edge.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn parent_count_not_double_counted_on_revalidation() {
let (tt, _persistence_dir) = create_tt("parent_count_not_double_counted_on_revalidation");
let tt2 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
unmark_top_level_task_may_leak_eventually_consistent_state();
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
let selector = selector_op.read_strongly_consistent().await?;
let output = stable_child_parent(selector_vc);
assert_eq!(*output.read_strongly_consistent().await?, 30);
let leaf30_id = task_id_of(leaf(30).resolve().await?);
assert_eq!(
tt2.backend().parent_count_for_testing(leaf30_id),
1,
"leaf(30) has exactly one parent after the first execution"
);
// Re-execute the parent several times; it keeps the same child edge each time.
for i in 0..5 {
selector.set(i % 2 == 0);
output.read_strongly_consistent().await?;
assert_eq!(
tt2.backend().parent_count_for_testing(leaf30_id),
1,
"leaf(30)'s parent_count must stay 1 across re-validation, grew at iteration {i}"
);
}
anyhow::Ok(())
})
.await;
tt.stop_and_wait().await;
result.unwrap();
}
//! Shared setup for tests that need a real on-disk persistent backend and direct access to
//! [`TurboTasksBackend`] test hooks (`snapshot_and_evict_for_testing`, `parent_count_for_testing`,
//! …).
//!
//! These tests can't use `turbo_tasks_testing::register!`: that harness hands back an
//! `Arc<dyn TurboTasksApi>`, which erases the concrete backend type and makes `tt.backend()`
//! unreachable. It also owns the session loop (re-running the body and comparing results), whereas
//! these tests need to drive snapshots — and sometimes a DB reopen — on their own schedule.
use std::{path::Path, sync::Arc};
use turbo_tasks::TurboTasks;
use turbo_tasks_backend::{
BackendOptions, BackingStorageOptions, EvictionMode, GitVersionInfo, TurboTasksBackend,
};
/// Opens a backend rooted at `path`.
///
/// Reusing the same `path` (after the previous backend has been stopped) reopens the persisted
/// database, which is how a test can assert that state survives a restart.
fn open_tt_at(path: &Path, num_workers: usize) -> Arc<TurboTasks<TurboTasksBackend>> {
TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
num_workers: Some(num_workers),
small_preallocation: true,
// Avoid racing with the background snapshot loop; these tests drive
// snapshot_and_evict_for_testing manually.
storage_mode: Some(turbo_tasks_backend::StorageMode::ReadWriteOnShutdown),
eviction_mode: EvictionMode::Full,
..Default::default()
},
turbo_tasks_backend::turbo_backing_storage(
path,
&GitVersionInfo {
describe: "test-unversioned",
dirty: false,
},
BackingStorageOptions {
is_short_session: true,
skip_compaction: true,
..Default::default()
},
)
.unwrap()
.0,
))
}
/// A fresh persistent backend in its own temp directory, with `num_workers` workers.
pub fn create_tt_with_workers(
name: &str,
num_workers: usize,
) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
let dir = tempfile::Builder::new()
.prefix(&format!("{name}-"))
.tempdir()
.unwrap();
let tt = open_tt_at(dir.path(), num_workers);
(tt, dir)
}
/// A fresh persistent backend in its own temp directory, with the default worker count.
pub fn create_tt(name: &str) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
create_tt_with_workers(name, 2)
}
...@@ -1676,6 +1676,7 @@ fn generate_task_storage_accessors_trait(grouped_fields: &GroupedFields) -> Toke ...@@ -1676,6 +1676,7 @@ fn generate_task_storage_accessors_trait(grouped_fields: &GroupedFields) -> Toke
#[doc = "- `Data` or `Meta`: Checks that the task was accessed with that category"] #[doc = "- `Data` or `Meta`: Checks that the task was accessed with that category"]
#[doc = ""] #[doc = ""]
#[doc = "Implementors should check that the provided category matches how the task was accessed."] #[doc = "Implementors should check that the provided category matches how the task was accessed."]
#[track_caller]
fn check_access(&self, category: crate::backend::storage::SpecificTaskDataCategory); fn check_access(&self, category: crate::backend::storage::SpecificTaskDataCategory);
#[doc = "Shrink all collection fields to fit their current contents."] #[doc = "Shrink all collection fields to fit their current contents."]
...@@ -3087,9 +3088,13 @@ fn generate_drop_method(grouped_fields: &GroupedFields) -> TokenStream { ...@@ -3087,9 +3088,13 @@ fn generate_drop_method(grouped_fields: &GroupedFields) -> TokenStream {
StorageType::AutoMap | StorageType::AutoSet | StorageType::CounterMap => quote! { StorageType::AutoMap | StorageType::AutoSet | StorageType::CounterMap => quote! {
self.#field_name.is_empty() self.#field_name.is_empty()
}, },
StorageType::Direct => quote! { StorageType::Direct => {
self.#field_name == Default::default() // Fully-qualified `Default` so we don't have type-inference ambiguity.
}, let field_type = &field.field_type;
quote! {
self.#field_name == <#field_type as Default>::default()
}
}
StorageType::Flag => unreachable!(), StorageType::Flag => unreachable!(),
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment