@rocicorp/zero 1.11.0-canary.25 → 1.11.0-canary.26

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 (117) hide show
  1. package/out/shared/src/hash.d.ts.map +1 -1
  2. package/out/shared/src/hash.js +32 -7
  3. package/out/shared/src/hash.js.map +1 -1
  4. package/out/zero/package.js +1 -1
  5. package/out/zero/package.js.map +1 -1
  6. package/out/zero-cache/src/config/zero-config.d.ts +13 -0
  7. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  8. package/out/zero-cache/src/config/zero-config.js +52 -12
  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 +13 -12
  12. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  13. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  14. package/out/zero-cache/src/server/replicator.js +18 -8
  15. package/out/zero-cache/src/server/replicator.js.map +1 -1
  16. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +1 -1
  17. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -1
  18. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +1 -1
  19. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  20. package/out/zero-cache/src/services/change-source/pg/change-source.js +19 -11
  21. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  22. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts +8 -4
  23. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  24. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +20 -8
  25. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  26. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts.map +1 -1
  27. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js +2 -2
  28. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js.map +1 -1
  29. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  30. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +10 -9
  31. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  32. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +26 -1
  33. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  34. package/out/zero-cache/src/services/change-streamer/subscriber.js +47 -12
  35. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  36. package/out/zero-cache/src/services/http-service.d.ts +1 -1
  37. package/out/zero-cache/src/services/http-service.d.ts.map +1 -1
  38. package/out/zero-cache/src/services/http-service.js.map +1 -1
  39. package/out/zero-cache/src/services/litestream/commands.d.ts +6 -0
  40. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  41. package/out/zero-cache/src/services/litestream/commands.js +15 -6
  42. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  43. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts +3 -1
  44. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  45. package/out/zero-cache/src/services/replicator/incremental-sync.js +5 -2
  46. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  47. package/out/zero-cache/src/services/replicator/replicator.d.ts +3 -1
  48. package/out/zero-cache/src/services/replicator/replicator.d.ts.map +1 -1
  49. package/out/zero-cache/src/services/replicator/replicator.js +5 -2
  50. package/out/zero-cache/src/services/replicator/replicator.js.map +1 -1
  51. package/out/zero-cache/src/services/replicator/reporter/recorder.d.ts +16 -1
  52. package/out/zero-cache/src/services/replicator/reporter/recorder.d.ts.map +1 -1
  53. package/out/zero-cache/src/services/replicator/reporter/recorder.js +70 -27
  54. package/out/zero-cache/src/services/replicator/reporter/recorder.js.map +1 -1
  55. package/out/zero-cache/src/services/run-ast.d.ts.map +1 -1
  56. package/out/zero-cache/src/services/run-ast.js +3 -2
  57. package/out/zero-cache/src/services/run-ast.js.map +1 -1
  58. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  59. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +21 -4
  60. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  61. package/out/zero-cache/src/types/row-key.d.ts +1 -15
  62. package/out/zero-cache/src/types/row-key.d.ts.map +1 -1
  63. package/out/zero-cache/src/types/row-key.js +1 -3
  64. package/out/zero-cache/src/types/row-key.js.map +1 -1
  65. package/out/zero-client/src/client/custom.d.ts +5 -4
  66. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  67. package/out/zero-client/src/client/custom.js +1 -1
  68. package/out/zero-client/src/client/custom.js.map +1 -1
  69. package/out/zero-client/src/client/keys.d.ts.map +1 -1
  70. package/out/zero-client/src/client/keys.js +19 -1
  71. package/out/zero-client/src/client/keys.js.map +1 -1
  72. package/out/zero-client/src/client/make-mutate-property.d.ts +1 -1
  73. package/out/zero-client/src/client/make-mutate-property.d.ts.map +1 -1
  74. package/out/zero-client/src/client/make-mutate-property.js.map +1 -1
  75. package/out/zero-client/src/client/make-replicache-mutators.d.ts.map +1 -1
  76. package/out/zero-client/src/client/make-replicache-mutators.js +1 -1
  77. package/out/zero-client/src/client/make-replicache-mutators.js.map +1 -1
  78. package/out/zero-client/src/client/mutation-tracker.d.ts +2 -2
  79. package/out/zero-client/src/client/mutation-tracker.d.ts.map +1 -1
  80. package/out/zero-client/src/client/mutation-tracker.js +2 -6
  81. package/out/zero-client/src/client/mutation-tracker.js.map +1 -1
  82. package/out/zero-client/src/client/mutator-proxy.js +4 -2
  83. package/out/zero-client/src/client/mutator-proxy.js.map +1 -1
  84. package/out/zero-client/src/client/version.js +1 -1
  85. package/out/zero-client/src/mod.d.ts +1 -1
  86. package/out/zero-client/src/mod.d.ts.map +1 -1
  87. package/out/zero-server/src/closable-db-transaction.d.ts +16 -0
  88. package/out/zero-server/src/closable-db-transaction.d.ts.map +1 -0
  89. package/out/zero-server/src/closable-db-transaction.js +31 -0
  90. package/out/zero-server/src/closable-db-transaction.js.map +1 -0
  91. package/out/zero-server/src/custom.d.ts +6 -4
  92. package/out/zero-server/src/custom.d.ts.map +1 -1
  93. package/out/zero-server/src/custom.js +9 -3
  94. package/out/zero-server/src/custom.js.map +1 -1
  95. package/out/zero-server/src/mod.d.ts +1 -1
  96. package/out/zero-server/src/mod.d.ts.map +1 -1
  97. package/out/zero-server/src/process-mutations.d.ts +13 -1
  98. package/out/zero-server/src/process-mutations.d.ts.map +1 -1
  99. package/out/zero-server/src/process-mutations.js +90 -44
  100. package/out/zero-server/src/process-mutations.js.map +1 -1
  101. package/out/zero-server/src/push-processor.js.map +1 -1
  102. package/out/zero-server/src/zql-database.d.ts.map +1 -1
  103. package/out/zero-server/src/zql-database.js +28 -22
  104. package/out/zero-server/src/zql-database.js.map +1 -1
  105. package/out/zql/src/mutate/custom.d.ts +29 -0
  106. package/out/zql/src/mutate/custom.d.ts.map +1 -1
  107. package/out/zql/src/mutate/custom.js.map +1 -1
  108. package/out/zql/src/mutate/mutator-registry.js +1 -1
  109. package/out/zql/src/mutate/mutator-registry.js.map +1 -1
  110. package/out/zql/src/mutate/mutator.d.ts +2 -2
  111. package/out/zql/src/mutate/mutator.d.ts.map +1 -1
  112. package/out/zql/src/mutate/mutator.js.map +1 -1
  113. package/out/zqlite/src/resolve-scalar-subqueries.d.ts +3 -1
  114. package/out/zqlite/src/resolve-scalar-subqueries.d.ts.map +1 -1
  115. package/out/zqlite/src/resolve-scalar-subqueries.js +1 -0
  116. package/out/zqlite/src/resolve-scalar-subqueries.js.map +1 -1
  117. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"incremental-sync.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/replicator/incremental-sync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAKjD,OAAO,KAAK,EAAQ,MAAM,EAAC,MAAM,wBAAwB,CAAC;AAG1D,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,4CAA4C,CAAC;AACpF,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,uCAAuC,CAAC;AAM/C,OAAO,EAEL,KAAK,0BAA0B,EAChC,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAC,YAAY,EAAE,cAAc,EAAC,MAAM,iBAAiB,CAAC;AAGlE,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAOhE;;;;;;GAMG;AACH,qBAAa,iBAAiB;;IAsB5B,YACE,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,cAAc,EAAE,cAAc,EAC9B,MAAM,EAAE,iBAAiB,EACzB,IAAI,EAAE,cAAc,EACpB,eAAe,EAAE,0BAA0B,GAAG,IAAI,EAClD,iBAAiB,CAAC,EAAE,mBAAmB,EAYxC;IAEK,GAAG,kBA2OR;IA6BD,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,CAEhC;IAED,IAAI,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,OAAO,QAIjC;CACF"}
