|
2 | 2 | CommandId, |
3 | 3 | type OrchestrationV2Run, |
4 | 4 | OrchestrationV2DomainEvent, |
| 5 | + type OrchestrationV2ProviderThread, |
5 | 6 | OrchestrationV2StoredEvent, |
6 | 7 | ProviderThreadId, |
7 | 8 | RunAttemptId, |
@@ -119,6 +120,22 @@ export interface EventSinkV2Shape { |
119 | 120 | }, |
120 | 121 | EventSinkV2Error |
121 | 122 | >; |
| 123 | + /** |
| 124 | + * Atomically commit only while the stored provider thread still equals |
| 125 | + * `expected`, the row its writer read, so a read-modify-write of the row |
| 126 | + * cannot overwrite an update published in between. The whole row is |
| 127 | + * compared because not every writer moves `updatedAt`. |
| 128 | + */ |
| 129 | + readonly writeIfProviderThreadUnchanged: (input: { |
| 130 | + readonly expected: OrchestrationV2ProviderThread; |
| 131 | + readonly events: ReadonlyArray<OrchestrationV2DomainEvent>; |
| 132 | + }) => Effect.Effect< |
| 133 | + { |
| 134 | + readonly committed: boolean; |
| 135 | + readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>; |
| 136 | + }, |
| 137 | + EventSinkV2Error |
| 138 | + >; |
122 | 139 | readonly commitCommand: (input: { |
123 | 140 | readonly commandId: CommandId; |
124 | 141 | readonly threadId: ThreadId; |
@@ -509,6 +526,39 @@ const layerBase: Layer.Layer< |
509 | 526 | ); |
510 | 527 | }); |
511 | 528 |
|
| 529 | + const writeIfProviderThreadUnchangedEffect = Effect.fn( |
| 530 | + "orchestrationV2.EventSink.writeIfProviderThreadUnchanged", |
| 531 | + )(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) { |
| 532 | + yield* Effect.annotateCurrentSpan({ |
| 533 | + "orchestration_v2.event_count": input.events.length, |
| 534 | + "orchestration_v2.provider_thread_id": input.expected.id, |
| 535 | + }); |
| 536 | + const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected); |
| 537 | + |
| 538 | + return yield* commitThenPublish( |
| 539 | + Effect.gen(function* () { |
| 540 | + const rows = yield* sql<{ readonly payload_json: string }>` |
| 541 | + SELECT payload_json |
| 542 | + FROM orchestration_v2_projection_provider_threads |
| 543 | + WHERE provider_thread_id = ${input.expected.id} |
| 544 | + LIMIT 1 |
| 545 | + `; |
| 546 | + if (rows[0]?.payload_json !== expectedJson) { |
| 547 | + return { |
| 548 | + committed: false as const, |
| 549 | + storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>, |
| 550 | + }; |
| 551 | + } |
| 552 | + |
| 553 | + const normalized = yield* normalizeEvents(input.events); |
| 554 | + const storedEvents = yield* eventStore.append({ events: normalized }); |
| 555 | + yield* applyStoredEvents(storedEvents); |
| 556 | + return { committed: true as const, storedEvents }; |
| 557 | + }), |
| 558 | + (result) => (result.committed ? publishStoredEvents(result.storedEvents) : Effect.void), |
| 559 | + ); |
| 560 | + }); |
| 561 | + |
512 | 562 | const existingCommandResult = (commandId: CommandId) => |
513 | 563 | Effect.gen(function* () { |
514 | 564 | const existing = yield* commandReceipts.getByCommandId(commandId); |
@@ -804,6 +854,12 @@ const layerBase: Layer.Layer< |
804 | 854 | }), |
805 | 855 | ), |
806 | 856 | ), |
| 857 | + writeIfProviderThreadUnchanged: (input) => |
| 858 | + writeIfProviderThreadUnchangedEffect(input).pipe( |
| 859 | + Effect.mapError( |
| 860 | + (cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }), |
| 861 | + ), |
| 862 | + ), |
807 | 863 | commitCommand: (input) => |
808 | 864 | commitCommandEffect(input).pipe( |
809 | 865 | Effect.mapError( |
|
0 commit comments