next.js
f4ea7a70 - turbo-persistence: make batch_get callback-oriented (#100678)

Commit
22 days ago
turbo-persistence: make batch_get callback-oriented (#100678) ## What? `TurboPersistence::batch_get` no longer returns an ordered `Vec<Option<ArcBytes>>`. It now reports each key to a callback: ```rust pub fn batch_get<K: QueryKey + Sync>( &self, family: usize, keys: &[K], on_result: impl Fn(usize, Option<ArcBytes>) -> Result<()> + Sync, ) -> Result<()> ``` - `on_result` gets the index of the key in `keys`, and its value, or `None` when the key is missing or deleted. - On success it has been called exactly once per index, including each occurrence of a duplicate key. The calls are in no particular order and may run concurrently on the scheduler's threads: for batches above the parallel threshold, the callbacks run inside the parallel chunks. - A read error or a callback error is returned. Other callbacks may already have run, and some indices may never be reported. - `on_result` runs while the database holds its internal read lock, so it must not call back into the database. The turbo-tasks backend is updated the whole way down: - `TurboKeyValueDatabase::batch_get`, `TurboBackingStorage::batch_lookup_data` and `restore_task_data_batch` take the same kind of callback. - `batch_lookup_data` decodes `TaskStorage` inside the callback. - The batched restore in `prepare_tasks_with_callback` stores each decoded result in its `TaskRestoreEntry` directly. The entry's result fields are now `OnceLock`s, so the writes stay safe. - If a batch fails, every claimed entry still gets the batch error, replacing any result already stored, and the later restore phases are unchanged. - This replaces the separate parallel decode step from #100453: `decode_task_storage_batch`, `PARALLEL_DECODE_THRESHOLD`, `batch_lookup_raw` and the `batch_lookup` bench are removed, since decoding now happens in the read's callbacks. Its batch lookup round-trip test is kept and ported to the callback API. It now runs at 2048 and 2049 keys (both sides of `batch_get`'s parallel cutoff), checks that every index is reported exactly once, and checks that a malformed value fails the lookup. ## Why? The main caller only used the order of the results to map each value back to its task, and then decoded every `TaskStorage` serially on the calling thread. With a callback: - callers get the read's parallelism for free, and decoding now runs in parallel with the reads for large batches; - the persistence layer no longer assembles an ordered result, which removes the `results` vector and the unsafe `ConcurrentSlice` scatter writes added in #100620. ## How? - Each chunk calls `on_result` as soon as a value (including a blob) is materialized, and reports a tombstone as `None` when it is found. After walking its SST files, the chunk reports the keys it didn't find as `None`. - The chunking, the threshold (more than 2048 keys) and the statistics are unchanged from #100620. - In the tests, a test-only `batch_get_vec` helper rebuilds the ordered results and asserts that every index is reported exactly once. New tests cover one call per index for small and parallel batches (with misses and duplicates), and callback errors on found and missing keys. The benchmarks consume the callback values with `black_box`. ## Verification On the current head (rebased onto `canary` after #100620 and #100821 merged): - `cargo test -p turbo-persistence` with and without `--features stats`: 156 passed, 1 ignored. - `cargo test -p turbo-tasks-backend` passes, including restores from a real on-disk database. - clippy (`-D warnings`, with and without `stats`), rustfmt, strict rustdoc and ast-grep on the changed crates. From earlier versions of this change: - Miri, file mode: the batch tests and the new callback tests. - A warm-build run of the benchmark from #100570 (`--files=1000`) with a release build: all builds succeeded with no restore errors. Timings against an earlier build of the parent change were mixed, so this makes no performance claim. <!-- NEXT_JS_LLM --> <!-- fleet 2e9e7c99-0cc2-4e7b-9e62-627c0627638b --> --------- 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> Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Author
Parents
Loading