Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.4k
perf(webapp): read per-run environment config from the replica at dequeue#4560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -689,10 +689,11 @@ export class EnvironmentVariablesRepository implements Repository { | ||
| async #getSecretEnvironmentVariables( | ||
| projectId: string, | ||
| environmentId: string, | ||
| parentEnvironmentId?: string | ||
| parentEnvironmentId?: string, | ||
| readFromReplica?: boolean | ||
| ): Promise<EnvironmentVariable[]> { | ||
| const secretStore = getSecretStore("DATABASE", { | ||
| prismaClient: this.prismaClient, | ||
| prismaClient: readFromReplica ? this.replicaClient : this.prismaClient, | ||
| }); | ||
| const parentSecrets = parentEnvironmentId | ||
| @@ -731,9 +732,15 @@ export class EnvironmentVariablesRepository implements Repository { | ||
| async getEnvironmentVariables( | ||
| projectId: string, | ||
| environmentId: string, | ||
| parentEnvironmentId?: string | ||
| parentEnvironmentId?: string, | ||
| options?: { readFromReplica?: boolean } | ||
| ): Promise<EnvironmentVariable[]> { | ||
| return this.#getSecretEnvironmentVariables(projectId, environmentId, parentEnvironmentId); | ||
| return this.#getSecretEnvironmentVariables( | ||
| projectId, | ||
| environmentId, | ||
| parentEnvironmentId, | ||
| options?.readFromReplica | ||
| ); | ||
| } | ||
| async delete(projectId: string, options: DeleteEnvironmentVariable): Promise<Result> { | ||
| @@ -947,7 +954,8 @@ export async function resolveVariablesForEnvironment( | ||
| let projectSecrets = await environmentVariablesRepository.getEnvironmentVariables( | ||
| runtimeEnvironment.projectId, | ||
| runtimeEnvironment.id, | ||
| parentEnvironment?.id | ||
| parentEnvironment?.id, | ||
| { readFromReplica: env.CONTROL_PLANE_DEQUEUE_READS_FROM_REPLICA === "1" } | ||
ericallam marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ); | ||
| projectSecrets = renameVariables(projectSecrets, { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -538,7 +538,10 @@ export class AuthenticatedWorkerInstance extends WithRunEngine { | ||
| const defaultMachinePreset = machinePresetFromName(defaultMachine); | ||
| const environment = await this._prisma.runtimeEnvironment.findFirst({ | ||
| const environmentReader = | ||
| env.CONTROL_PLANE_DEQUEUE_READS_FROM_REPLICA === "1" ? this._replica : this._prisma; | ||
| const environment = await environmentReader.runtimeEnvironment.findFirst({ | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| where: { | ||
| id: engineResult.execution.environment.id, | ||
| }, | ||
ericallam marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { setupAuthenticatedEnvironment } from "@internal/run-engine/tests"; | ||
| import { containerTest } from "@internal/testcontainers"; | ||
| import { describe, expect, vi } from "vitest"; | ||
| import type { PrismaClient, PrismaReplicaClient } from "~/db.server"; | ||
| import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server"; | ||
| type OpLog = Array<{ label: string; model?: string; operation: string }>; | ||
| function instrument<T>(client: T, label: string, log: OpLog): T { | ||
| return (client as any).$extends({ | ||
| name: `spy-${label}`, | ||
| query: { | ||
| $allOperations({ model, operation, args, query }: any) { | ||
| log.push({ label, model, operation }); | ||
| return query(args); | ||
| }, | ||
| }, | ||
| }) as T; | ||
| } | ||
| vi.setConfig({ testTimeout: 60_000 }); | ||
| describe("EnvironmentVariablesRepository control-plane read routing", () => { | ||
| containerTest( | ||
| "getEnvironmentVariables reads SecretStore from the replica only when readFromReplica is set, returning the same values", | ||
| async ({ prisma }) => { | ||
| const environment = await setupAuthenticatedEnvironment(prisma, "PRODUCTION"); | ||
| const projectId = environment.projectId; | ||
| const environmentId = environment.id; | ||
| await prisma.secretStore.create({ | ||
| data: { | ||
| key: `environmentvariable:${projectId}:${environmentId}:MY_VAR`, | ||
| value: { secret: "hello-from-db" }, | ||
| }, | ||
| }); | ||
| const log: OpLog = []; | ||
| const writer = instrument(prisma, "writer", log) as unknown as PrismaClient; | ||
| const replica = instrument(prisma, "replica", log) as unknown as PrismaReplicaClient; | ||
| const repository = new EnvironmentVariablesRepository(writer, replica); | ||
| log.length = 0; | ||
| const viaReplica = await repository.getEnvironmentVariables( | ||
| projectId, | ||
| environmentId, | ||
| undefined, | ||
| { | ||
| readFromReplica: true, | ||
| } | ||
| ); | ||
| expect(viaReplica).toContainEqual({ key: "MY_VAR", value: "hello-from-db" }); | ||
| const replicaSecretOps = log.filter((l) => l.model === "SecretStore"); | ||
| expect(replicaSecretOps.length).toBeGreaterThan(0); | ||
| expect(replicaSecretOps.every((l) => l.label === "replica")).toBe(true); | ||
| expect(log.some((l) => l.model === "SecretStore" && l.label === "writer")).toBe(false); | ||
| log.length = 0; | ||
| const viaWriter = await repository.getEnvironmentVariables(projectId, environmentId); | ||
| expect(viaWriter).toContainEqual({ key: "MY_VAR", value: "hello-from-db" }); | ||
| const writerSecretOps = log.filter((l) => l.model === "SecretStore"); | ||
| expect(writerSecretOps.length).toBeGreaterThan(0); | ||
| expect(writerSecretOps.every((l) => l.label === "writer")).toBe(true); | ||
| } | ||
| ); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.