@rocicorp/zero 1.12.0 → 1.13.0-canary.0

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.
Files changed (153) hide show
  1. package/out/shared/src/bigint-json.d.ts +6 -0
  2. package/out/shared/src/bigint-json.d.ts.map +1 -1
  3. package/out/shared/src/bigint-json.js +14 -1
  4. package/out/shared/src/bigint-json.js.map +1 -1
  5. package/out/shared/src/objects.d.ts +7 -0
  6. package/out/shared/src/objects.d.ts.map +1 -1
  7. package/out/shared/src/objects.js +10 -1
  8. package/out/shared/src/objects.js.map +1 -1
  9. package/out/shared/src/resolver.d.ts.map +1 -1
  10. package/out/shared/src/resolver.js +12 -1
  11. package/out/shared/src/resolver.js.map +1 -1
  12. package/out/zero/package.js +1 -1
  13. package/out/zero/package.js.map +1 -1
  14. package/out/zero-cache/src/auth/write-authorizer.js +4 -3
  15. package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
  16. package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
  17. package/out/zero-cache/src/config/normalize.js +3 -23
  18. package/out/zero-cache/src/config/normalize.js.map +1 -1
  19. package/out/zero-cache/src/config/zero-config.d.ts +1 -27
  20. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  21. package/out/zero-cache/src/config/zero-config.js +19 -47
  22. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  23. package/out/zero-cache/src/custom/fetch.js +17 -2
  24. package/out/zero-cache/src/custom/fetch.js.map +1 -1
  25. package/out/zero-cache/src/db/create.js +45 -1
  26. package/out/zero-cache/src/db/create.js.map +1 -1
  27. package/out/zero-cache/src/db/lite-tables.d.ts.map +1 -1
  28. package/out/zero-cache/src/db/lite-tables.js +4 -3
  29. package/out/zero-cache/src/db/lite-tables.js.map +1 -1
  30. package/out/zero-cache/src/db/pg-copy-binary.js +61 -25
  31. package/out/zero-cache/src/db/pg-copy-binary.js.map +1 -1
  32. package/out/zero-cache/src/db/pg-to-lite.d.ts +17 -0
  33. package/out/zero-cache/src/db/pg-to-lite.d.ts.map +1 -1
  34. package/out/zero-cache/src/db/pg-to-lite.js +84 -1
  35. package/out/zero-cache/src/db/pg-to-lite.js.map +1 -1
  36. package/out/zero-cache/src/server/change-streamer.js +21 -30
  37. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  38. package/out/zero-cache/src/server/main.js +0 -1
  39. package/out/zero-cache/src/server/main.js.map +1 -1
  40. package/out/zero-cache/src/server/replicator.js +1 -1
  41. package/out/zero-cache/src/server/replicator.js.map +1 -1
  42. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +6 -5
  43. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
  44. package/out/zero-cache/src/services/change-source/pg/change-source.js +49 -11
  45. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  46. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +64 -21
  47. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  48. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +11 -6
  49. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  50. package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts +13 -1
  51. package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts.map +1 -1
  52. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +16 -3
  53. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js.map +1 -1
  54. package/out/zero-cache/src/services/change-source/pg/schema/init.js +59 -3
  55. package/out/zero-cache/src/services/change-source/pg/schema/init.js.map +1 -1
  56. package/out/zero-cache/src/services/change-source/pg/schema/published.d.ts +1 -0
  57. package/out/zero-cache/src/services/change-source/pg/schema/published.d.ts.map +1 -1
  58. package/out/zero-cache/src/services/change-source/pg/schema/published.js +56 -12
  59. package/out/zero-cache/src/services/change-source/pg/schema/published.js.map +1 -1
  60. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts +1 -0
  61. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
  62. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +35 -12
  63. package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
  64. package/out/zero-cache/src/services/change-source/pg/schema/validation.d.ts +6 -1
  65. package/out/zero-cache/src/services/change-source/pg/schema/validation.d.ts.map +1 -1
  66. package/out/zero-cache/src/services/change-source/pg/schema/validation.js +25 -1
  67. package/out/zero-cache/src/services/change-source/pg/schema/validation.js.map +1 -1
  68. package/out/zero-cache/src/services/change-source/protocol/backfill-progress.js +6 -5
  69. package/out/zero-cache/src/services/change-source/protocol/backfill-progress.js.map +1 -1
  70. package/out/zero-cache/src/services/change-streamer/backfill-state.js +7 -6
  71. package/out/zero-cache/src/services/change-streamer/backfill-state.js.map +1 -1
  72. package/out/zero-cache/src/services/change-streamer/change-log-codec.js +2 -2
  73. package/out/zero-cache/src/services/change-streamer/change-log-codec.js.map +1 -1
  74. package/out/zero-cache/src/services/change-streamer/change-log-initializer.js +16 -199
  75. package/out/zero-cache/src/services/change-streamer/change-log-initializer.js.map +1 -1
  76. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +76 -127
  77. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  78. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js +1 -1
  79. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js.map +1 -1
  80. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.js.map +1 -1
  81. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-purge-scheduler.js +1 -1
  82. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.js +15 -49
  83. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.js.map +1 -1
  84. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-reader.js +8 -2
  85. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-reader.js.map +1 -1
  86. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js +20 -36
  87. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js.map +1 -1
  88. package/out/zero-cache/src/services/change-streamer/storer.js +32 -20
  89. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  90. package/out/zero-cache/src/services/mutagen/mutagen.js +4 -2
  91. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  92. package/out/zero-cache/src/services/replicator/change-log-cookies.d.ts +0 -16
  93. package/out/zero-cache/src/services/replicator/change-log-cookies.d.ts.map +1 -1
  94. package/out/zero-cache/src/services/replicator/change-log-cookies.js +3 -95
  95. package/out/zero-cache/src/services/replicator/change-log-cookies.js.map +1 -1
  96. package/out/zero-cache/src/services/replicator/change-log-db.js +2 -2
  97. package/out/zero-cache/src/services/replicator/change-log-db.js.map +1 -1
  98. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js +1 -30
  99. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js.map +1 -1
  100. package/out/zero-cache/src/services/replicator/change-processor.js +82 -9
  101. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  102. package/out/zero-cache/src/services/replicator/incremental-sync.js +7 -1
  103. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  104. package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts +1 -0
  105. package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts.map +1 -1
  106. package/out/zero-cache/src/services/replicator/reporter/report-schema.js +4 -1
  107. package/out/zero-cache/src/services/replicator/reporter/report-schema.js.map +1 -1
  108. package/out/zero-cache/src/services/replicator/schema/change-log.d.ts.map +1 -1
  109. package/out/zero-cache/src/services/replicator/schema/change-log.js +2 -1
  110. package/out/zero-cache/src/services/replicator/schema/change-log.js.map +1 -1
  111. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js +6 -102
  112. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js.map +1 -1
  113. package/out/zero-cache/src/services/statz.d.ts.map +1 -1
  114. package/out/zero-cache/src/services/statz.js +1 -1
  115. package/out/zero-cache/src/services/statz.js.map +1 -1
  116. package/out/zero-cache/src/services/view-syncer/schema/init.js +3 -0
  117. package/out/zero-cache/src/services/view-syncer/schema/init.js.map +1 -1
  118. package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.js +47 -3
  119. package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.js.map +1 -1
  120. package/out/zero-cache/src/services/view-syncer/view-syncer.js +51 -6
  121. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  122. package/out/zero-cache/src/types/lite.d.ts +7 -0
  123. package/out/zero-cache/src/types/lite.d.ts.map +1 -1
  124. package/out/zero-cache/src/types/lite.js +12 -2
  125. package/out/zero-cache/src/types/lite.js.map +1 -1
  126. package/out/zero-cache/src/types/pg-types.d.ts +2 -1
  127. package/out/zero-cache/src/types/pg-types.d.ts.map +1 -1
  128. package/out/zero-cache/src/types/pg-types.js +2 -1
  129. package/out/zero-cache/src/types/pg-types.js.map +1 -1
  130. package/out/zero-cache/src/types/pg.d.ts +20 -0
  131. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  132. package/out/zero-cache/src/types/pg.js +64 -27
  133. package/out/zero-cache/src/types/pg.js.map +1 -1
  134. package/out/zero-cache/src/types/streams.d.ts +2 -0
  135. package/out/zero-cache/src/types/streams.d.ts.map +1 -1
  136. package/out/zero-cache/src/types/streams.js +2 -1
  137. package/out/zero-cache/src/types/streams.js.map +1 -1
  138. package/out/zero-cache/src/workers/replicator.js +7 -6
  139. package/out/zero-cache/src/workers/replicator.js.map +1 -1
  140. package/out/zero-cache/src/workers/syncer.js +18 -1
  141. package/out/zero-cache/src/workers/syncer.js.map +1 -1
  142. package/out/zero-client/src/client/version.js +1 -1
  143. package/out/zqlite/src/db.js +4 -1
  144. package/out/zqlite/src/db.js.map +1 -1
  145. package/package.json +1 -1
  146. package/out/zero-cache/src/services/change-streamer/change-log-compare-digest.js +0 -67
  147. package/out/zero-cache/src/services/change-streamer/change-log-compare-digest.js.map +0 -1
  148. package/out/zero-cache/src/services/change-streamer/change-log-transaction-hash.js +0 -39
  149. package/out/zero-cache/src/services/change-streamer/change-log-transaction-hash.js.map +0 -1
  150. package/out/zero-cache/src/services/change-streamer/shard-sampling.js +0 -19
  151. package/out/zero-cache/src/services/change-streamer/shard-sampling.js.map +0 -1
  152. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-comparator.js +0 -417
  153. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-comparator.js.map +0 -1
