|
8 | 8 | type PrismaTransactionOptions, |
9 | 9 | } from "@trigger.dev/database"; |
10 | 10 | import { RunOpsPrismaClient } from "@internal/run-ops-database"; |
| 11 | +import { markReadReplicaClient } from "@internal/run-store"; |
11 | 12 | import invariant from "tiny-invariant"; |
12 | 13 | import { z } from "zod"; |
13 | 14 | import { env } from "./env.server"; |
@@ -170,7 +171,11 @@ export const prisma = singleton("prisma", () => |
170 | 171 |
|
171 | 172 | export const $replica: PrismaReplicaClient = singleton("replica", () => { |
172 | 173 | const replica = getReplicaClient(); |
173 | | - return replica ? captureInfrastructureErrors(tagDatasource("replica", replica)) : prisma; |
| 174 | + // Brand ONLY a real replica so the run-store routing layer keeps replica reads off the primary. |
| 175 | + // No replica configured → fall back to the writer `prisma`, which must stay UNBRANDED. |
| 176 | + return replica |
| 177 | + ? markReadReplicaClient(captureInfrastructureErrors(tagDatasource("replica", replica))) |
| 178 | + : prisma; |
174 | 179 | }); |
175 | 180 |
|
176 | 181 | export type RunOpsClients = { writer: PrismaClient; replica: PrismaReplicaClient }; |
@@ -254,9 +259,14 @@ const runOpsTopology: RunOpsTopology = singleton("runOpsTopology", () => { |
254 | 259 | captureInfraErrorsRunOps( |
255 | 260 | tagDatasourceRunOps("writer", buildRunOpsWriterClient({ url, clientType })) |
256 | 261 | ), |
| 262 | + // Brand the run-ops replica (only built for a real replica URL) so routed replica reads stay |
| 263 | + // off the primary. When no replica URL is set, selectRunOpsTopology reuses the writer here — |
| 264 | + // which this callback never touches, so the writer stays unbranded. |
257 | 265 | buildNewReplica: (url, clientType) => |
258 | | - captureInfraErrorsRunOps( |
259 | | - tagDatasourceRunOps("replica", buildRunOpsReplicaClient({ url, clientType })) |
| 266 | + markReadReplicaClient( |
| 267 | + captureInfraErrorsRunOps( |
| 268 | + tagDatasourceRunOps("replica", buildRunOpsReplicaClient({ url, clientType })) |
| 269 | + ) |
260 | 270 | ), |
261 | 271 | } |
262 | 272 | ); |
|
0 commit comments