Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 76 additions & 23 deletions apps/server/src/orchestration-v2/EventSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,22 @@ export type EventSinkV2Error = typeof EventSinkV2Error.Type;

interface ProviderBatchPublication {
readonly publish: Array<Effect.Effect<void>>;
readonly projection: ProjectionStore.ProviderBatchProjection;
/** The projection high-water mark, recorded once before the batch commits. */
projectedSequence: number | undefined;
eventCount: number;
}
const CurrentProviderBatch = Context.Reference<ProviderBatchPublication | undefined>(
"t3/orchestration-v2/EventSink/CurrentProviderBatch",
{ defaultValue: () => undefined },
);
// Projecting `provider-thread.updated` also rewrites its app thread's row.
const projectedThreadIds = (events: ReadonlyArray<OrchestrationV2DomainEvent>) =>
events.flatMap((event) =>
event.type === "provider-thread.updated" && event.payload.appThreadId !== null
? [event.threadId, event.payload.appThreadId]
: [event.threadId],
);

export interface EventSinkV2Shape {
/** Items that are not provider snapshots, such as a consumer's own control items, never batch. */
Expand Down Expand Up @@ -409,6 +419,30 @@ const layerBase: Layer.Layer<
);
};

// The enclosing write or commit span already traces this projection bookkeeping.
const recordProjectedSequence = Effect.fnUntraced(function* (sequence: number) {
const now = DateTime.formatIso(yield* DateTime.now);
yield* sql`
INSERT INTO orchestration_v2_projection_metadata (
projection_name,
schema_version,
last_sequence,
updated_at
)
VALUES (
'thread-projections',
${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
${sequence},
${now}
)
ON CONFLICT(projection_name)
DO UPDATE SET
schema_version = excluded.schema_version,
last_sequence = excluded.last_sequence,
updated_at = excluded.updated_at
`;
});

