Unverified Commit f07ce3e7 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Cover concurrent cache writes for root params

The tests hold the redirect and value writes independently while a second
request reads different root param values. They also cover a failed write
whose sibling remains pending. A cache handler lookup for the second
request must wait until both producer writes settle.

A separate test controls generations at the initial and redirected cache
handler keys. The assertions record which handler reads the current
cross-request deduplication skips, so PR #100511 can verify its lookup
changes without losing generation or write coordination coverage.
parent 4fed142e
...@@ -9,19 +9,31 @@ async function readCachedValue(id: string) { ...@@ -9,19 +9,31 @@ async function readCachedValue(id: string) {
const language = await lang() const language = await lang()
cacheTag(`write-failure:${id}`, `write-failure-language:${language}`) cacheTag(`write-failure:${id}`, `write-failure-language:${language}`)
writeFailureState.generatedLanguages.push(language) writeFailureState.generatedLanguages.push(language)
cacheTag(
`app-root-params-getters:write-generation:${writeFailureState.generatedLanguages.length}`
)
if (id.startsWith('lookup-history-')) {
cacheLife({ stale: 300, revalidate: 0, expire: 3600 })
}
await writeFailureState.pendingFill await writeFailureState.pendingFill
return `${language}:${crypto.randomUUID()}` return `${language}:${crypto.randomUUID()}`
} }
async function readOuterValue(id: string, _name: string) {
'use cache'
cacheLife('hours')
return readCachedValue(id)
}
export default async function Page({ export default async function Page({
params, params,
searchParams, searchParams,
}: { }: {
params: Promise<{ countryCode: string; lang: string }> params: Promise<{ countryCode: string; lang: string }>
searchParams: Promise<{ id?: string | string[] }> searchParams: Promise<{ id?: string | string[]; outer?: string }>
}) { }) {
await connection() await connection()
const { id } = await searchParams const { id, outer } = await searchParams
if (id === undefined) { if (id === undefined) {
return null return null
} }
...@@ -30,5 +42,11 @@ export default async function Page({ ...@@ -30,5 +42,11 @@ export default async function Page({
} }
const { lang: language } = await params const { lang: language } = await params
writeFailureState.requestedLanguages.push(language) writeFailureState.requestedLanguages.push(language)
return <p id="value">{await readCachedValue(id)}</p> return (
<p id="value">
{outer === undefined
? await readCachedValue(id)
: await readOuterValue(id, 'outer')}
</p>
)
} }
...@@ -8,6 +8,10 @@ export async function GET() { ...@@ -8,6 +8,10 @@ export async function GET() {
generatedLanguages: writeFailureState.generatedLanguages, generatedLanguages: writeFailureState.generatedLanguages,
failedWriteLanguages: writeFailureState.failedWriteLanguages, failedWriteLanguages: writeFailureState.failedWriteLanguages,
writtenLanguages: writeFailureState.writtenLanguages, writtenLanguages: writeFailureState.writtenLanguages,
startedWrites: writeFailureState.startedWrites,
lookupKeys: writeFailureState.lookupKeys,
initialLookupCount: writeFailureState.initialLookupCount,
specificLookupCount: writeFailureState.specificLookupCount,
}) })
} }
...@@ -23,11 +27,66 @@ export async function POST(request: Request) { ...@@ -23,11 +27,66 @@ export async function POST(request: Request) {
writeFailureState.generatedLanguages = [] writeFailureState.generatedLanguages = []
writeFailureState.failedWriteLanguages = [] writeFailureState.failedWriteLanguages = []
writeFailureState.writtenLanguages = [] writeFailureState.writtenLanguages = []
writeFailureState.startedWrites = []
writeFailureState.lookupKeys = []
writeFailureState.failedWrite = 'both'
writeFailureState.lookupHistory = false
writeFailureState.pendingFill = new Promise((resolve) => { writeFailureState.pendingFill = new Promise((resolve) => {
writeFailureState.releaseFill = resolve writeFailureState.releaseFill = resolve
}) })
break break
} }
case 'hold-lookup-history':
writeFailureState.lookupHistory = true
writeFailureState.failedWrite = null
writeFailureState.heldWrite = 'value'
writeFailureState.initialLookupKey = ''
writeFailureState.initialLookupCount = 0
writeFailureState.specificLookupCount = 0
writeFailureState.pendingWrite = new Promise((resolve) => {
writeFailureState.releaseWrite = resolve
})
writeFailureState.pendingSecondWrite = new Promise((resolve) => {
writeFailureState.releaseSecondWrite = resolve
})
writeFailureState.pendingInitialLookup = new Promise((resolve) => {
writeFailureState.releaseInitialLookup = resolve
})
writeFailureState.pendingSpecificLookup = new Promise((resolve) => {
writeFailureState.releaseSpecificLookup = resolve
})
break
case 'release-initial-lookup':
writeFailureState.releaseInitialLookup?.()
writeFailureState.pendingInitialLookup = null
break
case 'release-specific-lookup':
writeFailureState.releaseSpecificLookup?.()
writeFailureState.pendingSpecificLookup = null
break
case 'release-second-write':
writeFailureState.releaseSecondWrite?.()
writeFailureState.pendingSecondWrite = null
break
case 'hold-redirect-write':
case 'hold-value-write': {
const searchParams = new URL(request.url).searchParams
writeFailureState.heldWrite =
searchParams.get('held') === 'redirect' ? 'redirect' : 'value'
const failure = searchParams.get('failure')
writeFailureState.failedWrite =
failure === 'redirect' || failure === 'value' ? failure : null
writeFailureState.pendingWrite = new Promise((resolve) => {
writeFailureState.releaseWrite = resolve
})
break
}
case 'release-write':
writeFailureState.releaseWrite?.()
writeFailureState.releaseWrite = null
writeFailureState.pendingWrite = null
writeFailureState.heldWrite = null
break
case 'release-fill': case 'release-fill':
writeFailureState.releaseFill?.() writeFailureState.releaseFill?.()
writeFailureState.releaseFill = null writeFailureState.releaseFill = null
......
...@@ -7,6 +7,37 @@ const { EventEmitter, once } = require('node:events') ...@@ -7,6 +7,37 @@ const { EventEmitter, once } = require('node:events')
const outerWrites = new EventEmitter() const outerWrites = new EventEmitter()
const pendingOuterWrites = once(outerWrites, 'complete') const pendingOuterWrites = once(outerWrites, 'complete')
const observedOuters = new Set() const observedOuters = new Set()
/**
* @type {Map<string, import('next/dist/server/lib/cache-handlers/types').CacheEntry>}
*/
const historyEntries = new Map()
/**
* @param {string} cacheKey
*/
function readHistoryEntry(cacheKey) {
const entry = historyEntries.get(cacheKey)
if (entry === undefined) {
return undefined
}
const [value, retained] = entry.value.tee()
entry.value = retained
return { ...entry, value }
}
/**
* @param {string} cacheKey
* @param {Promise<import('next/dist/server/lib/cache-handlers/types').CacheEntry>} pendingEntry
*/
async function saveHistoryEntry(cacheKey, pendingEntry) {
const entry = await pendingEntry
const [value, consumed] = entry.value.tee()
const reader = consumed.getReader()
while (!(await reader.read()).done) {}
// This backend retains stale entries so an enclosing cache can require
// generation after its redirected lookup returns.
historyEntries.set(cacheKey, { ...entry, value })
}
// Delay the English inner write until both outer entries reach `set()`. The // Delay the English inner write until both outer entries reach `set()`. The
// nested route calls `outerOne()` and then `outerTwo()`, which share `inner()`. // nested route calls `outerOne()` and then `outerTwo()`, which share `inner()`.
...@@ -19,14 +50,81 @@ const observedOuters = new Set() ...@@ -19,14 +50,81 @@ const observedOuters = new Set()
*/ */
module.exports = { module.exports = {
...defaultCacheHandler, ...defaultCacheHandler,
async get(cacheKey, softTags) {
if (
writeFailureState.id !== '' &&
cacheKey.includes(JSON.stringify(writeFailureState.id))
) {
writeFailureState.lookupKeys.push(cacheKey)
}
if (
writeFailureState.lookupHistory &&
cacheKey.includes(JSON.stringify(writeFailureState.id))
) {
// The outer function has a different argument shape, so only the inner
// function participates in these lookup barriers.
if (!cacheKey.includes('"outer"')) {
writeFailureState.initialLookupKey ||= cacheKey
if (cacheKey === writeFailureState.initialLookupKey) {
writeFailureState.initialLookupCount++
if (writeFailureState.initialLookupCount === 2) {
await writeFailureState.pendingInitialLookup
}
} else {
writeFailureState.specificLookupCount++
if (writeFailureState.specificLookupCount === 2) {
const snapshot = readHistoryEntry(cacheKey)
await writeFailureState.pendingSpecificLookup
return snapshot
}
}
}
}
return writeFailureState.lookupHistory &&
cacheKey.includes(JSON.stringify(writeFailureState.id))
? readHistoryEntry(cacheKey)
: defaultCacheHandler.get(cacheKey, softTags)
},
async set(cacheKey, pendingEntry) { async set(cacheKey, pendingEntry) {
const { tags } = await pendingEntry const { tags } = await pendingEntry
// Reject the English `set()` calls. The French request must still generate // Reject the English `set()` calls. The French request must still generate
// and store an entry with its own root parameter value. // and store an entry with its own root parameter value.
if (tags.includes(`write-failure:${writeFailureState.id}`)) { if (tags.includes(`write-failure:${writeFailureState.id}`)) {
if (tags.includes('write-failure-language:en')) { if (tags.includes('write-failure-language:en')) {
writeFailureState.failedWriteLanguages.push('en') const role = tags.some((tag) => tag.startsWith('_N_RP_'))
throw new Error('expected English cache write failure') ? 'redirect'
: 'value'
writeFailureState.startedWrites.push(role)
const secondGeneration = tags.includes(
'app-root-params-getters:write-generation:2'
)
if (
writeFailureState.lookupHistory &&
secondGeneration &&
role === 'value'
) {
await writeFailureState.pendingSecondWrite
} else if (
writeFailureState.heldWrite === role &&
(!writeFailureState.lookupHistory ||
tags.includes('app-root-params-getters:write-generation:1'))
) {
await writeFailureState.pendingWrite
}
if (
writeFailureState.failedWrite === 'both' ||
writeFailureState.failedWrite === role
) {
writeFailureState.failedWriteLanguages.push('en')
throw new Error('expected English cache write failure')
}
if (writeFailureState.lookupHistory) {
await saveHistoryEntry(cacheKey, pendingEntry)
} else {
await defaultCacheHandler.set(cacheKey, pendingEntry)
}
writeFailureState.writtenLanguages.push('en')
return
} }
await defaultCacheHandler.set(cacheKey, pendingEntry) await defaultCacheHandler.set(cacheKey, pendingEntry)
writeFailureState.writtenLanguages.push('fr') writeFailureState.writtenLanguages.push('fr')
......
...@@ -17,6 +17,22 @@ ...@@ -17,6 +17,22 @@
* @property {Promise<void> | null} pendingFill * @property {Promise<void> | null} pendingFill
* Keeps `readCachedValue()` pending. * Keeps `readCachedValue()` pending.
* @property {(() => void) | null} releaseFill * @property {(() => void) | null} releaseFill
* @property {'redirect' | 'value' | null} heldWrite
* @property {'both' | 'redirect' | 'value' | null} failedWrite
* @property {string[]} startedWrites
* @property {string[]} lookupKeys
* @property {Promise<void> | null} pendingWrite
* @property {(() => void) | null} releaseWrite
* @property {boolean} lookupHistory
* @property {string} initialLookupKey
* @property {number} initialLookupCount
* @property {number} specificLookupCount
* @property {Promise<void> | null} pendingInitialLookup
* @property {(() => void) | null} releaseInitialLookup
* @property {Promise<void> | null} pendingSpecificLookup
* @property {(() => void) | null} releaseSpecificLookup
* @property {Promise<void> | null} pendingSecondWrite
* @property {(() => void) | null} releaseSecondWrite
*/ */
/** /**
...@@ -32,4 +48,20 @@ module.exports = reference.writeFailureState ??= { ...@@ -32,4 +48,20 @@ module.exports = reference.writeFailureState ??= {
writtenLanguages: [], writtenLanguages: [],
pendingFill: null, pendingFill: null,
releaseFill: null, releaseFill: null,
heldWrite: null,
failedWrite: 'both',
startedWrites: [],
lookupKeys: [],
pendingWrite: null,
releaseWrite: null,
lookupHistory: false,
initialLookupKey: '',
initialLookupCount: 0,
specificLookupCount: 0,
pendingInitialLookup: null,
releaseInitialLookup: null,
pendingSpecificLookup: null,
releaseSpecificLookup: null,
pendingSecondWrite: null,
releaseSecondWrite: null,
} }
...@@ -174,10 +174,10 @@ describe('app-root-param-getters - cache - at runtime', () => { ...@@ -174,10 +174,10 @@ describe('app-root-param-getters - cache - at runtime', () => {
expect(await browser.elementById('dynamic').text()).toBe('dynamic') expect(await browser.elementById('dynamic').text()).toBe('dynamic')
}) })
// If the Resume Data Cache lookup missed during the resume (e.g. due // If the Resume Data Cache lookup missed during the resume (e.g. due to a
// to a key mismatch caused by root params), the "use cache" function // key mismatch caused by root params), the "use cache" function would
// would re-execute and produce a different random value than the one // re-execute and produce a different random value than the one in the
// in the prerendered shell, causing a hydration error. // prerendered shell, causing a hydration error.
await assertNoConsoleErrors(browser) await assertNoConsoleErrors(browser)
}) })
...@@ -252,8 +252,8 @@ describe('app-root-param-getters - cache - at runtime', () => { ...@@ -252,8 +252,8 @@ describe('app-root-param-getters - cache - at runtime', () => {
expect(await browser.elementById('lang-value').text()).toBe('fr') expect(await browser.elementById('lang-value').text()).toBe('fr')
}) })
// Wait for the cache to settle by verifying the random value is // Wait for the cache to settle by verifying the random value is stable
// stable across two consecutive loads. // across two consecutive loads.
let frRandom: string = '' let frRandom: string = ''
await retry(async () => { await retry(async () => {
await browser.loadPage(next.url + '/fr/ca/conditional-on-another-cache') await browser.loadPage(next.url + '/fr/ca/conditional-on-another-cache')
...@@ -262,8 +262,8 @@ describe('app-root-param-getters - cache - at runtime', () => { ...@@ -262,8 +262,8 @@ describe('app-root-param-getters - cache - at runtime', () => {
expect(await browser.elementById('random').text()).toBe(frRandom) expect(await browser.elementById('random').text()).toBe(frRandom)
}) })
// Different root params must now produce different entries because // Different root params must now produce different entries because lang
// lang is being read. // is being read.
await browser.loadPage(next.url + '/en/us/conditional-on-another-cache') await browser.loadPage(next.url + '/en/us/conditional-on-another-cache')
expect(await browser.elementById('lang-value').text()).toBe('en') expect(await browser.elementById('lang-value').text()).toBe('en')
...@@ -367,7 +367,8 @@ describe('app-root-param-getters - cache - at build', () => { ...@@ -367,7 +367,8 @@ describe('app-root-param-getters - cache - at build', () => {
}) })
if (isNextDev) { if (isNextDev) {
// we omit these tests in dev because they are duplicates semantically to the runtime fixture tested above // we omit these tests in dev because they are duplicates semantically to
// the runtime fixture tested above
it('noop in dev', () => {}) it('noop in dev', () => {})
} else { } else {
it('should allow using root params within a "use cache" at build time', async () => { it('should allow using root params within a "use cache" at build time', async () => {
...@@ -520,3 +521,191 @@ describe('app-root-param-getters - cache dedup with root params', () => { ...@@ -520,3 +521,191 @@ describe('app-root-param-getters - cache dedup with root params', () => {
} }
}) })
}) })
for (const { held, failure } of [
{ held: 'redirect', failure: '' },
{ held: 'value', failure: '' },
{ held: 'redirect', failure: 'value' },
{ held: 'value', failure: 'redirect' },
]) {
// Handler barriers require both requests to reach the same server process.
// @force-gate start
describe(`root cache writes: hold ${held}, fail ${failure}`, () => {
const { next } = nextTestSetup({
files: join(__dirname, 'fixtures', 'use-cache-dedup'),
env: { NEXT_PRIVATE_DEBUG_CACHE: '1' },
})
it('currently shares the producer lookup while waiting for both root-param writes', async () => {
const id = `write-order-${randomUUID()}`
const control = (action: string) =>
next.fetch(
`/write-failure-control?id=${id}&held=${held}&failure=${failure}`,
{ method: 'POST', body: action }
)
const readState = () =>
next.fetch('/write-failure-control').then(
(response) =>
response.json() as Promise<{
requestedLanguages: string[]
generatedLanguages: string[]
startedWrites: string[]
failedWriteLanguages: string[]
writtenLanguages: string[]
lookupKeys: string[]
}>
)
await control('hold-fill')
await control(`hold-${held}-write`)
const english = next.render$(`/ca/en/write-failure?id=${id}`)
let french: ReturnType<typeof next.render$> | undefined
try {
await retry(async () => {
expect((await readState()).generatedLanguages).toEqual(['en'])
})
french = next.render$(`/ca/fr/write-failure?id=${id}`)
await retry(async () => {
const state = await readState()
expect(state.requestedLanguages).toEqual(['en', 'fr'])
expect(state.lookupKeys).toHaveLength(1)
expect(
next.cliOutput
.split('\n')
.some(
(line) =>
line.includes('joining pending cross-request invocation') &&
line.includes(id)
)
).toBe(true)
})
await control('release-fill')
await retry(async () => {
const state = await readState()
expect(state.startedWrites).toEqual(
expect.arrayContaining(['redirect', 'value'])
)
if (failure !== '') {
expect(state.failedWriteLanguages).toEqual(['en'])
} else {
expect(state.writtenLanguages).toContain('en')
}
})
// The other write already settled. Root-key retries still have to wait
// for the held write, including when that settled write rejected.
const pendingState = await readState()
expect(pendingState.generatedLanguages).toEqual(['en'])
expect(pendingState.lookupKeys).toHaveLength(1)
await control('release-write')
const [englishPage, frenchPage] = await Promise.all([english, french])
expect(englishPage('#value').text()).toStartWith('en:')
const frenchValue = frenchPage('#value').text()
expect(frenchValue).toStartWith('fr:')
await retry(async () => {
expect((await readState()).writtenLanguages).toContain('fr')
})
expect(
(await next.render$(`/ca/fr/write-failure?id=${id}`))('#value').text()
).toBe(frenchValue)
} finally {
await control('release-fill')
await control('release-write')
await Promise.allSettled([english, french])
}
})
})
}
// The handler and both generation controls require a shared server process.
// @force-gate start
describe('root cache lookup across two pending generations', () => {
const { next } = nextTestSetup({
files: join(__dirname, 'fixtures', 'use-cache-dedup'),
env: { NEXT_PRIVATE_DEBUG_CACHE: '1' },
})
it('currently joins both generations without reading the redirected handler key', async () => {
const id = `lookup-history-${randomUUID()}`
const pathname = `/ca/en/write-failure?id=${id}`
const control = (action: string) =>
next.fetch(`/write-failure-control?id=${id}`, {
method: 'POST',
body: action,
})
const readState = () =>
next.fetch('/write-failure-control').then(
(response) =>
response.json() as Promise<{
generatedLanguages: string[]
requestedLanguages: string[]
initialLookupCount: number
specificLookupCount: number
writtenLanguages: string[]
}>
)
await control('hold-fill')
await control('hold-lookup-history')
const initialProducer = next.render$(pathname)
let consumer: ReturnType<typeof next.render$> | undefined
let specificProducer: ReturnType<typeof next.render$> | undefined
try {
await retry(async () => {
expect((await readState()).generatedLanguages).toEqual(['en'])
})
consumer = next.render$(`${pathname}&outer=1`)
await retry(async () => {
expect((await readState()).requestedLanguages).toEqual(['en', 'en'])
expect(
next.cliOutput
.split('\n')
.some(
(line) =>
line.includes('joining pending cross-request invocation') &&
line.includes(id)
)
).toBe(true)
})
// Read root params become known while the initial value write is held.
// The new request starts generation under the root-specific key.
await control('release-fill')
await retry(async () => {
expect((await readState()).writtenLanguages).toContain('en')
})
specificProducer = next.render$(pathname)
await retry(async () => {
expect((await readState()).generatedLanguages).toEqual(['en', 'en'])
})
const pendingState = await readState()
expect(pendingState.initialLookupCount).toBe(1)
expect(pendingState.specificLookupCount).toBe(1)
await control('release-write')
await retry(() => {
expect(
next.cliOutput
.split('\n')
.some(
(line) =>
line.includes('joining pending cross-request invocation') &&
line.includes(id) &&
line.includes('"lang","en"')
)
).toBe(true)
})
await control('release-second-write')
const [, specificPage, page] = await Promise.all([
initialProducer,
specificProducer,
consumer,
])
expect(page('#value').text()).toBe(specificPage('#value').text())
expect((await readState()).generatedLanguages).toEqual(['en', 'en'])
expect(page('#value').text()).toStartWith('en:')
} finally {
await control('release-fill')
await control('release-initial-lookup')
await control('release-specific-lookup')
await control('release-second-write')
await control('release-write')
await Promise.allSettled([initialProducer, consumer, specificProducer])
}
})
})
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