|
2 | 2 | CommandId, |
3 | 3 | type OrchestrationV2Run, |
4 | 4 | OrchestrationV2DomainEvent, |
| 5 | + type OrchestrationV2ProviderThread, |
5 | 6 | OrchestrationV2StoredEvent, |
6 | 7 | ProviderThreadId, |
7 | 8 | RunAttemptId, |
@@ -31,6 +32,7 @@ import { |
31 | 32 | } from "./EffectOutbox.ts"; |
32 | 33 | import { EventStoreV2 } from "./EventStore.ts"; |
33 | 34 | import { |
| 35 | + encodeProviderThreadPayload, |
34 | 36 | ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION, |
35 | 37 | ProjectionStoreV2, |
36 | 38 | } from "./ProjectionStore.ts"; |
@@ -148,6 +150,22 @@ export interface EventSinkV2Shape { |
148 | 150 | }, |
149 | 151 | EventSinkV2Error |
150 | 152 | >; |
| 153 | + /** |
| 154 | + * Atomically commit only while the stored provider thread still equals |
| 155 | + * `expected`, the row its writer read, so a read-modify-write of the row |
| 156 | + * cannot overwrite an update published in between. The whole row is |
| 157 | + * compared because not every writer moves `updatedAt`. |
| 158 | + */ |
| 159 | + readonly writeIfProviderThreadUnchanged: (input: { |
| 160 | + readonly expected: OrchestrationV2ProviderThread; |
| 161 | + readonly events: ReadonlyArray<OrchestrationV2DomainEvent>; |
| 162 | + }) => Effect.Effect< |
| 163 | + { |
| 164 | + readonly committed: boolean; |
| 165 | + readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>; |
| 166 | + }, |
| 167 | + EventSinkV2Error |
| 168 | + >; |
151 | 169 | readonly commitCommand: ( |
152 | 170 | input: EventSinkCommitCommandInput, |
153 | 171 | ) => Effect.Effect<EventSinkCommitCommandResult, EventSinkV2Error>; |
@@ -434,6 +452,43 @@ const baseLayer: Layer.Layer< |
434 | 452 | return result; |
435 | 453 | }); |
436 | 454 |
|
| 455 | + const writeIfProviderThreadUnchangedEffect = Effect.fn( |
| 456 | + "orchestrationV2.EventSink.writeIfProviderThreadUnchanged", |
| 457 | + )(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) { |
| 458 | + yield* Effect.annotateCurrentSpan({ |
| 459 | + "orchestration_v2.event_count": input.events.length, |
| 460 | + "orchestration_v2.provider_thread_id": input.expected.id, |
| 461 | + }); |
| 462 | + const expectedJson = yield* encodeProviderThreadPayload(input.expected); |
| 463 | + |
| 464 | + const result = yield* sql.withTransaction( |
| 465 | + Effect.gen(function* () { |
| 466 | + const rows = yield* sql<{ readonly payload_json: string }>` |
| 467 | + SELECT payload_json |
| 468 | + FROM orchestration_v2_projection_provider_threads |
| 469 | + WHERE provider_thread_id = ${input.expected.id} |
| 470 | + LIMIT 1 |
| 471 | + `; |
| 472 | + if (rows[0]?.payload_json !== expectedJson) { |
| 473 | + return { |
| 474 | + committed: false as const, |
| 475 | + storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>, |
| 476 | + }; |
| 477 | + } |
| 478 | + |
| 479 | + const normalized = yield* normalizeEvents(input.events); |
| 480 | + const storedEvents = yield* eventStore.append({ events: normalized }); |
| 481 | + yield* applyStoredEvents(storedEvents); |
| 482 | + return { committed: true as const, storedEvents }; |
| 483 | + }), |
| 484 | + ); |
| 485 | + if (result.committed) { |
| 486 | + yield* eventStore.publishCommitted(result.storedEvents); |
| 487 | + yield* PubSub.publishAll(liveEvents, result.storedEvents); |
| 488 | + } |
| 489 | + return result; |
| 490 | + }); |
| 491 | + |
437 | 492 | const existingCommandResult = (commandId: CommandId) => |
438 | 493 | Effect.gen(function* () { |
439 | 494 | const existing = yield* commandReceipts.getByCommandId(commandId); |
@@ -680,6 +735,12 @@ const baseLayer: Layer.Layer< |
680 | 735 | }), |
681 | 736 | ), |
682 | 737 | ), |
| 738 | + writeIfProviderThreadUnchanged: (input) => |
| 739 | + writeIfProviderThreadUnchangedEffect(input).pipe( |
| 740 | + Effect.mapError( |
| 741 | + (cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }), |
| 742 | + ), |
| 743 | + ), |
683 | 744 | commitCommand: (input) => |
684 | 745 | commitCommandEffect(input).pipe( |
685 | 746 | Effect.flatMap((result) => |
|
0 commit comments