@@ -14,14 +14,13 @@ import { publishReplicationError, replicationStatusError } from "../replicator/r
14
14
  import "./change-streamer.js";
15
15
  import { BackfillStateTracker } from "./backfill-state-tracker.js";
16
16
  import { serializeChangeStreamDataWithChange } from "./change-log-codec.js";
17
- import { SQLiteChangeLogReader } from "./sqlite-change-log-reader.js";
18
17
  import { ChangeLogInitializer, replicaInitializationSource } from "./change-log-initializer.js";
19
18
  import { DrainTracker } from "./drain-tracker.js";
20
19
  import { Forwarder } from "./forwarder.js";
21
20
  import { RestoreAdmission } from "./restore-admission.js";
22
21
  import { SnapshotReservations } from "./snapshot-reservations.js";
23
22
  import { SQLiteChangeLogCatchup } from "./sqlite-change-log-catchup.js";
24
- import { SQLiteChangeLogComparator } from "./sqlite-change-log-comparator.js";
23
+ import { SQLiteChangeLogReader } from "./sqlite-change-log-reader.js";
25
24
  import { SQLiteChangeLogPurgeScheduler } from "./sqlite-change-log-purge-scheduler.js";
26
25
  import { SQLiteChangeLogReadRouter, inspectSQLiteChangeLog } from "./sqlite-change-log-read-router.js";
