forked from crackedstudio/tikka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor-manager.service.ts
More file actions
235 lines (202 loc) · 6.54 KB
/
Copy pathcursor-manager.service.ts
File metadata and controls
235 lines (202 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* cursor-manager.service.ts
*
* Manages the singleton cursor row (id=1) in the indexer_cursor table.
*/
import { Injectable, Logger, Optional } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { EntityManager, QueryRunner, Repository } from "typeorm";
import { IndexerCursorEntity } from "../database/entities/indexer-cursor.entity";
import {
CursorCheckpoint,
CURSOR_CHECKPOINT_VERSION,
CursorIntegrity,
IntegrityViolation,
validateBeforeSave,
validateLedgerHash,
} from "./cursor-integrity";
import { PipelineStateMachine } from "./pipeline-state";
export type IngestorMode = "RUNNING" | "DEGRADED" | "STOPPED";
export interface IndexerCursor {
lastLedger: number;
lastPagingToken?: string;
ledgerHashes: Array<{ ledger: number; hash: string }>;
}
export interface CursorManagerStatus {
mode: IngestorMode;
lastCheckpoint: CursorCheckpoint | null;
lastViolation: IntegrityViolation | null;
startupIntegrityPassed: boolean;
uptimeMs: number;
}
export class CursorIntegrityError extends Error {
constructor(
public readonly violation: IntegrityViolation,
public readonly checkpoint: CursorCheckpoint,
) {
super(`Cursor integrity validation failed: ${violation.code}`);
this.name = "CursorIntegrityError";
}
}
const HASH_RING_SIZE = 200;
@Injectable()
export class CursorManagerService {
private readonly logger = new Logger(CursorManagerService.name);
private mode: IngestorMode = "RUNNING";
private lastCheckpoint: CursorCheckpoint | null = null;
private lastViolation: IntegrityViolation | null = null;
private startupIntegrityPassed = true;
private readonly startedAt = Date.now();
constructor(
@InjectRepository(IndexerCursorEntity)
private readonly cursorRepo: Repository<IndexerCursorEntity>,
@Optional() private readonly pipeline?: PipelineStateMachine,
) {}
getStatus(): CursorManagerStatus {
return {
mode: this.mode,
lastCheckpoint: this.lastCheckpoint,
lastViolation: this.lastViolation,
startupIntegrityPassed: this.startupIntegrityPassed,
uptimeMs: Date.now() - this.startedAt,
};
}
async validateStartupIntegrity(): Promise<void> {
this.logger.debug("Validating cursor integrity on startup...");
const row = await this.cursorRepo.findOne({ where: { id: 1 } });
if (!row || row.lastLedger === 0) {
this.startupIntegrityPassed = true;
this.lastViolation = null;
return;
}
const checkpoint = this.toCheckpoint(row);
const violation = CursorIntegrity.validate(checkpoint);
if (violation) {
this.startupIntegrityPassed = false;
this.transitionDegraded(violation);
throw new CursorIntegrityError(violation, checkpoint);
}
this.startupIntegrityPassed = true;
this.lastCheckpoint = checkpoint;
this.lastViolation = null;
}
async getCursor(): Promise<IndexerCursor | null> {
this.logger.debug("Fetching cursor from storage...");
const row = await this.cursorRepo.findOne({ where: { id: 1 } });
if (!row || row.lastLedger === 0) return null;
const stored = this.toCheckpoint(row);
const violation = CursorIntegrity.validate(stored);
if (violation) {
this.startupIntegrityPassed = false;
this.transitionDegraded(violation);
return null;
}
this.lastCheckpoint = stored;
return {
lastLedger: row.lastLedger,
lastPagingToken: row.lastPagingToken,
ledgerHashes: row.ledgerHashes ?? [],
};
}
async saveCursor(
ledger: number,
ledgerHash: string,
token?: string,
processedCount?: number,
queryRunner?: QueryRunner,
): Promise<void> {
if (this.mode === "DEGRADED") {
this.logger.warn(
"saveCursor called while in DEGRADED mode — write suppressed",
);
return;
}
const savedAt = new Date().toISOString();
const eventCount =
processedCount ?? this.lastCheckpoint?.processedEventCount ?? 0;
const candidate: CursorCheckpoint = {
sequence: ledger,
ledgerHash,
processedEventCount: eventCount,
savedAt,
version: CURSOR_CHECKPOINT_VERSION,
};
const violation = validateBeforeSave(candidate, this.lastCheckpoint);
if (violation) {
this.transitionDegraded(violation);
throw new CursorIntegrityError(violation, candidate);
}
const manager: EntityManager = queryRunner
? queryRunner.manager
: this.cursorRepo.manager;
const existing = await manager.findOne(IndexerCursorEntity, {
where: { id: 1 },
});
const hashes = [
...(existing?.ledgerHashes ?? []),
{ ledger, hash: ledgerHash },
];
while (hashes.length > HASH_RING_SIZE) {
hashes.shift();
}
await manager.upsert(
IndexerCursorEntity,
{
id: 1,
lastLedger: ledger,
lastPagingToken: token ?? "",
ledgerHashes: hashes,
processedEventCount: eventCount,
savedAt: new Date(savedAt),
checkpointVersion: CURSOR_CHECKPOINT_VERSION,
},
["id"],
);
this.lastCheckpoint = candidate;
}
async checkForReorg(
ledger: number,
expectedHash: string,
): Promise<number | null> {
const cursor = await this.cursorRepo.findOne({ where: { id: 1 } });
if (!cursor) return null;
const stored = (cursor.ledgerHashes ?? []).find(
(entry: { ledger: number; hash: string }) => entry.ledger === ledger,
);
if (!stored) return null;
if (stored.hash !== expectedHash) {
this.logger.warn(
`Reorg detected at ledger ${ledger}: expected ${expectedHash}, got ${stored.hash}`,
);
if (this.lastCheckpoint?.sequence === ledger) {
const violation = validateLedgerHash(this.lastCheckpoint, expectedHash);
if (violation) {
this.transitionDegraded(violation);
}
}
return ledger;
}
return null;
}
private toCheckpoint(row: IndexerCursorEntity): CursorCheckpoint {
return {
sequence: row.lastLedger,
ledgerHash: row.ledgerHashes?.[row.ledgerHashes.length - 1]?.hash ?? "",
processedEventCount: Number(row.processedEventCount),
savedAt:
row.savedAt instanceof Date
? row.savedAt.toISOString()
: String(row.savedAt),
version: row.checkpointVersion,
};
}
private transitionDegraded(violation: IntegrityViolation): void {
this.mode = "DEGRADED";
this.lastViolation = violation;
this.logger.error("cursor_integrity_violation", {
violation: violation.code,
detail: violation,
action: "ingestion_paused_awaiting_operator",
});
}
}