Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/workflows/multi-pod-e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Multi-pod E2E

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

permissions:
contents: read

jobs:
multi-pod:
name: Multi-pod (${{ matrix.storage }}, ${{ matrix.control }})
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
storage: [memory, redis]
control: [fixed, reverted]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "22"
- name: Build production standalone fixture
env:
STORAGE: ${{ matrix.storage }}
CONTROL: ${{ matrix.control }}
run: |
revert=0
if [ "$CONTROL" = reverted ]; then revert=1; fi
docker build -f k8s/e2e/Dockerfile \
--build-arg USE_CACHE="$STORAGE" --build-arg REVERT_TIMESTAMP="$revert" \
-t cache-e2e:pr .
- name: Create kind cluster
uses: helm/kind-action@v1.12.0
with:
cluster_name: cache-e2e
- name: Load fixture
run: kind load docker-image cache-e2e:pr --name cache-e2e
- name: Single save and five CMS bursts
env:
CONTROL: ${{ matrix.control }}
run: |
mkdir -p artifacts
set +e
bash k8s/e2e/run.sh 2>&1 | tee artifacts/driver.txt
result=${PIPESTATUS[0]}
set -e
if [ "$CONTROL" = reverted ]; then
# Infrastructure/build failures are not evidence that the regression was caught.
test "$result" -ne 0
grep -q 'STALE_CONTENT' artifacts/driver.txt
echo 'Negative control caught stale content as required.'
else
test "$result" -eq 0
grep -q 'MULTI_POD_PASS' artifacts/driver.txt
fi
- name: Collect pod logs and Redis tag records
if: always()
run: bash k8s/e2e/diagnostics.sh
- uses: actions/upload-artifact@v4
if: always()
with:
name: multi-pod-${{ matrix.storage }}-${{ matrix.control }}
path: artifacts/
retention-days: 7
- name: Remove disposable cluster
if: always()
run: kind delete cluster --name cache-e2e
22 changes: 22 additions & 0 deletions apps/e2e-test-app/app/api/e2e/multi-pod/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { revalidateTag } from "next/cache";
import { type NextRequest, NextResponse } from "next/server";

/** Test-only CMS webhook; the kind Secret supplies its bearer token. */
export async function POST(request: NextRequest) {
const secret = process.env.E2E_REVALIDATE_SECRET;
if (!secret || request.headers.get("authorization") !== `Bearer ${secret}`) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const body: unknown = await request.json().catch(() => null);
const tags = body && typeof body === "object" && "tags" in body ? body.tags : null;
if (
!Array.isArray(tags) ||
tags.length === 0 ||
tags.length > 19 ||
!tags.every((tag): tag is string => typeof tag === "string" && /^multi-pod-[\w-]+$/.test(tag))
) {
return NextResponse.json({ error: "Expected 1–19 multi-pod tags" }, { status: 400 });
}
for (const tag of tags) revalidateTag(tag, { expire: 0 });
return NextResponse.json({ pod: process.env.POD_NAME, tags });
}
36 changes: 36 additions & 0 deletions apps/e2e-test-app/app/e2e/multi-pod/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { cacheLife, cacheTag } from "next/cache";
import { cookies } from "next/headers";
import { Suspense } from "react";

export function generateStaticParams() {
return [{ id: "build" }];
}

async function Snapshot({ id }: { id: string }) {
"use cache";
cacheLife({ stale: 60, revalidate: 86400, expire: 172800 });
cacheTag(`multi-pod-${id}`);
if (id === "build") return <p data-testid="version">build</p>;
const origin = process.env.E2E_ORIGIN_URL;
if (!origin) throw new Error("E2E_ORIGIN_URL is required for the multi-pod fixture");
const response = await fetch(`${origin}/${encodeURIComponent(id)}`);
if (!response.ok) throw new Error("Multi-pod fixture origin failed");
return <p data-testid="version">{await response.text()}</p>;
}

async function RequestPart() {
await cookies();
return <p>Request content</p>;
}

