Unverified Commit 4fed142e authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Cover cache reuse after failed handler writes (#100976)

The tests verify that a request reuses its generated entry after its
cache handler write fails. The next cached invocation starts in a later
task, after failed-write cleanup has completed, so pending generation
cannot satisfy the assertion by itself.

The background revalidation test verifies that a later stale cache hit
retries a failed cache handler write and persists a replacement entry.
These cases extend the joining-action failure coverage added in PR
#100652 and protect the cache coordination changes in PR #100511.
parent edd20185
...@@ -40,8 +40,8 @@ describe('app-custom-cache-handler-errors - get throws', () => { ...@@ -40,8 +40,8 @@ describe('app-custom-cache-handler-errors - get throws', () => {
} }
`) `)
} else { } else {
// TODO(veil): Webpack renders the cache handler frame as a file:// // TODO(veil): Webpack renders the cache handler frame as a file:// URL,
// URL, which the redbox matcher flags as unintended. // which the redbox matcher flags as unintended.
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "CustomCacheHandler.get failed", "description": "CustomCacheHandler.get failed",
...@@ -153,6 +153,54 @@ describe('app-custom-cache-handler-errors - lookup recovery', () => { ...@@ -153,6 +153,54 @@ describe('app-custom-cache-handler-errors - lookup recovery', () => {
env: { CACHE_HANDLER_THROW_ON: 'retry-get' }, env: { CACHE_HANDLER_THROW_ON: 'retry-get' },
}) })
// Development's front handler completes its write before the configured
// handler fails. This test requires the configured handler's write lifetime.
// @force-gate start
it('retains generated data in the producer request after its handler write fails', async () => {
const id = randomUUID()
const pathname = `/lookup-retry?id=${id}&retention`
const control = (action: string) =>
next.fetch(pathname, { method: 'POST', body: action })
const readState = () =>
next.fetch(`${pathname}&state`).then(
(response) =>
response.json() as Promise<{
lookupCount: number
generationCount: number
writeStarted: boolean
writeFailed: boolean
}>
)
await control('fail-write-and-hold-read')
const rendering = next.fetch(pathname)
try {
await retry(async () => {
expect((await readState()).writeStarted).toBe(true)
})
await control('release-write')
await retry(async () => {
expect((await readState()).writeFailed).toBe(true)
})
// The next call starts in a later task, after failed-write cleanup runs.
await control('release-read')
const response = await rendering
expect(response.status).toBe(200)
const { first, second } = await response.json()
expect(first).toEqual(expect.any(Number))
expect(second).toBe(first)
expect(await readState()).toEqual({
lookupCount: 1,
generationCount: 1,
writeStarted: true,
writeFailed: true,
})
} finally {
await control('release-write')
await control('release-read')
await Promise.allSettled([rendering])
}
})
it('retries a shared failed lookup and shares its successful fill while the write is pending', async () => { it('retries a shared failed lookup and shares its successful fill while the write is pending', async () => {
const id = randomUUID() const id = randomUUID()
const outputIndex = next.cliOutput.length const outputIndex = next.cliOutput.length
...@@ -237,7 +285,8 @@ describe('app-custom-cache-handler-errors - shared write failure', () => { ...@@ -237,7 +285,8 @@ describe('app-custom-cache-handler-errors - shared write failure', () => {
.elementById('shared-write-render') .elementById('shared-write-render')
.text() .text()
// The held write keeps the producer's fill available for the action to join. // The held write keeps the producer's fill available for the action to
// join.
await control('hold-write') await control('hold-write')
const producer = next.render$(`/shared-write-failure/producer?id=${id}`) const producer = next.render$(`/shared-write-failure/producer?id=${id}`)
try { try {
......
...@@ -6,12 +6,22 @@ async function readCachedValue(id: string) { ...@@ -6,12 +6,22 @@ async function readCachedValue(id: string) {
'use cache' 'use cache'
cacheLife('hours') cacheLife('hours')
cacheTag(`lookup-retry:${id}`) cacheTag(`lookup-retry:${id}`)
lookupRetryState.generationCount++
return Math.random() return Math.random()
} }
export async function GET(request: Request) { export async function GET(request: Request) {
await connection() await connection()
if (new URL(request.url).searchParams.has('state')) { const searchParams = new URL(request.url).searchParams
if (searchParams.has('state') && searchParams.has('retention')) {
return Response.json({
lookupCount: lookupRetryState.lookupCount,
generationCount: lookupRetryState.generationCount,
writeStarted: lookupRetryState.writeStarted,
writeFailed: lookupRetryState.writeFailed,
})
}
if (searchParams.has('state')) {
return Response.json({ return Response.json({
lookupCount: lookupRetryState.lookupCount, lookupCount: lookupRetryState.lookupCount,
writeStarted: lookupRetryState.writeStarted, writeStarted: lookupRetryState.writeStarted,
...@@ -23,6 +33,12 @@ export async function GET(request: Request) { ...@@ -23,6 +33,12 @@ export async function GET(request: Request) {
if (id === null) { if (id === null) {
return new Response(null, { status: 400 }) return new Response(null, { status: 400 })
} }
if (searchParams.has('retention')) {
const first = await readCachedValue(id)
await lookupRetryState.pendingRead
const second = await readCachedValue(id)
return Response.json({ first, second })
}
// Both concurrent calls must reject from the same handler `get()`. // Both concurrent calls must reject from the same handler `get()`.
const results = await Promise.allSettled([ const results = await Promise.allSettled([
readCachedValue(id), readCachedValue(id),
...@@ -44,12 +60,38 @@ export async function GET(request: Request) { ...@@ -44,12 +60,38 @@ export async function GET(request: Request) {
export async function POST(request: Request) { export async function POST(request: Request) {
switch (await request.text()) { switch (await request.text()) {
case 'fail-write-and-hold-read': {
const id = new URL(request.url).searchParams.get('id')
if (id === null) {
return new Response(null, { status: 400 })
}
lookupRetryState.id = id
lookupRetryState.failNextLookup = false
lookupRetryState.failWrite = true
lookupRetryState.writeFailed = false
lookupRetryState.writeStarted = false
lookupRetryState.lookupCount = 0
lookupRetryState.generationCount = 0
lookupRetryState.pendingWrite = new Promise((resolve) => {
lookupRetryState.releaseWrite = resolve
})
lookupRetryState.pendingRead = new Promise((resolve) => {
lookupRetryState.releaseRead = resolve
})
break
}
case 'release-read':
lookupRetryState.releaseRead?.()
lookupRetryState.releaseRead = null
lookupRetryState.pendingRead = null
break
case 'fail-lookup-and-hold-write': { case 'fail-lookup-and-hold-write': {
const id = new URL(request.url).searchParams.get('id') const id = new URL(request.url).searchParams.get('id')
if (id === null) { if (id === null) {
return new Response(null, { status: 400 }) return new Response(null, { status: 400 })
} }
lookupRetryState.id = id lookupRetryState.id = id
lookupRetryState.failWrite = false
lookupRetryState.failNextLookup = true lookupRetryState.failNextLookup = true
lookupRetryState.lookupCount = 0 lookupRetryState.lookupCount = 0
lookupRetryState.writeStarted = false lookupRetryState.writeStarted = false
......
...@@ -14,6 +14,11 @@ ...@@ -14,6 +14,11 @@
* @property {Promise<void> | null} pendingWrite * @property {Promise<void> | null} pendingWrite
* Keeps `set()` pending after it receives the cache entry. * Keeps `set()` pending after it receives the cache entry.
* @property {(() => void) | null} releaseWrite * @property {(() => void) | null} releaseWrite
* @property {boolean} failWrite
* @property {boolean} writeFailed
* @property {number} generationCount
* @property {Promise<void> | null} pendingRead
* @property {(() => void) | null} releaseRead
*/ */
/** /**
...@@ -30,4 +35,9 @@ module.exports = reference.lookupRetryState ??= { ...@@ -30,4 +35,9 @@ module.exports = reference.lookupRetryState ??= {
cacheReadsCompleted: false, cacheReadsCompleted: false,
pendingWrite: null, pendingWrite: null,
releaseWrite: null, releaseWrite: null,
failWrite: false,
writeFailed: false,
generationCount: 0,
pendingRead: null,
releaseRead: null,
} }
...@@ -30,6 +30,10 @@ module.exports = { ...@@ -30,6 +30,10 @@ module.exports = {
} }
lookupRetryState.writeStarted = true lookupRetryState.writeStarted = true
await lookupRetryState.pendingWrite await lookupRetryState.pendingWrite
if (lookupRetryState.failWrite) {
lookupRetryState.writeFailed = true
throw new Error('expected retained cache write failure')
}
await cacheHandler.set(cacheKey, pendingEntry) await cacheHandler.set(cacheKey, pendingEntry)
lookupRetryState.writeCompleted = true lookupRetryState.writeCompleted = true
}, },
......
...@@ -8,11 +8,16 @@ const { setTimeout } = require('timers/promises') ...@@ -8,11 +8,16 @@ const { setTimeout } = require('timers/promises')
* in-memory handler which expires entries at revalidate time. * in-memory handler which expires entries at revalidate time.
*/ */
/** @type {Map<string, import('next/dist/server/lib/cache-handlers/types').CacheEntry>} */ /**
* @type {Map<string, import('next/dist/server/lib/cache-handlers/types').CacheEntry>}
*/
const store = new Map() const store = new Map()
/** @type {Map<string, Promise<void>>} */ /**
* @type {Map<string, Promise<void>>}
*/
const pendingSets = new Map() const pendingSets = new Map()
const writeAttempts = new Map()
/** /**
* @type {import('next/dist/server/lib/cache-handlers/types').CacheHandler} * @type {import('next/dist/server/lib/cache-handlers/types').CacheHandler}
...@@ -51,7 +56,9 @@ const cacheHandler = { ...@@ -51,7 +56,9 @@ const cacheHandler = {
}, },
async set(cacheKey, pendingEntry) { async set(cacheKey, pendingEntry) {
/** @type {() => void} */ /**
* @type {() => void}
*/
let resolvePending = () => {} let resolvePending = () => {}
const pendingPromise = new Promise((resolve) => { const pendingPromise = new Promise((resolve) => {
resolvePending = /** @type {() => void} */ (resolve) resolvePending = /** @type {() => void} */ (resolve)
...@@ -60,6 +67,17 @@ const cacheHandler = { ...@@ -60,6 +67,17 @@ const cacheHandler = {
try { try {
const entry = await pendingEntry const entry = await pendingEntry
const failureTag = entry.tags.find((tag) =>
tag.startsWith('caller-context:failed-refresh-')
)
if (failureTag !== undefined) {
const attempt = (writeAttempts.get(cacheKey) ?? 0) + 1
writeAttempts.set(cacheKey, attempt)
if (attempt === 2) {
console.log('PersistentCacheHandler::write-failed', failureTag)
throw new Error('expected background cache write failure')
}
}
const [value, clonedValue] = entry.value.tee() const [value, clonedValue] = entry.value.tee()
entry.value = value entry.value = value
...@@ -75,8 +93,14 @@ const cacheHandler = { ...@@ -75,8 +93,14 @@ const cacheHandler = {
store.set(cacheKey, entry) store.set(cacheKey, entry)
console.log('PersistentCacheHandler::set', cacheKey) console.log('PersistentCacheHandler::set', cacheKey)
if (failureTag !== undefined) {
console.log('PersistentCacheHandler::write-completed', failureTag)
}
} catch (err) { } catch (err) {
console.log('PersistentCacheHandler::set', cacheKey, 'failed', err) console.log('PersistentCacheHandler::set', cacheKey, 'failed', err)
if (cacheKey.includes('failed-refresh-')) {
throw err
}
} finally { } finally {
resolvePending() resolvePending()
pendingSets.delete(cacheKey) pendingSets.delete(cacheKey)
......
...@@ -415,6 +415,37 @@ describe('use-cache-swr', () => { ...@@ -415,6 +415,37 @@ describe('use-cache-swr', () => {
} }
}) })
// Development persists into its front cache before the configured write
// fails, so that failure does not leave the same stale entry to retry.
// @force-gate start
it('retries background revalidation after its handler write fails', async () => {
const id = `failed-refresh-${randomUUID()}`
const initial = await warmData(id)
const pathname = `/caller-context/warm?id=${id}`
const stale = await next.fetch(pathname).then((response) => response.json())
expect(stale.value).toBe(initial.value)
await retry(() => {
expect(getOutput()).toContain(
`PersistentCacheHandler::write-failed caller-context:${id}`
)
})
expect(dataRequestCountsByKey.get(`fill:${id}`)).toBe(2)
const retried = await next
.fetch(pathname)
.then((response) => response.json())
expect(retried.value).toBe(initial.value)
await retry(() => {
expect(dataRequestCountsByKey.get(`fill:${id}`)).toBe(3)
expect(getOutput()).toContain(
`PersistentCacheHandler::write-completed caller-context:${id}`
)
})
const refreshed = await next
.fetch(pathname)
.then((response) => response.json())
expect(refreshed.value).not.toBe(initial.value)
})
it('should serve stale data and then refreshed data on a subsequent request', async () => { it('should serve stale data and then refreshed data on a subsequent request', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
const initialOuter = await browser.elementById('outer-data').text() const initialOuter = await browser.elementById('outer-data').text()
......
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