-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
perf(webapp,run-store): point-lookup batch idempotency keys #4255
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: fix | ||
| --- | ||
|
|
||
| Speed up idempotency checks on `batchTrigger` calls that use idempotency keys. Large batches against a task with a big run history no longer degrade to multi-second lookups. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
internal-packages/run-store/src/PostgresRunStore.findRunsByIdempotencyKeys.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { postgresTest } from "@internal/testcontainers"; | ||
| import type { PrismaClient } from "@trigger.dev/database"; | ||
| import { describe, expect } from "vitest"; | ||
| import { PostgresRunStore } from "./PostgresRunStore.js"; | ||
|
|
||
| async function seedEnvironment(prisma: PrismaClient) { | ||
| const organization = await prisma.organization.create({ | ||
| data: { title: "Test Organization", slug: "test-organization" }, | ||
| }); | ||
| const project = await prisma.project.create({ | ||
| data: { | ||
| name: "Test Project", | ||
| slug: "test-project", | ||
| externalRef: "proj_1234", | ||
| organizationId: organization.id, | ||
| }, | ||
| }); | ||
| const environment = await prisma.runtimeEnvironment.create({ | ||
| data: { | ||
| type: "DEVELOPMENT", | ||
| slug: "dev", | ||
| projectId: project.id, | ||
| organizationId: organization.id, | ||
| apiKey: "tr_dev_apikey", | ||
| pkApiKey: "pk_dev_apikey", | ||
| shortcode: "short_code", | ||
| }, | ||
| }); | ||
| return { organization, project, environment }; | ||
| } | ||
|
|
||
| async function createRun( | ||
| prisma: PrismaClient, | ||
| params: { | ||
| runtimeEnvironmentId: string; | ||
| projectId: string; | ||
| friendlyId: string; | ||
| taskIdentifier: string; | ||
| idempotencyKey: string; | ||
| idempotencyKeyExpiresAt?: Date; | ||
| } | ||
| ) { | ||
| await prisma.taskRun.create({ | ||
| data: { | ||
| friendlyId: params.friendlyId, | ||
| taskIdentifier: params.taskIdentifier, | ||
| idempotencyKey: params.idempotencyKey, | ||
| idempotencyKeyExpiresAt: params.idempotencyKeyExpiresAt ?? null, | ||
| payload: "{}", | ||
| payloadType: "application/json", | ||
| runtimeEnvironmentId: params.runtimeEnvironmentId, | ||
| projectId: params.projectId, | ||
| queue: `task/${params.taskIdentifier}`, | ||
| traceId: `trace_${params.friendlyId}`, | ||
| spanId: `span_${params.friendlyId}`, | ||
| engine: "V2", | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| describe("PostgresRunStore.findRunsByIdempotencyKeys", () => { | ||
| postgresTest("resolves multiple keys, scoped to (env, task)", async ({ prisma }) => { | ||
| const { project, environment } = await seedEnvironment(prisma); | ||
| const store = new PostgresRunStore({ prisma, readOnlyPrisma: prisma }); | ||
|
|
||
| const expiresAt = new Date("2999-01-01T00:00:00.000Z"); | ||
| await createRun(prisma, { | ||
| runtimeEnvironmentId: environment.id, | ||
| projectId: project.id, | ||
| friendlyId: "run_a1", | ||
| taskIdentifier: "task-a", | ||
| idempotencyKey: "idem-1", | ||
| idempotencyKeyExpiresAt: expiresAt, | ||
| }); | ||
| await createRun(prisma, { | ||
| runtimeEnvironmentId: environment.id, | ||
| projectId: project.id, | ||
| friendlyId: "run_a2", | ||
| taskIdentifier: "task-a", | ||
| idempotencyKey: "idem-2", | ||
| }); | ||
| await createRun(prisma, { | ||
| runtimeEnvironmentId: environment.id, | ||
| projectId: project.id, | ||
| friendlyId: "run_b1", | ||
| taskIdentifier: "task-b", | ||
| idempotencyKey: "idem-1", | ||
| }); | ||
|
|
||
| const rows = await store.findRunsByIdempotencyKeys({ | ||
| runtimeEnvironmentId: environment.id, | ||
| taskIdentifier: "task-a", | ||
| idempotencyKeys: ["idem-1", "idem-2", "does-not-exist"], | ||
| }); | ||
|
|
||
| const byKey = new Map(rows.map((r) => [r.idempotencyKey, r])); | ||
| expect(rows).toHaveLength(2); | ||
| expect(byKey.get("idem-1")?.friendlyId).toBe("run_a1"); | ||
| expect(byKey.get("idem-2")?.friendlyId).toBe("run_a2"); | ||
| expect(rows.map((r) => r.friendlyId)).not.toContain("run_b1"); | ||
| expect(byKey.get("idem-1")?.idempotencyKeyExpiresAt).toBeInstanceOf(Date); | ||
| expect(byKey.get("idem-1")?.idempotencyKeyExpiresAt?.toISOString()).toBe( | ||
| expiresAt.toISOString() | ||
| ); | ||
| expect(byKey.get("idem-2")?.idempotencyKeyExpiresAt).toBeNull(); | ||
| }); | ||
|
|
||
| postgresTest("short-circuits on an empty key list without querying", async ({ prisma }) => { | ||
| const { environment } = await seedEnvironment(prisma); | ||
| const store = new PostgresRunStore({ prisma, readOnlyPrisma: prisma }); | ||
|
|
||
| const rows = await store.findRunsByIdempotencyKeys({ | ||
| runtimeEnvironmentId: environment.id, | ||
| taskIdentifier: "task-a", | ||
| idempotencyKeys: [], | ||
| }); | ||
|
|
||
| expect(rows).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.