GeezerVison is showing github.com — fix(server): a locked database no longer strands a claimed effect and… · pingdotgg/t3code@60511c1
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/EffectWorker.test.ts +69Lines changed: 69 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ import * as Exit from "effect/Exit"; | 21 | 21 | import * as Layer from "effect/Layer"; | 22 | 22 | import * as Option from "effect/Option"; | 23 | 23 | import * as Ref from "effect/Ref"; | | 24 | +import * as SqlError from "effect/sql/SqlError"; | 24 | 25 |
| 25 | 26 | import * as CheckpointRollbackService from "./CheckpointRollbackService.ts"; | 26 | 27 | import * as CheckpointService from "./CheckpointService.ts"; | @@ -379,6 +380,74 @@ it.effect("requeues a claim when a pre-execution worker check fails", () => | 379 | 380 | }), | 380 | 381 | ); | 381 | 382 |
| | 383 | +it.effect("keeps requeueing a failed claim while the database is locked", () => | | 384 | + Effect.gen(function* () { | | 385 | + const now = DateTime.formatIso(yield* DateTime.now); | | 386 | + const effectId = "effect:worker-requeue-lock-timeout"; | | 387 | + const workerId = "worker-requeue-lock-timeout"; | | 388 | + const claimedEffect: EffectOutbox.OrchestrationEffectV2 = { | | 389 | + id: effectId, | | 390 | + commandId: CommandId.make("command:worker-requeue-lock-timeout"), | | 391 | + threadId, | | 392 | + request: { type: "provider-runtime.continue", sourceRunId: runId }, | | 393 | + status: "running", | | 394 | + attemptCount: 1, | | 395 | + availableAt: now, | | 396 | + leaseOwner: workerId, | | 397 | + createdAt: now, | | 398 | + updatedAt: now, | | 399 | + completedAt: null, | | 400 | + lastError: null, | | 401 | + }; | | 402 | + const lockedWrites = yield* Ref.make(3); | | 403 | + const requeued = yield* Ref.make(false); | | 404 | + const layerOutbox = Layer.mock(EffectOutbox.EffectOutboxV2)({ | | 405 | + claimNext: () => Effect.succeedSome(claimedEffect), | | 406 | + get: () => Effect.succeedSome(claimedEffect), | | 407 | + awaitCancellation: () => Effect.never, | | 408 | + clearCancellation: () => Effect.void, | | 409 | + retry: () => | | 410 | + Effect.gen(function* () { | | 411 | + if ((yield* Ref.getAndUpdate(lockedWrites, (count) => count - 1)) > 0) { | | 412 | + return yield* new EffectOutbox.EffectOutboxError({ | | 413 | + operation: "retry", | | 414 | + effectId, | | 415 | + cause: new SqlError.SqlError({ | | 416 | + reason: new SqlError.LockTimeoutError({ cause: null }), | | 417 | + }), | | 418 | + }); | | 419 | + } | | 420 | + yield* Ref.set(requeued, true); | | 421 | + return true; | | 422 | + }), | | 423 | + }); | | 424 | + const layerExecutor = Layer.succeed( | | 425 | + EffectWorker.OrchestrationEffectExecutorV2, | | 426 | + EffectWorker.OrchestrationEffectExecutorV2.of({ | | 427 | + execute: () => | | 428 | + Effect.fail( | | 429 | + new EffectWorker.OrchestrationEffectExecutionError({ | | 430 | + effectId, | | 431 | + effectType: "provider-runtime.continue", | | 432 | + cause: "simulated database is locked", | | 433 | + }), | | 434 | + ), | | 435 | + }), | | 436 | + ); | | 437 | + const layerWorker = EffectWorker.layerWithOptions({ workerId }).pipe( | | 438 | + Layer.provide(Layer.merge(layerOutbox, layerExecutor)), | | 439 | + ); | | 440 | + | | 441 | + yield* EffectWorker.OrchestrationEffectWorkerV2.pipe( | | 442 | + Effect.flatMap((worker) => worker.runOnce), | | 443 | + Effect.provide(layerWorker), | | 444 | + Effect.exit, | | 445 | + ); | | 446 | + | | 447 | + assert.isTrue(yield* Ref.get(requeued)); | | 448 | + }), | | 449 | +); | | 450 | + | 382 | 451 | it.effect("arms cancellation before the durable pre-execution check", () => | 383 | 452 | Effect.gen(function* () { | 384 | 453 | const now = DateTime.formatIso(yield* DateTime.now); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/EffectWorker.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ import * as Layer from "effect/Layer"; | 9 | 9 | import * as Metric from "effect/Metric"; | 10 | 10 | import * as Option from "effect/Option"; | 11 | 11 | import * as Schema from "effect/Schema"; | | 12 | +import * as SqlError from "effect/sql/SqlError"; | 12 | 13 |
| 13 | 14 | import { | 14 | 15 | increment, | @@ -596,6 +597,13 @@ export class OrchestrationEffectWorkerError extends Schema.TaggedError<Orchestra | 596 | 597 |
| 597 | 598 | const isOrchestrationEffectWorkerError = Schema.is(OrchestrationEffectWorkerError); | 598 | 599 |
| | 600 | +// Only the claiming worker settles its row, and a running row blocks every later effect on its | | 601 | +// thread until the server restarts. Each lock timeout has already waited out busy_timeout. | | 602 | +const isLockTimeout = (error: EffectOutbox.EffectOutboxError) => | | 603 | + SqlError.isSqlError(error.cause) && error.cause.reason._tag === "LockTimeoutError"; | | 604 | +const retryWhileLocked = <A>(settlement: Effect.Effect<A, EffectOutbox.EffectOutboxError>) => | | 605 | + Effect.retry(settlement, { while: isLockTimeout }); | | 606 | + | 599 | 607 | export interface OrchestrationEffectWorkerV2Shape { | 600 | 608 | readonly awaitWork: Effect.Effect<void>; | 601 | 609 | readonly runOnce: Effect.Effect<boolean, OrchestrationEffectWorkerError>; | @@ -654,6 +662,7 @@ export const layerWithOptions = ( | 654 | 662 | delayMs: 0, | 655 | 663 | }) | 656 | 664 | .pipe( | | 665 | + retryWhileLocked, | 657 | 666 | Effect.flatMap((requeued) => | 658 | 667 | requeued | 659 | 668 | ? Effect.logWarning("Requeued effect after unexpected worker failure", { | @@ -685,6 +694,7 @@ export const layerWithOptions = ( | 685 | 694 | error: `Worker failed to settle a process-bound effect after execution started: ${Cause.pretty(cause)}`, | 686 | 695 | }) | 687 | 696 | .pipe( | | 697 | + retryWhileLocked, | 688 | 698 | Effect.flatMap((failed) => | 689 | 699 | failed | 690 | 700 | ? Effect.logError("Terminalized process-bound effect after settlement failure", { | |
|