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

Keep recorded GC roots until their TTL expires

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 4b9b3d87
...@@ -156,8 +156,8 @@ pub struct BackendOptions { ...@@ -156,8 +156,8 @@ pub struct BackendOptions {
/// Whether the reference-counting GC runs for this backend. `None` (default) leaves it off. /// Whether the reference-counting GC runs for this backend. `None` (default) leaves it off.
pub gc: Option<bool>, pub gc: Option<bool>,
/// How long a GC root may go un-anchored before it ages out. `None` (default) uses /// How long a recorded GC root may go without a session-only pin before it ages out. `None`
/// `DEFAULT_GC_ROOT_TTL`. /// (default) uses `DEFAULT_GC_ROOT_TTL`.
pub gc_root_ttl: Option<Duration>, pub gc_root_ttl: Option<Duration>,
/// How long a GC pass runs before it will honour an interrupt. `None` (default) uses /// How long a GC pass runs before it will honour an interrupt. `None` (default) uses
...@@ -272,17 +272,9 @@ pub struct TurboTasksBackend { ...@@ -272,17 +272,9 @@ pub struct TurboTasksBackend {
task_statistics: TaskStatisticsApi, task_statistics: TaskStatisticsApi,
backing_storage: TurboBackingStorage, backing_storage: TurboBackingStorage,
/// How long a GC root may go un-anchored before it ages out. /// How long a recorded GC root may go without a session-only pin before it ages out.
gc_root_ttl: Duration, gc_root_ttl: Duration,
/// Current roots: tasks a GC pass in this session has observed as roots (reported by
/// `gc_scan_roots`, or found pinned while refreshing the roots map). Never persisted.
///
/// A persisted root that is not a current root is from an earlier session and keeps its TTL,
/// even while resident. A current root that is later released was let go in this session, so a
/// pass may collect it right away. See `TurboTasksBackend::gc_roots_refresh_and_age_out`.
gc_current_roots: Mutex<FxHashSet<TaskId>>,
/// How long a GC pass runs before it will honour an interrupt /// How long a GC pass runs before it will honour an interrupt
gc_min_progress: Duration, gc_min_progress: Duration,
...@@ -383,7 +375,6 @@ impl TurboTasksBackend { ...@@ -383,7 +375,6 @@ impl TurboTasksBackend {
task_statistics: TaskStatisticsApi::default(), task_statistics: TaskStatisticsApi::default(),
backing_storage, backing_storage,
gc_root_ttl, gc_root_ttl,
gc_current_roots: Default::default(),
gc_min_progress, gc_min_progress,
#[cfg(feature = "verify_aggregation_graph")] #[cfg(feature = "verify_aggregation_graph")]
root_tasks: Default::default(), root_tasks: Default::default(),
......
...@@ -12,7 +12,7 @@ use turbo_tasks::{ResolvedVc, TaskId, Vc, prevent_gc}; ...@@ -12,7 +12,7 @@ use turbo_tasks::{ResolvedVc, TaskId, Vc, prevent_gc};
use crate::{ use crate::{
gc_fixture::{Constant, Selector, create_constant, create_selector}, gc_fixture::{Constant, Selector, create_constant, create_selector},
util::create_tt, util::{create_persistence_dir, create_tt, reopen_tt_with_gc_ttl},
}; };
/// The `TaskId` backing a `Vc`'s `TaskOutput` node. /// The `TaskId` backing a `Vc`'s `TaskOutput` node.
...@@ -218,7 +218,11 @@ async fn construction_pin_is_released_after_connecting_task() { ...@@ -218,7 +218,11 @@ async fn construction_pin_is_released_after_connecting_task() {
/// safe after the backend has stopped, which `RootTask::Drop` relies on. /// safe after the backend has stopped, which `RootTask::Drop` relies on.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dispose_root_task_releases_anchored_subgraph() { async fn dispose_root_task_releases_anchored_subgraph() {
let (tt, _persistence_dir) = create_tt("dispose_root_task_releases_anchored_subgraph"); // The leaf is recorded as a GC root while the root task holds it, so after disposal it waits
// for its TTL. Keep the TTL short so it ages out inside the test.
const TTL: std::time::Duration = std::time::Duration::from_millis(50);
let persistence_dir = create_persistence_dir("dispose_root_task_releases_anchored_subgraph");
let tt = reopen_tt_with_gc_ttl(&persistence_dir, TTL);
// The root sends the leaf's id out via a oneshot rather than a `run_once` probe: a probe is its // The root sends the leaf's id out via a oneshot rather than a `run_once` probe: a probe is its
// own transient `Once` task that would also connect the leaf, and `Once` tasks are never // own transient `Once` task that would also connect the leaf, and `Once` tasks are never
...@@ -260,15 +264,23 @@ async fn dispose_root_task_releases_anchored_subgraph() { ...@@ -260,15 +264,23 @@ async fn dispose_root_task_releases_anchored_subgraph() {
.await .await
.unwrap(); .unwrap();
// Two: the leaf, and the disposed root_task` itself. The `root_task` is a transient task, and // Two: the leaf, and the disposed root_task` itself. The `root_task` is a transient task, and
// transient tasks are collectible, so releasing the last reference to the subgraph reclaims // transient tasks are collectible. The leaf is a recorded root, so it waits for its TTL.
// both. let first = tt
assert_eq!( .backend()
.snapshot_and_evict_for_testing(&tt)
.gc_stats()
.collected;
assert_eq!(first, 1, "only the transient root task is collected first");
assert!(
tt.backend() tt.backend()
.snapshot_and_evict_for_testing(&tt) .persisted_gc_roots_for_testing()
.gc_stats() .iter()
.collected, .any(|(id, _)| *id == leaf_id),
2 "the released leaf remains a recorded root until its TTL"
); );
tokio::time::sleep(TTL + std::time::Duration::from_millis(50)).await;
let second = tt.backend().gc_for_testing(&tt);
assert_eq!(second, 1, "the aged-out leaf is collected");
// Disposal after the backend has stopped (the whole task map is dropped by `stop`), as a // Disposal after the backend has stopped (the whole task map is dropped by `stop`), as a
// `RootTask` finalized during Node worker teardown would be. // `RootTask` finalized during Node worker teardown would be.
......
...@@ -598,10 +598,11 @@ async fn parentless_connect_of_an_evicted_operation_does_not_abort() { ...@@ -598,10 +598,11 @@ async fn parentless_connect_of_an_evicted_operation_does_not_abort() {
// --- Root map TTL vs. residency --- // --- Root map TTL vs. residency ---
// //
// A prior-session root must keep its TTL until a pass observes it as a root again (pinned or // Once a root is recorded in the root map it stays protected until its TTL expires, no matter what
// re-connected), no matter what makes it resident in the meantime (in a real session, startup // makes it resident in the meantime (in a real session, startup invalidation propagating through
// invalidation propagating through the aggregation graph restores most of them). And a root that // the aggregation graph restores most of them) or whether it was released in this session. And a
// is pinned must never be dropped from the map, even when eviction has dropped its `Meta`. // root that is pinned must never be dropped from the map, even when eviction has dropped its
// `Meta`.
fn in_root_map(tt: &Arc<TurboTasks<TurboTasksBackend>>, id: TaskId) -> bool { fn in_root_map(tt: &Arc<TurboTasks<TurboTasksBackend>>, id: TaskId) -> bool {
tt.backend() tt.backend()
...@@ -649,8 +650,8 @@ async fn rerun_leaf_executions(dir: &tempfile::TempDir, n: u32) -> u32 { ...@@ -649,8 +650,8 @@ async fn rerun_leaf_executions(dir: &tempfile::TempDir, n: u32) -> u32 {
leaf_executions(n) - before leaf_executions(n) - before
} }
/// A prior-session root that becomes resident without being re-pinned or re-connected is still /// A prior-session root that becomes resident without being pinned again is still within its TTL,
/// within its TTL, so it must not be collected. It only goes once it ages out. /// so it must not be collected. It only goes once it ages out.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn resident_prior_root_survives_until_ttl() { async fn resident_prior_root_survives_until_ttl() {
const N: u32 = 3; const N: u32 = 3;
...@@ -665,7 +666,7 @@ async fn resident_prior_root_survives_until_ttl() { ...@@ -665,7 +666,7 @@ async fn resident_prior_root_survives_until_ttl() {
let collected = tt.backend().gc_for_testing(&tt); let collected = tt.backend().gc_for_testing(&tt);
assert_eq!( assert_eq!(
collected, 0, collected, 0,
"a resident prior root that is not a current root is protected by its TTL" "a resident, unpinned prior root is protected by its TTL"
); );
assert!( assert!(
in_root_map(&tt, root), in_root_map(&tt, root),
...@@ -674,7 +675,7 @@ async fn resident_prior_root_survives_until_ttl() { ...@@ -674,7 +675,7 @@ async fn resident_prior_root_survives_until_ttl() {
tt.stop_and_wait().await; tt.stop_and_wait().await;
} }
// Session 3: the TTL has expired and nothing pinned or re-connected it, so it ages out with its // Session 3: the TTL has expired and nothing pinned it, so it ages out with its
// leaf. // leaf.
{ {
let tt = reopen_tt_with_gc_ttl(&dir, Duration::ZERO); let tt = reopen_tt_with_gc_ttl(&dir, Duration::ZERO);
...@@ -693,7 +694,7 @@ async fn resident_prior_root_survives_until_ttl() { ...@@ -693,7 +694,7 @@ async fn resident_prior_root_survives_until_ttl() {
); );
} }
/// A pinned root whose `Meta` was evicted after a snapshot is still a current root: it must stay in /// A pinned root whose `Meta` was evicted after a snapshot is still pinned: it must stay in
/// the root map, even though a pass cannot judge an evicted task as a root. /// the root map, even though a pass cannot judge an evicted task as a root.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pinned_root_with_evicted_meta_stays_in_root_map() { async fn pinned_root_with_evicted_meta_stays_in_root_map() {
...@@ -781,72 +782,3 @@ async fn cascade_does_not_collect_protected_prior_root() { ...@@ -781,72 +782,3 @@ async fn cascade_does_not_collect_protected_prior_root() {
"the protected root's subtree must still be cached" "the protected root's subtree must still be cached"
); );
} }
/// Within one session, releasing one of two session-only references keeps the root, and releasing
/// the last one makes it collectible right away, as before: the TTL only protects roots from
/// earlier sessions.
///
/// The two references are a subscription-style root task (a transient parent, released by
/// `dispose_root_task`) and a pin. A `run_once` connect would add a third reference that is never
/// released in-session.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn in_session_partial_release_keeps_root_last_release_collects() {
const N: u32 = 6;
let dir = create_persistence_dir("in_session_partial_release_keeps_root_last_release_collects");
let tt = reopen_tt_with_gc(&dir);
let (tx, rx) = tokio::sync::oneshot::channel();
let tx = Arc::new(std::sync::Mutex::new(Some(tx)));
let root_task = tt.spawn_root_task(move || {
let tx = tx.lock().unwrap().take();
Box::pin(async move {
let op = root_with_child(N);
let value = *op.read_strongly_consistent().await?;
if let Some(tx) = tx {
let _ = tx.send(op);
}
anyhow::Ok(Vc::<u32>::cell(value))
})
});
let op: OperationVc<u32> = rx.await.unwrap();
let id = op.task_id();
for _ in 0..100 {
if tt.backend().transient_ref_count_for_testing(id) > 0 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
let pin = GcRoot::pin(tt.clone(), op);
assert_eq!(tt.backend().transient_ref_count_for_testing(id), 2);
// Record the root while both references are held.
assert_eq!(tt.backend().gc_for_testing(&tt), 0);
assert!(in_root_map(&tt, id));
// Partial release, then evict: the remaining pin keeps the root in the map.
tt.dispose_root_task(root_task);
// A `run_once` keeps touched tasks active until it returns, so GC has to run after it.
turbo_tasks::run_once(tt.clone(), async move { anyhow::Ok(()) })
.await
.unwrap();
tt.backend().snapshot_and_evict_for_testing(&tt);
tt.backend().gc_for_testing(&tt);
assert!(
in_root_map(&tt, id),
"a root with a remaining pin stays in the map"
);
// Last release: no TTL protection for a root released in this session.
drop(pin);
// A task with evicted `Meta` is never collectible, so make sure it is restored: the point here
// is that the root map no longer protects a root released in this session.
tt.backend().assert_task_exists_for_testing(id, &tt);
let collected = tt.backend().gc_for_testing(&tt);
assert!(
collected >= 2,
"the released root and its leaf are collected in the same session (got {collected})"
);
assert!(!in_root_map(&tt, id), "the released root leaves the map");
tt.stop_and_wait().await;
}
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