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

Cache baseline-independent server HMR snapshots

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent a38f91f0
...@@ -19,7 +19,9 @@ use napi::{ ...@@ -19,7 +19,9 @@ use napi::{
}; };
use napi_derive::napi; use napi_derive::napi;
use next_api::{ use next_api::{
aggregate_hmr::{ServerHmrChunkListVersion, ServerHmrUpdate, compute_server_hmr_update}, aggregate_hmr::{
ServerHmrChunkListVersion, ServerHmrChunkLists, ServerHmrUpdate, compute_server_hmr_update,
},
entrypoints::Entrypoints, entrypoints::Entrypoints,
next_server_nft::next_server_nft_assets, next_server_nft::next_server_nft_assets,
operation::{ operation::{
...@@ -54,7 +56,8 @@ use turbo_tasks::{ ...@@ -54,7 +56,8 @@ use turbo_tasks::{
ResolvedVc, TaskId, TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi, ResolvedVc, TaskId, TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi,
UpdateInfo, Vc, mark_top_level_task, UpdateInfo, Vc, mark_top_level_task,
message_queue::{CompilationEvent, Severity, TraceEvent}, message_queue::{CompilationEvent, Severity, TraceEvent},
read_strongly_consistent_and_apply_effects, take_effects, turbo_tasks, read_strongly_consistent_and_apply_effects,
resolve_strongly_consistent_and_take_and_apply_effects, take_effects, turbo_tasks,
unmark_top_level_task_may_leak_eventually_consistent_state, unmark_top_level_task_may_leak_eventually_consistent_state,
}; };
use turbo_tasks_backend::{BackingStorageOptions, db_invalidation::invalidation_reasons}; use turbo_tasks_backend::{BackingStorageOptions, db_invalidation::invalidation_reasons};
...@@ -1914,58 +1917,66 @@ async fn hmr_update_with_issues_operation( ...@@ -1914,58 +1917,66 @@ async fn hmr_update_with_issues_operation(
} }
#[turbo_tasks::value(serialization = "skip")] #[turbo_tasks::value(serialization = "skip")]
struct ServerHmrSnapshotWithEffects { struct ServerHmrUpdateSnapshot {
update: ServerHmrUpdate,
}
#[turbo_tasks::value(serialization = "skip")]
struct ServerHmrPullResult {
update: ServerHmrUpdate, update: ServerHmrUpdate,
issues: Arc<Vec<ReadRef<PlainIssue>>>, issues: Arc<Vec<ReadRef<PlainIssue>>>,
effects: Arc<Effects>,
} }
#[turbo_tasks::value(serialization = "skip")] #[turbo_tasks::value(serialization = "skip")]
struct ServerHmrSnapshot { struct ServerHmrSnapshot {
update: ServerHmrUpdate, chunk_lists: ReadRef<ServerHmrChunkLists>,
version: ReadRef<ServerHmrChunkListVersion>,
} }
#[turbo_tasks::function(operation, root)] #[turbo_tasks::function(operation, root)]
async fn project_server_hmr_snapshot_operation( async fn project_server_hmr_snapshot_operation(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
entry_paths: Vec<RcStr>, entry_paths: Vec<RcStr>,
from: TransientInstance<Option<ReadRef<ServerHmrChunkListVersion>>>,
) -> Result<Vc<ServerHmrSnapshot>> { ) -> Result<Vc<ServerHmrSnapshot>> {
let chunk_lists = project.server_hmr_chunks_for_entries(entry_paths).await?; let chunk_lists = project.server_hmr_chunks_for_entries(entry_paths).await?;
let version = ServerHmrChunkListVersion::from_chunk_lists(chunk_lists.as_slice()) let version = ServerHmrChunkListVersion::from_chunk_lists(chunk_lists.as_slice())
.await? .await?
.cell() .cell()
.await?; .await?;
let update = // Keep baseline-independent work cacheable across pull requests and warm restarts.
compute_server_hmr_update(chunk_lists.as_slice(), from.as_deref(), version).await?; Ok(ServerHmrSnapshot {
Ok(ServerHmrSnapshot { update }.cell()) chunk_lists,
version,
}
.cell())
} }
/// The caller reads this within a disposable root task so the old pull baseline /// The caller reads this within a disposable root task so the old pull baseline
/// cannot reactivate after the pull has completed. /// cannot reactivate after the pull has completed.
#[tracing::instrument(level = "info", name = "server hmr snapshot", skip_all)] #[tracing::instrument(level = "info", name = "server hmr update", skip_all)]
#[turbo_tasks::function(operation, root)] #[turbo_tasks::function(operation, root)]
async fn server_hmr_snapshot_with_effects_operation( async fn server_hmr_update_operation(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
entry_paths: Vec<RcStr>, entry_paths: Vec<RcStr>,
from: TransientInstance<Option<ReadRef<ServerHmrChunkListVersion>>>, from: TransientInstance<Option<ReadRef<ServerHmrChunkListVersion>>>,
) -> Result<Vc<ServerHmrSnapshotWithEffects>> { ) -> Result<Vc<ServerHmrUpdateSnapshot>> {
tracing::info!("server hmr snapshot"); tracing::info!("server hmr update");
let snapshot_op = project_server_hmr_snapshot_operation(project, entry_paths, from); // The old baseline is a transient input: this diff must miss on a warm
// Build-graph failures must reach the JS recovery path. // restart, while the snapshot operation below remains persistable.
let snapshot_op = project_server_hmr_snapshot_operation(project, entry_paths);
// The outer pull's strong read protects both this cached snapshot and the
// transient diff, so their update and next baseline cannot cross revisions.
let snapshot = snapshot_op let snapshot = snapshot_op
.read_strongly_consistent() .read_strongly_consistent()
.final_read_hint() .final_read_hint()
.await?; .await?;
let filter = project.issue_filter().await?; let update = compute_server_hmr_update(
let issues = get_issues(snapshot_op, &filter).await?; snapshot.chunk_lists.as_slice(),
let effects = Arc::new(take_effects(snapshot_op).await?); from.as_deref(),
Ok(ServerHmrSnapshotWithEffects { snapshot.version.clone(),
update: snapshot.update.clone(), )
issues, .await?;
effects, Ok(ServerHmrUpdateSnapshot { update }.cell())
}
.cell())
} }
/// Explicitly dispose the pull's reactive root, including its old-version diff /// Explicitly dispose the pull's reactive root, including its old-version diff
...@@ -2032,16 +2043,24 @@ pub async fn project_get_server_hmr_update( ...@@ -2032,16 +2043,24 @@ pub async fn project_get_server_hmr_update(
// Match the existing project-resolution and effects boundary. // Match the existing project-resolution and effects boundary.
unmark_top_level_task_may_leak_eventually_consistent_state(); unmark_top_level_task_may_leak_eventually_consistent_state();
let project = container.project().to_resolved().await?; let project = container.project().to_resolved().await?;
mark_top_level_task(); let snapshot_op = server_hmr_update_operation(
let snapshot_op = server_hmr_snapshot_with_effects_operation(
project, project,
entry_paths, entry_paths,
TransientInstance::new(from), TransientInstance::new(from),
); );
let snapshot = // Capture and apply effects from the entire transient diff,
read_strongly_consistent_and_apply_effects(snapshot_op, |v| &v.effects) // not just the cached, baseline-independent snapshot. The
.await?; // helper owns retries when an effect requests invalidation.
Ok(ReadRef::cell(snapshot)) resolve_strongly_consistent_and_take_and_apply_effects(snapshot_op).await?;
let snapshot = snapshot_op.read_strongly_consistent().await?;
let filter = project.issue_filter().await?;
let issues = get_issues(snapshot_op, &filter).await?;
mark_top_level_task();
Ok(ServerHmrPullResult {
update: snapshot.update.clone(),
issues,
}
.cell())
} }
} }
}), }),
...@@ -2053,7 +2072,7 @@ pub async fn project_get_server_hmr_update( ...@@ -2053,7 +2072,7 @@ pub async fn project_get_server_hmr_update(
// A root task is not an OperationVc. Read it directly: wrapping it // A root task is not an OperationVc. Read it directly: wrapping it
// in an operation would connect another parentless GC root that // in an operation would connect another parentless GC root that
// disposing `root` could not release. // disposing `root` could not release.
Vc::<ServerHmrSnapshotWithEffects>::from(RawVc::task_output(root_id)) Vc::<ServerHmrPullResult>::from(RawVc::task_output(root_id))
.strongly_consistent() .strongly_consistent()
.await .await
}) })
......
...@@ -104,6 +104,8 @@ describe('warm-restart task statistics', () => { ...@@ -104,6 +104,8 @@ describe('warm-restart task statistics', () => {
await stop() await stop()
const missed = await readMissedTaskNames() const missed = await readMissedTaskNames()
// Server HMR's old pull baseline is transient and must be diffed again,
// while its baseline-independent chunk-list snapshot stays cached.
expect(missed).toMatchInlineSnapshot(` expect(missed).toMatchInlineSnapshot(`
[ [
"<turbopack_browser::ecmascript::list::content::EcmascriptDevChunkListContent as dyn turbopack_core::version::VersionedContent>::update", "<turbopack_browser::ecmascript::list::content::EcmascriptDevChunkListContent as dyn turbopack_core::version::VersionedContent>::update",
...@@ -111,6 +113,7 @@ describe('warm-restart task statistics', () => { ...@@ -111,6 +113,7 @@ describe('warm-restart task statistics', () => {
"next_api::project::Project::hmr_version_state", "next_api::project::Project::hmr_version_state",
"next_napi_bindings::next_api::project::hmr_update_with_issues_operation", "next_napi_bindings::next_api::project::hmr_update_with_issues_operation",
"next_napi_bindings::next_api::project::project_hmr_update_operation", "next_napi_bindings::next_api::project::project_hmr_update_operation",
"next_napi_bindings::next_api::project::server_hmr_update_operation",
"turbopack_core::version::VersionState::get", "turbopack_core::version::VersionState::get",
] ]
`) `)
......
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