|
| 1 | +import { z } from "zod"; |
| 2 | +import { isValidShardChar } from "@trigger.dev/core/v3/isomorphic"; |
| 3 | +import { isValidDatabaseUrl } from "~/utils/db"; |
| 4 | + |
| 5 | +const KnobsSchema = z |
| 6 | + .object({ |
| 7 | + writerPoolTimeout: z.number().int().optional(), |
| 8 | + writerConnectionTimeout: z.number().int().optional(), |
| 9 | + writerDriverAdapter: z.boolean().optional(), |
| 10 | + connectionLimit: z.number().int().optional(), |
| 11 | + replicaConnectionLimit: z.number().int().optional(), |
| 12 | + replicaPoolTimeout: z.number().int().optional(), |
| 13 | + replicaConnectionTimeout: z.number().int().optional(), |
| 14 | + replicaDriverAdapter: z.boolean().optional(), |
| 15 | + transactionMaxWaitMs: z.number().int().optional(), |
| 16 | + transactionStartRetryEnabled: z.boolean().optional(), |
| 17 | + transactionStartRetryMaxAttempts: z.number().int().optional(), |
| 18 | + transactionStartRetryBackoffMinMs: z.number().int().optional(), |
| 19 | + transactionStartRetryBackoffMaxMs: z.number().int().optional(), |
| 20 | + transactionStartRetryBudgetPerSec: z.number().int().optional(), |
| 21 | + transactionStartRetryBudgetBurst: z.number().int().optional(), |
| 22 | + }) |
| 23 | + .strict(); |
| 24 | +export type RunOpsShardKnobs = z.infer<typeof KnobsSchema>; |
| 25 | + |
| 26 | +const ReplicationSchema = z.object({ |
| 27 | + slotName: z.string().min(1), |
| 28 | + publicationName: z.string().min(1), |
| 29 | + originGeneration: z.number().int().min(2).max(255), |
| 30 | +}); |
| 31 | + |
| 32 | +const DescriptorSchema = z |
| 33 | + .object({ |
| 34 | + key: z.string().refine(isValidShardChar, "shard key must be a single [a-z0-9] char"), |
| 35 | + region: z.string().min(1), |
| 36 | + url: z.string().refine(isValidDatabaseUrl, "url is invalid").optional(), |
| 37 | + replicaUrl: z.string().refine(isValidDatabaseUrl, "replicaUrl is invalid").optional(), |
| 38 | + directUrl: z.string().refine(isValidDatabaseUrl, "directUrl is invalid").optional(), |
| 39 | + replication: ReplicationSchema.optional(), |
| 40 | + knobs: KnobsSchema.optional(), |
| 41 | + aliasOf: z.literal("new").optional(), |
| 42 | + }) |
| 43 | + .strict() |
| 44 | + .superRefine((d, ctx) => { |
| 45 | + const hasUrl = d.url !== undefined; |
| 46 | + const hasAlias = d.aliasOf !== undefined; |
| 47 | + if (hasUrl === hasAlias) { |
| 48 | + ctx.addIssue({ |
| 49 | + code: z.ZodIssueCode.custom, |
| 50 | + message: "exactly one of url or aliasOf is required", |
| 51 | + }); |
| 52 | + } |
| 53 | + if (!hasAlias && d.replication === undefined) { |
| 54 | + ctx.addIssue({ |
| 55 | + code: z.ZodIssueCode.custom, |
| 56 | + message: "replication is required unless aliasOf is set", |
| 57 | + }); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | +export type RunOpsShardDescriptor = z.infer<typeof DescriptorSchema>; |
| 62 | + |
| 63 | +// Boot-validated transform, in the style of parseMachinePresetCsv. Undefined and "" both mean the |
| 64 | +// off state and resolve to []. The undefined guard is load-bearing: an unguarded JSON.parse would |
| 65 | +// kill every single-DB boot, which never sets this variable. |
| 66 | +export function parseRunOpsShards( |
| 67 | + raw: string | undefined, |
| 68 | + ctx: z.RefinementCtx |
| 69 | +): RunOpsShardDescriptor[] { |
| 70 | + if (raw === undefined || raw.trim() === "") return []; |
| 71 | + |
| 72 | + let parsed: unknown; |
| 73 | + try { |
| 74 | + parsed = JSON.parse(raw); |
| 75 | + } catch { |
| 76 | + ctx.addIssue({ code: z.ZodIssueCode.custom, message: "RUN_OPS_SHARDS is not valid JSON" }); |
| 77 | + return z.NEVER; |
| 78 | + } |
| 79 | + |
| 80 | + const result = z.array(DescriptorSchema).safeParse(parsed); |
| 81 | + if (!result.success) { |
| 82 | + for (const issue of result.error.issues) { |
| 83 | + ctx.addIssue({ |
| 84 | + code: z.ZodIssueCode.custom, |
| 85 | + message: `RUN_OPS_SHARDS[${issue.path.join(".")}]: ${issue.message}`, |
| 86 | + }); |
| 87 | + } |
| 88 | + return z.NEVER; |
| 89 | + } |
| 90 | + |
| 91 | + const keys = new Set<string>(); |
| 92 | + const gens = new Set<number>(); |
| 93 | + for (const d of result.data) { |
| 94 | + if (keys.has(d.key)) { |
| 95 | + ctx.addIssue({ |
| 96 | + code: z.ZodIssueCode.custom, |
| 97 | + message: `RUN_OPS_SHARDS: duplicate key ${d.key}`, |
| 98 | + }); |
| 99 | + return z.NEVER; |
| 100 | + } |
| 101 | + keys.add(d.key); |
| 102 | + if (d.replication) { |
| 103 | + if (gens.has(d.replication.originGeneration)) { |
| 104 | + ctx.addIssue({ |
| 105 | + code: z.ZodIssueCode.custom, |
| 106 | + message: `RUN_OPS_SHARDS: duplicate originGeneration ${d.replication.originGeneration}`, |
| 107 | + }); |
| 108 | + return z.NEVER; |
| 109 | + } |
| 110 | + gens.add(d.replication.originGeneration); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return result.data; |
| 115 | +} |
| 116 | + |
| 117 | +// A non-empty shard list requires the gen-1 new store, because gen-1 v1 ids resolve to "new" |
| 118 | +// forever (append-only). Pure so the boot refinement and its test share one rule. |
| 119 | +export function validateShardListAgainstNewUrl( |
| 120 | + shards: RunOpsShardDescriptor[], |
| 121 | + newUrl: string | undefined |
| 122 | +): boolean { |
| 123 | + return shards.length === 0 || !!newUrl; |
| 124 | +} |
0 commit comments