|
| 1 | +// The O(1) gate in front of the completed-waitpoint record build. |
| 2 | +// |
| 3 | +// Deciding whether to build a record set by scanning a run's blocking waitpoints made every |
| 4 | +// resume, for every organisation, pay work proportional to its fan-in to reach the same answer: |
| 5 | +// no. The predicate moves that decision in front of the scan, and defaults to never. |
| 6 | +import { containerTest } from "@internal/testcontainers"; |
| 7 | +import { trace } from "@internal/tracing"; |
| 8 | +import { expect } from "vitest"; |
| 9 | +import { RunEngine } from "../index.js"; |
| 10 | +import { setTimeout } from "node:timers/promises"; |
| 11 | +import { setupAuthenticatedEnvironment, setupBackgroundWorker } from "./setup.js"; |
| 12 | + |
| 13 | +// The trigger has to reach the queue, and the completion resumes the run out of band, so both |
| 14 | +// need settling before the assertions read state back. |
| 15 | +const SETTLE_MS = 500; |
| 16 | + |
| 17 | +async function waitForResume( |
| 18 | + engine: RunEngine, |
| 19 | + runId: string |
| 20 | +): Promise<{ completedWaitpointIds: string[] }> { |
| 21 | + for (let attempt = 0; attempt < 20; attempt++) { |
| 22 | + const data = await engine.getRunExecutionData({ runId }); |
| 23 | + const ids = data?.completedWaitpoints?.map((w) => w.id) ?? []; |
| 24 | + if (ids.length > 0) { |
| 25 | + return { completedWaitpointIds: ids }; |
| 26 | + } |
| 27 | + await setTimeout(100); |
| 28 | + } |
| 29 | + return { completedWaitpointIds: [] }; |
| 30 | +} |
| 31 | + |
| 32 | +vi.setConfig({ testTimeout: 60_000 }); |
| 33 | + |
| 34 | +function engineWith( |
| 35 | + prisma: never, |
| 36 | + redisOptions: never, |
| 37 | + completedWaitpointRecordsEnabled?: (runId: string) => boolean |
| 38 | +) { |
| 39 | + return new RunEngine({ |
| 40 | + prisma, |
| 41 | + worker: { redis: redisOptions, workers: 1, tasksPerWorker: 10, pollIntervalMs: 100 }, |
| 42 | + queue: { redis: redisOptions }, |
| 43 | + runLock: { redis: redisOptions }, |
| 44 | + machines: { |
| 45 | + defaultMachine: "small-1x", |
| 46 | + machines: { |
| 47 | + "small-1x": { name: "small-1x" as const, cpu: 0.5, memory: 0.5, centsPerMs: 0.0001 }, |
| 48 | + }, |
| 49 | + baseCostInCents: 0.0001, |
| 50 | + }, |
| 51 | + tracer: trace.getTracer("test", "0.0.0"), |
| 52 | + ...(completedWaitpointRecordsEnabled && { completedWaitpointRecordsEnabled }), |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +describe("the completed-waitpoint records gate", () => { |
| 57 | + containerTest( |
| 58 | + "is consulted once per resume, and the resume is unaffected", |
| 59 | + async ({ prisma, redisOptions }) => { |
| 60 | + const consulted: string[] = []; |
| 61 | + const engine = engineWith(prisma as never, redisOptions as never, (runId) => { |
| 62 | + consulted.push(runId); |
| 63 | + return false; |
| 64 | + }); |
| 65 | + |
| 66 | + try { |
| 67 | + const env = await setupAuthenticatedEnvironment(prisma, "PRODUCTION"); |
| 68 | + const taskIdentifier = "test-task"; |
| 69 | + await setupBackgroundWorker(engine, env, taskIdentifier); |
| 70 | + |
| 71 | + const run = await engine.trigger( |
| 72 | + { |
| 73 | + number: 1, |
| 74 | + friendlyId: "run_gate1", |
| 75 | + environment: env, |
| 76 | + taskIdentifier, |
| 77 | + payload: "{}", |
| 78 | + payloadType: "application/json", |
| 79 | + context: {}, |
| 80 | + traceContext: {}, |
| 81 | + traceId: "t12345", |
| 82 | + spanId: "s12345", |
| 83 | + workerQueue: "main", |
| 84 | + queue: `task/${taskIdentifier}`, |
| 85 | + isTest: false, |
| 86 | + tags: [], |
| 87 | + }, |
| 88 | + prisma |
| 89 | + ); |
| 90 | + |
| 91 | + await setTimeout(SETTLE_MS); |
| 92 | + const dequeued = await engine.dequeueFromWorkerQueue({ |
| 93 | + consumerId: "test_12345", |
| 94 | + workerQueue: "main", |
| 95 | + }); |
| 96 | + const attempt = await engine.startRunAttempt({ |
| 97 | + runId: dequeued[0]!.run.id, |
| 98 | + snapshotId: dequeued[0]!.snapshot.id, |
| 99 | + }); |
| 100 | + |
| 101 | + // Block on a manual waitpoint, then release it: the release is what resumes the run and |
| 102 | + // reaches the gate. |
| 103 | + const waitpoint = await engine.createManualWaitpoint({ |
| 104 | + environmentId: env.id, |
| 105 | + projectId: env.projectId, |
| 106 | + }); |
| 107 | + await engine.blockRunWithWaitpoint({ |
| 108 | + runId: run.id, |
| 109 | + waitpoints: [waitpoint.waitpoint.id], |
| 110 | + projectId: env.project.id, |
| 111 | + organizationId: env.organization.id, |
| 112 | + }); |
| 113 | + |
| 114 | + await engine.completeWaitpoint({ id: waitpoint.waitpoint.id }); |
| 115 | + |
| 116 | + const { completedWaitpointIds } = await waitForResume(engine, run.id); |
| 117 | + |
| 118 | + // The run resumed, because a disabled gate only skips the record build... |
| 119 | + expect(completedWaitpointIds).toContain(waitpoint.waitpoint.id); |
| 120 | + // ...and the gate was consulted for it, which is what puts it in the resume path. |
| 121 | + expect(consulted).toContain(run.id); |
| 122 | + expect(attempt.run.id).toBe(run.id); |
| 123 | + } finally { |
| 124 | + await engine.quit(); |
| 125 | + } |
| 126 | + } |
| 127 | + ); |
| 128 | + |
| 129 | + containerTest("defaults to never, so no resume consults it", async ({ prisma, redisOptions }) => { |
| 130 | + // No predicate supplied, which is every deployment until the store that consumes a record set |
| 131 | + // is wired. The default has to be a constant false rather than a scan, or the cost this gate |
| 132 | + // exists to remove comes straight back for everyone. |
| 133 | + const engine = engineWith(prisma as never, redisOptions as never); |
| 134 | + |
| 135 | + try { |
| 136 | + const env = await setupAuthenticatedEnvironment(prisma, "PRODUCTION"); |
| 137 | + const taskIdentifier = "test-task"; |
| 138 | + await setupBackgroundWorker(engine, env, taskIdentifier); |
| 139 | + |
| 140 | + const run = await engine.trigger( |
| 141 | + { |
| 142 | + number: 1, |
| 143 | + friendlyId: "run_gate2", |
| 144 | + environment: env, |
| 145 | + taskIdentifier, |
| 146 | + payload: "{}", |
| 147 | + payloadType: "application/json", |
| 148 | + context: {}, |
| 149 | + traceContext: {}, |
| 150 | + traceId: "t22345", |
| 151 | + spanId: "s22345", |
| 152 | + workerQueue: "main", |
| 153 | + queue: `task/${taskIdentifier}`, |
| 154 | + isTest: false, |
| 155 | + tags: [], |
| 156 | + }, |
| 157 | + prisma |
| 158 | + ); |
| 159 | + |
| 160 | + await setTimeout(SETTLE_MS); |
| 161 | + const dequeued = await engine.dequeueFromWorkerQueue({ |
| 162 | + consumerId: "test_22345", |
| 163 | + workerQueue: "main", |
| 164 | + }); |
| 165 | + await engine.startRunAttempt({ |
| 166 | + runId: dequeued[0]!.run.id, |
| 167 | + snapshotId: dequeued[0]!.snapshot.id, |
| 168 | + }); |
| 169 | + |
| 170 | + const waitpoint = await engine.createManualWaitpoint({ |
| 171 | + environmentId: env.id, |
| 172 | + projectId: env.projectId, |
| 173 | + }); |
| 174 | + await engine.blockRunWithWaitpoint({ |
| 175 | + runId: run.id, |
| 176 | + waitpoints: [waitpoint.waitpoint.id], |
| 177 | + projectId: env.project.id, |
| 178 | + organizationId: env.organization.id, |
| 179 | + }); |
| 180 | + |
| 181 | + await engine.completeWaitpoint({ id: waitpoint.waitpoint.id }); |
| 182 | + |
| 183 | + const { completedWaitpointIds } = await waitForResume(engine, run.id); |
| 184 | + expect(completedWaitpointIds).toContain(waitpoint.waitpoint.id); |
| 185 | + } finally { |
| 186 | + await engine.quit(); |
| 187 | + } |
| 188 | + }); |
| 189 | +}); |
0 commit comments