27
26
  import { SQLiteChangeLogWriter } from "./sqlite-change-log-writer.js";
@@ -200,7 +199,6 @@ var ChangeStreamerImpl = class {
200
199
  #sqliteCatchupOptions;
201
200
  #changeLogWriter;
202
201
  #purgeScheduler;
203
- #comparator;
204
202
  #acker;
205
203
  #backupMonitor;
206
204
  #initializer;
@@ -214,8 +212,7 @@ var ChangeStreamerImpl = class {
214
212
  #changeCounter = getOrCreateCounter("replication", "changes", "Count of replicated changes (DML or DDL statements)");
215
213
  #transactionForwardDuration = getOrCreateLatencyHistogram("replication", "transaction_forward_duration", "Time from receiving a transaction's `begin` to forwarding its `commit`, i.e. the change-streamer half of forward-to-subscriber latency.");
216
214
  #catchupRoutes = getOrCreateCounter("replication", "sqlite_change_log.catchup_routes", "Catchup subscriptions by selected source and low-cardinality reason.");
217
- #reservationDemotions = getOrCreateCounter("replication", "sqlite_change_log.reservation_demotions", "Snapshot reservations demoted from SQLite to PG because the change log could not cover the backup being restored. With PG retired these followers have no fallback, so this is the rate at which one would instead have to wait for a later backup.");
218
- #reservationConfirmDelays = getOrCreateCounter("replication", "sqlite_change_log.reservation_confirm_delays", "Snapshot reservations whose confirmation was deferred because the selected source's change-log minimum was later than the backup watermark. Counted once per reservation, by that source.");
215
+ #reservationConfirmDelays = getOrCreateCounter("replication", "sqlite_change_log.reservation_confirm_delays", "Snapshot reservations whose confirmation was deferred because the change log's minimum was later than the backup watermark. Counted once per reservation.");
219
216
  #latestLagReportCommitTimeMs = 0;
220
217
  #lagReports = resolver();
221
218
  #backupWatermark;
@@ -250,26 +247,21 @@ var ChangeStreamerImpl = class {
250
247
  this.#source = source;
251
248
  this.#pgChangeLogBehindSlot = opts.pgChangeLogBehindSlot;
252
249
  this.#storer = changeDBProvider ? new Storer(lc, shard, taskID, discoveryAddress, discoveryProtocol, changeDBProvider, subscriptionState, (consumed) => this.#acker.trackPgChangeLog(consumed[2].watermark), (err) => this.stop(err), opts) : null;
253
- if (!this.#pgChangeLogEnabled) {
254
- assert(opts.sqliteChangeLogWriter && opts.sqliteChangeLogPurge && opts.sqliteCatchup && opts.sqliteChangeLogServe?.readPercent === 100 && opts.sqliteChangeLogServe.coldReadPercent === 100, "disabling the PG change log requires SQLite serving at 100 percent and a v5 backup");
255
- assert(opts.sqliteChangeLogCompare === void 0, "SQLite change-log comparison requires the PG change log");
256
- }
250
+ if (this.#pgChangeLogEnabled) assert(!opts.sqliteChangeLogWriter && !opts.sqliteChangeLogPurge && !opts.sqliteCatchup, "the SQLite change log is only used when the PG change log is disabled");
251
+ else assert(opts.sqliteChangeLogWriter && opts.sqliteChangeLogPurge && opts.sqliteCatchup, "disabling the PG change log requires the SQLite change log");
257
252
  assert(!(this.#pgChangeLogEnabled && opts.pgChangeLogBehindSlot && !backupConfig?.replicaFile), "the PG change log is behind the replication slot, but there is no restored replica to re-initialize it from");
258
253
  this.#forwarder = new Forwarder(lc, {
259
254
  flowControlConsensusTimeoutProportion: opts.flowControlConsensusTimeoutProportion,
260
255
  flowControlSlowSubscriberGracePeriodMs: opts.flowControlSlowSubscriberGracePeriodMs
261
256
  });
262
- const serveOptions = opts.sqliteChangeLogServe;
263
257
  const writerOptions = opts.sqliteChangeLogWriter;
258
+ const purgeOptions = opts.sqliteChangeLogPurge;
264
259
  const catchupOptions = opts.sqliteCatchup;
265
- this.#readRouter = serveOptions && writerOptions && catchupOptions ? new SQLiteChangeLogReadRouter({
266
- shard,
267
- readPercent: serveOptions.readPercent,
268
- coldReadPercent: serveOptions.coldReadPercent,
269
- retentionMs: serveOptions.retentionMs,
270
- failureCooldownMs: serveOptions.failureCooldownMs,
271
- now: serveOptions.now,
272
- inspect: serveOptions.inspect ?? (() => inspectSQLiteChangeLog(lc, catchupOptions.changeLogFile, writerOptions.identity))
260
+ this.#readRouter = writerOptions && purgeOptions && catchupOptions ? new SQLiteChangeLogReadRouter({
261
+ retentionMs: purgeOptions.retentionMs,
262
+ failureCooldownMs: catchupOptions.failureCooldownMs,
263
+ now: catchupOptions.now,
264
+ inspect: catchupOptions.inspect ?? (() => inspectSQLiteChangeLog(lc, catchupOptions.changeLogFile, writerOptions.identity))
273
265
  }) : void 0;
274
266
  this.#reservations = backupConfig ? new SnapshotReservations(lc, backupConfig, {
275
267
  onClose: (taskID) => {
@@ -294,40 +286,28 @@ var ChangeStreamerImpl = class {
294
286
  onDisabled: () => {
295
287
  this.#readRouter?.trip(true);
296
288
  this.#closeSQLiteCatchup();
297
- this.#comparator?.stop();
298
289
  },
299
- onRebuilt: () => {
300
- this.#closeSQLiteCatchup();
301
- this.#comparator?.invalidate();
302
- }
290
+ onRebuilt: () => this.#closeSQLiteCatchup()
303
291
  }) : void 0;
304
292
  this.#purgeScheduler = opts.sqliteChangeLogPurge && this.#changeLogWriter ? new SQLiteChangeLogPurgeScheduler(lc, () => this.#changeLogWriter?.connection, () => this.#forwarder.getAcks(), opts.sqliteChangeLogPurge) : void 0;
305
293
  this.#acker = new UpstreamAcker({
306
294
  trackPgChangeLog: this.#pgChangeLogEnabled,
307
295
  trackBackup: backupConfig ? backupConfig.litestreamVersion === "v5" : !this.#pgChangeLogEnabled
308
296
  });
