Unverified Commit 4dad02d1 authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] abort warmup after caches are complete (#72474)

When we warmup we are trying to fill caches so the actual dev dynamic
render can have the right environment for logs to help with debugging.
If we have no inflight cache reads/fills happening there is no reason to
continue the warmup.

This change updates the warmup function to wait for the cacheSignal to
indicate it is complete before aborting. This should not alter any
program behavior but is observable if you contrive a very slow prefetch
request like awaiting a setTimeout inside loading.tsx

This allows us to remove the dev only property tracking the
prerenderResumeCache in the request store type. I also moved the
prerender->render cache conversion into the warmup function since it
leaks less about the implementation generatlized the name since it being
a cache from a warmup is sort of an implementation detail from the
render interface perspective so we'll just call it a
devRenderResumeCache. still quite the mouthful
parent 81271bae
...@@ -171,7 +171,10 @@ import './clean-async-snapshot.external' ...@@ -171,7 +171,10 @@ import './clean-async-snapshot.external'
import { INFINITE_CACHE } from '../../lib/constants' import { INFINITE_CACHE } from '../../lib/constants'
import { createComponentStylesAndScripts } from './create-component-styles-and-scripts' import { createComponentStylesAndScripts } from './create-component-styles-and-scripts'
import { parseLoaderTree } from './parse-loader-tree' import { parseLoaderTree } from './parse-loader-tree'
import { createPrerenderResumeDataCache } from '../resume-data-cache/resume-data-cache' import {
createPrerenderResumeDataCache,
createRenderResumeDataCache,
} from '../resume-data-cache/resume-data-cache'
export type GetDynamicParamFromSegment = ( export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug] // [slug] / [[slug]] / [...slug]
...@@ -590,8 +593,7 @@ async function generateDynamicFlightRenderResult( ...@@ -590,8 +593,7 @@ async function generateDynamicFlightRenderResult(
*/ */
async function warmupDevRender( async function warmupDevRender(
req: BaseNextRequest, req: BaseNextRequest,
ctx: AppRenderContext, ctx: AppRenderContext
requestStore: RequestStore
): Promise<RenderResult> { ): Promise<RenderResult> {
const renderOpts = ctx.renderOpts const renderOpts = ctx.renderOpts
if (!renderOpts.dev) { if (!renderOpts.dev) {
...@@ -614,50 +616,60 @@ async function warmupDevRender( ...@@ -614,50 +616,60 @@ async function warmupDevRender(
// We're doing a dev warmup, so we should create a new resume data cache so // We're doing a dev warmup, so we should create a new resume data cache so
// we can fill it. // we can fill it.
const devWarmupPrerenderResumeDataCache = createPrerenderResumeDataCache() const prerenderResumeDataCache = createPrerenderResumeDataCache()
// Attach this to the request store so that it can be used during the const renderController = new AbortController()
// render. const prerenderController = new AbortController()
requestStore.devWarmupPrerenderResumeDataCache = const cacheSignal = new CacheSignal()
devWarmupPrerenderResumeDataCache const prerenderStore: PrerenderStore = {
type: 'prerender',
phase: 'render',
implicitTags: [],
renderSignal: renderController.signal,
controller: prerenderController,
cacheSignal,
dynamicTracking: null,
revalidate: INFINITE_CACHE,
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [],
prerenderResumeDataCache,
}
const rscPayload = await workUnitAsyncStorage.run( const rscPayload = await workUnitAsyncStorage.run(
requestStore, prerenderStore,
generateDynamicRSCPayload, generateDynamicRSCPayload,
ctx ctx
) )
// For app dir, use the bundled version of Flight server renderer (renderToReadableStream) // For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
// which contains the subset React. // which contains the subset React.
const flightReadableStream = workUnitAsyncStorage.run( workUnitAsyncStorage.run(
requestStore, prerenderStore,
ctx.componentMod.renderToReadableStream, ctx.componentMod.renderToReadableStream,
rscPayload, rscPayload,
ctx.clientReferenceManifest.clientModules, ctx.clientReferenceManifest.clientModules,
{ {
onError, onError,
signal: renderController.signal,
} }
) )
const reader = flightReadableStream.getReader() // Wait for all caches to be finished filling
await cacheSignal.cacheReady()
// Read the entire stream. // We unset the cache so any late over-run renders aren't able to write into this cache
while (true) { prerenderStore.prerenderResumeDataCache = null
if ((await reader.read()).done) { // Abort the render
break renderController.abort()
}
}
// As we're finished rendering, remove the reference to the prerender resume
// data cache so it can't be written to again.
requestStore.devWarmupPrerenderResumeDataCache = null
// We don't really want to return a result here but the stack of functions // We don't really want to return a result here but the stack of functions
// that calls into renderToHTML... expects a result. We should refactor this to // that calls into renderToHTML... expects a result. We should refactor this to
// lift the warmup pathway outside of renderToHTML... but for now this suffices // lift the warmup pathway outside of renderToHTML... but for now this suffices
return new FlightRenderResult('', { return new FlightRenderResult('', {
fetchMetrics: ctx.workStore.fetchMetrics, fetchMetrics: ctx.workStore.fetchMetrics,
devWarmupPrerenderResumeDataCache, devRenderResumeDataCache: createRenderResumeDataCache(
prerenderResumeDataCache
),
}) })
} }
...@@ -1270,7 +1282,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1270,7 +1282,7 @@ async function renderToHTMLOrFlightImpl(
} else { } else {
// We're rendering dynamically // We're rendering dynamically
const renderResumeDataCache = const renderResumeDataCache =
renderOpts.devWarmupRenderResumeDataCache ?? renderOpts.devRenderResumeDataCache ??
postponedState?.renderResumeDataCache postponedState?.renderResumeDataCache
const requestStore = createRequestStoreForRender( const requestStore = createRequestStoreForRender(
...@@ -1304,7 +1316,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1304,7 +1316,7 @@ async function renderToHTMLOrFlightImpl(
} }
if (isDevWarmupRequest) { if (isDevWarmupRequest) {
return warmupDevRender(req, ctx, requestStore) return warmupDevRender(req, ctx)
} else if (isRSCRequest) { } else if (isRSCRequest) {
return generateDynamicFlightRenderResult(req, ctx, requestStore) return generateDynamicFlightRenderResult(req, ctx, requestStore)
} }
...@@ -1453,7 +1465,7 @@ export const renderToHTMLOrFlight: AppPageRender = ( ...@@ -1453,7 +1465,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
if ( if (
postponedState?.renderResumeDataCache && postponedState?.renderResumeDataCache &&
renderOpts.devWarmupRenderResumeDataCache renderOpts.devRenderResumeDataCache
) { ) {
throw new InvariantError( throw new InvariantError(
'postponed state and dev warmup immutable resume data cache should not be provided together' 'postponed state and dev warmup immutable resume data cache should not be provided together'
......
...@@ -189,7 +189,7 @@ export interface RenderOptsPartial { ...@@ -189,7 +189,7 @@ export interface RenderOptsPartial {
* The resume data cache that was generated for this partially prerendered * The resume data cache that was generated for this partially prerendered
* page during dev warmup. * page during dev warmup.
*/ */
devWarmupRenderResumeDataCache?: RenderResumeDataCache devRenderResumeDataCache?: RenderResumeDataCache
/** /**
* When true, only the static shell of the page will be rendered. This will * When true, only the static shell of the page will be rendered. This will
......
...@@ -61,11 +61,6 @@ export type RequestStore = { ...@@ -61,11 +61,6 @@ export type RequestStore = {
// DEV-only // DEV-only
usedDynamic?: boolean usedDynamic?: boolean
prerenderPhase?: boolean prerenderPhase?: boolean
/**
* The resume data cache for this request. This will be a mutable cache.
*/
devWarmupPrerenderResumeDataCache: PrerenderResumeDataCache | null
} & PhasePartial } & PhasePartial
/** /**
...@@ -220,14 +215,9 @@ export function getPrerenderResumeDataCache( ...@@ -220,14 +215,9 @@ export function getPrerenderResumeDataCache(
workUnitStore: WorkUnitStore workUnitStore: WorkUnitStore
): PrerenderResumeDataCache | null { ): PrerenderResumeDataCache | null {
if ( if (
workUnitStore.type !== 'prerender-legacy' && workUnitStore.type === 'prerender' ||
workUnitStore.type !== 'cache' && workUnitStore.type === 'prerender-ppr'
workUnitStore.type !== 'unstable-cache'
) { ) {
if (workUnitStore.type === 'request') {
return workUnitStore.devWarmupPrerenderResumeDataCache
}
return workUnitStore.prerenderResumeDataCache return workUnitStore.prerenderResumeDataCache
} }
......
...@@ -248,7 +248,6 @@ function createRequestStoreImpl( ...@@ -248,7 +248,6 @@ function createRequestStoreImpl(
return cache.draftMode return cache.draftMode
}, },
renderResumeDataCache: renderResumeDataCache ?? null, renderResumeDataCache: renderResumeDataCache ?? null,
devWarmupPrerenderResumeDataCache: null,
isHmrRefresh, isHmrRefresh,
serverComponentsHmrCache: serverComponentsHmrCache:
serverComponentsHmrCache || serverComponentsHmrCache ||
......
...@@ -175,7 +175,6 @@ import type { RouteModule } from './route-modules/route-module' ...@@ -175,7 +175,6 @@ import type { RouteModule } from './route-modules/route-module'
import { FallbackMode, parseFallbackField } from '../lib/fallback' import { FallbackMode, parseFallbackField } from '../lib/fallback'
import { toResponseCacheEntry } from './response-cache/utils' import { toResponseCacheEntry } from './response-cache/utils'
import { scheduleOnNextTick } from '../lib/scheduler' import { scheduleOnNextTick } from '../lib/scheduler'
import { createRenderResumeDataCache } from './resume-data-cache/resume-data-cache'
export type FindComponentsResult = { export type FindComponentsResult = {
components: LoadComponentsReturnType components: LoadComponentsReturnType
...@@ -2654,11 +2653,9 @@ export default abstract class Server< ...@@ -2654,11 +2653,9 @@ export default abstract class Server<
// If the warmup is successful, we should use the resume data // If the warmup is successful, we should use the resume data
// cache from the warmup. // cache from the warmup.
if (warmup.metadata.devWarmupPrerenderResumeDataCache) { if (warmup.metadata.devRenderResumeDataCache) {
renderOpts.devWarmupRenderResumeDataCache = renderOpts.devRenderResumeDataCache =
createRenderResumeDataCache( warmup.metadata.devRenderResumeDataCache
warmup.metadata.devWarmupPrerenderResumeDataCache
)
} }
} }
......
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
streamToString, streamToString,
} from './stream-utils/node-web-streams-helper' } from './stream-utils/node-web-streams-helper'
import { isAbortError, pipeToNodeResponse } from './pipe-readable' import { isAbortError, pipeToNodeResponse } from './pipe-readable'
import type { PrerenderResumeDataCache } from './resume-data-cache/resume-data-cache' import type { RenderResumeDataCache } from './resume-data-cache/resume-data-cache'
type ContentTypeOption = string | undefined type ContentTypeOption = string | undefined
...@@ -40,7 +40,7 @@ export type AppPageRenderResultMetadata = { ...@@ -40,7 +40,7 @@ export type AppPageRenderResultMetadata = {
* In development, the cache is warmed up before the render. This is attached * In development, the cache is warmed up before the render. This is attached
* to the metadata so that it can be used during the render. * to the metadata so that it can be used during the render.
*/ */
devWarmupPrerenderResumeDataCache?: PrerenderResumeDataCache devRenderResumeDataCache?: RenderResumeDataCache
} }
export type PagesRenderResultMetadata = { export type PagesRenderResultMetadata = {
......
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