1
+ {"version":3,"file":"incremental-sync.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/replicator/incremental-sync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAKjD,OAAO,KAAK,EAAQ,MAAM,EAAC,MAAM,wBAAwB,CAAC;AAG1D,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,4CAA4C,CAAC;AACpF,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,uCAAuC,CAAC;AAM/C,OAAO,EAEL,KAAK,0BAA0B,EAChC,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAC,YAAY,EAAE,cAAc,EAAC,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAEL,KAAK,8BAA8B,EACpC,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAOhE;;;;;;GAMG;AACH,qBAAa,iBAAiB;;IAsB5B,YACE,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,cAAc,EAAE,cAAc,EAC9B,MAAM,EAAE,iBAAiB,EACzB,IAAI,EAAE,cAAc,EACpB,eAAe,EAAE,0BAA0B,GAAG,IAAI,EAClD,8BAA8B,GAAE,8BAAmC,EACnE,iBAAiB,CAAC,EAAE,mBAAmB,EAexC;IAED,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;IAEK,GAAG,kBA2OR;IA6BD,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,CAEhC;IAED,IAAI,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,OAAO,QAIjC;CACF"}
@@ -29,7 +29,7 @@ var IncrementalSyncer = class {
29
29
  #initialConnection;
30
30
  #state = new RunningState("IncrementalSyncer");
31
31
  #replicationEvents = getOrCreateCounter("replication", "events", "Number of replication events processed");
32
- constructor(lc, taskID, id, changeStreamer, worker, mode, statusPublisher, initialConnection) {
32
+ constructor(lc, taskID, id, changeStreamer, worker, mode, statusPublisher, replicationLagReadinessOptions = {}, initialConnection) {
33
33
  this.#lc = lc;
34
34
  this.#taskID = taskID;
35
35
  this.#id = id;
@@ -38,9 +38,12 @@ var IncrementalSyncer = class {
38
38
  this.#mode = mode;
39
39
  this.#statusPublisher = statusPublisher;
40
40
  this.#notifier = new Notifier();
41
- this.#reporter = new ReplicationReportRecorder(lc);
41
+ this.#reporter = new ReplicationReportRecorder(lc, replicationLagReadinessOptions);
42
42
  this.#initialConnection = initialConnection;
43
43
  }
44
+ ready() {
45
+ return this.#reporter.ready();
46
+ }
44
47
  async run() {
45
48
  const lc = this.#lc;
46
49
  let workerError;
@@ -1 +1 @@
1
- {"version":3,"file":"incremental-sync.js","names":["#lc","#taskID","#id","#changeStreamer","#worker","#mode","#statusPublisher","#notifier","#reporter","#state","#replicationEvents","#initialConnection","#handleResult"],"sources":["../../../../../../zero-cache/src/services/replicator/incremental-sync.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {AbortError} from '../../../../shared/src/abort-error.ts';\nimport type {Enum} from '../../../../shared/src/enum.ts';\nimport {mapPostgresToLiteIndex} from '../../db/pg-to-lite.ts';\nimport {getOrCreateCounter} from '../../observability/metrics.ts';\nimport type {Sized, Source} from '../../types/streams.ts';\nimport type {DownloadStatus} from '../change-source/protocol/current.ts';\nimport type {ChangeStreamData} from '../change-source/protocol/current/downstream.ts';\nimport type {ReservationFollowup} from '../change-streamer/change-streamer-http.ts';\nimport {\n errorTypeToReadableName,\n PROTOCOL_VERSION,\n type ChangeStreamer,\n} from '../change-streamer/change-streamer.ts';\nimport type * as ErrorType from '../change-streamer/error-type-enum.ts';\nimport type {SubscribeDownstream} from '../change-streamer/subscribe.ts';\nimport {RunningState} from '../running-state.ts';\nimport type {CommitResult} from './change-processor.ts';\nimport {Notifier} from './notifier.ts';\nimport {\n IndexingProgress,\n type ReplicationStatusPublisher,\n} from './replication-status.ts';\nimport type {ReplicaState, ReplicatorMode} from './replicator.ts';\nimport {ReplicationReportRecorder} from './reporter/recorder.ts';\nimport type {ReplicationReport} from './reporter/report-schema.ts';\nimport type {WriteWorkerClient} from './write-worker-client.ts';\n\ntype ErrorType = Enum<typeof ErrorType>;\n\nconst MAX_WORKER_BATCH_MESSAGES = 256;\nconst MAX_WORKER_BATCH_SIZE = 1024 * 1024;\n\n/**\n * The {@link IncrementalSyncer} manages a logical replication stream from upstream,\n * handling application lifecycle events (start, stop) and retrying the\n * connection with exponential backoff. The actual handling of the logical\n * replication messages is done by the {@link ChangeProcessor}, which runs\n * in a worker thread via the {@link WriteWorkerClient}.\n */\nexport class IncrementalSyncer {\n readonly #lc: LogContext;\n readonly #taskID: string;\n readonly #id: string;\n readonly #changeStreamer: ChangeStreamer;\n readonly #worker: WriteWorkerClient;\n readonly #mode: ReplicatorMode;\n readonly #statusPublisher: ReplicationStatusPublisher | null;\n readonly #notifier: Notifier;\n readonly #reporter: ReplicationReportRecorder;\n // Consumed (and cleared) by the first subscribe() in run()'s loop; every\n // subsequent (re)connection goes through #changeStreamer.subscribe().\n #initialConnection: ReservationFollowup | undefined;\n\n readonly #state = new RunningState('IncrementalSyncer');\n\n readonly #replicationEvents = getOrCreateCounter(\n 'replication',\n 'events',\n 'Number of replication events processed',\n );\n\n constructor(\n lc: LogContext,\n taskID: string,\n id: string,\n changeStreamer: ChangeStreamer,\n worker: WriteWorkerClient,\n mode: ReplicatorMode,\n statusPublisher: ReplicationStatusPublisher | null,\n initialConnection?: ReservationFollowup,\n ) {\n this.#lc = lc;\n this.#taskID = taskID;\n this.#id = id;\n this.#changeStreamer = changeStreamer;\n this.#worker = worker;\n this.#mode = mode;\n this.#statusPublisher = statusPublisher;\n this.#notifier = new Notifier();\n this.#reporter = new ReplicationReportRecorder(lc);\n this.#initialConnection = initialConnection;\n }\n\n async run() {\n const lc = this.#lc;\n let workerError: Error | undefined;\n this.#worker.onError(err => {\n workerError ??= err;\n this.#state.stop(lc, err);\n });\n lc.info?.(`Starting IncrementalSyncer`);\n const {watermark: initialWatermark} =\n await this.#worker.getSubscriptionState();\n\n // Notify any waiting subscribers that the replica is ready to be read.\n // This initial notification intentionally omits replicaReadyTimeMs because\n // it represents already-current state, not newly-unserved work.\n void this.#notifier.notifySubscribers({\n state: 'version-ready',\n watermark: initialWatermark,\n });\n\n while (this.#state.shouldRun()) {\n const {replicaVersion, watermark, backfills} =\n await this.#worker.getSubscriptionState();\n\n let downstream: Source<Sized<SubscribeDownstream>> | undefined;\n let unregister = () => {};\n let err: unknown | undefined;\n\n try {\n const ctx = {\n protocolVersion: PROTOCOL_VERSION,\n taskID: this.#taskID,\n id: this.#id,\n mode: this.#mode,\n watermark,\n replicaVersion,\n backfills,\n };\n // The first connection reuses the still-open reservation connection\n // (if any) from the backup restore, so the replication-manager that\n // reserved the change log is the one that serves the subscription.\n // Every reconnect after that opens a fresh connection.\n const initialConnection = this.#initialConnection;\n this.#initialConnection = undefined;\n downstream = initialConnection\n ? await initialConnection.subscribe(ctx)\n : await this.#changeStreamer.subscribe(ctx);\n unregister = this.#state.cancelOnStop(downstream);\n this.#statusPublisher?.publish(\n lc,\n 'Replicating',\n `Replicating from ${watermark}`,\n );\n\n let backfill:\n | {status: DownloadStatus; table: string; columns: string[]}\n | undefined;\n let writeBatch: ChangeStreamData[] = [];\n let writeBatchSize = 0;\n let inTransaction = false;\n // Indexes created in the current run of consecutive index creations.\n let indexing: IndexingProgress | undefined;\n\n const publishBackfillStatus = (table: string) =>\n this.#statusPublisher?.publish(\n lc,\n 'Replicating',\n `Backfilling ${table} table`,\n 3000,\n () =>\n backfill\n ? {\n downloadStatus: [\n {\n ...backfill.status,\n table: backfill.table,\n columns: backfill.columns,\n },\n ],\n }\n : {},\n );\n\n const flushWrites = async () => {\n if (writeBatch.length === 0) {\n return;\n }\n const batch = writeBatch;\n writeBatch = [];\n writeBatchSize = 0;\n\n const result = await this.#worker.processMessages(batch);\n this.#handleResult(lc, result);\n if (result?.completedBackfill) {\n backfill = undefined;\n }\n };\n\n for await (const {data: message, size} of downstream) {\n // Only reset backoff after receiving an actual message.\n this.#state.resetBackoff();\n\n this.#replicationEvents.add(1);\n switch (message[0]) {\n case 'reserved':\n lc.warn?.(`unexpected snapshot reservation message received`, {\n message: message[0],\n });\n break;\n case 'status': {\n const {lagReport} = message[1];\n if (lagReport) {\n const report: ReplicationReport = {\n nextSendTimeMs: lagReport.nextSendTimeMs,\n };\n if (lagReport.lastTimings) {\n report.lastTimings = {\n ...lagReport.lastTimings,\n replicateTimeMs: Date.now(),\n };\n }\n this.#reporter.record(report);\n }\n break;\n }\n case 'error': {\n // Signal from the replication-manager that the view-syncer must\n // shut down and restore a new backup from litestream.\n const {type, message: msg} = message[1];\n this.stop(\n lc,\n // Note: The AbortError indicates a clean / intentional shutdown.\n new AbortError(\n `${errorTypeToReadableName(type as ErrorType)}: ${msg}`,\n ),\n );\n break;\n }\n default: {\n const msg = message[1];\n if (msg.tag === 'backfill' && msg.status) {\n const {status, relation} = msg;\n const first = !backfill;\n backfill = {\n status, // Update the current status\n table: relation.name,\n columns: [...relation.rowKey.columns, ...msg.columns],\n };\n if (first) {\n // Start publishing the status every 3 seconds.\n publishBackfillStatus(relation.name);\n }\n }\n\n if (msg.tag === 'create-index' && this.#statusPublisher) {\n // Creating an index on an existing table can take a long\n // time. Flush preceding changes so that the index creation\n // is processed (and timed) on its own.\n await flushWrites();\n const index = mapPostgresToLiteIndex(msg.spec);\n indexing ??= new IndexingProgress();\n indexing.start(index);\n this.#statusPublisher.publish(\n lc,\n 'Replicating',\n `Creating index ${index.name} on ${index.tableName}`,\n 3000,\n indexing.state,\n );\n // Exclude the time spent publishing the status.\n indexing.restartTimer();\n writeBatch.push(message as ChangeStreamData);\n writeBatchSize += size;\n await flushWrites();\n const elapsed = indexing.finish();\n lc.info?.(\n `Created index ${index.name} (${elapsed.toFixed(3)} ms)`,\n );\n this.#statusPublisher.publish(\n lc,\n 'Replicating',\n `Created index ${index.name} on ${index.tableName}`,\n 0,\n indexing.state,\n );\n if (backfill) {\n // Resume reporting the progress of the ongoing backfill.\n publishBackfillStatus(backfill.table);\n }\n break;\n }\n indexing = undefined;\n\n const type = message[0];\n const invalidTransactionSequence =\n type === 'begin' ? inTransaction : !inTransaction;\n if (type === 'begin') {\n inTransaction = true;\n } else if (type === 'commit' || type === 'rollback') {\n inTransaction = false;\n }\n\n writeBatch.push(message as ChangeStreamData);\n writeBatchSize += size;\n if (\n // Waiting here ensures that consuming the commit, which ACKs\n // it upstream, only happens after the SQLite commit finishes.\n type === 'commit' ||\n type === 'rollback' ||\n // Promptly surface malformed transaction sequences instead\n // of leaving a partial batch waiting for another message.\n invalidTransactionSequence ||\n writeBatch.length >= MAX_WORKER_BATCH_MESSAGES ||\n writeBatchSize >= MAX_WORKER_BATCH_SIZE\n ) {\n await flushWrites();\n }\n break;\n }\n }\n }\n this.#worker.abort();\n } catch (e) {\n err = e;\n this.#worker.abort();\n } finally {\n downstream?.cancel();\n unregister();\n this.#statusPublisher?.stop();\n }\n await this.#state.backoff(lc, err);\n }\n lc.info?.('IncrementalSyncer stopped');\n if (workerError) {\n throw workerError;\n }\n }\n\n #handleResult(lc: LogContext, result: CommitResult | null) {\n if (!result) {\n return;\n }\n if (result.completedBackfill) {\n // Publish the final status\n const status = result.completedBackfill;\n this.#statusPublisher?.publish(\n lc,\n 'Replicating',\n `Backfilled ${status.table} table`,\n 0,\n () => ({downloadStatus: [status]}),\n );\n } else if (result.schemaUpdated) {\n this.#statusPublisher?.publish(lc, 'Replicating', 'Schema updated');\n }\n if (result.watermark && result.changeLogUpdated) {\n void this.#notifier.notifySubscribers({\n state: 'version-ready',\n watermark: result.watermark,\n replicaReadyTimeMs: Date.now(),\n upstreamCommitTimeMs: result.upstreamCommitTimeMs,\n });\n }\n }\n\n subscribe(): Source<ReplicaState> {\n return this.#notifier.subscribe();\n }\n\n stop(lc: LogContext, err?: unknown) {\n this.#state.stop(lc, err);\n // Abort any polling loop that the write worker may be in.\n this.#worker.abort();\n }\n}\n"],"mappings":";;;;;;;;;AA8BA,IAAM,4BAA4B;AAClC,IAAM,wBAAwB,OAAO;;;;;;;;AASrC,IAAa,oBAAb,MAA+B;CAC7B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAGA;CAEA,SAAkB,IAAI,aAAa,mBAAmB;CAEtD,qBAA8B,mBAC5B,eACA,UACA,wCACF;CAEA,YACE,IACA,QACA,IACA,gBACA,QACA,MACA,iBACA,mBACA;EACA,KAAKA,MAAM;EACX,KAAKC,UAAU;EACf,KAAKC,MAAM;EACX,KAAKC,kBAAkB;EACvB,KAAKC,UAAU;EACf,KAAKC,QAAQ;EACb,KAAKC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AO,eAChB,KAAKA,kBAAkB,QAAQ,IAAI,eAAe,gBAAgB;EAEpE,IAAI,OAAO,aAAa,OAAO,kBAC7B,KAAUC,UAAU,kBAAkB;GACpC,OAAO;GACP,WAAW,OAAO;GAClB,oBAAoB,KAAK,IAAI;GAC7B,sBAAsB,OAAO;EAC/B,CAAC;CAEL;CAEA,YAAkC;EAChC,OAAO,KAAKA,UAAU,UAAU;CAClC;CAEA,KAAK,IAAgB,KAAe;EAClC,KAAKE,OAAO,KAAK,IAAI,GAAG;EAExB,KAAKL,QAAQ,MAAM;CACrB;AACF"}
1
+ {"version":3,"file":"incremental-sync.js","names":["#lc","#taskID","#id","#changeStreamer","#worker","#mode","#statusPublisher","#notifier","#reporter","#state","#replicationEvents","#initialConnection","#handleResult"],"sources":["../../../../../../zero-cache/src/services/replicator/incremental-sync.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {AbortError} from '../../../../shared/src/abort-error.ts';\nimport type {Enum} from '../../../../shared/src/enum.ts';\nimport {mapPostgresToLiteIndex} from '../../db/pg-to-lite.ts';\nimport {getOrCreateCounter} from '../../observability/metrics.ts';\nimport type {Sized, Source} from '../../types/streams.ts';\nimport type {DownloadStatus} from '../change-source/protocol/current.ts';\nimport type {ChangeStreamData} from '../change-source/protocol/current/downstream.ts';\nimport type {ReservationFollowup} from '../change-streamer/change-streamer-http.ts';\nimport {\n errorTypeToReadableName,\n PROTOCOL_VERSION,\n type ChangeStreamer,\n} from '../change-streamer/change-streamer.ts';\nimport type * as ErrorType from '../change-streamer/error-type-enum.ts';\nimport type {SubscribeDownstream} from '../change-streamer/subscribe.ts';\nimport {RunningState} from '../running-state.ts';\nimport type {CommitResult} from './change-processor.ts';\nimport {Notifier} from './notifier.ts';\nimport {\n IndexingProgress,\n type ReplicationStatusPublisher,\n} from './replication-status.ts';\nimport type {ReplicaState, ReplicatorMode} from './replicator.ts';\nimport {\n ReplicationReportRecorder,\n type ReplicationLagReadinessOptions,\n} from './reporter/recorder.ts';\nimport type {ReplicationReport} from './reporter/report-schema.ts';\nimport type {WriteWorkerClient} from './write-worker-client.ts';\n\ntype ErrorType = Enum<typeof ErrorType>;\n\nconst MAX_WORKER_BATCH_MESSAGES = 256;\nconst MAX_WORKER_BATCH_SIZE = 1024 * 1024;\n\n/**\n * The {@link IncrementalSyncer} manages a logical replication stream from upstream,\n * handling application lifecycle events (start, stop) and retrying the\n * connection with exponential backoff. The actual handling of the logical\n * replication messages is done by the {@link ChangeProcessor}, which runs\n * in a worker thread via the {@link WriteWorkerClient}.\n */\nexport class IncrementalSyncer {\n readonly #lc: LogContext;\n readonly #taskID: string;\n readonly #id: string;\n readonly #changeStreamer: ChangeStreamer;\n readonly #worker: WriteWorkerClient;\n readonly #mode: ReplicatorMode;\n readonly #statusPublisher: ReplicationStatusPublisher | null;\n readonly #notifier: Notifier;\n readonly #reporter: ReplicationReportRecorder;\n // Consumed (and cleared) by the first subscribe() in run()'s loop; every\n // subsequent (re)connection goes through #changeStreamer.subscribe().\n #initialConnection: ReservationFollowup | undefined;\n\n readonly #state = new RunningState('IncrementalSyncer');\n\n readonly #replicationEvents = getOrCreateCounter(\n 'replication',\n 'events',\n 'Number of replication events processed',\n );\n\n constructor(\n lc: LogContext,\n taskID: string,\n id: string,\n changeStreamer: ChangeStreamer,\n worker: WriteWorkerClient,\n mode: ReplicatorMode,\n statusPublisher: ReplicationStatusPublisher | null,\n replicationLagReadinessOptions: ReplicationLagReadinessOptions = {},\n initialConnection?: ReservationFollowup,\n ) {\n this.#lc = lc;\n this.#taskID = taskID;\n this.#id = id;\n this.#changeStreamer = changeStreamer;\n this.#worker = worker;\n this.#mode = mode;\n this.#statusPublisher = statusPublisher;\n this.#notifier = new Notifier();\n this.#reporter = new ReplicationReportRecorder(\n lc,\n replicationLagReadinessOptions,\n );\n this.#initialConnection = initialConnection;\n }\n\n ready(): Promise<void> {\n return this.#reporter.ready();\n }\n\n async run() {\n const lc = this.#lc;\n let workerError: Error | undefined;\n this.#worker.onError(err => {\n workerError ??= err;\n this.#state.stop(lc, err);\n });\n lc.info?.(`Starting IncrementalSyncer`);\n const {watermark: initialWatermark} =\n await this.#worker.getSubscriptionState();\n\n // Notify any waiting subscribers that the replica is ready to be read.\n // This initial notification intentionally omits replicaReadyTimeMs because\n // it represents already-current state, not newly-unserved work.\n void this.#notifier.notifySubscribers({\n state: 'version-ready',\n watermark: initialWatermark,\n });\n\n while (this.#state.shouldRun()) {\n const {replicaVersion, watermark, backfills} =\n await this.#worker.getSubscriptionState();\n\n let downstream: Source<Sized<SubscribeDownstream>> | undefined;\n let unregister = () => {};\n let err: unknown | undefined;\n\n try {\n const ctx = {\n protocolVersion: PROTOCOL_VERSION,\n taskID: this.#taskID,\n id: this.#id,\n mode: this.#mode,\n watermark,\n replicaVersion,\n backfills,\n };\n // The first connection reuses the still-open reservation connection\n // (if any) from the backup restore, so the replication-manager that\n // reserved the change log is the one that serves the subscription.\n // Every reconnect after that opens a fresh connection.\n const initialConnection = this.#initialConnection;\n this.#initialConnection = undefined;\n downstream = initialConnection\n ? await initialConnection.subscribe(ctx)\n : await this.#changeStreamer.subscribe(ctx);\n unregister = this.#state.cancelOnStop(downstream);\n this.#statusPublisher?.publish(\n lc,\n 'Replicating',\n `Replicating from ${watermark}`,\n );\n\n let backfill:\n | {status: DownloadStatus; table: string; columns: string[]}\n | undefined;\n let writeBatch: ChangeStreamData[] = [];\n let writeBatchSize = 0;\n let inTransaction = false;\n // Indexes created in the current run of consecutive index creations.\n let indexing: IndexingProgress | undefined;\n\n const publishBackfillStatus = (table: string) =>\n this.#statusPublisher?.publish(\n lc,\n 'Replicating',\n `Backfilling ${table} table`,\n 3000,\n () =>\n backfill\n ? {\n downloadStatus: [\n {\n ...backfill.status,\n table: backfill.table,\n columns: backfill.columns,\n },\n ],\n }\n : {},\n );\n\n const flushWrites = async () => {\n if (writeBatch.length === 0) {\n return;\n }\n const batch = writeBatch;\n writeBatch = [];\n writeBatchSize = 0;\n\n const result = await this.#worker.processMessages(batch);\n this.#handleResult(lc, result);\n if (result?.completedBackfill) {\n backfill = undefined;\n }\n };\n\n for await (const {data: message, size} of downstream) {\n // Only reset backoff after receiving an actual message.\n this.#state.resetBackoff();\n\n this.#replicationEvents.add(1);\n switch (message[0]) {\n case 'reserved':\n lc.warn?.(`unexpected snapshot reservation message received`, {\n message: message[0],\n });\n break;\n case 'status': {\n const {lagReport} = message[1];\n if (lagReport) {\n const report: ReplicationReport = {\n nextSendTimeMs: lagReport.nextSendTimeMs,\n };\n if (lagReport.lastTimings) {\n report.lastTimings = {\n ...lagReport.lastTimings,\n replicateTimeMs: Date.now(),\n };\n }\n this.#reporter.record(report);\n }\n break;\n }\n case 'error': {\n // Signal from the replication-manager that the view-syncer must\n // shut down and restore a new backup from litestream.\n const {type, message: msg} = message[1];\n this.stop(\n lc,\n // Note: The AbortError indicates a clean / intentional shutdown.\n new AbortError(\n `${errorTypeToReadableName(type as ErrorType)}: ${msg}`,\n ),\n );\n break;\n }\n default: {\n const msg = message[1];\n if (msg.tag === 'backfill' && msg.status) {\n const {status, relation} = msg;\n const first = !backfill;\n backfill = {\n status, // Update the current status\n table: relation.name,\n columns: [...relation.rowKey.columns, ...msg.columns],\n };\n if (first) {\n // Start publishing the status every 3 seconds.\n publishBackfillStatus(relation.name);\n }\n }\n\n if (msg.tag === 'create-index' && this.#statusPublisher) {\n // Creating an index on an existing table can take a long\n // time. Flush preceding changes so that the index creation\n // is processed (and timed) on its own.\n await flushWrites();\n const index = mapPostgresToLiteIndex(msg.spec);\n indexing ??= new IndexingProgress();\n indexing.start(index);\n this.#statusPublisher.publish(\n lc,\n 'Replicating',\n `Creating index ${index.name} on ${index.tableName}`,\n 3000,\n indexing.state,\n );\n // Exclude the time spent publishing the status.\n indexing.restartTimer();\n writeBatch.push(message as ChangeStreamData);\n writeBatchSize += size;\n await flushWrites();\n const elapsed = indexing.finish();\n lc.info?.(\n `Created index ${index.name} (${elapsed.toFixed(3)} ms)`,\n );\n this.#statusPublisher.publish(\n lc,\n 'Replicating',\n `Created index ${index.name} on ${index.tableName}`,\n 0,\n indexing.state,\n );\n if (backfill) {\n // Resume reporting the progress of the ongoing backfill.\n publishBackfillStatus(backfill.table);\n }\n break;\n }\n indexing = undefined;\n\n const type = message[0];\n const invalidTransactionSequence =\n type === 'begin' ? inTransaction : !inTransaction;\n if (type === 'begin') {\n inTransaction = true;\n } else if (type === 'commit' || type === 'rollback') {\n inTransaction = false;\n }\n\n writeBatch.push(message as ChangeStreamData);\n writeBatchSize += size;\n if (\n // Waiting here ensures that consuming the commit, which ACKs\n // it upstream, only happens after the SQLite commit finishes.\n type === 'commit' ||\n type === 'rollback' ||\n // Promptly surface malformed transaction sequences instead\n // of leaving a partial batch waiting for another message.\n invalidTransactionSequence ||\n writeBatch.length >= MAX_WORKER_BATCH_MESSAGES ||\n writeBatchSize >= MAX_WORKER_BATCH_SIZE\n ) {\n await flushWrites();\n }\n break;\n }\n }\n }\n this.#worker.abort();\n } catch (e) {\n err = e;\n this.#worker.abort();\n } finally {\n downstream?.cancel();\n unregister();\n this.#statusPublisher?.stop();\n }\n await this.#state.backoff(lc, err);\n }\n lc.info?.('IncrementalSyncer stopped');\n if (workerError) {\n throw workerError;\n }\n }\n\n #handleResult(lc: LogContext, result: CommitResult | null) {\n if (!result) {\n return;\n }\n if (result.completedBackfill) {\n // Publish the final status\n const status = result.completedBackfill;\n this.#statusPublisher?.publish(\n lc,\n 'Replicating',\n `Backfilled ${status.table} table`,\n 0,\n () => ({downloadStatus: [status]}),\n );\n } else if (result.schemaUpdated) {\n this.#statusPublisher?.publish(lc, 'Replicating', 'Schema updated');\n }\n if (result.watermark && result.changeLogUpdated) {\n void this.#notifier.notifySubscribers({\n state: 'version-ready',\n watermark: result.watermark,\n replicaReadyTimeMs: Date.now(),\n upstreamCommitTimeMs: result.upstreamCommitTimeMs,\n });\n }\n }\n\n subscribe(): Source<ReplicaState> {\n return this.#notifier.subscribe();\n }\n\n stop(lc: LogContext, err?: unknown) {\n this.#state.stop(lc, err);\n // Abort any polling loop that the write worker may be in.\n this.#worker.abort();\n }\n}\n"],"mappings":";;;;;;;;;AAiCA,IAAM,4BAA4B;AAClC,IAAM,wBAAwB,OAAO;;;;;;;;AASrC,IAAa,oBAAb,MAA+B;CAC7B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAGA;CAEA,SAAkB,IAAI,aAAa,mBAAmB;CAEtD,qBAA8B,mBAC5B,eACA,UACA,wCACF;CAEA,YACE,IACA,QACA,IACA,gBACA,QACA,MACA,iBACA,iCAAiE,CAAC,GAClE,mBACA;EACA,KAAKA,MAAM;EACX,KAAKC,UAAU;EACf,KAAKC,MAAM;EACX,KAAKC,kBAAkB;EACvB,KAAKC,UAAU;EACf,KAAKC,QAAQ;EACb,KAAKC,mBAAmB;EACxB,KAAKC,YAAY,IAAI,SAAS;EAC9B,KAAKC,YAAY,IAAI,0BACnB,IACA,8BACF;EACA,KAAKG,qBAAqB;CAC5B;CAEA,QAAuB;EACrB,OAAO,KAAKH,UAAU,MAAM;CAC9B;CAEA,MAAM,MAAM;EACV,MAAM,KAAK,KAAKR;EAChB,IAAI;EACJ,KAAKI,QAAQ,SAAQ,QAAO;GAC1B,gBAAgB;GAChB,KAAKK,OAAO,KAAK,IAAI,GAAG;EAC1B,CAAC;EACD,GAAG,OAAO,4BAA4B;EACtC,MAAM,EAAC,WAAW,qBAChB,MAAM,KAAKL,QAAQ,qBAAqB;EAK1C,KAAUG,UAAU,kBAAkB;GACpC,OAAO;GACP,WAAW;EACb,CAAC;EAED,OAAO,KAAKE,OAAO,UAAU,GAAG;GAC9B,MAAM,EAAC,gBAAgB,WAAW,cAChC,MAAM,KAAKL,QAAQ,qBAAqB;GAE1C,IAAI;GACJ,IAAI,mBAAmB,CAAC;GACxB,IAAI;GAEJ,IAAI;IACF,MAAM,MAAM;KACV,iBAAA;KACA,QAAQ,KAAKH;KACb,IAAI,KAAKC;KACT,MAAM,KAAKG;KACX;KACA;KACA;IACF;IAKA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,6BACrB,kBAAkB,uBAElB,MAAM,YAAY;OAEpB;MACF;KACF;IACF;IACA,KAAKF,QAAQ,MAAM;GACrB,SAAS,GAAG;IACV,MAAM;IACN,KAAKA,QAAQ,MAAM;GACrB,UAAU;IACR,YAAY,OAAO;IACnB,WAAW;IACX,KAAKE,kBAAkB,KAAK;GAC9B;GACA,MAAM,KAAKG,OAAO,QAAQ,IAAI,GAAG;EACnC;EACA,GAAG,OAAO,2BAA2B;EACrC,IAAI,aACF,MAAM;CAEV;CAEA,cAAc,IAAgB,QAA6B;EACzD,IAAI,CAAC,QACH;EAEF,IAAI,OAAO,mBAAmB;GAE5B,MAAM,SAAS,OAAO;GACtB,KAAKH,kBAAkB,QACrB,IACA,eACA,cAAc,OAAO,MAAM,SAC3B,UACO,EAAC,gBAAgB,CAAC,MAAM,EAAC,EAClC;EACF,OAAO,IAAI,OAAO,eAChB,KAAKA,kBAAkB,QAAQ,IAAI,eAAe,gBAAgB;EAEpE,IAAI,OAAO,aAAa,OAAO,kBAC7B,KAAUC,UAAU,kBAAkB;GACpC,OAAO;GACP,WAAW,OAAO;GAClB,oBAAoB,KAAK,IAAI;GAC7B,sBAAsB,OAAO;EAC/B,CAAC;CAEL;CAEA,YAAkC;EAChC,OAAO,KAAKA,UAAU,UAAU;CAClC;CAEA,KAAK,IAAgB,KAAe;EAClC,KAAKE,OAAO,KAAK,IAAI,GAAG;EAExB,KAAKL,QAAQ,MAAM;CACrB;AACF"}
@@ -5,6 +5,7 @@ import type { ReservationFollowup } from '../change-streamer/change-streamer-htt
5
5
  import type { ChangeStreamer } from '../change-streamer/change-streamer.ts';
6
6
  import type { Service } from '../service.ts';
7
7
  import { type ReplicationStatusPublisher } from './replication-status.ts';
8
+ import type { ReplicationLagReadinessOptions } from './reporter/recorder.ts';
8
9
  import type { WriteWorkerClient } from './write-worker-client.ts';
9
10
  /** See {@link ReplicaStateNotifier.subscribe()}. */
10
11
  export type ReplicaState = {
@@ -72,10 +73,11 @@ export type ReplicatorMode = 'backup' | 'serving';
72
73
  export declare class ReplicatorService implements Replicator, Service {
73
74
  #private;
74
75
  readonly id: string;
75
- constructor(lc: LogContext, taskID: string, id: string, mode: ReplicatorMode, changeStreamer: ChangeStreamer, worker: WriteWorkerClient, statusPublisher: ReplicationStatusPublisher | null, initialConnection?: ReservationFollowup);
76
+ constructor(lc: LogContext, taskID: string, id: string, mode: ReplicatorMode, changeStreamer: ChangeStreamer, worker: WriteWorkerClient, statusPublisher: ReplicationStatusPublisher | null, replicationLagReadinessOptions?: ReplicationLagReadinessOptions, initialConnection?: ReservationFollowup);
76
77
  status(): Promise<{
77
78
  status: string;
78
79
  }>;
80
+ ready(): Promise<void>;
79
81
  run(): Promise<void>;
80
82
  subscribe(): Source<ReplicaState>;
81
83
  stop(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"replicator.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/replicator/replicator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,gCAAgC,CAAC;AACvE,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,wBAAwB,CAAC;AACnD,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,4CAA4C,CAAC;AACpF,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,uCAAuC,CAAC;AAC1E,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AAE3C,OAAO,EAEL,KAAK,0BAA0B,EAChC,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEhE,oDAAoD;AACpD,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC;IAChC;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC;;OAEG;IACH,QAAQ,CAAC,kBAAkB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEjD;;;;;;;;;;;;;OAaG;IACH,QAAQ,CAAC,oBAAoB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAInD,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C,CAAC;AAEF,MAAM,WAAW,oBAAoB;IACnC;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,CAAC;CACnC;AAED,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD;;;;OAIG;IACH,MAAM,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAAC;CACvC;AAED,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,SAAS,CAAC;AAElD,qBAAa,iBAAkB,YAAW,UAAU,EAAE,OAAO;;IAC3D,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAOpB,YACE,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,cAAc,EACpB,cAAc,EAAE,cAAc,EAC9B,MAAM,EAAE,iBAAiB,EACzB,eAAe,EAAE,0BAA0B,GAAG,IAAI,EAKlD,iBAAiB,CAAC,EAAE,mBAAmB,EAmBxC;IAED,MAAM;;OAEL;IAED,GAAG,kBAYF;IAED,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,CAEhC;IAEK,IAAI,kBAOT;CACF"}
1
+ {"version":3,"file":"replicator.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/replicator/replicator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,gCAAgC,CAAC;AACvE,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,wBAAwB,CAAC;AACnD,OAAO,KAAK,EAAC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,MAAM,EAAE,iBAAiB,EACzB,eAAe,EAAE,0BAA0B,GAAG,IAAI,EAClD,8BAA8B,GAAE,8BAAmC,EAKnE,iBAAiB,CAAC,EAAE,mBAAmB,EAoBxC;IAED,MAAM;;OAEL;IAED,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;IAED,GAAG,kBAYF;IAED,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,CAEhC;IAEK,IAAI,kBAOT;CACF"}
@@ -8,16 +8,19 @@ var ReplicatorService = class {
8
8
  #shouldPublishErrors;
9
9
  #worker;
10
10
  #runPromise;
11
- constructor(lc, taskID, id, mode, changeStreamer, worker, statusPublisher, initialConnection) {
11
+ constructor(lc, taskID, id, mode, changeStreamer, worker, statusPublisher, replicationLagReadinessOptions = {}, initialConnection) {
12
12
  this.id = id;
13
13
  this.#lc = lc.withContext("component", "replicator").withContext("serviceID", this.id);
14
14
  this.#worker = worker;
15
15
  this.#shouldPublishErrors = statusPublisher !== null;
16
- this.#incrementalSyncer = new IncrementalSyncer(this.#lc, taskID, `${taskID}/${id}`, changeStreamer, worker, mode, statusPublisher, initialConnection);
16
+ this.#incrementalSyncer = new IncrementalSyncer(this.#lc, taskID, `${taskID}/${id}`, changeStreamer, worker, mode, statusPublisher, replicationLagReadinessOptions, initialConnection);
17
17
  }
18
18
  status() {
19
19
  return Promise.resolve({ status: "ok" });
20
20
  }
21
+ ready() {
22
+ return this.#incrementalSyncer.ready();
23
+ }
21
24
  run() {
22
25
  this.#runPromise = this.#incrementalSyncer.run().catch(async (error) => {
23
26
  if (this.#shouldPublishErrors) await publishReplicationError(this.#lc, "Replicating", "Replication stopped because the replica writer failed");
@@ -1 +1 @@
1
- {"version":3,"file":"replicator.js","names":["#lc","#incrementalSyncer","#shouldPublishErrors","#worker","#runPromise"],"sources":["../../../../../../zero-cache/src/services/replicator/replicator.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {ReadonlyJSONObject} from '../../../../shared/src/json.ts';\nimport type {Source} from '../../types/streams.ts';\nimport type {ReservationFollowup} from '../change-streamer/change-streamer-http.ts';\nimport type {ChangeStreamer} from '../change-streamer/change-streamer.ts';\nimport type {Service} from '../service.ts';\nimport {IncrementalSyncer} from './incremental-sync.ts';\nimport {\n publishReplicationError,\n type ReplicationStatusPublisher,\n} from './replication-status.ts';\nimport type {WriteWorkerClient} from './write-worker-client.ts';\n\n/** See {@link ReplicaStateNotifier.subscribe()}. */\nexport type ReplicaState = {\n readonly state: 'version-ready';\n /**\n * The replica watermark that became ready. Omitted for the initial\n * \"current state is ready\" notification, which should not count as newly\n * unserved work for a freshly-started ViewSyncer.\n */\n readonly watermark?: string | undefined;\n /**\n * Millisecond epoch when `watermark` became ready to read from the replica.\n */\n readonly replicaReadyTimeMs?: number | undefined;\n\n /**\n * Millisecond epoch at which the transaction behind `watermark` committed\n * *upstream*, as reported by the ChangeSource. Unlike `replicaReadyTimeMs`,\n * which is stamped locally when the change lands in the replica, this is the\n * origin of the whole pipeline, so `now - upstreamCommitTimeMs` at the point\n * the change is poked to clients is the end-to-end serving lag.\n *\n * Note that this crosses a clock domain: it comes from the upstream\n * database, whereas it is subtracted from the local clock in the ViewSyncer.\n * The existing `zero.replication.total_lag` metric spans the same boundary.\n *\n * Undefined when the ChangeSource does not report a commit time, and for the\n * initial \"current state is ready\" notification.\n */\n readonly upstreamCommitTimeMs?: number | undefined;\n\n // Used in tests to verify behavior when additional information\n // is ferried in the future. Not set in production.\n readonly testSeqNum?: number | undefined;\n};\n\nexport interface ReplicaStateNotifier {\n /**\n * Creates a cancelable subscription of changes in the replica state.\n *\n * A `version-ready` message indicates that the replica is ready to be\n * read, and henceforth that a _new_ version is ready, i.e. whenever a\n * change is committed to the replica. The `version-ready` message itself\n * otherwise contains no other information; the subscriber queries the\n * replica for the current data.\n *\n * A `maintenance` state indicates that the replica should not be read from.\n * If a subscriber is holding any transaction locks, it should release them\n * until the next `version-ready` signal.\n *\n * Upon subscription, the current state of the replica is sent immediately\n * if known. If multiple notifications occur before the subscriber\n * can consume them, all but the last notification are discarded by the\n * Subscription object (i.e. not buffered). Thus, a subscriber only\n * ever consumes the current (i.e. known) state of the replica. This avoids\n * a buildup of \"work\" if a subscriber is too busy to consume all\n * notifications.\n */\n subscribe(): Source<ReplicaState>;\n}\n\nexport interface Replicator extends ReplicaStateNotifier {\n /**\n * Returns an opaque message for human-readable consumption. This is\n * purely for ensuring that the Replicator has started at least once to\n * bootstrap a new replica.\n */\n status(): Promise<ReadonlyJSONObject>;\n}\n\nexport type ReplicatorMode = 'backup' | 'serving';\n\nexport class ReplicatorService implements Replicator, Service {\n readonly id: string;\n readonly #lc: LogContext;\n readonly #incrementalSyncer: IncrementalSyncer;\n readonly #shouldPublishErrors: boolean;\n readonly #worker: WriteWorkerClient;\n #runPromise: Promise<void> | undefined;\n\n constructor(\n lc: LogContext,\n taskID: string,\n id: string,\n mode: ReplicatorMode,\n changeStreamer: ChangeStreamer,\n worker: WriteWorkerClient,\n statusPublisher: ReplicationStatusPublisher | null,\n // The still-open connection from the snapshot reservation that produced\n // the replica this process just restored (serving mode only). Used for\n // IncrementalSyncer's first subscribe() so the replication-manager that\n // reserved the change log is the one that serves the subscription.\n initialConnection?: ReservationFollowup,\n ) {\n this.id = id;\n this.#lc = lc\n .withContext('component', 'replicator')\n .withContext('serviceID', this.id);\n this.#worker = worker;\n this.#shouldPublishErrors = statusPublisher !== null;\n\n this.#incrementalSyncer = new IncrementalSyncer(\n this.#lc,\n taskID,\n `${taskID}/${id}`,\n changeStreamer,\n worker,\n mode,\n statusPublisher,\n initialConnection,\n );\n }\n\n status() {\n return Promise.resolve({status: 'ok'});\n }\n\n run() {\n this.#runPromise = this.#incrementalSyncer.run().catch(async error => {\n if (this.#shouldPublishErrors) {\n await publishReplicationError(\n this.#lc,\n 'Replicating',\n 'Replication stopped because the replica writer failed',\n );\n }\n throw error;\n });\n return this.#runPromise;\n }\n\n subscribe(): Source<ReplicaState> {\n return this.#incrementalSyncer.subscribe();\n }\n\n async stop() {\n this.#incrementalSyncer.stop(this.#lc);\n // Wait for the syncer's run loop to finish so that any in-flight\n // worker.processMessages() call completes and clears #pending\n // before we send the 'stop' message to the worker.\n await this.#runPromise;\n await this.#worker.stop();\n }\n}\n"],"mappings":";;;AAoFA,IAAa,oBAAb,MAA8D;CAC5D;CACA;CACA;CACA;CACA;CACA;CAEA,YACE,IACA,QACA,IACA,MACA,gBACA,QACA,iBAKA,mBACA;EACA,KAAK,KAAK;EACV,KAAKA,MAAM,GACR,YAAY,aAAa,YAAY,EACrC,YAAY,aAAa,KAAK,EAAE;EACnC,KAAKG,UAAU;EACf,KAAKD,uBAAuB,oBAAoB;EAEhD,KAAKD,qBAAqB,IAAI,kBAC5B,KAAKD,KACL,QACA,GAAG,OAAO,GAAG,MACb,gBACA,QACA,MACA,iBACA,iBACF;CACF;CAEA,SAAS;EACP,OAAO,QAAQ,QAAQ,EAAC,QAAQ,KAAI,CAAC;CACvC;CAEA,MAAM;EACJ,KAAKI,cAAc,KAAKH,mBAAmB,IAAI,EAAE,MAAM,OAAM,UAAS;GACpE,IAAI,KAAKC,sBACP,MAAM,wBACJ,KAAKF,KACL,eACA,uDACF;GAEF,MAAM;EACR,CAAC;EACD,OAAO,KAAKI;CACd;CAEA,YAAkC;EAChC,OAAO,KAAKH,mBAAmB,UAAU;CAC3C;CAEA,MAAM,OAAO;EACX,KAAKA,mBAAmB,KAAK,KAAKD,GAAG;EAIrC,MAAM,KAAKI;EACX,MAAM,KAAKD,QAAQ,KAAK;CAC1B;AACF"}
1
+ {"version":3,"file":"replicator.js","names":["#lc","#incrementalSyncer","#shouldPublishErrors","#worker","#runPromise"],"sources":["../../../../../../zero-cache/src/services/replicator/replicator.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {ReadonlyJSONObject} from '../../../../shared/src/json.ts';\nimport type {Source} from '../../types/streams.ts';\nimport type {ReservationFollowup} from '../change-streamer/change-streamer-http.ts';\nimport type {ChangeStreamer} from '../change-streamer/change-streamer.ts';\nimport type {Service} from '../service.ts';\nimport {IncrementalSyncer} from './incremental-sync.ts';\nimport {\n publishReplicationError,\n type ReplicationStatusPublisher,\n} from './replication-status.ts';\nimport type {ReplicationLagReadinessOptions} from './reporter/recorder.ts';\nimport type {WriteWorkerClient} from './write-worker-client.ts';\n\n/** See {@link ReplicaStateNotifier.subscribe()}. */\nexport type ReplicaState = {\n readonly state: 'version-ready';\n /**\n * The replica watermark that became ready. Omitted for the initial\n * \"current state is ready\" notification, which should not count as newly\n * unserved work for a freshly-started ViewSyncer.\n */\n readonly watermark?: string | undefined;\n /**\n * Millisecond epoch when `watermark` became ready to read from the replica.\n */\n readonly replicaReadyTimeMs?: number | undefined;\n\n /**\n * Millisecond epoch at which the transaction behind `watermark` committed\n * *upstream*, as reported by the ChangeSource. Unlike `replicaReadyTimeMs`,\n * which is stamped locally when the change lands in the replica, this is the\n * origin of the whole pipeline, so `now - upstreamCommitTimeMs` at the point\n * the change is poked to clients is the end-to-end serving lag.\n *\n * Note that this crosses a clock domain: it comes from the upstream\n * database, whereas it is subtracted from the local clock in the ViewSyncer.\n * The existing `zero.replication.total_lag` metric spans the same boundary.\n *\n * Undefined when the ChangeSource does not report a commit time, and for the\n * initial \"current state is ready\" notification.\n */\n readonly upstreamCommitTimeMs?: number | undefined;\n\n // Used in tests to verify behavior when additional information\n // is ferried in the future. Not set in production.\n readonly testSeqNum?: number | undefined;\n};\n\nexport interface ReplicaStateNotifier {\n /**\n * Creates a cancelable subscription of changes in the replica state.\n *\n * A `version-ready` message indicates that the replica is ready to be\n * read, and henceforth that a _new_ version is ready, i.e. whenever a\n * change is committed to the replica. The `version-ready` message itself\n * otherwise contains no other information; the subscriber queries the\n * replica for the current data.\n *\n * A `maintenance` state indicates that the replica should not be read from.\n * If a subscriber is holding any transaction locks, it should release them\n * until the next `version-ready` signal.\n *\n * Upon subscription, the current state of the replica is sent immediately\n * if known. If multiple notifications occur before the subscriber\n * can consume them, all but the last notification are discarded by the\n * Subscription object (i.e. not buffered). Thus, a subscriber only\n * ever consumes the current (i.e. known) state of the replica. This avoids\n * a buildup of \"work\" if a subscriber is too busy to consume all\n * notifications.\n */\n subscribe(): Source<ReplicaState>;\n}\n\nexport interface Replicator extends ReplicaStateNotifier {\n /**\n * Returns an opaque message for human-readable consumption. This is\n * purely for ensuring that the Replicator has started at least once to\n * bootstrap a new replica.\n */\n status(): Promise<ReadonlyJSONObject>;\n}\n\nexport type ReplicatorMode = 'backup' | 'serving';\n\nexport class ReplicatorService implements Replicator, Service {\n readonly id: string;\n readonly #lc: LogContext;\n readonly #incrementalSyncer: IncrementalSyncer;\n readonly #shouldPublishErrors: boolean;\n readonly #worker: WriteWorkerClient;\n #runPromise: Promise<void> | undefined;\n\n constructor(\n lc: LogContext,\n taskID: string,\n id: string,\n mode: ReplicatorMode,\n changeStreamer: ChangeStreamer,\n worker: WriteWorkerClient,\n statusPublisher: ReplicationStatusPublisher | null,\n replicationLagReadinessOptions: ReplicationLagReadinessOptions = {},\n // The still-open connection from the snapshot reservation that produced\n // the replica this process just restored (serving mode only). Used for\n // IncrementalSyncer's first subscribe() so the replication-manager that\n // reserved the change log is the one that serves the subscription.\n initialConnection?: ReservationFollowup,\n ) {\n this.id = id;\n this.#lc = lc\n .withContext('component', 'replicator')\n .withContext('serviceID', this.id);\n this.#worker = worker;\n this.#shouldPublishErrors = statusPublisher !== null;\n\n this.#incrementalSyncer = new IncrementalSyncer(\n this.#lc,\n taskID,\n `${taskID}/${id}`,\n changeStreamer,\n worker,\n mode,\n statusPublisher,\n replicationLagReadinessOptions,\n initialConnection,\n );\n }\n\n status() {\n return Promise.resolve({status: 'ok'});\n }\n\n ready(): Promise<void> {\n return this.#incrementalSyncer.ready();\n }\n\n run() {\n this.#runPromise = this.#incrementalSyncer.run().catch(async error => {\n if (this.#shouldPublishErrors) {\n await publishReplicationError(\n this.#lc,\n 'Replicating',\n 'Replication stopped because the replica writer failed',\n );\n }\n throw error;\n });\n return this.#runPromise;\n }\n\n subscribe(): Source<ReplicaState> {\n return this.#incrementalSyncer.subscribe();\n }\n\n async stop() {\n this.#incrementalSyncer.stop(this.#lc);\n // Wait for the syncer's run loop to finish so that any in-flight\n // worker.processMessages() call completes and clears #pending\n // before we send the 'stop' message to the worker.\n await this.#runPromise;\n await this.#worker.stop();\n }\n}\n"],"mappings":";;;AAqFA,IAAa,oBAAb,MAA8D;CAC5D;CACA;CACA;CACA;CACA;CACA;CAEA,YACE,IACA,QACA,IACA,MACA,gBACA,QACA,iBACA,iCAAiE,CAAC,GAKlE,mBACA;EACA,KAAK,KAAK;EACV,KAAKA,MAAM,GACR,YAAY,aAAa,YAAY,EACrC,YAAY,aAAa,KAAK,EAAE;EACnC,KAAKG,UAAU;EACf,KAAKD,uBAAuB,oBAAoB;EAEhD,KAAKD,qBAAqB,IAAI,kBAC5B,KAAKD,KACL,QACA,GAAG,OAAO,GAAG,MACb,gBACA,QACA,MACA,iBACA,gCACA,iBACF;CACF;CAEA,SAAS;EACP,OAAO,QAAQ,QAAQ,EAAC,QAAQ,KAAI,CAAC;CACvC;CAEA,QAAuB;EACrB,OAAO,KAAKC,mBAAmB,MAAM;CACvC;CAEA,MAAM;EACJ,KAAKG,cAAc,KAAKH,mBAAmB,IAAI,EAAE,MAAM,OAAM,UAAS;GACpE,IAAI,KAAKC,sBACP,MAAM,wBACJ,KAAKF,KACL,eACA,uDACF;GAEF,MAAM;EACR,CAAC;EACD,OAAO,KAAKI;CACd;CAEA,YAAkC;EAChC,OAAO,KAAKH,mBAAmB,UAAU;CAC3C;CAEA,MAAM,OAAO;EACX,KAAKA,mBAAmB,KAAK,KAAKD,GAAG;EAIrC,MAAM,KAAKI;EACX,MAAM,KAAKD,QAAQ,KAAK;CAC1B;AACF"}
@@ -1,9 +1,16 @@
1
1
  import type { ObservableResult } from '@opentelemetry/api';
2
2
  import type { LogContext } from '@rocicorp/logger';
3
3
  import type { ReplicationReport } from './report-schema.ts';
4
+ export type ReplicationLagReadinessOptions = {
5
+ readinessThresholdMs?: number | undefined;
6
+ maxReadinessDelayMs?: number | undefined;
7
+ setTimeoutFn?: typeof setTimeout | undefined;
8
+ clearTimeoutFn?: typeof clearTimeout | undefined;
9
+ };
4
10
  export declare class ReplicationReportRecorder {
5
11
  #private;
6
- constructor(lc: LogContext);
12
+ constructor(lc: LogContext, { readinessThresholdMs, maxReadinessDelayMs, setTimeoutFn, clearTimeoutFn, }?: ReplicationLagReadinessOptions);
13
+ ready(): Promise<void>;
7
14
  record(report: ReplicationReport): void;
8
15
  readonly reportUpstreamLag: (o: ObservableResult) => void;
9
16
  readonly reportReplicaLag: (o: ObservableResult) => void;
@@ -37,4 +44,12 @@ export declare function estimateUpstreamClockSkewMs({ sendTimeMs, commitTimeMs,
37
44
  commitTimeMs: number;
38
45
  receiveTimeMs: number;
39
46
  }): number;
47
+ /**
48
+ * Returns whether the `report` is the signal sent by the change-streamer,
49
+ * once the subscriber is caught up, to indicate that lag reporting is
50
+ * disabled upstream (and thus readiness cannot be gated on lag reports).
51
+ *
52
+ * This is a lag report with a `nextSendTimeMs` of 0 and no `lastTimings`.
53
+ */
54
+ export declare function isLagReportingDisabledSignal(report: ReplicationReport): boolean;
40
55
  //# sourceMappingURL=recorder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/replicator/reporter/recorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,oBAAoB,CAAC;AAM1D,qBAAa,yBAAyB;;IAIpC,YAAY,EAAE,EAAE,UAAU,EAEzB;IAED,MAAM,CAAC,MAAM,EAAE,iBAAiB,QAgE/B;IAED,QAAQ,CAAC,iBAAiB,MAAO,gBAAgB,UAK/C;IAEF,QAAQ,CAAC,gBAAgB,MAAO,gBAAgB,UAK9C;IAEF,QAAQ,CAAC,cAAc,MAAO,gBAAgB,UAK5C;IAEF,QAAQ,CAAC,kBAAkB,MAAO,gBAAgB,UAKhD;IAEF,QAAQ,CAAC,uBAAuB,MAAO,gBAAgB,UAKrD;CACH;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,2BAA2B,CAAC,EAC1C,UAAU,EACV,YAAY,EACZ,aAAa,GACd,EAAE;IACD,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;CACvB,GAAG,MAAM,CAET"}
1
+ {"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/replicator/reporter/recorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAC,UAAU,EAAW,MAAM,kBAAkB,CAAC;AAG3D,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,oBAAoB,CAAC;AAM1D,MAAM,MAAM,8BAA8B,GAAG;IAC3C,oBAAoB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1C,mBAAmB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACzC,YAAY,CAAC,EAAE,OAAO,UAAU,GAAG,SAAS,CAAC;IAC7C,cAAc,CAAC,EAAE,OAAO,YAAY,GAAG,SAAS,CAAC;CAClD,CAAC;AAEF,qBAAa,yBAAyB;;IASpC,YACE,EAAE,EAAE,UAAU,EACd,EACE,oBAA2B,EAC3B,mBAA6B,EAC7B,YAAyB,EACzB,cAA6B,GAC9B,GAAE,8BAAmC,EAgBvC;IAED,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;IAED,MAAM,CAAC,MAAM,EAAE,iBAAiB,QAsC/B;IA0DD,QAAQ,CAAC,iBAAiB,MAAO,gBAAgB,UAK/C;IAEF,QAAQ,CAAC,gBAAgB,MAAO,gBAAgB,UAK9C;IAEF,QAAQ,CAAC,cAAc,MAAO,gBAAgB,UAK5C;IAEF,QAAQ,CAAC,kBAAkB,MAAO,gBAAgB,UAKhD;IAEF,QAAQ,CAAC,uBAAuB,MAAO,gBAAgB,UAKrD;CACH;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,2BAA2B,CAAC,EAC1C,UAAU,EACV,YAAY,EACZ,aAAa,GACd,EAAE;IACD,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;CACvB,GAAG,MAAM,CAET;AAED;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAET"}
@@ -1,45 +1,78 @@
1
1
  import { getOrCreateGauge } from "../../../observability/metrics.js";
2
+ import { resolver } from "@rocicorp/resolver";
2
3
  //#region ../zero-cache/src/services/replicator/reporter/recorder.ts
3
4
  var LOG_ALL_REPLICATION_REPORTS_AT_DEBUG = process.env.ZERO_LOG_ALL_REPLICATION_REPORTS_AT_DEBUG === "1";
4
5
  var ReplicationReportRecorder = class {
5
6
  #lc;
7
+ #readinessThresholdMs;
8
+ #readyGate = resolver();
9
+ #clearTimeout;
10
+ #readinessTimer;
11
+ #ready = false;
6
12
  #last = null;
7
- constructor(lc) {
13
+ constructor(lc, { readinessThresholdMs = 1e3, maxReadinessDelayMs = 6e5, setTimeoutFn = setTimeout, clearTimeoutFn = clearTimeout } = {}) {
8
14
  this.#lc = lc;
15
+ this.#readinessThresholdMs = readinessThresholdMs;
16
+ this.#clearTimeout = clearTimeoutFn;
17
+ this.#readinessTimer = setTimeoutFn(() => this.#setReady("warn", `timed out waiting for replication lag readiness threshold of ${this.#readinessThresholdMs} ms`), maxReadinessDelayMs);
18
+ this.#readinessTimer?.unref?.();
19
+ }
20
+ ready() {
21
+ return this.#readyGate.promise;
9
22
  }
10
23
  record(report) {
11
- const first = this.#last === null;
24
+ if (isLagReportingDisabledSignal(report)) {
25
+ this.#setReady("info", `replica is caught up (replication lag reporting is disabled upstream)`);
26
+ return;
27
+ }
12
28
  this.#last = report;
13
29
  const { lastTimings } = report;
14
30
  if (lastTimings) {
15
31
  const total = lastTimings.replicateTimeMs - lastTimings.sendTimeMs;
16
- if (total > 1e4) this.#lc.warn?.(`high replication lag: ${total} ms`, report);
17
- else if (total > 1e3) this.#lc.info?.(`replication lag: ${total} ms`, report);
18
- if (LOG_ALL_REPLICATION_REPORTS_AT_DEBUG) this.#lc.debug?.(`replication lag ${total} ms`, report);
19
- }
20
- if (first) {
21
- getOrCreateGauge("replication", "upstream_lag", {
22
- description: "Latency from sending an upstream replication report to receiving it in the replication stream",
23
- unit: "millisecond"
24
- }).addCallback(this.reportUpstreamLag);
25
- getOrCreateGauge("replication", "replica_lag", {
26
- description: "Latency from receiving an upstream replication report to its reaching the replica",
27
- unit: "millisecond"
28
- }).addCallback(this.reportReplicaLag);
29
- getOrCreateGauge("replication", "total_lag", {
30
- description: "Latency from sending an upstream replication report to its reaching the replica. This reflects the actual measured round-trip of the most recently received report and does not grow when reports stop arriving.",
31
- unit: "millisecond"
32
- }).addCallback(this.reportTotalLag);
33
- getOrCreateGauge("replication", "last_total_lag", {
34
- description: "Latency from sending the most recently received upstream replication report to its reaching the replica. This is an alias of replication.total_lag retained for dashboards that explicitly want the non-extrapolated value.",
35
- unit: "millisecond"
36
- }).addCallback(this.reportLastTotalLag);
37
- getOrCreateGauge("replication", "upstream_clock_skew", {
38
- description: "Estimated offset of the upstream database clock relative to zero-cache's. Positive means upstream is running ahead. Derived from the lag report round-trip, whose send and receive times are stamped on our clock either side of the upstream commit timestamp. This matters because sync.e2e_serving_lag subtracts an upstream commit timestamp from a local clock, so a skewed upstream clock biases it directly -- and an upstream clock running ahead biases it *low*, which looks like healthy serving rather than a broken measurement.",
39
- unit: "millisecond"
40
- }).addCallback(this.reportUpstreamClockSkew);
32
+ if (total > 1e4) {
33
+ const log = this.#ready ? "warn" : "info";
34
+ this.#lc[log]?.(`high replication lag: ${total} ms`, {
35
+ ready: this.#ready,
36
+ report
37
+ });
38
+ } else if (total > 1e3) this.#lc.info?.(`replication lag: ${total} ms`, {
39
+ ready: this.#ready,
40
+ report
41
+ });
42
+ if (LOG_ALL_REPLICATION_REPORTS_AT_DEBUG) this.#lc.debug?.(`replication lag ${total} ms`, {
43
+ ready: this.#ready,
44
+ report
45
+ });
46
+ if (!this.#ready && total <= this.#readinessThresholdMs) this.#setReady("info", `replication lag is now below readiness threshold of ${this.#readinessThresholdMs} ms`);
41
47
  }
42
48
  }
49
+ #setReady(log, message) {
50
+ if (this.#ready) return;
51
+ this.#lc[log]?.(message, { lastReport: this.#last });
52
+ this.#ready = true;
53
+ this.#clearTimeout(this.#readinessTimer);
54
+ this.#readyGate.resolve();
55
+ getOrCreateGauge("replication", "upstream_lag", {
56
+ description: "Latency from sending an upstream replication report to receiving it in the replication stream",
57
+ unit: "millisecond"
58
+ }).addCallback(this.reportUpstreamLag);
59
+ getOrCreateGauge("replication", "replica_lag", {
60
+ description: "Latency from receiving an upstream replication report to its reaching the replica",
61
+ unit: "millisecond"
62
+ }).addCallback(this.reportReplicaLag);
63
+ getOrCreateGauge("replication", "total_lag", {
64
+ description: "Latency from sending an upstream replication report to its reaching the replica. This reflects the actual measured round-trip of the most recently received report and does not grow when reports stop arriving.",
65
+ unit: "millisecond"
66
+ }).addCallback(this.reportTotalLag);
67
+ getOrCreateGauge("replication", "last_total_lag", {
68
+ description: "Latency from sending the most recently received upstream replication report to its reaching the replica. This is an alias of replication.total_lag retained for dashboards that explicitly want the non-extrapolated value.",
69
+ unit: "millisecond"
70
+ }).addCallback(this.reportLastTotalLag);
71
+ getOrCreateGauge("replication", "upstream_clock_skew", {
72
+ description: "Estimated offset of the upstream database clock relative to zero-cache's. Positive means upstream is running ahead. Derived from the lag report round-trip, whose send and receive times are stamped on our clock either side of the upstream commit timestamp. This matters because sync.e2e_serving_lag subtracts an upstream commit timestamp from a local clock, so a skewed upstream clock biases it directly -- and an upstream clock running ahead biases it *low*, which looks like healthy serving rather than a broken measurement.",
73
+ unit: "millisecond"
74
+ }).addCallback(this.reportUpstreamClockSkew);
75
+ }
43
76
  reportUpstreamLag = (o) => {
44
77
  const last = this.#last?.lastTimings;
45
78
  if (last) o.observe(last.receiveTimeMs - last.sendTimeMs);
@@ -85,6 +118,16 @@ var ReplicationReportRecorder = class {
85
118
  function estimateUpstreamClockSkewMs({ sendTimeMs, commitTimeMs, receiveTimeMs }) {
86
119
  return commitTimeMs - (sendTimeMs + receiveTimeMs) / 2;
87
120
  }
121
+ /**
122
+ * Returns whether the `report` is the signal sent by the change-streamer,
123
+ * once the subscriber is caught up, to indicate that lag reporting is
124
+ * disabled upstream (and thus readiness cannot be gated on lag reports).
125
+ *
126
+ * This is a lag report with a `nextSendTimeMs` of 0 and no `lastTimings`.
127
+ */
128
+ function isLagReportingDisabledSignal(report) {
129
+ return report.nextSendTimeMs === 0 && report.lastTimings === void 0;
130
+ }
88
131
  //#endregion
89
132
  export { ReplicationReportRecorder };
90
133
 
@@ -1 +1 @@
1
- {"version":3,"file":"recorder.js","names":["#lc","#last"],"sources":["../../../../../../../zero-cache/src/services/replicator/reporter/recorder.ts"],"sourcesContent":["import type {ObservableResult} from '@opentelemetry/api';\nimport type {LogContext} from '@rocicorp/logger';\nimport {getOrCreateGauge} from '../../../observability/metrics.ts';\nimport type {ReplicationReport} from './report-schema.ts';\n\n// Hook for sanity checking lag reports in development.\nconst LOG_ALL_REPLICATION_REPORTS_AT_DEBUG =\n process.env.ZERO_LOG_ALL_REPLICATION_REPORTS_AT_DEBUG === '1';\n\nexport class ReplicationReportRecorder {\n readonly #lc: LogContext;\n #last: ReplicationReport | null = null;\n\n constructor(lc: LogContext) {\n this.#lc = lc;\n }\n\n record(report: ReplicationReport) {\n const first = this.#last === null;\n this.#last = report;\n\n const {lastTimings} = report;\n if (lastTimings) {\n const total = lastTimings.replicateTimeMs - lastTimings.sendTimeMs;\n if (total > 10_000) {\n this.#lc.warn?.(`high replication lag: ${total} ms`, report);\n } else if (total > 1_000) {\n this.#lc.info?.(`replication lag: ${total} ms`, report);\n }\n if (LOG_ALL_REPLICATION_REPORTS_AT_DEBUG) {\n this.#lc.debug?.(`replication lag ${total} ms`, report);\n }\n }\n\n if (first) {\n getOrCreateGauge('replication', 'upstream_lag', {\n description:\n 'Latency from sending an upstream replication report ' +\n 'to receiving it in the replication stream',\n unit: 'millisecond',\n }).addCallback(this.reportUpstreamLag);\n\n getOrCreateGauge('replication', 'replica_lag', {\n description:\n 'Latency from receiving an upstream replication report ' +\n 'to its reaching the replica',\n unit: 'millisecond',\n }).addCallback(this.reportReplicaLag);\n\n getOrCreateGauge('replication', 'total_lag', {\n description:\n 'Latency from sending an upstream replication report to its ' +\n 'reaching the replica. This reflects the actual measured ' +\n 'round-trip of the most recently received report and does not ' +\n 'grow when reports stop arriving.',\n unit: 'millisecond',\n }).addCallback(this.reportTotalLag);\n\n getOrCreateGauge('replication', 'last_total_lag', {\n description:\n 'Latency from sending the most recently received upstream ' +\n 'replication report to its reaching the replica. This is an alias ' +\n 'of replication.total_lag retained for dashboards that explicitly ' +\n 'want the non-extrapolated value.',\n unit: 'millisecond',\n }).addCallback(this.reportLastTotalLag);\n\n getOrCreateGauge('replication', 'upstream_clock_skew', {\n description:\n 'Estimated offset of the upstream database clock relative to ' +\n \"zero-cache's. Positive means upstream is running ahead. \" +\n 'Derived from the lag report round-trip, whose send and receive ' +\n 'times are stamped on our clock either side of the upstream ' +\n 'commit timestamp. This matters because ' +\n 'sync.e2e_serving_lag subtracts an upstream commit timestamp from ' +\n 'a local clock, so a skewed upstream clock biases it directly -- ' +\n 'and an upstream clock running ahead biases it *low*, which looks ' +\n 'like healthy serving rather than a broken measurement.',\n unit: 'millisecond',\n }).addCallback(this.reportUpstreamClockSkew);\n }\n }\n\n readonly reportUpstreamLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.receiveTimeMs - last.sendTimeMs);\n }\n };\n\n readonly reportReplicaLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.replicateTimeMs - last.receiveTimeMs);\n }\n };\n\n readonly reportTotalLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.replicateTimeMs - last.sendTimeMs);\n }\n };\n\n readonly reportLastTotalLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.replicateTimeMs - last.sendTimeMs);\n }\n };\n\n readonly reportUpstreamClockSkew = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(estimateUpstreamClockSkewMs(last));\n }\n };\n}\n\n/**\n * Estimates how far the upstream database's clock is from ours, in\n * milliseconds, positive meaning upstream is ahead.\n *\n * `sendTimeMs` and `receiveTimeMs` are both stamped in the\n * replication-manager, either side of `commitTimeMs`, which upstream stamps.\n * Assuming the upstream commit fell midway through that round trip, the\n * difference between where it actually landed and that midpoint is the clock\n * offset -- the same round-trip estimate NTP uses.\n *\n * The estimate is biased by any asymmetry between the two legs: the outbound\n * leg is a SQL round trip, while the return leg additionally waits on a WAL\n * flush and the walsender (which the pre-PG-17 path notes can add 50~100ms on\n * an idle database). So treat this as a detector of gross skew -- the kind\n * that invalidates sync.e2e_serving_lag -- and not as a sub-100ms correction.\n *\n * The rigorous bound, if a tighter reading is ever needed: the commit\n * genuinely happened between send and receive, so a `commitTimeMs` outside\n * that window proves skew of at least the amount by which it falls outside,\n * independent of any latency assumption.\n */\nexport function estimateUpstreamClockSkewMs({\n sendTimeMs,\n commitTimeMs,\n receiveTimeMs,\n}: {\n sendTimeMs: number;\n commitTimeMs: number;\n receiveTimeMs: number;\n}): number {\n return commitTimeMs - (sendTimeMs + receiveTimeMs) / 2;\n}\n"],"mappings":";;AAMA,IAAM,uCACJ,QAAQ,IAAI,8CAA8C;AAE5D,IAAa,4BAA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,kBAAkB,KAAK,UAAU;CAEpD;CAEA,sBAA+B,MAAwB;EACrD,MAAM,OAAO,KAAKA,OAAO;EACzB,IAAI,MACF,EAAE,QAAQ,KAAK,kBAAkB,KAAK,UAAU;CAEpD;CAEA,2BAAoC,MAAwB;EAC1D,MAAM,OAAO,KAAKA,OAAO;EACzB,IAAI,MACF,EAAE,QAAQ,4BAA4B,IAAI,CAAC;CAE/C;AACF;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,4BAA4B,EAC1C,YACA,cACA,iBAKS;CACT,OAAO,gBAAgB,aAAa,iBAAiB;AACvD"}
1
+ {"version":3,"file":"recorder.js","names":["#lc","#readinessThresholdMs","#readyGate","#clearTimeout","#readinessTimer","#setReady","#last","#ready"],"sources":["../../../../../../../zero-cache/src/services/replicator/reporter/recorder.ts"],"sourcesContent":["import type {ObservableResult} from '@opentelemetry/api';\nimport type {LogContext, LogLevel} from '@rocicorp/logger';\nimport {resolver} from '@rocicorp/resolver';\nimport {getOrCreateGauge} from '../../../observability/metrics.ts';\nimport type {ReplicationReport} from './report-schema.ts';\n\n// Hook for sanity checking lag reports in development.\nconst LOG_ALL_REPLICATION_REPORTS_AT_DEBUG =\n process.env.ZERO_LOG_ALL_REPLICATION_REPORTS_AT_DEBUG === '1';\n\nexport type ReplicationLagReadinessOptions = {\n readinessThresholdMs?: number | undefined;\n maxReadinessDelayMs?: number | undefined;\n setTimeoutFn?: typeof setTimeout | undefined;\n clearTimeoutFn?: typeof clearTimeout | undefined;\n};\n\nexport class ReplicationReportRecorder {\n readonly #lc: LogContext;\n readonly #readinessThresholdMs: number;\n readonly #readyGate = resolver();\n readonly #clearTimeout: typeof clearTimeout;\n readonly #readinessTimer: ReturnType<typeof setTimeout>;\n #ready = false;\n #last: ReplicationReport | null = null;\n\n constructor(\n lc: LogContext,\n {\n readinessThresholdMs = 1000,\n maxReadinessDelayMs = 600_000,\n setTimeoutFn = setTimeout,\n clearTimeoutFn = clearTimeout,\n }: ReplicationLagReadinessOptions = {},\n ) {\n this.#lc = lc;\n this.#readinessThresholdMs = readinessThresholdMs;\n this.#clearTimeout = clearTimeoutFn;\n\n this.#readinessTimer = setTimeoutFn(\n () =>\n this.#setReady(\n 'warn',\n `timed out waiting for replication lag readiness threshold of ${this.#readinessThresholdMs} ms`,\n ),\n maxReadinessDelayMs,\n );\n // The timer should not keep the process alive.\n this.#readinessTimer?.unref?.();\n }\n\n ready(): Promise<void> {\n return this.#readyGate.promise;\n }\n\n record(report: ReplicationReport) {\n if (isLagReportingDisabledSignal(report)) {\n this.#setReady(\n 'info',\n `replica is caught up (replication lag reporting is disabled upstream)`,\n );\n return;\n }\n this.#last = report;\n\n const {lastTimings} = report;\n if (lastTimings) {\n const total = lastTimings.replicateTimeMs - lastTimings.sendTimeMs;\n if (total > 10_000) {\n const log = this.#ready ? 'warn' : 'info';\n this.#lc[log]?.(`high replication lag: ${total} ms`, {\n ready: this.#ready,\n report,\n });\n } else if (total > 1_000) {\n this.#lc.info?.(`replication lag: ${total} ms`, {\n ready: this.#ready,\n report,\n });\n }\n if (LOG_ALL_REPLICATION_REPORTS_AT_DEBUG) {\n this.#lc.debug?.(`replication lag ${total} ms`, {\n ready: this.#ready,\n report,\n });\n }\n if (!this.#ready && total <= this.#readinessThresholdMs) {\n this.#setReady(\n 'info',\n `replication lag is now below readiness threshold of ${this.#readinessThresholdMs} ms`,\n );\n }\n }\n }\n\n #setReady(log: LogLevel, message: string) {\n if (this.#ready) {\n return;\n }\n this.#lc[log]?.(message, {lastReport: this.#last});\n this.#ready = true;\n this.#clearTimeout(this.#readinessTimer);\n this.#readyGate.resolve();\n\n getOrCreateGauge('replication', 'upstream_lag', {\n description:\n 'Latency from sending an upstream replication report ' +\n 'to receiving it in the replication stream',\n unit: 'millisecond',\n }).addCallback(this.reportUpstreamLag);\n\n getOrCreateGauge('replication', 'replica_lag', {\n description:\n 'Latency from receiving an upstream replication report ' +\n 'to its reaching the replica',\n unit: 'millisecond',\n }).addCallback(this.reportReplicaLag);\n\n getOrCreateGauge('replication', 'total_lag', {\n description:\n 'Latency from sending an upstream replication report to its ' +\n 'reaching the replica. This reflects the actual measured ' +\n 'round-trip of the most recently received report and does not ' +\n 'grow when reports stop arriving.',\n unit: 'millisecond',\n }).addCallback(this.reportTotalLag);\n\n getOrCreateGauge('replication', 'last_total_lag', {\n description:\n 'Latency from sending the most recently received upstream ' +\n 'replication report to its reaching the replica. This is an alias ' +\n 'of replication.total_lag retained for dashboards that explicitly ' +\n 'want the non-extrapolated value.',\n unit: 'millisecond',\n }).addCallback(this.reportLastTotalLag);\n\n getOrCreateGauge('replication', 'upstream_clock_skew', {\n description:\n 'Estimated offset of the upstream database clock relative to ' +\n \"zero-cache's. Positive means upstream is running ahead. \" +\n 'Derived from the lag report round-trip, whose send and receive ' +\n 'times are stamped on our clock either side of the upstream ' +\n 'commit timestamp. This matters because ' +\n 'sync.e2e_serving_lag subtracts an upstream commit timestamp from ' +\n 'a local clock, so a skewed upstream clock biases it directly -- ' +\n 'and an upstream clock running ahead biases it *low*, which looks ' +\n 'like healthy serving rather than a broken measurement.',\n unit: 'millisecond',\n }).addCallback(this.reportUpstreamClockSkew);\n }\n\n readonly reportUpstreamLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.receiveTimeMs - last.sendTimeMs);\n }\n };\n\n readonly reportReplicaLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.replicateTimeMs - last.receiveTimeMs);\n }\n };\n\n readonly reportTotalLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.replicateTimeMs - last.sendTimeMs);\n }\n };\n\n readonly reportLastTotalLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.replicateTimeMs - last.sendTimeMs);\n }\n };\n\n readonly reportUpstreamClockSkew = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(estimateUpstreamClockSkewMs(last));\n }\n };\n}\n\n/**\n * Estimates how far the upstream database's clock is from ours, in\n * milliseconds, positive meaning upstream is ahead.\n *\n * `sendTimeMs` and `receiveTimeMs` are both stamped in the\n * replication-manager, either side of `commitTimeMs`, which upstream stamps.\n * Assuming the upstream commit fell midway through that round trip, the\n * difference between where it actually landed and that midpoint is the clock\n * offset -- the same round-trip estimate NTP uses.\n *\n * The estimate is biased by any asymmetry between the two legs: the outbound\n * leg is a SQL round trip, while the return leg additionally waits on a WAL\n * flush and the walsender (which the pre-PG-17 path notes can add 50~100ms on\n * an idle database). So treat this as a detector of gross skew -- the kind\n * that invalidates sync.e2e_serving_lag -- and not as a sub-100ms correction.\n *\n * The rigorous bound, if a tighter reading is ever needed: the commit\n * genuinely happened between send and receive, so a `commitTimeMs` outside\n * that window proves skew of at least the amount by which it falls outside,\n * independent of any latency assumption.\n */\nexport function estimateUpstreamClockSkewMs({\n sendTimeMs,\n commitTimeMs,\n receiveTimeMs,\n}: {\n sendTimeMs: number;\n commitTimeMs: number;\n receiveTimeMs: number;\n}): number {\n return commitTimeMs - (sendTimeMs + receiveTimeMs) / 2;\n}\n\n/**\n * Returns whether the `report` is the signal sent by the change-streamer,\n * once the subscriber is caught up, to indicate that lag reporting is\n * disabled upstream (and thus readiness cannot be gated on lag reports).\n *\n * This is a lag report with a `nextSendTimeMs` of 0 and no `lastTimings`.\n */\nexport function isLagReportingDisabledSignal(\n report: ReplicationReport,\n): boolean {\n return report.nextSendTimeMs === 0 && report.lastTimings === undefined;\n}\n"],"mappings":";;;AAOA,IAAM,uCACJ,QAAQ,IAAI,8CAA8C;AAS5D,IAAa,4BAAb,MAAuC;CACrC;CACA;CACA,aAAsB,SAAS;CAC/B;CACA;CACA,SAAS;CACT,QAAkC;CAElC,YACE,IACA,EACE,uBAAuB,KACvB,sBAAsB,KACtB,eAAe,YACf,iBAAiB,iBACiB,CAAC,GACrC;EACA,KAAKA,MAAM;EACX,KAAKC,wBAAwB;EAC7B,KAAKE,gBA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AC;IAC3B,oBAAoB,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IAC9C,SAAS,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAAC;IAC5C,EAAE,EAAE,QAAQ,CAAC;IACb,IAAI,EAAE,eAAe,CAAC;IACtB;;;OAGG;IACH,qBAAqB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,WAAW,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC;IAC5C,YAAY,CAAC,EAAE,YAAY,GAAG,SAAS,CAAC;IACxC,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACjC,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACxC,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAClC,CAAC;AAEF,wBAAsB,MAAM,CAC1B,EAAE,EAAE,UAAU,EACd,YAAY,EAAE,YAAY,EAC1B,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,OAAO,EACtB,OAAO,EAAE,aAAa,EACtB,YAAY,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAChC,OAAO,CAAC,kBAAkB,CAAC,CAyL7B"}
@@ -47,12 +47,13 @@ async function runAst(lc, clientSchema, ast, isTransformed, options, yieldProces
47
47
  companionInputs.push(input);
48
48
  return fetchScalarValue(input, childField);
49
49
  };
50
- const { ast: resolvedAst, ignoredScalarHints } = resolveSimpleScalarSubqueries(ast, options.tableSpecs, executor);
50
+ const { ast: resolvedAst, companions, ignoredScalarHints } = resolveSimpleScalarSubqueries(ast, options.tableSpecs, executor);
51
51
  for (const { table, uniqueKeys } of ignoredScalarHints) lc.warn?.(`Ignoring {scalar: true} on the "${table}" subquery: it does not constrain every column of any unique key [${uniqueKeys.map((k) => `(${k.join(", ")})`).join(", ")}] to a literal with "=", so it is not provably limited to one row. The gate runs as a plain EXISTS.`);
52
52
  const pipeline = buildPipeline(resolvedAst, host, "query-id", options.costModel, lc, options.planDebugger);
53
+ const syncedCompanionInputs = companionInputs.filter((_, i) => companions[i].system !== "permissions");
53
54
  function* rowChanges() {
54
55
  try {
55
- for (const input of [pipeline, ...companionInputs]) yield* hydrate(input, hash, clientSchema, zqlSpecs);
56
+ for (const input of [pipeline, ...syncedCompanionInputs]) yield* hydrate(input, hash, clientSchema, zqlSpecs);
56
57
  } finally {
57
58
  for (const input of companionInputs) input.destroy();
58
59
  }
@@ -1 +1 @@
1
- {"version":3,"file":"run-ast.js","names":[],"sources":["../../../../../zero-cache/src/services/run-ast.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\n// @circular-dep-ignore\nimport {astToZQL} from '../../../ast-to-zql/src/ast-to-zql.ts';\n// @circular-dep-ignore\nimport {formatOutput} from '../../../ast-to-zql/src/format.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {sleep} from '../../../shared/src/sleep.ts';\nimport type {AnalyzeQueryResult} from '../../../zero-protocol/src/analyze-query-result.ts';\nimport type {AST, LiteralValue} from '../../../zero-protocol/src/ast.ts';\nimport {mapAST} from '../../../zero-protocol/src/ast.ts';\nimport type {ClientSchema} from '../../../zero-protocol/src/client-schema.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {hashOfAST} from '../../../zero-protocol/src/query-hash.ts';\nimport type {PermissionsConfig} from '../../../zero-schema/src/compiled-permissions.ts';\nimport type {NameMapper} from '../../../zero-schema/src/name-mapper.ts';\nimport {\n buildPipeline,\n type BuilderDelegate,\n} from '../../../zql/src/builder/builder.ts';\nimport {ChangeType} from '../../../zql/src/ivm/change-type.ts';\nimport type {Input} from '../../../zql/src/ivm/operator.ts';\nimport type {ConnectionCostModel} from '../../../zql/src/planner/planner-connection.ts';\nimport type {PlanDebugger} from '../../../zql/src/planner/planner-debug.ts';\nimport type {Database} from '../../../zqlite/src/db.ts';\nimport {resolveSimpleScalarSubqueries} from '../../../zqlite/src/resolve-scalar-subqueries.ts';\nimport type {JWTAuth} from '../auth/auth.ts';\nimport {transformAndHashQuery} from '../auth/read-authorizer.ts';\nimport {computeZqlSpecs} from '../db/lite-tables.ts';\nimport type {LiteAndZqlSpec} from '../db/specs.ts';\nimport {\n fetchScalarValue,\n hydrate,\n type RowChange,\n} from './view-syncer/pipeline-driver.ts';\n\n/**\n * Maximum number of synced rows returned per table, and read rows returned\n * per table/query, when analyzing a query in zero-cache. Rows past this are\n * still counted. Keeping every row can exhaust the heap when analyzing queries\n * over large tables.\n */\nexport const MAX_ANALYZE_ROWS = 1000;\n\nexport type RunAstOptions = {\n applyPermissions?: boolean | undefined;\n auth?: JWTAuth | undefined;\n clientToServerMapper?: NameMapper | undefined;\n costModel?: ConnectionCostModel | undefined;\n db: Database;\n host: BuilderDelegate;\n /**\n * Maximum number of synced rows returned per table. Rows past this are still\n * counted. Defaults to no limit.\n */\n maxSyncedRowsPerTable?: number | undefined;\n permissions?: PermissionsConfig | undefined;\n planDebugger?: PlanDebugger | undefined;\n syncedRows?: boolean | undefined;\n tableSpecs: Map<string, LiteAndZqlSpec>;\n vendedRows?: boolean | undefined;\n};\n\nexport async function runAst(\n lc: LogContext,\n clientSchema: ClientSchema,\n ast: AST,\n isTransformed: boolean,\n options: RunAstOptions,\n yieldProcess: () => Promise<void>,\n): Promise<AnalyzeQueryResult> {\n const {\n clientToServerMapper,\n permissions,\n host,\n db,\n maxSyncedRowsPerTable = Infinity,\n } = options;\n const result: AnalyzeQueryResult = {\n warnings: [],\n syncedRows: undefined,\n syncedRowCount: 0,\n start: 0,\n end: 0,\n elapsed: 0,\n afterPermissions: undefined,\n readRows: undefined,\n readRowCountsByQuery: {},\n readRowCount: undefined,\n };\n\n if (!isTransformed) {\n // map the AST to server names if not already transformed\n ast = mapAST(ast, must(clientToServerMapper));\n }\n if (options.applyPermissions) {\n const auth = options.auth;\n if (!auth) {\n result.warnings.push(\n 'No auth data provided. Permission rules will compare to `NULL` wherever an auth data field is referenced.',\n );\n }\n ast = transformAndHashQuery(\n lc,\n 'clientGroupIDForAnalyze',\n ast,\n must(permissions),\n auth,\n false,\n ).transformedAst;\n result.afterPermissions = await formatOutput(ast.table + astToZQL(ast));\n }\n\n const hash = hashOfAST(ast);\n const zqlSpecs = computeZqlSpecs(lc, db, {includeBackfillingColumns: false});\n\n // Resolve scalar subqueries (e.g. whereExists with {scalar: true}) to\n // literal equality conditions so that SQLite can use indexes effectively.\n // Without this, correlated subqueries get stripped from SQL filters and\n // queries on large tables fall back to full table scans.\n // As in zero-cache, the rows the client reads for the gates are synced too,\n // so the gates' pipelines are kept to be hydrated after the query's.\n const companionInputs: Input[] = [];\n const executor = (\n subqueryAST: AST,\n childField: string,\n ): LiteralValue | null | undefined => {\n const input = buildPipeline(subqueryAST, host, 'scalar-subquery');\n companionInputs.push(input);\n return fetchScalarValue(input, childField);\n };\n\n const {ast: resolvedAst, ignoredScalarHints} = resolveSimpleScalarSubqueries(\n ast,\n options.tableSpecs,\n executor,\n );\n for (const {table, uniqueKeys} of ignoredScalarHints) {\n lc.warn?.(\n `Ignoring {scalar: true} on the \"${table}\" subquery: it does not ` +\n `constrain every column of any unique key ` +\n `[${uniqueKeys.map(k => `(${k.join(', ')})`).join(', ')}] to a ` +\n `literal with \"=\", so it is not provably limited to one row. ` +\n `The gate runs as a plain EXISTS.`,\n );\n }\n\n const pipeline = buildPipeline(\n resolvedAst,\n host,\n 'query-id',\n options.costModel,\n lc,\n options.planDebugger,\n );\n\n // As in zero-cache, the rows of the gates follow the rows of the query.\n function* rowChanges(): Iterable<RowChange | 'yield'> {\n try {\n for (const input of [pipeline, ...companionInputs]) {\n yield* hydrate(input, hash, clientSchema, zqlSpecs);\n }\n } finally {\n for (const input of companionInputs) {\n input.destroy();\n }\n }\n }\n\n const start = performance.now();\n\n let syncedRowCount = 0;\n const rowsByTable: Record<string, Row[]> = {};\n // Dedupe on the row key rather than the whole row to bound memory.\n const seen: Set<string> = new Set();\n const truncatedTables: Set<string> = new Set();\n for (const rowChange of rowChanges()) {\n if (rowChange === 'yield') {\n await yieldProcess();\n continue;\n }\n assert(\n rowChange.type === ChangeType.ADD,\n 'Hydration only handles add row changes',\n );\n\n // yield to other tasks to avoid blocking for too long\n if (syncedRowCount % 10 === 0) {\n await Promise.resolve();\n }\n if (syncedRowCount % 100 === 0) {\n await sleep(1);\n }\n\n const key = JSON.stringify([rowChange.table, rowChange.rowKey]);\n if (seen.has(key)) {\n continue; // skip duplicates\n }\n syncedRowCount++;\n seen.add(key);\n if (options.syncedRows) {\n const rows = (rowsByTable[rowChange.table] ??= []);\n if (rows.length < maxSyncedRowsPerTable) {\n rows.push(rowChange.row);\n } else {\n truncatedTables.add(rowChange.table);\n }\n }\n }\n\n const end = performance.now();\n if (options.syncedRows) {\n result.syncedRows = rowsByTable;\n for (const table of truncatedTables) {\n result.warnings.push(\n `Only the first ${maxSyncedRowsPerTable} synced rows of \"${table}\" are included.`,\n );\n }\n }\n result.start = start;\n result.end = end;\n result.elapsed = end - start;\n\n // Always include the count of synced and vended rows.\n result.syncedRowCount = syncedRowCount;\n result.readRowCountsByQuery = host.debug?.getVendedRowCounts() ?? {};\n let readRowCount = 0;\n for (const c of Object.values(result.readRowCountsByQuery)) {\n for (const v of Object.values(c)) {\n readRowCount += v;\n }\n }\n result.readRowCount = readRowCount;\n result.dbScansByQuery = host.debug?.getNVisitCounts() ?? {};\n result.sqlitePlans = host.debug?.getSQLitePlans() ?? {};\n\n if (options.vendedRows) {\n const readRows = host.debug?.getVendedRows();\n result.readRows = readRows;\n for (const [table, byQuery] of Object.entries(readRows ?? {})) {\n for (const [query, rows] of Object.entries(byQuery)) {\n if ((result.readRowCountsByQuery[table]?.[query] ?? 0) > rows.length) {\n result.warnings.push(\n `Only the first ${rows.length} read rows of \"${table}\" are included for: ${query}`,\n );\n }\n }\n }\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0CA,IAAa,mBAAmB;AAqBhC,eAAsB,OACpB,IACA,cACA,KACA,eACA,SACA,cAC6B;CAC7B,MAAM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,OAAO,cAAc,KAAK,OAAO,eAAe,KAAK,CAAC;CAEtD,IAAI,QAAQ,YAAY;EACtB,MAAM,WAAW,KAAK,OAAO,cAAc;EAC3C,OAAO,WAAW;EAClB,KAAK,MAAM,CAAC,OAAO,YAAY,OAAO,QAAQ,YAAY,CAAC,CAAC,GAC1D,KAAK,MAAM,CAAC,OAAO,SAAS,OAAO,QAAQ,OAAO,GAChD,KAAK,OAAO,qBAAqB,SAAS,UAAU,KAAK,KAAK,QAC5D,OAAO,SAAS,KACd,kBAAkB,KAAK,OAAO,iBAAiB,MAAM,sBAAsB,OAC7E;CAIR;CACA,OAAO;AACT"}
1
+ {"version":3,"file":"run-ast.js","names":[],"sources":["../../../../../zero-cache/src/services/run-ast.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\n// @circular-dep-ignore\nimport {astToZQL} from '../../../ast-to-zql/src/ast-to-zql.ts';\n// @circular-dep-ignore\nimport {formatOutput} from '../../../ast-to-zql/src/format.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {sleep} from '../../../shared/src/sleep.ts';\nimport type {AnalyzeQueryResult} from '../../../zero-protocol/src/analyze-query-result.ts';\nimport type {AST, LiteralValue} from '../../../zero-protocol/src/ast.ts';\nimport {mapAST} from '../../../zero-protocol/src/ast.ts';\nimport type {ClientSchema} from '../../../zero-protocol/src/client-schema.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {hashOfAST} from '../../../zero-protocol/src/query-hash.ts';\nimport type {PermissionsConfig} from '../../../zero-schema/src/compiled-permissions.ts';\nimport type {NameMapper} from '../../../zero-schema/src/name-mapper.ts';\nimport {\n buildPipeline,\n type BuilderDelegate,\n} from '../../../zql/src/builder/builder.ts';\nimport {ChangeType} from '../../../zql/src/ivm/change-type.ts';\nimport type {Input} from '../../../zql/src/ivm/operator.ts';\nimport type {ConnectionCostModel} from '../../../zql/src/planner/planner-connection.ts';\nimport type {PlanDebugger} from '../../../zql/src/planner/planner-debug.ts';\nimport type {Database} from '../../../zqlite/src/db.ts';\nimport {resolveSimpleScalarSubqueries} from '../../../zqlite/src/resolve-scalar-subqueries.ts';\nimport type {JWTAuth} from '../auth/auth.ts';\nimport {transformAndHashQuery} from '../auth/read-authorizer.ts';\nimport {computeZqlSpecs} from '../db/lite-tables.ts';\nimport type {LiteAndZqlSpec} from '../db/specs.ts';\nimport {\n fetchScalarValue,\n hydrate,\n type RowChange,\n} from './view-syncer/pipeline-driver.ts';\n\n/**\n * Maximum number of synced rows returned per table, and read rows returned\n * per table/query, when analyzing a query in zero-cache. Rows past this are\n * still counted. Keeping every row can exhaust the heap when analyzing queries\n * over large tables.\n */\nexport const MAX_ANALYZE_ROWS = 1000;\n\nexport type RunAstOptions = {\n applyPermissions?: boolean | undefined;\n auth?: JWTAuth | undefined;\n clientToServerMapper?: NameMapper | undefined;\n costModel?: ConnectionCostModel | undefined;\n db: Database;\n host: BuilderDelegate;\n /**\n * Maximum number of synced rows returned per table. Rows past this are still\n * counted. Defaults to no limit.\n */\n maxSyncedRowsPerTable?: number | undefined;\n permissions?: PermissionsConfig | undefined;\n planDebugger?: PlanDebugger | undefined;\n syncedRows?: boolean | undefined;\n tableSpecs: Map<string, LiteAndZqlSpec>;\n vendedRows?: boolean | undefined;\n};\n\nexport async function runAst(\n lc: LogContext,\n clientSchema: ClientSchema,\n ast: AST,\n isTransformed: boolean,\n options: RunAstOptions,\n yieldProcess: () => Promise<void>,\n): Promise<AnalyzeQueryResult> {\n const {\n clientToServerMapper,\n permissions,\n host,\n db,\n maxSyncedRowsPerTable = Infinity,\n } = options;\n const result: AnalyzeQueryResult = {\n warnings: [],\n syncedRows: undefined,\n syncedRowCount: 0,\n start: 0,\n end: 0,\n elapsed: 0,\n afterPermissions: undefined,\n readRows: undefined,\n readRowCountsByQuery: {},\n readRowCount: undefined,\n };\n\n if (!isTransformed) {\n // map the AST to server names if not already transformed\n ast = mapAST(ast, must(clientToServerMapper));\n }\n if (options.applyPermissions) {\n const auth = options.auth;\n if (!auth) {\n result.warnings.push(\n 'No auth data provided. Permission rules will compare to `NULL` wherever an auth data field is referenced.',\n );\n }\n ast = transformAndHashQuery(\n lc,\n 'clientGroupIDForAnalyze',\n ast,\n must(permissions),\n auth,\n false,\n ).transformedAst;\n result.afterPermissions = await formatOutput(ast.table + astToZQL(ast));\n }\n\n const hash = hashOfAST(ast);\n const zqlSpecs = computeZqlSpecs(lc, db, {includeBackfillingColumns: false});\n\n // Resolve scalar subqueries (e.g. whereExists with {scalar: true}) to\n // literal equality conditions so that SQLite can use indexes effectively.\n // Without this, correlated subqueries get stripped from SQL filters and\n // queries on large tables fall back to full table scans.\n // As in zero-cache, the rows the client reads for the gates are synced too,\n // so the gates' pipelines are kept to be hydrated after the query's.\n const companionInputs: Input[] = [];\n const executor = (\n subqueryAST: AST,\n childField: string,\n ): LiteralValue | null | undefined => {\n const input = buildPipeline(subqueryAST, host, 'scalar-subquery');\n companionInputs.push(input);\n return fetchScalarValue(input, childField);\n };\n\n const {\n ast: resolvedAst,\n companions,\n ignoredScalarHints,\n } = resolveSimpleScalarSubqueries(ast, options.tableSpecs, executor);\n for (const {table, uniqueKeys} of ignoredScalarHints) {\n lc.warn?.(\n `Ignoring {scalar: true} on the \"${table}\" subquery: it does not ` +\n `constrain every column of any unique key ` +\n `[${uniqueKeys.map(k => `(${k.join(', ')})`).join(', ')}] to a ` +\n `literal with \"=\", so it is not provably limited to one row. ` +\n `The gate runs as a plain EXISTS.`,\n );\n }\n\n const pipeline = buildPipeline(\n resolvedAst,\n host,\n 'query-id',\n options.costModel,\n lc,\n options.planDebugger,\n );\n\n // As in zero-cache, the rows of the gates follow the rows of the query,\n // except for gates from a permission rule: rows read for a rule are never\n // synced.\n const syncedCompanionInputs = companionInputs.filter(\n (_, i) => companions[i].system !== 'permissions',\n );\n function* rowChanges(): Iterable<RowChange | 'yield'> {\n try {\n for (const input of [pipeline, ...syncedCompanionInputs]) {\n yield* hydrate(input, hash, clientSchema, zqlSpecs);\n }\n } finally {\n for (const input of companionInputs) {\n input.destroy();\n }\n }\n }\n\n const start = performance.now();\n\n let syncedRowCount = 0;\n const rowsByTable: Record<string, Row[]> = {};\n // Dedupe on the row key rather than the whole row to bound memory.\n const seen: Set<string> = new Set();\n const truncatedTables: Set<string> = new Set();\n for (const rowChange of rowChanges()) {\n if (rowChange === 'yield') {\n await yieldProcess();\n continue;\n }\n assert(\n rowChange.type === ChangeType.ADD,\n 'Hydration only handles add row changes',\n );\n\n // yield to other tasks to avoid blocking for too long\n if (syncedRowCount % 10 === 0) {\n await Promise.resolve();\n }\n if (syncedRowCount % 100 === 0) {\n await sleep(1);\n }\n\n const key = JSON.stringify([rowChange.table, rowChange.rowKey]);\n if (seen.has(key)) {\n continue; // skip duplicates\n }\n syncedRowCount++;\n seen.add(key);\n if (options.syncedRows) {\n const rows = (rowsByTable[rowChange.table] ??= []);\n if (rows.length < maxSyncedRowsPerTable) {\n rows.push(rowChange.row);\n } else {\n truncatedTables.add(rowChange.table);\n }\n }\n }\n\n const end = performance.now();\n if (options.syncedRows) {\n result.syncedRows = rowsByTable;\n for (const table of truncatedTables) {\n result.warnings.push(\n `Only the first ${maxSyncedRowsPerTable} synced rows of \"${table}\" are included.`,\n );\n }\n }\n result.start = start;\n result.end = end;\n result.elapsed = end - start;\n\n // Always include the count of synced and vended rows.\n result.syncedRowCount = syncedRowCount;\n result.readRowCountsByQuery = host.debug?.getVendedRowCounts() ?? {};\n let readRowCount = 0;\n for (const c of Object.values(result.readRowCountsByQuery)) {\n for (const v of Object.values(c)) {\n readRowCount += v;\n }\n }\n result.readRowCount = readRowCount;\n result.dbScansByQuery = host.debug?.getNVisitCounts() ?? {};\n result.sqlitePlans = host.debug?.getSQLitePlans() ?? {};\n\n if (options.vendedRows) {\n const readRows = host.debug?.getVendedRows();\n result.readRows = readRows;\n for (const [table, byQuery] of Object.entries(readRows ?? {})) {\n for (const [query, rows] of Object.entries(byQuery)) {\n if ((result.readRowCountsByQuery[table]?.[query] ?? 0) > rows.length) {\n result.warnings.push(\n `Only the first ${rows.length} read rows of \"${table}\" are included for: ${query}`,\n );\n }\n }\n }\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0CA,IAAa,mBAAmB;AAqBhC,eAAsB,OACpB,IACA,cACA,KACA,eACA,SACA,cAC6B;CAC7B,MAAM,EACJ,sBACA,aACA,MACA,IACA,wBAAwB,aACtB;CACJ,MAAM,SAA6B;EACjC,UAAU,CAAC;EACX,YAAY,KAAA;EACZ,gBAAgB;EAChB,OAAO;EACP,KAAK;EACL,SAAS;EACT,kBAAkB,KAAA;EAClB,UAAU,KAAA;EACV,sBAAsB,CAAC;EACvB,cAAc,KAAA;CAChB;CAEA,IAAI,CAAC,eAEH,MAAM,OAAO,KAAK,KAAK,oBAAoB,CAAC;CAE9C,IAAI,QAAQ,kBAAkB;EAC5B,MAAM,OAAO,QAAQ;EACrB,IAAI,CAAC,MACH,OAAO,SAAS,KACd,2GACF;EAEF,MAAM,sBACJ,IACA,2BACA,KACA,KAAK,WAAW,GAChB,MACA,KACF,EAAE;EACF,OAAO,mBAAmB,MAAM,aAAa,IAAI,QAAQ,SAAS,GAAG,CAAC;CACxE;CAEA,MAAM,OAAO,UAAU,GAAG;CAC1B,MAAM,WAAW,gBAAgB,IAAI,IAAI,EAAC,2BAA2B,MAAK,CAAC;CAQ3E,MAAM,kBAA2B,CAAC;CAClC,MAAM,YACJ,aACA,eACoC;EACpC,MAAM,QAAQ,cAAc,aAAa,MAAM,iBAAiB;EAChE,gBAAgB,KAAK,KAAK;EAC1B,OAAO,iBAAiB,OAAO,UAAU;CAC3C;CAEA,MAAM,EACJ,KAAK,aACL,YACA,uBACE,8BAA8B,KAAK,QAAQ,YAAY,QAAQ;CACnE,KAAK,MAAM,EAAC,OAAO,gBAAe,oBAChC,GAAG,OACD,mCAAmC,MAAM,oEAEnC,WAAW,KAAI,MAAK,IAAI,EAAE,KAAK,IAAI,EAAE,EAAE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ShD;IAyBD;;;;OAIG;IACH,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEnD;IAED;;;;OAIG;IACH,uBAAuB,IAAI,WAAW,CAAC,MAAM,CAAC,CAE7C;IAED;;OAEG;IACH,4BAA4B,IAAI,IAAI,CAEnC;IA0BD;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAIjD;IAED;;;;;;;;;;OAUG;IACH,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG;QACrB,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,EAAE,MAAM,CAAC;QACnB,OAAO,EAAE,QAAQ,CAAC,SAAS,GAAG,OAAO,CAAC,CAAC;QACvC,uEAAuE;QACvE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;KACrC,CAgCA;CA4vBF;AA2OD;;;;;GAKG;AACH,wBAAgB,OAAO,CACrB,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,YAAY,EAC1B,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GACtC,QAAQ,CAAC,SAAS,GAAG,OAAO,CAAC,CAK/B;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,MAAM,GACjB,YAAY,GAAG,IAAI,GAAG,SAAS,CAYjC"}
1
+ {"version":3,"file":"pipeline-driver.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/pipeline-driver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAQjD,OAAO,KAAK,EAAC,GAAG,EAAE,YAAY,EAAC,MAAM,sCAAsC,CAAC;AAC5E,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,gDAAgD,CAAC;AACjF,OAAO,KAAK,EAAC,GAAG,EAAC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,EACZ,SAAS,CAAC,EAAE,MAAM,EAClB,eAAe,GAAE,uBAA0C,GAC1D,QAAQ,CAAC,SAAS,GAAG,OAAO,CAAC,CAW/B;IAwSD;;;OAGG;IACH,WAAW,CACT,OAAO,EAAE,MAAM,EACf,UAAU,GAAE,kBAAmC,QAShD;IAyBD;;;;OAIG;IACH,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEnD;IAED;;;;OAIG;IACH,uBAAuB,IAAI,WAAW,CAAC,MAAM,CAAC,CAE7C;IAED;;OAEG;IACH,4BAA4B,IAAI,IAAI,CAEnC;IA0BD;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAIjD;IAED;;;;;;;;;;OAUG;IACH,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG;QACrB,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,EAAE,MAAM,CAAC;QACnB,OAAO,EAAE,QAAQ,CAAC,SAAS,GAAG,OAAO,CAAC,CAAC;QACvC,uEAAuE;QACvE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;KACrC,CAgCA;CA4vBF;AA2OD;;;;;GAKG;AACH,wBAAgB,OAAO,CACrB,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,YAAY,EAC1B,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GACtC,QAAQ,CAAC,SAAS,GAAG,OAAO,CAAC,CAK/B;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,MAAM,GACjB,YAAY,GAAG,IAAI,GAAG,SAAS,CAYjC"}
@@ -460,9 +460,12 @@ var PipelineDriver = class {
460
460
  }, planWarnings);
461
461
  const schema = input.getSchema();
462
462
  input.setOutput({ push: (change) => this.#streamPushed(queryID, schema, change) });
463
- for (const pipeline of [input, ...companionInputs]) for (const change of must(this.#streamer).streamNodes(queryID, pipeline.getSchema(), 0, pipeline.fetch({}))) {
464
- if (change !== "yield") hydrationRowCount++;
465
- yield change;
463
+ for (let i = -1; i < companionInputs.length; i++) {
464
+ const pipeline = i < 0 ? input : companionInputs[i];
465
+ for (const change of must(this.#streamer).streamNodes(queryID, i < 0 ? schema : companionStreamSchema(pipeline, companionMeta[i]), 0, pipeline.fetch({}))) {
466
+ if (change !== "yield") hydrationRowCount++;
467
+ yield change;
468
+ }
466
469
  }
467
470
  const hydrationTimeMs = timer.totalElapsed();
468
471
  const hydrationRowsRead = this.#totalRowsRead() - rowsReadAtStart;
@@ -483,7 +486,7 @@ var PipelineDriver = class {
483
486
  for (let i = 0; i < companionMeta.length; i++) {
484
487
  const meta = companionMeta[i];
485
488
  const companionInput = companionInputs[i];
486
- const companionSchema = companionInput.getSchema();
489
+ const companionSchema = companionStreamSchema(companionInput, meta);
487
490
  const { childField, resolvedValue } = meta;
488
491
  companionInput.setOutput({ push: (change) => {
489
492
  let newValue;
@@ -1257,6 +1260,20 @@ function fetchScalarValue(input, childField) {
1257
1260
  if (!node) return;
1258
1261
  return node.row[childField] ?? null;
1259
1262
  }
1263
+ /**
1264
+ * The schema the rows of a companion pipeline are streamed with. A companion is
1265
+ * built from the bare subquery, so its schema says `'client'` even when its
1266
+ * gate came from a permission rule. Rows read for a rule are never synced, so
1267
+ * such a companion takes the gate's system and the Streamer skips it, the rows
1268
+ * behind any EXISTS nested in it included.
1269
+ */
1270
+ function companionStreamSchema(input, { system }) {
1271
+ const schema = input.getSchema();
1272
+ return system === "permissions" ? {
1273
+ ...schema,
1274
+ system
1275
+ } : schema;
1276
+ }
1260
1277
  function buildPrimaryKeys(clientSchema, primaryKeys = /* @__PURE__ */ new Map()) {
1261
1278
  for (const [tableName, { primaryKey }] of Object.entries(clientSchema.tables)) primaryKeys.set(tableName, primaryKey);
1262
1279
  return primaryKeys;