309
- const replicaFileForInitialization = opts.sqliteChangeLogCompare?.replicaFile ?? (!this.#pgChangeLogEnabled ? opts.sqliteChangeLogWriter?.replicaFile : void 0);
297
+ const replicaFileForInitialization = opts.sqliteChangeLogWriter?.replicaFile;
310
298
  const replicaFileForReinitialization = this.#pgChangeLogEnabled && opts.pgChangeLogBehindSlot ? backupConfig?.replicaFile : void 0;
311
299
  const replicaFile = replicaFileForInitialization ?? replicaFileForReinitialization;
312
300
  const replicaSource = replicaFile ? replicaInitializationSource(lc, replicaFile) : void 0;
313
- this.#initializer = new ChangeLogInitializer(lc, {
314
- initFromPgChangeLog: this.#pgChangeLogEnabled,
315
- initFromReplica: replicaFileForInitialization !== void 0
316
- }, {
301
+ this.#initializer = new ChangeLogInitializer(lc, this.#pgChangeLogEnabled, {
317
302
  pgChangeLog: () => must(this.#storer).getStartStreamInitializationParameters(),
318
303
  replica: () => must(replicaSource)(),
319
- reconcileChangeLog: (resumeFrom, seed) => resumeFrom ? this.#changeLogWriter?.reconcile(resumeFrom) : this.#changeLogWriter?.reconcileFromLog(seed),
320
- changeLog: () => this.#changeLogWriter?.connection,
304
+ reconcileChangeLog: (seed) => this.#changeLogWriter?.reconcileFromLog(seed),
321
305
  reinitializePgChangeLog: replicaFileForReinitialization ? (params) => must(this.#storer).reinitializeFromReplica(params) : void 0
322
306
  });
323
307
  this.#sqliteCatchupOptions = opts.sqliteCatchup ? {
324
308
  ...opts.sqliteCatchup,
325
309
  cleanupGuard: opts.sqliteCatchup.cleanupGuard ?? this.#purgeScheduler?.cleanupGuard
326
310
  } : void 0;
