Skip to content

v4: Improved run locking #2173

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ const EnvironmentSchema = z.object({
RUN_ENGINE_DEQUEUE_BLOCKING_TIMEOUT_SECONDS: z.coerce.number().int().default(10),
RUN_ENGINE_MASTER_QUEUE_CONSUMERS_INTERVAL_MS: z.coerce.number().int().default(500),

RUN_ENGINE_RUN_LOCK_DURATION: z.coerce.number().int().default(5000),
RUN_ENGINE_RUN_LOCK_AUTOMATIC_EXTENSION_THRESHOLD: z.coerce.number().int().default(1000),
RUN_ENGINE_RUN_LOCK_MAX_RETRIES: z.coerce.number().int().default(10),
RUN_ENGINE_RUN_LOCK_BASE_DELAY: z.coerce.number().int().default(100),
RUN_ENGINE_RUN_LOCK_MAX_DELAY: z.coerce.number().int().default(3000),
RUN_ENGINE_RUN_LOCK_BACKOFF_MULTIPLIER: z.coerce.number().default(1.8),
RUN_ENGINE_RUN_LOCK_JITTER_FACTOR: z.coerce.number().default(0.15),
RUN_ENGINE_RUN_LOCK_MAX_TOTAL_WAIT_TIME: z.coerce.number().int().default(15000),

RUN_ENGINE_WORKER_REDIS_HOST: z
.string()
.optional()
Expand Down
10 changes: 10 additions & 0 deletions apps/webapp/app/v3/runEngine.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ function createRunEngine() {
enableAutoPipelining: true,
...(env.RUN_ENGINE_RUN_LOCK_REDIS_TLS_DISABLED === "true" ? {} : { tls: {} }),
},
duration: env.RUN_ENGINE_RUN_LOCK_DURATION,
automaticExtensionThreshold: env.RUN_ENGINE_RUN_LOCK_AUTOMATIC_EXTENSION_THRESHOLD,
retryConfig: {
maxAttempts: env.RUN_ENGINE_RUN_LOCK_MAX_RETRIES,
baseDelay: env.RUN_ENGINE_RUN_LOCK_BASE_DELAY,
maxDelay: env.RUN_ENGINE_RUN_LOCK_MAX_DELAY,
backoffMultiplier: env.RUN_ENGINE_RUN_LOCK_BACKOFF_MULTIPLIER,
jitterFactor: env.RUN_ENGINE_RUN_LOCK_JITTER_FACTOR,
maxTotalWaitTime: env.RUN_ENGINE_RUN_LOCK_MAX_TOTAL_WAIT_TIME,
},
},
tracer,
meter,
Expand Down
101 changes: 54 additions & 47 deletions internal-packages/run-engine/src/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ export class RunEngine {
logger: this.logger,
tracer: trace.getTracer("RunLocker"),
meter: options.meter,
duration: options.runLock.duration ?? 5000,
automaticExtensionThreshold: options.runLock.automaticExtensionThreshold ?? 1000,
retryConfig: {
maxAttempts: 10,
baseDelay: 100,
maxDelay: 3000,
backoffMultiplier: 1.8,
jitterFactor: 0.15,
maxTotalWaitTime: 15000,
...options.runLock.retryConfig,
},
});

const keys = new RunQueueFullKeyProducer();
Expand Down Expand Up @@ -486,56 +497,52 @@ export class RunEngine {

span.setAttribute("runId", taskRun.id);

await this.runLock.lock("trigger", [taskRun.id], 5000, async (signal) => {
//create associated waitpoint (this completes when the run completes)
const associatedWaitpoint = await this.waitpointSystem.createRunAssociatedWaitpoint(
prisma,
{
projectId: environment.project.id,
environmentId: environment.id,
completedByTaskRunId: taskRun.id,
}
);

//triggerAndWait or batchTriggerAndWait
if (resumeParentOnCompletion && parentTaskRunId) {
//this will block the parent run from continuing until this waitpoint is completed (and removed)
await this.waitpointSystem.blockRunWithWaitpoint({
runId: parentTaskRunId,
waitpoints: associatedWaitpoint.id,
projectId: associatedWaitpoint.projectId,
organizationId: environment.organization.id,
batch,
workerId,
runnerId,
tx: prisma,
releaseConcurrency,
});
//create associated waitpoint (this completes when the run completes)
const associatedWaitpoint = await this.waitpointSystem.createRunAssociatedWaitpoint(
prisma,
{
projectId: environment.project.id,
environmentId: environment.id,
completedByTaskRunId: taskRun.id,
}
);

//Make sure lock extension succeeded
signal.throwIfAborted();

if (taskRun.delayUntil) {
// Schedule the run to be enqueued at the delayUntil time
await this.delayedRunSystem.scheduleDelayedRunEnqueuing({
runId: taskRun.id,
delayUntil: taskRun.delayUntil,
});
} else {
await this.enqueueSystem.enqueueRun({
run: taskRun,
env: environment,
workerId,
runnerId,
tx: prisma,
});
//triggerAndWait or batchTriggerAndWait
if (resumeParentOnCompletion && parentTaskRunId) {
//this will block the parent run from continuing until this waitpoint is completed (and removed)
await this.waitpointSystem.blockRunWithWaitpoint({
runId: parentTaskRunId,
waitpoints: associatedWaitpoint.id,
projectId: associatedWaitpoint.projectId,
organizationId: environment.organization.id,
batch,
workerId,
runnerId,
tx: prisma,
releaseConcurrency,
});
}

if (taskRun.ttl) {
await this.ttlSystem.scheduleExpireRun({ runId: taskRun.id, ttl: taskRun.ttl });
}
if (taskRun.delayUntil) {
// Schedule the run to be enqueued at the delayUntil time
await this.delayedRunSystem.scheduleDelayedRunEnqueuing({
runId: taskRun.id,
delayUntil: taskRun.delayUntil,
});
} else {
if (taskRun.ttl) {
await this.ttlSystem.scheduleExpireRun({ runId: taskRun.id, ttl: taskRun.ttl });
}
});

await this.enqueueSystem.enqueueRun({
run: taskRun,
env: environment,
workerId,
runnerId,
tx: prisma,
skipRunLock: true,
});
}

this.eventBus.emit("runCreated", {
time: new Date(),
Expand Down Expand Up @@ -1155,7 +1162,7 @@ export class RunEngine {
tx?: PrismaClientOrTransaction;
}) {
const prisma = tx ?? this.prisma;
return await this.runLock.lock("handleStalledSnapshot", [runId], 5_000, async () => {
return await this.runLock.lock("handleStalledSnapshot", [runId], async () => {
const latestSnapshot = await getLatestExecutionSnapshot(prisma, runId);
if (latestSnapshot.id !== snapshotId) {
this.logger.log(
Expand Down
Loading