|
| 1 | +// Repro for the checkpoint WAIT_FOR_BATCH replica-lag stall (createCheckpoint.server.ts:148-181). |
| 2 | +// |
| 3 | +// The service resolves the batch via `runStore.findBatchTaskRunByFriendlyId(friendlyId, envId)` with |
| 4 | +// NO client, so the read is served from the REPLICA. Its decision hinges on `batchRun.resumedAt`: |
| 5 | +// resumedAt set -> return keepRunAlive:true (batch already resumed; the run must keep executing) |
| 6 | +// resumedAt null -> fall through -> create the checkpoint -> SUSPEND the run |
| 7 | +// If the batch just resumed (primary has resumedAt), but the replica still lags (resumedAt null), the |
| 8 | +// service suspends a run whose batch already completed -> it stalls until a sweep. The sibling |
| 9 | +// WAIT_FOR_TASK arm reads the primary (this._prisma); only the batch arm defaults to the replica. |
| 10 | +// |
| 11 | +// This is invisible to the normal single-DB harness (no lag). We reintroduce the lag with the shared |
| 12 | +// `laggingReplica` primitive: the store's replica is frozen at the pre-resume snapshot while the |
| 13 | +// primary advances. RED = the current (client-less, replica) read -> SUSPEND. GREEN = threading the |
| 14 | +// primary (the one-line fix) -> KEEP_ALIVE. |
| 15 | + |
| 16 | +import { laggingReplica, postgresTest } from "@internal/testcontainers"; |
| 17 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 18 | +import { describe, expect } from "vitest"; |
| 19 | +import { PostgresRunStore } from "./PostgresRunStore.js"; |
| 20 | +import type { ReadClient } from "./types.js"; |
| 21 | + |
| 22 | +type BatchRow = { resumedAt: Date | null } | null; |
| 23 | + |
| 24 | +// A line-for-line mirror of the service's WAIT_FOR_BATCH pre-check. `readClient` models the fix: the |
| 25 | +// buggy code passes nothing (replica default); the fix threads the primary. |
| 26 | +async function precheckWaitForBatch( |
| 27 | + store: PostgresRunStore, |
| 28 | + batchFriendlyId: string, |
| 29 | + environmentId: string, |
| 30 | + readClient?: ReadClient |
| 31 | +): Promise<"DROP_RUN" | "KEEP_ALIVE" | "SUSPEND"> { |
| 32 | + const batchRun = (await store.findBatchTaskRunByFriendlyId( |
| 33 | + batchFriendlyId, |
| 34 | + environmentId, |
| 35 | + undefined, |
| 36 | + readClient |
| 37 | + )) as BatchRow; |
| 38 | + if (!batchRun) return "DROP_RUN"; // keepRunAlive:false |
| 39 | + if (batchRun.resumedAt) return "KEEP_ALIVE"; // batch already resumed -> run continues |
| 40 | + return "SUSPEND"; // falls through -> checkpoint created -> run suspended |
| 41 | +} |
| 42 | + |
| 43 | +async function seedEnvironment(prisma: PrismaClient, suffix: string) { |
| 44 | + const organization = await prisma.organization.create({ |
| 45 | + data: { title: `Org ${suffix}`, slug: `org-${suffix}` }, |
| 46 | + }); |
| 47 | + const project = await prisma.project.create({ |
| 48 | + data: { |
| 49 | + name: `Project ${suffix}`, |
| 50 | + slug: `project-${suffix}`, |
| 51 | + externalRef: `proj_${suffix}`, |
| 52 | + organizationId: organization.id, |
| 53 | + }, |
| 54 | + }); |
| 55 | + const environment = await prisma.runtimeEnvironment.create({ |
| 56 | + data: { |
| 57 | + type: "DEVELOPMENT", |
| 58 | + slug: "dev", |
| 59 | + projectId: project.id, |
| 60 | + organizationId: organization.id, |
| 61 | + apiKey: `tr_dev_${suffix}`, |
| 62 | + pkApiKey: `pk_dev_${suffix}`, |
| 63 | + shortcode: `short_${suffix}`, |
| 64 | + }, |
| 65 | + }); |
| 66 | + return { organization, project, environment }; |
| 67 | +} |
| 68 | + |
| 69 | +describe("checkpoint WAIT_FOR_BATCH under replica lag", () => { |
| 70 | + postgresTest( |
| 71 | + "a batch resumed on the primary but stale on the replica suspends an already-resumed run", |
| 72 | + async ({ prisma }) => { |
| 73 | + const { environment } = await seedEnvironment(prisma, "ckpt_lag"); |
| 74 | + const friendlyId = "batch_ckpt_lag"; |
| 75 | + const batch = await prisma.batchTaskRun.create({ |
| 76 | + data: { friendlyId, runtimeEnvironmentId: environment.id }, |
| 77 | + }); |
| 78 | + |
| 79 | + // Snapshot the batch as the replica still sees it (pre-resume: resumedAt = null). |
| 80 | + const staleBatch = await prisma.batchTaskRun.findFirstOrThrow({ where: { id: batch.id } }); |
| 81 | + expect(staleBatch.resumedAt).toBeNull(); |
| 82 | + |
| 83 | + // The batch completes and resumes the parent: primary now has resumedAt set... |
| 84 | + await prisma.batchTaskRun.update({ where: { id: batch.id }, data: { resumedAt: new Date() } }); |
| 85 | + |
| 86 | + // ...but the replica lags, frozen at the pre-resume snapshot. |
| 87 | + const replica = laggingReplica(prisma, [ |
| 88 | + { model: "batchTaskRun", mode: "frozen", rows: [staleBatch] }, |
| 89 | + ]); |
| 90 | + const store = new PostgresRunStore({ |
| 91 | + prisma, |
| 92 | + readOnlyPrisma: replica.client, |
| 93 | + schemaVariant: "legacy", |
| 94 | + }); |
| 95 | + |
| 96 | + // RED - the current service call (no client -> replica): stale null -> SUSPEND an already-resumed run. |
| 97 | + const buggy = await precheckWaitForBatch(store, friendlyId, environment.id); |
| 98 | + expect(replica.wasHit("batchTaskRun")).toBe(true); // proves it read the (stale) replica |
| 99 | + expect(buggy).toBe("SUSPEND"); // the stall bug |
| 100 | + |
| 101 | + // GREEN - the fix (thread the primary): sees resumedAt -> keep the run alive. |
| 102 | + const fixed = await precheckWaitForBatch(store, friendlyId, environment.id, prisma); |
| 103 | + expect(fixed).toBe("KEEP_ALIVE"); |
| 104 | + } |
| 105 | + ); |
| 106 | +}); |
0 commit comments