fix(orchestrator): avoid replaying settled effects
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
5e711d3b10
commit
ccb3e269ec
2 files changed
+199
-19
No files matched your search
@@ -250,6 +250,133 @@ it.effect("requeues a claim when a pre-execution worker check fails", () =>
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("terminalizes a process-bound claim when success settlement fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const now = DateTime.formatIso(yield* DateTime.now);
|
||||
const effectId = "effect:worker-process-bound-settlement-failure";
|
||||
const workerId = "worker-process-bound-settlement-failure";
|
||||
const claimedEffect: OrchestrationEffectV2 = {
|
||||
id: effectId,
|
||||
commandId: CommandId.make("command:worker-process-bound-settlement-failure"),
|
||||
threadId: ThreadId.make("thread:worker-process-bound-settlement-failure"),
|
||||
request: {
|
||||
type: "provider-turn.start",
|
||||
runId: RunId.make("run:worker-process-bound-settlement-failure"),
|
||||
},
|
||||
status: "running",
|
||||
attemptCount: 1,
|
||||
availableAt: now,
|
||||
leaseOwner: workerId,
|
||||
leaseExpiresAt: now,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
completedAt: null,
|
||||
lastError: null,
|
||||
};
|
||||
const retries = yield* Ref.make(0);
|
||||
const terminalErrors = yield* Ref.make<ReadonlyArray<string>>([]);
|
||||
const executionCount = yield* Ref.make(0);
|
||||
const outboxLayer = Layer.mock(EffectOutboxV2)({
|
||||
claimNext: () => Effect.succeed(Option.some(claimedEffect)),
|
||||
get: () => Effect.succeed(Option.some(claimedEffect)),
|
||||
awaitCancellation: () => Effect.never,
|
||||
clearCancellation: () => Effect.void,
|
||||
succeed: () =>
|
||||
Effect.fail(
|
||||
new EffectOutboxError({
|
||||
operation: "succeed",
|
||||
effectId,
|
||||
cause: "simulated success settlement failure",
|
||||
}),
|
||||
),
|
||||
retry: () => Ref.update(retries, (count) => count + 1).pipe(Effect.as(true)),
|
||||
fail: ({ error }) =>
|
||||
Ref.update(terminalErrors, (existing) => [...existing, error]).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));
|
||||
assert.equal(yield* Ref.get(executionCount), 1);
|
||||
assert.equal(yield* Ref.get(retries), 0);
|
||||
const terminalError = (yield* Ref.get(terminalErrors))[0];
|
||||
assert.isDefined(terminalError);
|
||||
assert.include(terminalError, "after execution started");
|
||||
assert.include(terminalError, "simulated success settlement failure");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("requeues a replay-safe claim when success settlement fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const now = DateTime.formatIso(yield* DateTime.now);
|
||||
const effectId = "effect:worker-replay-safe-settlement-failure";
|
||||
const workerId = "worker-replay-safe-settlement-failure";
|
||||
const claimedEffect: OrchestrationEffectV2 = {
|
||||
id: effectId,
|
||||
commandId: CommandId.make("command:worker-replay-safe-settlement-failure"),
|
||||
threadId: ThreadId.make("thread:worker-replay-safe-settlement-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(0);
|
||||
const terminalizations = yield* Ref.make(0);
|
||||
const outboxLayer = Layer.mock(EffectOutboxV2)({
|
||||
claimNext: () => Effect.succeed(Option.some(claimedEffect)),
|
||||
get: () => Effect.succeed(Option.some(claimedEffect)),
|
||||
awaitCancellation: () => Effect.never,
|
||||
clearCancellation: () => Effect.void,
|
||||
succeed: () =>
|
||||
Effect.fail(
|
||||
new EffectOutboxError({
|
||||
operation: "succeed",
|
||||
effectId,
|
||||
cause: "simulated replay-safe settlement failure",
|
||||
}),
|
||||
),
|
||||
retry: () => Ref.update(retries, (count) => count + 1).pipe(Effect.as(true)),
|
||||
fail: () => Ref.update(terminalizations, (count) => count + 1).pipe(Effect.as(true)),
|
||||
});
|
||||
const executorLayer = Layer.succeed(
|
||||
OrchestrationEffectExecutorV2,
|
||||
OrchestrationEffectExecutorV2.of({ execute: () => Effect.void }),
|
||||
);
|
||||
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));
|
||||
assert.equal(yield* Ref.get(retries), 1);
|
||||
assert.equal(yield* Ref.get(terminalizations), 0);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("uses durable deadlines, notifications, and a slow liveness poll", () =>
|
||||
Effect.gen(function* () {
|
||||
const attempts = yield* Ref.make(0);
|
||||
|
||||
@@ -17,7 +17,11 @@ import {
|
||||
} from "../observability/Metrics.ts";
|
||||
import { RunFinalizationService } from "./RunFinalizationService.ts";
|
||||
import { ResourceCleanupService } from "./ResourceCleanupService.ts";
|
||||
import { EffectOutboxV2, type OrchestrationEffectV2 } from "./EffectOutbox.ts";
|
||||
import {
|
||||
EffectOutboxV2,
|
||||
REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS,
|
||||
type OrchestrationEffectV2,
|
||||
} from "./EffectOutbox.ts";
|
||||
import { CheckpointRollbackServiceV2 } from "./CheckpointRollbackService.ts";
|
||||
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
|
||||
import { ProviderTurnControlServiceV2 } from "./ProviderTurnControlService.ts";
|
||||
@@ -348,10 +352,7 @@ export const layerWithOptions = (
|
||||
}),
|
||||
),
|
||||
);
|
||||
const requeueAfterUnexpectedFailure = (
|
||||
effect: OrchestrationEffectV2,
|
||||
cause: Cause.Cause<unknown>,
|
||||
) =>
|
||||
const requeueClaim = (effect: OrchestrationEffectV2, cause: Cause.Cause<unknown>) =>
|
||||
Cause.hasInterruptsOnly(cause)
|
||||
? Effect.void
|
||||
: outbox
|
||||
@@ -381,6 +382,51 @@ export const layerWithOptions = (
|
||||
}),
|
||||
),
|
||||
);
|
||||
const recoverClaimAfterExecutionStarted = (
|
||||
effect: OrchestrationEffectV2,
|
||||
cause: Cause.Cause<unknown>,
|
||||
) => {
|
||||
if (Cause.hasInterruptsOnly(cause)) return Effect.void;
|
||||
if (
|
||||
REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS.some(
|
||||
(effectType) => effectType === effect.request.type,
|
||||
)
|
||||
) {
|
||||
return requeueClaim(effect, cause);
|
||||
}
|
||||
return outbox
|
||||
.fail({
|
||||
effectId: effect.id,
|
||||
workerId,
|
||||
error: `Worker failed to settle a process-bound effect after execution started: ${Cause.pretty(cause)}`,
|
||||
})
|
||||
.pipe(
|
||||
Effect.flatMap((failed) =>
|
||||
failed
|
||||
? Effect.logError("Terminalized process-bound effect after settlement failure", {
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
})
|
||||
: Effect.logWarning(
|
||||
"Could not terminalize process-bound effect after worker lost its lease",
|
||||
{
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
},
|
||||
),
|
||||
),
|
||||
Effect.catchCause((failCause) =>
|
||||
Effect.logError(
|
||||
"Failed to terminalize process-bound effect after settlement failure",
|
||||
{
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
error: Cause.pretty(failCause),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const runOnce = Effect.gen(function* () {
|
||||
const claimExit = yield* Effect.exit(outbox.claimNext({ workerId, leaseDurationMs }));
|
||||
@@ -397,7 +443,7 @@ export const layerWithOptions = (
|
||||
return false;
|
||||
}
|
||||
const effect = claimed.value;
|
||||
return yield* Effect.gen(function* () {
|
||||
const cancelledBeforeExecution = yield* Effect.gen(function* () {
|
||||
const claimedAt = DateTime.toEpochMillis(yield* DateTime.now);
|
||||
const eligibleAt = Math.max(
|
||||
DateTime.toEpochMillis(DateTime.makeUnsafe(effect.createdAt)),
|
||||
@@ -418,17 +464,22 @@ export const layerWithOptions = (
|
||||
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)) {
|
||||
return false;
|
||||
}).pipe(Effect.onError((cause) => requeueClaim(effect, cause)));
|
||||
if (cancelledBeforeExecution) 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)) {
|
||||
return yield* Effect.gen(function* () {
|
||||
const completed = yield* outbox.succeed({ effectId: effect.id, workerId });
|
||||
if (!completed) {
|
||||
if (yield* wasCancelled(effect.id)) return true;
|
||||
@@ -439,8 +490,10 @@ export const layerWithOptions = (
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}).pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause)));
|
||||
}
|
||||
|
||||
return yield* Effect.gen(function* () {
|
||||
const error = Cause.pretty(exit.cause);
|
||||
const nonRetryable = isNonRetryableProviderTurnControlFailure(effect.request.type, error);
|
||||
yield* Effect.logWarning("Orchestration effect execution failed", {
|
||||
@@ -471,7 +524,7 @@ export const layerWithOptions = (
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}).pipe(Effect.onError((cause) => requeueAfterUnexpectedFailure(effect, cause)));
|
||||
}).pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause)));
|
||||
}).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
isOrchestrationEffectWorkerError(cause)
|
||||
|
||||
Reference in new issue
Block a user