export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
return (
<main>
<Snapshot id={id} />
<Suspense fallback={<p>Loading</p>}>
<RequestPart />
</Suspense>
</main>
);
}
6 changes: 5 additions & 1 deletion apps/e2e-test-app/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const handlers =
/** @type {import('next').NextConfig} */
const nextConfig = {
cacheComponents: true,
...(process.env.E2E_STANDALONE === "1" ? { output: "standalone" } : {}),

// Custom cacheLife profiles for testing
cacheLife: {
Expand Down Expand Up @@ -51,4 +52,7 @@ const config16_4_2 = {

export default process.env.E2E_CONFIG === "16.4.2"
? config16_4_2
: withCache(nextConfig, { handlers });
: withCache(nextConfig, {
handlers,
...(process.env.E2E_USE_CACHE ? { useCache: process.env.E2E_USE_CACHE } : {}),
});
49 changes: 49 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,52 @@ npm. See [auto-release.md](./auto-release.md) and
- **Next.js deploy tests**: Next.js's own caching suites against the package, on PRs that touch the package source
- **Publish**: Runs on version tags (tests + npm publish)
- **Version Check**: Runs daily to check for new Next.js versions

### Three-pod invalidation gate

`multi-pod-e2e.yml` builds the PR's production standalone fixture and loads it into a disposable
kind cluster: three app replicas behind a ClusterIP Service, one Redis, and a driver Job that
also acts as the fixture CMS origin. Both default memory storage and `useCache: "redis"` run.
The driver discovers all three ready Pod IPs from the Service Endpoints; it does not use a
Service port-forward, which would pin requests to one pod.

The driver warms 19 tagged pages on every pod, changes the CMS versions, sends one authenticated
webhook, and checks the first subsequent read of every page on every pod. It then repeats five
bursts: force regeneration, hold all 19 pre-save origin snapshots, change the CMS versions,
send 19 invalidations spaced 150 ms apart through the Service, observe their commits in Redis,
release the renders, and check every page directly on all three pods. Freshness assertions never
retry. Only setup and invalidation-commit barriers poll. The bearer token is generated for each
cluster and injected through a Kubernetes Secret.

Separate negative-control jobs build a throwaway image with the #104 page timestamp assignment
reverted. They must fail specifically with `STALE_CONTENT`; build, networking or timeout errors
do not satisfy the negative control. `REVERT_TIMESTAMP` defaults to `0` and never edits source
in the checkout. All four jobs have a ten-minute limit. Artifacts include the driver transcript,
pod logs/events, and Redis tag hash/index dumps, including on expected negative-control failures.

To reproduce with Docker, kind, kubectl, Node 22 and OpenSSL installed:

```bash
docker build -f k8s/e2e/Dockerfile \
--build-arg USE_CACHE=redis -t cache-e2e:pr .
kind create cluster --name cache-e2e
kind load docker-image cache-e2e:pr --name cache-e2e
bash k8s/e2e/run.sh
bash k8s/e2e/diagnostics.sh
kind delete cluster --name cache-e2e
```

For the negative control add `--build-arg REVERT_TIMESTAMP=1`; the driver must exit nonzero
with `STALE_CONTENT`. Use a fresh cluster for each run.

After the workflow has run, the repository owner should add these exact status checks to the
`main` branch protection/ruleset alongside the existing required checks:

- `Multi-pod (memory, fixed)`
- `Multi-pod (redis, fixed)`
- `Multi-pod (memory, reverted)`
- `Multi-pod (redis, reverted)`

Select GitHub Actions as their source. Do not make the negative-control failure itself a required
failed check: its job succeeds only when it observes the specific expected regression. Workflow
changes do not change branch protection automatically.
31 changes: 31 additions & 0 deletions k8s/e2e/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM node:22-bookworm-slim AS build
WORKDIR /app
RUN corepack enable
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY packages/cache-handler/package.json packages/cache-handler/package.json
COPY apps/e2e-test-app/package.json apps/e2e-test-app/package.json
RUN pnpm install --frozen-lockfile
COPY packages/cache-handler packages/cache-handler
COPY apps/e2e-test-app apps/e2e-test-app
# Negative control lives only in this throwaway image; never changes the checkout.
ARG REVERT_TIMESTAMP=0
RUN if [ "$REVERT_TIMESTAMP" = 1 ]; then \
sed -i 's/lastModified: this.renderStarts.get(cacheKey) ?? nowMs()/lastModified: nowMs()/' packages/cache-handler/src/handlers/redis.ts; \
fi
RUN pnpm --filter @mrjasonroy/cache-components-cache-handler build
ARG USE_CACHE=memory
ENV E2E_USE_CACHE=$USE_CACHE
ENV E2E_STANDALONE=1 NEXT_TELEMETRY_DISABLED=1 REDIS_URL=redis://redis:6379
RUN pnpm --filter e2e-test-app build
# Next cannot trace the handler's lazy ioredis require. Keep the locked production closure.
RUN pnpm --filter e2e-test-app deploy --prod --legacy /runtime-deps

FROM node:22-bookworm-slim
WORKDIR /app
ENV NODE_ENV=production HOSTNAME=0.0.0.0 PORT=3000 NEXT_TELEMETRY_DISABLED=1
COPY --from=build /app/apps/e2e-test-app/.next/standalone ./
COPY --from=build /runtime-deps/node_modules ./node_modules
COPY --from=build /app/apps/e2e-test-app/.next/static ./apps/e2e-test-app/.next/static
COPY k8s/e2e/driver.mjs ./apps/e2e-test-app/driver.mjs
USER node
CMD ["node", "apps/e2e-test-app/server.js"]
6 changes: 6 additions & 0 deletions k8s/e2e/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/node_modules
**/.next
**/dist
**/.turbo
.git
drafts
89 changes: 89 additions & 0 deletions k8s/e2e/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
apiVersion: v1
kind: Namespace
metadata:
name: cache-e2e
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: cache-e2e
spec:
replicas: 1
selector:
matchLabels: { app: redis }
template:
metadata:
labels: { app: redis }
spec:
containers:
- name: redis
image: redis:7.4-alpine
args: ["--maxmemory-policy", "noeviction"]
ports: [{ containerPort: 6379 }]
readinessProbe:
exec:
command: ["redis-cli", "ping"]
periodSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: cache-e2e
spec:
selector: { app: redis }
ports: [{ port: 6379, targetPort: 6379 }]
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
namespace: cache-e2e
spec:
replicas: 3
selector:
matchLabels: { app: cache-app }
template:
metadata:
labels: { app: cache-app }
spec:
containers:
- name: app
image: cache-e2e:pr
imagePullPolicy: Never
env:
- name: POD_NAME
valueFrom:
fieldRef: { fieldPath: metadata.name }
- { name: REDIS_URL, value: "redis://redis:6379" }
- { name: CACHE_KEY_PREFIX, value: "multi-pod:" }
- { name: E2E_ORIGIN_URL, value: "http://driver:8080" }
- name: E2E_REVALIDATE_SECRET
valueFrom:
secretKeyRef: { name: revalidate, key: token }
ports: [{ containerPort: 3000 }]
readinessProbe:
tcpSocket: { port: 3000 }
periodSeconds: 1
resources:
requests: { cpu: "100m", memory: "128Mi" }
limits: { memory: "768Mi" }
---
apiVersion: v1
kind: Service
metadata:
name: app
namespace: cache-e2e
spec:
selector: { app: cache-app }
ports: [{ port: 3000, targetPort: 3000 }]
---
apiVersion: v1
kind: Service
metadata:
name: driver
namespace: cache-e2e
spec:
selector: { job-name: driver }
ports: [{ port: 8080, targetPort: 8080 }]
12 changes: 12 additions & 0 deletions k8s/e2e/diagnostics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -u
mkdir -p artifacts
kubectl -n cache-e2e get pods -o wide > artifacts/pods.txt 2>&1
kubectl -n cache-e2e describe pods > artifacts/describe.txt 2>&1
kubectl -n cache-e2e get events --sort-by=.metadata.creationTimestamp > artifacts/events.txt 2>&1
for pod in $(kubectl -n cache-e2e get pods -o name); do
kubectl -n cache-e2e logs "$pod" --all-containers > "artifacts/${pod#pod/}.log" 2>&1
kubectl -n cache-e2e logs "$pod" --all-containers --previous > "artifacts/${pod#pod/}-previous.log" 2>&1 || true
done
kubectl -n cache-e2e exec deployment/redis -- redis-cli HGETALL multi-pod:tags > artifacts/redis-tags.txt 2>&1
kubectl -n cache-e2e exec deployment/redis -- redis-cli ZRANGE multi-pod:tag-index 0 -1 WITHSCORES > artifacts/redis-tag-index.txt 2>&1
Loading
Loading