@supalive/core 1.20.0 → 1.20.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,792 @@
1
+ import { r as supaliveStringify } from "./helper-zdJT5FUc.js";
2
+ import { a as mapWriteSetToRaw, i as mapReadSetToRaw, n as checkWriteSetAffectsReadSet, r as evaluateCacheFreshness } from "./overlap-checker-CCgq_Tpa.js";
3
+ import { b as WriteEntrySchema, m as QueryCacheMetadataSchema, n as BigIntSchema, p as QueryCacheEntrySchema, r as CachedPgMetadataSchema, y as ReadEntrySchema } from "./types_db-OUou3o2Z.js";
4
+ import { t as MySqlDatabase } from "./mysql-BX3cm94v.js";
5
+ import { t as PgDatabase } from "./postgres-DwiUT_A6.js";
6
+ import { i as logger$1, o as SupaliveDb, r as logLevelOf } from "./logger-DrXccWFZ.js";
7
+ import z from "zod";
8
+ import { ExponentialBackoff, handleAll, retry } from "cockatiel";
9
+ import { createHash } from "node:crypto";
10
+ import Redis from "ioredis";
11
+ //#region src/server/retention-watermark.ts
12
+ /** Redis pub/sub channel the sub-manager publishes the retention watermark on
13
+ * after each prune (and at startup). App-server instances subscribe and update
14
+ * their in-memory {@link RetentionWatermark}. Payload is the watermark `ts` as a
15
+ * string. */
16
+ const RETENTION_WATERMARK_CHANNEL = "supalive:retention_watermark";
17
+ /**
18
+ * In-memory holder for the commit-log retention watermark: the oldest commit-log
19
+ * ts still retained after prune (see {@link Database.getMinSnapshotTimestamp}).
20
+ * Read on the hot path by the query-cache freshness check — a plain variable, no
21
+ * per-check Redis/DB round trip. Loaded once at startup from the `metadata`
22
+ * table and kept fresh out-of-band: the sub-manager broadcasts the new watermark
23
+ * (in-process to its workers, over Redis pub/sub to app servers) each time it
24
+ * prunes.
25
+ *
26
+ * `set` is monotonic — the watermark only ever moves forward (pruning never
27
+ * un-deletes rows), so an out-of-order or stale update can never lower it, which
28
+ * keeps the freshness guard on the safe side (an over-estimate only costs an
29
+ * extra recompute; an under-estimate could serve stale data).
30
+ *
31
+ * Initial value is the -1n genesis (not 0), matching `getMinSnapshotTimestamp`
32
+ * and the empty-DB snapshot ts — so before anything is pruned the guard never
33
+ * rejects a genesis-era entry (whose snapshot is -1).
34
+ */
35
+ var RetentionWatermark = class {
36
+ ts = -1n;
37
+ get() {
38
+ return this.ts;
39
+ }
40
+ set(ts) {
41
+ if (ts > this.ts) this.ts = ts;
42
+ }
43
+ /** Seed from the persisted watermark (metadata table). Returns the current value. */
44
+ async loadFrom(db) {
45
+ this.set(await db.getMinSnapshotTimestamp());
46
+ return this.ts;
47
+ }
48
+ };
49
+ //#endregion
50
+ //#region src/server/sub-manager-types.ts
51
+ const RegisterSubscriptionParamsSchema = z.object({
52
+ /**
53
+ * Caller-computed FNV-1a routing key + identity for this subscription.
54
+ * The sub-manager trusts these values rather than re-deriving them from
55
+ * (queryName, args, queryIdentity); this avoids one stringify+SHA1 per
56
+ * register RPC. Trust is fine: the sub-manager RPC channel is fully
57
+ * internal.
58
+ */
59
+ subId: z.string(),
60
+ cacheKey: z.string(),
61
+ queryName: z.string(),
62
+ args: z.unknown(),
63
+ /**
64
+ * Segmentation key used in the subId/cacheKey hash. Defaults to the
65
+ * authenticated user's id ("anonymous" when none), unless the
66
+ * procedure overrides it via `QueryConfig.queryIdentity`.
67
+ */
68
+ queryIdentity: z.string(),
69
+ instanceName: z.string(),
70
+ lastSnapshotTs: BigIntSchema
71
+ });
72
+ const RegisterSubscriptionResultSchema = z.object({
73
+ cacheKey: z.string(),
74
+ recompute: z.boolean(),
75
+ readSet: z.array(ReadEntrySchema),
76
+ version: z.string().optional()
77
+ });
78
+ const RegisterSubscriptionBatchParamsSchema = z.array(RegisterSubscriptionParamsSchema);
79
+ const RegisterSubscriptionBatchResultSchema = z.array(RegisterSubscriptionResultSchema);
80
+ const UpdateSubscriptionReadSetParamsSchema = z.object({
81
+ cacheKey: z.string(),
82
+ subId: z.string(),
83
+ instanceName: z.string(),
84
+ lastSnapshotTs: BigIntSchema,
85
+ readSet: z.array(ReadEntrySchema)
86
+ });
87
+ /**
88
+ * `tracked: false` means the sub-manager has no record of this subId (e.g.
89
+ * a restart wiped state and the caller hasn't re-registered yet). The app
90
+ * server should treat this as a signal to register the sub before retrying.
91
+ */
92
+ const UpdateSubscriptionReadSetResultSchema = z.object({ tracked: z.boolean() });
93
+ const UnregisterSubscriptionParamsSchema = z.object({
94
+ subId: z.string(),
95
+ instanceName: z.string()
96
+ });
97
+ const UnregisterSubscriptionsParamsSchema = z.array(UnregisterSubscriptionParamsSchema);
98
+ const UnregisterSubscriptionResultSchema = z.object({ ok: z.boolean() });
99
+ const UnregisterSubscriptionsResultSchema = z.array(z.object({ ok: z.boolean() }));
100
+ const InvalidateWritesetParamsSchema = z.object({ writeSet: z.array(WriteEntrySchema) });
101
+ const AffectedSubscriptionSchema = z.object({
102
+ subId: z.string(),
103
+ queryName: z.string(),
104
+ args: z.unknown(),
105
+ cacheKey: z.string(),
106
+ /**
107
+ * Every live instance that currently holds this subId. Routing rules:
108
+ * • If the writer is in this list, it recomputes locally and publishes
109
+ * an `update` to the rest (its own id is filtered by the publisher).
110
+ * • Otherwise the writer broadcasts a `recompute-race` to every entry;
111
+ * receivers race for a Redis lock and the winner recomputes.
112
+ */
113
+ notifyInstances: z.array(z.string())
114
+ });
115
+ const InvalidateWritesetResultSchema = z.object({ affected: z.array(AffectedSubscriptionSchema) });
116
+ const RegistrationRecordSchema = z.object({
117
+ queryName: z.string(),
118
+ args: z.unknown(),
119
+ queryIdentity: z.string(),
120
+ cacheKey: z.string()
121
+ });
122
+ //#endregion
123
+ //#region src/db/cache.ts
124
+ const logger = logger$1.child({ "name": "CACHE_LAYER" }, { level: logLevelOf("SERVER_LOG_LEVEL") });
125
+ const redisRetryPolicy = retry(handleAll, {
126
+ maxAttempts: 3,
127
+ backoff: new ExponentialBackoff({
128
+ initialDelay: 5,
129
+ maxDelay: 2300,
130
+ exponent: 2
131
+ })
132
+ });
133
+ /** Atomic one-shot recompute write: set data + metadata iff incoming version
134
+ * (ARGV[3]) is not older than the stored one. KEYS: data, meta. */
135
+ const WRITE_DATA_AND_META = `
136
+ local cur = redis.call('GET', KEYS[2])
137
+ if cur then
138
+ local ok, m = pcall(cjson.decode, cur)
139
+ if ok and type(m) == 'table' and m.version and tonumber(ARGV[3]) < tonumber(m.version) then
140
+ return 0
141
+ end
142
+ end
143
+ redis.call('SET', KEYS[1], ARGV[1], 'EX', tonumber(ARGV[4]))
144
+ redis.call('SET', KEYS[2], ARGV[2], 'EX', tonumber(ARGV[4]))
145
+ return 1`;
146
+ /** Data-only write (subscribe path): set data iff incoming version (ARGV[2]) is
147
+ * not older than the stored metadata's version. KEYS: data, meta. */
148
+ const WRITE_DATA_ONLY = `
149
+ local cur = redis.call('GET', KEYS[2])
150
+ if cur then
151
+ local ok, m = pcall(cjson.decode, cur)
152
+ if ok and type(m) == 'table' and m.version and tonumber(ARGV[2]) < tonumber(m.version) then
153
+ return 0
154
+ end
155
+ end
156
+ redis.call('SET', KEYS[1], ARGV[1], 'EX', tonumber(ARGV[3]))
157
+ return 1`;
158
+ /** Metadata-only replace (subscription recompute): set meta iff incoming version
159
+ * (ARGV[2]) is not older than the stored one. KEYS: meta. */
160
+ const REPLACE_META = `
161
+ local cur = redis.call('GET', KEYS[1])
162
+ if cur then
163
+ local ok, m = pcall(cjson.decode, cur)
164
+ if ok and type(m) == 'table' and m.version and tonumber(ARGV[2]) < tonumber(m.version) then
165
+ return 0
166
+ end
167
+ end
168
+ redis.call('SET', KEYS[1], ARGV[1], 'EX', tonumber(ARGV[3]))
169
+ return 1`;
170
+ /** Metadata-only horizon advance (still-fresh subscription): bump lastSnapshotTs
171
+ * ONLY, without recomputing. Applies ARGV[1] iff the new horizon (ARGV[2]) is
172
+ * strictly ahead of the stored one AND no newer computation (stored.version >
173
+ * ARGV[3], the version the caller based its advance on) has landed meanwhile —
174
+ * so an advance never reverts a fresh recompute. KEYS: meta. */
175
+ const ADVANCE_META = `
176
+ local cur = redis.call('GET', KEYS[1])
177
+ if not cur then return 0 end
178
+ local ok, m = pcall(cjson.decode, cur)
179
+ if not ok or type(m) ~= 'table' then return 0 end
180
+ if tonumber(ARGV[2]) <= tonumber(m.lastSnapshotTs) then return 0 end
181
+ if m.version and tonumber(m.version) > tonumber(ARGV[3]) then return 0 end
182
+ redis.call('SET', KEYS[1], ARGV[1], 'EX', tonumber(ARGV[4]))
183
+ return 1`;
184
+ var CacheLayer = class {
185
+ redis;
186
+ ttlSeconds;
187
+ /** Cached EVALSHA digests keyed by Lua source (see evalScript). */
188
+ scriptShaCache = /* @__PURE__ */ new Map();
189
+ constructor(redis, ttlSeconds = 3600) {
190
+ this.redis = redis;
191
+ this.ttlSeconds = ttlSeconds;
192
+ }
193
+ /**
194
+ * Run a Lua script via EVALSHA using the script's SHA1 (computed locally — the
195
+ * same digest Redis keys scripts by, so no SCRIPT LOAD round-trip is needed on
196
+ * the hot path). On NOSCRIPT (the script isn't in Redis's cache yet, e.g. first
197
+ * use or after a Redis restart/failover) load it explicitly via SCRIPT LOAD and
198
+ * re-run via EVALSHA — so the script ends up cached server-side and subsequent
199
+ * EVALSHA calls hit. The runtime client is always ioredis (see initCacheLayer).
200
+ */
201
+ async evalScript(src, keys, args) {
202
+ const redis = this.redis;
203
+ let sha = this.scriptShaCache.get(src);
204
+ if (!sha) {
205
+ sha = createHash("sha1").update(src).digest("hex");
206
+ this.scriptShaCache.set(src, sha);
207
+ }
208
+ return redisRetryPolicy.execute(async () => {
209
+ try {
210
+ return await redis.evalsha(sha, keys.length, ...keys, ...args);
211
+ } catch (err) {
212
+ if (err instanceof Error && err.message.includes("NOSCRIPT")) {
213
+ const loadedSha = await redis.call("SCRIPT", "LOAD", src);
214
+ this.scriptShaCache.set(src, loadedSha);
215
+ return await redis.evalsha(loadedSha, keys.length, ...keys, ...args);
216
+ }
217
+ throw err;
218
+ }
219
+ });
220
+ }
221
+ /** One-shot recompute: atomically write data + metadata (CAS on version). */
222
+ async setQueryCacheAndMetadataFor(cacheKey, data, metaData, ttlSeconds) {
223
+ const ttl = ttlSeconds ?? this.ttlSeconds;
224
+ const dataJson = supaliveStringify({
225
+ data,
226
+ version: metaData.version,
227
+ lastSnapshotTs: metaData.lastSnapshotTs
228
+ });
229
+ const metaJson = supaliveStringify(metaData);
230
+ await this.evalScript(WRITE_DATA_AND_META, [this.getQueryCacheDataKey(cacheKey), this.getQeruyCacheMetaKey(cacheKey)], [
231
+ dataJson,
232
+ metaJson,
233
+ metaData.version,
234
+ String(ttl)
235
+ ]);
236
+ }
237
+ /** Subscribe path: write the data blob only (CAS against the metadata's
238
+ * version). The metadata is written separately by the sub-worker; the
239
+ * `version` links the two so a divergent pair is detected on read. Takes the
240
+ * snapshot ts directly (rather than a pre-stringified version) so the entry
241
+ * can carry `lastSnapshotTs` as its own field; `version` is derived from it,
242
+ * preserving the `version === String(ts)` convention the CAS relies on. */
243
+ async setQueryCacheFor(cacheKey, data, lastSnapshotTs, ttlSeconds) {
244
+ const ttl = ttlSeconds ?? this.ttlSeconds;
245
+ const version = String(lastSnapshotTs);
246
+ const dataJson = supaliveStringify({
247
+ data,
248
+ version,
249
+ lastSnapshotTs
250
+ });
251
+ await this.evalScript(WRITE_DATA_ONLY, [this.getQueryCacheDataKey(cacheKey), this.getQeruyCacheMetaKey(cacheKey)], [
252
+ dataJson,
253
+ version,
254
+ String(ttl)
255
+ ]);
256
+ }
257
+ async getQueryCacheFor(cacheKey) {
258
+ const dataRaw = await redisRetryPolicy.execute(() => this.redis.get(this.getQueryCacheDataKey(cacheKey)));
259
+ if (!dataRaw) return null;
260
+ try {
261
+ const parsed = typeof dataRaw === "string" ? JSON.parse(dataRaw) : dataRaw;
262
+ return QueryCacheEntrySchema.parse(parsed);
263
+ } catch {
264
+ return null;
265
+ }
266
+ }
267
+ /** Subscription recompute: replace the whole metadata (CAS on version). */
268
+ async replaceQueryCacheMetadata(cacheKey, metaData, ttlSeconds) {
269
+ const ttl = ttlSeconds ?? this.ttlSeconds;
270
+ const metaJson = supaliveStringify(metaData);
271
+ await this.evalScript(REPLACE_META, [this.getQeruyCacheMetaKey(cacheKey)], [
272
+ metaJson,
273
+ metaData.version,
274
+ String(ttl)
275
+ ]);
276
+ }
277
+ /** Still-fresh subscription: advance `lastSnapshotTs` only, preserving the
278
+ * data's `version` + `readSet` (no recompute). Monotonic and non-clobbering
279
+ * — see ADVANCE_META. `metaData` carries the new horizon plus the version +
280
+ * readSet the caller read, which the script keeps unless a newer computation
281
+ * has since landed. */
282
+ async advanceQueryCacheMetadata(cacheKey, metaData, ttlSeconds) {
283
+ const ttl = ttlSeconds ?? this.ttlSeconds;
284
+ const metaJson = supaliveStringify(metaData);
285
+ await this.evalScript(ADVANCE_META, [this.getQeruyCacheMetaKey(cacheKey)], [
286
+ metaJson,
287
+ String(metaData.lastSnapshotTs),
288
+ metaData.version,
289
+ String(ttl)
290
+ ]);
291
+ }
292
+ async getQueryCacheMetaData(cacheKey) {
293
+ const dataRaw = await redisRetryPolicy.execute(() => this.redis.get(this.getQeruyCacheMetaKey(cacheKey)));
294
+ if (!dataRaw) return null;
295
+ try {
296
+ const parsed = typeof dataRaw === "string" ? JSON.parse(dataRaw) : dataRaw;
297
+ return QueryCacheMetadataSchema.parse(parsed);
298
+ } catch {
299
+ return null;
300
+ }
301
+ }
302
+ async persistRegistrationRecord(subId, record) {
303
+ const data = supaliveStringify(record);
304
+ await redisRetryPolicy.execute(() => this.redis.set(this.getRegistrationKey(subId), data));
305
+ }
306
+ async deleteRegistrationRecord(subId) {
307
+ await redisRetryPolicy.execute(() => this.redis.del(this.getRegistrationKey(subId)));
308
+ }
309
+ async deleteRegistrationsRecord(subIds) {
310
+ if (subIds.length === 0) return;
311
+ await redisRetryPolicy.execute(() => this.redis.del(...subIds.map((subId) => this.getRegistrationKey(subId))));
312
+ }
313
+ async fetchRegistrationRecordsBatch(subIds) {
314
+ const result = /* @__PURE__ */ new Map();
315
+ const CHUNK_SIZE = 100;
316
+ for (let i = 0; i < subIds.length; i += CHUNK_SIZE) {
317
+ const chunk = subIds.slice(i, i + CHUNK_SIZE);
318
+ const keys = chunk.map((id) => `sl:reg:${id}`);
319
+ try {
320
+ if (keys.length === 0) continue;
321
+ const rawValues = await redisRetryPolicy.execute(() => this.redis.mget(...keys));
322
+ for (let j = 0; j < chunk.length; j++) {
323
+ const subId = chunk[j];
324
+ const raw = rawValues[j];
325
+ const rawJson = typeof raw === "string" ? JSON.parse(raw) : raw;
326
+ if (!raw) result.set(subId, null);
327
+ else try {
328
+ const parsed = RegistrationRecordSchema.parse(rawJson);
329
+ result.set(subId, parsed);
330
+ } catch {
331
+ logger.error(`Failed to parse sl:reg:${subId} during batch recovery`);
332
+ result.set(subId, null);
333
+ }
334
+ }
335
+ } catch (err) {
336
+ logger.error(err, "Failed to fetch registration records batch");
337
+ for (const subId of chunk) result.set(subId, null);
338
+ }
339
+ }
340
+ return result;
341
+ }
342
+ async getCachedTableColumnTypes(key) {
343
+ const dataRaw = await redisRetryPolicy.execute(() => this.redis.get(key));
344
+ if (!dataRaw) return null;
345
+ try {
346
+ const parsed = typeof dataRaw === "string" ? JSON.parse(dataRaw) : dataRaw;
347
+ return CachedPgMetadataSchema.parse(parsed);
348
+ } catch {
349
+ return null;
350
+ }
351
+ }
352
+ async setCachedTableColumnsTypes(key, value) {
353
+ await redisRetryPolicy.execute(() => this.redis.set(key, supaliveStringify(value)));
354
+ }
355
+ /**
356
+ * Race participant: try to claim `lock:recompute:<subId>:<commitTs>`
357
+ * with `SET NX EX`. The single Redis round-trip both arbitrates and
358
+ * sets a TTL fallback in case the winner crashes mid-recompute.
359
+ */
360
+ async tryAcquireRecomputeLock(instanceId, subId, commitTs, ttl) {
361
+ const key = `lock:recompute:${subId}:${commitTs}`;
362
+ return redisRetryPolicy.execute(async () => {
363
+ try {
364
+ return await this.redis.set(key, instanceId, "EX", ttl, "NX") === "OK";
365
+ } catch (err) {
366
+ logger.error(err, `Failed to acquire recompute lock ${key}`);
367
+ return false;
368
+ }
369
+ });
370
+ }
371
+ getRegistrationKey(subId) {
372
+ return `sl:reg:${subId}`;
373
+ }
374
+ getQueryCacheDataKey(cacheKey) {
375
+ return `sl:cache:${cacheKey}`;
376
+ }
377
+ getQeruyCacheMetaKey(cacheKey) {
378
+ return `sl:meta:${cacheKey}`;
379
+ }
380
+ };
381
+ //#endregion
382
+ //#region src/db/init_db.ts
383
+ /**
384
+ * Current core runtime-bootstrap version. Bump this whenever core needs a new
385
+ * runtime database operation — a data seed, a backfill, or a schema change that
386
+ * must be applied directly at runtime rather than through the migration
387
+ * pipeline. Each increment adds a case to `Database.coreMigrationStatements`.
388
+ *
389
+ * The core *schema* (the `commit_logs`/`metadata` tables and the
390
+ * `global_commit_ts` sequence/counter) is NOT versioned here — it comes from
391
+ * the generated schema (`coreTablesSql` / `generateSchemaSql`), which must be
392
+ * applied to the database BEFORE `initCore` runs.
393
+ */
394
+ const CORE_VERSION = 1n;
395
+ /**
396
+ * Bring the core runtime state up to {@link CORE_VERSION}. Idempotent and safe
397
+ * to call from every process at startup: a lock-free version read short-circuits
398
+ * the common "already current" case, and the actual migration runs under a
399
+ * cross-process lock with a re-check so only one process applies it.
400
+ *
401
+ * Assumes the core schema already exists (apply the generated schema first);
402
+ * the `metadata` table is where the version and seed rows live.
403
+ */
404
+ async function initCore(db) {
405
+ if (await db.getCoreVersion() >= 1n) return;
406
+ await db.withCoreInitLock(async (ctx) => {
407
+ const from = await ctx.getVersion();
408
+ if (from >= 1n) return;
409
+ for (let v = from + 1n; v <= CORE_VERSION; v++) {
410
+ const statements = db.coreMigrationStatements(v);
411
+ logger$1.info(`Migrating core schema ${v - 1n} -> ${v} (${statements.length} statements)`);
412
+ for (const sql of statements) await ctx.exec(sql);
413
+ }
414
+ await ctx.setVersion(CORE_VERSION);
415
+ logger$1.info(`Core schema now at version ${CORE_VERSION}`);
416
+ });
417
+ }
418
+ /**
419
+ * Default and env override for the per-statement DB timeout. PG enforces
420
+ * this via `statement_timeout` (server cancels), MySQL via the per-query
421
+ * `timeout` option (driver issues KILL QUERY). Set 0 to disable.
422
+ */
423
+ const DB_QUERY_TIMEOUT_MS = Number(process.env.SUPALIVE_DB_QUERY_TIMEOUT_MS ?? 3e4);
424
+ async function initDatabase(options) {
425
+ const impl = createDatabase(options.DbConfig, options.workerId);
426
+ const replicaImpl = options.ReplicaDbConfig ? createDatabase(options.ReplicaDbConfig, options.workerId) : void 0;
427
+ const supaliveDb = SupaliveDb.create({
428
+ db: impl,
429
+ replica: replicaImpl
430
+ });
431
+ await initCore(impl);
432
+ await bootstrapDbTypes(supaliveDb, options.CacheLayer, options.forceBootstrapReload ?? false);
433
+ return supaliveDb;
434
+ }
435
+ function createDatabase(config, workerId) {
436
+ const bindings = { "db": config.type };
437
+ if (workerId) bindings["worker"] = workerId;
438
+ if (config.type === "postgres") return new PgDatabase(logger$1.child(bindings), {
439
+ ...config,
440
+ connectionString: config.connectionString,
441
+ min: config.minConnections ?? 0,
442
+ max: config.maxConnections ?? 20,
443
+ connectionTimeoutMillis: config.connectionTimeoutMillis ?? 15e3,
444
+ idleTimeoutMillis: config.idleTimeoutMillis ?? 3e4,
445
+ statement_timeout: DB_QUERY_TIMEOUT_MS > 0 ? DB_QUERY_TIMEOUT_MS : void 0
446
+ });
447
+ else return new MySqlDatabase(logger$1.child(bindings), {
448
+ uri: config.connectionString,
449
+ supportBigNumbers: true,
450
+ connectionLimit: config.maxConnections ?? 20,
451
+ connectTimeout: config.connectionTimeoutMillis ?? 15e3,
452
+ idleTimeout: config.idleTimeoutMillis ?? 3e4,
453
+ queueLimit: config.queueLimit ?? 0,
454
+ timezone: "Z",
455
+ queryTimeoutMs: DB_QUERY_TIMEOUT_MS
456
+ });
457
+ }
458
+ /**
459
+ * Eagerly load column types into the apply-path cache at server start.
460
+ * In production, the result is cached in Redis under
461
+ * `supalive:schema:types:${app_migration}` (the applied-migration marker from
462
+ * the `metadata` table, via {@link Database.getAppMigration}) so other
463
+ * instances starting cold can skip the pg_attribute round-trip — and a schema
464
+ * migration bumps that marker, so the key changes and stale types aren't
465
+ * reused. In any
466
+ * other env we go straight to PG every time so local schema iteration
467
+ * is never blocked by stale Redis state. Migrations should publish a
468
+ * `schema:reload` message (see {@link publishSchemaReload}) after
469
+ * applying DDL — every instance picks that up and re-bootstraps.
470
+ */
471
+ async function bootstrapDbTypes(db, cache, forceReload = false) {
472
+ if (db.impl.dbType === "mysql") return;
473
+ const inProd = process.env.NODE_ENV === "production";
474
+ const key = `supalive:schema:types:${await db.impl.getAppMigration()}`;
475
+ try {
476
+ await db.impl.bootstrapColumnTypes({
477
+ cacheKey: key,
478
+ redis: inProd && !forceReload ? cache : void 0
479
+ });
480
+ logger$1.info({ columnsTypes: db.impl.columnTypes }, `Bootstrapped db column types (redis=${inProd}, key=${key})`);
481
+ } catch (err) {
482
+ logger$1.error(err, "Failed to bootstrap db column types");
483
+ throw err;
484
+ }
485
+ }
486
+ function initCacheLayer(config) {
487
+ const redis = new Redis(config.upstash.redisUrl, {
488
+ connectTimeout: config.connectionTimeout ?? 15e3,
489
+ maxRetriesPerRequest: config.commandMaxRetries ?? 1,
490
+ keepAlive: 1e4
491
+ });
492
+ const redisSub = config.createSubClient ? new Redis(config.upstash.redisUrl, {
493
+ connectTimeout: config.connectionTimeout ?? 15e3,
494
+ maxRetriesPerRequest: config.commandSubMaxRetries ?? 5,
495
+ disableClientInfo: true,
496
+ retryStrategy: (times) => {
497
+ if (times > 5) return null;
498
+ return Math.min(times * 200, 2e3);
499
+ }
500
+ }) : void 0;
501
+ return {
502
+ cacheLayer: new CacheLayer(redis, config.cacheTtlSeconds ?? 3600),
503
+ redisSubClient: redisSub
504
+ };
505
+ }
506
+ //#endregion
507
+ //#region src/server/sub-worker.ts
508
+ var SubscriptionWorker = class {
509
+ id;
510
+ subscriptions = /* @__PURE__ */ new Map();
511
+ cache;
512
+ db;
513
+ retentionWatermark = new RetentionWatermark();
514
+ logger;
515
+ constructor(id, cache, db) {
516
+ this.id = id;
517
+ this.cache = cache;
518
+ this.db = db;
519
+ this.logger = logger$1.child({
520
+ name: "SUBS_WRK",
521
+ wid: id
522
+ }, { level: logLevelOf("SUBS_MANAGER_SERVER_LOG_LEVEL") });
523
+ }
524
+ size() {
525
+ return this.subscriptions.size;
526
+ }
527
+ has(subId) {
528
+ return this.subscriptions.has(subId);
529
+ }
530
+ /**
531
+ * Lowest `lastSnapshotTs` across this worker's subscriptions, or `null`
532
+ * when there are none. Used by the dispatcher to derive a safe prune watermark.
533
+ */
534
+ minSubscriptionTs() {
535
+ let min = null;
536
+ for (const sub of this.subscriptions.values()) if (min === null || sub.lastSnapshotTs < min) min = sub.lastSnapshotTs;
537
+ return min;
538
+ }
539
+ /**
540
+ * Seed/repair the persisted retained watermark from the actual oldest retained
541
+ * commit log (`min(ts)`), then load it into memory and broadcast it. Called
542
+ * once at sub-manager startup — the `min(ts)` scan is off the hot path, and
543
+ * it corrects `metadata.min_retained_ts` even for a DB that was pruned before
544
+ * this feature existed. Returns the resulting watermark so the manager can
545
+ * broadcast it to the other workers.
546
+ */
547
+ async refreshRetentionWatermark() {
548
+ const real = await this.db.getMinCommitLogTs() ?? 0n;
549
+ await this.db.updateMinSnapshotTimestamp(real);
550
+ const persisted = await this.db.getMinSnapshotTimestamp();
551
+ this.retentionWatermark.set(persisted);
552
+ await this.publishRetentionWatermark(persisted);
553
+ return this.retentionWatermark.get();
554
+ }
555
+ /** Adopt a watermark broadcast by the manager (in-process from the pruning
556
+ * worker). Monotonic, so an out-of-order message can't lower it. */
557
+ setRetentionWatermark(ts) {
558
+ this.retentionWatermark.set(ts);
559
+ }
560
+ /**
561
+ * Prune commit logs older than `minRetainedTs`. Advertises the new watermark FIRST —
562
+ * persist it, bump our in-memory copy, and broadcast it over Redis — and only
563
+ * then deletes, so no reader ever trusts a cache entry whose freshness scan
564
+ * would include rows we're about to remove. Returns rows deleted.
565
+ */
566
+ async prune(minRetainedTs) {
567
+ await this.db.updateMinSnapshotTimestamp(minRetainedTs);
568
+ this.retentionWatermark.set(minRetainedTs);
569
+ await this.publishRetentionWatermark(minRetainedTs);
570
+ return await this.db.pruneCommitLogsBefore(minRetainedTs);
571
+ }
572
+ /** Broadcast the watermark to app-server instances over Redis pub/sub. Workers in
573
+ * this process get it in-process via {@link setRetentionWatermark}. */
574
+ async publishRetentionWatermark(ts) {
575
+ try {
576
+ await this.cache.redis.publish(RETENTION_WATERMARK_CHANNEL, ts.toString());
577
+ } catch (err) {
578
+ this.logger.error(err, "failed to publish commit-log watermark");
579
+ }
580
+ }
581
+ async register(params) {
582
+ const { subId, cacheKey, queryName, args, queryIdentity, instanceName, lastSnapshotTs } = params;
583
+ this.logger.info(`registering subscription: ${subId} from ${instanceName}`);
584
+ const existing = this.subscriptions.get(subId);
585
+ if (existing && !existing.timeout) {
586
+ existing.instances.add(instanceName);
587
+ return {
588
+ cacheKey,
589
+ recompute: false,
590
+ readSet: existing.readSet
591
+ };
592
+ }
593
+ if (existing?.timeout) {
594
+ this.logger.info(`reactivating subscription: ${subId} from: ${instanceName}`);
595
+ clearTimeout(existing.timeout);
596
+ existing.timeout = null;
597
+ }
598
+ const metadata = await this.cache.getQueryCacheMetaData(cacheKey);
599
+ const freshness = await evaluateCacheFreshness(this.db, metadata, lastSnapshotTs, this.retentionWatermark.get());
600
+ let needsRecompute = !freshness.fresh;
601
+ const readSet = freshness.readSet;
602
+ const rawReadSet = freshness.rawReadSet;
603
+ if (!needsRecompute && metadata && lastSnapshotTs > metadata.lastSnapshotTs) this.cache.advanceQueryCacheMetadata(cacheKey, {
604
+ lastSnapshotTs,
605
+ version: metadata.version,
606
+ readSet
607
+ }).catch((err) => this.logger.error(err, `Failed to advance cache metadata for ${cacheKey}`));
608
+ let newSubscription = {
609
+ subId,
610
+ queryName,
611
+ args,
612
+ cacheKey,
613
+ lastSnapshotTs,
614
+ readSet,
615
+ rawReadSet,
616
+ instances: /* @__PURE__ */ new Set([instanceName]),
617
+ timeout: null
618
+ };
619
+ const createdWhileChecking = this.subscriptions.get(subId);
620
+ if (createdWhileChecking) {
621
+ if (createdWhileChecking.timeout) {
622
+ clearTimeout(createdWhileChecking.timeout);
623
+ createdWhileChecking.timeout = null;
624
+ }
625
+ if (createdWhileChecking.lastSnapshotTs > lastSnapshotTs) {
626
+ newSubscription = {
627
+ ...newSubscription,
628
+ ...createdWhileChecking,
629
+ instances: /* @__PURE__ */ new Set([...createdWhileChecking.instances, instanceName])
630
+ };
631
+ needsRecompute = false;
632
+ }
633
+ }
634
+ this.subscriptions.set(subId, newSubscription);
635
+ this.persistRegistration(subId, {
636
+ queryName,
637
+ args,
638
+ queryIdentity,
639
+ cacheKey
640
+ });
641
+ return {
642
+ cacheKey,
643
+ recompute: needsRecompute,
644
+ readSet,
645
+ version: metadata?.version
646
+ };
647
+ }
648
+ async updateReadSet(params) {
649
+ const { cacheKey, subId, instanceName, lastSnapshotTs, readSet } = params;
650
+ this.logger.info(`updating subscription readSet: ${subId} from ${instanceName}`);
651
+ const existing = this.subscriptions.get(subId);
652
+ if (!existing) return { tracked: false };
653
+ if (lastSnapshotTs < existing.lastSnapshotTs) return { tracked: true };
654
+ const rawReadSet = mapReadSetToRaw(readSet);
655
+ this.subscriptions.set(subId, {
656
+ ...existing,
657
+ readSet,
658
+ rawReadSet,
659
+ lastSnapshotTs
660
+ });
661
+ await this.cache.replaceQueryCacheMetadata(cacheKey, {
662
+ lastSnapshotTs,
663
+ version: String(lastSnapshotTs),
664
+ readSet
665
+ });
666
+ return { tracked: true };
667
+ }
668
+ async unregister(params, jitter = 0) {
669
+ const { subId, instanceName } = params;
670
+ this.logger.info(`unregistering subscription: ${subId} from: ${instanceName}`);
671
+ const sub = this.subscriptions.get(subId);
672
+ if (!sub) return { ok: true };
673
+ if (sub.timeout) clearTimeout(sub.timeout);
674
+ if (sub.instances.size > 1) {
675
+ sub.instances.delete(instanceName);
676
+ return { ok: true };
677
+ }
678
+ this.logger.debug(`scheduling unregister subscription: ${subId} from: ${instanceName} in 15s`);
679
+ sub.timeout = setTimeout(async () => {
680
+ sub.instances.delete(instanceName);
681
+ if (sub.instances.size === 0) {
682
+ this.logger.info(`unregistered subscription: ${subId} from: ${instanceName}`);
683
+ this.subscriptions.delete(subId);
684
+ await this.deleteRegistration(subId);
685
+ }
686
+ }, 15e3 + jitter);
687
+ return { ok: true };
688
+ }
689
+ async unregisters(params) {
690
+ this.logger.info({ subIds: params }, `scheduling unregistering subscriptions`);
691
+ for (const param of params) this.unregister(param, Math.floor(Math.random() * 100));
692
+ return params.map(() => ({ ok: true }));
693
+ }
694
+ /**
695
+ * Synchronous, CPU-bound. Scans this worker's slice of subscriptions and
696
+ * returns the ones affected by the writeSet.
697
+ */
698
+ invalidate(rawWriteSet) {
699
+ const affected = [];
700
+ for (const [, sub] of this.subscriptions) {
701
+ if (sub.instances.size === 0) continue;
702
+ if (!checkWriteSetAffectsReadSet(rawWriteSet, sub.rawReadSet)) continue;
703
+ affected.push({
704
+ subId: sub.subId,
705
+ queryName: sub.queryName,
706
+ args: sub.args,
707
+ cacheKey: sub.cacheKey,
708
+ notifyInstances: Array.from(sub.instances)
709
+ });
710
+ }
711
+ return affected;
712
+ }
713
+ async persistRegistration(subId, record) {
714
+ try {
715
+ await this.cache.persistRegistrationRecord(subId, record);
716
+ } catch (err) {
717
+ this.logger.error(err, `[worker ${this.id}] Failed to persist registration for ${subId}`);
718
+ }
719
+ }
720
+ async deleteRegistration(subId) {
721
+ try {
722
+ await this.cache.deleteRegistrationRecord(subId);
723
+ } catch (err) {
724
+ this.logger.error(err, `[worker ${this.id}] Failed to delete registration for ${subId}`);
725
+ }
726
+ }
727
+ };
728
+ //#endregion
729
+ //#region src/server/sub-worker-dispatch.ts
730
+ /**
731
+ * Dispatch a single worker operation against an already-constructed
732
+ * {@link SubscriptionWorker} + {@link Database}. Shared by:
733
+ *
734
+ * • the worker-thread entry (`subscription-manager-worker-entry.ts`), where
735
+ * the payload just crossed a `postMessage` boundary and is untrusted, so it
736
+ * is Zod-parsed against the matching schema; and
737
+ * • the in-process {@link InlineWorkerHandle}, where the payload is the
738
+ * manager's own already-typed object (no thread hop, no structured-clone) —
739
+ * `trusted: true` skips the redundant parse so the call goes straight
740
+ * through to the worker method.
741
+ *
742
+ * `MsgType.Init` is NOT handled here: it constructs the worker/db, which is
743
+ * bootstrap logic owned by each caller (the thread entry builds its own pool;
744
+ * the inline handle is handed live instances).
745
+ */
746
+ async function dispatchWorkerMessage(worker, db, type, payload, opts) {
747
+ const trusted = opts?.trusted ?? false;
748
+ switch (type) {
749
+ case 2: {
750
+ const params = trusted ? payload : RegisterSubscriptionParamsSchema.parse(payload);
751
+ return worker.register(params);
752
+ }
753
+ case 3: {
754
+ const params = trusted ? payload : RegisterSubscriptionBatchParamsSchema.parse(payload);
755
+ return Promise.all(params.map((p) => worker.register(p)));
756
+ }
757
+ case 4: {
758
+ const params = trusted ? payload : UpdateSubscriptionReadSetParamsSchema.parse(payload);
759
+ return worker.updateReadSet(params);
760
+ }
761
+ case 5: {
762
+ const params = trusted ? payload : UnregisterSubscriptionParamsSchema.parse(payload);
763
+ return worker.unregister(params);
764
+ }
765
+ case 6: {
766
+ const params = trusted ? payload : UnregisterSubscriptionsParamsSchema.parse(payload);
767
+ return worker.unregisters(params);
768
+ }
769
+ case 7: {
770
+ const { writeSet } = trusted ? payload : InvalidateWritesetParamsSchema.parse(payload);
771
+ return worker.invalidate(mapWriteSetToRaw(writeSet));
772
+ }
773
+ case 8: return worker.minSubscriptionTs();
774
+ case 9: return db.getLatestSnapshotTimestamp();
775
+ case 10: return worker.prune(payload);
776
+ case 12: return worker.refreshRetentionWatermark();
777
+ case 13:
778
+ worker.setRetentionWatermark(payload);
779
+ return null;
780
+ case 11:
781
+ try {
782
+ await db.close();
783
+ } catch (_) {}
784
+ return null;
785
+ case 1: throw new Error("MsgType.Init must be handled by the caller (worker bootstrap)");
786
+ default: throw new Error(`Unknown MsgType: ${type}`);
787
+ }
788
+ }
789
+ //#endregion
790
+ export { RetentionWatermark as S, UnregisterSubscriptionResultSchema as _, bootstrapDbTypes as a, UpdateSubscriptionReadSetResultSchema as b, initCore as c, InvalidateWritesetParamsSchema as d, InvalidateWritesetResultSchema as f, UnregisterSubscriptionParamsSchema as g, RegisterSubscriptionResultSchema as h, DB_QUERY_TIMEOUT_MS as i, initDatabase as l, RegisterSubscriptionParamsSchema as m, SubscriptionWorker as n, createDatabase as o, RegisterSubscriptionBatchResultSchema as p, CORE_VERSION as r, initCacheLayer as s, dispatchWorkerMessage as t, CacheLayer as u, UnregisterSubscriptionsResultSchema as v, RETENTION_WATERMARK_CHANNEL as x, UpdateSubscriptionReadSetParamsSchema as y };
791
+
792
+ //# sourceMappingURL=sub-worker-dispatch-Ci_z8w10.js.map