fix(orchestrator): release stranded effect claims
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
86ab0942d2
commit
d9d0d41d37
2 files changed
+176
-67
No files matched your search
@@ -8,6 +8,7 @@ import {
|
||||
RunId,
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
@@ -18,10 +19,11 @@ import * as Ref from "effect/Ref";
|
||||
import * as TestClock from "effect/testing/TestClock";
|
||||
|
||||
import { CheckpointRollbackServiceV2 } from "./CheckpointRollbackService.ts";
|
||||
import type { OrchestrationEffectV2 } from "./EffectOutbox.ts";
|
||||
import { EffectOutboxError, EffectOutboxV2, type OrchestrationEffectV2 } from "./EffectOutbox.ts";
|
||||
import {
|
||||
executorLayer,
|
||||
isNonRetryableProviderTurnControlFailure,
|
||||
layerWithOptions as effectWorkerLayerWithOptions,
|
||||
OrchestrationEffectExecutorV2,
|
||||
OrchestrationEffectWorkerError,
|
||||
OrchestrationEffectWorkerV2,
|
||||
@@ -176,6 +178,78 @@ it("does not retry pure interrupt races where the turn is already gone", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("requeues a claim when a pre-execution worker check fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const now = DateTime.formatIso(yield* DateTime.now);
|
||||
const effectId = "effect:worker-pre-execution-failure";
|
||||
const workerId = "worker-pre-execution-failure";
|
||||
const claimedEffect: OrchestrationEffectV2 = {
|
||||
id: effectId,
|
||||
commandId: CommandId.make("command:worker-pre-execution-failure"),
|
||||
threadId: ThreadId.make("thread:worker-pre-execution-failure"),
|
||||
request: { type: "terminal.cleanup" },
|
||||
status: "running",
|
||||
attemptCount: 1,
|
||||
availableAt: now,
|
||||
leaseOwner: workerId,
|
||||
leaseExpiresAt: now,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
completedAt: null,
|
||||
lastError: null,
|
||||
};
|
||||
const retries = yield* Ref.make<
|
||||
ReadonlyArray<{
|
||||
readonly effectId: string;
|
||||
readonly workerId: string;
|
||||
readonly error: string;
|
||||
readonly delayMs: number;
|
||||
}>
|
||||
>([]);
|
||||
const executionCount = yield* Ref.make(0);
|
||||
const outboxLayer = Layer.mock(EffectOutboxV2)({
|
||||
claimNext: () => Effect.succeed(Option.some(claimedEffect)),
|
||||
get: () =>
|
||||
Effect.fail(
|
||||
new EffectOutboxError({
|
||||
operation: "get",
|
||||
effectId,
|
||||
cause: "simulated cancellation-state read failure",
|
||||
}),
|
||||
),
|
||||
retry: (input) =>
|
||||
Ref.update(retries, (existing) => [...existing, input]).pipe(Effect.as(true)),
|
||||
});
|
||||
const executorLayer = Layer.succeed(
|
||||
OrchestrationEffectExecutorV2,
|
||||
OrchestrationEffectExecutorV2.of({
|
||||
execute: () => Ref.update(executionCount, (count) => count + 1),
|
||||
}),
|
||||
);
|
||||
const workerLayer = effectWorkerLayerWithOptions({ workerId }).pipe(
|
||||
Layer.provide(Layer.merge(outboxLayer, executorLayer)),
|
||||
);
|
||||
|
||||
const exit = yield* OrchestrationEffectWorkerV2.pipe(
|
||||
Effect.flatMap((worker) => worker.runOnce),
|
||||
Effect.provide(workerLayer),
|
||||
Effect.exit,
|
||||
);
|
||||
|
||||
assert.isTrue(Exit.isFailure(exit));
|
||||
if (Exit.isFailure(exit)) {
|
||||
assert.include(Cause.pretty(exit.cause), "simulated cancellation-state read failure");
|
||||
}
|
||||
assert.equal(yield* Ref.get(executionCount), 0);
|
||||
const retry = (yield* Ref.get(retries))[0];
|
||||
assert.isDefined(retry);
|
||||
assert.equal(retry.effectId, effectId);
|
||||
assert.equal(retry.workerId, workerId);
|
||||
assert.equal(retry.delayMs, 0);
|
||||
assert.include(retry.error, "simulated cancellation-state read failure");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("uses durable deadlines, notifications, and a slow liveness poll", () =>
|
||||
Effect.gen(function* () {
|
||||
const attempts = yield* Ref.make(0);
|
||||
|
||||
@@ -348,6 +348,39 @@ export const layerWithOptions = (
|
||||
}),
|
||||
),
|
||||
);
|
||||
const requeueAfterUnexpectedFailure = (
|
||||
effect: OrchestrationEffectV2,
|
||||
cause: Cause.Cause<unknown>,
|
||||
) =>
|
||||
Cause.hasInterruptsOnly(cause)
|
||||
? Effect.void
|
||||
: outbox
|
||||
.retry({
|
||||
effectId: effect.id,
|
||||
workerId,
|
||||
error: `Worker failed before settling the claimed effect: ${Cause.pretty(cause)}`,
|
||||
delayMs: 0,
|
||||
})
|
||||
.pipe(
|
||||
Effect.flatMap((requeued) =>
|
||||
requeued
|
||||
? Effect.logWarning("Requeued effect after unexpected worker failure", {
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
})
|
||||
: Effect.logWarning("Could not requeue effect after worker lost its lease", {
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
}),
|
||||
),
|
||||
Effect.catchCause((requeueCause) =>
|
||||
Effect.logError("Failed to requeue effect after unexpected worker failure", {
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
error: Cause.pretty(requeueCause),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const runOnce = Effect.gen(function* () {
|
||||
const claimExit = yield* Effect.exit(outbox.claimNext({ workerId, leaseDurationMs }));
|
||||
@@ -364,79 +397,81 @@ export const layerWithOptions = (
|
||||
return false;
|
||||
}
|
||||
const effect = claimed.value;
|
||||
const claimedAt = DateTime.toEpochMillis(yield* DateTime.now);
|
||||
const eligibleAt = Math.max(
|
||||
DateTime.toEpochMillis(DateTime.makeUnsafe(effect.createdAt)),
|
||||
DateTime.toEpochMillis(DateTime.makeUnsafe(effect.availableAt)),
|
||||
);
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(
|
||||
orchestrationEffectQueueWait,
|
||||
metricAttributes({ effect_type: effect.request.type }),
|
||||
),
|
||||
Duration.millis(Math.max(0, claimedAt - eligibleAt)),
|
||||
);
|
||||
// Cancellation can commit after the durable claim but before the
|
||||
// process-local Deferred is registered. Re-read the authoritative row
|
||||
// once before starting external work; later cancellations use the
|
||||
// Deferred raced below.
|
||||
if (yield* wasCancelled(effect.id)) {
|
||||
yield* outbox.clearCancellation(effect.id);
|
||||
return true;
|
||||
}
|
||||
const execution = executor.execute(effect).pipe(Effect.as("executed" as const));
|
||||
const cancellation = outbox
|
||||
.awaitCancellation(effect.id)
|
||||
.pipe(Effect.as("cancelled" as const));
|
||||
const exit = yield* Effect.exit(Effect.raceFirst(execution, cancellation)).pipe(
|
||||
Effect.ensuring(outbox.clearCancellation(effect.id)),
|
||||
);
|
||||
if (Exit.isSuccess(exit) && exit.value === "cancelled") {
|
||||
return true;
|
||||
}
|
||||
if (Exit.isSuccess(exit)) {
|
||||
const completed = yield* outbox.succeed({ effectId: effect.id, workerId });
|
||||
if (!completed) {
|
||||
return yield* Effect.gen(function* () {
|
||||
const claimedAt = DateTime.toEpochMillis(yield* DateTime.now);
|
||||
const eligibleAt = Math.max(
|
||||
DateTime.toEpochMillis(DateTime.makeUnsafe(effect.createdAt)),
|
||||
DateTime.toEpochMillis(DateTime.makeUnsafe(effect.availableAt)),
|
||||
);
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(
|
||||
orchestrationEffectQueueWait,
|
||||
metricAttributes({ effect_type: effect.request.type }),
|
||||
),
|
||||
Duration.millis(Math.max(0, claimedAt - eligibleAt)),
|
||||
);
|
||||
// Cancellation can commit after the durable claim but before the
|
||||
// process-local Deferred is registered. Re-read the authoritative row
|
||||
// once before starting external work; later cancellations use the
|
||||
// Deferred raced below.
|
||||
if (yield* wasCancelled(effect.id)) {
|
||||
yield* outbox.clearCancellation(effect.id);
|
||||
return true;
|
||||
}
|
||||
const execution = executor.execute(effect).pipe(Effect.as("executed" as const));
|
||||
const cancellation = outbox
|
||||
.awaitCancellation(effect.id)
|
||||
.pipe(Effect.as("cancelled" as const));
|
||||
const exit = yield* Effect.exit(Effect.raceFirst(execution, cancellation)).pipe(
|
||||
Effect.ensuring(outbox.clearCancellation(effect.id)),
|
||||
);
|
||||
if (Exit.isSuccess(exit) && exit.value === "cancelled") {
|
||||
return true;
|
||||
}
|
||||
if (Exit.isSuccess(exit)) {
|
||||
const completed = yield* outbox.succeed({ effectId: effect.id, workerId });
|
||||
if (!completed) {
|
||||
if (yield* wasCancelled(effect.id)) return true;
|
||||
return yield* new OrchestrationEffectWorkerError({
|
||||
operation: "complete",
|
||||
effectId: effect.id,
|
||||
cause: "The worker no longer owns the effect lease.",
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const error = Cause.pretty(exit.cause);
|
||||
const nonRetryable = isNonRetryableProviderTurnControlFailure(effect.request.type, error);
|
||||
yield* Effect.logWarning("Orchestration effect execution failed", {
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
attemptCount: effect.attemptCount,
|
||||
nonRetryable,
|
||||
error,
|
||||
});
|
||||
// Prefer succeed for terminal interrupt races so the outbox does not
|
||||
// keep a failed interrupt around; fail only when we must not retry.
|
||||
const updated = nonRetryable
|
||||
? yield* outbox.succeed({ effectId: effect.id, workerId })
|
||||
: effect.attemptCount >= maxAttempts
|
||||
? yield* outbox.fail({ effectId: effect.id, workerId, error })
|
||||
: yield* outbox.retry({
|
||||
effectId: effect.id,
|
||||
workerId,
|
||||
error,
|
||||
delayMs: Math.min(30_000, 100 * 2 ** Math.max(0, effect.attemptCount - 1)),
|
||||
});
|
||||
if (!updated) {
|
||||
if (yield* wasCancelled(effect.id)) return true;
|
||||
return yield* new OrchestrationEffectWorkerError({
|
||||
operation: "complete",
|
||||
operation: "reschedule",
|
||||
effectId: effect.id,
|
||||
cause: "The worker no longer owns the effect lease.",
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const error = Cause.pretty(exit.cause);
|
||||
const nonRetryable = isNonRetryableProviderTurnControlFailure(effect.request.type, error);
|
||||
yield* Effect.logWarning("Orchestration effect execution failed", {
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
attemptCount: effect.attemptCount,
|
||||
nonRetryable,
|
||||
error,
|
||||
});
|
||||
// Prefer succeed for terminal interrupt races so the outbox does not
|
||||
// keep a failed interrupt around; fail only when we must not retry.
|
||||
const updated = nonRetryable
|
||||
? yield* outbox.succeed({ effectId: effect.id, workerId })
|
||||
: effect.attemptCount >= maxAttempts
|
||||
? yield* outbox.fail({ effectId: effect.id, workerId, error })
|
||||
: yield* outbox.retry({
|
||||
effectId: effect.id,
|
||||
workerId,
|
||||
error,
|
||||
delayMs: Math.min(30_000, 100 * 2 ** Math.max(0, effect.attemptCount - 1)),
|
||||
});
|
||||
if (!updated) {
|
||||
if (yield* wasCancelled(effect.id)) return true;
|
||||
return yield* new OrchestrationEffectWorkerError({
|
||||
operation: "reschedule",
|
||||
effectId: effect.id,
|
||||
cause: "The worker no longer owns the effect lease.",
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}).pipe(Effect.onError((cause) => requeueAfterUnexpectedFailure(effect, cause)));
|
||||
}).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
isOrchestrationEffectWorkerError(cause)
|
||||
|
||||
Reference in new issue
Block a user