-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(run-engine): stop a '*' concurrency key stranding its whole base queue #4628
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
Draft
1stvamp
wants to merge
1
commit into
main
Choose a base branch
from
fix/ck-wildcard-master-queue-strand
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−20
Draft
Changes from all commits
Commits
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 | ||
| --- | ||
|
|
||
| Using `*` as a concurrency key no longer stops a queue from being processed. Triggering a single run with that key could leave the whole queue stalled, including runs using other concurrency keys on it, until something else was triggered on the same queue. |
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
191 changes: 191 additions & 0 deletions
191
internal-packages/run-engine/src/run-queue/tests/ckWildcardKey.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,191 @@ | ||
| import { redisTest } from "@internal/testcontainers"; | ||
| import { trace } from "@internal/tracing"; | ||
| import { Logger } from "@trigger.dev/core/logger"; | ||
| import { Decimal } from "@trigger.dev/database"; | ||
| import { describe } from "node:test"; | ||
| import { FairQueueSelectionStrategy } from "../fairQueueSelectionStrategy.js"; | ||
| import { RunQueue } from "../index.js"; | ||
| import { RunQueueFullKeyProducer } from "../keyProducer.js"; | ||
| import type { InputPayload } from "../types.js"; | ||
|
|
||
| const testOptions = { | ||
| name: "rq", | ||
| tracer: trace.getTracer("rq"), | ||
| workers: 1, | ||
| defaultEnvConcurrency: 25, | ||
| logger: new Logger("RunQueue", "warn"), | ||
| retryOptions: { | ||
| maxAttempts: 5, | ||
| factor: 1.1, | ||
| minTimeoutInMs: 100, | ||
| maxTimeoutInMs: 1_000, | ||
| randomize: true, | ||
| }, | ||
| keys: new RunQueueFullKeyProducer(), | ||
| }; | ||
|
|
||
| const authenticatedEnvDev = { | ||
| id: "e1234", | ||
| type: "DEVELOPMENT" as const, | ||
| maximumConcurrencyLimit: 10, | ||
| concurrencyLimitBurstFactor: new Decimal(2.0), | ||
| project: { id: "p1234" }, | ||
| organization: { id: "o1234" }, | ||
| }; | ||
|
|
||
| function createQueue(redisContainer: any) { | ||
| return new RunQueue({ | ||
| ...testOptions, | ||
| masterQueueConsumersDisabled: true, | ||
| workerOptions: { disabled: true }, | ||
| queueSelectionStrategy: new FairQueueSelectionStrategy({ | ||
| redis: { | ||
| keyPrefix: "runqueue:test:", | ||
| host: redisContainer.getHost(), | ||
| port: redisContainer.getPort(), | ||
| }, | ||
| keys: testOptions.keys, | ||
| }), | ||
| redis: { | ||
| keyPrefix: "runqueue:test:", | ||
| host: redisContainer.getHost(), | ||
| port: redisContainer.getPort(), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function makeMessage(overrides: Partial<InputPayload> = {}): InputPayload { | ||
| return { | ||
| runId: "r1", | ||
| taskIdentifier: "task/my-task", | ||
| orgId: "o1234", | ||
| projectId: "p1234", | ||
| environmentId: "e1234", | ||
| environmentType: "DEVELOPMENT", | ||
| queue: "task/my-task", | ||
| timestamp: Date.now(), | ||
| attempt: 0, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| const QUEUE = "task/my-task"; | ||
|
|
||
| vi.setConfig({ testTimeout: 60_000 }); | ||
|
|
||
| // A concurrency key is an unrestricted client string, so `*` is reachable from the public | ||
| // API, and `queueKey` renders it as `...:queue:<q>:ck:*`, which is byte-identical to the | ||
| // wildcard member the CK scripts keep in the master queue. Each of those scripts rebalances | ||
| // the master queue with that wildcard member and then removes the "old-format" entry for the | ||
| // variant it just touched. When the variant IS the wildcard, the second call undid the | ||
| // first, taking the whole base queue's master-queue entry with it: nothing pointed at the | ||
| // queue any more, so every concurrency key on it stopped being dequeued, silently, until | ||
| // some later write happened to re-add the member. | ||
| describe("concurrency key of '*'", () => { | ||
| redisTest("enqueueing it leaves the base queue reachable", async ({ redisContainer }) => { | ||
| const queue = createQueue(redisContainer); | ||
| try { | ||
| const t0 = Date.now() - 100_000; | ||
| const shard = testOptions.keys.masterQueueShardForEnvironment(authenticatedEnvDev.id, 2); | ||
| const masterQueueKey = testOptions.keys.masterQueueKeyForShard(shard); | ||
|
|
||
| // An ordinary key with real queued work: the bystander that used to be taken down. | ||
| await queue.enqueueMessage({ | ||
| env: authenticatedEnvDev, | ||
| message: makeMessage({ runId: "r-victim", concurrencyKey: "user-1", timestamp: t0 }), | ||
| workerQueue: authenticatedEnvDev.id, | ||
| skipDequeueProcessing: true, | ||
| }); | ||
| expect(await queue.redis.zcard(masterQueueKey)).toBe(1); | ||
|
|
||
| await queue.enqueueMessage({ | ||
| env: authenticatedEnvDev, | ||
| message: makeMessage({ runId: "r-star", concurrencyKey: "*", timestamp: t0 + 1 }), | ||
| workerQueue: authenticatedEnvDev.id, | ||
| skipDequeueProcessing: true, | ||
| }); | ||
|
|
||
| // The master queue still points at this base queue. | ||
| expect(await queue.redis.zcard(masterQueueKey)).toBe(1); | ||
|
|
||
| // Both variants are registered, and both runs come back out. | ||
| const ckIndexKey = testOptions.keys.ckIndexKeyFromQueue( | ||
| testOptions.keys.queueKey(authenticatedEnvDev, QUEUE, "user-1") | ||
| ); | ||
| expect((await queue.redis.zrange(ckIndexKey, 0, -1)).length).toBe(2); | ||
|
|
||
| const served = await queue.testDequeueFromMasterQueue(shard, authenticatedEnvDev.id, 10); | ||
| expect(served.map((m) => m.messageId).sort()).toEqual(["r-star", "r-victim"]); | ||
| } finally { | ||
| await queue.quit(); | ||
| } | ||
| }); | ||
|
|
||
| redisTest("acking it leaves the base queue reachable", async ({ redisContainer }) => { | ||
| const queue = createQueue(redisContainer); | ||
| try { | ||
| const t0 = Date.now() - 100_000; | ||
| const shard = testOptions.keys.masterQueueShardForEnvironment(authenticatedEnvDev.id, 2); | ||
| const masterQueueKey = testOptions.keys.masterQueueKeyForShard(shard); | ||
|
|
||
| await queue.enqueueMessage({ | ||
| env: authenticatedEnvDev, | ||
| message: makeMessage({ runId: "r-star", concurrencyKey: "*", timestamp: t0 }), | ||
| workerQueue: authenticatedEnvDev.id, | ||
| skipDequeueProcessing: true, | ||
| }); | ||
| await queue.enqueueMessage({ | ||
| env: authenticatedEnvDev, | ||
| message: makeMessage({ runId: "r-victim", concurrencyKey: "user-1", timestamp: t0 + 1 }), | ||
| workerQueue: authenticatedEnvDev.id, | ||
| skipDequeueProcessing: true, | ||
| }); | ||
|
|
||
| // Ack the '*' run while the other key still has work queued: the ack script runs the | ||
| // same rebalance-then-cleanup pair as the enqueue one. | ||
| await queue.acknowledgeMessage(authenticatedEnvDev.organization.id, "r-star", { | ||
| skipDequeueProcessing: true, | ||
| }); | ||
|
|
||
| expect(await queue.redis.zcard(masterQueueKey)).toBe(1); | ||
|
|
||
| const served = await queue.testDequeueFromMasterQueue(shard, authenticatedEnvDev.id, 10); | ||
| expect(served.map((m) => m.messageId)).toEqual(["r-victim"]); | ||
| } finally { | ||
| await queue.quit(); | ||
| } | ||
| }); | ||
|
|
||
| redisTest("nacking it leaves the base queue reachable", async ({ redisContainer }) => { | ||
| const queue = createQueue(redisContainer); | ||
| try { | ||
| const t0 = Date.now() - 100_000; | ||
| const shard = testOptions.keys.masterQueueShardForEnvironment(authenticatedEnvDev.id, 2); | ||
| const masterQueueKey = testOptions.keys.masterQueueKeyForShard(shard); | ||
|
|
||
| await queue.enqueueMessage({ | ||
| env: authenticatedEnvDev, | ||
| message: makeMessage({ runId: "r-star", concurrencyKey: "*", timestamp: t0 }), | ||
| workerQueue: authenticatedEnvDev.id, | ||
| skipDequeueProcessing: true, | ||
| }); | ||
|
|
||
| const [dequeued] = await queue.testDequeueFromMasterQueue(shard, authenticatedEnvDev.id, 1); | ||
| expect(dequeued?.messageId).toBe("r-star"); | ||
|
|
||
| await queue.nackMessage({ | ||
| orgId: authenticatedEnvDev.organization.id, | ||
| messageId: "r-star", | ||
| retryAt: Date.now() - 1, | ||
| skipDequeueProcessing: true, | ||
| }); | ||
|
|
||
| expect(await queue.redis.zcard(masterQueueKey)).toBe(1); | ||
|
|
||
| const served = await queue.testDequeueFromMasterQueue(shard, authenticatedEnvDev.id, 1); | ||
| expect(served.map((m) => m.messageId)).toEqual(["r-star"]); | ||
| } finally { | ||
| await queue.quit(); | ||
| } | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: triggerdotdev/trigger.dev
Length of output: 10921
🏁 Script executed:
Repository: triggerdotdev/trigger.dev
Length of output: 11898
Import
describefrom Vitest.This file runs under Vitest. Importing
describefromnode:testmixes test frameworks.Proposed fix
📝 Committable suggestion
Source: Coding guidelines