@rocicorp/zero 1.11.0-canary.21 → 1.11.0-canary.23

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 (109) hide show
  1. package/out/zero/package.js +1 -1
  2. package/out/zero/package.js.map +1 -1
  3. package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
  4. package/out/zero-cache/src/config/normalize.js +1 -2
  5. package/out/zero-cache/src/config/normalize.js.map +1 -1
  6. package/out/zero-cache/src/config/zero-config.d.ts +0 -22
  7. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  8. package/out/zero-cache/src/config/zero-config.js +38 -42
  9. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  10. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  11. package/out/zero-cache/src/server/change-streamer.js +90 -73
  12. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  13. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.d.ts.map +1 -1
  14. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +1 -1
  15. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
  16. package/out/zero-cache/src/services/change-source/common/init-cleanup.d.ts +24 -0
  17. package/out/zero-cache/src/services/change-source/common/init-cleanup.d.ts.map +1 -0
  18. package/out/zero-cache/src/services/change-source/common/init-cleanup.js +44 -0
  19. package/out/zero-cache/src/services/change-source/common/init-cleanup.js.map +1 -0
  20. package/out/zero-cache/src/services/change-source/common/replica-restore.d.ts +49 -1
  21. package/out/zero-cache/src/services/change-source/common/replica-restore.d.ts.map +1 -1
  22. package/out/zero-cache/src/services/change-source/common/replica-restore.js.map +1 -1
  23. package/out/zero-cache/src/services/change-source/custom/change-source.d.ts +1 -1
  24. package/out/zero-cache/src/services/change-source/custom/change-source.d.ts.map +1 -1
  25. package/out/zero-cache/src/services/change-source/custom/change-source.js +7 -3
  26. package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
  27. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts +10 -5
  28. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts.map +1 -1
  29. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +65 -21
  30. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -1
  31. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  32. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  33. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts +2 -1
  34. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  35. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +3 -1
  36. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  37. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.d.ts +31 -0
  38. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.d.ts.map +1 -1
  39. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +100 -2
  40. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js.map +1 -1
  41. package/out/zero-cache/src/services/change-streamer/backfill-state-tracker.d.ts +6 -0
  42. package/out/zero-cache/src/services/change-streamer/backfill-state-tracker.d.ts.map +1 -1
  43. package/out/zero-cache/src/services/change-streamer/backfill-state-tracker.js +8 -0
  44. package/out/zero-cache/src/services/change-streamer/backfill-state-tracker.js.map +1 -1
  45. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts.map +1 -1
  46. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js +3 -16
  47. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js.map +1 -1
  48. package/out/zero-cache/src/services/change-streamer/change-log-initializer.d.ts +17 -0
  49. package/out/zero-cache/src/services/change-streamer/change-log-initializer.d.ts.map +1 -1
  50. package/out/zero-cache/src/services/change-streamer/change-log-initializer.js +44 -1
  51. package/out/zero-cache/src/services/change-streamer/change-log-initializer.js.map +1 -1
  52. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +7 -0
  53. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  54. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +27 -8
  55. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  56. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts +57 -0
  57. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts.map +1 -1
  58. package/out/zero-cache/src/services/change-streamer/schema/tables.js +103 -23
  59. package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
  60. package/out/zero-cache/src/services/change-streamer/storer.d.ts +86 -1
  61. package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
  62. package/out/zero-cache/src/services/change-streamer/storer.js +272 -77
  63. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  64. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts +7 -5
  65. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts.map +1 -1
  66. package/out/zero-cache/src/services/change-streamer/upstream-acker.js +9 -5
  67. package/out/zero-cache/src/services/change-streamer/upstream-acker.js.map +1 -1
  68. package/out/zero-cache/src/services/litestream/commands.d.ts +3 -11
  69. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  70. package/out/zero-cache/src/services/litestream/commands.js +29 -23
  71. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  72. package/out/zero-cache/src/services/litestream/config-v5.yml +0 -7
  73. package/out/zero-cache/src/services/profz.d.ts.map +1 -1
  74. package/out/zero-cache/src/services/profz.js +19 -1
  75. package/out/zero-cache/src/services/profz.js.map +1 -1
  76. package/out/zero-cache/src/services/replicator/replication-status.js +1 -1
  77. package/out/zero-cache/src/services/replicator/replication-status.js.map +1 -1
  78. package/out/zero-cache/src/services/view-syncer/advancement-metrics.d.ts +26 -0
  79. package/out/zero-cache/src/services/view-syncer/advancement-metrics.d.ts.map +1 -0
  80. package/out/zero-cache/src/services/view-syncer/advancement-metrics.js +26 -0
  81. package/out/zero-cache/src/services/view-syncer/advancement-metrics.js.map +1 -0
  82. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +10 -2
  83. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  84. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +102 -122
  85. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  86. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +8 -9
  87. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  88. package/out/zero-cache/src/services/view-syncer/snapshotter.js +15 -3
  89. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  90. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  91. package/out/zero-cache/src/services/view-syncer/view-syncer.js +50 -28
  92. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  93. package/out/zero-client/src/client/version.js +1 -1
  94. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  95. package/out/zero-client/src/client/zero.js +2 -1
  96. package/out/zero-client/src/client/zero.js.map +1 -1
  97. package/out/zero-react/src/use-query.js +12 -3
  98. package/out/zero-react/src/use-query.js.map +1 -1
  99. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  100. package/out/zql/src/ivm/array-view.js +21 -12
  101. package/out/zql/src/ivm/array-view.js.map +1 -1
  102. package/out/zqlite/src/db.d.ts.map +1 -1
  103. package/out/zqlite/src/db.js +34 -6
  104. package/out/zqlite/src/db.js.map +1 -1
  105. package/package.json +1 -1
  106. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.d.ts +0 -34
  107. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.d.ts.map +0 -1
  108. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js +0 -184
  109. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"stream.js","names":[],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/logical-replication/stream.ts"],"sourcesContent":["import {\n PG_ADMIN_SHUTDOWN,\n PG_OBJECT_IN_USE,\n PG_OBJECT_NOT_IN_PREREQUISITE_STATE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport {defu} from 'defu';\nimport postgres, {type Options, type PostgresType} from 'postgres';\nimport {sleep} from '../../../../../../shared/src/sleep.ts';\nimport {getTypeParsers} from '../../../../db/pg-type-parser.ts';\nimport {type PostgresDB} from '../../../../types/pg.ts';\nimport {id, lit} from '../../../../types/sql.ts';\nimport {pipe, type Sink, type Source} from '../../../../types/streams.ts';\nimport {Subscription} from '../../../../types/subscription.ts';\nimport {AutoResetSignal} from '../../../change-streamer/schema/tables.ts';\nimport {fromBigInt} from '../lsn.ts';\nimport {PgoutputParser} from './pgoutput-parser.ts';\nimport type {Message} from './pgoutput.types.ts';\n\nconst DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE = 5;\n\nexport type StreamMessage = [\n lsn: bigint,\n Message | {tag: 'keepalive'; shouldRespond: boolean},\n];\n\n// Expose the `queued` variable of the Subscription to allow\n// the change-source to determine if there are more messages\n// immediately available.\ntype SourceWithPendingQueue<T> = Source<T> & {\n queued: number;\n};\n\nexport function createReplicationSessionFor(\n db: PostgresDB,\n applicationName: string,\n) {\n return postgres(\n defu(\n {\n max: 1,\n ['fetch_types']: false, // Necessary for the streaming protocol\n ['idle_timeout']: null,\n ['max_lifetime']: null as unknown as number,\n connection: {\n ['application_name']: applicationName,\n replication: 'database', // https://www.postgresql.org/docs/current/protocol-replication.html\n },\n },\n // ParsedOptions are technically compatible with Options, but happen\n // to not be typed that way. The postgres.js author does an equivalent\n // merge of ParsedOptions and Options here:\n // https://github.com/porsager/postgres/blob/089214e85c23c90cf142d47fb30bd03f42874984/src/subscribe.js#L13\n db.options as unknown as Options<Record<string, PostgresType>>,\n ),\n );\n}\n\nexport async function subscribe(\n lc: LogContext,\n db: PostgresDB,\n slot: string,\n publications: string[],\n lsn: bigint,\n retriesIfReplicationSlotActive = DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE,\n applicationName = 'zero-replicator',\n inboundTimeoutOverrideMs?: number | undefined,\n): Promise<{\n messages: SourceWithPendingQueue<StreamMessage>;\n acks: Sink<bigint>;\n}> {\n const session = createReplicationSessionFor(db, applicationName);\n\n // Postgres will send keepalives before timing out a wal_sender. It is possible that\n // these keepalives are not received if there is back-pressure in the replication\n // stream. To keep the connection alive, explicitly send keepalives if none have been\n // sent within the last 75% of the wal_sender_timeout.\n //\n // https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n const [{walSenderTimeoutMs}] = await session<\n {walSenderTimeoutMs: number}[]\n >`SELECT EXTRACT(EPOCH FROM (setting || unit)::interval) * 1000 \n AS \"walSenderTimeoutMs\" FROM pg_settings\n WHERE name = 'wal_sender_timeout'`.simple();\n const {\n enabled: livenessEnabled,\n manualKeepaliveTimeout,\n inboundTimeoutMs,\n timerIntervalMs,\n } = computeLivenessTimings(walSenderTimeoutMs, inboundTimeoutOverrideMs);\n lc.info?.(\n livenessEnabled\n ? `wal_sender_timeout: ${walSenderTimeoutMs}ms. ` +\n `Ensuring manual keepalives at least every ${manualKeepaliveTimeout}ms. ` +\n `Inbound timeout: ${inboundTimeoutMs}ms` +\n (inboundTimeoutMs === walSenderTimeoutMs * 2\n ? ''\n : ' (configured override)')\n : `wal_sender_timeout is disabled (0); skipping manual keepalives and ` +\n `inbound liveness detection.`,\n );\n\n const [readable, writable] = await startReplicationStream(\n lc,\n session,\n slot,\n publications,\n lsn,\n retriesIfReplicationSlotActive + 1,\n );\n\n let lastAckTime = Date.now();\n function sendAck(lsn: bigint) {\n writable.write(makeAck(lsn));\n lastAckTime = Date.now();\n }\n\n // Tear down the readable to force a reconnect if the inbound half of the\n // connection goes silent (our outbound acks keep flowing into the void,\n // neither side errors, and the change-source would otherwise sit there\n // forever). See computeLivenessTimings for the derivation of these\n // thresholds — and why the whole timer is disabled when wal_sender_timeout\n // is 0.\n let lastReceivedTime = Date.now();\n\n const livenessTimer = livenessEnabled\n ? setInterval(() => {\n const now = Date.now();\n if (now - lastAckTime > manualKeepaliveTimeout) {\n sendAck(0n);\n lc.debug?.(`sent manual keepalive`);\n }\n const sinceLastReceived = now - lastReceivedTime;\n const {resetWindow, timedOut} = evaluateInboundLiveness({\n sinceLastReceived,\n inboundTimeoutMs,\n queued: messages.queued,\n });\n if (resetWindow) {\n // Downstream back-pressure is preventing us from consuming (and thus\n // receiving) upstream messages. Keep the inbound window fresh so that\n // when the backlog finally drains we grant a full inboundTimeoutMs\n // before concluding the connection is dead — otherwise a long stall\n // would leave lastReceivedTime stale and fire this watchdog on the\n // first post-drain tick, tearing down a healthy connection.\n lastReceivedTime = now;\n } else if (timedOut && !readable.destroyed) {\n lc.warn?.(\n `no message received from ${db.options.host} in ${sinceLastReceived}ms ` +\n `(> inbound timeout ${inboundTimeoutMs}ms). Destroying ` +\n `replication stream to force reconnect.`,\n );\n readable.destroy(\n new Error(\n `replication stream inbound timeout after ${sinceLastReceived}ms`,\n ),\n );\n }\n }, timerIntervalMs)\n : undefined;\n\n let destroyed = false;\n const typeParsers = await getTypeParsers(db, {returnJsonAsString: true});\n const parser = new PgoutputParser(typeParsers);\n const messages = Subscription.create<StreamMessage>({\n cleanup: () => {\n destroyed = true;\n readable.destroyed || readable.destroy();\n clearInterval(livenessTimer);\n return session.end();\n },\n });\n\n readable.once(\n 'close',\n () =>\n // Only log a warning if the stream was not manually closed.\n destroyed || lc.warn?.(`replication stream closed by ${db.options.host}`),\n );\n readable.once(\n 'error',\n e =>\n // Don't log the shutdown signal. This is the expected way for upstream\n // to close the connection (and will be logged downstream).\n (e instanceof postgres.PostgresError && e.code === PG_ADMIN_SHUTDOWN) ||\n lc.warn?.(`error from ${db.options.host}`, e),\n );\n\n pipe({\n source: readable,\n sink: messages,\n parse: buffer => {\n lastReceivedTime = Date.now();\n return parseStreamMessage(lc, buffer, parser);\n },\n // Allow a small buffer of messages to be queued in the subscription so\n // that the change-source loop can check the queue to determine if more\n // messages are immediately available.\n bufferMessages: 5,\n });\n\n return {\n messages,\n acks: {push: sendAck},\n };\n}\n\n/**\n * Derives the timings for the replication liveness timer from the server's\n * `wal_sender_timeout` (in milliseconds).\n *\n * Postgres treats `wal_sender_timeout = 0` as \"disabled\": it will neither\n * terminate an idle wal sender nor emit the periodic keepalives it otherwise\n * sends at roughly `wal_sender_timeout / 2`. Every timing below is derived from\n * that interval, so a disabled timeout disables the liveness machinery entirely\n * (`enabled: false`). Otherwise the derived thresholds would all collapse to 0,\n * inverting the intended meaning: the inbound watchdog would fire on the very\n * first tick and destroy the stream, while the timer itself would busy-spin at\n * a 0ms interval — together producing a continuous reconnect storm.\n *\n * An `inboundTimeoutOverrideMs` (when positive) replaces the default\n * `2x wal_sender_timeout` inbound threshold. The two timeouts protect\n * different parties with asymmetric false-positive costs: `wal_sender_timeout`\n * is the server reaping dead clients (a false positive there costs a cheap\n * reconnect), while the inbound watchdog tears down the stream and replays the\n * in-flight transaction — expensive, and self-defeating when the wal sender is\n * legitimately silent for long stretches (e.g. decoding through WAL from\n * unpublished tables, or assembling a large transaction that pgoutput only\n * sends at commit). Operators who don't own the server setting (e.g. managed\n * or operator-provisioned Postgres with an aggressive cluster-wide\n * `wal_sender_timeout`) can widen the client-side fuse independently.\n *\n * The override has no effect when `wal_sender_timeout` is disabled (0): the\n * server then sends no periodic keepalives, so inbound silence is normal even\n * on an idle healthy connection and any silence-based watchdog would\n * false-positive.\n *\n * https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n */\nexport function computeLivenessTimings(\n walSenderTimeoutMs: number,\n inboundTimeoutOverrideMs?: number | undefined,\n): {\n enabled: boolean;\n manualKeepaliveTimeout: number;\n inboundTimeoutMs: number;\n timerIntervalMs: number;\n} {\n // Guard with `!(x > 0)` so that 0, negative, and NaN values all disable the\n // timer rather than producing degenerate (zero / NaN) thresholds.\n if (!(walSenderTimeoutMs > 0)) {\n return {\n enabled: false,\n manualKeepaliveTimeout: 0,\n inboundTimeoutMs: 0,\n timerIntervalMs: 0,\n };\n }\n // Send a manual keepalive if nothing has been sent in the last 75% of the\n // wal_sender_timeout, so Postgres never times us out under back-pressure.\n const manualKeepaliveTimeout = Math.floor(walSenderTimeoutMs * 0.75);\n return {\n enabled: true,\n manualKeepaliveTimeout,\n // Force a reconnect after 2x wal_sender_timeout — i.e. two consecutive\n // missed keepalives — without any inbound message, to avoid spurious\n // reconnects from a single delayed keepalive (PG scheduling jitter under\n // load, our own event-loop stalls, etc.). A 0 / negative / NaN override\n // falls back to the default rather than degenerating the threshold.\n inboundTimeoutMs:\n inboundTimeoutOverrideMs !== undefined && inboundTimeoutOverrideMs > 0\n ? inboundTimeoutOverrideMs\n : walSenderTimeoutMs * 2,\n // Poll at 1/5th of the manual keepalive interval.\n timerIntervalMs: manualKeepaliveTimeout / 5,\n };\n}\n\n/**\n * Pure decision for the inbound-liveness watchdog, given the time since the\n * last inbound message, the timeout threshold, and the number of messages\n * currently queued downstream (see {@link SourceWithPendingQueue.queued}).\n *\n * A non-empty downstream queue means we have received data we haven't yet\n * consumed: the reason we aren't receiving *more* is our own back-pressure, not\n * upstream silence. In that case we `resetWindow` (treat the backlog as\n * liveness evidence) rather than counting the stall against the connection.\n * This both suppresses false timeouts during back-pressure and — crucially —\n * keeps `lastReceivedTime` fresh so the drain transition (queue emptying after\n * a long stall) doesn't fire the watchdog against a stale timestamp on the very\n * next tick.\n *\n * Only when the queue is empty (consumption has caught up, so reading — and\n * thus keepalives — should be flowing again) does an over-threshold gap\n * indicate a genuinely dead inbound half.\n */\nexport function evaluateInboundLiveness({\n sinceLastReceived,\n inboundTimeoutMs,\n queued,\n}: {\n sinceLastReceived: number;\n inboundTimeoutMs: number;\n queued: number;\n}): {resetWindow: boolean; timedOut: boolean} {\n if (queued > 0) {\n return {resetWindow: true, timedOut: false};\n }\n return {resetWindow: false, timedOut: sinceLastReceived > inboundTimeoutMs};\n}\n\n/**\n * Formats publication names for the START_REPLICATION command.\n * The replication protocol expects format: publication_names 'pub1,pub2'\n * Each name is escaped to handle any quotes that may have passed validation.\n */\nfunction formatPublicationNames(publications: string[]): string {\n // lit() returns 'escaped_name' with surrounding quotes\n // We strip the quotes since the outer quotes are in the template\n return publications.map(p => lit(p).slice(1, -1)).join(',');\n}\n\nasync function startReplicationStream(\n lc: LogContext,\n session: postgres.Sql,\n slot: string,\n publications: string[],\n lsn: bigint,\n maxAttempts: number,\n) {\n for (let i = 0; i < maxAttempts; i++) {\n try {\n const stream = session\n .unsafe(\n `START_REPLICATION SLOT ${id(slot)} LOGICAL ${fromBigInt(lsn)} (\n proto_version '1',\n publication_names '${formatPublicationNames(publications)}',\n messages 'true'\n )`,\n )\n .execute();\n return await Promise.all([stream.readable(), stream.writable()]);\n } catch (e) {\n if (e instanceof postgres.PostgresError) {\n // error: replication slot \"zero_slot_change_source_test_id\" is active for PID 268\n if (e.code === PG_OBJECT_IN_USE) {\n // The freeing up of the replication slot is not transactional;\n // sometimes it takes time for Postgres to consider the slot\n // inactive.\n lc.warn?.(`attempt ${i + 1}: ${String(e)}`, e);\n await sleep(10);\n continue;\n }\n // error: This slot has been invalidated because it exceeded the maximum reserved size.\n // (This is a different manifestation of a slot being invalidated when\n // the wal exceeds the max_slot_wal_keep_size)\n if (e.code === PG_OBJECT_NOT_IN_PREREQUISITE_STATE) {\n lc.error?.(`error starting replication stream`, e);\n throw new AutoResetSignal(`unable to start replication stream`, {\n cause: e,\n });\n }\n }\n throw e;\n }\n }\n throw new Error(\n `exceeded max attempts (${maxAttempts}) to start the Postgres stream`,\n );\n}\n\n/**\n * Starts a \"dummy\" replication session for the specified `slot` which:\n * * does not consume any records\n * * keeps the session alive by sending status messages at the starting LSN\n *\n * This essentially reserves the replication slot for this process by marking\n * it as \"active\" in pg_replication_slots, allowing replication slot cleanup\n * logic to distinguish a new-but-initializing replication slot from one that\n * has been abandoned / orphaned.\n *\n * When initialization (e.g. initial-sync) has finished, the server should take\n * over the replication slot using pg_terminate_backend, which will close the\n * session.\n *\n * Note that caller should not close the supplied `session`, as that will\n * also terminate the dummy replication session.\n */\nexport async function keepSlotActiveUntilTakenOver(\n lc: LogContext,\n session: postgres.Sql,\n slot: string,\n dummyPublication: string,\n lsn: bigint,\n) {\n const [readable, writeable] = await startReplicationStream(\n lc,\n session,\n slot,\n [dummyPublication],\n lsn,\n 1,\n );\n lc.info?.(`keeping ${slot} active until taken over ...`);\n // Send periodic status messages upstream to indicate that the session is\n // alive, but keep the confirmed_flush_lsn at the initial lsn.\n const keepalive = setInterval(() => writeable.write(makeAck(lsn)), 10_000);\n\n readable.on('close', async () => {\n lc.info?.(`released initial reservation of ${slot}`);\n clearInterval(keepalive);\n try {\n await session.end();\n lc.info?.(`ended session`);\n } catch (e) {\n lc.warn?.(`error ending session`, e);\n }\n });\n readable.once('error', e => {\n lc.info?.(`closing replication session to ${session.options.host}`, e);\n readable.destroy();\n });\n return readable;\n}\n\nfunction parseStreamMessage(\n lc: LogContext,\n buffer: Buffer,\n parser: PgoutputParser,\n): StreamMessage | null {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-START-REPLICATION\n if (buffer[0] !== 0x77 && buffer[0] !== 0x6b) {\n lc.warn?.('Unknown message', buffer[0]);\n return null;\n }\n const lsn = buffer.readBigUInt64BE(1);\n if (buffer[0] === 0x77) {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-XLOGDATA\n // (Byte 25 is where the WAL data begins)\n return [lsn, parser.parse(buffer.subarray(25))];\n }\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-PRIMARY-KEEPALIVE-MESSAGE\n // (Byte 17: shouldRespond)\n const shouldRespond = buffer.readInt8(17) !== 0;\n return [lsn, {tag: 'keepalive', shouldRespond}];\n}\n\n// https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-STANDBY-STATUS-UPDATE\nfunction makeAck(lsn: bigint): Buffer {\n const microNow = BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000);\n\n const x = Buffer.alloc(34);\n x[0] = 'r'.charCodeAt(0);\n x.writeBigInt64BE(lsn, 1);\n x.writeBigInt64BE(lsn, 9);\n x.writeBigInt64BE(lsn, 17);\n x.writeBigInt64BE(microNow, 25);\n return x;\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,IAAM,6CAA6C;AAcnD,SAAgB,4BACd,IACA,iBACA;CACA,OAAO,SACL,KACE;EACE,KAAK;GACJ,gBAAgB;GAChB,iBAAiB;GACjB,iBAAiB;EAClB,YAAY;IACT,qBAAqB;GACtB,aAAa;EACf;CACF,GAKA,GAAG,OACL,CACF;AACF;AAEA,eAAsB,UACpB,IACA,IACA,MACA,cACA,KACA,iCAAiC,4CACjC,kBAAkB,mBAClB,0BAIC;CACD,MAAM,UAAU,4BAA4B,IAAI,eAAe;CAQ/D,MAAM,CAAC,EAAC,wBAAuB,MAAM,OAEpC;;2CAEwC,OAAO;CAChD,MAAM,EACJ,SAAS,iBACT,wBACA,kBACA,oBACE,uBAAuB,oBAAoB,wBAAwB;CACvE,GAAG,OACD,kBACI,uBAAuB,mBAAmB,gDACK,uBAAuB,uBAChD,iBAAiB,OACpC,qBAAqB,qBAAqB,IACvC,KACA,4BACN,gGAEN;CAEA,MAAM,CAAC,UAAU,YAAY,MAAM,uBACjC,IACA,SACA,MACA,cACA,KACA,iCAAiC,CACnC;CAEA,IAAI,cAAc,KAAK,IAAI;CAC3B,SAAS,QAAQ,KAAa;EAC5B,SAAS,MAAM,QAAQ,GAAG,CAAC;EAC3B,cAAc,KAAK,IAAI;CACzB;CAQA,IAAI,mBAAmB,KAAK,IAAI;CAEhC,MAAM,gBAAgB,kBAClB,kBAAkB;EAChB,M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K,EAAE;CACzB,EAAE,gBAAgB,UAAU,EAAE;CAC9B,OAAO;AACT"}
1
+ {"version":3,"file":"stream.js","names":["#lc","#start","#restartLSN","#confirmedFlushLSN","#startTime","#done","#first"],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/logical-replication/stream.ts"],"sourcesContent":["import {\n PG_ADMIN_SHUTDOWN,\n PG_OBJECT_IN_USE,\n PG_OBJECT_NOT_IN_PREREQUISITE_STATE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport {defu} from 'defu';\nimport postgres, {type Options, type PostgresType} from 'postgres';\nimport {sleep} from '../../../../../../shared/src/sleep.ts';\nimport {getTypeParsers} from '../../../../db/pg-type-parser.ts';\nimport {getOrCreateHistogram} from '../../../../observability/metrics.ts';\nimport {type PostgresDB} from '../../../../types/pg.ts';\nimport {id, lit} from '../../../../types/sql.ts';\nimport {pipe, type Sink, type Source} from '../../../../types/streams.ts';\nimport {Subscription} from '../../../../types/subscription.ts';\nimport {AutoResetSignal} from '../../../change-streamer/schema/tables.ts';\nimport {fromBigInt, toBigInt, type LSN} from '../lsn.ts';\nimport {PgoutputParser} from './pgoutput-parser.ts';\nimport type {Message} from './pgoutput.types.ts';\n\nconst DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE = 5;\n\nexport type StreamMessage = [\n lsn: bigint,\n Message | {tag: 'keepalive'; shouldRespond: boolean},\n];\n\n// Expose the `queued` variable of the Subscription to allow\n// the change-source to determine if there are more messages\n// immediately available.\ntype SourceWithPendingQueue<T> = Source<T> & {\n queued: number;\n};\n\nexport function createReplicationSessionFor(\n db: PostgresDB,\n applicationName: string,\n) {\n return postgres(\n defu(\n {\n max: 1,\n ['fetch_types']: false, // Necessary for the streaming protocol\n ['idle_timeout']: null,\n ['max_lifetime']: null as unknown as number,\n connection: {\n ['application_name']: applicationName,\n replication: 'database', // https://www.postgresql.org/docs/current/protocol-replication.html\n },\n },\n // ParsedOptions are technically compatible with Options, but happen\n // to not be typed that way. The postgres.js author does an equivalent\n // merge of ParsedOptions and Options here:\n // https://github.com/porsager/postgres/blob/089214e85c23c90cf142d47fb30bd03f42874984/src/subscribe.js#L13\n db.options as unknown as Options<Record<string, PostgresType>>,\n ),\n );\n}\n\nexport async function subscribe(\n lc: LogContext,\n db: PostgresDB,\n slot: string,\n publications: string[],\n lsn: bigint,\n retriesIfReplicationSlotActive = DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE,\n applicationName = 'zero-replicator',\n inboundTimeoutOverrideMs?: number | undefined,\n): Promise<{\n messages: SourceWithPendingQueue<StreamMessage>;\n acks: Sink<bigint>;\n}> {\n const session = createReplicationSessionFor(db, applicationName);\n\n // Postgres will send keepalives before timing out a wal_sender. It is possible that\n // these keepalives are not received if there is back-pressure in the replication\n // stream. To keep the connection alive, explicitly send keepalives if none have been\n // sent within the last 75% of the wal_sender_timeout.\n //\n // https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n const [{walSenderTimeoutMs}] = await session<\n {walSenderTimeoutMs: number}[]\n >`SELECT EXTRACT(EPOCH FROM (setting || unit)::interval) * 1000 \n AS \"walSenderTimeoutMs\" FROM pg_settings\n WHERE name = 'wal_sender_timeout'`.simple();\n const {\n enabled: livenessEnabled,\n manualKeepaliveTimeout,\n inboundTimeoutMs,\n timerIntervalMs,\n } = computeLivenessTimings(walSenderTimeoutMs, inboundTimeoutOverrideMs);\n lc.info?.(\n livenessEnabled\n ? `wal_sender_timeout: ${walSenderTimeoutMs}ms. ` +\n `Ensuring manual keepalives at least every ${manualKeepaliveTimeout}ms. ` +\n `Inbound timeout: ${inboundTimeoutMs}ms` +\n (inboundTimeoutMs === walSenderTimeoutMs * 2\n ? ''\n : ' (configured override)')\n : `wal_sender_timeout is disabled (0); skipping manual keepalives and ` +\n `inbound liveness detection.`,\n );\n\n const decode = await InitialDecodeTracker.start(lc, session, slot, lsn);\n const [readable, writable] = await startReplicationStream(\n lc,\n session,\n slot,\n publications,\n lsn,\n retriesIfReplicationSlotActive + 1,\n );\n\n let lastAckTime = Date.now();\n function sendAck(lsn: bigint) {\n writable.write(makeAck(lsn));\n lastAckTime = Date.now();\n }\n\n // Tear down the readable to force a reconnect if the inbound half of the\n // connection goes silent (our outbound acks keep flowing into the void,\n // neither side errors, and the change-source would otherwise sit there\n // forever). See computeLivenessTimings for the derivation of these\n // thresholds — and why the whole timer is disabled when wal_sender_timeout\n // is 0.\n let lastReceivedTime = Date.now();\n\n const livenessTimer = livenessEnabled\n ? setInterval(() => {\n const now = Date.now();\n if (now - lastAckTime > manualKeepaliveTimeout) {\n sendAck(0n);\n lc.debug?.(`sent manual keepalive`);\n }\n const sinceLastReceived = now - lastReceivedTime;\n const {resetWindow, timedOut} = evaluateInboundLiveness({\n sinceLastReceived,\n inboundTimeoutMs,\n queued: messages.queued,\n });\n if (resetWindow) {\n // Downstream back-pressure is preventing us from consuming (and thus\n // receiving) upstream messages. Keep the inbound window fresh so that\n // when the backlog finally drains we grant a full inboundTimeoutMs\n // before concluding the connection is dead — otherwise a long stall\n // would leave lastReceivedTime stale and fire this watchdog on the\n // first post-drain tick, tearing down a healthy connection.\n lastReceivedTime = now;\n } else if (timedOut && !readable.destroyed) {\n lc.warn?.(\n `no message received from ${db.options.host} in ${sinceLastReceived}ms ` +\n `(> inbound timeout ${inboundTimeoutMs}ms). Destroying ` +\n `replication stream to force reconnect.`,\n );\n readable.destroy(\n new Error(\n `replication stream inbound timeout after ${sinceLastReceived}ms`,\n ),\n );\n }\n }, timerIntervalMs)\n : undefined;\n\n let destroyed = false;\n const typeParsers = await getTypeParsers(db, {returnJsonAsString: true});\n const parser = new PgoutputParser(typeParsers);\n const messages = Subscription.create<StreamMessage>({\n cleanup: () => {\n destroyed = true;\n readable.destroyed || readable.destroy();\n clearInterval(livenessTimer);\n return session.end();\n },\n });\n\n readable.once(\n 'close',\n () =>\n // Only log a warning if the stream was not manually closed.\n destroyed || lc.warn?.(`replication stream closed by ${db.options.host}`),\n );\n readable.once(\n 'error',\n e =>\n // Don't log the shutdown signal. This is the expected way for upstream\n // to close the connection (and will be logged downstream).\n (e instanceof postgres.PostgresError && e.code === PG_ADMIN_SHUTDOWN) ||\n lc.warn?.(`error from ${db.options.host}`, e),\n );\n\n pipe({\n source: readable,\n sink: messages,\n parse: buffer => {\n lastReceivedTime = Date.now();\n const msg = parseStreamMessage(lc, buffer, parser);\n if (msg) {\n decode.observe(msg, lastReceivedTime);\n }\n return msg;\n },\n // Allow a small buffer of messages to be queued in the subscription so\n // that the change-source loop can check the queue to determine if more\n // messages are immediately available.\n bufferMessages: 5,\n });\n\n return {\n messages,\n acks: {push: sendAck},\n };\n}\n\nconst INITIAL_DECODE_HISTOGRAM_BOUNDARIES_S = [\n 0.1, 0.5, 1, 2, 5, 10, 30, 60, 120, 300, 600, 1200, 2400, 3600, 7200,\n];\n\nfunction initialDecodeHistogram() {\n return getOrCreateHistogram('replication', 'stream_initial_decode', {\n description:\n 'Time from START_REPLICATION until upstream has decoded the WAL ' +\n \"between the slot's restart_lsn and the start of the stream\",\n unit: 's',\n bucketBoundaries: INITIAL_DECODE_HISTOGRAM_BOUNDARIES_S,\n });\n}\n\n/**\n * Measures how long upstream takes to decode the WAL between the slot's\n * `restart_lsn` and the start of the stream, during which the stream\n * delivers nothing but keepalives. This span can be large, e.g. for a slot\n * that was created (or last acked) long before the requested start LSN.\n *\n * The walsender streams from `max(lsn, confirmed_flush_lsn)`, but must\n * decode from `restart_lsn` to rebuild its transaction state. The decode is\n * considered complete upon the first message at or past the start:\n * * any XLogData message, since only transactions committed after the\n * start are sent, or\n * * a keepalive at or past the start. Keepalives report the decoding\n * position, except for the first one of the stream, which reports the\n * slot's `confirmed_flush_lsn` and is thus ignored if it is at or past the\n * start.\n *\n * Note that on an idle upstream, a walsender whose start is already\n * confirmed may not send a keepalive until other WAL is written or the\n * periodic keepalive (`wal_sender_timeout / 2`) is due, so the measured\n * time is an upper bound when completed by a keepalive.\n */\nexport class InitialDecodeTracker {\n static async start(\n lc: LogContext,\n session: postgres.Sql,\n slot: string,\n lsn: bigint,\n ): Promise<InitialDecodeTracker> {\n let restartLSN: bigint | undefined;\n let confirmedFlushLSN: bigint | undefined;\n try {\n // Replication sessions only support the simple query protocol, so the\n // slot name is inlined as a literal.\n const [row] = await session\n .unsafe<{restartLSN: LSN | null; confirmedFlushLSN: LSN | null}[]>(\n `SELECT restart_lsn AS \"restartLSN\",\n confirmed_flush_lsn AS \"confirmedFlushLSN\"\n FROM pg_replication_slots\n WHERE slot_name = ${lit(slot)}`,\n )\n .simple();\n restartLSN = row?.restartLSN ? toBigInt(row.restartLSN) : undefined;\n confirmedFlushLSN = row?.confirmedFlushLSN\n ? toBigInt(row.confirmedFlushLSN)\n : undefined;\n } catch (e) {\n lc.warn?.(`unable to read position of replication slot ${slot}`, e);\n }\n return new InitialDecodeTracker(\n lc,\n lsn,\n restartLSN,\n confirmedFlushLSN,\n Date.now(),\n );\n }\n\n readonly #lc: LogContext;\n readonly #start: bigint;\n readonly #restartLSN: bigint | undefined;\n readonly #confirmedFlushLSN: bigint | undefined;\n readonly #startTime: number;\n #first = true;\n #done = false;\n\n constructor(\n lc: LogContext,\n lsn: bigint,\n restartLSN: bigint | undefined,\n confirmedFlushLSN: bigint | undefined,\n startTime: number,\n ) {\n // The walsender silently starts at confirmed_flush_lsn if the\n // requested lsn is behind it.\n this.#start =\n confirmedFlushLSN !== undefined && confirmedFlushLSN > lsn\n ? confirmedFlushLSN\n : lsn;\n this.#lc = lc.withContext('decodeStart', fromBigInt(this.#start));\n this.#restartLSN = restartLSN;\n this.#confirmedFlushLSN = confirmedFlushLSN;\n this.#startTime = startTime;\n\n if (restartLSN !== undefined) {\n this.#lc.info?.(\n `decoding ${this.#start - restartLSN} bytes of WAL from ` +\n `restart_lsn ${fromBigInt(restartLSN)} to the start of the stream`,\n );\n }\n }\n\n /**\n * Observes a received message, returning the elapsed decode time (ms) if\n * this message completed the initial decode.\n */\n observe([lsn, msg]: StreamMessage, now: number): number | undefined {\n if (this.#done) {\n return undefined;\n }\n const first = this.#first;\n this.#first = false;\n if (msg.tag === 'keepalive') {\n if (\n lsn < this.#start ||\n (first && lsn === this.#confirmedFlushLSN) // reports confirmed_flush\n ) {\n return undefined;\n }\n }\n this.#done = true;\n const elapsed = now - this.#startTime;\n initialDecodeHistogram().recordMs(elapsed);\n this.#lc.info?.(\n `upstream decoded the WAL to the start of the stream in ` +\n `${elapsed} ms (${msg.tag} at ${fromBigInt(lsn)}` +\n (this.#restartLSN === undefined\n ? ')'\n : `, ${this.#start - this.#restartLSN} bytes from restart_lsn)`),\n {elapsed},\n );\n return elapsed;\n }\n}\n\n/**\n * Derives the timings for the replication liveness timer from the server's\n * `wal_sender_timeout` (in milliseconds).\n *\n * Postgres treats `wal_sender_timeout = 0` as \"disabled\": it will neither\n * terminate an idle wal sender nor emit the periodic keepalives it otherwise\n * sends at roughly `wal_sender_timeout / 2`. Every timing below is derived from\n * that interval, so a disabled timeout disables the liveness machinery entirely\n * (`enabled: false`). Otherwise the derived thresholds would all collapse to 0,\n * inverting the intended meaning: the inbound watchdog would fire on the very\n * first tick and destroy the stream, while the timer itself would busy-spin at\n * a 0ms interval — together producing a continuous reconnect storm.\n *\n * An `inboundTimeoutOverrideMs` (when positive) replaces the default\n * `2x wal_sender_timeout` inbound threshold. The two timeouts protect\n * different parties with asymmetric false-positive costs: `wal_sender_timeout`\n * is the server reaping dead clients (a false positive there costs a cheap\n * reconnect), while the inbound watchdog tears down the stream and replays the\n * in-flight transaction — expensive, and self-defeating when the wal sender is\n * legitimately silent for long stretches (e.g. decoding through WAL from\n * unpublished tables, or assembling a large transaction that pgoutput only\n * sends at commit). Operators who don't own the server setting (e.g. managed\n * or operator-provisioned Postgres with an aggressive cluster-wide\n * `wal_sender_timeout`) can widen the client-side fuse independently.\n *\n * The override has no effect when `wal_sender_timeout` is disabled (0): the\n * server then sends no periodic keepalives, so inbound silence is normal even\n * on an idle healthy connection and any silence-based watchdog would\n * false-positive.\n *\n * https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n */\nexport function computeLivenessTimings(\n walSenderTimeoutMs: number,\n inboundTimeoutOverrideMs?: number | undefined,\n): {\n enabled: boolean;\n manualKeepaliveTimeout: number;\n inboundTimeoutMs: number;\n timerIntervalMs: number;\n} {\n // Guard with `!(x > 0)` so that 0, negative, and NaN values all disable the\n // timer rather than producing degenerate (zero / NaN) thresholds.\n if (!(walSenderTimeoutMs > 0)) {\n return {\n enabled: false,\n manualKeepaliveTimeout: 0,\n inboundTimeoutMs: 0,\n timerIntervalMs: 0,\n };\n }\n // Send a manual keepalive if nothing has been sent in the last 75% of the\n // wal_sender_timeout, so Postgres never times us out under back-pressure.\n const manualKeepaliveTimeout = Math.floor(walSenderTimeoutMs * 0.75);\n return {\n enabled: true,\n manualKeepaliveTimeout,\n // Force a reconnect after 2x wal_sender_timeout — i.e. two consecutive\n // missed keepalives — without any inbound message, to avoid spurious\n // reconnects from a single delayed keepalive (PG scheduling jitter under\n // load, our own event-loop stalls, etc.). A 0 / negative / NaN override\n // falls back to the default rather than degenerating the threshold.\n inboundTimeoutMs:\n inboundTimeoutOverrideMs !== undefined && inboundTimeoutOverrideMs > 0\n ? inboundTimeoutOverrideMs\n : walSenderTimeoutMs * 2,\n // Poll at 1/5th of the manual keepalive interval.\n timerIntervalMs: manualKeepaliveTimeout / 5,\n };\n}\n\n/**\n * Pure decision for the inbound-liveness watchdog, given the time since the\n * last inbound message, the timeout threshold, and the number of messages\n * currently queued downstream (see {@link SourceWithPendingQueue.queued}).\n *\n * A non-empty downstream queue means we have received data we haven't yet\n * consumed: the reason we aren't receiving *more* is our own back-pressure, not\n * upstream silence. In that case we `resetWindow` (treat the backlog as\n * liveness evidence) rather than counting the stall against the connection.\n * This both suppresses false timeouts during back-pressure and — crucially —\n * keeps `lastReceivedTime` fresh so the drain transition (queue emptying after\n * a long stall) doesn't fire the watchdog against a stale timestamp on the very\n * next tick.\n *\n * Only when the queue is empty (consumption has caught up, so reading — and\n * thus keepalives — should be flowing again) does an over-threshold gap\n * indicate a genuinely dead inbound half.\n */\nexport function evaluateInboundLiveness({\n sinceLastReceived,\n inboundTimeoutMs,\n queued,\n}: {\n sinceLastReceived: number;\n inboundTimeoutMs: number;\n queued: number;\n}): {resetWindow: boolean; timedOut: boolean} {\n if (queued > 0) {\n return {resetWindow: true, timedOut: false};\n }\n return {resetWindow: false, timedOut: sinceLastReceived > inboundTimeoutMs};\n}\n\n/**\n * Formats publication names for the START_REPLICATION command.\n * The replication protocol expects format: publication_names 'pub1,pub2'\n * Each name is escaped to handle any quotes that may have passed validation.\n */\nfunction formatPublicationNames(publications: string[]): string {\n // lit() returns 'escaped_name' with surrounding quotes\n // We strip the quotes since the outer quotes are in the template\n return publications.map(p => lit(p).slice(1, -1)).join(',');\n}\n\nasync function startReplicationStream(\n lc: LogContext,\n session: postgres.Sql,\n slot: string,\n publications: string[],\n lsn: bigint,\n maxAttempts: number,\n) {\n for (let i = 0; i < maxAttempts; i++) {\n try {\n const stream = session\n .unsafe(\n `START_REPLICATION SLOT ${id(slot)} LOGICAL ${fromBigInt(lsn)} (\n proto_version '1',\n publication_names '${formatPublicationNames(publications)}',\n messages 'true'\n )`,\n )\n .execute();\n return await Promise.all([stream.readable(), stream.writable()]);\n } catch (e) {\n if (e instanceof postgres.PostgresError) {\n // error: replication slot \"zero_slot_change_source_test_id\" is active for PID 268\n if (e.code === PG_OBJECT_IN_USE) {\n // The freeing up of the replication slot is not transactional;\n // sometimes it takes time for Postgres to consider the slot\n // inactive.\n lc.warn?.(`attempt ${i + 1}: ${String(e)}`, e);\n await sleep(10);\n continue;\n }\n // error: This slot has been invalidated because it exceeded the maximum reserved size.\n // (This is a different manifestation of a slot being invalidated when\n // the wal exceeds the max_slot_wal_keep_size)\n if (e.code === PG_OBJECT_NOT_IN_PREREQUISITE_STATE) {\n lc.error?.(`error starting replication stream`, e);\n throw new AutoResetSignal(`unable to start replication stream`, {\n cause: e,\n });\n }\n }\n throw e;\n }\n }\n throw new Error(\n `exceeded max attempts (${maxAttempts}) to start the Postgres stream`,\n );\n}\n\n/**\n * Starts a \"dummy\" replication session for the specified `slot` which:\n * * does not consume any records\n * * keeps the session alive by sending status messages at the starting LSN\n *\n * This essentially reserves the replication slot for this process by marking\n * it as \"active\" in pg_replication_slots, allowing replication slot cleanup\n * logic to distinguish a new-but-initializing replication slot from one that\n * has been abandoned / orphaned.\n *\n * When initialization (e.g. initial-sync) has finished, the server should take\n * over the replication slot using pg_terminate_backend, which will close the\n * session.\n *\n * Note that caller should not close the supplied `session`, as that will\n * also terminate the dummy replication session.\n */\nexport async function keepSlotActiveUntilTakenOver(\n lc: LogContext,\n session: postgres.Sql,\n slot: string,\n dummyPublication: string,\n lsn: bigint,\n) {\n const [readable, writeable] = await startReplicationStream(\n lc,\n session,\n slot,\n [dummyPublication],\n lsn,\n 1,\n );\n lc.info?.(`keeping ${slot} active until taken over ...`);\n // Send periodic status messages upstream to indicate that the session is\n // alive, but keep the confirmed_flush_lsn at the initial lsn.\n const keepalive = setInterval(() => writeable.write(makeAck(lsn)), 10_000);\n\n readable.on('close', async () => {\n lc.info?.(`released initial reservation of ${slot}`);\n clearInterval(keepalive);\n try {\n await session.end();\n lc.info?.(`ended session`);\n } catch (e) {\n lc.warn?.(`error ending session`, e);\n }\n });\n readable.once('error', e => {\n lc.info?.(`closing replication session to ${session.options.host}`, e);\n readable.destroy();\n });\n return readable;\n}\n\nfunction parseStreamMessage(\n lc: LogContext,\n buffer: Buffer,\n parser: PgoutputParser,\n): StreamMessage | null {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-START-REPLICATION\n if (buffer[0] !== 0x77 && buffer[0] !== 0x6b) {\n lc.warn?.('Unknown message', buffer[0]);\n return null;\n }\n const lsn = buffer.readBigUInt64BE(1);\n if (buffer[0] === 0x77) {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-XLOGDATA\n // (Byte 25 is where the WAL data begins)\n return [lsn, parser.parse(buffer.subarray(25))];\n }\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-PRIMARY-KEEPALIVE-MESSAGE\n // (Byte 17: shouldRespond)\n const shouldRespond = buffer.readInt8(17) !== 0;\n return [lsn, {tag: 'keepalive', shouldRespond}];\n}\n\n// https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-STANDBY-STATUS-UPDATE\nfunction makeAck(lsn: bigint): Buffer {\n const microNow = BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000);\n\n const x = Buffer.alloc(34);\n x[0] = 'r'.charCodeAt(0);\n x.writeBigInt64BE(lsn, 1);\n x.writeBigInt64BE(lsn, 9);\n x.writeBigInt64BE(lsn, 17);\n x.writeBigInt64BE(microNow, 25);\n return x;\n}\n"],"mappings":";;;;;;;;;;;;;;AAoBA,IAAM,6CAA6C;AAcnD,SAAgB,4BACd,IACA,iBACA;CACA,OAAO,SACL,KACE;EACE,KAAK;GACJ,gBAAgB;GAChB,iBAAiB;GACjB,iBAAiB;EAClB,YAAY;IACT,qBAAqB;GACtB,aAAa;EACf;CACF,GAKA,GAAG,OACL,CACF;AACF;AAEA,eAAsB,UACpB,IACA,IACA,MACA,cACA,KACA,iCAAiC,4CACjC,kBAAkB,mBAClB,0BAIC;CACD,MAAM,UAAU,4BAA4B,IAAI,eAAe;CAQ/D,MAAM,CAAC,EAAC,wBAAuB,MAAM,OAEpC;;2CAEwC,OAAO;CAChD,MAAM,EACJ,SAAS,iBACT,wBACA,kBACA,oBACE,uBAAuB,oBAAoB,wBAAwB;CACvE,GAAG,OACD,kBACI,uBAAuB,mBAAmB,gDACK,uBAAuB,uBAChD,iBAAiB,OACpC,qBAAqB,qBAAqB,IACvC,KACA,4BACN,gGAEN;CAEA,MAAM,SAAS,MAAM,qBAAqB,MAAM,IAAI,SAAS,MAAM,GAAG;CACtE,MAAM,CAA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EA,SAAS,mBACP,IACA,QACA,QACsB;CAEtB,IAAI,OAAO,OAAO,OAAQ,OAAO,OAAO,KAAM;EAC5C,GAAG,OAAO,mBAAmB,OAAO,EAAE;EACtC,OAAO;CACT;CACA,MAAM,MAAM,OAAO,gBAAgB,CAAC;CACpC,IAAI,OAAO,OAAO,KAGhB,OAAO,CAAC,KAAK,OAAO,MAAM,OAAO,SAAS,EAAE,CAAC,CAAC;CAKhD,OAAO,CAAC,KAAK;EAAC,KAAK;EAAa,eADV,OAAO,SAAS,EAAE,MAAM;CACD,CAAC;AAChD;AAGA,SAAS,QAAQ,KAAqB;CACpC,MAAM,WAAW,OAAO,KAAK,IAAI,IAAI,KAAK,IAAI,KAAM,GAAG,CAAC,CAAC,IAAI,OAAO,GAAI;CAExE,MAAM,IAAI,OAAO,MAAM,EAAE;CACzB,EAAE,KAAK,IAAI,WAAW,CAAC;CACvB,EAAE,gBAAgB,KAAK,CAAC;CACxB,EAAE,gBAAgB,KAAK,CAAC;CACxB,EAAE,gBAAgB,KAAK,EAAE;CACzB,EAAE,gBAAgB,UAAU,EAAE;CAC9B,OAAO;AACT"}
@@ -49,6 +49,12 @@ export declare class BackfillStateTracker {
49
49
  * state is never ahead of that of aligned subscribers.
50
50
  */
51
51
  track(change: ChangeStreamData): void;
52
+ /**
53
+ * Requests a restart of the change stream (at the next transaction
54
+ * boundary), e.g. to start it with BackfillRequests that it was not
55
+ * started with.
56
+ */
57
+ requestRestart(reason: string): void;
52
58
  /** Logs and records a (planned) restart for the {@link restartReason}. */
53
59
  recordRestart(): void;
54
60
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"backfill-state-tracker.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/backfill-state-tracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAGjD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,6BAA6B,CAAC;AAK9D,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,iDAAiD,CAAC;AACtF,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,+CAA+C,CAAC;AAEnF,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAC,UAAU,EAAE,iBAAiB,EAAC,MAAM,iBAAiB,CAAC;AAEnE,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,iBAAiB,EACjB,WAAW,GAAG,WAAW,GAAG,mBAAmB,CAChD,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,oBAAoB;;IA6C/B,YAAY,EAAE,EAAE,UAAU,EAEzB;IAED;;;;;OAKG;IACH,IAAI,aAAa,IAAI,MAAM,GAAG,IAAI,CAEjC;IAED;;;;;;;;;OASG;IACH,WAAW,CAAC,mBAAmB,EAAE,eAAe,EAAE,GAAG,eAAe,EAAE,CAyBrE;IAED;;;;OAIG;IACH,KAAK,CAAC,MAAM,EAAE,gBAAgB,QAG7B;IAED,0EAA0E;IAC1E,aAAa,SAGZ;IAED;;;OAGG;IACH,iBAAiB,CACf,EAAE,EAAE,UAAU,EACd,GAAG,EAAE,iBAAiB,GACrB,yBAAyB,CAqB3B;IAED;;;;OAIG;IACH,QAAQ,CACN,UAAU,EAAE,UAAU,EACtB,UAAU,EAAE,YAAY,CAAC,MAAM,GAAG,kBAAkB,CAAC,QAMtD;CAsBF"}
1
+ {"version":3,"file":"backfill-state-tracker.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/backfill-state-tracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAGjD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,6BAA6B,CAAC;AAK9D,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,iDAAiD,CAAC;AACtF,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,+CAA+C,CAAC;AAEnF,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAC,UAAU,EAAE,iBAAiB,EAAC,MAAM,iBAAiB,CAAC;AAEnE,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,iBAAiB,EACjB,WAAW,GAAG,WAAW,GAAG,mBAAmB,CAChD,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,oBAAoB;;IA6C/B,YAAY,EAAE,EAAE,UAAU,EAEzB;IAED;;;;;OAKG;IACH,IAAI,aAAa,IAAI,MAAM,GAAG,IAAI,CAEjC;IAED;;;;;;;;;OASG;IACH,WAAW,CAAC,mBAAmB,EAAE,eAAe,EAAE,GAAG,eAAe,EAAE,CAyBrE;IAED;;;;OAIG;IACH,KAAK,CAAC,MAAM,EAAE,gBAAgB,QAG7B;IAED;;;;OAIG;IACH,cAAc,CAAC,MAAM,EAAE,MAAM,QAE5B;IAED,0EAA0E;IAC1E,aAAa,SAGZ;IAED;;;OAGG;IACH,iBAAiB,CACf,EAAE,EAAE,UAAU,EACd,GAAG,EAAE,iBAAiB,GACrB,yBAAyB,CAqB3B;IAED;;;;OAIG;IACH,QAAQ,CACN,UAAU,EAAE,UAAU,EACtB,UAAU,EAAE,YAAY,CAAC,MAAM,GAAG,kBAAkB,CAAC,QAMtD;CAsBF"}
@@ -88,6 +88,14 @@ var BackfillStateTracker = class {
88
88
  this.#sessionBackfills.apply(change);
89
89
  this.#logBackfills.apply(change);
90
90
  }
91
+ /**
92
+ * Requests a restart of the change stream (at the next transaction
93
+ * boundary), e.g. to start it with BackfillRequests that it was not
94
+ * started with.
95
+ */
96
+ requestRestart(reason) {
97
+ this.#restartReason ??= reason;
98
+ }
91
99
  /** Logs and records a (planned) restart for the {@link restartReason}. */
92
100
  recordRestart() {
93
101
  this.#lc.info?.(`restarting change stream: ${this.#restartReason}`);
@@ -1 +1 @@
1
- {"version":3,"file":"backfill-state-tracker.js","names":["#lc","#subscribers","#restarts","#unexpectedIgnores","#restartReason","#logBackfills","#sessionBackfills","#checkCoverage"],"sources":["../../../../../../zero-cache/src/services/change-streamer/backfill-state-tracker.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {must} from '../../../../shared/src/must.ts';\nimport {getOrCreateCounter} from '../../observability/metrics.ts';\nimport type {Subscription} from '../../types/subscription.ts';\nimport {\n getSuperset,\n withResumePoint,\n} from '../change-source/protocol/backfill-progress.ts';\nimport type {ChangeStreamData} from '../change-source/protocol/current/downstream.ts';\nimport type {BackfillRequest} from '../change-source/protocol/current/upstream.ts';\nimport {BackfillState} from './backfill-state.ts';\nimport type {PreSerializedBatch} from './broadcast.ts';\nimport type {SubscriberContext} from './change-streamer.ts';\nimport type {Subscriber, SubscriberOptions} from './subscriber.ts';\n\nexport type SubscriberBackfillOptions = Pick<\n SubscriberOptions,\n 'backfills' | 'onAligned' | 'onBackfillIgnored'\n>;\n\n/**\n * Tracks the pending backfills (and their progress) of the change log, the\n * subscribers, and the current change stream, in order to determine:\n *\n * * the BackfillRequests with which to start a change stream, i.e. the\n * superset of the pending backfills of the change log and all aligned\n * subscribers, starting from the earliest progress, and\n * * whether (and why) the change stream must be restarted, i.e. when a\n * newly aligned subscriber's pending backfills are not covered by the\n * current stream.\n *\n * The change-streamer calls {@link startStream()} whenever it starts a change\n * stream, {@link track()} for every change that it forwards, and restarts\n * the stream at the next transaction boundary whenever a\n * {@link restartReason} is set.\n */\nexport class BackfillStateTracker {\n readonly #lc: LogContext;\n\n /**\n * The pending backfills of the change log itself, i.e. those of a\n * (hypothetical) subscriber that has applied every change in the log.\n * Whenever a stream is started, it is reset from the change log's cookie\n * jar (which is consistent with the stream's starting watermark), carrying\n * over the progress tracked in memory (as the cookie jar is\n * progress-agnostic). It stands in for subscribers that do not report their\n * pending backfills (i.e. protocol < v8), and ensures that backfills that\n * are pending in the change log continue to be requested.\n */\n #logBackfills = new BackfillState();\n\n /**\n * The backfill progress of the current change stream (i.e. \"session\"),\n * seeded from the BackfillRequests with which the stream was started.\n * This determines whether a (newly aligned) subscriber's pending backfills\n * are covered by the stream.\n */\n #sessionBackfills = new BackfillState();\n\n /** Subscribers that report their pending backfills (protocol v8+). */\n readonly #subscribers = new Set<Subscriber>();\n\n /**\n * Set (to the reason) when the change stream must be restarted with\n * new BackfillRequests.\n */\n #restartReason: string | null = null;\n\n readonly #restarts = getOrCreateCounter(\n 'replication',\n 'backfill_stream_restarts',\n 'Count of change stream restarts initiated to rewind backfills for ' +\n 'subscribers whose pending backfills were not covered by the stream',\n );\n readonly #unexpectedIgnores = getOrCreateCounter(\n 'replication',\n 'backfill_unexpected_ignores',\n 'Count of backfill messages unexpectedly ignored (for some columns) by ' +\n 'an aligned subscriber. The expected count is 0.',\n );\n\n constructor(lc: LogContext) {\n this.#lc = lc;\n }\n\n /**\n * The reason for which the change stream must be restarted, or `null` if\n * it need not be. The restart should happen at the next transaction\n * boundary, i.e. upon the next `commit`, or `status` message outside of a\n * transaction.\n */\n get restartReason(): string | null {\n return this.#restartReason;\n }\n\n /**\n * Computes the BackfillRequests with which to start a new change stream:\n * the superset of the pending backfills of the change log and of all\n * aligned subscribers, all of which are consistent with the stream's\n * starting watermark since streams are (re)started at transaction\n * boundaries.\n *\n * @param logBackfillRequests The BackfillRequests from the change log's\n * cookie jar, consistent with the stream's starting watermark.\n */\n startStream(logBackfillRequests: BackfillRequest[]): BackfillRequest[] {\n // The cookie jar can be behind #logBackfills (e.g. if the storer failed\n // to persist a forwarded commit), in which case the new stream\n // re-delivers those changes.\n this.#logBackfills = new BackfillState(\n this.#logBackfills.withProgress(logBackfillRequests),\n );\n\n const aligned = [...this.#subscribers].filter(s => s.aligned);\n const requests = getSuperset(\n this.#logBackfills.requests(),\n ...aligned.map(s => must(s.backfills).requests()),\n ).map(withResumePoint);\n\n this.#sessionBackfills = new BackfillState(requests);\n // Aligned subscribers are accounted for in the requests.\n this.#restartReason = null;\n if (requests.length) {\n this.#lc.info?.(\n `starting change stream with ${requests.length} backfill requests ` +\n `(from the change log and ${aligned.length} subscribers)`,\n {requests},\n );\n }\n return requests;\n }\n\n /**\n * Tracks a change that is forwarded to subscribers. This must be called\n * (synchronously) before the change is forwarded, so that the stream's\n * state is never ahead of that of aligned subscribers.\n */\n track(change: ChangeStreamData) {\n this.#sessionBackfills.apply(change);\n this.#logBackfills.apply(change);\n }\n\n /** Logs and records a (planned) restart for the {@link restartReason}. */\n recordRestart() {\n this.#lc.info?.(`restarting change stream: ${this.#restartReason}`);\n this.#restarts.add(1);\n }\n\n /**\n * Returns the backfill-related {@link SubscriberOptions} for a new\n * subscriber, which must subsequently be passed to {@link register()}.\n */\n subscriberOptions(\n lc: LogContext,\n ctx: SubscriberContext,\n ): SubscriberBackfillOptions {\n const backfills = newBackfillState(lc, ctx);\n if (!backfills) {\n return {};\n }\n return {\n backfills,\n onAligned: subscriber => {\n if (this.#subscribers.has(subscriber)) {\n this.#checkCoverage(subscriber);\n }\n },\n onBackfillIgnored: columns => {\n if (this.#restartReason === null) {\n lc.warn?.(\n `aligned subscriber ignored backfill data for ${columns.join(', ')}`,\n );\n this.#unexpectedIgnores.add(1);\n }\n },\n };\n }\n\n /**\n * Registers a subscriber (created with {@link subscriberOptions()}) until\n * its `downstream` is closed. This is a no-op for subscribers that do not\n * report their pending backfills.\n */\n register(\n subscriber: Subscriber,\n downstream: Subscription<string | PreSerializedBatch>,\n ) {\n if (subscriber.backfills) {\n this.#subscribers.add(subscriber);\n downstream.addCloseHandler(() => this.#subscribers.delete(subscriber));\n }\n }\n\n /**\n * Checks whether the pending backfills of a newly aligned subscriber are\n * covered by the current stream, requesting a restart of the stream if not.\n *\n * This can be checked as soon as the subscriber is aligned (even in the\n * middle of a transaction), since both the subscriber's and the stream's\n * BackfillStates compare their committed state, and the stream's state is\n * never updated with a change before the change is forwarded to (aligned)\n * subscribers. The restart itself happens at the next transaction boundary.\n */\n #checkCoverage(subscriber: Subscriber) {\n const uncovered = must(subscriber.backfills).uncovered(\n this.#sessionBackfills,\n );\n if (uncovered.length) {\n this.#restartReason ??=\n `subscriber ${subscriber.id} needs backfills that are not ` +\n `covered by the current stream: ${uncovered.join(', ')}`;\n }\n }\n}\n\nfunction newBackfillState(\n lc: LogContext,\n ctx: SubscriberContext,\n): BackfillState | undefined {\n if (ctx.backfills === undefined) {\n return undefined; // protocol < v8\n }\n try {\n return new BackfillState(ctx.backfills);\n } catch (e) {\n lc.warn?.(`invalid backfills from subscriber ${ctx.id}`, e);\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,uBAAb,MAAkC;CAChC;;;;;;;;;;;CAYA,gBAAgB,IAAI,cAAc;;;;;;;CAQlC,oBAAoB,IAAI,cAAc;;CAGtC,+BAAwB,IAAI,IAAgB;;;;;CAM5C,iBAAgC;CAEhC,YAAqB,mBACnB,eACA,4BACA,sIAEF;CACA,qBAA8B,mBAC5B,eACA,+BACA,uHAEF;CAEA,YAAY,IAAgB;EAC1B,KAAKA,MAAM;CACb;;;;;;;CAQA,IAAI,gBAA+B;EACjC,OAAO,KAAKI;CACd;;;;;;;;;;;CAYA,YAAY,qBAA2D;EAIrE,KAAKC,gBAAgB,IAAI,cACvB,KAAKA,cAAc,aAAa,mBAAmB,CACrD;EAEA,MAAM,UAAU,CAAC,GAAG,KAAKJ,YAAY,EAAE,QAAO,MAAK,EAAE,OAAO;EAC5D,MAAM,WAAW,YACf,KAAKI,cAAc,SAAS,GAC5B,GAAG,QAAQ,KAAI,MAAK,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,CAClD,EAAE,IAAI,eAAe;EAErB,KAAKC,oBAAoB,IAAI,cAAc,QAAQ;EAEnD,KAAKF,iBAAiB;EACtB,IAAI,SAAS,QACX,KAAKJ,IAAI,OACP,+BAA+B,SAAS,OAAO,8CACjB,QAAQ,OAAO,gBAC7C,EAAC,SAAQ,CACX;EAEF,OAAO;CACT;;;;;;CAOA,MAAM,QAA0B;EAC9B,KAAKM,kBAAkB,MAAM,MAAM;EACnC,KAAKD,cAAc,MAAM,MAAM;CACjC;;CAGA,gBAAgB;EACd,KAAKL,IAAI,OAAO,6BAA6B,KAAKI,gBAAgB;EAClE,KAAKF,UAAU,IAAI,CAAC;CACtB;;;;;CAMA,kBACE,IACA,KAC2B;EAC3B,MAAM,YAAY,iBAAiB,IAAI,GAAG;EAC1C,IAAI,CAAC,WACH,OAAO,CAAC;EAEV,OAAO;GACL;GACA,YAAW,eAAc;IACvB,IAAI,KAAKD,aAAa,IAAI,UAAU,GAClC,KAAKM,eAAe,UAAU;GAElC;GACA,oBAAmB,YAAW;IAC5B,IAAI,KAAKH,mBAAmB,MAAM;KAChC,GAAG,OACD,gDAAgD,QAAQ,KAAK,IAAI,GACnE;KACA,KAAKD,mBAAmB,IAAI,CAAC;IAC/B;GACF;EACF;CACF;;;;;;CAOA,SACE,YACA,YACA;EACA,IAAI,WAAW,WAAW;GACxB,KAAKF,aAAa,IAAI,UAAU;GAChC,WAAW,sBAAsB,KAAKA,aAAa,OAAO,UAAU,CAAC;EACvE;CACF;;;;;;;;;;;CAYA,eAAe,YAAwB;EACrC,MAAM,YAAY,KAAK,WAAW,SAAS,EAAE,UAC3C,KAAKK,iBACP;EACA,IAAI,UAAU,QACZ,KAAKF,mBACH,cAAc,WAAW,GAAG,+DACM,UAAU,KAAK,IAAI;CAE3D;AACF;AAEA,SAAS,iBACP,IACA,KAC2B;CAC3B,IAAI,IAAI,cAAc,KAAA,GACpB;CAEF,IAAI;EACF,OAAO,IAAI,cAAc,IAAI,SAAS;CACxC,SAAS,GAAG;EACV,GAAG,OAAO,qCAAqC,IAAI,MAAM,CAAC;EAC1D;CACF;AACF"}
1
+ {"version":3,"file":"backfill-state-tracker.js","names":["#lc","#subscribers","#restarts","#unexpectedIgnores","#restartReason","#logBackfills","#sessionBackfills","#checkCoverage"],"sources":["../../../../../../zero-cache/src/services/change-streamer/backfill-state-tracker.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {must} from '../../../../shared/src/must.ts';\nimport {getOrCreateCounter} from '../../observability/metrics.ts';\nimport type {Subscription} from '../../types/subscription.ts';\nimport {\n getSuperset,\n withResumePoint,\n} from '../change-source/protocol/backfill-progress.ts';\nimport type {ChangeStreamData} from '../change-source/protocol/current/downstream.ts';\nimport type {BackfillRequest} from '../change-source/protocol/current/upstream.ts';\nimport {BackfillState} from './backfill-state.ts';\nimport type {PreSerializedBatch} from './broadcast.ts';\nimport type {SubscriberContext} from './change-streamer.ts';\nimport type {Subscriber, SubscriberOptions} from './subscriber.ts';\n\nexport type SubscriberBackfillOptions = Pick<\n SubscriberOptions,\n 'backfills' | 'onAligned' | 'onBackfillIgnored'\n>;\n\n/**\n * Tracks the pending backfills (and their progress) of the change log, the\n * subscribers, and the current change stream, in order to determine:\n *\n * * the BackfillRequests with which to start a change stream, i.e. the\n * superset of the pending backfills of the change log and all aligned\n * subscribers, starting from the earliest progress, and\n * * whether (and why) the change stream must be restarted, i.e. when a\n * newly aligned subscriber's pending backfills are not covered by the\n * current stream.\n *\n * The change-streamer calls {@link startStream()} whenever it starts a change\n * stream, {@link track()} for every change that it forwards, and restarts\n * the stream at the next transaction boundary whenever a\n * {@link restartReason} is set.\n */\nexport class BackfillStateTracker {\n readonly #lc: LogContext;\n\n /**\n * The pending backfills of the change log itself, i.e. those of a\n * (hypothetical) subscriber that has applied every change in the log.\n * Whenever a stream is started, it is reset from the change log's cookie\n * jar (which is consistent with the stream's starting watermark), carrying\n * over the progress tracked in memory (as the cookie jar is\n * progress-agnostic). It stands in for subscribers that do not report their\n * pending backfills (i.e. protocol < v8), and ensures that backfills that\n * are pending in the change log continue to be requested.\n */\n #logBackfills = new BackfillState();\n\n /**\n * The backfill progress of the current change stream (i.e. \"session\"),\n * seeded from the BackfillRequests with which the stream was started.\n * This determines whether a (newly aligned) subscriber's pending backfills\n * are covered by the stream.\n */\n #sessionBackfills = new BackfillState();\n\n /** Subscribers that report their pending backfills (protocol v8+). */\n readonly #subscribers = new Set<Subscriber>();\n\n /**\n * Set (to the reason) when the change stream must be restarted with\n * new BackfillRequests.\n */\n #restartReason: string | null = null;\n\n readonly #restarts = getOrCreateCounter(\n 'replication',\n 'backfill_stream_restarts',\n 'Count of change stream restarts initiated to rewind backfills for ' +\n 'subscribers whose pending backfills were not covered by the stream',\n );\n readonly #unexpectedIgnores = getOrCreateCounter(\n 'replication',\n 'backfill_unexpected_ignores',\n 'Count of backfill messages unexpectedly ignored (for some columns) by ' +\n 'an aligned subscriber. The expected count is 0.',\n );\n\n constructor(lc: LogContext) {\n this.#lc = lc;\n }\n\n /**\n * The reason for which the change stream must be restarted, or `null` if\n * it need not be. The restart should happen at the next transaction\n * boundary, i.e. upon the next `commit`, or `status` message outside of a\n * transaction.\n */\n get restartReason(): string | null {\n return this.#restartReason;\n }\n\n /**\n * Computes the BackfillRequests with which to start a new change stream:\n * the superset of the pending backfills of the change log and of all\n * aligned subscribers, all of which are consistent with the stream's\n * starting watermark since streams are (re)started at transaction\n * boundaries.\n *\n * @param logBackfillRequests The BackfillRequests from the change log's\n * cookie jar, consistent with the stream's starting watermark.\n */\n startStream(logBackfillRequests: BackfillRequest[]): BackfillRequest[] {\n // The cookie jar can be behind #logBackfills (e.g. if the storer failed\n // to persist a forwarded commit), in which case the new stream\n // re-delivers those changes.\n this.#logBackfills = new BackfillState(\n this.#logBackfills.withProgress(logBackfillRequests),\n );\n\n const aligned = [...this.#subscribers].filter(s => s.aligned);\n const requests = getSuperset(\n this.#logBackfills.requests(),\n ...aligned.map(s => must(s.backfills).requests()),\n ).map(withResumePoint);\n\n this.#sessionBackfills = new BackfillState(requests);\n // Aligned subscribers are accounted for in the requests.\n this.#restartReason = null;\n if (requests.length) {\n this.#lc.info?.(\n `starting change stream with ${requests.length} backfill requests ` +\n `(from the change log and ${aligned.length} subscribers)`,\n {requests},\n );\n }\n return requests;\n }\n\n /**\n * Tracks a change that is forwarded to subscribers. This must be called\n * (synchronously) before the change is forwarded, so that the stream's\n * state is never ahead of that of aligned subscribers.\n */\n track(change: ChangeStreamData) {\n this.#sessionBackfills.apply(change);\n this.#logBackfills.apply(change);\n }\n\n /**\n * Requests a restart of the change stream (at the next transaction\n * boundary), e.g. to start it with BackfillRequests that it was not\n * started with.\n */\n requestRestart(reason: string) {\n this.#restartReason ??= reason;\n }\n\n /** Logs and records a (planned) restart for the {@link restartReason}. */\n recordRestart() {\n this.#lc.info?.(`restarting change stream: ${this.#restartReason}`);\n this.#restarts.add(1);\n }\n\n /**\n * Returns the backfill-related {@link SubscriberOptions} for a new\n * subscriber, which must subsequently be passed to {@link register()}.\n */\n subscriberOptions(\n lc: LogContext,\n ctx: SubscriberContext,\n ): SubscriberBackfillOptions {\n const backfills = newBackfillState(lc, ctx);\n if (!backfills) {\n return {};\n }\n return {\n backfills,\n onAligned: subscriber => {\n if (this.#subscribers.has(subscriber)) {\n this.#checkCoverage(subscriber);\n }\n },\n onBackfillIgnored: columns => {\n if (this.#restartReason === null) {\n lc.warn?.(\n `aligned subscriber ignored backfill data for ${columns.join(', ')}`,\n );\n this.#unexpectedIgnores.add(1);\n }\n },\n };\n }\n\n /**\n * Registers a subscriber (created with {@link subscriberOptions()}) until\n * its `downstream` is closed. This is a no-op for subscribers that do not\n * report their pending backfills.\n */\n register(\n subscriber: Subscriber,\n downstream: Subscription<string | PreSerializedBatch>,\n ) {\n if (subscriber.backfills) {\n this.#subscribers.add(subscriber);\n downstream.addCloseHandler(() => this.#subscribers.delete(subscriber));\n }\n }\n\n /**\n * Checks whether the pending backfills of a newly aligned subscriber are\n * covered by the current stream, requesting a restart of the stream if not.\n *\n * This can be checked as soon as the subscriber is aligned (even in the\n * middle of a transaction), since both the subscriber's and the stream's\n * BackfillStates compare their committed state, and the stream's state is\n * never updated with a change before the change is forwarded to (aligned)\n * subscribers. The restart itself happens at the next transaction boundary.\n */\n #checkCoverage(subscriber: Subscriber) {\n const uncovered = must(subscriber.backfills).uncovered(\n this.#sessionBackfills,\n );\n if (uncovered.length) {\n this.#restartReason ??=\n `subscriber ${subscriber.id} needs backfills that are not ` +\n `covered by the current stream: ${uncovered.join(', ')}`;\n }\n }\n}\n\nfunction newBackfillState(\n lc: LogContext,\n ctx: SubscriberContext,\n): BackfillState | undefined {\n if (ctx.backfills === undefined) {\n return undefined; // protocol < v8\n }\n try {\n return new BackfillState(ctx.backfills);\n } catch (e) {\n lc.warn?.(`invalid backfills from subscriber ${ctx.id}`, e);\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,uBAAb,MAAkC;CAChC;;;;;;;;;;;CAYA,gBAAgB,IAAI,cAAc;;;;;;;CAQlC,oBAAoB,IAAI,cAAc;;CAGtC,+BAAwB,IAAI,IAAgB;;;;;CAM5C,iBAAgC;CAEhC,YAAqB,mBACnB,eACA,4BACA,sIAEF;CACA,qBAA8B,mBAC5B,eACA,+BACA,uHAEF;CAEA,YAAY,IAAgB;EAC1B,KAAKA,MAAM;CACb;;;;;;;CAQA,IAAI,gBAA+B;EACjC,OAAO,KAAKI;CACd;;;;;;;;;;;CAYA,YAAY,qBAA2D;EAIrE,KAAKC,gBAAgB,IAAI,cACvB,KAAKA,cAAc,aAAa,mBAAmB,CACrD;EAEA,MAAM,UAAU,CAAC,GAAG,KAAKJ,YAAY,EAAE,QAAO,MAAK,EAAE,OAAO;EAC5D,MAAM,WAAW,YACf,KAAKI,cAAc,SAAS,GAC5B,GAAG,QAAQ,KAAI,MAAK,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,CAClD,EAAE,IAAI,eAAe;EAErB,KAAKC,oBAAoB,IAAI,cAAc,QAAQ;EAEnD,KAAKF,iBAAiB;EACtB,IAAI,SAAS,QACX,KAAKJ,IAAI,OACP,+BAA+B,SAAS,OAAO,8CACjB,QAAQ,OAAO,gB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d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,oBAAoB,EAAC,MAAM,2BAA2B,CAAC;AAGpE,OAAO,EAAyB,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAC1E,OAAO,KAAK,EAAC,qBAAqB,EAAC,MAAM,sBAAsB,CAAC;AAEhE,OAAO,EACL,KAAK,mBAAmB,EAEzB,MAAM,oCAAoC,CAAC;AAI5C,MAAM,MAAM,kCAAkC,GAAG;IAC/C,EAAE,EAAE,UAAU,CAAC;IACf,MAAM,EAAE,oBAAoB,CAAC;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,qBAAqB,CAAC;IACtC,iBAAiB,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAAC;CACrD,CAAC;AAEF,wBAAgB,0BAA0B,CAAC,EACzC,EAAE,EACF,MAAM,EACN,WAAW,EACX,cAAc,EACd,iBAAiB,GAClB,EAAE,kCAAkC,GAAG,aAAa,CAgDpD"}
1
+ {"version":3,"file":"backup-cleanup-monitor-factory.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAC,oBAAoB,EAAC,MAAM,2BAA2B,CAAC;AAGpE,OAAO,EAAyB,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAC1E,OAAO,KAAK,EAAC,qBAAqB,EAAC,MAAM,sBAAsB,CAAC;AAEhE,OAAO,EACL,KAAK,mBAAmB,EAEzB,MAAM,oCAAoC,CAAC;AAG5C,MAAM,MAAM,kCAAkC,GAAG;IAC/C,EAAE,EAAE,UAAU,CAAC;IACf,MAAM,EAAE,oBAAoB,CAAC;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,qBAAqB,CAAC;IACtC,iBAAiB,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAAC;CACrD,CAAC;AAEF,wBAAgB,0BAA0B,CAAC,EACzC,EAAE,EACF,MAAM,EACN,WAAW,EACX,cAAc,EACd,iBAAiB,GAClB,EAAE,kCAAkC,GAAG,aAAa,CAyBpD"}
@@ -1,29 +1,16 @@
1
- import { must } from "../../../../shared/src/must.js";
2
1
  import { getLastBackupTime } from "../litestream/commands.js";
3
2
  import { BackupMonitor } from "./backup-monitor.js";
4
3
  import { LitestreamSyncRequester } from "./litestream-sync-requester.js";
5
4
  import { Litestream3PrometheusPoller } from "./litestream3-prometheus-poller.js";
6
5
  import { ReplicaPoller } from "./replica-poller.js";
7
- import { VfsWatermarkPoller } from "./vfs-watermark-poller.js";
8
6
  //#region ../zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.ts
9
7
  function createBackupCleanupMonitor({ lc, config, replicaFile, changeStreamer, verifyBackupState }) {
10
- const { log, litestream, replica } = config;
8
+ const { litestream, replica } = config;
11
9
  const { backupURL } = litestream;
12
10
  let stream;
13
11
  if (!backupURL) stream = new ReplicaPoller(lc, replicaFile).start();
14
- else if (config.litestream.backupUsingV5 && config.litestream.syncRequesterEnabled) stream = new LitestreamSyncRequester(lc, replicaFile, { intervalMs: litestream.incrementalBackupIntervalSeconds * 1e3 }).start();
15
- else if (config.litestream.backupUsingV5) {
16
- const { logLevel, endpoint, region, vfsQueryExecutable, vfsPollIntervalMs: remotePollIntervalMs } = litestream;
17
- stream = new VfsWatermarkPoller(lc, replicaFile, {
18
- executable: must(vfsQueryExecutable, `litestream-vfs-query-executable must be defined`),
19
- remotePollIntervalMs,
20
- backupURL,
21
- region,
22
- endpoint,
23
- logLevel,
24
- logFormat: log.format
25
- }).start();
26
- } else {
12
+ else if (config.litestream.backupUsingV5) stream = new LitestreamSyncRequester(lc, replicaFile, { intervalMs: litestream.incrementalBackupIntervalSeconds * 1e3 }).start();
13
+ else {
27
14
  const { port: metricsPort } = litestream;
28
15
  stream = new Litestream3PrometheusPoller(lc, replicaFile, backupURL, `http://localhost:${metricsPort}/metrics`, verifyBackupState ?? (() => getLastBackupTime(lc, litestream, replica.file))).start();
29
16
  }
@@ -1 +1 @@
1
- {"version":3,"file":"backup-cleanup-monitor-factory.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {must} from '../../../../shared/src/must.ts';\nimport type {NormalizedZeroConfig} from '../../config/normalize.ts';\nimport type {Source} from '../../types/streams.ts';\nimport {getLastBackupTime} from '../litestream/commands.ts';\nimport {type BackedUpWatermark, BackupMonitor} from './backup-monitor.ts';\nimport type {ChangeStreamerService} from './change-streamer.ts';\nimport {LitestreamSyncRequester} from './litestream-sync-requester.ts';\nimport {\n type BackupStateVerifier,\n Litestream3PrometheusPoller,\n} from './litestream3-prometheus-poller.ts';\nimport {ReplicaPoller} from './replica-poller.ts';\nimport {VfsWatermarkPoller} from './vfs-watermark-poller.ts';\n\nexport type BackupCleanupMonitorFactoryOptions = {\n lc: LogContext;\n config: NormalizedZeroConfig;\n replicaFile: string;\n changeStreamer: ChangeStreamerService;\n verifyBackupState?: BackupStateVerifier | undefined;\n};\n\nexport function createBackupCleanupMonitor({\n lc,\n config,\n replicaFile,\n changeStreamer,\n verifyBackupState,\n}: BackupCleanupMonitorFactoryOptions): BackupMonitor {\n const {log, litestream, replica} = config;\n const {backupURL} = litestream;\n\n let stream: Source<BackedUpWatermark>;\n\n if (!backupURL) {\n stream = new ReplicaPoller(lc, replicaFile).start();\n } else if (\n config.litestream.backupUsingV5 &&\n config.litestream.syncRequesterEnabled\n ) {\n stream = new LitestreamSyncRequester(lc, replicaFile, {\n intervalMs: litestream.incrementalBackupIntervalSeconds * 1000,\n }).start();\n } else if (config.litestream.backupUsingV5) {\n const {\n logLevel,\n endpoint,\n region,\n vfsQueryExecutable,\n vfsPollIntervalMs: remotePollIntervalMs,\n } = litestream;\n stream = new VfsWatermarkPoller(lc, replicaFile, {\n executable: must(\n vfsQueryExecutable,\n `litestream-vfs-query-executable must be defined`,\n ),\n remotePollIntervalMs,\n backupURL,\n region,\n endpoint,\n logLevel,\n logFormat: log.format,\n }).start();\n } else {\n const {port: metricsPort} = litestream;\n stream = new Litestream3PrometheusPoller(\n lc,\n replicaFile,\n backupURL,\n `http://localhost:${metricsPort}/metrics`,\n verifyBackupState ??\n (() => getLastBackupTime(lc, litestream, replica.file)),\n ).start();\n }\n\n return new BackupMonitor(lc, stream, changeStreamer, replicaFile);\n}\n"],"mappings":";;;;;;;;AAuBA,SAAgB,2BAA2B,EACzC,IACA,QACA,aACA,gBACA,qBACoD;CACpD,MAAM,EAAC,KAAK,YAAY,YAAW;CACnC,MAAM,EAAC,cAAa;CAEpB,IAAI;CAEJ,IAAI,CAAC,WACH,SAAS,IAAI,cAAc,IAAI,WAAW,EAAE,MAAM;MAC7C,IACL,OAAO,WAAW,iBAClB,OAAO,WAAW,sBAElB,SAAS,IAAI,wBAAwB,IAAI,aAAa,EACpD,YAAY,WAAW,mCAAmC,IAC5D,CAAC,EAAE,MAAM;MACJ,IAAI,OAAO,WAAW,eAAe;EAC1C,MAAM,EACJ,UACA,UACA,QACA,oBACA,mBAAmB,yBACjB;EACJ,SAAS,IAAI,mBAAmB,IAAI,aAAa;GAC/C,YAAY,KACV,oBACA,iDACF;GACA;GACA;GACA;GACA;GACA;GACA,WAAW,IAAI;EACjB,CAAC,EAAE,MAAM;CACX,OAAO;EACL,MAAM,EAAC,MAAM,gBAAe;EAC5B,SAAS,IAAI,4BACX,IACA,aACA,WACA,oBAAoB,YAAY,WAChC,4BACS,kBAAkB,IAAI,YAAY,QAAQ,IAAI,EACzD,EAAE,MAAM;CACV;CAEA,OAAO,IAAI,cAAc,IAAI,QAAQ,gBAAgB,WAAW;AAClE"}
1
+ {"version":3,"file":"backup-cleanup-monitor-factory.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {NormalizedZeroConfig} from '../../config/normalize.ts';\nimport type {Source} from '../../types/streams.ts';\nimport {getLastBackupTime} from '../litestream/commands.ts';\nimport {type BackedUpWatermark, BackupMonitor} from './backup-monitor.ts';\nimport type {ChangeStreamerService} from './change-streamer.ts';\nimport {LitestreamSyncRequester} from './litestream-sync-requester.ts';\nimport {\n type BackupStateVerifier,\n Litestream3PrometheusPoller,\n} from './litestream3-prometheus-poller.ts';\nimport {ReplicaPoller} from './replica-poller.ts';\n\nexport type BackupCleanupMonitorFactoryOptions = {\n lc: LogContext;\n config: NormalizedZeroConfig;\n replicaFile: string;\n changeStreamer: ChangeStreamerService;\n verifyBackupState?: BackupStateVerifier | undefined;\n};\n\nexport function createBackupCleanupMonitor({\n lc,\n config,\n replicaFile,\n changeStreamer,\n verifyBackupState,\n}: BackupCleanupMonitorFactoryOptions): BackupMonitor {\n const {litestream, replica} = config;\n const {backupURL} = litestream;\n\n let stream: Source<BackedUpWatermark>;\n\n if (!backupURL) {\n stream = new ReplicaPoller(lc, replicaFile).start();\n } else if (config.litestream.backupUsingV5) {\n stream = new LitestreamSyncRequester(lc, replicaFile, {\n intervalMs: litestream.incrementalBackupIntervalSeconds * 1000,\n }).start();\n } else {\n const {port: metricsPort} = litestream;\n stream = new Litestream3PrometheusPoller(\n lc,\n replicaFile,\n backupURL,\n `http://localhost:${metricsPort}/metrics`,\n verifyBackupState ??\n (() => getLastBackupTime(lc, litestream, replica.file)),\n ).start();\n }\n\n return new BackupMonitor(lc, stream, changeStreamer, replicaFile);\n}\n"],"mappings":";;;;;;AAqBA,SAAgB,2BAA2B,EACzC,IACA,QACA,aACA,gBACA,qBACoD;CACpD,MAAM,EAAC,YAAY,YAAW;CAC9B,MAAM,EAAC,cAAa;CAEpB,IAAI;CAEJ,IAAI,CAAC,WACH,SAAS,IAAI,cAAc,IAAI,WAAW,EAAE,MAAM;MAC7C,IAAI,OAAO,WAAW,eAC3B,SAAS,IAAI,wBAAwB,IAAI,aAAa,EACpD,YAAY,WAAW,mCAAmC,IAC5D,CAAC,EAAE,MAAM;MACJ;EACL,MAAM,EAAC,MAAM,gBAAe;EAC5B,SAAS,IAAI,4BACX,IACA,aACA,WACA,oBAAoB,YAAY,WAChC,4BACS,kBAAkB,IAAI,YAAY,QAAQ,IAAI,EACzD,EAAE,MAAM;CACV;CAEA,OAAO,IAAI,cAAc,IAAI,QAAQ,gBAAgB,WAAW;AAClE"}
@@ -80,6 +80,23 @@ export type ChangeLogInitializerSources = {
80
80
  * an error, since neither says anything about the two stores.
81
81
  */
82
82
  readonly changeLog: () => Database | undefined;
83
+ /**
84
+ * Re-initializes the Postgres change log from the replica's resume point
85
+ * (taking ownership of it) if it does not contain all of the replica's
86
+ * changes, re-checking under lock. Typically
87
+ * `Storer.reinitializeFromReplica()`.
88
+ *
89
+ * Supplied only when the change log's head is behind the position of the
90
+ * replication slot from which replication resumes, i.e. when it may be
91
+ * missing transactions that the slot will not stream. Otherwise, a replica
92
+ * that is ahead of the change log (as changes are forwarded before they are
93
+ * stored) is caught up by the change stream, which resumes from the change
94
+ * log's head.
95
+ */
96
+ readonly reinitializePgChangeLog?: ((params: {
97
+ watermark: string;
98
+ cookies: CookieSet;
99
+ }) => Promise<'reinitialized' | 'maintained'>) | undefined;
83
100
  };
84
101
  type Opts = {
85
102
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"change-log-initializer.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/change-log-initializer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAIjD,OAAO,EAAC,QAAQ,EAAC,MAAM,8BAA8B,CAAC;AAKtD,OAAO,KAAK,EAAC,eAA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I,oBAAoB,GAAG,SAAS,CAEjD;IAED;;;;;;OAMG;IACG,UAAU,IAAI,OAAO,CAAC,wBAAwB,CAAC,CAqCpD;CA+GF;AAUD;;;;GAIG;AACH,wBAAgB,mCAAmC,CACjD,EAAE,EAAE,QAAQ,GACX,wBAAwB,CAc1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,2BAA2B,CACzC,EAAE,EAAE,UAAU,EACd,WAAW,EAAE,MAAM,GAClB,MAAM,wBAAwB,CAahC"}
1
+ {"version":3,"file":"change-log-initializer.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/change-log-initializer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAIjD,OAAO,EAAC,QAAQ,EAAC,MAAM,8BAA8B,CAAC;AAKtD,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,+CAA+C,CAAC;AACnF,OAAO,EAGL,KAAK,SAAS,EACf,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAEL,KAAK,oBAAoB,EAC1B,MAAM,gCAAgC,CAAC;AAQxC,0EAA0E;AAC1E,MAAM,MAAM,wBAAwB,GAAG;IACrC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,gBAAgB,EAAE,eAAe,EAAE,CAAC;IAC7C,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,oBAAoB,GAC5B,OAAO,GACP,kBAAkB,GAClB,WAAW,GACX,cAAc,GACd,OAAO,CAAC;AAEZ,MAAM,MAAM,2BAA2B,GAAG;IACxC,yDAAyD;IACzD,QAAQ,CAAC,WAAW,EAAE,MAAM,OAAO,CAAC,wBAAwB,CAAC,CAAC;IAC9D,qDAAqD;IACrD,QAAQ,CAAC,OAAO,EAAE,MAAM,wBAAwB,CAAC;IACjD;;;;;;;OAOG;IACH,QAAQ,CAAC,kBAAkB,EAAE,CAC3B,UAAU,EAAE,oBAAoB,GAAG,SAAS,EAC5C,IAAI,EAAE,MAAM,oBAAoB,KAC7B,oBAAoB,GAAG,SAAS,CAAC;IACtC;;;;;OAKG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,QAAQ,GAAG,SAAS,CAAC;IAC/C;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,uBAAuB,CAAC,EAC7B,CAAC,CAAC,MAAM,EAAE;QACR,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,SAAS,CAAC;KACpB,KAAK,OAAO,CAAC,eAAe,GAAG,YAAY,CAAC,CAAC,GAC9C,SAAS,CAAC;CACf,CAAC;AAEF,KAAK,IAAI,GAAG;IACV;;;;;;OAMG;IAEH,QAAQ,CAAC,mBAAmB,EAAE,OAAO,CAAC;IACtC;;;;;;OAMG;IACH,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;CACnC,CAAC;AAcF,qBAAa,oBAAoB;;IAqB/B,YACE,EAAE,EAAE,UAAU,EACd,EAAC,mBAAmB,EAAE,eAAe,EAAC,EAAE,IAAI,EAC5C,OAAO,EAAE,2BAA2B,EAUrC;IAED;;;;;OAKG;IACH,cAAc,IAAI,oBAAoB,GAAG,SAAS,CAEjD;IAED;;;;;;OAMG;IACG,UAAU,IAAI,OAAO,CAAC,wBAAwB,CAAC,CAqCpD;CA2JF;AAUD;;;;GAIG;AACH,wBAAgB,mCAAmC,CACjD,EAAE,EAAE,QAAQ,GACX,wBAAwB,CAc1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,2BAA2B,CACzC,EAAE,EAAE,UAAU,EACd,WAAW,EAAE,MAAM,GAClB,MAAM,wBAAwB,CAahC"}
@@ -29,6 +29,11 @@ var ChangeLogInitializer = class {
29
29
  #sources;
30
30
  #resultCounter = getOrCreateCounter("replica", "sqlite_change_log.init_compare_result", "Comparisons of the replica-derived stream initialization parameters against the Postgres-derived ones, by classification.");
31
31
  #lastComparison;
32
+ /**
33
+ * The (restored) replica's resume point that the Postgres change log must
34
+ * cover, read once. See {@link #pgChangeLog}.
35
+ */
36
+ #restoredReplica;
32
37
  constructor(lc, { initFromPgChangeLog, initFromReplica }, sources) {
33
38
  assert(initFromPgChangeLog || initFromReplica, `At least one of initFromPgChangeLog or initFromReplica must be true`);
34
39
  this.#lc = lc.withContext("component", "change-log-initializer");
@@ -54,7 +59,7 @@ var ChangeLogInitializer = class {
54
59
  */
55
60
  async initialize() {
56
61
  this.#lastComparison = void 0;
57
- const pg = this.#initFromPgChangeLog ? await this.#sources.pgChangeLog() : void 0;
62
+ const pg = this.#initFromPgChangeLog ? await this.#pgChangeLog() : void 0;
58
63
  const replica = this.#initFromReplica ? this.#readReplica(pg === void 0) : void 0;
59
64
  const reconciled = this.#sources.reconcileChangeLog(pg && {
60
65
  resumeWatermark: pg.lastWatermark,
@@ -72,6 +77,44 @@ var ChangeLogInitializer = class {
72
77
  };
73
78
  }
74
79
  /**
80
+ * Reads the Postgres change log's initialization parameters, first
81
+ * re-initializing it from the replica (taking ownership of it) if it may be
82
+ * missing transactions that the replication slot will not stream, e.g. if it
83
+ * is no longer maintained after running in high-availability mode. This is
84
+ * the Postgres counterpart of seeding a new SQLite change log from the
85
+ * replica.
86
+ *
87
+ * This only applies when the change log's head is behind the slot's position
88
+ * (i.e. when {@link ChangeLogInitializerSources.reinitializePgChangeLog} is
89
+ * supplied), and the change log is only re-initialized if its head is also
90
+ * behind the replica's. Otherwise, it contains every transaction before the
91
+ * slot's position: one that was acked after being stored would be in the
92
+ * change log, and one that was acked after being backed up would be in the
93
+ * replica, which the change log's head covers.
94
+ *
95
+ * The replica is read only once, i.e. as it was restored from the backup,
96
+ * before any stream is started. The live replica is no reference: it is
97
+ * caught up from the forwarded changes, which can be ahead of what has been
98
+ * stored, e.g. by the change log's previous owner. Against that fixed
99
+ * resume point, re-initialization is idempotent: afterwards, the change log's
100
+ * head (which only advances) covers it, so subsequent stream connections
101
+ * leave it untouched.
102
+ */
103
+ async #pgChangeLog() {
104
+ const pg = await this.#sources.pgChangeLog();
105
+ const reinitialize = this.#sources.reinitializePgChangeLog;
106
+ if (!reinitialize) return pg;
107
+ this.#restoredReplica ??= this.#sources.replica();
108
+ const { lastWatermark, cookies } = this.#restoredReplica;
109
+ if (pg.lastWatermark >= lastWatermark) return pg;
110
+ const result = await reinitialize({
111
+ watermark: lastWatermark,
112
+ cookies
113
+ });
114
+ this.#lc.info?.(`Postgres change log at ${pg.lastWatermark} is behind the replica at ${lastWatermark}: ${result}`);
115
+ return this.#sources.pgChangeLog();
116
+ }
117
+ /**
75
118
  * Compares both sources and records the result. Postgres remains
76
119
  * authoritative when both sources are enabled.
77
120
  */
@@ -1 +1 @@
1
- {"version":3,"file":"change-log-initializer.js","names":["#lc","#initFromPgChangeLog","#initFromReplica","#sources","#resultCounter","#lastComparison","#readReplica","#compare","#classify","#report"],"sources":["../../../../../../zero-cache/src/services/change-streamer/change-log-initializer.ts"],"sourcesContent":["/**\n * Where a stream connection's initialization parameters come from.\n *\n * A change-streamer starts a stream from three things read at one position: the\n * watermark to resume at, the {@link BackfillRequest}s to hand the change\n * source, and the cookie set those requests were derived from. Postgres has\n * always supplied all three (`Storer.getStartStreamInitializationParameters()`).\n * The point of this workstream is that a restored replica can supply them\n * instead, which is what lets the `cdc` schema be decommissioned.\n *\n * This is the two-source stage. Both are computed on every stream connection,\n * compared, and Postgres's is used — so the replica-derived path runs in\n * production, at full rate, before anything depends on it. The flip is\n * `initFromPgChangeLog: false`, not a rewrite.\n *\n * The shape is {@link UpstreamAcker}'s, deliberately: two stores, one output, a\n * flag per store, and the Postgres flag carrying the marker that retires it.\n * That class solved the identical problem for upstream ACKs earlier in this\n * workstream.\n *\n * ### Why the comparison is a fold and not an equality\n *\n * The replica is a subscriber downstream of the change-streamer's write loop,\n * so its state version trails the log's head by however long the replicator is\n * behind. A raw difference between the two cookie sets is therefore the normal\n * case and says nothing. What is checked instead is that the replica's set,\n * carried forward over exactly the schema changes the log holds in the interval,\n * equals Postgres's set at Postgres's watermark. The log holds precisely those\n * transactions, schema changes are rare, and the check is exact rather than\n * approximate — so it also happens to be the strongest available test of the\n * premise that three interpreters of one fold agree.\n */\n\nimport type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {BigIntJSON} from '../../../../shared/src/bigint-json.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {Database} from '../../../../zqlite/src/db.ts';\nimport {StatementRunner} from '../../db/statements.ts';\nimport {getOrCreateCounter} from '../../observability/metrics.ts';\nimport type {SchemaChange} from '../change-source/protocol/current/data.ts';\nimport {schemaChangeTags} from '../change-source/protocol/current/schema-change-tags.ts';\nimport type {BackfillRequest} from '../change-source/protocol/current/upstream.ts';\nimport {\n backfillRequestsFrom,\n foldCookies,\n type CookieSet,\n} from '../replicator/change-log-cookies.ts';\nimport {\n CHANGE_LOG_STREAM_TABLE,\n type ChangeLogResumePoint,\n} from '../replicator/change-log-db.ts';\nimport {\n readBackfillRequests,\n readReplicaCookies,\n} from '../replicator/schema/backfilling.ts';\nimport {getReplicationState} from '../replicator/schema/replication-state.ts';\nimport {SQLITE_CHANGE_LOG_BOUNDARY_SQL} from './sqlite-change-log-reader.ts';\n\n/** Everything one stream connection starts from, read at one position. */\nexport type InitializationParameters = {\n readonly lastWatermark: string;\n readonly backfillRequests: BackfillRequest[];\n readonly cookies: CookieSet;\n};\n\n/**\n * - `equal` — the two stores were at the same watermark and held the same\n * cookies. Nothing had to be folded, which is the strongest of the four.\n * - `equal-after-fold` — the replica trailed, and carrying its set forward over\n * the log's schema changes in the interval reproduced Postgres's exactly.\n * The expected steady-state result.\n * - `divergent` — the two disagree at the same position. Alert; investigate\n * individually.\n * - `inconclusive` — the log does not span the interval, so the fold cannot be\n * performed. Routine on a cold log, and not a finding.\n * - `error` — the comparison itself failed: the replica could not be read, or\n * the fold threw. Not one of the four classifications, because it is a\n * statement about this process rather than about the two stores. Kept\n * separate rather than folded into `inconclusive` so that a change-streamer\n * that has silently stopped being able to read its replica is visible before\n * that read becomes the only source.\n */\nexport type InitComparisonResult =\n | 'equal'\n | 'equal-after-fold'\n | 'divergent'\n | 'inconclusive'\n | 'error';\n\nexport type ChangeLogInitializerSources = {\n /** `Storer.getStartStreamInitializationParameters()`. */\n readonly pgChangeLog: () => Promise<InitializationParameters>;\n /** Typically {@link replicaInitializationSource}. */\n readonly replica: () => InitializationParameters;\n /**\n * Reconciles the SQLite change log and returns its current resume point.\n *\n * When `resumeFrom` is present, it supplies the resume point. Otherwise, a\n * valid log uses its own head. A new or invalid log uses `seed`.\n *\n * Returns `undefined` when the writer is disabled or unavailable.\n */\n readonly reconcileChangeLog: (\n resumeFrom: ChangeLogResumePoint | undefined,\n seed: () => ChangeLogResumePoint,\n ) => ChangeLogResumePoint | undefined;\n /**\n * The change log, for the fold. `undefined` before the stream loop's first\n * reconcile has created it, and again once the writer has failed soft and\n * deleted it — both of which make the comparison `inconclusive` rather than\n * an error, since neither says anything about the two stores.\n */\n readonly changeLog: () => Database | undefined;\n};\n\ntype Opts = {\n /**\n * When true, Postgres supplies the resume point and the cookies for a new\n * change log.\n *\n * When false, a valid log resumes from its own head. The replica supplies the\n * resume point for a new or invalid log.\n */\n // TODO: set false when retiring PG\n readonly initFromPgChangeLog: boolean;\n /**\n * Derive the parameters from the replica. When Postgres is enabled, these\n * parameters are used only for comparison.\n *\n * When Postgres is disabled, the replica supplies the seed and the fallback\n * resume point.\n */\n readonly initFromReplica: boolean;\n};\n\n/**\n * The most change-log rows one comparison will scan.\n *\n * The interval is `(replicaWatermark, pgWatermark]`, so it is normally a\n * handful of transactions — but a replicator that has been stalled can leave it\n * arbitrarily large, and this runs on the stream loop between reconciliation and\n * `startStream`, where latency delays the resumption of replication. Over the\n * cap the comparison declines rather than pays: a chronically lagging replicator\n * shows up as `inconclusive`, which is charted, and not as a stall.\n */\nconst MAX_FOLD_SCAN_ROWS = 10_000;\n\nexport class ChangeLogInitializer {\n readonly #lc: LogContext;\n readonly #initFromPgChangeLog: boolean;\n readonly #initFromReplica: boolean;\n readonly #sources: ChangeLogInitializerSources;\n\n readonly #resultCounter = getOrCreateCounter(\n 'replica',\n 'sqlite_change_log.init_compare_result',\n 'Comparisons of the replica-derived stream initialization parameters ' +\n 'against the Postgres-derived ones, by classification.',\n );\n\n #lastComparison: InitComparisonResult | undefined;\n\n constructor(\n lc: LogContext,\n {initFromPgChangeLog, initFromReplica}: Opts,\n sources: ChangeLogInitializerSources,\n ) {\n assert(\n initFromPgChangeLog || initFromReplica,\n `At least one of initFromPgChangeLog or initFromReplica must be true`,\n );\n this.#lc = lc.withContext('component', 'change-log-initializer');\n this.#initFromPgChangeLog = initFromPgChangeLog;\n this.#initFromReplica = initFromReplica;\n this.#sources = sources;\n }\n\n /**\n * Returns the comparison result from the most recent {@link initialize}\n * call. Returns `undefined` when that call used only one source.\n *\n * Tests and metrics use this result. It does not affect initialization.\n */\n lastComparison(): InitComparisonResult | undefined {\n return this.#lastComparison;\n }\n\n /**\n * Returns the initialization parameters for one stream connection after it\n * reconciles the change log.\n *\n * The selected source determines how reconciliation works. Keeping both\n * operations here hides that choice from the caller.\n */\n async initialize(): Promise<InitializationParameters> {\n this.#lastComparison = undefined;\n const pg = this.#initFromPgChangeLog\n ? await this.#sources.pgChangeLog()\n : undefined;\n // The replica is required when Postgres is disabled. It seeds a new log and\n // supplies a resume point when the writer is unavailable.\n const replica = this.#initFromReplica\n ? this.#readReplica(pg === undefined)\n : undefined;\n\n const reconciled = this.#sources.reconcileChangeLog(\n pg && {resumeWatermark: pg.lastWatermark, cookies: pg.cookies},\n () => resumePointOf(must(replica, 'no seed for the SQLite change log')),\n );\n\n if (pg) {\n if (replica) {\n // Compare only after reconciliation. The fold must read the schema\n // changes from the replica watermark through the Postgres watermark.\n // Reconciliation makes sure that the log contains that interval.\n this.#lastComparison = this.#compare(pg, replica);\n }\n return pg;\n }\n\n // Use the reconciled log position. If the writer is unavailable, use the\n // replica position.\n const resumePoint =\n reconciled ?? resumePointOf(must(replica, 'no initialization source'));\n return {\n lastWatermark: resumePoint.resumeWatermark,\n cookies: resumePoint.cookies,\n // Derive the requests from these cookies so that the requests and the\n // watermark describe the same position.\n backfillRequests: backfillRequestsFrom(resumePoint.cookies),\n };\n }\n\n /**\n * Compares both sources and records the result. Postgres remains\n * authoritative when both sources are enabled.\n */\n #compare(\n pg: InitializationParameters,\n replica: InitializationParameters,\n ): InitComparisonResult {\n let result: InitComparisonResult;\n try {\n result = this.#classify(pg, replica);\n } catch (e) {\n this.#lc.warn?.('failed to compare change-log initialization sources', e);\n result = 'error';\n }\n this.#resultCounter.add(1, {result});\n this.#lc.debug?.('compared change-log initialization sources', {\n changeLogInitCompare: {\n result,\n pgWatermark: pg.lastWatermark,\n replicaWatermark: replica.lastWatermark,\n },\n });\n return result;\n }\n\n #readReplica(required: boolean): InitializationParameters | undefined {\n try {\n return this.#sources.replica();\n } catch (e) {\n if (required) {\n throw e;\n }\n // Observational until the flip, so a replica that cannot be read must not\n // stop replication -- but it must not be quiet either, since the flip\n // makes this read the only source.\n this.#lc.warn?.(\n 'failed to derive change-log initialization from the replica',\n e,\n );\n this.#resultCounter.add(1, {result: 'error'});\n return undefined;\n }\n }\n\n #classify(\n pg: InitializationParameters,\n replica: InitializationParameters,\n ): InitComparisonResult {\n if (replica.lastWatermark > pg.lastWatermark) {\n // Transactions reach subscribers before the storer's commit, so a replica\n // read taken while one is in flight can be a transaction ahead. The fold\n // only runs forward, so there is nothing to be said either way.\n return 'inconclusive';\n }\n if (replica.lastWatermark === pg.lastWatermark) {\n return this.#report(pg, replica, replica.cookies, 'equal');\n }\n\n const db = this.#sources.changeLog();\n if (db === undefined || !spansInterval(db, replica.lastWatermark)) {\n return 'inconclusive';\n }\n const changes = readSchemaChanges(\n db,\n replica.lastWatermark,\n pg.lastWatermark,\n );\n if (changes === undefined) {\n return 'inconclusive'; // over MAX_FOLD_SCAN_ROWS\n }\n return this.#report(\n pg,\n replica,\n foldCookies(replica.cookies, changes),\n 'equal-after-fold',\n changes.length,\n );\n }\n\n #report(\n pg: InitializationParameters,\n replica: InitializationParameters,\n derived: CookieSet,\n whenEqual: InitComparisonResult,\n foldedChanges = 0,\n ): InitComparisonResult {\n const expected = canonicalizeCookies(pg.cookies);\n const actual = canonicalizeCookies(derived);\n if (expected === actual) {\n return whenEqual;\n }\n // Both sets are normally empty and are at most a handful of rows, so they\n // are logged whole: a divergence is investigated individually (`P§:10`)\n // and the two renderings are the whole of what there is to look at.\n this.#lc.error?.(\n 'the replica-derived change-log cookies do not match Postgres',\n {\n changeLogInitCompare: {\n pgWatermark: pg.lastWatermark,\n replicaWatermark: replica.lastWatermark,\n foldedChanges,\n expected,\n actual,\n },\n },\n );\n return 'divergent';\n }\n}\n\nconst resumePointOf = ({\n lastWatermark,\n cookies,\n}: InitializationParameters): ChangeLogResumePoint => ({\n resumeWatermark: lastWatermark,\n cookies,\n});\n\n/**\n * Reads the replica's initialization parameters in one snapshot, which is\n * invariant 15 on the replica side: the state version and the cookies are only\n * meaningful together.\n */\nexport function readReplicaInitializationParameters(\n db: Database,\n): InitializationParameters {\n const runner = new StatementRunner(db);\n runner.begin(); // deferred, i.e. one read snapshot for the three statements\n try {\n return {\n lastWatermark: getReplicationState(runner).stateVersion,\n backfillRequests: readBackfillRequests(db),\n cookies: readReplicaCookies(db),\n };\n } finally {\n if (db.inTransaction) {\n runner.rollback();\n }\n }\n}\n\n/**\n * The replica source for {@link ChangeLogInitializer}, opening and closing the\n * file per stream connection rather than holding a handle on a database another\n * process owns.\n *\n * The handle is read-only, which is invariant 19 stated in the API: the\n * replica's cookie tables are written by the change-processor and by nothing\n * else. Note that a read-only SQLite connection still needs to attach the\n * WAL's `-shm`, so this can fail if it opens at a moment when no writer holds\n * the replica; that surfaces as the `error` classification, which is the point\n * of running the path under observation before anything depends on it.\n */\nexport function replicaInitializationSource(\n lc: LogContext,\n replicaFile: string,\n): () => InitializationParameters {\n return () => {\n const db = new Database(\n lc.withContext('component', 'change-log-initializer'),\n replicaFile,\n {readonly: true},\n );\n try {\n return readReplicaInitializationParameters(db);\n } finally {\n db.close();\n }\n };\n}\n\n/**\n * Whether the log holds the complete interval above `fromWatermark`, using the\n * same predicate catchup uses to decide `too-old`: the log both reaches back\n * that far and contains that transaction's `commit` row, so the next row after\n * it is the first change of the interval rather than the middle of a\n * transaction the log only partly holds.\n */\nfunction spansInterval(db: Database, fromWatermark: string): boolean {\n if (!streamTableExists(db)) {\n return false;\n }\n const row = db\n .prepare(/*sql*/ `\n SELECT\n (SELECT min(\"watermark\") FROM \"${CHANGE_LOG_STREAM_TABLE}\")\n AS \"minWatermark\",\n coalesce((${SQLITE_CHANGE_LOG_BOUNDARY_SQL}), 0) AS \"boundaryExists\"\n `)\n .get<{minWatermark: string | null; boundaryExists: number}>({\n fromWatermark,\n });\n return (\n row.minWatermark !== null &&\n fromWatermark >= row.minWatermark &&\n row.boundaryExists === 1\n );\n}\n\nconst SCHEMA_CHANGE_TAG_LIST = schemaChangeTags\n .map(tag => `'${tag}'`)\n .join(', ');\n\n/**\n * The log's schema changes over `(after, through]`, in stream order, or\n * `undefined` if the interval is too large to scan (see\n * {@link MAX_FOLD_SCAN_ROWS}).\n *\n * The tag is stored beside the verbatim change so this scan never parses the\n * payloads of ordinary data changes. The row count is still checked first so a\n * stalled replicator cannot make the range scan unbounded.\n *\n * The count is itself capped, at one row past the cap it is deciding. A bare\n * `count(*)` over the interval is `O(interval)`, so the unbounded interval this\n * exists to decline would still be walked in full before being declined —\n * which is the latency the cap is here to avoid, not a cheaper form of it. The\n * `LIMIT` makes the decision `O(MAX_FOLD_SCAN_ROWS)`, and the comparison is\n * unaffected: the subquery returns the true count whenever it is within the\n * cap, and `cap + 1` whenever it is not.\n */\nfunction readSchemaChanges(\n db: Database,\n after: string,\n through: string,\n): SchemaChange[] | undefined {\n const {rows} = db\n .prepare(/*sql*/ `\n SELECT count(*) AS \"rows\" FROM (\n SELECT 1 FROM \"${CHANGE_LOG_STREAM_TABLE}\"\n WHERE \"watermark\" > ? AND \"watermark\" <= ?\n LIMIT ${MAX_FOLD_SCAN_ROWS + 1}\n )\n `)\n .get<{rows: number}>(after, through);\n if (rows > MAX_FOLD_SCAN_ROWS) {\n return undefined;\n }\n return db\n .prepare(/*sql*/ `\n SELECT \"change\" FROM \"${CHANGE_LOG_STREAM_TABLE}\"\n WHERE \"watermark\" > ? AND \"watermark\" <= ?\n AND \"tag\" IN (${SCHEMA_CHANGE_TAG_LIST})\n ORDER BY \"watermark\", \"pos\"\n `)\n .all<{change: string}>(after, through)\n .map(({change}) => BigIntJSON.parse(change) as SchemaChange);\n}\n\nfunction streamTableExists(db: Database): boolean {\n return (\n db\n .prepare(/*sql*/ `\n SELECT 1 FROM \"sqlite_master\" WHERE \"type\" = 'table' AND \"name\" = ?\n `)\n .get<{1: number} | undefined>(CHANGE_LOG_STREAM_TABLE) !== undefined\n );\n}\n\n/**\n * A canonical rendering of a whole cookie set: rows sorted by primary key,\n * object keys sorted at every depth.\n *\n * Both the equality check and the `divergent` log line fall out of this one\n * value, and neither can be defeated by a difference that is not a difference.\n * Postgres stores the cookie documents as `jsonb`, which does not preserve key\n * order, and the two readers order their rows in SQL in two engines under two\n * collations -- `storer.ts` pins `COLLATE \"C\"` for exactly that reason. Sorting\n * again here means an ordering or key-order regression can never be reported as\n * a divergence, which is a class of false alert that would cost more than it\n * caught.\n */\nfunction canonicalizeCookies(cookies: CookieSet): string {\n return BigIntJSON.stringify({\n tableMetadata: byKey(cookies.tableMetadata, c => [c.schema, c.table]).map(\n c => [c.schema, c.table, sortKeys(c.metadata)],\n ),\n backfilling: byKey(cookies.backfilling, c => [\n c.schema,\n c.table,\n c.column,\n ]).map(c => [c.schema, c.table, c.column, sortKeys(c.backfill)]),\n });\n}\n\nfunction byKey<T>(rows: readonly T[], key: (row: T) => string[]): T[] {\n return rows\n .map(row => [JSON.stringify(key(row)), row] as const)\n .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n .map(([, row]) => row);\n}\n\nfunction sortKeys(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(sortKeys);\n }\n if (value === null || typeof value !== 'object') {\n return value;\n }\n return Object.fromEntries(\n Object.entries(value)\n .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n .map(([k, v]) => [k, sortKeys(v)]),\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAkJA,IAAM,qBAAqB;AAE3B,IAAa,uBAAb,MAAkC;CAChC;CACA;CACA;CACA;CAEA,iBAA0B,mBACxB,WACA,yCACA,2HAEF;CAEA;CAEA,YACE,IACA,EAAC,qBAAqB,mBACtB,SACA;EACA,OACE,uBAAuB,iBACvB,qEACF;EACA,KAAKA,MAAM,GAAG,YAAY,aAAa,wBAAwB;EAC/D,KAAKC,uBAAuB;EAC5B,KAAKC,mBAAmB;EACxB,KAAKC,WAAW;CAClB;;;;;;;CAQA,iBAAmD;EACjD,OAAO,KAAKE;CACd;;;;;;;;CASA,MAAM,aAAgD;EACpD,KAAKA,kBAAkB,KAAA;EACvB,MAAM,KAAK,KAAKJ,uBACZ,MAAM,KAAKE,SAAS,YAAY,IAChC,KAAA;EAGJ,MAAM,UAAU,KAAKD,mBACjB,KAAKI,aAAa,OAAO,KAAA,CAAS,IAClC,KAAA;EAEJ,MAAM,aAAa,KAAKH,SAAS,mBAC/B,MAAM;GAAC,iBAAiB,GAAG;GAAe,SAAS,GAAG;EAAO,SACvD,cAAc,KAAK,SAAS,mCAAmC,CAAC,CACxE;EAEA,IAAI,IAAI;GACN,IAAI,SAIF,KAAKE,kBAAkB,KAAKE,SAAS,IAAI,OAAO;GAElD,OAAO;EACT;EAIA,MAAM,cACJ,cAAc,cAAc,KAAK,SAAS,0BAA0B,CAAC;EACvE,OAAO;GACL,eAAe,YAAY;GAC3B,SAAS,YAAY;GAGrB,kBAAkB,qBAAqB,YAA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","#pgChangeLog","#readReplica","#compare","#restoredReplica","#classify","#report"],"sources":["../../../../../../zero-cache/src/services/change-streamer/change-log-initializer.ts"],"sourcesContent":["/**\n * Where a stream connection's initialization parameters come from.\n *\n * A change-streamer starts a stream from three things read at one position: the\n * watermark to resume at, the {@link BackfillRequest}s to hand the change\n * source, and the cookie set those requests were derived from. Postgres has\n * always supplied all three (`Storer.getStartStreamInitializationParameters()`).\n * The point of this workstream is that a restored replica can supply them\n * instead, which is what lets the `cdc` schema be decommissioned.\n *\n * This is the two-source stage. Both are computed on every stream connection,\n * compared, and Postgres's is used — so the replica-derived path runs in\n * production, at full rate, before anything depends on it. The flip is\n * `initFromPgChangeLog: false`, not a rewrite.\n *\n * The shape is {@link UpstreamAcker}'s, deliberately: two stores, one output, a\n * flag per store, and the Postgres flag carrying the marker that retires it.\n * That class solved the identical problem for upstream ACKs earlier in this\n * workstream.\n *\n * ### Why the comparison is a fold and not an equality\n *\n * The replica is a subscriber downstream of the change-streamer's write loop,\n * so its state version trails the log's head by however long the replicator is\n * behind. A raw difference between the two cookie sets is therefore the normal\n * case and says nothing. What is checked instead is that the replica's set,\n * carried forward over exactly the schema changes the log holds in the interval,\n * equals Postgres's set at Postgres's watermark. The log holds precisely those\n * transactions, schema changes are rare, and the check is exact rather than\n * approximate — so it also happens to be the strongest available test of the\n * premise that three interpreters of one fold agree.\n */\n\nimport type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {BigIntJSON} from '../../../../shared/src/bigint-json.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {Database} from '../../../../zqlite/src/db.ts';\nimport {StatementRunner} from '../../db/statements.ts';\nimport {getOrCreateCounter} from '../../observability/metrics.ts';\nimport type {SchemaChange} from '../change-source/protocol/current/data.ts';\nimport {schemaChangeTags} from '../change-source/protocol/current/schema-change-tags.ts';\nimport type {BackfillRequest} from '../change-source/protocol/current/upstream.ts';\nimport {\n backfillRequestsFrom,\n foldCookies,\n type CookieSet,\n} from '../replicator/change-log-cookies.ts';\nimport {\n CHANGE_LOG_STREAM_TABLE,\n type ChangeLogResumePoint,\n} from '../replicator/change-log-db.ts';\nimport {\n readBackfillRequests,\n readReplicaCookies,\n} from '../replicator/schema/backfilling.ts';\nimport {getReplicationState} from '../replicator/schema/replication-state.ts';\nimport {SQLITE_CHANGE_LOG_BOUNDARY_SQL} from './sqlite-change-log-reader.ts';\n\n/** Everything one stream connection starts from, read at one position. */\nexport type InitializationParameters = {\n readonly lastWatermark: string;\n readonly backfillRequests: BackfillRequest[];\n readonly cookies: CookieSet;\n};\n\n/**\n * - `equal` — the two stores were at the same watermark and held the same\n * cookies. Nothing had to be folded, which is the strongest of the four.\n * - `equal-after-fold` — the replica trailed, and carrying its set forward over\n * the log's schema changes in the interval reproduced Postgres's exactly.\n * The expected steady-state result.\n * - `divergent` — the two disagree at the same position. Alert; investigate\n * individually.\n * - `inconclusive` — the log does not span the interval, so the fold cannot be\n * performed. Routine on a cold log, and not a finding.\n * - `error` — the comparison itself failed: the replica could not be read, or\n * the fold threw. Not one of the four classifications, because it is a\n * statement about this process rather than about the two stores. Kept\n * separate rather than folded into `inconclusive` so that a change-streamer\n * that has silently stopped being able to read its replica is visible before\n * that read becomes the only source.\n */\nexport type InitComparisonResult =\n | 'equal'\n | 'equal-after-fold'\n | 'divergent'\n | 'inconclusive'\n | 'error';\n\nexport type ChangeLogInitializerSources = {\n /** `Storer.getStartStreamInitializationParameters()`. */\n readonly pgChangeLog: () => Promise<InitializationParameters>;\n /** Typically {@link replicaInitializationSource}. */\n readonly replica: () => InitializationParameters;\n /**\n * Reconciles the SQLite change log and returns its current resume point.\n *\n * When `resumeFrom` is present, it supplies the resume point. Otherwise, a\n * valid log uses its own head. A new or invalid log uses `seed`.\n *\n * Returns `undefined` when the writer is disabled or unavailable.\n */\n readonly reconcileChangeLog: (\n resumeFrom: ChangeLogResumePoint | undefined,\n seed: () => ChangeLogResumePoint,\n ) => ChangeLogResumePoint | undefined;\n /**\n * The change log, for the fold. `undefined` before the stream loop's first\n * reconcile has created it, and again once the writer has failed soft and\n * deleted it — both of which make the comparison `inconclusive` rather than\n * an error, since neither says anything about the two stores.\n */\n readonly changeLog: () => Database | undefined;\n /**\n * Re-initializes the Postgres change log from the replica's resume point\n * (taking ownership of it) if it does not contain all of the replica's\n * changes, re-checking under lock. Typically\n * `Storer.reinitializeFromReplica()`.\n *\n * Supplied only when the change log's head is behind the position of the\n * replication slot from which replication resumes, i.e. when it may be\n * missing transactions that the slot will not stream. Otherwise, a replica\n * that is ahead of the change log (as changes are forwarded before they are\n * stored) is caught up by the change stream, which resumes from the change\n * log's head.\n */\n readonly reinitializePgChangeLog?:\n | ((params: {\n watermark: string;\n cookies: CookieSet;\n }) => Promise<'reinitialized' | 'maintained'>)\n | undefined;\n};\n\ntype Opts = {\n /**\n * When true, Postgres supplies the resume point and the cookies for a new\n * change log.\n *\n * When false, a valid log resumes from its own head. The replica supplies the\n * resume point for a new or invalid log.\n */\n // TODO: set false when retiring PG\n readonly initFromPgChangeLog: boolean;\n /**\n * Derive the parameters from the replica. When Postgres is enabled, these\n * parameters are used only for comparison.\n *\n * When Postgres is disabled, the replica supplies the seed and the fallback\n * resume point.\n */\n readonly initFromReplica: boolean;\n};\n\n/**\n * The most change-log rows one comparison will scan.\n *\n * The interval is `(replicaWatermark, pgWatermark]`, so it is normally a\n * handful of transactions — but a replicator that has been stalled can leave it\n * arbitrarily large, and this runs on the stream loop between reconciliation and\n * `startStream`, where latency delays the resumption of replication. Over the\n * cap the comparison declines rather than pays: a chronically lagging replicator\n * shows up as `inconclusive`, which is charted, and not as a stall.\n */\nconst MAX_FOLD_SCAN_ROWS = 10_000;\n\nexport class ChangeLogInitializer {\n readonly #lc: LogContext;\n readonly #initFromPgChangeLog: boolean;\n readonly #initFromReplica: boolean;\n readonly #sources: ChangeLogInitializerSources;\n\n readonly #resultCounter = getOrCreateCounter(\n 'replica',\n 'sqlite_change_log.init_compare_result',\n 'Comparisons of the replica-derived stream initialization parameters ' +\n 'against the Postgres-derived ones, by classification.',\n );\n\n #lastComparison: InitComparisonResult | undefined;\n\n /**\n * The (restored) replica's resume point that the Postgres change log must\n * cover, read once. See {@link #pgChangeLog}.\n */\n #restoredReplica: InitializationParameters | undefined;\n\n constructor(\n lc: LogContext,\n {initFromPgChangeLog, initFromReplica}: Opts,\n sources: ChangeLogInitializerSources,\n ) {\n assert(\n initFromPgChangeLog || initFromReplica,\n `At least one of initFromPgChangeLog or initFromReplica must be true`,\n );\n this.#lc = lc.withContext('component', 'change-log-initializer');\n this.#initFromPgChangeLog = initFromPgChangeLog;\n this.#initFromReplica = initFromReplica;\n this.#sources = sources;\n }\n\n /**\n * Returns the comparison result from the most recent {@link initialize}\n * call. Returns `undefined` when that call used only one source.\n *\n * Tests and metrics use this result. It does not affect initialization.\n */\n lastComparison(): InitComparisonResult | undefined {\n return this.#lastComparison;\n }\n\n /**\n * Returns the initialization parameters for one stream connection after it\n * reconciles the change log.\n *\n * The selected source determines how reconciliation works. Keeping both\n * operations here hides that choice from the caller.\n */\n async initialize(): Promise<InitializationParameters> {\n this.#lastComparison = undefined;\n const pg = this.#initFromPgChangeLog\n ? await this.#pgChangeLog()\n : undefined;\n // The replica is required when Postgres is disabled. It seeds a new log and\n // supplies a resume point when the writer is unavailable.\n const replica = this.#initFromReplica\n ? this.#readReplica(pg === undefined)\n : undefined;\n\n const reconciled = this.#sources.reconcileChangeLog(\n pg && {resumeWatermark: pg.lastWatermark, cookies: pg.cookies},\n () => resumePointOf(must(replica, 'no seed for the SQLite change log')),\n );\n\n if (pg) {\n if (replica) {\n // Compare only after reconciliation. The fold must read the schema\n // changes from the replica watermark through the Postgres watermark.\n // Reconciliation makes sure that the log contains that interval.\n this.#lastComparison = this.#compare(pg, replica);\n }\n return pg;\n }\n\n // Use the reconciled log position. If the writer is unavailable, use the\n // replica position.\n const resumePoint =\n reconciled ?? resumePointOf(must(replica, 'no initialization source'));\n return {\n lastWatermark: resumePoint.resumeWatermark,\n cookies: resumePoint.cookies,\n // Derive the requests from these cookies so that the requests and the\n // watermark describe the same position.\n backfillRequests: backfillRequestsFrom(resumePoint.cookies),\n };\n }\n\n /**\n * Reads the Postgres change log's initialization parameters, first\n * re-initializing it from the replica (taking ownership of it) if it may be\n * missing transactions that the replication slot will not stream, e.g. if it\n * is no longer maintained after running in high-availability mode. This is\n * the Postgres counterpart of seeding a new SQLite change log from the\n * replica.\n *\n * This only applies when the change log's head is behind the slot's position\n * (i.e. when {@link ChangeLogInitializerSources.reinitializePgChangeLog} is\n * supplied), and the change log is only re-initialized if its head is also\n * behind the replica's. Otherwise, it contains every transaction before the\n * slot's position: one that was acked after being stored would be in the\n * change log, and one that was acked after being backed up would be in the\n * replica, which the change log's head covers.\n *\n * The replica is read only once, i.e. as it was restored from the backup,\n * before any stream is started. The live replica is no reference: it is\n * caught up from the forwarded changes, which can be ahead of what has been\n * stored, e.g. by the change log's previous owner. Against that fixed\n * resume point, re-initialization is idempotent: afterwards, the change log's\n * head (which only advances) covers it, so subsequent stream connections\n * leave it untouched.\n */\n async #pgChangeLog(): Promise<InitializationParameters> {\n const pg = await this.#sources.pgChangeLog();\n const reinitialize = this.#sources.reinitializePgChangeLog;\n if (!reinitialize) {\n return pg;\n }\n this.#restoredReplica ??= this.#sources.replica();\n const {lastWatermark, cookies} = this.#restoredReplica;\n if (pg.lastWatermark >= lastWatermark) {\n return pg;\n }\n const result = await reinitialize({watermark: lastWatermark, cookies});\n this.#lc.info?.(\n `Postgres change log at ${pg.lastWatermark} is behind the replica at ` +\n `${lastWatermark}: ${result}`,\n );\n // Either way, the change log has changed since it was read.\n return this.#sources.pgChangeLog();\n }\n\n /**\n * Compares both sources and records the result. Postgres remains\n * authoritative when both sources are enabled.\n */\n #compare(\n pg: InitializationParameters,\n replica: InitializationParameters,\n ): InitComparisonResult {\n let result: InitComparisonResult;\n try {\n result = this.#classify(pg, replica);\n } catch (e) {\n this.#lc.warn?.('failed to compare change-log initialization sources', e);\n result = 'error';\n }\n this.#resultCounter.add(1, {result});\n this.#lc.debug?.('compared change-log initialization sources', {\n changeLogInitCompare: {\n result,\n pgWatermark: pg.lastWatermark,\n replicaWatermark: replica.lastWatermark,\n },\n });\n return result;\n }\n\n #readReplica(required: boolean): InitializationParameters | undefined {\n try {\n return this.#sources.replica();\n } catch (e) {\n if (required) {\n throw e;\n }\n // Observational until the flip, so a replica that cannot be read must not\n // stop replication -- but it must not be quiet either, since the flip\n // makes this read the only source.\n this.#lc.warn?.(\n 'failed to derive change-log initialization from the replica',\n e,\n );\n this.#resultCounter.add(1, {result: 'error'});\n return undefined;\n }\n }\n\n #classify(\n pg: InitializationParameters,\n replica: InitializationParameters,\n ): InitComparisonResult {\n if (replica.lastWatermark > pg.lastWatermark) {\n // Transactions reach subscribers before the storer's commit, so a replica\n // read taken while one is in flight can be a transaction ahead. The fold\n // only runs forward, so there is nothing to be said either way.\n return 'inconclusive';\n }\n if (replica.lastWatermark === pg.lastWatermark) {\n return this.#report(pg, replica, replica.cookies, 'equal');\n }\n\n const db = this.#sources.changeLog();\n if (db === undefined || !spansInterval(db, replica.lastWatermark)) {\n return 'inconclusive';\n }\n const changes = readSchemaChanges(\n db,\n replica.lastWatermark,\n pg.lastWatermark,\n );\n if (changes === undefined) {\n return 'inconclusive'; // over MAX_FOLD_SCAN_ROWS\n }\n return this.#report(\n pg,\n replica,\n foldCookies(replica.cookies, changes),\n 'equal-after-fold',\n changes.length,\n );\n }\n\n #report(\n pg: InitializationParameters,\n replica: InitializationParameters,\n derived: CookieSet,\n whenEqual: InitComparisonResult,\n foldedChanges = 0,\n ): InitComparisonResult {\n const expected = canonicalizeCookies(pg.cookies);\n const actual = canonicalizeCookies(derived);\n if (expected === actual) {\n return whenEqual;\n }\n // Both sets are normally empty and are at most a handful of rows, so they\n // are logged whole: a divergence is investigated individually (`P§:10`)\n // and the two renderings are the whole of what there is to look at.\n this.#lc.error?.(\n 'the replica-derived change-log cookies do not match Postgres',\n {\n changeLogInitCompare: {\n pgWatermark: pg.lastWatermark,\n replicaWatermark: replica.lastWatermark,\n foldedChanges,\n expected,\n actual,\n },\n },\n );\n return 'divergent';\n }\n}\n\nconst resumePointOf = ({\n lastWatermark,\n cookies,\n}: InitializationParameters): ChangeLogResumePoint => ({\n resumeWatermark: lastWatermark,\n cookies,\n});\n\n/**\n * Reads the replica's initialization parameters in one snapshot, which is\n * invariant 15 on the replica side: the state version and the cookies are only\n * meaningful together.\n */\nexport function readReplicaInitializationParameters(\n db: Database,\n): InitializationParameters {\n const runner = new StatementRunner(db);\n runner.begin(); // deferred, i.e. one read snapshot for the three statements\n try {\n return {\n lastWatermark: getReplicationState(runner).stateVersion,\n backfillRequests: readBackfillRequests(db),\n cookies: readReplicaCookies(db),\n };\n } finally {\n if (db.inTransaction) {\n runner.rollback();\n }\n }\n}\n\n/**\n * The replica source for {@link ChangeLogInitializer}, opening and closing the\n * file per stream connection rather than holding a handle on a database another\n * process owns.\n *\n * The handle is read-only, which is invariant 19 stated in the API: the\n * replica's cookie tables are written by the change-processor and by nothing\n * else. Note that a read-only SQLite connection still needs to attach the\n * WAL's `-shm`, so this can fail if it opens at a moment when no writer holds\n * the replica; that surfaces as the `error` classification, which is the point\n * of running the path under observation before anything depends on it.\n */\nexport function replicaInitializationSource(\n lc: LogContext,\n replicaFile: string,\n): () => InitializationParameters {\n return () => {\n const db = new Database(\n lc.withContext('component', 'change-log-initializer'),\n replicaFile,\n {readonly: true},\n );\n try {\n return readReplicaInitializationParameters(db);\n } finally {\n db.close();\n }\n };\n}\n\n/**\n * Whether the log holds the complete interval above `fromWatermark`, using the\n * same predicate catchup uses to decide `too-old`: the log both reaches back\n * that far and contains that transaction's `commit` row, so the next row after\n * it is the first change of the interval rather than the middle of a\n * transaction the log only partly holds.\n */\nfunction spansInterval(db: Database, fromWatermark: string): boolean {\n if (!streamTableExists(db)) {\n return false;\n }\n const row = db\n .prepare(/*sql*/ `\n SELECT\n (SELECT min(\"watermark\") FROM \"${CHANGE_LOG_STREAM_TABLE}\")\n AS \"minWatermark\",\n coalesce((${SQLITE_CHANGE_LOG_BOUNDARY_SQL}), 0) AS \"boundaryExists\"\n `)\n .get<{minWatermark: string | null; boundaryExists: number}>({\n fromWatermark,\n });\n return (\n row.minWatermark !== null &&\n fromWatermark >= row.minWatermark &&\n row.boundaryExists === 1\n );\n}\n\nconst SCHEMA_CHANGE_TAG_LIST = schemaChangeTags\n .map(tag => `'${tag}'`)\n .join(', ');\n\n/**\n * The log's schema changes over `(after, through]`, in stream order, or\n * `undefined` if the interval is too large to scan (see\n * {@link MAX_FOLD_SCAN_ROWS}).\n *\n * The tag is stored beside the verbatim change so this scan never parses the\n * payloads of ordinary data changes. The row count is still checked first so a\n * stalled replicator cannot make the range scan unbounded.\n *\n * The count is itself capped, at one row past the cap it is deciding. A bare\n * `count(*)` over the interval is `O(interval)`, so the unbounded interval this\n * exists to decline would still be walked in full before being declined —\n * which is the latency the cap is here to avoid, not a cheaper form of it. The\n * `LIMIT` makes the decision `O(MAX_FOLD_SCAN_ROWS)`, and the comparison is\n * unaffected: the subquery returns the true count whenever it is within the\n * cap, and `cap + 1` whenever it is not.\n */\nfunction readSchemaChanges(\n db: Database,\n after: string,\n through: string,\n): SchemaChange[] | undefined {\n const {rows} = db\n .prepare(/*sql*/ `\n SELECT count(*) AS \"rows\" FROM (\n SELECT 1 FROM \"${CHANGE_LOG_STREAM_TABLE}\"\n WHERE \"watermark\" > ? AND \"watermark\" <= ?\n LIMIT ${MAX_FOLD_SCAN_ROWS + 1}\n )\n `)\n .get<{rows: number}>(after, through);\n if (rows > MAX_FOLD_SCAN_ROWS) {\n return undefined;\n }\n return db\n .prepare(/*sql*/ `\n SELECT \"change\" FROM \"${CHANGE_LOG_STREAM_TABLE}\"\n WHERE \"watermark\" > ? AND \"watermark\" <= ?\n AND \"tag\" IN (${SCHEMA_CHANGE_TAG_LIST})\n ORDER BY \"watermark\", \"pos\"\n `)\n .all<{change: string}>(after, through)\n .map(({change}) => BigIntJSON.parse(change) as SchemaChange);\n}\n\nfunction streamTableExists(db: Database): boolean {\n return (\n db\n .prepare(/*sql*/ `\n SELECT 1 FROM \"sqlite_master\" WHERE \"type\" = 'table' AND \"name\" = ?\n `)\n .get<{1: number} | undefined>(CHANGE_LOG_STREAM_TABLE) !== undefined\n );\n}\n\n/**\n * A canonical rendering of a whole cookie set: rows sorted by primary key,\n * object keys sorted at every depth.\n *\n * Both the equality check and the `divergent` log line fall out of this one\n * value, and neither can be defeated by a difference that is not a difference.\n * Postgres stores the cookie documents as `jsonb`, which does not preserve key\n * order, and the two readers order their rows in SQL in two engines under two\n * collations -- `storer.ts` pins `COLLATE \"C\"` for exactly that reason. Sorting\n * again here means an ordering or key-order regression can never be reported as\n * a divergence, which is a class of false alert that would cost more than it\n * caught.\n */\nfunction canonicalizeCookies(cookies: CookieSet): string {\n return BigIntJSON.stringify({\n tableMetadata: byKey(cookies.tableMetadata, c => [c.schema, c.table]).map(\n c => [c.schema, c.table, sortKeys(c.metadata)],\n ),\n backfilling: byKey(cookies.backfilling, c => [\n c.schema,\n c.table,\n c.column,\n ]).map(c => [c.schema, c.table, c.column, sortKeys(c.backfill)]),\n });\n}\n\nfunction byKey<T>(rows: readonly T[], key: (row: T) => string[]): T[] {\n return rows\n .map(row => [JSON.stringify(key(row)), row] as const)\n .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n .map(([, row]) => row);\n}\n\nfunction sortKeys(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(sortKeys);\n }\n if (value === null || typeof value !== 'object') {\n return value;\n }\n return Object.fromEntries(\n Object.entries(value)\n .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n .map(([k, v]) => [k, sortKeys(v)]),\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAqKA,IAAM,qBAAqB;AAE3B,IAAa,uBAAb,MAAkC;CAChC;CACA;CACA;CACA;CAEA,iBAA0B,mBACxB,WACA,yCACA,2HAEF;CAE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.
78
78
  */
79
79
  pgChangeLogEnabled: boolean;
80
+ /**
81
+ * Whether the PG change log's head was behind the position of the
82
+ * replication slot from which replication resumes, in which case the
83
+ * change log is re-initialized from the (restored) replica if it does not
84
+ * contain all of the replica's changes (see `PgChangeLogPurgeLocker`).
85
+ */
86
+ pgChangeLogBehindSlot?: boolean | undefined;
80
87
  flowControlConsensusTimeoutProportion: number;
81
88
  flowControlSlowSubscriberGracePeriodMs?: number | undefined;
82
89
  sqliteCatchup?: SQLiteCatchupOptions | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"change-streamer-service.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/change-streamer-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAcjD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,uBAAuB,CAAC;AAGnD,OAAO,KAAK,EACV,YAAY,EAEb,MAAM,mCAAmC,CAAC;AAM3C,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAGL,KAAK,0BAA0B,EAChC,MAAM,qCAAqC,CAAC;AAC7C,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,2CAA2C,CAAC;AAajF,OAAO,EACL,KAAK,qBAAqB,EAE1B,KAAK,iBAAiB,EAEvB,MAAM,sBAAsB,CAAC;AAU9B,OAAO,EAEL,KAAK,2BAA2B,EACjC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAEL,KAAK,6BAA6B,EACnC,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAGL,KAAK,oCAAoC,EAC1C,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAIL,KAAK,uBAAuB,EAC7B,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAEL,KAAK,4BAA4B,EAClC,MAAM,+BAA+B,CAAC;AAEv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AAC;IAC5B,qCAAqC,EAAE,MAAM,CAAC;IAC9C,sCAAsC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5D,aAAa,CAAC,EAAE,oBAAoB,GAAG,SAAS,CAAC;IACjD;;;OAGG;IACH,qBAAqB,CAAC,EAClB,IAAI,CACF,4BAA4B,EAC5B,UAAU,GAAG,YAAY,GAAG,WAAW,CACxC,GACD,SAAS,CAAC;IACd;;;;;;;OAOG;IACH,oBAAoB,CAAC,EAAE,oCAAoC,GAAG,SAAS,CAAC;IACxE;;;;;;OAMG;IACH,sBAAsB,CAAC,EACnB,CAAC,6BAA6B,GAAG;QAAC,WAAW,EAAE,MAAM,CAAA;KAAC,CAAC,GACvD,SAAS,CAAC;IACd,+EAA+E;IAC/E,oBAAoB,CAAC,EAAE,2BAA2B,GAAG,SAAS,CAAC;CAChE,CAAC;AAEF;;GAEG;AACH,wBAAsB,kBAAkB,CACtC,EAAE,EAAE,UAAU,EACd,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,MAAM,EACxB,iBAAiB,EAAE,MAAM,EACzB,QAAQ,EAAE,UAAU,EACpB,YAAY,EAAE,YAAY,EAC1B,0BAA0B,EAAE,0BAA0B,EACtD,iBAAiB,EAAE,iBAAiB,EACpC,YAAY,EAAE,YAAY,GAAG,IAAI,EACjC,SAAS,EAAE,SAAS,GAAG,IAAI,EAC3B,SAAS,EAAE,OAAO,EAClB,IAAI,EAAE,aAAa,EACnB,YAAY,oBAAa,GACxB,OAAO,CAAC,qBAAqB,CAAC,CAiDhC"}
1
+ {"version":3,"file":"change-streamer-service.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/change-streamer-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAcjD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAC,OAAO,EAA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