Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestrationRecovery.ts

136 lines4.1 KB

1export type OrchestrationRecoveryReason = "bootstrap" | "sequence-gap" | "replay-failed";
2​
3export interface OrchestrationRecoveryPhase {
4 kind: "snapshot" | "replay";
5 reason: OrchestrationRecoveryReason;
6}
7​
8export interface OrchestrationRecoveryState {
9 latestSequence: number;
10 highestObservedSequence: number;
11 bootstrapped: boolean;
12 pendingReplay: boolean;
13 inFlight: OrchestrationRecoveryPhase | null;
15​
16type SequencedEvent = Readonly<{ sequence: number }>;
17​
18export function createOrchestrationRecoveryCoordinator() {
19 let state: OrchestrationRecoveryState = {
20 latestSequence: 0,
21 highestObservedSequence: 0,
22 bootstrapped: false,
23 pendingReplay: false,
24 inFlight: null,
25 };
26 let replayStartSequence: number | null = null;
27​
28 const snapshotState = (): OrchestrationRecoveryState => ({
29 ...state,
30 ...(state.inFlight ? { inFlight: { ...state.inFlight } } : {}),
31 });
32​
33 const observeSequence = (sequence: number) => {
34 state.highestObservedSequence = Math.max(state.highestObservedSequence, sequence);
35 };
36​
37 const shouldReplayAfterRecovery = (): boolean => {
38 const shouldReplay =
39 state.pendingReplay || state.highestObservedSequence > state.latestSequence;
40 state.pendingReplay = false;
41 return shouldReplay;
42 };
43​
44 return {
45 getState(): OrchestrationRecoveryState {
46 return snapshotState();
47 },
48​
49 classifyDomainEvent(sequence: number): "ignore" | "defer" | "recover" | "apply" {
50 observeSequence(sequence);
51 if (sequence <= state.latestSequence) {
52 return "ignore";
53 }
54 if (!state.bootstrapped || state.inFlight) {
55 state.pendingReplay = true;
56 return "defer";
57 }
58 if (sequence !== state.latestSequence + 1) {
59 state.pendingReplay = true;
60 return "recover";
61 }
62 return "apply";
63 },
64​
65 markEventBatchApplied<T extends SequencedEvent>(events: ReadonlyArray<T>): ReadonlyArray<T> {
66 const nextEvents = events
67 .filter((event) => event.sequence > state.latestSequence)
68 .toSorted((left, right) => left.sequence - right.sequence);
69 if (nextEvents.length === 0) {
70 return [];
71 }
72​
73 state.latestSequence = nextEvents.at(-1)?.sequence ?? state.latestSequence;
74 state.highestObservedSequence = Math.max(state.highestObservedSequence, state.latestSequence);
75 return nextEvents;
76 },
77​
78 beginSnapshotRecovery(reason: OrchestrationRecoveryReason): boolean {
79 if (state.inFlight?.kind === "snapshot") {
80 state.pendingReplay = true;
81 return false;
82 }
83 if (state.inFlight?.kind === "replay") {
84 state.pendingReplay = true;
85 return false;
86 }
87 state.inFlight = { kind: "snapshot", reason };
88 return true;
89 },
90​
91 completeSnapshotRecovery(snapshotSequence: number): boolean {
92 state.latestSequence = Math.max(state.latestSequence, snapshotSequence);
93 state.highestObservedSequence = Math.max(state.highestObservedSequence, state.latestSequence);
94 state.bootstrapped = true;
95 state.inFlight = null;
96 return shouldReplayAfterRecovery();
97 },
98​
99 failSnapshotRecovery(): void {
100 state.inFlight = null;
101 },
102​
103 beginReplayRecovery(reason: OrchestrationRecoveryReason): boolean {
104 if (!state.bootstrapped || state.inFlight?.kind === "snapshot") {
105 state.pendingReplay = true;
106 return false;
107 }
108 if (state.inFlight?.kind === "replay") {
109 state.pendingReplay = true;
110 return false;
111 }
112 state.pendingReplay = false;
113 replayStartSequence = state.latestSequence;
114 state.inFlight = { kind: "replay", reason };
115 return true;
116 },
117​
118 completeReplayRecovery(): boolean {
119 const replayMadeProgress =
120 replayStartSequence !== null && state.latestSequence > replayStartSequence;
121 replayStartSequence = null;
122 state.inFlight = null;
123 if (!replayMadeProgress) {
124 state.pendingReplay = false;
125 return false;
126 }
127 return shouldReplayAfterRecovery();
128 },
129​
130 failReplayRecovery(): void {
131 replayStartSequence = null;
132 state.bootstrapped = false;
133 state.inFlight = null;
134 },
135 };