const applyStoredEvents =
/* This helper runs for individual orchestration events; the enclosing operation owns the span. */
Effect.fnUntraced(function* (storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>) {
Expand All @@ -417,26 +451,9 @@ const layerBase: Layer.Layer<
});
const sequence = storedEvents.at(-1)?.sequence;
if (sequence !== undefined) {
const now = DateTime.formatIso(yield* DateTime.now);
yield* sql`
INSERT INTO orchestration_v2_projection_metadata (
projection_name,
schema_version,
last_sequence,
updated_at
)
VALUES (
'thread-projections',
${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
${sequence},
${now}
)
ON CONFLICT(projection_name)
DO UPDATE SET
schema_version = excluded.schema_version,
last_sequence = excluded.last_sequence,
updated_at = excluded.updated_at
`;
const batch = yield* CurrentProviderBatch;
if (batch !== undefined) batch.projectedSequence = sequence;
else yield* recordProjectedSequence(sequence);
}
// Read lineage here, on the connection this transaction holds; see makeRunOutcomeRecorder.
return yield* runOutcomes.prepare(storedEvents);
Expand Down Expand Up @@ -476,6 +493,7 @@ const layerBase: Layer.Layer<
const batch = yield* CurrentProviderBatch;
if (batch !== undefined) batch.eventCount += input.events.length;
const { committed } = yield* commitThenPublish(
projectedThreadIds(input.events),
Effect.gen(function* () {
// Check ownership in the same transaction that claims the native session.
const imported = input.unlessNativeThreadOwned;
Expand Down Expand Up @@ -530,6 +548,7 @@ const layerBase: Layer.Layer<
});

return yield* commitThenPublish(
[input.threadId, ...projectedThreadIds(input.events)],
Effect.gen(function* () {
const rows = yield* sql<{
readonly status: string;
Expand Down Expand Up @@ -611,6 +630,7 @@ const layerBase: Layer.Layer<
});

return yield* commitThenPublish(
projectedThreadIds(input.events),
Effect.gen(function* () {
const rows = yield* sql<{
readonly active_attempt_id: string | null;
Expand Down Expand Up @@ -664,6 +684,7 @@ const layerBase: Layer.Layer<
events: ReadonlyArray<OrchestrationV2DomainEvent>,
) =>
commitThenPublish(
projectedThreadIds(events),
Effect.gen(function* () {
const rows = yield* readStoredPayload;
if (rows[0]?.payload_json !== expectedJson) {
Expand Down Expand Up @@ -764,6 +785,7 @@ const layerBase: Layer.Layer<
input: Parameters<EventSinkV2Shape["commitCommand"]>[0],
) {
const result = yield* commitThenPublish(
[input.threadId, ...projectedThreadIds(input.events)],
Effect.gen(function* () {
const reserved = yield* commandReceipts.insertIfAbsent({
commandId: input.commandId,
Expand Down Expand Up @@ -909,6 +931,7 @@ const layerBase: Layer.Layer<
const commitProjectCommandEffect = Effect.fn("orchestrationV2.EventSink.commitProjectCommand")(
function* (input: Parameters<EventSinkV2Shape["commitProjectCommand"]>[0]) {
const result = yield* commitThenPublish(
[],
Effect.gen(function* () {
const reserved: CommandReceiptStore.ProjectCommandReceiptV2 = {
commandId: input.commandId,
Expand Down Expand Up @@ -1037,7 +1060,9 @@ const layerBase: Layer.Layer<
);
};

// Waits only for queued provider snapshots of `threadIds`, the threads this write touches.
const commitThenPublish = <A, E, R>(
threadIds: ReadonlyArray<ThreadId>,
transaction: Effect.Effect<A, E, R>,
publish: (committed: A) => Effect.Effect<void>,
) =>
Expand All @@ -1046,7 +1071,7 @@ const layerBase: Layer.Layer<
? Effect.flatMap(Effect.serviceOption(sql.transactionService), (existing) =>
Option.isSome(existing)
? commitThenPublishNow(transaction, publish)
: coordinator.barrier(commitThenPublishNow(transaction, publish)),
: coordinator.barrier(commitThenPublishNow(transaction, publish), threadIds),
)
: transaction.pipe(
Effect.tap((committed) =>
Expand All @@ -1061,12 +1086,33 @@ const layerBase: Layer.Layer<
"orchestrationV2.EventSink.providerBatch",
)(function* <A, E, R>(effect: Effect.Effect<A, E, R>) {
if ((yield* CurrentProviderBatch) !== undefined) return yield* effect;
const batch: ProviderBatchPublication = { publish: [], eventCount: 0 };
const batch: ProviderBatchPublication = {
publish: [],
eventCount: 0,
projection: { nodeReads: new Map(), threadTouches: new Map() },
projectedSequence: undefined,
};
let started = false;
const result = yield* commitThenPublishNow(
Effect.sync(() => {
started = true;
}).pipe(Effect.andThen(Effect.provideService(effect, CurrentProviderBatch, batch))),
}).pipe(
Effect.andThen(
effect.pipe(
Effect.provideService(CurrentProviderBatch, batch),
Effect.provideService(ProjectionStore.ProviderBatchProjectionState, batch.projection),
Effect.tap(() =>
ProjectionStore.writeThreadTouches(sql, batch.projection.threadTouches).pipe(
Effect.andThen(
batch.projectedSequence === undefined
? Effect.void
: recordProjectedSequence(batch.projectedSequence),
),
),
),
),
),
),
() => Effect.forEach(batch.publish, (publish) => publish, { discard: true }),
).pipe(
// Normalization mutates run-local state, so only retry before its transaction body starts.
Expand Down Expand Up @@ -1105,12 +1151,19 @@ const layerBase: Layer.Layer<
const batch = yield* CurrentProviderBatch;
const publicationCount = batch?.publish.length ?? 0;
const eventCount = batch?.eventCount ?? 0;
const projectedSequence = batch?.projectedSequence;
const threadTouches = new Map(batch?.projection.threadTouches);
return yield* sql.withTransaction(work).pipe(
Effect.onError(() =>
Effect.sync(() => {
if (batch !== undefined) {
batch.publish.length = publicationCount;
batch.eventCount = eventCount;
batch.projectedSequence = projectedSequence;
batch.projection.nodeReads.clear();
batch.projection.threadTouches.clear();
for (const [threadId, updatedAt] of threadTouches)
batch.projection.threadTouches.set(threadId, updatedAt);
}
}),
),
Expand Down
Loading
Loading