327
- this.#comparator = this.#storer && opts.sqliteChangeLogCompare && opts.sqliteChangeLogWriter && opts.sqliteCatchup ? new SQLiteChangeLogComparator(lc, shard, opts.sqliteCatchup.changeLogFile, opts.sqliteChangeLogWriter.identity, this.#storer, {
328
- setTimeoutFn,
329
- ...opts.sqliteChangeLogCompare
330
- }) : void 0;
331
311
  this.#purgeLock = initialPurgeLock;
332
312
  this.#autoReset = autoReset;
333
313
  this.#state = new RunningState(this.id, void 0, setTimeoutFn);
@@ -537,29 +517,27 @@ var ChangeStreamerImpl = class {
537
517
  return downstream;
538
518
  }
539
519
  /**
540
- * Routes the catchup of an accepted `subscriber` to the SQLite or PG change
541
- * log (or rejects it), and returns the function that removes the subscriber
542
- * from wherever it was registered. If routing fails partway, any partial
520
+ * Starts the catchup of an accepted `subscriber` from the change log in use
521
+ * (or rejects it), and returns the function that removes the subscriber from
522
+ * wherever it was registered. If this fails partway, any partial
543
523
  * registration is removed before the error is rethrown.
544
524
  */
545
525
  async #startCatchup(lc, ctx, subscriber) {
