-
-
Notifications
You must be signed in to change notification settings - Fork 729
feat: v4 deadlock detection #1970
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
15 commits
Select commit
Hold shift + click to select a range
6864f5d
Locked task runs will now require queues and tasks to be in the locke…
ericallam 7837402
Client errors caught in a run function now will skip retrying
ericallam 78d431a
Extracted out the trigger queues logic
ericallam b35a26a
extract validation, idempotency keys, payloads to concerns
ericallam 2b1d9d9
Extracted out a bunch of more stuff and getting trigger tests to work
ericallam 0dd60ea
Add queue and locked version tests
ericallam 1144d7e
Deadlock detection WIP
ericallam 531698c
more deadlock detection
ericallam 4927901
Only detect deadlocks when the parent run is waiting on the child run
ericallam 81a6540
Improve the error experience around deadlocks
ericallam a072884
A couple tweaks to make CodeRabbit happy and fixing the tests in CI
ericallam abe2b54
Fixed failing test
ericallam 2a2825f
Changeset
ericallam 4312701
wip
ericallam 21d9eee
Make sure to scope queries to the runtime env
ericallam 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,5 @@ | ||
--- | ||
"trigger.dev": patch | ||
--- | ||
|
||
TriggerApiError 4xx errors will no longer cause tasks to be retried |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class EngineServiceValidationError extends Error { | ||
constructor(message: string, public status?: number) { | ||
super(message); | ||
this.name = "EngineServiceValidationError"; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
apps/webapp/app/runEngine/concerns/idempotencyKeys.server.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,96 @@ | ||
import { RunId } from "@trigger.dev/core/v3/isomorphic"; | ||
import type { PrismaClientOrTransaction, TaskRun } from "@trigger.dev/database"; | ||
import { logger } from "~/services/logger.server"; | ||
import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server"; | ||
import type { RunEngine } from "~/v3/runEngine.server"; | ||
import type { TraceEventConcern, TriggerTaskRequest } from "../types"; | ||
|
||
export type IdempotencyKeyConcernResult = | ||
| { isCached: true; run: TaskRun } | ||
| { isCached: false; idempotencyKey?: string; idempotencyKeyExpiresAt?: Date }; | ||
|
||
export class IdempotencyKeyConcern { | ||
constructor( | ||
private readonly prisma: PrismaClientOrTransaction, | ||
private readonly engine: RunEngine, | ||
private readonly traceEventConcern: TraceEventConcern | ||
) {} | ||
|
||
async handleTriggerRequest(request: TriggerTaskRequest): Promise<IdempotencyKeyConcernResult> { | ||
const idempotencyKey = request.options?.idempotencyKey ?? request.body.options?.idempotencyKey; | ||
const idempotencyKeyExpiresAt = | ||
request.options?.idempotencyKeyExpiresAt ?? | ||
resolveIdempotencyKeyTTL(request.body.options?.idempotencyKeyTTL) ?? | ||
new Date(Date.now() + 24 * 60 * 60 * 1000 * 30); // 30 days | ||
|
||
if (!idempotencyKey) { | ||
return { isCached: false, idempotencyKey, idempotencyKeyExpiresAt }; | ||
} | ||
|
||
const existingRun = idempotencyKey | ||
? await this.prisma.taskRun.findFirst({ | ||
where: { | ||
runtimeEnvironmentId: request.environment.id, | ||
idempotencyKey, | ||
taskIdentifier: request.taskId, | ||
}, | ||
include: { | ||
associatedWaitpoint: true, | ||
}, | ||
}) | ||
: undefined; | ||
|
||
if (existingRun) { | ||
if (existingRun.idempotencyKeyExpiresAt && existingRun.idempotencyKeyExpiresAt < new Date()) { | ||
logger.debug("[TriggerTaskService][call] Idempotency key has expired", { | ||
idempotencyKey: request.options?.idempotencyKey, | ||
run: existingRun, | ||
}); | ||
|
||
// Update the existing run to remove the idempotency key | ||
await this.prisma.taskRun.updateMany({ | ||
where: { id: existingRun.id, idempotencyKey }, | ||
data: { idempotencyKey: null, idempotencyKeyExpiresAt: null }, | ||
}); | ||
} else { | ||
const associatedWaitpoint = existingRun.associatedWaitpoint; | ||
const parentRunId = request.body.options?.parentRunId; | ||
const resumeParentOnCompletion = request.body.options?.resumeParentOnCompletion; | ||
//We're using `andWait` so we need to block the parent run with a waitpoint | ||
if (associatedWaitpoint && resumeParentOnCompletion && parentRunId) { | ||
await this.traceEventConcern.traceIdempotentRun( | ||
request, | ||
{ | ||
existingRun, | ||
idempotencyKey, | ||
incomplete: associatedWaitpoint.status === "PENDING", | ||
isError: associatedWaitpoint.outputIsError, | ||
}, | ||
async (event) => { | ||
//block run with waitpoint | ||
await this.engine.blockRunWithWaitpoint({ | ||
runId: RunId.fromFriendlyId(parentRunId), | ||
waitpoints: associatedWaitpoint.id, | ||
spanIdToComplete: event.spanId, | ||
batch: request.options?.batchId | ||
? { | ||
id: request.options.batchId, | ||
index: request.options.batchIndex ?? 0, | ||
} | ||
: undefined, | ||
projectId: request.environment.projectId, | ||
organizationId: request.environment.organizationId, | ||
tx: this.prisma, | ||
releaseConcurrency: request.body.options?.releaseConcurrency, | ||
}); | ||
} | ||
); | ||
} | ||
|
||
return { isCached: true, run: existingRun }; | ||
} | ||
} | ||
|
||
return { isCached: false, idempotencyKey, idempotencyKeyExpiresAt }; | ||
} | ||
} |
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,63 @@ | ||
import { IOPacket, packetRequiresOffloading, tryCatch } from "@trigger.dev/core/v3"; | ||
import { PayloadProcessor, TriggerTaskRequest } from "../types"; | ||
import { env } from "~/env.server"; | ||
import { startActiveSpan } from "~/v3/tracer.server"; | ||
import { uploadPacketToObjectStore } from "~/v3/r2.server"; | ||
import { EngineServiceValidationError } from "./errors"; | ||
|
||
export class DefaultPayloadProcessor implements PayloadProcessor { | ||
async process(request: TriggerTaskRequest): Promise<IOPacket> { | ||
return await startActiveSpan("handlePayloadPacket()", async (span) => { | ||
const payload = request.body.payload; | ||
const payloadType = request.body.options?.payloadType ?? "application/json"; | ||
|
||
const packet = this.#createPayloadPacket(payload, payloadType); | ||
|
||
if (!packet.data) { | ||
return packet; | ||
} | ||
|
||
const { needsOffloading, size } = packetRequiresOffloading( | ||
packet, | ||
env.TASK_PAYLOAD_OFFLOAD_THRESHOLD | ||
); | ||
|
||
span.setAttribute("needsOffloading", needsOffloading); | ||
span.setAttribute("size", size); | ||
|
||
if (!needsOffloading) { | ||
return packet; | ||
} | ||
|
||
const filename = `${request.friendlyId}/payload.json`; | ||
|
||
const [uploadError] = await tryCatch( | ||
uploadPacketToObjectStore(filename, packet.data, packet.dataType, request.environment) | ||
); | ||
|
||
if (uploadError) { | ||
throw new EngineServiceValidationError( | ||
"Failed to upload large payload to object store", | ||
500 | ||
); // This is retryable | ||
} | ||
ericallam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
data: filename, | ||
dataType: "application/store", | ||
}; | ||
}); | ||
} | ||
|
||
#createPayloadPacket(payload: any, payloadType: string): IOPacket { | ||
if (payloadType === "application/json") { | ||
return { data: JSON.stringify(payload), dataType: "application/json" }; | ||
} | ||
|
||
if (typeof payload === "string") { | ||
return { data: payload, dataType: payloadType }; | ||
} | ||
|
||
return { dataType: payloadType }; | ||
} | ||
} |
Oops, something went wrong.
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.