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

Preserve cached evaluations across worker pool sessions

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 8a0cb4a4
...@@ -33,8 +33,6 @@ type IpcOutgoingMessage = ...@@ -33,8 +33,6 @@ type IpcOutgoingMessage =
const ipc = IPC as GenericIpc<IpcIncomingMessage, IpcOutgoingMessage> const ipc = IPC as GenericIpc<IpcIncomingMessage, IpcOutgoingMessage>
const queue: Array<{ entry: string; args: string[] }> = []
export const run = async () => { export const run = async () => {
let nextId = 1 let nextId = 1
const requests = new Map() const requests = new Map()
...@@ -64,25 +62,18 @@ export const run = async () => { ...@@ -64,25 +62,18 @@ export const run = async () => {
// Only the generic bootstrap owns the IPC connection. Evaluation modules are loaded on demand. // Only the generic bootstrap owns the IPC connection. Evaluation modules are loaded on demand.
await ipc.sendReady() await ipc.sendReady()
// Queue handling const run = async (entry: string, args: string[]) => {
let isRunning = false try {
const run = async () => { const getValue = await loadEvaluation(entry)
while (queue.length > 0) { const value = await getValue(internalIpc, ...args)
const { entry, args } = queue.shift()! await ipc.send({
try { type: 'end',
const getValue = await loadEvaluation(entry) data: value === undefined ? undefined : JSON.stringify(value, null, 2),
const value = await getValue(internalIpc, ...args) duration: 0,
await ipc.send({ })
type: 'end', } catch (e) {
data: await ipc.sendError(e as Error)
value === undefined ? undefined : JSON.stringify(value, null, 2),
duration: 0,
})
} catch (e) {
await ipc.sendError(e as Error)
}
} }
isRunning = false
} }
// Communication handling // Communication handling
...@@ -91,11 +82,7 @@ export const run = async () => { ...@@ -91,11 +82,7 @@ export const run = async () => {
switch (msg.type) { switch (msg.type) {
case 'evaluate': { case 'evaluate': {
queue.push({ entry: msg.entry, args: msg.args }) run(msg.entry, msg.args)
if (!isRunning) {
isRunning = true
run()
}
break break
} }
case 'result': { case 'result': {
......
...@@ -6,7 +6,7 @@ type EvaluationModule = { ...@@ -6,7 +6,7 @@ type EvaluationModule = {
} }
// A worker can serve many emitted entries. Cache their initialization independently, including // A worker can serve many emitted entries. Cache their initialization independently, including
// asynchronous exports. A pool restart replaces the worker and its module/runtime caches together. // asynchronous exports.
const modules = new Map<string, Promise<EvaluationModule['default']>>() const modules = new Map<string, Promise<EvaluationModule['default']>>()
/** Resolves to the entry's callable default export after its optional async initialization. */ /** Resolves to the entry's callable default export after its optional async initialization. */
......
...@@ -14,9 +14,6 @@ const binding: Binding = require( ...@@ -14,9 +14,6 @@ const binding: Binding = require(
binding.workerCreated(workerId) binding.workerCreated(workerId)
export const run = async () => { export const run = async () => {
let isRunning = false
const queue: Array<{ taskId: number; entry: string; args: string[] }> = []
const run = async (taskId: number, entry: string, args: string[]) => { const run = async (taskId: number, entry: string, args: string[]) => {
try { try {
const getValue = await loadEvaluation(entry) const getValue = await loadEvaluation(entry)
...@@ -42,12 +39,6 @@ export const run = async () => { ...@@ -42,12 +39,6 @@ export const run = async () => {
), ),
}) })
} }
if (queue.length > 0) {
const next = queue.shift()!
run(next.taskId, next.entry, next.args)
} else {
isRunning = false
}
} }
while (true) { while (true) {
...@@ -70,12 +61,7 @@ export const run = async () => { ...@@ -70,12 +61,7 @@ export const run = async () => {
switch (msg.type) { switch (msg.type) {
case 'evaluate': { case 'evaluate': {
if (!isRunning) { run(taskId, msg.entry, msg.args)
isRunning = true
run(taskId, msg.entry, msg.args)
} else {
queue.push({ taskId, entry: msg.entry, args: msg.args })
}
break break
} }
case 'result': { case 'result': {
......
...@@ -580,7 +580,10 @@ impl PartialEq for CompletionRevision { ...@@ -580,7 +580,10 @@ impl PartialEq for CompletionRevision {
} }
} }
#[turbo_tasks::function(session_dependent)] // Do not make this session-dependent: that would invalidate cached semantic evaluation results
// on every restore. The serialization-skipped cells already recompute this worker-lifetime
// metadata on demand in a new engine, without forcing otherwise cached consumers to execute.
#[turbo_tasks::function]
async fn completion_revision(completion: ResolvedVc<Completion>) -> Result<Vc<CompletionRevision>> { async fn completion_revision(completion: ResolvedVc<Completion>) -> Result<Vc<CompletionRevision>> {
let completion = completion.await?; let completion = completion.await?;
let state = CompletionRevisionState::default().resolved_cell(); let state = CompletionRevisionState::default().resolved_cell();
......
...@@ -521,6 +521,166 @@ async fn revision_snapshot(input: ResolvedVc<ChangingCompletion>) -> Result<Vc<u ...@@ -521,6 +521,166 @@ async fn revision_snapshot(input: ResolvedVc<ChangingCompletion>) -> Result<Vc<u
)) ))
} }
static CACHED_COMPLETION_EXECUTIONS: AtomicUsize = AtomicUsize::new(0);
#[turbo_tasks::function(operation, root)]
async fn cached_completion_consumer(input: ResolvedVc<ChangingCompletion>) -> Result<Vc<u64>> {
completion_revision(changing_completion(*input)).await?;
// Unlike the revision itself, this detects semantic work being repeated on restore.
Ok(Vc::cell(
CACHED_COMPLETION_EXECUTIONS.fetch_add(1, Ordering::SeqCst) as u64,
))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn completion_revision_preserves_cached_consumers_on_restore() {
turbo_tasks_testing::run(&REGISTRATION, || async {
let input = changing_completion_input(2)
.resolve()
.strongly_consistent()
.await?;
Ok(*cached_completion_consumer(input)
.read_strongly_consistent()
.await?)
})
.await
.unwrap();
}
#[turbo_tasks::function(operation, root)]
fn revision_observation(
input: ResolvedVc<ChangingCompletion>,
) -> Vc<crate::evaluate::CompletionRevision> {
completion_revision(changing_completion(*input))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn restored_completion_observations_get_fresh_ordinals_before_changes() {
// Observe the helper directly, as evaluation does, rather than persisting a copy of its
// session-local ordinal in an unrelated task's semantic result.
let mut previous = 0;
for initial in [true, false, false] {
let instance = REGISTRATION.create_turbo_tasks(
"restored_completion_observations_get_fresh_ordinals_before_changes",
initial,
);
let latest = turbo_tasks::run_once(instance.tt.clone(), async move {
let input_op = changing_completion_input(3);
let input_vc = input_op.resolve().strongly_consistent().await?;
let input = input_op.read_strongly_consistent().await?;
let revision = revision_observation(input_vc);
let restored = revision.read_strongly_consistent().await?;
assert!(
restored.n > previous,
"an unserialized helper is recomputed on demand"
);
assert_eq!(restored.n, revision.read_strongly_consistent().await?.n);
let tick = *input.tick.get_untracked();
input.tick.set(tick + 1);
let changed = revision.read_strongly_consistent().await?;
assert!(changed.n > restored.n);
let other = changing_completion_input(4)
.resolve()
.strongly_consistent()
.await?;
let other = revision_observation(other)
.read_strongly_consistent()
.await?;
assert!(other.n > changed.n, "restored lineages remain comparable");
assert!(restored.n < changed.n, "old observations stay immutable");
Ok(other.n)
})
.await
.unwrap();
instance.tt.stop_and_wait().await;
previous = latest;
}
}
#[test]
fn completion_revision_cache_survives_process_restart() {
let cache = std::env::temp_dir().join(format!(
"turbopack-node-completion-process-cache-{}",
std::process::id()
));
for phase in ["initial", "restored"] {
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"evaluate::tests::completion_revision_persistence_subprocess",
"--nocapture",
])
.env("TURBOPACK_NODE_TEST_CACHE_ROOT", &cache)
.env("TURBOPACK_NODE_COMPLETION_CACHE_PHASE", phase)
.output()
.unwrap();
assert!(
output.status.success(),
"{phase} subprocess failed:\n{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
std::fs::remove_dir_all(cache).unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn completion_revision_persistence_subprocess() {
let Ok(phase) = std::env::var("TURBOPACK_NODE_COMPLETION_CACHE_PHASE") else {
return;
};
let initial = phase == "initial";
let instance = REGISTRATION.create_turbo_tasks("completion_process_restart", initial);
turbo_tasks::run_once(instance.tt.clone(), async move {
let consumer_input = changing_completion_input(2)
.resolve()
.strongly_consistent()
.await?;
cached_completion_consumer(consumer_input)
.read_strongly_consistent()
.await?;
assert_eq!(
CACHED_COMPLETION_EXECUTIONS.load(Ordering::SeqCst),
usize::from(initial),
"the restored semantic consumer must not execute, even with a fresh process clock"
);
if initial {
// Make the persisted helper ordinal larger than the allocator in a new process.
for key in 100..164 {
let input = changing_completion_input(key)
.resolve()
.strongly_consistent()
.await?;
revision_observation(input)
.read_strongly_consistent()
.await?;
}
}
let input_op = changing_completion_input(3);
let input_vc = input_op.resolve().strongly_consistent().await?;
let input = input_op.read_strongly_consistent().await?;
let revision = revision_observation(input_vc);
let old = revision.read_strongly_consistent().await?;
if initial {
assert!(old.n > 64);
} else {
assert!(
old.n < 64,
"old session ordinals must not be restored into a fresh clock"
);
}
let tick = *input.tick.get_untracked();
input.tick.set(tick + 1);
let new = revision.read_strongly_consistent().await?;
assert!(new.n > old.n);
assert!(old.n < new.n, "old snapshots stay immutable after restore");
Ok(())
})
.await
.unwrap();
instance.tt.stop_and_wait().await;
}
#[tokio::test] #[tokio::test]
async fn completion_revisions_advance_on_invalidation_not_on_reads_and_preserve_snapshots() { async fn completion_revisions_advance_on_invalidation_not_on_reads_and_preserve_snapshots() {
run_once_without_cache_check(&REGISTRATION, async { run_once_without_cache_check(&REGISTRATION, async {
......
|name, initial| { |name, initial| {
use std::hash::BuildHasher; use std::hash::BuildHasher;
// Cargo only defines CARGO_TARGET_TMPDIR for integration tests, not library unit tests. // Cargo only defines CARGO_TARGET_TMPDIR for integration tests, not library unit tests.
let root = option_env!("CARGO_TARGET_TMPDIR") // Subprocess cache tests share a directory across different process IDs.
let root = std::env::var_os("TURBOPACK_NODE_TEST_CACHE_ROOT")
.map(std::path::PathBuf::from) .map(std::path::PathBuf::from)
.or_else(|| option_env!("CARGO_TARGET_TMPDIR").map(std::path::PathBuf::from))
.unwrap_or_else(|| std::env::temp_dir().join(format!("turbopack-node-tests-{}", std::process::id()))); .unwrap_or_else(|| std::env::temp_dir().join(format!("turbopack-node-tests-{}", std::process::id())));
let path = root.join(format!(".cache/{}", rustc_hash::FxBuildHasher.hash_one(name))); let path = root.join(format!(".cache/{}", rustc_hash::FxBuildHasher.hash_one(name)));
if initial { if initial {
......
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