546
- const removeFromForwarder = () => {
547
- lc.info?.(`removing subscriber ${subscriber.id}`);
548
- this.#forwarder.remove(subscriber);
549
- };
550
- const catchupFromPG = () => {
551
- const storer = must(this.#storer, "cannot catch up from a disabled PG change log");
526
+ if (this.#storer) {
527
+ const removeFromForwarder = () => {
528
+ lc.info?.(`removing subscriber ${subscriber.id}`);
529
+ this.#forwarder.remove(subscriber);
530
+ };
552
531
  try {
553
532
  this.#forwarder.add(subscriber);
554
- storer.catchup(subscriber, ctx.mode);
533
+ this.#storer.catchup(subscriber, ctx.mode);
555
534
  } catch (e) {
556
535
  removeFromForwarder();
557
536
  throw e;
558
537
  }
559
538
  return removeFromForwarder;
560
- };
539
+ }
561
540
  const sqliteDecision = this.#selectSQLiteCatchup(lc, ctx);
562
- if (!sqliteDecision) return catchupFromPG();
563
541
  if (sqliteDecision.kind === "rejected") {
564
542
  lc.warn?.(`${sqliteDecision.terminal ? "rejecting" : "ending"} subscription for ${ctx.id}: ${sqliteDecision.message}`);
565
543
  if (sqliteDecision.terminal) subscriber.close(2, sqliteDecision.message);
@@ -577,30 +555,21 @@ var ChangeStreamerImpl = class {
577
555
  }
578
556
  switch (registration.kind) {
579
557
  case "registered":
580
- lc.debug?.(`serving ${ctx.id} from SQLite catchup`, ...coverage ? [{ sqliteChangeLogCoverage: coverage }] : []);
558
+ lc.debug?.(`serving ${ctx.id} from SQLite catchup`, { sqliteChangeLogCoverage: coverage });
581
559
  this.#recordCatchupRoute("sqlite", reason);
582
560
  return removeFromSQLite;
583
- case "uncovered": if (this.#pgChangeLogEnabled) {
584
- lc.info?.(`serving ${ctx.id} from PG catchup: subscriber watermark ${ctx.watermark} is below the SQLite change-log minimum ` + registration.minWatermark);
585
- this.#recordCatchupRoute("pg", "watermark-uncovered");
586
- return catchupFromPG();
587
- } else {
561
+ case "uncovered": {
588
562
  const message = `earliest supported watermark is ${registration.minWatermark} (requested ${ctx.watermark})`;
589
- lc.warn?.(`rejecting subscriber at watermark ${ctx.watermark}: the SQLite change log starts at ${registration.minWatermark} and the PG change log is disabled`);
563
+ lc.warn?.(`rejecting subscriber at watermark ${ctx.watermark}: the SQLite change log starts at ${registration.minWatermark}`);
590
564
  this.#recordCatchupRoute("none", "watermark-uncovered");
591
565
  subscriber.close(2, message);
592
566
  return removeFromSQLite;
593
567
  }
594
- case "declined": if (this.#pgChangeLogEnabled) {
595
- lc.error?.(`serving ${ctx.id} from PG catchup: SQLite catchup registration failed`, registration.error);
596
- this.#recordCatchupRoute("pg", "registration-failed");
597
- return catchupFromPG();
598
- } else {
599
- lc.error?.(`ending subscription for ${ctx.id} to retry SQLite catchup: registration failed and the PG change log is disabled`, registration.error);
568
+ case "declined":
569
+ lc.error?.(`ending subscription for ${ctx.id} to retry SQLite catchup: registration failed`, registration.error);
600
570
  this.#recordCatchupRoute("none", "registration-failed");
601
571
  subscriber.fail(registration.error);
602
572
  return removeFromSQLite;
603
- }
604
573
  case "handled":
605
574
  this.#recordCatchupRoute("sqlite", "registration-handled");
606
575
  return removeFromSQLite;
@@ -647,38 +616,34 @@ var ChangeStreamerImpl = class {
647
616
  const backupWatermark = this.#backupWatermark;
648
617
  const reservations = this.#reservations;
649
618
  if (backupWatermark === void 0 || !reservations?.confirmationsRequired()) return;
650
- const pgState = this.#pgChangeLogEnabled ? await this.#getChangeLogState() : void 0;
619
+ if (this.#storer) {
620
+ const { minWatermark } = await this.#getChangeLogState();
621
+ for (const taskID of reservations.unconfirmedTaskIDs()) this.#confirmReservation(reservations, taskID, "pg", minWatermark, backupWatermark);
622
+ return;
623
+ }
624
+ const router = must(this.#readRouter);
651
625
  for (const taskID of reservations.unconfirmedTaskIDs()) {
652
- let route = this.#readRouter?.peek(taskID);
653
- if (this.#readRouter && route === void 0) continue;
654
- if (!this.#pgChangeLogEnabled && route?.source === "pg") {
655
- this.#readRouter?.release(taskID);
656
- route = this.#readRouter?.pin(taskID);
657
- if (route?.source === "pg") {
626
+ let route = router.peek(taskID);
627
+ if (route === void 0) continue;
628
+ if (!route.available) {
629
+ router.release(taskID);
630
+ route = router.pin(taskID);
631
+ if (!route.available) {
658
632
  if (reservations.noteConfirmationDelayed(taskID)) this.#reservationConfirmDelays.add(1);
659
- this.#lc.warn?.(`delaying snapshot reservation for ${taskID}: SQLite change log is unavailable and the PG change log is disabled`);
633
+ this.#lc.warn?.(`delaying snapshot reservation for ${taskID}: SQLite change log is unavailable`);
660
634
  continue;
661
635
  }
662
636
  }
663
- if (route?.source === "sqlite") {
664
- const coverage = must(route.coverage, "a pinned SQLite route must carry its covered range");
665
- if (this.#pgChangeLogEnabled && coverage.minWatermark > backupWatermark) {
666
- this.#lc.info?.(`demoting ${taskID} to PG catchup: SQLite change-log minimum ${coverage.minWatermark} is later than backupWatermark ` + backupWatermark);
667
- this.#reservationDemotions.add(1);
668
- route = must(this.#readRouter).demote(taskID);
669
- }
670
- }
671
- const source = route?.source ?? "pg";
672
- let minWatermark;
673
- if (route?.source === "sqlite") minWatermark = must(route.coverage, "a pinned SQLite route must carry its covered range").minWatermark;
674
- else minWatermark = must(pgState, "a PG reservation route requires the PG change log").minWatermark;
675
- if (minWatermark <= backupWatermark) {
676
- reservations.confirmFor(taskID, this.#replicaVersion, backupWatermark, source);
677
- this.#restoreAdmission?.reservationConfirmed(taskID, backupWatermark);
678
- } else {
679
- if (reservations.noteConfirmationDelayed(taskID)) this.#reservationConfirmDelays.add(1);
680
- this.#lc.error?.(`${source} change-log minWatermark ${minWatermark} is later than backupWatermark ${backupWatermark}. Delaying confirmation of snapshot reservation until next backup.`);
681
- }
637
+ this.#confirmReservation(reservations, taskID, "sqlite", route.coverage.minWatermark, backupWatermark);
638
+ }
639
+ }
640
+ #confirmReservation(reservations, taskID, source, minWatermark, backupWatermark) {
641
+ if (minWatermark <= backupWatermark) {
642
+ reservations.confirmFor(taskID, this.#replicaVersion, backupWatermark, source);
643
+ this.#restoreAdmission?.reservationConfirmed(taskID, backupWatermark);
644
+ } else {
645
+ if (reservations.noteConfirmationDelayed(taskID)) this.#reservationConfirmDelays.add(1);
646
+ this.#lc.error?.(`${source} change-log minWatermark ${minWatermark} is later than backupWatermark ${backupWatermark}. Delaying confirmation of snapshot reservation until next backup.`);
682
647
  }
683
648
  }
684
649
  #maybeSchedulePGPurge() {
@@ -781,7 +746,6 @@ var ChangeStreamerImpl = class {
781
746
  this.#lagReports.resolve({ request: () => {} });
782
747
  this.#stream?.changes.cancel();
783
748
  this.#purgeScheduler?.stop();
784
- this.#comparator?.stop();
785
749
  this.#sqliteCatchup?.close();
786
750
  this.#changeLogWriter?.close();
787
751
  await Promise.allSettled([this.#storer?.stop(), this.#source.stop()]);
@@ -840,43 +804,31 @@ var ChangeStreamerImpl = class {
840
804
  return this.#currentTransactionCompletion?.promise.then((completion) => completion.watermark) ?? committed;
841
805
  }
842
806
  #selectSQLiteCatchup(lc, ctx) {
843
- const opts = this.#sqliteCatchupOptions;
844
- if (this.#lastForwardedCommitWatermark === void 0 || !opts) return this.#fallbackOrReject("not-ready", "SQLite catchup is not ready and the PG change log is disabled");
845
- if (ctx.mode === "backup" && this.#pgChangeLogEnabled) {
846
- lc.info?.(`not serving backup subscriber ${ctx.id} from SQLite catchup`);
847
- return this.#fallbackOrReject("ineligible-mode", "backup subscribers retain the PG change-log recovery policy");
807
+ const opts = must(this.#sqliteCatchupOptions);
808
+ if (this.#lastForwardedCommitWatermark === void 0) return this.#rejectSQLiteCatchup("not-ready", "SQLite catchup is not ready");
809
+ const route = must(this.#readRouter).consume(ctx.taskID);
810
+ if (!route.available) {
811
+ if (route.reason === "log-unavailable") this.#declineSQLiteCatchup(lc, ctx, opts, `${opts.changeLogFile} is unavailable or incompatible`);
812
+ return this.#rejectSQLiteCatchup(route.reason, `SQLite catchup is unavailable (${route.reason})`);
848
813
  }
849
- let route;
850
- if (this.#readRouter) {
851
- route = this.#readRouter.consume(ctx.taskID);
852
- if (route.source === "pg") {
853
- if (route.reason === "cold-log") lc.info?.(`serving ${ctx.id} from PG catchup: SQLite change log is still warming`, { sqliteChangeLogCoverage: route.coverage });
854
- else if (route.reason === "log-unavailable") this.#declineSQLiteCatchup(lc, ctx, opts, `${opts.changeLogFile} is unavailable or incompatible`);
855
- else lc.debug?.(`serving ${ctx.id} from PG catchup: SQLite route ${route.reason}`, ...route.coverage ? [{ sqliteChangeLogCoverage: route.coverage }] : []);
856
- return this.#fallbackOrReject(route.reason, `SQLite catchup route ${route.reason} is unavailable and the PG change log is disabled`);
857
- }
858
- const coverage = route.coverage;
859
- assert(coverage, "a SQLite route must carry its covered range");
860
- if (ctx.watermark < coverage.minWatermark) {
861
- lc.info?.(`serving ${ctx.id} from PG catchup: subscriber watermark ${ctx.watermark} is below the SQLite change-log minimum ` + coverage.minWatermark, { sqliteChangeLogCoverage: coverage });
862
- return this.#fallbackOrReject("watermark-uncovered", `earliest supported watermark is ${coverage.minWatermark} (requested ${ctx.watermark})`, true);
863
- }
814
+ const { coverage } = route;
815
+ if (ctx.watermark < coverage.minWatermark) {
816
+ lc.info?.(`subscriber watermark ${ctx.watermark} is below the SQLite change-log minimum ${coverage.minWatermark}`, { sqliteChangeLogCoverage: coverage });
817
+ return this.#rejectSQLiteCatchup("watermark-uncovered", `earliest supported watermark is ${coverage.minWatermark} (requested ${ctx.watermark})`, true);
864
818
  }
865
- if (!this.#readRouter && !opts.shouldUse?.(ctx)) return this.#fallbackOrReject("selector", "SQLite catchup was not selected and the PG change log is disabled");
866
- if (this.#readRouter && opts.shouldUse && !opts.shouldUse(ctx)) return this.#fallbackOrReject("selector", "SQLite catchup was not selected and the PG change log is disabled");
867
819
  const catchup = this.#sqliteCatchup ?? this.#openSQLiteCatchup(lc, opts, ctx);
868
- if (catchup) return {
820
+ if (!catchup) return this.#rejectSQLiteCatchup("log-unavailable", "SQLite change log is unavailable");
821
+ return {
869
822
  kind: "selected",
870
823
  catchup,
871
- reason: route?.reason ?? "selector",
872
- coverage: route?.coverage,
873
- logWarm: route === void 0 ? void 0 : route.reason !== "selected-cold"
824
+ reason: route.reason,
825
+ coverage,
826
+ logWarm: route.reason !== "selected-cold"
874
827
  };
875
- else return this.#fallbackOrReject("log-unavailable", "SQLite change log is unavailable and the PG change log is disabled");
876
828
  }
877
- #fallbackOrReject(reason, message, terminal = false) {
878
- this.#recordCatchupRoute(this.#pgChangeLogEnabled ? "pg" : "none", reason);
879
- return this.#pgChangeLogEnabled ? void 0 : {
829
+ #rejectSQLiteCatchup(reason, message, terminal = false) {
830
+ this.#recordCatchupRoute("none", reason);
831
+ return {
880
832
  kind: "rejected",
881
833
  message,
882
834
  terminal
@@ -894,13 +846,10 @@ var ChangeStreamerImpl = class {
894
846
  *
895
847
  * The log may not exist yet -- the writer creates it at the stream loop's
896
848
  * first reconcile, and deletes it when it fails soft -- or may exist without
897
- * content, or may be unreadable. None of those can be allowed to fail a
898
- * subscription:
899
- * this is the last point at which PG catchup can be selected when it is
900
- * enabled -- past `Forwarder.add()` the subscriber is committed to SQLite --
901
- * so each declines here instead. With PG disabled, the caller ends the
902
- * subscription so that it retries. Neither the failure nor the reader is
903
- * retained, so a later subscription retries from scratch.
849
+ * content, or may be unreadable. Each declines here -- before `Forwarder.add()`
850
+ * commits the subscriber to SQLite -- and the caller ends the subscription
851
+ * so that it retries. Neither the failure nor the reader is retained, so a
852
+ * later subscription retries from scratch.
904
853
  */
905
854
  #openSQLiteCatchup(lc, opts, ctx) {
906
855
  let reader;
@@ -930,7 +879,7 @@ var ChangeStreamerImpl = class {
930
879
  return this.#sqliteCatchup;
931
880
  }
932
881
  /**
933
- * Reports falling back to PG catchup, tracking how long the log has been
882
+ * Reports declining SQLite catchup, tracking how long the log has been
934
883
  * unavailable so that a subscription that merely arrived before the writer's
935
884
  * first reconcile does not look like an incident.
936
885
  */
@@ -938,7 +887,7 @@ var ChangeStreamerImpl = class {
938
887
  const now = Date.now();
939
888
  this.#changeLogUnavailableSince ??= now;
940
889
  const unavailableMs = now - this.#changeLogUnavailableSince;
941
- lc[unavailableMs >= (opts.notReadyWarnThresholdMs ?? DEFAULT_CHANGE_LOG_UNAVAILABLE_WARN_THRESHOLD_MS) ? "warn" : "debug"]?.((this.#pgChangeLogEnabled ? `serving ${ctx.id} from PG catchup: ` : `cannot serve ${ctx.id} from SQLite catchup: `) + `${reason} (unavailable for ${unavailableMs} ms)`, ...error === void 0 ? [] : [error]);
890
+ lc[unavailableMs >= (opts.notReadyWarnThresholdMs ?? DEFAULT_CHANGE_LOG_UNAVAILABLE_WARN_THRESHOLD_MS) ? "warn" : "debug"]?.(`cannot serve ${ctx.id} from SQLite catchup: ${reason} (unavailable for ${unavailableMs} ms)`, ...error === void 0 ? [] : [error]);
942
891
  }
943
892
  };
944
893
  var CLEANUP_DELAY_MS = DEFAULT_MAX_RETRY_DELAY_MS * 3;