turbo-tasks-backend: restore invalidated tasks with batched optional reads (#100465)
### What? `invalidate()` now opens its tasks with a bulk optional read (`ExecuteContext::for_each_try_task_all`) instead of one `try_task` per id. Non-resident persisted tasks are restored with one batched disk lookup per category instead of one lookup per task. ### Why? Follow-up to #100446 (https://github.com/vercel/next.js/pull/100446#discussion_r4326845752). The existing bulk read APIs (`prepare_tasks` / `for_each_task*`) are `MustExist`: a task missing in memory and on disk panics. Invalidation needs `try_task` semantics instead, because a task being invalidated may legitimately be gone. ### How? - `prepare_tasks_with_callback` takes a `TaskAccess` mode. - `MustExist` is the existing behavior and keeps the same panics and messages. - `AllowMissing` skips any task that `try_task` would not return, and the callback isn't called for it: - a soft-deleted task, which is left in place - a task that isn't restorable (transient, or no backing storage) and isn't resident, which is discarded - a task whose requested category has no disk key, including when only one category is missing; it is discarded and its restoring claims are cleared - The existing restore phases are reused for both modes: - ready tasks are handed off first - claimed reads are done as batched I/O - claims are applied and waiters notified before callbacks run for the tasks this call restored - tasks a peer is restoring are waited for - New `for_each_try_task_all` is built on that, sharing the guard and lock-counter handling with `for_each_task`. - `invalidate()` uses `for_each_try_task_all`. - It requires unique ids: a `debug_assert!` checks this, with no deduplication cost in release builds. The no-duplicates contract is documented on `Backend::invalidate_tasks`, and every in-tree caller already passes unique ids. - Each task is still marked dirty, and its priority computed, while its guard is held. - Scheduling is still a single `schedule_tasks` call. In-tree, `invalidate()` currently gets a single id from every caller (`Invalidator` → `Backend::invalidate_task`). Batching real multi-task invalidations (file watcher, filesystem invalidation, `State` dependents) through `Backend::invalidate_tasks` is a separate, independent PR, and it carries the benchmark. This PR makes no performance claim. ### Tests - 11 unit tests for `for_each_try_task_all` in `backend/operation/mod.rs`: - resident tasks - persisted tasks that aren't resident, checked with a test-only lookup counter: 3 tasks need 2 batched lookups and 0 single lookups - missing persistent and missing transient ids - soft-deleted tasks - a task with only one category on disk - no backing storage - a mixed batch - waiting on a peer's restore, then either restoring the task or skipping it when missing - `tests/invalidate_tasks.rs`: `Backend::invalidate_tasks` with present, missing persistent and missing transient ids. Each existing task re-executes exactly once. This test also passes on the parent commit. - `cargo test -p turbo-tasks-backend`, `cargo clippy -p turbo-tasks-backend --all-targets -- -D warnings`, `cargo check -p turbo-tasks-backend --features task_dirty_cause` <!-- NEXT_JS_LLM --> <!-- fleet d21ddb86-de88-41df-82c7-aa6a29a6165e --> --------- Co-authored-by:vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by:
Tobias Koppers <1365881+sokra@users.noreply.github.com>
Showing
This diff is collapsed.
Please
register
or
sign in
to comment