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

turbo-tasks-backend: shared-lock tracked reads with existing edges

When the read task already has the reader as dependent, make the
reader-side edge current first and then read under a shared lock,
instead of locking both tasks exclusively. Adds concurrency tests for
cell, keyed cell and output invalidation.
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 51931489
......@@ -693,35 +693,89 @@ fn is_immutable(task: &storage::StorageReadGuard<'_>) -> bool {
impl TurboTasksBackend {
/// Shared-lock fast path for cell reads. It only takes a shared lock on the read task, so many
/// readers of the same cell don't serialize on it. Returns `None` when the slow path is needed
/// (cell missing, task not resident/restored/alive, or a dependency edge must be recorded).
/// (cell missing, task not resident/restored/alive, or a new dependency edge must be inserted).
///
/// Reads that don't need a dependency edge (immutable task, untracked reads, ...) just read
/// the content under the shared lock. Tracked reads of mutable tasks take the slow path.
/// the content under the shared lock.
///
/// Tracked reads are only handled when the task already has the reader as dependent (e.g. a
/// repeated read in the same execution or a re-execution). Race argument: `update_cell` removes
/// the old content and snapshots `cell_dependents` under the task's exclusive lock, and only
/// afterwards checks each dependent's current/outdated `cell_dependencies`. We first make the
/// reader-side dependency current (locking only the reader), then, under the shared lock,
/// verify the task-side edge still exists and read the content. If an update's exclusive
/// section happens before our shared section, we see no content and fall back. If it happens
/// after, the update sees the task-side edge and then a *current* reader-side edge, so the
/// reader is made stale. Making the reader-side edge current when we then fall back is
/// harmless: the slow path does the same.
///
/// Inserting a *new* task-side edge still requires the slow path, which locks both tasks
/// together (see `lock_task_and_optional_reader`).
fn try_read_task_cell_shared(
&self,
_ctx: &mut ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
need_reader_task: Option<TaskId>,
cell: CellId,
tracking: ReadCellTracking,
) -> Option<SharedReference> {
let key = tracking.key();
let has_edge = |task: &storage::StorageReadGuard<'_>, reader: TaskId| {
let reverse = CellRef { task: reader, cell };
if let Some(k) = key {
task.cell_dependents_hashed_contains(&(reverse, k))
} else {
task.cell_dependents_contains(&reverse)
}
};
let task = self.storage.access_read(task_id)?;
if !is_task_readable(&task) {
return None;
}
match need_reader_task {
Some(_) if tracking.should_track(false) && !is_immutable(&task) => None,
let reader_id = match need_reader_task {
Some(reader_id) if tracking.should_track(false) && !is_immutable(&task) => reader_id,
// No dependency edge needed.
_ => task.get_cell_data(&cell).cloned(),
_ => return task.get_cell_data(&cell).cloned(),
};
if !has_edge(&task, reader_id) || !task.cell_data_contains(&cell) {
return None;
}
drop(task);
// Make the reader side current *before* reading the content (see above).
{
let mut reader_task = ctx.task(reader_id, TaskDataCategory::All);
let target = CellRef {
task: task_id,
cell,
};
if let Some(k) = key {
if !reader_task.remove_outdated_cell_dependencies_hashed(&(target, k)) {
let _ = reader_task.add_cell_dependencies_hashed((target, k));
}
} else if !reader_task.remove_outdated_cell_dependencies(&target) {
let _ = reader_task.add_cell_dependencies(target);
}
}
let task = self.storage.access_read(task_id)?;
if !is_task_readable(&task) || !has_edge(&task, reader_id) {
return None;
}
task.get_cell_data(&cell).cloned()
}
/// Shared-lock fast path for eventually consistent output reads, analogous to
/// [`Self::try_read_task_cell_shared`]. Only handles a task that is not in progress and has a
/// non-error output; everything else returns `None` and takes the slow path.
///
/// The race argument for the tracked case mirrors the cell case: an output only changes when
/// the task completes, which happens under the task's exclusive lock and is followed by
/// checking each output dependent's current/outdated `output_dependencies`. Before that, the
/// task is in progress, which makes this fast path bail out.
fn try_read_task_output_shared(
&self,
_ctx: &mut ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
need_reader_task: Option<TaskId>,
tracking: ReadTracking,
......@@ -741,11 +795,29 @@ impl TurboTasksBackend {
if !is_task_readable(&task) {
return None;
}
match need_reader_task {
Some(_) if tracking.should_track(false) && !is_immutable(&task) => None,
let reader_id = match need_reader_task {
Some(reader_id) if tracking.should_track(false) && !is_immutable(&task) => reader_id,
// No dependency edge needed.
_ => read(&task),
_ => return read(&task),
};
if !task.output_dependent_contains(&reader_id) || read(&task).is_none() {
return None;
}
drop(task);
// Make the reader side current *before* reading the output (see above).
{
let mut reader_task = ctx.task(reader_id, TaskDataCategory::All);
if !reader_task.remove_outdated_output_dependencies(&task_id) {
let _ = reader_task.add_output_dependencies(task_id);
}
}
let task = self.storage.access_read(task_id)?;
if !is_task_readable(&task) || !task.output_dependent_contains(&reader_id) {
return None;
}
read(&task)
}
fn try_read_task_output(
......
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
//! Many tasks reading the same Vcs while the Vcs change. Exercises the shared-lock read fast paths
//! (repeated tracked reads, re-executions with existing dependency edges, immutable and untracked
//! reads) and checks that no invalidation is lost.
use std::time::Duration;
use anyhow::{Result, bail};
use rustc_hash::FxHashMap;
use turbo_tasks::{ResolvedVc, State, TryJoinIterExt, Vc};
use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!();
const WIDTH: u32 = 256;
const ROUNDS: u32 = 40;
/// Value a reader reports when its source failed.
const ERROR: u32 = u32::MAX;
#[turbo_tasks::value(transparent)]
struct Step(State<u32>);
#[turbo_tasks::value(transparent)]
struct Values(Vec<u32>);
#[turbo_tasks::value(transparent)]
struct OptValue(Option<u32>);
#[turbo_tasks::value(transparent)]
struct OptValues(Vec<Option<u32>>);
#[turbo_tasks::function(operation, root)]
fn create_step() -> Vc<Step> {
Step(State::new(0)).cell()
}
/// Sets `step` to `base`, then keeps changing it while `read` runs, so updates race with readers
/// that are executing. Returns the last value set.
async fn race_updates<F: Future<Output = Result<()>>>(
step: &State<u32>,
base: u32,
read: F,
) -> u32 {
step.set(base);
let updates = async {
let mut last = base;
for i in 1..=3 {
tokio::time::sleep(Duration::from_micros(200 * i as u64)).await;
last = base + i;
step.set(last);
}
last
};
let (read_result, last) = futures::join!(read, updates);
// Errors are fine here (the root may read a deliberate error state), only the final,
// strongly consistent read is checked.
let _ = read_result;
last
}
// --- Tracked cell and output reads ---
#[turbo_tasks::function]
async fn forward(step: ResolvedVc<Step>) -> Result<Vc<u32>> {
Ok(Vc::cell(*step.await?.get() * 10))
}
/// Changes cell content, output identity (own cell vs. another task's output) and errors.
#[turbo_tasks::function]
async fn source(step: ResolvedVc<Step>) -> Result<Vc<u32>> {
let value = *step.await?.get();
if value % 7 == 6 {
bail!("error state {value}");
}
if value % 2 == 0 {
Ok(forward(*step))
} else {
Ok(Vc::cell(value * 10))
}
}
fn expected_source(value: u32) -> u32 {
if value % 7 == 6 { ERROR } else { value * 10 }
}
#[turbo_tasks::function]
async fn tracked_reader(step: ResolvedVc<Step>, index: u32) -> Result<Vc<u32>> {
let source = source(*step);
let mut last = 0;
// Read repeatedly: the first read creates the dependency edges, the following reads (and
// reads after re-executions) find them already in place.
for _ in 0..4 {
last = match source.await {
Ok(value) => *value,
Err(_) => ERROR,
};
}
Ok(Vc::cell(if last == ERROR { ERROR } else { last + index }))
}
#[turbo_tasks::function(operation, root)]
async fn tracked_root(step: ResolvedVc<Step>) -> Result<Vc<Values>> {
let values = (0..WIDTH)
.map(|index| tracked_reader(*step, index))
.try_join()
.await?;
Ok(Vc::cell(values.iter().map(|v| **v).collect()))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn tracked_readers_see_latest_value() {
run(&REGISTRATION, async || {
let step_op = create_step();
let step_vc = step_op.resolve().strongly_consistent().await?;
let step = step_op.read_strongly_consistent().await?;
let root = tracked_root(step_vc);
root.read_strongly_consistent().await?;
for round in 0..ROUNDS {
let last = race_updates(&step, round * 4, async {
root.read_strongly_consistent().await?;
Ok(())
})
.await;
let values = root.read_strongly_consistent().await?;
let expected = expected_source(last);
for (index, value) in values.iter().enumerate() {
let expected = if expected == ERROR {
ERROR
} else {
expected + index as u32
};
assert_eq!(
*value, expected,
"round {round}: reader {index} is stale (state = {last})"
);
}
}
anyhow::Ok(())
})
.await
.unwrap()
}
// --- Keyed cell reads ---
#[turbo_tasks::value(transparent, cell = "keyed")]
struct Map(FxHashMap<u32, u32>);
const KEYS: u32 = 16;
/// Key `k` is present unless `(value + k) % 5 == 0`, and its value depends on `value` for every
/// third key only, so only some keys change in each update.
fn expected_entry(value: u32, key: u32) -> Option<u32> {
if (value + key).is_multiple_of(5) {
None
} else if key.is_multiple_of(3) {
Some(value * 100 + key)
} else {
Some(key)
}
}
#[turbo_tasks::function]
async fn map(step: ResolvedVc<Step>) -> Result<Vc<Map>> {
let value = *step.await?.get();
Ok(Vc::cell(
(0..KEYS)
.filter_map(|key| expected_entry(value, key).map(|v| (key, v)))
.collect(),
))
}
#[turbo_tasks::function]
async fn keyed_reader(step: ResolvedVc<Step>, index: u32) -> Result<Vc<OptValue>> {
let map = map(*step);
let key = index % KEYS;
let mut last = None;
for _ in 0..3 {
last = map.get(&key).await?.as_deref().copied();
}
Ok(Vc::cell(last))
}
#[turbo_tasks::function(operation, root)]
async fn keyed_root(step: ResolvedVc<Step>) -> Result<Vc<OptValues>> {
let values = (0..WIDTH)
.map(|index| keyed_reader(*step, index))
.try_join()
.await?;
Ok(Vc::cell(values.iter().map(|v| **v).collect()))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn keyed_readers_see_latest_value() {
run(&REGISTRATION, async || {
let step_op = create_step();
let step_vc = step_op.resolve().strongly_consistent().await?;
let step = step_op.read_strongly_consistent().await?;
let root = keyed_root(step_vc);
root.read_strongly_consistent().await?;
for round in 0..ROUNDS {
let last = race_updates(&step, round * 4, async {
root.read_strongly_consistent().await?;
Ok(())
})
.await;
let values = root.read_strongly_consistent().await?;
for (index, value) in values.iter().enumerate() {
let key = index as u32 % KEYS;
assert_eq!(
*value,
expected_entry(last, key),
"round {round}: keyed reader {index} (key {key}) is stale (state = {last})"
);
}
}
anyhow::Ok(())
})
.await
.unwrap()
}
// --- Immutable reads ---
#[turbo_tasks::function]
fn immutable_source(key: u32) -> Vc<u32> {
Vc::cell(key * 3)
}
#[turbo_tasks::function]
async fn immutable_reader(seed: u32, index: u32) -> Result<Vc<u32>> {
let _ = seed;
let source = immutable_source(index % 4);
let mut sum = 0;
for _ in 0..4 {
sum += *source.await?;
}
Ok(Vc::cell(sum))
}
#[turbo_tasks::function(operation, root)]
async fn immutable_root(seed: u32) -> Result<Vc<Values>> {
let values = (0..WIDTH)
.map(|index| immutable_reader(seed, index))
.try_join()
.await?;
Ok(Vc::cell(values.iter().map(|v| **v).collect()))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn immutable_shared_reads() {
run(&REGISTRATION, async || {
for seed in 0..8 {
let values = immutable_root(seed).read_strongly_consistent().await?;
for (index, value) in values.iter().enumerate() {
assert_eq!(*value, 4 * 3 * (index as u32 % 4), "reader {index}");
}
}
anyhow::Ok(())
})
.await
.unwrap()
}
// --- Untracked reads ---
#[turbo_tasks::function]
async fn untracked_reader(step: ResolvedVc<Step>, seed: u32, index: u32) -> Result<Vc<u32>> {
let _ = (seed, index);
let source = source(*step);
let mut last = 0;
for _ in 0..4 {
last = match source.untracked().await {
Ok(value) => *value,
Err(_) => ERROR,
};
}
Ok(Vc::cell(last))
}
#[turbo_tasks::function(operation, root)]
async fn untracked_root(step: ResolvedVc<Step>, seed: u32) -> Result<Vc<Values>> {
let values = (0..WIDTH)
.map(|index| untracked_reader(*step, seed, index))
.try_join()
.await?;
Ok(Vc::cell(values.iter().map(|v| **v).collect()))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn untracked_shared_reads() {
run(&REGISTRATION, async || {
let step_op = create_step();
let step_vc = step_op.resolve().strongly_consistent().await?;
let step = step_op.read_strongly_consistent().await?;
// Keep a tracked reader of `source` around, so `source` gets recomputed on every change.
let tracked = tracked_root(step_vc);
tracked.read_strongly_consistent().await?;
for round in 0..ROUNDS {
let base = round * 4;
let last = race_updates(&step, base, async {
untracked_root(step_vc, round)
.read_strongly_consistent()
.await?;
Ok(())
})
.await;
tracked.read_strongly_consistent().await?;
// Untracked reads don't propagate invalidations, so only check that every value is one
// the source had at some point.
let values = untracked_root(step_vc, round)
.read_strongly_consistent()
.await?;
for (index, value) in values.iter().enumerate() {
assert!(
*value == ERROR || (*value % 10 == 0 && *value / 10 <= last),
"round {round}: untracked reader {index} read an impossible value {value}"
);
}
}
anyhow::Ok(())
})
.await
.unwrap()
}
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