Unverified Commit 929f1f5a authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add backend-owned state slots to turbo-tasks

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent dc7bf6e6
...@@ -45,8 +45,9 @@ use tracing::{Instrument, field::Empty}; ...@@ -45,8 +45,9 @@ use tracing::{Instrument, field::Empty};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Completion, Completions, FxIndexMap, InvalidationReason, NonLocalValue, OperationValue, Completion, Completions, FxIndexMap, InvalidationReason, NonLocalValue, OperationValue,
OperationVc, ReadRef, ResolvedVc, State, TransientInstance, TryFlatJoinIterExt, TryJoinIterExt, OperationVc, ReadRef, ResolvedVc, StateSlot, TransientInstance, TryFlatJoinIterExt,
Vc, debug::ValueDebugFormat, fxindexmap, message_queue::TraceEvent, turbo_tasks, TryJoinIterExt, TurboTasksState, Vc, debug::ValueDebugFormat, fxindexmap,
message_queue::TraceEvent, turbo_tasks,
}; };
use turbo_tasks_env::{EnvMap, ProcessEnv}; use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
...@@ -420,12 +421,20 @@ struct ProjectFileSystemState { ...@@ -420,12 +421,20 @@ struct ProjectFileSystemState {
output_file_system: OperationVc<DiskFileSystem>, output_file_system: OperationVc<DiskFileSystem>,
} }
#[turbo_tasks::state]
static PROJECT_OPTIONS: StateSlot<Option<ProjectOptions>> = StateSlot::new();
#[turbo_tasks::state]
static PROJECT_FILE_SYSTEMS: StateSlot<Option<ProjectFileSystemState>> = StateSlot::new();
#[turbo_tasks::state]
static PROJECT_ADDITIONAL_ROOTS: StateSlot<Vec<(RcStr, AdditionalDiskFileSystem)>> =
StateSlot::new();
#[turbo_tasks::value(evict = "never", eq = "manual", cell = "new")] #[turbo_tasks::value(evict = "never", eq = "manual", cell = "new")]
pub struct ProjectContainer { pub struct ProjectContainer {
name: RcStr, name: RcStr,
options_state: State<Option<ProjectOptions>>, options_state: TurboTasksState<Option<ProjectOptions>>,
file_systems_state: State<Option<ProjectFileSystemState>>, file_systems_state: TurboTasksState<Option<ProjectFileSystemState>>,
additional_roots_state: State<Vec<(RcStr, AdditionalDiskFileSystem)>>, additional_roots_state: TurboTasksState<Vec<(RcStr, AdditionalDiskFileSystem)>>,
#[turbo_tasks(debug_ignore, unsafe_ignore)] #[turbo_tasks(debug_ignore, unsafe_ignore)]
#[bincode(skip)] #[bincode(skip)]
fs_map_init_lock: tokio::sync::Mutex<()>, fs_map_init_lock: tokio::sync::Mutex<()>,
...@@ -445,9 +454,9 @@ impl ProjectContainer { ...@@ -445,9 +454,9 @@ impl ProjectContainer {
} else { } else {
None None
}, },
options_state: State::new(None), options_state: PROJECT_OPTIONS.for_current_task(None),
file_systems_state: State::new(None), file_systems_state: PROJECT_FILE_SYSTEMS.for_current_task(None),
additional_roots_state: State::new(Vec::new()), additional_roots_state: PROJECT_ADDITIONAL_ROOTS.for_current_task(Vec::new()),
fs_map_init_lock: tokio::sync::Mutex::new(()), fs_map_init_lock: tokio::sync::Mutex::new(()),
} }
.cell()) .cell())
...@@ -3044,3 +3053,41 @@ fn all_assets_from_entries_operation( ...@@ -3044,3 +3053,41 @@ fn all_assets_from_entries_operation(
let assets = operation.connect(); let assets = operation.connect();
Ok(all_assets_from_entries(assets)) Ok(all_assets_from_entries(assets))
} }
#[cfg(test)]
mod state_slot_tests {
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use crate::project::ProjectContainer;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn project_container_uses_independent_backend_state_slots() {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async {
let container = ProjectContainer::new_operation("state-slot-test".into(), false)
.read_strongly_consistent()
.await?;
assert!(container.options_state.get_untracked().is_none());
assert!(container.file_systems_state.get_untracked().is_none());
assert!(container.additional_roots_state.get_untracked().is_empty());
assert_ne!(
container.options_state.key(),
container.file_systems_state.key()
);
assert_ne!(
container.options_state.key(),
container.additional_roots_state.key()
);
assert_ne!(
container.file_systems_state.key(),
container.additional_roots_state.key()
);
anyhow::Ok(())
})
.await
.unwrap();
}
}
...@@ -19,13 +19,18 @@ ...@@ -19,13 +19,18 @@
use std::{ use std::{
fmt::Display, fmt::Display,
ops::ControlFlow, ops::ControlFlow,
sync::atomic::{AtomicBool, Ordering}, sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::{Duration, Instant, SystemTime, UNIX_EPOCH}, time::{Duration, Instant, SystemTime, UNIX_EPOCH},
}; };
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use turbo_tasks::{TaskId, TurboTasks, scope_unbounded::scope_unbounded_with}; use turbo_tasks::{
StateKey, StateOwner, TaskId, TurboTasks, scope_unbounded::scope_unbounded_with,
};
use crate::{ use crate::{
backend::{ backend::{
...@@ -132,6 +137,9 @@ pub struct GcPassResult { ...@@ -132,6 +137,9 @@ pub struct GcPassResult {
/// Dependents of collected tasks whose forward edge was scrubbed, deferred from the main GC /// Dependents of collected tasks whose forward edge was scrubbed, deferred from the main GC
/// loop. Dirtying propagates through the aggregation graph, so it must not race the cascade. /// loop. Dirtying propagates through the aggregation graph, so it must not race the cascade.
deferred_dirty_dependents: FxHashSet<TaskId>, deferred_dirty_dependents: FxHashSet<TaskId>,
/// State slots removed along with their creator. Clear surviving readers
/// only after parallel task collection is quiescent.
deleted_state_dependents: Vec<(StateKey, Vec<TaskId>)>,
/// The gc loop was interrupted by competing work. /// The gc loop was interrupted by competing work.
pub interrupted: bool, pub interrupted: bool,
} }
...@@ -188,6 +196,8 @@ impl GcPassResult { ...@@ -188,6 +196,8 @@ impl GcPassResult {
} }
self.deferred_dirty_dependents self.deferred_dirty_dependents
.extend(other.deferred_dirty_dependents); .extend(other.deferred_dirty_dependents);
self.deleted_state_dependents
.extend(other.deleted_state_dependents);
self.interrupted |= other.interrupted; self.interrupted |= other.interrupted;
self self
} }
...@@ -289,6 +299,20 @@ impl TurboTasksBackend { ...@@ -289,6 +299,20 @@ impl TurboTasksBackend {
let _ = task.track_modification(SpecificTaskDataCategory::Meta, "gc_deleted"); let _ = task.track_modification(SpecificTaskDataCategory::Meta, "gc_deleted");
} }
drop(task); // drop the lock so edge cleanup can run drop(task); // drop the lock so edge cleanup can run
{
let mut states = self.states.lock();
states.retain(|key, state| {
if key.owner == StateOwner::Task(task_id) {
result
.deleted_state_dependents
.push((key.clone(), state.dependents.iter().copied().collect()));
self.state_dirty.store(true, Ordering::Release);
false
} else {
true
}
});
}
stats.collected += 1; stats.collected += 1;
stats.edges_deleted += old_edges.len(); stats.edges_deleted += old_edges.len();
// If we happened to delete a known root at this point record it so we can reconcile // If we happened to delete a known root at this point record it so we can reconcile
...@@ -327,6 +351,59 @@ impl TurboTasksBackend { ...@@ -327,6 +351,59 @@ impl TurboTasksBackend {
while !queue.process(&mut ctx) {} while !queue.process(&mut ctx) {}
} }
// Named states age out like task roots, but their owner is independent
// of a task. The timestamp is persisted with each state so a restart
// does not reset the GC grace period.
{
let pins = self.named_state_pins.lock();
let mut states = self.states.lock();
states.retain(|key, state| {
let StateOwner::Named(name) = &key.owner else {
return true;
};
if pins.contains_key(name) {
if state.unrooted_since.take().is_some() {
self.state_dirty.store(true, Ordering::Release);
}
return true;
}
if let Some(since) = state.unrooted_since
&& now.saturating_sub(since) > self.gc_root_ttl.as_millis() as u64
{
result
.deleted_state_dependents
.push((key.clone(), state.dependents.iter().copied().collect()));
self.state_dirty.store(true, Ordering::Release);
return false;
}
if state.unrooted_since.is_none() {
state.unrooted_since = Some(now);
self.state_dirty.store(true, Ordering::Release);
}
true
});
}
// State slots owned by collected tasks disappeared from the state map.
// Drop surviving readers' forward edges before dirtying, just like
// reverse cell edges in CleanupOldEdgesOperation.
let removed_states = std::mem::take(&mut result.deleted_state_dependents);
if !removed_states.is_empty() {
let noop_collector = |_task_id| {};
let mut ctx = ExecuteContext::new_for_gc(self, turbo_tasks, phase, &noop_collector);
for (key, readers) in removed_states {
let shared_key = Arc::new(key);
for reader in readers {
if let Some(mut task) = ctx.try_task(reader, TaskDataCategory::Data)
&& task.remove_state_dependencies(&shared_key)
{
task.remove_outdated_state_dependencies(&shared_key);
result.deferred_dirty_dependents.insert(reader);
}
}
}
}
// Dirty the dependents whose edges were scrubbed. After the rebalance above so the // Dirty the dependents whose edges were scrubbed. After the rebalance above so the
// aggregation graph is settled, and before the root scan below because dirtying can change // aggregation graph is settled, and before the root scan below because dirtying can change
// activeness and therefore rootness. // activeness and therefore rootness.
...@@ -459,9 +536,11 @@ impl TurboTasksBackend { ...@@ -459,9 +536,11 @@ impl TurboTasksBackend {
// Persist the roots map this pass produced. Some tests query the roots set and GC itself // Persist the roots map this pass produced. Some tests query the roots set and GC itself
// does as well, this ensures it is available to the next cycle. // does as well, this ensures it is available to the next cycle.
if let Some(roots) = roots if let Some(roots) = roots
&& let Err(err) = self && let Err(err) = self.backing_storage.save_snapshot(
.backing_storage Some(roots),
.save_snapshot(Some(roots), Vec::<Vec<SnapshotItem>>::new()) Vec::<Vec<SnapshotItem>>::new(),
None,
)
{ {
panic!("gc_for_testing: failed to persist GC roots: {err:?}"); panic!("gc_for_testing: failed to persist GC roots: {err:?}");
} }
......
...@@ -3,7 +3,7 @@ use std::mem::take; ...@@ -3,7 +3,7 @@ use std::mem::take;
use auto_hash_map::AutoSet; use auto_hash_map::AutoSet;
use rustc_hash::{FxBuildHasher, FxHashSet}; use rustc_hash::{FxBuildHasher, FxHashSet};
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_tasks::TaskId; use turbo_tasks::{StateKey, TaskId};
use crate::{ use crate::{
backend::{ backend::{
...@@ -26,6 +26,7 @@ pub enum OutdatedEdge { ...@@ -26,6 +26,7 @@ pub enum OutdatedEdge {
CellDependency(CellRef), CellDependency(CellRef),
HashedCellDependency(CellRef, u64), HashedCellDependency(CellRef, u64),
OutputDependency(TaskId), OutputDependency(TaskId),
StateDependency(StateKey),
CollectiblesDependency(CollectiblesRef), CollectiblesDependency(CollectiblesRef),
/// Reverse cell and output edges: some other task reads a cell of the task being deleted by GC. /// Reverse cell and output edges: some other task reads a cell of the task being deleted by GC.
/// Not modeled as a reversed `CellDependency` since the cleanup is assymetric, one side is /// Not modeled as a reversed `CellDependency` since the cleanup is assymetric, one side is
...@@ -51,6 +52,10 @@ pub fn capture_all_edges(task: &impl TaskStorageAccessors) -> Vec<OutdatedEdge> ...@@ -51,6 +52,10 @@ pub fn capture_all_edges(task: &impl TaskStorageAccessors) -> Vec<OutdatedEdge>
task.iter_cell_dependencies_hashed() task.iter_cell_dependencies_hashed()
.map(|(r, k)| OutdatedEdge::HashedCellDependency(r, k)), .map(|(r, k)| OutdatedEdge::HashedCellDependency(r, k)),
); );
old_edges.extend(
task.iter_state_dependencies()
.map(|key| OutdatedEdge::StateDependency((*key).clone())),
);
old_edges.extend( old_edges.extend(
task.iter_collectibles_dependencies() task.iter_collectibles_dependencies()
.map(OutdatedEdge::CollectiblesDependency), .map(OutdatedEdge::CollectiblesDependency),
...@@ -289,6 +294,9 @@ fn cleanup_old_edges_inner( ...@@ -289,6 +294,9 @@ fn cleanup_old_edges_inner(
task.remove_output_dependencies(&output_task_id); task.remove_output_dependencies(&output_task_id);
} }
} }
OutdatedEdge::StateDependency(ref key) => {
ctx.remove_state_dependency(task_id, key);
}
OutdatedEdge::CollectiblesDependency(CollectiblesRef { OutdatedEdge::CollectiblesDependency(CollectiblesRef {
collectible_type, collectible_type,
task: dependent_task_id, task: dependent_task_id,
......
...@@ -101,6 +101,38 @@ impl TaskLockCounter { ...@@ -101,6 +101,38 @@ impl TaskLockCounter {
} }
} }
#[cfg(debug_assertions)]
thread_local! {
static IN_STATE_MUTATION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
/// User serialization under the state lock must never re-enter the task graph.
pub(crate) struct StateMutationScope;
impl StateMutationScope {
pub(crate) fn enter() -> Self {
#[cfg(debug_assertions)]
IN_STATE_MUTATION.with(|flag| {
assert!(!flag.replace(true), "state mutation must not call back into turbo-tasks");
});
Self
}
fn assert_not_inside() {
#[cfg(debug_assertions)]
IN_STATE_MUTATION.with(|flag| {
assert!(!flag.get(), "state mutation must not call back into turbo-tasks");
});
}
}
impl Drop for StateMutationScope {
fn drop(&mut self) {
#[cfg(debug_assertions)]
IN_STATE_MUTATION.with(|flag| flag.set(false));
}
}
pub struct ExecuteContext<'e> { pub struct ExecuteContext<'e> {
backend: &'e TurboTasksBackend, backend: &'e TurboTasksBackend,
turbo_tasks: &'e TurboTasks<TurboTasksBackend>, turbo_tasks: &'e TurboTasks<TurboTasksBackend>,
...@@ -118,6 +150,7 @@ impl<'e> ExecuteContext<'e> { ...@@ -118,6 +150,7 @@ impl<'e> ExecuteContext<'e> {
backend: &'e TurboTasksBackend, backend: &'e TurboTasksBackend,
turbo_tasks: &'e TurboTasks<TurboTasksBackend>, turbo_tasks: &'e TurboTasks<TurboTasksBackend>,
) -> Self { ) -> Self {
StateMutationScope::assert_not_inside();
Self { Self {
backend, backend,
turbo_tasks, turbo_tasks,
...@@ -136,6 +169,7 @@ impl<'e> ExecuteContext<'e> { ...@@ -136,6 +169,7 @@ impl<'e> ExecuteContext<'e> {
turbo_tasks: &'e TurboTasks<TurboTasksBackend>, turbo_tasks: &'e TurboTasks<TurboTasksBackend>,
shutdown_guard: RwLockReadGuard<'e, bool>, shutdown_guard: RwLockReadGuard<'e, bool>,
) -> Self { ) -> Self {
StateMutationScope::assert_not_inside();
Self { Self {
backend, backend,
turbo_tasks, turbo_tasks,
...@@ -912,6 +946,28 @@ impl<'e> ExecuteContext<'e> { ...@@ -912,6 +946,28 @@ impl<'e> ExecuteContext<'e> {
/// This creates *storage for* an already-minted `TaskId`; it does not mint one. Compare /// This creates *storage for* an already-minted `TaskId`; it does not mint one. Compare
/// `TurboTasksBackend::get_or_create_task`, which takes a function and arguments and returns a /// `TurboTasksBackend::get_or_create_task`, which takes a function and arguments and returns a
/// `TaskId` (reusing one if the task is already cached). /// `TaskId` (reusing one if the task is already cached).
pub fn remove_state_dependency(&mut self, reader: TaskId, key: &turbo_tasks::StateKey) {
// State before reader, as for registration. The caller has released its
// task guard, so a re-read either revives the edge or observes removal.
let backend = self.backend;
let mut states = backend.states.lock();
let Some(mut task) = self.try_task(reader, TaskDataCategory::Data) else {
if let Some(state) = states.get_mut(key) {
state.dependents.remove(&reader);
backend.state_dirty.store(true, std::sync::atomic::Ordering::Release);
}
return;
};
let shared_key = Arc::new(key.clone());
if task.remove_outdated_state_dependencies(&shared_key) {
task.remove_state_dependencies(&shared_key);
if let Some(state) = states.get_mut(key) {
state.dependents.remove(&reader);
backend.state_dirty.store(true, std::sync::atomic::Ordering::Release);
}
}
}
pub fn task_or_create(&mut self, task_id: TaskId, category: TaskDataCategory) -> TaskGuard<'e> { pub fn task_or_create(&mut self, task_id: TaskId, category: TaskDataCategory) -> TaskGuard<'e> {
self.open_task(task_id, category, TaskAccess::MaybeCreate) self.open_task(task_id, category, TaskAccess::MaybeCreate)
.expect("a MaybeCreate open always yields a task") .expect("a MaybeCreate open always yields a task")
...@@ -1720,7 +1776,7 @@ pub use self::{ ...@@ -1720,7 +1776,7 @@ pub use self::{
}, },
connect_child::connect_child, connect_child::connect_child,
connect_children::connect_children, connect_children::connect_children,
invalidate::{invalidate, make_task_dirty_internal}, invalidate::{invalidate, make_task_dirty_internal, try_make_task_dirty},
leaf_distance_update::LeafDistanceUpdateQueue, leaf_distance_update::LeafDistanceUpdateQueue,
prepare_new_children::prepare_new_children, prepare_new_children::prepare_new_children,
update_cell::update_cell, update_cell::update_cell,
......
...@@ -25,7 +25,8 @@ use std::{ ...@@ -25,7 +25,8 @@ use std::{
use parking_lot::Mutex; use parking_lot::Mutex;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
use turbo_tasks::{ use turbo_tasks::{
CellId, SharedReference, TaskExecutionReason, TaskId, TraitTypeId, ValueTypeId, CellId, SharedReference, StateKey, StateOwner, TaskExecutionReason, TaskId, TraitTypeId,
ValueTypeId,
backend::{CachedTaskTypeArc, CellHash, TransientTaskType}, backend::{CachedTaskTypeArc, CellHash, TransientTaskType},
event::Event, event::Event,
task_storage, task_storage,
...@@ -284,6 +285,16 @@ struct TaskStorageSchema { ...@@ -284,6 +285,16 @@ struct TaskStorageSchema {
)] )]
cell_dependencies_hashed: AutoSet<(CellRef, u64), 1>, cell_dependencies_hashed: AutoSet<(CellRef, u64), 1>,
/// Backend-owned state slots this task has read.
#[field(
storage = "auto_set",
category = "data",
filter_transient,
shrink_on_completion,
drop_on_completion_if_immutable
)]
state_dependencies: AutoSet<Arc<StateKey>, 2>,
/// Collectibles this task depends on. /// Collectibles this task depends on.
#[field( #[field(
storage = "auto_set", storage = "auto_set",
...@@ -306,6 +317,10 @@ struct TaskStorageSchema { ...@@ -306,6 +317,10 @@ struct TaskStorageSchema {
#[field(storage = "auto_set", category = "transient", drop_on_completion)] #[field(storage = "auto_set", category = "transient", drop_on_completion)]
outdated_cell_dependencies_hashed: AutoSet<(CellRef, u64), 1>, outdated_cell_dependencies_hashed: AutoSet<(CellRef, u64), 1>,
/// State dependencies not yet re-read in this execution.
#[field(storage = "auto_set", category = "transient", shrink_on_completion)]
outdated_state_dependencies: AutoSet<Arc<StateKey>, 2>,
/// Outdated collectibles dependencies to be cleaned up (transient). /// Outdated collectibles dependencies to be cleaned up (transient).
#[field(storage = "auto_set", category = "transient", drop_on_completion)] #[field(storage = "auto_set", category = "transient", drop_on_completion)]
outdated_collectibles_dependencies: AutoSet<CollectiblesRef, 3>, outdated_collectibles_dependencies: AutoSet<CollectiblesRef, 3>,
...@@ -970,6 +985,12 @@ trait IsTransient { ...@@ -970,6 +985,12 @@ trait IsTransient {
fn is_transient(&self) -> bool; fn is_transient(&self) -> bool;
} }
impl IsTransient for Arc<StateKey> {
fn is_transient(&self) -> bool {
matches!(&self.owner, StateOwner::Task(id) if id.is_transient())
}
}
impl IsTransient for TaskId { impl IsTransient for TaskId {
fn is_transient(&self) -> bool { fn is_transient(&self) -> bool {
TaskId::is_transient(self) TaskId::is_transient(self)
......
...@@ -10,7 +10,7 @@ use smallvec::SmallVec; ...@@ -10,7 +10,7 @@ use smallvec::SmallVec;
use turbo_bincode::{new_turbo_bincode_decoder, turbo_bincode_decode, turbo_bincode_encode}; use turbo_bincode::{new_turbo_bincode_decoder, turbo_bincode_decode, turbo_bincode_encode};
use turbo_persistence::CommitStats; use turbo_persistence::CommitStats;
use turbo_tasks::{ use turbo_tasks::{
DynTaskInputs, RawVc, TaskId, DynTaskInputs, RawVc, StateKey, TaskId,
macro_helpers::NativeFunction, macro_helpers::NativeFunction,
panic_hooks::{PanicHookGuard, register_panic_hook}, panic_hooks::{PanicHookGuard, register_panic_hook},
parallel, parallel,
...@@ -36,6 +36,7 @@ use crate::{ ...@@ -36,6 +36,7 @@ use crate::{
enum InfraKey { enum InfraKey {
NextFreeTaskId = 0, NextFreeTaskId = 0,
GcRoots = 1, GcRoots = 1,
States = 2,
} }
impl InfraKey { impl InfraKey {
...@@ -249,10 +250,22 @@ impl TurboBackingStorage { ...@@ -249,10 +250,22 @@ impl TurboBackingStorage {
get(&self.inner.database).context("Unable to read GC roots from database") get(&self.inner.database).context("Unable to read GC roots from database")
} }
pub(crate) fn load_states(&self) -> Result<Vec<(StateKey, Vec<u8>, Vec<TaskId>, Option<u64>)>> {
let Some(bytes) = self
.inner
.database
.get(KeySpace::Infra, InfraKey::States.key().as_ref())?
else {
return Ok(Vec::new());
};
turbo_bincode_decode(bytes.borrow()).context("Unable to restore backend-owned states")
}
pub(crate) fn save_snapshot<I>( pub(crate) fn save_snapshot<I>(
&self, &self,
roots: Option<Vec<(TaskId, TtlCounter)>>, roots: Option<Vec<(TaskId, TtlCounter)>>,
snapshots: Vec<I>, snapshots: Vec<I>,
state_snapshot: Option<Vec<u8>>,
) -> Result<SnapshotMeta> ) -> Result<SnapshotMeta>
where where
I: IntoIterator<Item = SnapshotItem> + Send + Sync, I: IntoIterator<Item = SnapshotItem> + Send + Sync,
...@@ -352,7 +365,7 @@ impl TurboBackingStorage { ...@@ -352,7 +365,7 @@ impl TurboBackingStorage {
let mut next_task_id = get_next_free_task_id(&batch)?; let mut next_task_id = get_next_free_task_id(&batch)?;
next_task_id = next_task_id.max(snapshot_meta.max_next_task_id + 1); next_task_id = next_task_id.max(snapshot_meta.max_next_task_id + 1);
save_infra(&batch, next_task_id, roots)?; save_infra(&batch, next_task_id, roots, state_snapshot)?;
{ {
let _span = tracing::trace_span!("commit").entered(); let _span = tracing::trace_span!("commit").entered();
// Byte totals are the physical on-disk bytes (post-compression, including .sst / // Byte totals are the physical on-disk bytes (post-compression, including .sst /
...@@ -481,6 +494,7 @@ fn save_infra( ...@@ -481,6 +494,7 @@ fn save_infra(
batch: &TurboWriteBatch<'_>, batch: &TurboWriteBatch<'_>,
next_task_id: u32, next_task_id: u32,
roots: Option<Vec<(TaskId, TtlCounter)>>, roots: Option<Vec<(TaskId, TtlCounter)>>,
state_snapshot: Option<Vec<u8>>,
) -> Result<(), anyhow::Error> { ) -> Result<(), anyhow::Error> {
batch batch
.put( .put(
...@@ -500,6 +514,15 @@ fn save_infra( ...@@ -500,6 +514,15 @@ fn save_infra(
) )
.context("Unable to write GC roots")?; .context("Unable to write GC roots")?;
} }
if let Some(states) = state_snapshot {
batch
.put(
KeySpace::Infra,
WriteBuffer::Borrowed(InfraKey::States.key().as_ref()),
WriteBuffer::Vec(states),
)
.context("Unable to write backend-owned states")?;
}
// Safety: save_infra is called after all concurrent writes to Infra are done. // Safety: save_infra is called after all concurrent writes to Infra are done.
unsafe { batch.flush(KeySpace::Infra)? }; unsafe { batch.flush(KeySpace::Infra)? };
Ok(()) Ok(())
...@@ -708,6 +731,7 @@ mod tests { ...@@ -708,6 +731,7 @@ mod tests {
task_id: deleted_id, task_id: deleted_id,
task_type_hash: collision_hash.to_le_bytes(), task_type_hash: collision_hash.to_le_bytes(),
}]], }]],
None,
)?; )?;
let db = &storage.inner.database; let db = &storage.inner.database;
......
This diff is collapsed.
...@@ -111,6 +111,24 @@ pub fn create_tt(name: &str) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::T ...@@ -111,6 +111,24 @@ pub fn create_tt(name: &str) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::T
create_tt_with_workers(name, 2) create_tt_with_workers(name, 2)
} }
/// [`create_tt`] with the GC off, for tests of eviction alone. Between `run_once` scopes nothing
/// anchors the roots, so with the GC on it could collect the very tasks under test.
pub fn create_tt_without_gc(name: &str) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
let dir = create_persistence_dir(name);
let tt = open_tt_at_with_gc(dir.path(), 2, Some(false), None, None);
(tt, dir)
}
/// A fresh GC-enabled backend with a controlled named-owner/root TTL.
pub fn create_tt_with_gc_ttl(
name: &str,
ttl: Duration,
) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
let dir = create_persistence_dir(name);
let tt = open_tt_at_with_gc(dir.path(), 2, Some(true), Some(ttl), None);
(tt, dir)
}
/// [`create_tt`] with a custom GC min-progress floor /// [`create_tt`] with a custom GC min-progress floor
pub fn create_tt_with_gc_min_progress( pub fn create_tt_with_gc_min_progress(
name: &str, name: &str,
......
...@@ -2135,10 +2135,11 @@ fn generate_autoset_ops(field: &FieldInfo) -> TokenStream { ...@@ -2135,10 +2135,11 @@ fn generate_autoset_ops(field: &FieldInfo) -> TokenStream {
quote! { #ref_expr.contains(item) } quote! { #ref_expr.contains(item) }
}; };
// State keys may own a named namespace, so set iteration only requires Clone.
let iter_body = if is_option { let iter_body = if is_option {
quote! { #ref_expr.into_iter().flat_map(|set| set.iter().copied()) } quote! { #ref_expr.into_iter().flat_map(|set| set.iter().cloned()) }
} else { } else {
quote! { #ref_expr.iter().copied() } quote! { #ref_expr.iter().cloned() }
}; };
let len_body = if is_option { let len_body = if is_option {
......
...@@ -8,6 +8,7 @@ mod func; ...@@ -8,6 +8,7 @@ mod func;
mod function_macro; mod function_macro;
mod global_name; mod global_name;
mod primitive_macro; mod primitive_macro;
mod state_slot_macro;
mod task_input_attr_macro; mod task_input_attr_macro;
mod value_impl_macro; mod value_impl_macro;
mod value_macro; mod value_macro;
...@@ -75,6 +76,16 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -75,6 +76,16 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
value_macro::value(args, input) value_macro::value(args, input)
} }
#[proc_macro_attribute]
pub fn state(args: TokenStream, input: TokenStream) -> TokenStream {
if !args.is_empty() {
return syn::Error::new(proc_macro2::Span::call_site(), "state takes no arguments")
.to_compile_error()
.into();
}
state_slot_macro::state(input)
}
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn task_input(args: TokenStream, input: TokenStream) -> TokenStream { pub fn task_input(args: TokenStream, input: TokenStream) -> TokenStream {
task_input_attr_macro::task_input(args, input) task_input_attr_macro::task_input(args, input)
......
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{Expr, GenericArgument, ItemStatic, PathArguments, Type, parse_macro_input};
/// Attach a distinct, named link-time registry entry to a typed state slot.
pub fn state(input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as ItemStatic);
let ident = &item.ident;
let vis = &item.vis;
let ty = &item.ty;
let Type::Path(type_path) = ty.as_ref() else {
return syn::Error::new_spanned(ty, "expected StateSlot<T>")
.to_compile_error()
.into();
};
let Some(last) = type_path.path.segments.last() else {
return syn::Error::new_spanned(ty, "expected StateSlot<T>")
.to_compile_error()
.into();
};
if last.ident != "StateSlot" {
return syn::Error::new_spanned(ty, "expected StateSlot<T>")
.to_compile_error()
.into();
}
let PathArguments::AngleBracketed(arguments) = &last.arguments else {
return syn::Error::new_spanned(ty, "expected StateSlot<T>")
.to_compile_error()
.into();
};
let Some(GenericArgument::Type(value_ty)) = arguments.args.first() else {
return syn::Error::new_spanned(ty, "expected StateSlot<T>")
.to_compile_error()
.into();
};
if arguments.args.len() != 1 {
return syn::Error::new_spanned(ty, "expected exactly one state value type")
.to_compile_error()
.into();
}
let Expr::Call(initializer) = item.expr.as_ref() else {
return syn::Error::new_spanned(&item.expr, "expected StateSlot::new()")
.to_compile_error()
.into();
};
if !initializer.args.is_empty()
|| !matches!(initializer.func.as_ref(), Expr::Path(path)
if path.path.segments.last().is_some_and(|segment| segment.ident == "new"))
{
return syn::Error::new_spanned(&item.expr, "expected StateSlot::new()")
.to_compile_error()
.into();
}
let marker = format_ident!("__{ident}_state_slot_marker");
let definition = format_ident!("__{ident}_state_slot_definition");
let attrs = &item.attrs;
let cfg_attrs = attrs
.iter()
.filter(|attr| attr.path().is_ident("cfg") || attr.path().is_ident("cfg_attr"))
.collect::<Vec<_>>();
quote! {
#(#cfg_attrs)*
#[allow(non_camel_case_types)]
struct #marker;
#(#cfg_attrs)*
turbo_tasks::register_state!(#definition =
turbo_tasks::backend_state::StateFactory::new::<#marker, #value_ty>(
stringify!(#ident),
concat!(module_path!(), "::", stringify!(#ident)),
)
);
#(#attrs)*
#vis static #ident: #ty = turbo_tasks::StateSlot::with_factory(&#definition);
}
.into()
}
...@@ -13,6 +13,7 @@ Turbo Tasks defines 4 primitives: ...@@ -13,6 +13,7 @@ Turbo Tasks defines 4 primitives:
It defines some derived elements from that: It defines some derived elements from that:
- **Tasks:** An instance of a function together with its arguments. - **Tasks:** An instance of a function together with its arguments.
- **[`Vc`s ("Value Cells")][`Vc`]:** References to locations associated with tasks where values are stored. The contents of a cell can change after the reexecution of a function due to invalidation. A [`Vc`] can be read to get [a read-only reference][crate::ReadRef] to the stored data, representing a snapshot of that cell at that point in time. - **[`Vc`s ("Value Cells")][`Vc`]:** References to locations associated with tasks where values are stored. The contents of a cell can change after the reexecution of a function due to invalidation. A [`Vc`] can be read to get [a read-only reference][crate::ReadRef] to the stored data, representing a snapshot of that cell at that point in time.
- **[`StateSlot`]s and [`TurboTasksState`]s:** Registered, typed mutable sources whose canonical values live in the backend, independently of any task's value cell. One slot produces one state per task owner or explicitly rooted named owner; tracked synchronous reads invalidate their consumers on updates.
There are a few design patterns that are commonly used with Turbo Tasks: There are a few design patterns that are commonly used with Turbo Tasks:
- **[Singleton Pattern][crate::_singleton_pattern]:** Use a private constructor function to ensure a 1:1 mapping between values and value cells. - **[Singleton Pattern][crate::_singleton_pattern]:** Use a private constructor function to ensure a 1:1 mapping between values and value cells.
......
...@@ -32,6 +32,7 @@ use crate::{ ...@@ -32,6 +32,7 @@ use crate::{
CellId, RawVc, ReadCellOptions, ReadOutcome, ReadOutputOptions, ReadRef, SharedReference, CellId, RawVc, ReadCellOptions, ReadOutcome, ReadOutputOptions, ReadRef, SharedReference,
TaskId, TaskIdSet, TaskPriority, TraitRef, TraitTypeId, TurboTasksCallApi, TurboTasksPanic, TaskId, TaskIdSet, TaskPriority, TraitRef, TraitTypeId, TurboTasksCallApi, TurboTasksPanic,
ValueTypeId, ValueTypePersistence, VcValueTrait, VcValueType, ValueTypeId, ValueTypePersistence, VcValueTrait, VcValueType,
backend_state::StateKey,
dyn_task_inputs::{DynTaskInputs, DynTaskInputsStorage}, dyn_task_inputs::{DynTaskInputs, DynTaskInputsStorage},
macro_helpers::NativeFunction, macro_helpers::NativeFunction,
manager::{TaskPersistence, TurboTasks}, manager::{TaskPersistence, TurboTasks},
...@@ -606,6 +607,35 @@ pub trait Backend: Sized + Sync + Send { ...@@ -606,6 +607,35 @@ pub trait Backend: Sized + Sync + Send {
#[allow(unused_variables)] #[allow(unused_variables)]
fn idle_end(&self, turbo_tasks: &TurboTasks<Self>) {} fn idle_end(&self, turbo_tasks: &TurboTasks<Self>) {}
fn pin_named_state_owner(&self, name: &RcStr, turbo_tasks: &TurboTasks<Self>);
fn unpin_named_state_owner(&self, name: &RcStr, turbo_tasks: &TurboTasks<Self>);
/// Initialize only if this slot has no value yet. The initializer runs
/// under the state-store lock and must not call back into turbo-tasks.
fn create_state(
&self,
key: &StateKey,
initial: &mut dyn FnMut() -> Vec<u8>,
turbo_tasks: &TurboTasks<Self>,
);
/// Return a copy of the canonical state value, atomically registering a
/// dependency if `reader` is supplied.
fn read_state(
&self,
key: &StateKey,
reader: Option<TaskId>,
turbo_tasks: &TurboTasks<Self>,
) -> Result<Vec<u8>>;
/// Return whether the value changed, after dirtying its current readers.
fn set_state(
&self,
key: &StateKey,
value: Vec<u8>,
turbo_tasks: &TurboTasks<Self>,
) -> Result<bool>;
fn invalidate_task(&self, task: TaskId, turbo_tasks: &TurboTasks<Self>); fn invalidate_task(&self, task: TaskId, turbo_tasks: &TurboTasks<Self>);
fn invalidate_tasks(&self, tasks: &[TaskId], turbo_tasks: &TurboTasks<Self>); fn invalidate_tasks(&self, tasks: &[TaskId], turbo_tasks: &TurboTasks<Self>);
......
This diff is collapsed.
...@@ -149,6 +149,7 @@ define_id!( ...@@ -149,6 +149,7 @@ define_id!(
); );
define_id!(FunctionId: u16); define_id!(FunctionId: u16);
define_id!(TraitTypeId: u16); define_id!(TraitTypeId: u16);
define_id!(StateFactoryId: u16);
define_id!( define_id!(
LocalTaskId: u32, LocalTaskId: u32,
derive(Debug, Serialize, Deserialize, Encode, Decode), derive(Debug, Serialize, Deserialize, Encode, Decode),
...@@ -306,3 +307,9 @@ make_registered_serializable!( ...@@ -306,3 +307,9 @@ make_registered_serializable!(
registry::get_native_function, registry::get_native_function,
registry::validate_function_id, registry::validate_function_id,
); );
make_registered_serializable!(
StateFactoryId,
u16,
registry::get_state_factory,
registry::validate_state_factory_id,
);
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#![feature(mpmc_channel)] #![feature(mpmc_channel)]
pub mod backend; pub mod backend;
pub mod backend_state;
mod capture_future; mod capture_future;
mod collectibles; mod collectibles;
mod completion; mod completion;
...@@ -79,6 +80,7 @@ pub use crate::manager::InlineExecutionStats; ...@@ -79,6 +80,7 @@ pub use crate::manager::InlineExecutionStats;
#[cfg(feature = "task_dirty_cause")] #[cfg(feature = "task_dirty_cause")]
pub use crate::task_dirty_cause::TaskDirtyCause; pub use crate::task_dirty_cause::TaskDirtyCause;
pub use crate::{ pub use crate::{
backend_state::{StateKey, StateOwner, StateOwnerRoot, StateSlot, TurboTasksState},
capture_future::TurboTasksPanic, capture_future::TurboTasksPanic,
collectibles::CollectiblesSource, collectibles::CollectiblesSource,
completion::{Completion, Completions}, completion::{Completion, Completions},
...@@ -93,7 +95,8 @@ pub use crate::{ ...@@ -93,7 +95,8 @@ pub use crate::{
}, },
error::PrettyPrintError, error::PrettyPrintError,
id::{ id::{
ExecutionId, FunctionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId, ExecutionId, FunctionId, LocalTaskId, StateFactoryId, TRANSIENT_TASK_BIT, TaskId,
TraitTypeId, ValueTypeId,
}, },
invalidation::{ invalidation::{
InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator, InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator,
...@@ -173,6 +176,14 @@ macro_rules! fxindexset { ...@@ -173,6 +176,14 @@ macro_rules! fxindexset {
}; };
} }
/// Declares a registered, per-owner backend state slot.
///
/// ```ignore
/// #[turbo_tasks::state]
/// static OPTIONS: turbo_tasks::StateSlot<Option<MyOptions>> = turbo_tasks::StateSlot::new();
/// ```
pub use turbo_tasks_macros::state;
#[doc = include_str!("../singleton_pattern.md")] #[doc = include_str!("../singleton_pattern.md")]
pub mod _singleton_pattern {} pub mod _singleton_pattern {}
......
...@@ -28,6 +28,7 @@ use serde::{Deserialize, Serialize}; ...@@ -28,6 +28,7 @@ use serde::{Deserialize, Serialize};
use smallvec::SmallVec; use smallvec::SmallVec;
use tokio::{select, sync::mpsc::Receiver, task_local}; use tokio::{select, sync::mpsc::Receiver, task_local};
use tracing::{Instrument, Span, instrument}; use tracing::{Instrument, Span, instrument};
use turbo_rcstr::RcStr;
use turbo_tasks_hash::{DeterministicHash, hash_xxh3_hash128}; use turbo_tasks_hash::{DeterministicHash, hash_xxh3_hash128};
use crate::{ use crate::{
...@@ -128,6 +129,19 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send { ...@@ -128,6 +129,19 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send {
fn invalidate_serialization(&self, task: TaskId); fn invalidate_serialization(&self, task: TaskId);
/// Keep a named state owner alive until all its roots are dropped.
fn pin_named_state_owner(&self, name: &RcStr);
fn unpin_named_state_owner(&self, name: &RcStr);
/// Create a canonical state value if the slot is vacant.
fn create_state(&self, key: &crate::StateKey, initial: &mut dyn FnMut() -> Vec<u8>);
/// Read the state value synchronously, optionally tracking the current task.
fn read_state(&self, key: &crate::StateKey, tracked: bool) -> Result<Vec<u8>>;
/// Replace a state value, dirtying readers if it has changed.
fn set_state(&self, key: &crate::StateKey, value: Vec<u8>) -> Result<bool>;
fn try_read_task_output( fn try_read_task_output(
&self, &self,
task: TaskId, task: TaskId,
...@@ -1801,6 +1815,32 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> { ...@@ -1801,6 +1815,32 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> {
self.backend.invalidate_serialization(task, self); self.backend.invalidate_serialization(task, self);
} }
fn pin_named_state_owner(&self, name: &RcStr) {
self.backend.pin_named_state_owner(name, self);
}
fn unpin_named_state_owner(&self, name: &RcStr) {
self.backend.unpin_named_state_owner(name, self);
}
fn create_state(&self, key: &crate::StateKey, initial: &mut dyn FnMut() -> Vec<u8>) {
self.backend.create_state(key, initial, self);
}
fn read_state(&self, key: &crate::StateKey, tracked: bool) -> Result<Vec<u8>> {
self.backend.read_state(
key,
tracked
.then(|| current_task_if_available("reading backend state"))
.flatten(),
self,
)
}
fn set_state(&self, key: &crate::StateKey, value: Vec<u8>) -> Result<bool> {
self.backend.set_state(key, value, self)
}
#[track_caller] #[track_caller]
fn try_read_task_output( fn try_read_task_output(
&self, &self,
......
...@@ -5,7 +5,8 @@ use scattered_collect::slice::ScatteredSlice; ...@@ -5,7 +5,8 @@ use scattered_collect::slice::ScatteredSlice;
use crate::{ use crate::{
TraitType, ValueType, TraitType, ValueType,
id::{FunctionId, TraitTypeId, ValueTypeId}, backend_state::StateFactory,
id::{FunctionId, StateFactoryId, TraitTypeId, ValueTypeId},
native_function::NativeFunction, native_function::NativeFunction,
}; };
...@@ -55,6 +56,10 @@ pub static VALUES_SLICE: ScatteredSlice<&'static ValueType>; ...@@ -55,6 +56,10 @@ pub static VALUES_SLICE: ScatteredSlice<&'static ValueType>;
#[scattered_collect::gather] #[scattered_collect::gather]
pub static TRAITS_SLICE: ScatteredSlice<&'static TraitType>; pub static TRAITS_SLICE: ScatteredSlice<&'static TraitType>;
#[doc(hidden)]
#[scattered_collect::gather]
pub static STATES_SLICE: ScatteredSlice<&'static StateFactory>;
/// Register a [`NativeFunction`] definition into the link-time [`FUNCTIONS_SLICE`]. /// Register a [`NativeFunction`] definition into the link-time [`FUNCTIONS_SLICE`].
#[macro_export] #[macro_export]
#[doc(hidden)] #[doc(hidden)]
...@@ -104,6 +109,19 @@ macro_rules! register_trait { ...@@ -104,6 +109,19 @@ macro_rules! register_trait {
}; };
} }
/// Register a state slot's erased type information into the link-time registry.
#[macro_export]
#[doc(hidden)]
macro_rules! register_state {
($name:ident = $value:expr) => {
static $name: $crate::backend_state::StateFactory = $value;
$crate::macro_helpers::scattered_collect::declarative::scatter! {
#[scatter($crate::registry::STATES_SLICE)]
const _: &'static $crate::backend_state::StateFactory = &$name;
}
};
}
#[doc(hidden)] #[doc(hidden)]
pub trait RegistryDef<T: 'static> { pub trait RegistryDef<T: 'static> {
const DEF: &'static T; const DEF: &'static T;
...@@ -151,6 +169,14 @@ impl Registerable for TraitType { ...@@ -151,6 +169,14 @@ impl Registerable for TraitType {
} }
} }
impl Registerable for StateFactory {
type Id = StateFactoryId;
const TYPE_NAME: &'static str = "State";
fn ty(&self) -> &RegistryType {
&self.ty
}
}
/// Assign IDs to items and call post_init. Shared logic for all registry types. /// Assign IDs to items and call post_init. Shared logic for all registry types.
fn init_registry<T: Registerable>(mut items: Vec<&'static T>) -> Box<[&'static T]> { fn init_registry<T: Registerable>(mut items: Vec<&'static T>) -> Box<[&'static T]> {
// Sort by global name for stable, deterministic ID assignment // Sort by global name for stable, deterministic ID assignment
...@@ -306,6 +332,23 @@ pub fn validate_trait_type_id(id: TraitTypeId) -> Option<Error> { ...@@ -306,6 +332,23 @@ pub fn validate_trait_type_id(id: TraitTypeId) -> Option<Error> {
validate_id(&TRAITS, id) validate_id(&TRAITS, id)
} }
static STATES: LazyLock<Box<[&'static StateFactory]>> =
LazyLock::new(|| init_registry(STATES_SLICE.iter().copied().collect()));
#[inline]
pub fn get_state_factory_id(factory: &'static StateFactory) -> StateFactoryId {
get_id(&STATES, factory)
}
#[inline]
pub fn get_state_factory(id: StateFactoryId) -> &'static StateFactory {
get_item(&STATES, id)
}
pub fn validate_state_factory_id(id: StateFactoryId) -> Option<Error> {
validate_id(&STATES, id)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
......
...@@ -194,7 +194,8 @@ pub mod parking_lot_mutex_bincode { ...@@ -194,7 +194,8 @@ pub mod parking_lot_mutex_bincode {
/// **This API violates core assumption of turbo-tasks, is believed to be unsound, and there's no /// **This API violates core assumption of turbo-tasks, is believed to be unsound, and there's no
/// plan fix it.** You should prefer to use [collectibles][crate::CollectiblesSource] instead of /// plan fix it.** You should prefer to use [collectibles][crate::CollectiblesSource] instead of
/// state where at all possible. This API may be removed in the future. /// state where at all possible. This API may be removed in the future. For backend-owned mutable
/// sources, use [`TurboTasksState`][crate::TurboTasksState] instead.
/// ///
/// An [internally-mutable] type, similar to [`RefCell`][std::cell::RefCell] or [`Mutex`] that can /// An [internally-mutable] type, similar to [`RefCell`][std::cell::RefCell] or [`Mutex`] that can
/// be stored inside a [`VcValueType`]. /// be stored inside a [`VcValueType`].
......
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