@rocicorp/zero 1.12.0-canary.4 → 1.12.0-canary.6

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 (164) hide show
  1. package/out/analyze-query/src/analyze-cli.d.ts.map +1 -1
  2. package/out/analyze-query/src/analyze-cli.js +2 -1
  3. package/out/analyze-query/src/analyze-cli.js.map +1 -1
  4. package/out/analyze-query/src/bin-transform.js +1 -1
  5. package/out/analyze-query/src/zql-chain.d.ts +23 -0
  6. package/out/analyze-query/src/zql-chain.d.ts.map +1 -0
  7. package/out/analyze-query/src/zql-chain.js +162 -0
  8. package/out/analyze-query/src/zql-chain.js.map +1 -0
  9. package/out/ast-to-zql/src/bin.js +1 -1
  10. package/out/replicache/src/btree/node.js +1 -1
  11. package/out/replicache/src/deleted-clients.d.ts.map +1 -1
  12. package/out/replicache/src/deleted-clients.js +2 -1
  13. package/out/replicache/src/deleted-clients.js.map +1 -1
  14. package/out/replicache/src/kv/sqlite-store.d.ts.map +1 -1
  15. package/out/replicache/src/kv/sqlite-store.js +7 -3
  16. package/out/replicache/src/kv/sqlite-store.js.map +1 -1
  17. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  18. package/out/replicache/src/replicache-impl.js +17 -14
  19. package/out/replicache/src/replicache-impl.js.map +1 -1
  20. package/out/z2s/src/compiler.js +3 -3
  21. package/out/z2s/src/compiler.js.map +1 -1
  22. package/out/zero/package.js +22 -23
  23. package/out/zero/package.js.map +1 -1
  24. package/out/zero-cache/src/auth/load-permissions.js +1 -1
  25. package/out/zero-cache/src/auth/write-authorizer.js +1 -1
  26. package/out/zero-cache/src/config/zero-config.js +2 -2
  27. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  28. package/out/zero-cache/src/scripts/decommission.js +1 -1
  29. package/out/zero-cache/src/scripts/deploy-permissions.js +1 -1
  30. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  31. package/out/zero-cache/src/server/change-streamer.js +33 -10
  32. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  33. package/out/zero-cache/src/server/replicator.js +1 -1
  34. package/out/zero-cache/src/server/syncer.js +3 -3
  35. package/out/zero-cache/src/services/analyze.js +2 -2
  36. package/out/zero-cache/src/services/change-source/change-source.d.ts +7 -0
  37. package/out/zero-cache/src/services/change-source/change-source.d.ts.map +1 -1
  38. package/out/zero-cache/src/services/change-source/common/backfill-manager.js +1 -1
  39. package/out/zero-cache/src/services/change-source/common/replica-restore.d.ts +25 -0
  40. package/out/zero-cache/src/services/change-source/common/replica-restore.d.ts.map +1 -1
  41. package/out/zero-cache/src/services/change-source/common/replica-restore.js.map +1 -1
  42. package/out/zero-cache/src/services/change-source/common/replica-schema.js +1 -1
  43. package/out/zero-cache/src/services/change-source/custom/change-source.js +1 -1
  44. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +1 -1
  45. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts +8 -1
  46. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts.map +1 -1
  47. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +35 -18
  48. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -1
  49. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +12 -0
  50. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  51. package/out/zero-cache/src/services/change-source/pg/change-source.js +66 -9
  52. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  53. package/out/zero-cache/src/services/change-source/pg/fork-request.d.ts +57 -0
  54. package/out/zero-cache/src/services/change-source/pg/fork-request.d.ts.map +1 -0
  55. package/out/zero-cache/src/services/change-source/pg/fork-request.js +54 -0
  56. package/out/zero-cache/src/services/change-source/pg/fork-request.js.map +1 -0
  57. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +4 -4
  58. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +2 -2
  59. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  60. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +6 -0
  61. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  62. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +1 -1
  63. package/out/zero-cache/src/services/change-source/pg/schema/init.js +1 -1
  64. package/out/zero-cache/src/services/change-source/pg/schema/published.js +1 -1
  65. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +1 -1
  66. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts +1 -0
  67. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts.map +1 -1
  68. package/out/zero-cache/src/services/change-source/protocol/current/status.d.ts +3 -0
  69. package/out/zero-cache/src/services/change-source/protocol/current/status.d.ts.map +1 -1
  70. package/out/zero-cache/src/services/change-source/protocol/current/status.js +2 -1
  71. package/out/zero-cache/src/services/change-source/protocol/current/status.js.map +1 -1
  72. package/out/zero-cache/src/services/change-source/protocol/current/upstream.d.ts +1 -0
  73. package/out/zero-cache/src/services/change-source/protocol/current/upstream.d.ts.map +1 -1
  74. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts +1 -3
  75. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts.map +1 -1
  76. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js +6 -6
  77. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js.map +1 -1
  78. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts +41 -2
  79. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts.map +1 -1
  80. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +55 -20
  81. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  82. package/out/zero-cache/src/services/change-streamer/change-log-initializer.js +1 -1
  83. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +9 -0
  84. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  85. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +27 -8
  86. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  87. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +1 -12
  88. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  89. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  90. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.d.ts +29 -2
  91. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.d.ts.map +1 -1
  92. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.js +80 -11
  93. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.js.map +1 -1
  94. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.d.ts +2 -2
  95. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.d.ts.map +1 -1
  96. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js.map +1 -1
  97. package/out/zero-cache/src/services/change-streamer/replica-poller.d.ts +2 -2
  98. package/out/zero-cache/src/services/change-streamer/replica-poller.d.ts.map +1 -1
  99. package/out/zero-cache/src/services/change-streamer/replica-poller.js +1 -1
  100. package/out/zero-cache/src/services/change-streamer/replica-poller.js.map +1 -1
  101. package/out/zero-cache/src/services/change-streamer/server-start-metrics.d.ts +33 -0
  102. package/out/zero-cache/src/services/change-streamer/server-start-metrics.d.ts.map +1 -0
  103. package/out/zero-cache/src/services/change-streamer/server-start-metrics.js +42 -0
  104. package/out/zero-cache/src/services/change-streamer/server-start-metrics.js.map +1 -0
  105. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js +2 -2
  106. package/out/zero-cache/src/services/change-streamer/storer.js +1 -1
  107. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +17 -6
  108. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  109. package/out/zero-cache/src/services/change-streamer/subscriber.js +5 -4
  110. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  111. package/out/zero-cache/src/services/life-cycle.js +1 -1
  112. package/out/zero-cache/src/services/litestream/commands.js +2 -2
  113. package/out/zero-cache/src/services/plannerz.js +2 -2
  114. package/out/zero-cache/src/services/replicator/change-log-db.js +1 -1
  115. package/out/zero-cache/src/services/replicator/incremental-sync.js +3 -3
  116. package/out/zero-cache/src/services/replicator/schema/backfilling.js +1 -1
  117. package/out/zero-cache/src/services/replicator/write-worker.js +3 -3
  118. package/out/zero-cache/src/services/run-ast.js +1 -1
  119. package/out/zero-cache/src/services/statz.js +2 -2
  120. package/out/zero-cache/src/services/view-syncer/cvr-store.js +1 -1
  121. package/out/zero-cache/src/services/view-syncer/inspect-handler.js +1 -1
  122. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +5 -5
  123. package/out/zero-cache/src/services/view-syncer/row-record-cache.js +1 -1
  124. package/out/zero-cache/src/services/view-syncer/snapshotter.js +1 -1
  125. package/out/zero-cache/src/services/view-syncer/view-syncer.js +6 -6
  126. package/out/zero-cache/src/types/profiler.js +1 -1
  127. package/out/zero-client/src/client/version.js +1 -1
  128. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  129. package/out/zero-protocol/src/ast.js +2 -1
  130. package/out/zero-protocol/src/ast.js.map +1 -1
  131. package/out/zero-protocol/src/down.d.ts +27 -27
  132. package/out/zql/src/ivm/cap.d.ts +1 -1
  133. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  134. package/out/zql/src/ivm/cap.js +10 -0
  135. package/out/zql/src/ivm/cap.js.map +1 -1
  136. package/out/zql/src/ivm/data.d.ts +15 -1
  137. package/out/zql/src/ivm/data.d.ts.map +1 -1
  138. package/out/zql/src/ivm/data.js +15 -1
  139. package/out/zql/src/ivm/data.js.map +1 -1
  140. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  141. package/out/zql/src/ivm/exists.js +12 -4
  142. package/out/zql/src/ivm/exists.js.map +1 -1
  143. package/out/zql/src/ivm/filter-operators.d.ts +5 -0
  144. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  145. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  146. package/out/zql/src/ivm/join.d.ts.map +1 -1
  147. package/out/zql/src/ivm/join.js +6 -3
  148. package/out/zql/src/ivm/join.js.map +1 -1
  149. package/out/zql/src/ivm/memory-source.js +1 -1
  150. package/out/zql/src/ivm/operator.d.ts +6 -0
  151. package/out/zql/src/ivm/operator.d.ts.map +1 -1
  152. package/out/zql/src/ivm/operator.js.map +1 -1
  153. package/out/zql/src/ivm/take.d.ts.map +1 -1
  154. package/out/zql/src/ivm/take.js +4 -3
  155. package/out/zql/src/ivm/take.js.map +1 -1
  156. package/out/zqlite/src/table-source.js +1 -1
  157. package/package.json +22 -23
  158. package/out/analyze-query/src/bin-analyze.d.ts +0 -2
  159. package/out/analyze-query/src/bin-analyze.d.ts.map +0 -1
  160. package/out/analyze-query/src/bin-analyze.js +0 -285
  161. package/out/analyze-query/src/bin-analyze.js.map +0 -1
  162. package/out/zero/src/analyze-query.d.ts +0 -3
  163. package/out/zero/src/analyze-query.d.ts.map +0 -1
  164. package/out/zero/src/analyze-query.js +0 -3
@@ -0,0 +1,42 @@
1
+ import { getOrCreateGauge } from "../../observability/metrics.js";
2
+ //#region ../zero-cache/src/services/change-streamer/server-start-metrics.ts
3
+ /**
4
+ * Records where a replication-manager's start goes, reported once it is
5
+ * ready by the `zero.replication.server_start` gauge, in seconds, labeled
6
+ * by `phase` and `kind` (see {@link InitializeKind}). This is for diagnosis,
7
+ * e.g. of why a start is slow, as the process only starts once.
8
+ */
9
+ var ServerStartMetrics = class {
10
+ #phasesMs = /* @__PURE__ */ new Map();
11
+ #kind;
12
+ #ready = false;
13
+ setKind(kind) {
14
+ this.#kind = kind;
15
+ }
16
+ record(phase, ms) {
17
+ if (ms !== void 0 && !this.#ready) this.#phasesMs.set(phase, ms);
18
+ }
19
+ /**
20
+ * Records the `total` (by default, the time since the process started) and
21
+ * starts reporting the gauge. Phases recorded afterwards are ignored.
22
+ */
23
+ ready(totalMs = performance.now()) {
24
+ if (this.#ready) return;
25
+ this.record("total", totalMs);
26
+ this.#ready = true;
27
+ const kind = this.#kind ?? "unknown";
28
+ getOrCreateGauge("replication", "server_start", {
29
+ description: "The duration of each phase of the replication-manager start (fork_wait, restore, catch_up, backup_confirm, and the total from process start to readiness), by the kind of initialization (fork, resume, rmv1, initial_sync).",
30
+ unit: "s"
31
+ }).addCallback((o) => {
32
+ for (const [phase, ms] of this.#phasesMs) o.observe(ms / 1e3, {
33
+ phase,
34
+ kind
35
+ });
36
+ });
37
+ }
38
+ };
39
+ //#endregion
40
+ export { ServerStartMetrics };
41
+
42
+ //# sourceMappingURL=server-start-metrics.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-start-metrics.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/server-start-metrics.ts"],"sourcesContent":["import {getOrCreateGauge} from '../../observability/metrics.ts';\nimport type {InitializeKind} from '../change-source/common/replica-restore.ts';\n\n/**\n * The phases of a replication-manager's start:\n * - `fork_wait`: selecting the replica to restore from (and creating or\n * claiming its slot), including the wait for a forked replica to pass the\n * new slot's position\n * - `restore`: restoring the replica from its backup\n * - `catch_up`: from initialization to the backup-replicator being caught up\n * (i.e. its replication lag readiness)\n * - `backup_confirm`: waiting for the first backup to cover the replica\n * - `total`: from the start of the (change-streamer) process to readiness\n *\n * The phases are not exhaustive (e.g. connecting to and preparing the\n * upstream), and `catch_up` and `backup_confirm` overlap.\n */\nexport type ServerStartPhase =\n | 'fork_wait'\n | 'restore'\n | 'catch_up'\n | 'backup_confirm'\n | 'total';\n\n/**\n * Records where a replication-manager's start goes, reported once it is\n * ready by the `zero.replication.server_start` gauge, in seconds, labeled\n * by `phase` and `kind` (see {@link InitializeKind}). This is for diagnosis,\n * e.g. of why a start is slow, as the process only starts once.\n */\nexport class ServerStartMetrics {\n readonly #phasesMs = new Map<ServerStartPhase, number>();\n #kind: InitializeKind | undefined;\n #ready = false;\n\n setKind(kind: InitializeKind | undefined) {\n this.#kind = kind;\n }\n\n record(phase: ServerStartPhase, ms: number | undefined) {\n if (ms !== undefined && !this.#ready) {\n this.#phasesMs.set(phase, ms);\n }\n }\n\n /**\n * Records the `total` (by default, the time since the process started) and\n * starts reporting the gauge. Phases recorded afterwards are ignored.\n */\n ready(totalMs = performance.now()) {\n if (this.#ready) {\n return;\n }\n this.record('total', totalMs);\n this.#ready = true;\n const kind = this.#kind ?? 'unknown';\n getOrCreateGauge('replication', 'server_start', {\n description:\n 'The duration of each phase of the replication-manager start ' +\n '(fork_wait, restore, catch_up, backup_confirm, and the total ' +\n 'from process start to readiness), by the kind of initialization ' +\n '(fork, resume, rmv1, initial_sync).',\n unit: 's',\n }).addCallback(o => {\n for (const [phase, ms] of this.#phasesMs) {\n o.observe(ms / 1000, {phase, kind});\n }\n });\n }\n}\n"],"mappings":";;;;;;;;AA8BA,IAAa,qBAAb,MAAgC;CAC9B,4BAAqB,IAAI,IAA8B;CACvD;CACA,SAAS;CAET,QAAQ,MAAkC;EACxC,KAAK,QAAQ;CACf;CAEA,OAAO,OAAyB,IAAwB;EACtD,IAAI,OAAO,KAAA,KAAa,CAAC,KAAK,QAC5B,KAAK,UAAU,IAAI,OAAO,EAAE;CAEhC;;;;;CAMA,MAAM,UAAU,YAAY,IAAI,GAAG;EACjC,IAAI,KAAK,QACP;EAEF,KAAK,OAAO,SAAS,OAAO;EAC5B,KAAK,SAAS;EACd,MAAM,OAAO,KAAK,SAAS;EAC3B,iBAAiB,eAAe,gBAAgB;GAC9C,aACE;GAIF,MAAM;EACR,CAAC,CAAC,CAAC,aAAY,MAAK;GAClB,KAAK,MAAM,CAAC,OAAO,OAAO,KAAK,WAC7B,EAAE,QAAQ,KAAK,KAAM;IAAC;IAAO;GAAI,CAAC;EAEtC,CAAC;CACH;AACF"}
@@ -1,9 +1,9 @@
1
1
  import { assert } from "../../../../shared/src/asserts.js";
2
2
  import { must } from "../../../../shared/src/must.js";
3
3
  import { h32 } from "../../../../shared/src/hash.js";
4
- import { readCookieRowCounts, readCookies } from "../replicator/change-log-cookies.js";
5
- import { StatementRunner } from "../../db/statements.js";
6
4
  import { isSQLiteCorruption, logSQLiteCorruptionDiagnostics } from "../../db/sqlite-corruption.js";
5
+ import { StatementRunner } from "../../db/statements.js";
6
+ import { readCookieRowCounts, readCookies } from "../replicator/change-log-cookies.js";
7
7
  import { ChangeLogRebuildRequired, changeLogFileName, changeLogWipeReason, deleteChangeLogDB, openChangeLogDBForWriting, readChangeLogHead, rebuildChangeLogDBForWriting, reconcileChangeLog } from "../replicator/change-log-db.js";
8
8
  import { extractChangeSubstring } from "./change-log-codec.js";
9
9
  import { ChangeLogStreamWriter } from "../replicator/change-log-stream-writer.js";
@@ -9,9 +9,9 @@ import { isDataChange, isSchemaChange } from "../change-source/protocol/current/
9
9
  import "../change-source/protocol/current/downstream.js";
10
10
  import { backfillRequestSchema } from "../change-source/protocol/current/upstream.js";
11
11
  import "../change-source/protocol/current.js";
12
- import { cookieOps } from "../replicator/change-log-cookies.js";
13
12
  import "../../types/pg.js";
14
13
  import { orTimeout } from "../../types/timeout.js";
14
+ import { cookieOps } from "../replicator/change-log-cookies.js";
15
15
  import { READONLY, READ_COMMITTED } from "../../db/mode-enum.js";
16
16
  import { runTx } from "../../db/run-transaction.js";
17
17
  import { Queue } from "../../../../shared/src/queue.js";
@@ -35,13 +35,16 @@ export type SubscriberOptions = {
35
35
  */
36
36
  onBackfillIgnored?: ((columns: string[]) => void) | undefined;
37
37
  /**
38
- * Resolves to whether lag reporting is disabled upstream. If so, a
39
- * {@link LAG_REPORTING_DISABLED} status is sent once the subscriber is
40
- * aligned, as a stand-in for the lag reports that a subscriber would
41
- * otherwise use to determine that it is caught up (e.g. to advertise
42
- * readiness to serve traffic).
38
+ * Resolves, once it is known whether lag reporting is enabled upstream, to
39
+ * the means of requesting a lag report, or to `undefined` if it is disabled.
40
+ *
41
+ * Subscribers use lag reports to determine that they are caught up (e.g. to
42
+ * advertise readiness to serve traffic). Once the subscriber is aligned, a
43
+ * report is therefore requested, so that it is received without waiting out
44
+ * the report interval. If lag reporting is disabled, a
45
+ * {@link LAG_REPORTING_DISABLED} status is sent as a stand-in instead.
43
46
  */
44
- lagReportingDisabled?: Promise<boolean> | undefined;
47
+ lagReports?: Promise<LagReports | undefined> | undefined;
45
48
  /**
46
49
  * Whether the subscriber switches over to another replication-manager
47
50
  * when this one drains (see `DrainTracker`).
@@ -183,6 +186,14 @@ export declare class Subscriber {
183
186
  replicaVersion?: string | undefined;
184
187
  }): void;
185
188
  }
189
+ /** Requests lag reports from upstream (see {@link SubscriberOptions.lagReports}). */
190
+ export type LagReports = {
191
+ /**
192
+ * Requests that a lag report be sent now rather than at the next report
193
+ * interval. Requests are coalesced upstream, e.g. with a report in flight.
194
+ */
195
+ request(): void;
196
+ };
186
197
  /**
187
198
  * Sent to an aligned subscriber when lag reporting is disabled upstream,
188
199
  * signaling that the subscriber is caught up. This is a lag report with a
@@ -1 +1 @@
1
- {"version":3,"file":"subscriber.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/subscriber.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,gCAAgC,CAAC;AAKzD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,6BAA6B,CAAC;AAC9D,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EACV,SAAS,EAET,MAAM,EACN,iBAAiB,EAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAEvD,KAAK,SAAS,GAAG,IAAI,CAAC,OAAO,SAAS,CAAC,CAAC;AAKxC,MAAM,MAAM,iBAAiB,G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AG,WAAW,QAKlD;IAED;;;;;;;;;;OAUG;IACH,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,GAAG,aAAa,UAW9D;IAED,eAAe,CAAC,GAAG,EAAE,SAAS,WAO7B;IAED;;;;;;;;;;;OAWG;IACH,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,QAElB;IAED,KAAK,CACH,KAAK,CAAC,EAAE,SAAS,EACjB,OAAO,CAAC,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE;QAAC,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAC,QAoBhD;CA2GF;AA4CD;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,EAAE,MAGpC,CAAC"}
1
+ {"version":3,"file":"subscriber.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/subscriber.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,gCAAgC,CAAC;AAKzD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,6BAA6B,CAAC;AAC9D,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EACV,SAAS,EAET,MAAM,EACN,iBAAiB,EAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAEvD,KAAK,SAAS,GAAG,IAAI,CAAC,OAAO,SAAS,CAAC,CAAC;AAKxC,MAAM,MAAM,iBAAiB,GAAG;IAC9B,qBAAqB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,oBAAoB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1C,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAEhC;;;;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O7B;IAED;;;;;;;;;;;OAWG;IACH,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,QAElB;IAED,KAAK,CACH,KAAK,CAAC,EAAE,SAAS,EACjB,OAAO,CAAC,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE;QAAC,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAC,QAoBhD;CA6GF;AA4CD,qFAAqF;AACrF,MAAM,MAAM,UAAU,GAAG;IACvB;;;OAGG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,EAAE,MAGpC,CAAC"}
@@ -40,7 +40,7 @@ var Subscriber = class {
40
40
  #backfills;
41
41
  #onAligned;
42
42
  #onBackfillIgnored;
43
- #lagReportingDisabled;
43
+ #lagReports;
44
44
  #aligned = false;
45
45
  #caughtUpWatermark;
46
46
  #caughtUp = resolver();
@@ -59,7 +59,7 @@ var Subscriber = class {
59
59
  this.#backfills = options.backfills;
60
60
  this.#onAligned = options.onAligned;
61
61
  this.#onBackfillIgnored = options.onBackfillIgnored;
62
- this.#lagReportingDisabled = options.lagReportingDisabled;
62
+ this.#lagReports = options.lagReports;
63
63
  }
64
64
  /**
65
65
  * The subscriber's tracked pending backfills, or `undefined` if the
@@ -426,8 +426,9 @@ var Subscriber = class {
426
426
  this.#onAligned?.(this);
427
427
  this.#caughtUpWatermark = this.#watermark;
428
428
  this.#checkCaughtUp();
429
- this.#lagReportingDisabled?.then((disabled) => {
430
- if (disabled) this.sendStatus(LAG_REPORTING_DISABLED);
429
+ this.#lagReports?.then((lagReports) => {
430
+ if (lagReports) lagReports.request();
431
+ else this.sendStatus(LAG_REPORTING_DISABLED);
431
432
  });
432
433
  }
433
434
  break;
@@ -1 +1 @@
1
- {"version":3,"file":"subscriber.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/subscriber.ts"],"sourcesContent":["import {resolver, type Resolver} from '@rocicorp/resolver';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {BigIntJSON} from '../../../../shared/src/bigint-json.ts';\nimport type {Enum} from '../../../../shared/src/enum.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {promiseVoid} from '../../../../shared/src/resolved-promises.ts';\nimport {RingBuffer} from '../../../../shared/src/ring-buffer.ts';\nimport {max} from '../../types/lexi-version.ts';\nimport type {Subscription} from '../../types/subscription.ts';\nimport type {ReplicatorMode} from '../replicator/replicator.ts';\nimport type {BackfillState} from './backfill-state.ts';\nimport type {PreSerializedBatch} from './broadcast.ts';\nimport type {\n ChangeTag,\n Downstream,\n Status,\n WatermarkedChange,\n} from './change-streamer.ts';\nimport type * as ErrorType from './error-type-enum.ts';\n\ntype ErrorType = Enum<typeof ErrorType>;\n\nconst DEFAULT_BACKLOG_HIGH_WATER_BYTES = 16 * 1024 * 1024;\nconst DEFAULT_BACKLOG_LOW_WATER_RATIO = 0.8;\n\nexport type SubscriberOptions = {\n backlogHighWaterBytes?: number | undefined;\n backlogLowWaterRatio?: number | undefined;\n wsBatched?: boolean | undefined;\n\n /**\n * Called whenever the subscriber's acked watermark advances, i.e. when it\n * has confirmed that a commit was durably applied to its replica. The ACK\n * therefore lags the subscriber's replica rather than leading it.\n */\n onAck?: ((watermark: string) => void) | undefined;\n\n /**\n * The subscriber's pending backfills (protocol v8+), which are tracked\n * over the changes sent to the subscriber.\n */\n backfills?: BackfillState | undefined;\n\n /**\n * Called once the subscriber has been sent all changes up to the head of\n * the stream, i.e. its catchup and backlog have been flushed. From this\n * point on, the subscriber's tracked {@link backfills} reflect the same\n * position in the stream as that of the change-streamer.\n */\n onAligned?: ((subscriber: Subscriber) => void) | undefined;\n\n /**\n * Called when a `backfill` or `backfill-completed` message is ignored for\n * some of the subscriber's pending backfill columns, after the subscriber\n * is aligned.\n */\n onBackfillIgnored?: ((columns: string[]) => void) | undefined;\n\n /**\n * Resolves to whether lag reporting is disabled upstream. If so, a\n * {@link LAG_REPORTING_DISABLED} status is sent once the subscriber is\n * aligned, as a stand-in for the lag reports that a subscriber would\n * otherwise use to determine that it is caught up (e.g. to advertise\n * readiness to serve traffic).\n */\n lagReportingDisabled?: Promise<boolean> | undefined;\n\n /**\n * Whether the subscriber switches over to another replication-manager\n * when this one drains (see `DrainTracker`).\n */\n drainable?: boolean | undefined;\n};\n\n/**\n * A status message queued in the backlog, so that it is sent downstream in\n * the order in which it was received relative to (backlogged) changes.\n */\ntype QueuedStatus = {readonly status: string};\n\ntype BacklogEntry = WatermarkedChange | QueuedStatus;\n\nfunction isQueuedStatus(entry: BacklogEntry): entry is QueuedStatus {\n return !Array.isArray(entry);\n}\n\nfunction entryBytes(entry: BacklogEntry): number {\n return isQueuedStatus(entry) ? entry.status.length : entry[2].length;\n}\n\nexport type BacklogFullWait = {\n readonly promise: Promise<void>;\n cancel(): void;\n};\n\nexport type SubscriberStats = {\n processRate: number;\n pending: number;\n backlog: number;\n backlogBytes: number;\n totalBufferedBytes: number;\n missedLastTimeout: boolean;\n};\n\n/**\n * Encapsulates a subscriber to changes. All subscribers start in a\n * \"catchup\" phase in which changes are buffered in a backlog while the\n * storer is queried to send any changes that were committed since the\n * subscriber's watermark. Once the catchup is complete, calls to\n * {@link send()} result in immediately sending the change.\n */\nexport class Subscriber {\n readonly #protocolVersion: number;\n readonly id: string;\n readonly mode: ReplicatorMode;\n readonly drainable: boolean;\n readonly #downstream: Subscription<string | PreSerializedBatch>;\n readonly #wsBatched: boolean;\n #watermark: string;\n #acked: string;\n #backlog: RingBuffer<BacklogEntry> | null;\n // While catchup is running, live changes are buffered here instead of being\n // pushed downstream. RingBuffer lets drainBacklog consume that backlog without\n // shifting an array, which matters when a subscriber is far behind.\n #backlogBytes = 0;\n #backlogInFlightBytes = 0;\n #backlogDrain: Promise<void> | null = null;\n readonly #backlogBackpressure: ByteBackpressureGate;\n readonly #backlogFullWaiters = new Set<Resolver<void>>();\n readonly #onAck: ((watermark: string) => void) | undefined;\n readonly #backfills: BackfillState | undefined;\n readonly #onAligned: ((subscriber: Subscriber) => void) | undefined;\n readonly #onBackfillIgnored: ((columns: string[]) => void) | undefined;\n readonly #lagReportingDisabled: Promise<boolean> | undefined;\n #aligned = false;\n // The watermark to be acked for the subscriber to be caught up, set when\n // it is aligned.\n #caughtUpWatermark: string | undefined;\n readonly #caughtUp = resolver<void>();\n\n constructor(\n protocolVersion: number,\n id: string,\n mode: ReplicatorMode,\n watermark: string,\n downstream: Subscription<string | PreSerializedBatch>,\n options: SubscriberOptions = {},\n ) {\n this.#protocolVersion = protocolVersion;\n this.id = id;\n this.mode = mode;\n this.drainable = options.drainable ?? false;\n this.#downstream = downstream;\n this.#wsBatched = options.wsBatched ?? false;\n this.#watermark = watermark;\n this.#acked = watermark;\n this.#backlog = new RingBuffer();\n this.#backlogBackpressure = new ByteBackpressureGate(\n options.backlogHighWaterBytes ?? DEFAULT_BACKLOG_HIGH_WATER_BYTES,\n options.backlogLowWaterRatio ?? DEFAULT_BACKLOG_LOW_WATER_RATIO,\n );\n this.#onAck = options.onAck;\n this.#backfills = options.backfills;\n this.#onAligned = options.onAligned;\n this.#onBackfillIgnored = options.onBackfillIgnored;\n this.#lagReportingDisabled = options.lagReportingDisabled;\n }\n\n /**\n * The subscriber's tracked pending backfills, or `undefined` if the\n * subscriber does not report them (i.e. protocol < v8).\n */\n get backfills(): BackfillState | undefined {\n return this.#backfills;\n }\n\n /**\n * Whether the subscriber has been sent all changes up to the head of the\n * stream (see {@link SubscriberOptions.onAligned}).\n */\n get aligned(): boolean {\n return this.#aligned;\n }\n\n /**\n * Resolves once the subscriber has caught up, i.e. once it has acked\n * (durably applied) all changes up to the head of the stream at the time\n * it was {@link aligned}. Never resolves if the subscriber is closed first.\n */\n get caughtUp(): Promise<void> {\n return this.#caughtUp.promise;\n }\n\n #trackBackfills(change: WatermarkedChange) {\n if (this.#backfills) {\n const ignored = this.#backfills.applySerialized(change);\n if (ignored.length && this.#aligned) {\n this.#onBackfillIgnored?.(ignored);\n }\n }\n }\n\n get watermark() {\n return this.#watermark;\n }\n\n get acked() {\n return this.#acked;\n }\n\n /**\n * Whether the backlog of live changes buffered during catchup has reached the\n * point at which {@link send()} stops resolving. Past it the subscriber is no\n * longer free: it holds up every subsequent flush, and with no other\n * subscriber to form a majority it stalls replication outright.\n */\n get backlogFull() {\n return (\n this.#bufferedBacklogBytes >= this.#backlogBackpressure.highWaterBytes\n );\n }\n\n /**\n * @returns whether the subscriber is currently sending backlogged messages,\n * vs caught up and sending the \"head\" of the replication stream.\n */\n isBacklogged() {\n return this.#backlog !== null || this.#bufferedBacklogBytes > 0;\n }\n\n /**\n * Resolves the first time {@link backlogFull} becomes true, so that a caller\n * holding the subscriber in catchup can give up at the moment the subscriber\n * starts costing replication rather than on a timer. Call cancel() when the\n * wait is no longer needed so the subscriber does not retain it.\n */\n whenBacklogFull(): BacklogFullWait {\n if (this.backlogFull) {\n return {promise: promiseVoid, cancel() {}};\n }\n const r = resolver<void>();\n this.#backlogFullWaiters.add(r);\n return {\n promise: r.promise,\n cancel: () => {\n this.#backlogFullWaiters.delete(r);\n },\n };\n }\n\n send(change: WatermarkedChange): Promise<void> {\n const [watermark] = change;\n if (watermark > this.#watermark) {\n if (this.#backlog) {\n // During catchup, buffer live changes behind the durable catchup stream.\n // The returned promise applies backpressure if the buffered bytes cross\n // the high water mark.\n this.#pushBacklog(change);\n return this.#maybeWaitForBacklogSpace();\n }\n return this.#sendChange(change);\n }\n return promiseVoid;\n }\n\n sendBatch(\n changes: readonly WatermarkedChange[],\n preSerialized?: PreSerializedBatch | undefined,\n ): Promise<void> {\n if (changes.length === 0) {\n return promiseVoid;\n }\n\n if (\n this.#wsBatched &&\n preSerialized &&\n !this.#backlog &&\n this.#initialized &&\n changes[0][0] > this.#watermark &&\n (this.#protocolVersion >= 5 ||\n changes.every(c => this.supportsMessage(c[1])))\n ) {\n let commitWatermark: string | undefined;\n for (let i = changes.length - 1; i >= 0; i--) {\n if (changes[i][1] === 'commit') {\n commitWatermark = changes[i][0];\n break;\n }\n }\n if (commitWatermark) {\n this.#watermark = commitWatermark;\n }\n for (const change of changes) {\n this.#trackBackfills(change);\n }\n return this.#sendPreSerializedDownstream(preSerialized, commitWatermark);\n }\n\n const promises: Promise<void>[] = [];\n for (const change of changes) {\n const p = this.send(change);\n if (p !== promiseVoid) {\n promises.push(p);\n }\n }\n if (promises.length === 0) {\n return promiseVoid;\n }\n if (promises.length === 1) {\n return promises[0];\n }\n return Promise.all(promises).then(() => {});\n }\n\n #initialized = false;\n\n /**\n * Called once the subscriber's watermark has been validated in the initial\n * catchup process.\n */\n #initialize() {\n if (!this.#initialized) {\n this.#initialized = true;\n // The initial status precedes the catchup, so it bypasses the backlog.\n void this.#sendDownstream(['status', {tag: 'status'}]);\n }\n }\n\n /**\n * Sends a status message (e.g. a lag report) downstream. While the\n * subscriber is catching up, the status is queued in the backlog behind\n * the changes that preceded it in the replication stream, so that the\n * subscriber processes it in stream order. This is necessary for lag\n * reports to measure the lag of the subscriber, including its catchup.\n *\n * Status messages received before the subscriber is initialized are\n * dropped.\n */\n sendStatus(status: Status) {\n if (!this.#initialized) {\n return;\n }\n const downstream: Downstream = ['status', status];\n if (this.#backlog) {\n this.#pushBacklog({status: BigIntJSON.stringify(downstream)});\n return;\n }\n void this.#sendDownstream(downstream);\n }\n\n /**\n * Sends a control status (e.g. `draining` or `restore`) downstream\n * immediately, regardless of whether the subscriber is initialized or\n * catching up. Unlike {@link sendStatus}, such statuses are not tied to a\n * position in the replication stream.\n */\n sendControlStatus(status: Status) {\n void this.#sendDownstream(['status', status]);\n }\n\n /** catchup() is called on ChangeEntries loaded from the store. */\n async catchup(change: WatermarkedChange) {\n this.#initialize();\n await this.#sendChange(change);\n }\n\n /**\n * Marks the Subscribe as \"caught up\" and flushes any backlog of\n * entries that were received during the catchup.\n */\n setCaughtUp(): Promise<void> {\n this.#initialize();\n if (!this.#backlog) {\n return this.#backlogDrain ?? promiseVoid;\n }\n if (!this.#backlogDrain) {\n // Keep #backlog non-null while queued entries are being handed to\n // downstream. That preserves ordering for sends that race with\n // setCaughtUp(): they append to the same backlog instead of bypassing\n // older buffered changes.\n this.#backlogDrain = this.#drainBacklog();\n void this.#backlogDrain.catch(e => this.fail(e));\n }\n return this.#backlogDrain;\n }\n\n async #sendChange(change: WatermarkedChange) {\n const [watermark, tag, json] = change;\n if (watermark <= this.watermark) {\n return;\n }\n if (!this.supportsMessage(tag)) {\n return;\n }\n if (tag === 'commit') {\n this.#watermark = watermark;\n }\n this.#trackBackfills(change);\n const result = await this.#sendStringifiedDownstream(json);\n if (tag === 'commit' && result === 'consumed') {\n this.#advanceAck(watermark);\n }\n }\n\n #advanceAck(watermark: string) {\n // Sends can complete out of order (e.g. the bounded window in\n // #drainBacklog), so the ack only advances monotonically, and listeners\n // are only notified when it does.\n const acked = max(this.#acked, watermark);\n if (acked !== this.#acked) {\n this.#acked = acked;\n this.#onAck?.(acked);\n this.#checkCaughtUp();\n }\n }\n\n #checkCaughtUp() {\n if (\n this.#caughtUpWatermark !== undefined &&\n this.#acked >= this.#caughtUpWatermark\n ) {\n this.#caughtUpWatermark = undefined;\n this.#caughtUp.resolve();\n }\n }\n\n #sendDownstream(downstream: Downstream) {\n return this.#sendStringifiedDownstream(BigIntJSON.stringify(downstream));\n }\n\n async #sendStringifiedDownstream(json: string) {\n const size = json.length;\n this.#pending++;\n this.#pendingBytes += size;\n const {result} = this.#downstream.push(json);\n try {\n return await result;\n } finally {\n this.#pending--;\n this.#pendingBytes -= size;\n this.#processed++;\n }\n }\n\n async #sendPreSerializedDownstream(\n batch: PreSerializedBatch,\n commitWatermark?: string | undefined,\n ): Promise<void> {\n const size = batch.byteLength;\n this.#pending += batch.changes.length;\n this.#pendingBytes += size;\n const {result} = this.#downstream.push(batch);\n try {\n const outcome = await result;\n if (commitWatermark && outcome === 'consumed') {\n this.#advanceAck(commitWatermark);\n }\n } finally {\n this.#pending -= batch.changes.length;\n this.#pendingBytes -= size;\n this.#processed += batch.changes.length;\n }\n }\n\n // `pending` and `processed` stats are tracked by periodically sampling\n // the running totals (by the progress tracker in the Forwarder).\n // This information was originally collected for use in flow control\n // decisions. The final flow control algorithm ended up being simpler\n // than expected and does not actually use this information. However, the\n // stats are still tracked and logged during flow control decisions for\n // debugging, forensics, and potential improvements to the algorithm.\n\n #pending = 0;\n #pendingBytes = 0;\n #processed = 0;\n #samples: {processed: number; timestamp: number}[] = [\n {processed: 0, timestamp: performance.now()},\n ];\n\n /**\n * The number of downstream messages that have yet to be acked.\n */\n get numPending() {\n return this.#pending + this.#backlogCount;\n }\n\n /**\n * The total number of downstream messages that the subscriber has\n * processed (i.e. acked).\n */\n get numProcessed() {\n return this.#processed;\n }\n\n /**\n * Records a new history entry for the number of messages processed,\n * keeping the number of samples bounded to `maxSamples`.\n */\n sampleProcessRate(now: number, maxSamples = 10): this {\n while (this.#samples.length >= maxSamples) {\n this.#samples.shift();\n }\n this.#samples.push({processed: this.#processed, timestamp: now});\n return this;\n }\n\n getStats(): SubscriberStats {\n const pending = this.numPending;\n if (this.#samples.length < 2) {\n return {\n processRate: 0,\n pending,\n backlog: this.#backlogCount,\n backlogBytes: this.#bufferedBacklogBytes,\n totalBufferedBytes: this.#totalBufferedBytes,\n missedLastTimeout: this.#missedLastTimeout,\n };\n }\n const from = this.#samples[0];\n const to = must(this.#samples.at(-1));\n const processed = to.processed - from.processed;\n const seconds = (to.timestamp - from.timestamp) / 1000;\n const processRate = seconds === 0 ? 0 : processed / seconds;\n return {\n processRate,\n pending,\n backlog: this.#backlogCount,\n backlogBytes: this.#bufferedBacklogBytes,\n totalBufferedBytes: this.#totalBufferedBytes,\n missedLastTimeout: this.#missedLastTimeout,\n };\n }\n\n #missedLastTimeout = false;\n #laggingSinceMs: number | undefined;\n\n trackResponseResult(result: 'on-time' | 'timed-out') {\n this.#missedLastTimeout = result === 'timed-out';\n if (result === 'on-time') {\n this.#laggingSinceMs = undefined;\n }\n }\n\n /**\n * Reports the change rate of a slow subscriber (i.e. that missed the last\n * timeout) compared to the change rate of the (slowest) subscriber that\n * responded on time.\n *\n * * `lagging` indicates that this subscriber is slower\n * * `catching-up` indicates that it is faster\n *\n * Returns the total duration in which subscriber has been continuously\n * reported as `lagging`.\n */\n reportChangeRate(now: number, status: 'lagging' | 'catching-up') {\n assert(\n this.#missedLastTimeout,\n `reportChangeRate should only be called for slow subscribers`,\n );\n if (status === 'catching-up') {\n this.#laggingSinceMs = undefined;\n return 0;\n }\n this.#laggingSinceMs ??= now;\n return now - this.#laggingSinceMs;\n }\n\n supportsMessage(tag: ChangeTag) {\n switch (tag) {\n case 'update-table-metadata':\n // update-table-row-key is only understood by subscribers >= protocol v5\n return this.#protocolVersion >= 5;\n }\n return true;\n }\n\n /**\n * Ends the subscription without sending a downstream `['error', ...]`.\n *\n * This is deliberate, and not the same as reporting the failure to the\n * subscriber: `IncrementalSyncer` treats any `['error', ...]` as terminal and\n * shuts down to restore a fresh replica from litestream, whereas a clean end\n * backs off and re-subscribes. The failures routed here -- storer catchup\n * errors, backlog drain errors, backup-monitor errors -- are transient, so a\n * reconnect is the proportionate response and a fleet-wide restore is not.\n *\n * Callers are responsible for logging `err`; it is not carried downstream.\n */\n fail(_err?: unknown) {\n this.close();\n }\n\n close(\n error?: ErrorType,\n message?: string,\n details?: {replicaVersion?: string | undefined},\n ) {\n // Closing the subscriber must also release producers that are blocked on\n // backlog capacity; there is no future drain that could wake them.\n this.#backlog = null;\n this.#backlogBytes = 0;\n this.#backlogBackpressure.releaseAll();\n // The backlog can no longer grow, so nothing would ever resolve these.\n // Waiters re-check backlogFull, which is now false, and see the close.\n this.#resolveBacklogFullWaiters();\n\n if (error !== undefined) {\n // Wait for the ACK of the error message before closing the connection.\n void this.#sendDownstream([\n 'error',\n {type: error, message, ...details},\n ]).finally(() => this.#downstream.cancel());\n } else {\n this.#downstream.cancel();\n }\n }\n\n get #backlogCount() {\n return this.#backlog?.size ?? 0;\n }\n\n get #bufferedBacklogBytes() {\n // Include entries already handed to downstream but not yet consumed. Without\n // this, setCaughtUp() could move bytes out of #backlog faster than the\n // downstream Subscription can process them and release producers too early.\n return this.#backlogBytes + this.#backlogInFlightBytes;\n }\n\n get #totalBufferedBytes() {\n return this.#bufferedBacklogBytes + this.#pendingBytes;\n }\n\n #pushBacklog(entry: BacklogEntry) {\n assert(this.#backlog, 'cannot push to backlog after catchup completed');\n this.#backlog.push(entry);\n this.#backlogBytes += entryBytes(entry);\n if (this.backlogFull) {\n this.#resolveBacklogFullWaiters();\n }\n }\n\n #resolveBacklogFullWaiters() {\n const waiters = [...this.#backlogFullWaiters];\n this.#backlogFullWaiters.clear();\n for (const waiter of waiters) {\n waiter.resolve();\n }\n }\n\n #maybeWaitForBacklogSpace(): Promise<void> {\n return this.#backlogBackpressure.waitForSpace(this.#bufferedBacklogBytes);\n }\n\n async #drainBacklog() {\n const inFlight: {promise: Promise<void>; bytes: number}[] = [];\n let inFlightBytes = 0;\n\n try {\n for (;;) {\n const entry = this.#backlog?.shift();\n if (!entry) {\n const closed = this.#backlog === null;\n this.#backlog = null;\n this.#backlogBytes = 0;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n if (!closed && !this.#aligned) {\n // All changes up to the head have been handed to #sendChange()\n // (and thus tracked), and subsequent changes are sent directly.\n this.#aligned = true;\n this.#onAligned?.(this);\n this.#caughtUpWatermark = this.#watermark;\n this.#checkCaughtUp();\n void this.#lagReportingDisabled?.then(disabled => {\n if (disabled) {\n this.sendStatus(LAG_REPORTING_DISABLED);\n }\n });\n }\n break;\n }\n\n const bytes = entryBytes(entry);\n this.#backlogBytes -= bytes;\n this.#backlogInFlightBytes += bytes;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n\n // Send backlog entries in order, but keep only a bounded byte window in\n // flight. This avoids replacing one unbounded buffer with another inside\n // the downstream Subscription during catchup completion.\n const send = isQueuedStatus(entry)\n ? this.#sendStringifiedDownstream(entry.status).then(() => {})\n : this.#sendChange(entry);\n const promise = send.finally(() => {\n this.#backlogInFlightBytes -= bytes;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n });\n inFlight.push({promise, bytes});\n inFlightBytes += bytes;\n\n while (inFlightBytes >= this.#backlogBackpressure.highWaterBytes) {\n const next = must(inFlight.shift());\n await next.promise;\n inFlightBytes -= next.bytes;\n }\n }\n\n for (const {promise} of inFlight) {\n await promise;\n }\n } finally {\n this.#backlogDrain = null;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n }\n }\n}\n\nclass ByteBackpressureGate {\n readonly highWaterBytes: number;\n readonly #lowWaterBytes: number;\n readonly #waiters: Resolver<void>[] = [];\n\n constructor(highWaterBytes: number, lowWaterRatio: number) {\n this.highWaterBytes = Math.max(1, highWaterBytes);\n this.#lowWaterBytes =\n this.highWaterBytes * Math.min(1, Math.max(0, lowWaterRatio));\n }\n\n waitForSpace(bufferedBytes: number): Promise<void> {\n if (bufferedBytes < this.highWaterBytes) {\n return promiseVoid;\n }\n\n // One waiter represents one send() call that has already appended its\n // change. The producer is released when the backlog falls back below the low\n // water mark or the subscriber closes.\n const r = resolver<void>();\n this.#waiters.push(r);\n return r.promise;\n }\n\n releaseIfUnderLowWater(bufferedBytes: number) {\n if (this.#waiters.length === 0 || bufferedBytes > this.#lowWaterBytes) {\n return;\n }\n\n // Use a low water mark so waiting producers are released in batches instead\n // of waking one at a time around the high water boundary.\n this.releaseAll();\n }\n\n releaseAll() {\n const waiters = this.#waiters.splice(0);\n for (const waiter of waiters) {\n waiter.resolve();\n }\n }\n}\n\n/**\n * Sent to an aligned subscriber when lag reporting is disabled upstream,\n * signaling that the subscriber is caught up. This is a lag report with a\n * `nextSendTimeMs` of 0 and no `lastTimings`, so that no lag is measured\n * (see `isLagReportingDisabledSignal()` in the replicator's recorder).\n */\nexport const LAG_REPORTING_DISABLED: Status = {\n tag: 'status',\n lagReport: {nextSendTimeMs: 0},\n};\n"],"mappings":";;;;;;;;AA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;CACL,WAAW,EAAC,gBAAgB,EAAC;AAC/B"}
1
+ {"version":3,"file":"subscriber.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/subscriber.ts"],"sourcesContent":["import {resolver, type Resolver} from '@rocicorp/resolver';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {BigIntJSON} from '../../../../shared/src/bigint-json.ts';\nimport type {Enum} from '../../../../shared/src/enum.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {promiseVoid} from '../../../../shared/src/resolved-promises.ts';\nimport {RingBuffer} from '../../../../shared/src/ring-buffer.ts';\nimport {max} from '../../types/lexi-version.ts';\nimport type {Subscription} from '../../types/subscription.ts';\nimport type {ReplicatorMode} from '../replicator/replicator.ts';\nimport type {BackfillState} from './backfill-state.ts';\nimport type {PreSerializedBatch} from './broadcast.ts';\nimport type {\n ChangeTag,\n Downstream,\n Status,\n WatermarkedChange,\n} from './change-streamer.ts';\nimport type * as ErrorType from './error-type-enum.ts';\n\ntype ErrorType = Enum<typeof ErrorType>;\n\nconst DEFAULT_BACKLOG_HIGH_WATER_BYTES = 16 * 1024 * 1024;\nconst DEFAULT_BACKLOG_LOW_WATER_RATIO = 0.8;\n\nexport type SubscriberOptions = {\n backlogHighWaterBytes?: number | undefined;\n backlogLowWaterRatio?: number | undefined;\n wsBatched?: boolean | undefined;\n\n /**\n * Called whenever the subscriber's acked watermark advances, i.e. when it\n * has confirmed that a commit was durably applied to its replica. The ACK\n * therefore lags the subscriber's replica rather than leading it.\n */\n onAck?: ((watermark: string) => void) | undefined;\n\n /**\n * The subscriber's pending backfills (protocol v8+), which are tracked\n * over the changes sent to the subscriber.\n */\n backfills?: BackfillState | undefined;\n\n /**\n * Called once the subscriber has been sent all changes up to the head of\n * the stream, i.e. its catchup and backlog have been flushed. From this\n * point on, the subscriber's tracked {@link backfills} reflect the same\n * position in the stream as that of the change-streamer.\n */\n onAligned?: ((subscriber: Subscriber) => void) | undefined;\n\n /**\n * Called when a `backfill` or `backfill-completed` message is ignored for\n * some of the subscriber's pending backfill columns, after the subscriber\n * is aligned.\n */\n onBackfillIgnored?: ((columns: string[]) => void) | undefined;\n\n /**\n * Resolves, once it is known whether lag reporting is enabled upstream, to\n * the means of requesting a lag report, or to `undefined` if it is disabled.\n *\n * Subscribers use lag reports to determine that they are caught up (e.g. to\n * advertise readiness to serve traffic). Once the subscriber is aligned, a\n * report is therefore requested, so that it is received without waiting out\n * the report interval. If lag reporting is disabled, a\n * {@link LAG_REPORTING_DISABLED} status is sent as a stand-in instead.\n */\n lagReports?: Promise<LagReports | undefined> | undefined;\n\n /**\n * Whether the subscriber switches over to another replication-manager\n * when this one drains (see `DrainTracker`).\n */\n drainable?: boolean | undefined;\n};\n\n/**\n * A status message queued in the backlog, so that it is sent downstream in\n * the order in which it was received relative to (backlogged) changes.\n */\ntype QueuedStatus = {readonly status: string};\n\ntype BacklogEntry = WatermarkedChange | QueuedStatus;\n\nfunction isQueuedStatus(entry: BacklogEntry): entry is QueuedStatus {\n return !Array.isArray(entry);\n}\n\nfunction entryBytes(entry: BacklogEntry): number {\n return isQueuedStatus(entry) ? entry.status.length : entry[2].length;\n}\n\nexport type BacklogFullWait = {\n readonly promise: Promise<void>;\n cancel(): void;\n};\n\nexport type SubscriberStats = {\n processRate: number;\n pending: number;\n backlog: number;\n backlogBytes: number;\n totalBufferedBytes: number;\n missedLastTimeout: boolean;\n};\n\n/**\n * Encapsulates a subscriber to changes. All subscribers start in a\n * \"catchup\" phase in which changes are buffered in a backlog while the\n * storer is queried to send any changes that were committed since the\n * subscriber's watermark. Once the catchup is complete, calls to\n * {@link send()} result in immediately sending the change.\n */\nexport class Subscriber {\n readonly #protocolVersion: number;\n readonly id: string;\n readonly mode: ReplicatorMode;\n readonly drainable: boolean;\n readonly #downstream: Subscription<string | PreSerializedBatch>;\n readonly #wsBatched: boolean;\n #watermark: string;\n #acked: string;\n #backlog: RingBuffer<BacklogEntry> | null;\n // While catchup is running, live changes are buffered here instead of being\n // pushed downstream. RingBuffer lets drainBacklog consume that backlog without\n // shifting an array, which matters when a subscriber is far behind.\n #backlogBytes = 0;\n #backlogInFlightBytes = 0;\n #backlogDrain: Promise<void> | null = null;\n readonly #backlogBackpressure: ByteBackpressureGate;\n readonly #backlogFullWaiters = new Set<Resolver<void>>();\n readonly #onAck: ((watermark: string) => void) | undefined;\n readonly #backfills: BackfillState | undefined;\n readonly #onAligned: ((subscriber: Subscriber) => void) | undefined;\n readonly #onBackfillIgnored: ((columns: string[]) => void) | undefined;\n readonly #lagReports: Promise<LagReports | undefined> | undefined;\n #aligned = false;\n // The watermark to be acked for the subscriber to be caught up, set when\n // it is aligned.\n #caughtUpWatermark: string | undefined;\n readonly #caughtUp = resolver<void>();\n\n constructor(\n protocolVersion: number,\n id: string,\n mode: ReplicatorMode,\n watermark: string,\n downstream: Subscription<string | PreSerializedBatch>,\n options: SubscriberOptions = {},\n ) {\n this.#protocolVersion = protocolVersion;\n this.id = id;\n this.mode = mode;\n this.drainable = options.drainable ?? false;\n this.#downstream = downstream;\n this.#wsBatched = options.wsBatched ?? false;\n this.#watermark = watermark;\n this.#acked = watermark;\n this.#backlog = new RingBuffer();\n this.#backlogBackpressure = new ByteBackpressureGate(\n options.backlogHighWaterBytes ?? DEFAULT_BACKLOG_HIGH_WATER_BYTES,\n options.backlogLowWaterRatio ?? DEFAULT_BACKLOG_LOW_WATER_RATIO,\n );\n this.#onAck = options.onAck;\n this.#backfills = options.backfills;\n this.#onAligned = options.onAligned;\n this.#onBackfillIgnored = options.onBackfillIgnored;\n this.#lagReports = options.lagReports;\n }\n\n /**\n * The subscriber's tracked pending backfills, or `undefined` if the\n * subscriber does not report them (i.e. protocol < v8).\n */\n get backfills(): BackfillState | undefined {\n return this.#backfills;\n }\n\n /**\n * Whether the subscriber has been sent all changes up to the head of the\n * stream (see {@link SubscriberOptions.onAligned}).\n */\n get aligned(): boolean {\n return this.#aligned;\n }\n\n /**\n * Resolves once the subscriber has caught up, i.e. once it has acked\n * (durably applied) all changes up to the head of the stream at the time\n * it was {@link aligned}. Never resolves if the subscriber is closed first.\n */\n get caughtUp(): Promise<void> {\n return this.#caughtUp.promise;\n }\n\n #trackBackfills(change: WatermarkedChange) {\n if (this.#backfills) {\n const ignored = this.#backfills.applySerialized(change);\n if (ignored.length && this.#aligned) {\n this.#onBackfillIgnored?.(ignored);\n }\n }\n }\n\n get watermark() {\n return this.#watermark;\n }\n\n get acked() {\n return this.#acked;\n }\n\n /**\n * Whether the backlog of live changes buffered during catchup has reached the\n * point at which {@link send()} stops resolving. Past it the subscriber is no\n * longer free: it holds up every subsequent flush, and with no other\n * subscriber to form a majority it stalls replication outright.\n */\n get backlogFull() {\n return (\n this.#bufferedBacklogBytes >= this.#backlogBackpressure.highWaterBytes\n );\n }\n\n /**\n * @returns whether the subscriber is currently sending backlogged messages,\n * vs caught up and sending the \"head\" of the replication stream.\n */\n isBacklogged() {\n return this.#backlog !== null || this.#bufferedBacklogBytes > 0;\n }\n\n /**\n * Resolves the first time {@link backlogFull} becomes true, so that a caller\n * holding the subscriber in catchup can give up at the moment the subscriber\n * starts costing replication rather than on a timer. Call cancel() when the\n * wait is no longer needed so the subscriber does not retain it.\n */\n whenBacklogFull(): BacklogFullWait {\n if (this.backlogFull) {\n return {promise: promiseVoid, cancel() {}};\n }\n const r = resolver<void>();\n this.#backlogFullWaiters.add(r);\n return {\n promise: r.promise,\n cancel: () => {\n this.#backlogFullWaiters.delete(r);\n },\n };\n }\n\n send(change: WatermarkedChange): Promise<void> {\n const [watermark] = change;\n if (watermark > this.#watermark) {\n if (this.#backlog) {\n // During catchup, buffer live changes behind the durable catchup stream.\n // The returned promise applies backpressure if the buffered bytes cross\n // the high water mark.\n this.#pushBacklog(change);\n return this.#maybeWaitForBacklogSpace();\n }\n return this.#sendChange(change);\n }\n return promiseVoid;\n }\n\n sendBatch(\n changes: readonly WatermarkedChange[],\n preSerialized?: PreSerializedBatch | undefined,\n ): Promise<void> {\n if (changes.length === 0) {\n return promiseVoid;\n }\n\n if (\n this.#wsBatched &&\n preSerialized &&\n !this.#backlog &&\n this.#initialized &&\n changes[0][0] > this.#watermark &&\n (this.#protocolVersion >= 5 ||\n changes.every(c => this.supportsMessage(c[1])))\n ) {\n let commitWatermark: string | undefined;\n for (let i = changes.length - 1; i >= 0; i--) {\n if (changes[i][1] === 'commit') {\n commitWatermark = changes[i][0];\n break;\n }\n }\n if (commitWatermark) {\n this.#watermark = commitWatermark;\n }\n for (const change of changes) {\n this.#trackBackfills(change);\n }\n return this.#sendPreSerializedDownstream(preSerialized, commitWatermark);\n }\n\n const promises: Promise<void>[] = [];\n for (const change of changes) {\n const p = this.send(change);\n if (p !== promiseVoid) {\n promises.push(p);\n }\n }\n if (promises.length === 0) {\n return promiseVoid;\n }\n if (promises.length === 1) {\n return promises[0];\n }\n return Promise.all(promises).then(() => {});\n }\n\n #initialized = false;\n\n /**\n * Called once the subscriber's watermark has been validated in the initial\n * catchup process.\n */\n #initialize() {\n if (!this.#initialized) {\n this.#initialized = true;\n // The initial status precedes the catchup, so it bypasses the backlog.\n void this.#sendDownstream(['status', {tag: 'status'}]);\n }\n }\n\n /**\n * Sends a status message (e.g. a lag report) downstream. While the\n * subscriber is catching up, the status is queued in the backlog behind\n * the changes that preceded it in the replication stream, so that the\n * subscriber processes it in stream order. This is necessary for lag\n * reports to measure the lag of the subscriber, including its catchup.\n *\n * Status messages received before the subscriber is initialized are\n * dropped.\n */\n sendStatus(status: Status) {\n if (!this.#initialized) {\n return;\n }\n const downstream: Downstream = ['status', status];\n if (this.#backlog) {\n this.#pushBacklog({status: BigIntJSON.stringify(downstream)});\n return;\n }\n void this.#sendDownstream(downstream);\n }\n\n /**\n * Sends a control status (e.g. `draining` or `restore`) downstream\n * immediately, regardless of whether the subscriber is initialized or\n * catching up. Unlike {@link sendStatus}, such statuses are not tied to a\n * position in the replication stream.\n */\n sendControlStatus(status: Status) {\n void this.#sendDownstream(['status', status]);\n }\n\n /** catchup() is called on ChangeEntries loaded from the store. */\n async catchup(change: WatermarkedChange) {\n this.#initialize();\n await this.#sendChange(change);\n }\n\n /**\n * Marks the Subscribe as \"caught up\" and flushes any backlog of\n * entries that were received during the catchup.\n */\n setCaughtUp(): Promise<void> {\n this.#initialize();\n if (!this.#backlog) {\n return this.#backlogDrain ?? promiseVoid;\n }\n if (!this.#backlogDrain) {\n // Keep #backlog non-null while queued entries are being handed to\n // downstream. That preserves ordering for sends that race with\n // setCaughtUp(): they append to the same backlog instead of bypassing\n // older buffered changes.\n this.#backlogDrain = this.#drainBacklog();\n void this.#backlogDrain.catch(e => this.fail(e));\n }\n return this.#backlogDrain;\n }\n\n async #sendChange(change: WatermarkedChange) {\n const [watermark, tag, json] = change;\n if (watermark <= this.watermark) {\n return;\n }\n if (!this.supportsMessage(tag)) {\n return;\n }\n if (tag === 'commit') {\n this.#watermark = watermark;\n }\n this.#trackBackfills(change);\n const result = await this.#sendStringifiedDownstream(json);\n if (tag === 'commit' && result === 'consumed') {\n this.#advanceAck(watermark);\n }\n }\n\n #advanceAck(watermark: string) {\n // Sends can complete out of order (e.g. the bounded window in\n // #drainBacklog), so the ack only advances monotonically, and listeners\n // are only notified when it does.\n const acked = max(this.#acked, watermark);\n if (acked !== this.#acked) {\n this.#acked = acked;\n this.#onAck?.(acked);\n this.#checkCaughtUp();\n }\n }\n\n #checkCaughtUp() {\n if (\n this.#caughtUpWatermark !== undefined &&\n this.#acked >= this.#caughtUpWatermark\n ) {\n this.#caughtUpWatermark = undefined;\n this.#caughtUp.resolve();\n }\n }\n\n #sendDownstream(downstream: Downstream) {\n return this.#sendStringifiedDownstream(BigIntJSON.stringify(downstream));\n }\n\n async #sendStringifiedDownstream(json: string) {\n const size = json.length;\n this.#pending++;\n this.#pendingBytes += size;\n const {result} = this.#downstream.push(json);\n try {\n return await result;\n } finally {\n this.#pending--;\n this.#pendingBytes -= size;\n this.#processed++;\n }\n }\n\n async #sendPreSerializedDownstream(\n batch: PreSerializedBatch,\n commitWatermark?: string | undefined,\n ): Promise<void> {\n const size = batch.byteLength;\n this.#pending += batch.changes.length;\n this.#pendingBytes += size;\n const {result} = this.#downstream.push(batch);\n try {\n const outcome = await result;\n if (commitWatermark && outcome === 'consumed') {\n this.#advanceAck(commitWatermark);\n }\n } finally {\n this.#pending -= batch.changes.length;\n this.#pendingBytes -= size;\n this.#processed += batch.changes.length;\n }\n }\n\n // `pending` and `processed` stats are tracked by periodically sampling\n // the running totals (by the progress tracker in the Forwarder).\n // This information was originally collected for use in flow control\n // decisions. The final flow control algorithm ended up being simpler\n // than expected and does not actually use this information. However, the\n // stats are still tracked and logged during flow control decisions for\n // debugging, forensics, and potential improvements to the algorithm.\n\n #pending = 0;\n #pendingBytes = 0;\n #processed = 0;\n #samples: {processed: number; timestamp: number}[] = [\n {processed: 0, timestamp: performance.now()},\n ];\n\n /**\n * The number of downstream messages that have yet to be acked.\n */\n get numPending() {\n return this.#pending + this.#backlogCount;\n }\n\n /**\n * The total number of downstream messages that the subscriber has\n * processed (i.e. acked).\n */\n get numProcessed() {\n return this.#processed;\n }\n\n /**\n * Records a new history entry for the number of messages processed,\n * keeping the number of samples bounded to `maxSamples`.\n */\n sampleProcessRate(now: number, maxSamples = 10): this {\n while (this.#samples.length >= maxSamples) {\n this.#samples.shift();\n }\n this.#samples.push({processed: this.#processed, timestamp: now});\n return this;\n }\n\n getStats(): SubscriberStats {\n const pending = this.numPending;\n if (this.#samples.length < 2) {\n return {\n processRate: 0,\n pending,\n backlog: this.#backlogCount,\n backlogBytes: this.#bufferedBacklogBytes,\n totalBufferedBytes: this.#totalBufferedBytes,\n missedLastTimeout: this.#missedLastTimeout,\n };\n }\n const from = this.#samples[0];\n const to = must(this.#samples.at(-1));\n const processed = to.processed - from.processed;\n const seconds = (to.timestamp - from.timestamp) / 1000;\n const processRate = seconds === 0 ? 0 : processed / seconds;\n return {\n processRate,\n pending,\n backlog: this.#backlogCount,\n backlogBytes: this.#bufferedBacklogBytes,\n totalBufferedBytes: this.#totalBufferedBytes,\n missedLastTimeout: this.#missedLastTimeout,\n };\n }\n\n #missedLastTimeout = false;\n #laggingSinceMs: number | undefined;\n\n trackResponseResult(result: 'on-time' | 'timed-out') {\n this.#missedLastTimeout = result === 'timed-out';\n if (result === 'on-time') {\n this.#laggingSinceMs = undefined;\n }\n }\n\n /**\n * Reports the change rate of a slow subscriber (i.e. that missed the last\n * timeout) compared to the change rate of the (slowest) subscriber that\n * responded on time.\n *\n * * `lagging` indicates that this subscriber is slower\n * * `catching-up` indicates that it is faster\n *\n * Returns the total duration in which subscriber has been continuously\n * reported as `lagging`.\n */\n reportChangeRate(now: number, status: 'lagging' | 'catching-up') {\n assert(\n this.#missedLastTimeout,\n `reportChangeRate should only be called for slow subscribers`,\n );\n if (status === 'catching-up') {\n this.#laggingSinceMs = undefined;\n return 0;\n }\n this.#laggingSinceMs ??= now;\n return now - this.#laggingSinceMs;\n }\n\n supportsMessage(tag: ChangeTag) {\n switch (tag) {\n case 'update-table-metadata':\n // update-table-row-key is only understood by subscribers >= protocol v5\n return this.#protocolVersion >= 5;\n }\n return true;\n }\n\n /**\n * Ends the subscription without sending a downstream `['error', ...]`.\n *\n * This is deliberate, and not the same as reporting the failure to the\n * subscriber: `IncrementalSyncer` treats any `['error', ...]` as terminal and\n * shuts down to restore a fresh replica from litestream, whereas a clean end\n * backs off and re-subscribes. The failures routed here -- storer catchup\n * errors, backlog drain errors, backup-monitor errors -- are transient, so a\n * reconnect is the proportionate response and a fleet-wide restore is not.\n *\n * Callers are responsible for logging `err`; it is not carried downstream.\n */\n fail(_err?: unknown) {\n this.close();\n }\n\n close(\n error?: ErrorType,\n message?: string,\n details?: {replicaVersion?: string | undefined},\n ) {\n // Closing the subscriber must also release producers that are blocked on\n // backlog capacity; there is no future drain that could wake them.\n this.#backlog = null;\n this.#backlogBytes = 0;\n this.#backlogBackpressure.releaseAll();\n // The backlog can no longer grow, so nothing would ever resolve these.\n // Waiters re-check backlogFull, which is now false, and see the close.\n this.#resolveBacklogFullWaiters();\n\n if (error !== undefined) {\n // Wait for the ACK of the error message before closing the connection.\n void this.#sendDownstream([\n 'error',\n {type: error, message, ...details},\n ]).finally(() => this.#downstream.cancel());\n } else {\n this.#downstream.cancel();\n }\n }\n\n get #backlogCount() {\n return this.#backlog?.size ?? 0;\n }\n\n get #bufferedBacklogBytes() {\n // Include entries already handed to downstream but not yet consumed. Without\n // this, setCaughtUp() could move bytes out of #backlog faster than the\n // downstream Subscription can process them and release producers too early.\n return this.#backlogBytes + this.#backlogInFlightBytes;\n }\n\n get #totalBufferedBytes() {\n return this.#bufferedBacklogBytes + this.#pendingBytes;\n }\n\n #pushBacklog(entry: BacklogEntry) {\n assert(this.#backlog, 'cannot push to backlog after catchup completed');\n this.#backlog.push(entry);\n this.#backlogBytes += entryBytes(entry);\n if (this.backlogFull) {\n this.#resolveBacklogFullWaiters();\n }\n }\n\n #resolveBacklogFullWaiters() {\n const waiters = [...this.#backlogFullWaiters];\n this.#backlogFullWaiters.clear();\n for (const waiter of waiters) {\n waiter.resolve();\n }\n }\n\n #maybeWaitForBacklogSpace(): Promise<void> {\n return this.#backlogBackpressure.waitForSpace(this.#bufferedBacklogBytes);\n }\n\n async #drainBacklog() {\n const inFlight: {promise: Promise<void>; bytes: number}[] = [];\n let inFlightBytes = 0;\n\n try {\n for (;;) {\n const entry = this.#backlog?.shift();\n if (!entry) {\n const closed = this.#backlog === null;\n this.#backlog = null;\n this.#backlogBytes = 0;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n if (!closed && !this.#aligned) {\n // All changes up to the head have been handed to #sendChange()\n // (and thus tracked), and subsequent changes are sent directly.\n this.#aligned = true;\n this.#onAligned?.(this);\n this.#caughtUpWatermark = this.#watermark;\n this.#checkCaughtUp();\n void this.#lagReports?.then(lagReports => {\n if (lagReports) {\n lagReports.request();\n } else {\n this.sendStatus(LAG_REPORTING_DISABLED);\n }\n });\n }\n break;\n }\n\n const bytes = entryBytes(entry);\n this.#backlogBytes -= bytes;\n this.#backlogInFlightBytes += bytes;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n\n // Send backlog entries in order, but keep only a bounded byte window in\n // flight. This avoids replacing one unbounded buffer with another inside\n // the downstream Subscription during catchup completion.\n const send = isQueuedStatus(entry)\n ? this.#sendStringifiedDownstream(entry.status).then(() => {})\n : this.#sendChange(entry);\n const promise = send.finally(() => {\n this.#backlogInFlightBytes -= bytes;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n });\n inFlight.push({promise, bytes});\n inFlightBytes += bytes;\n\n while (inFlightBytes >= this.#backlogBackpressure.highWaterBytes) {\n const next = must(inFlight.shift());\n await next.promise;\n inFlightBytes -= next.bytes;\n }\n }\n\n for (const {promise} of inFlight) {\n await promise;\n }\n } finally {\n this.#backlogDrain = null;\n this.#backlogBackpressure.releaseIfUnderLowWater(\n this.#bufferedBacklogBytes,\n );\n }\n }\n}\n\nclass ByteBackpressureGate {\n readonly highWaterBytes: number;\n readonly #lowWaterBytes: number;\n readonly #waiters: Resolver<void>[] = [];\n\n constructor(highWaterBytes: number, lowWaterRatio: number) {\n this.highWaterBytes = Math.max(1, highWaterBytes);\n this.#lowWaterBytes =\n this.highWaterBytes * Math.min(1, Math.max(0, lowWaterRatio));\n }\n\n waitForSpace(bufferedBytes: number): Promise<void> {\n if (bufferedBytes < this.highWaterBytes) {\n return promiseVoid;\n }\n\n // One waiter represents one send() call that has already appended its\n // change. The producer is released when the backlog falls back below the low\n // water mark or the subscriber closes.\n const r = resolver<void>();\n this.#waiters.push(r);\n return r.promise;\n }\n\n releaseIfUnderLowWater(bufferedBytes: number) {\n if (this.#waiters.length === 0 || bufferedBytes > this.#lowWaterBytes) {\n return;\n }\n\n // Use a low water mark so waiting producers are released in batches instead\n // of waking one at a time around the high water boundary.\n this.releaseAll();\n }\n\n releaseAll() {\n const waiters = this.#waiters.splice(0);\n for (const waiter of waiters) {\n waiter.resolve();\n }\n }\n}\n\n/** Requests lag reports from upstream (see {@link SubscriberOptions.lagReports}). */\nexport type LagReports = {\n /**\n * Requests that a lag report be sent now rather than at the next report\n * interval. Requests are coalesced upstream, e.g. with a report in flight.\n */\n request(): void;\n};\n\n/**\n * Sent to an aligned subscriber when lag reporting is disabled upstream,\n * signaling that the subscriber is caught up. This is a lag report with a\n * `nextSendTimeMs` of 0 and no `lastTimings`, so that no lag is measured\n * (see `isLagReportingDisabledSignal()` in the replicator's recorder).\n */\nexport const LAG_REPORTING_DISABLED: Status = {\n tag: 'status',\n lagReport: {nextSendTimeMs: 0},\n};\n"],"mappings":";;;;;;;;AAsBA,IAAM,mCAAmC;AACzC,IAAM,kCAAkC;AA8DxC,SAAS,eAAe,OAA4C;CAClE,OAAO,CAAC,MAAM,QAAQ,KAAK;AAC7B;AAEA,SAAS,WAAW,OAA6B;CAC/C,OAAO,eAAe,KAAK,IAAI,MAAM,OAAO,SAAS,MAAM,EAAE,CAAC;AAChE;;;;;;;;AAuBA,IAAa,aAAb,MAAwB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAIA,gBAAgB;CAChB,wBAAwB;CACxB,gBAAsC;CACtC;CACA,sCAA+B,IAAI,IAAoB;CACvD;CACA;CACA;CACA;CACA;CACA,WAAW;CAGX;CACA,YAAqB,SAAe;CAEpC,YACE,iBACA,IACA,MACA,WACA,YACA,UAA6B,CAAC,GAC9B;EACA,KAAK,mBAAmB;EACxB,KAAK,KAAK;EACV,KAAK,OAAO;EACZ,KAAK,YAAY,QAAQ,aAAa;EACtC,KAAK,cAAc;EACnB,KAAK,aAAa,QAAQ,aAAa;EACvC,KAAK,aAAa;EAClB,KAAK,SAAS;EACd,KAAK,WAAW,IAAI,WAAW;EAC/B,KAAK,uBAAuB,IAAI,qBAC9B,QAAQ,yBAAyB,kCACjC,QAAQ,wBAAwB,+BAClC;EACA,KAAK,SAAS,QAAQ;EACtB,KAAK,aAAa,QAAQ;EAC1B,KAAK,aAAa,QAAQ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G,aAAa,CAAC;CAChE;CAEA,aAAa,eAAsC;EACjD,IAAI,gBAAgB,KAAK,gBACvB,OAAO;EAMT,MAAM,IAAI,SAAe;EACzB,KAAK,SAAS,KAAK,CAAC;EACpB,OAAO,EAAE;CACX;CAEA,uBAAuB,eAAuB;EAC5C,IAAI,KAAK,SAAS,WAAW,KAAK,gBAAgB,KAAK,gBACrD;EAKF,KAAK,WAAW;CAClB;CAEA,aAAa;EACX,MAAM,UAAU,KAAK,SAAS,OAAO,CAAC;EACtC,KAAK,MAAM,UAAU,SACnB,OAAO,QAAQ;CAEnB;AACF;;;;;;;AAiBA,IAAa,yBAAiC;CAC5C,KAAK;CACL,WAAW,EAAC,gBAAgB,EAAC;AAC/B"}
@@ -1,8 +1,8 @@
1
1
  import { AbortError } from "../../../shared/src/abort-error.js";
2
2
  import { singleProcessMode } from "../types/processes.js";
3
- import { LONG_DURATION_HISTOGRAM_BOUNDARIES_S, getOrCreateHistogram } from "../observability/metrics.js";
4
3
  import { ConfigurationError } from "../types/configuration-error.js";
5
4
  import { logLastChanceSQLiteCorruptionDiagnostics } from "../db/sqlite-corruption.js";
5
+ import { LONG_DURATION_HISTOGRAM_BOUNDARIES_S, getOrCreateHistogram } from "../observability/metrics.js";
6
6
  import { orTimeout } from "../types/timeout.js";
7
7
  import { RunningState } from "./running-state.js";
8
8
  import { resolver } from "@rocicorp/resolver";
@@ -2,9 +2,9 @@ import { parse, valita_exports } from "../../../../shared/src/valita.js";
2
2
  import { must } from "../../../../shared/src/must.js";
3
3
  import "../../config/normalize.js";
4
4
  import { Database } from "../../../../zqlite/src/db.js";
5
- import { getSubscriptionState } from "../replicator/schema/replication-state.js";
6
- import { StatementRunner } from "../../db/statements.js";
7
5
  import { isSQLiteCorruption, logSQLiteCorruptionDiagnostics } from "../../db/sqlite-corruption.js";
6
+ import { StatementRunner } from "../../db/statements.js";
7
+ import { getSubscriptionState } from "../replicator/schema/replication-state.js";
8
8
  import { deleteLiteDB } from "../../db/delete-lite-db.js";
9
9
  import { deleteChangeLogDB } from "../replicator/change-log-db.js";
10
10
  import { litestreamSocketPath } from "./litestream-controller.js";
@@ -1,9 +1,9 @@
1
1
  import { getOperatorAccess, getServerVersion } from "../config/zero-config.js";
2
2
  import { Database } from "../../../zqlite/src/db.js";
3
- import { computeZqlSpecsFromLiteSpecs, listIndexes, listTables } from "../db/lite-tables.js";
4
3
  import { decodeSampleKinds } from "../../../zqlite/src/sqlite-stat4-sample.js";
5
- import { getReplicationState } from "./replicator/schema/replication-state.js";
4
+ import { computeZqlSpecsFromLiteSpecs, listIndexes, listTables } from "../db/lite-tables.js";
6
5
  import { StatementRunner } from "../db/statements.js";
6
+ import { getReplicationState } from "./replicator/schema/replication-state.js";
7
7
  import _usingCtx from "../../../_virtual/_@oxc-project_runtime@0.154.0/helpers/esm/usingCtx.js";
8
8
  import auth from "basic-auth";
9
9
  //#region ../zero-cache/src/services/plannerz.ts
@@ -1,6 +1,6 @@
1
1
  import { Database } from "../../../../zqlite/src/db.js";
2
- import { CREATE_CHANGE_LOG_COOKIE_SCHEMA, DROP_CHANGE_LOG_COOKIE_TABLES, replaceCookies } from "./change-log-cookies.js";
3
2
  import { isSQLiteCorruption, logSQLiteCorruptionDiagnostics } from "../../db/sqlite-corruption.js";
3
+ import { CREATE_CHANGE_LOG_COOKIE_SCHEMA, DROP_CHANGE_LOG_COOKIE_TABLES, replaceCookies } from "./change-log-cookies.js";
4
4
  import { deleteLiteDB } from "../../db/delete-lite-db.js";
5
5
  import { existsSync } from "node:fs";
6
6
  /** https://www.sqlite.org/pragma.html#pragma_auto_vacuum */
@@ -1,11 +1,11 @@
1
1
  import { AbortError } from "../../../../shared/src/abort-error.js";
2
2
  import { Database } from "../../../../zqlite/src/db.js";
3
- import { mapPostgresToLiteIndex } from "../../db/pg-to-lite.js";
4
3
  import { getOrCreateCounter } from "../../observability/metrics.js";
4
+ import { RunningState } from "../running-state.js";
5
+ import { mapPostgresToLiteIndex } from "../../db/pg-to-lite.js";
6
+ import { StatementRunner } from "../../db/statements.js";
5
7
  import { readReplicaBackfills } from "./schema/backfilling.js";
6
8
  import { getSubscriptionState } from "./schema/replication-state.js";
7
- import { StatementRunner } from "../../db/statements.js";
8
- import { RunningState } from "../running-state.js";
9
9
  import { IndexingProgress } from "./replication-status.js";
10
10
  import { errorTypeToReadableName } from "../change-streamer/change-streamer.js";
11
11
  import { Notifier } from "./notifier.js";
@@ -1,7 +1,7 @@
1
1
  import { unreachable } from "../../../../../shared/src/asserts.js";
2
2
  import { BigIntJSON } from "../../../../../shared/src/bigint-json.js";
3
- import { liteTableName } from "../../../types/names.js";
4
3
  import { getOrCreateCounter } from "../../../observability/metrics.js";
4
+ import { liteTableName } from "../../../types/names.js";
5
5
  import { backfillRequestsFrom, cookieOps } from "../change-log-cookies.js";
6
6
  //#region ../zero-cache/src/services/replicator/schema/backfilling.ts
7
7
  var BACKFILLING_TABLE = "_zero.backfilling";
@@ -1,11 +1,11 @@
1
1
  import { assert } from "../../../../shared/src/asserts.js";
2
2
  import { must } from "../../../../shared/src/must.js";
3
3
  import { Database } from "../../../../zqlite/src/db.js";
4
- import { readReplicaBackfills } from "./schema/backfilling.js";
5
- import { getSubscriptionState } from "./schema/replication-state.js";
6
- import { StatementRunner } from "../../db/statements.js";
7
4
  import { isSQLiteCorruption, logSQLiteCorruptionDiagnostics, registerSQLiteCorruptionDiagnosticTarget } from "../../db/sqlite-corruption.js";
8
5
  import { createLogContext } from "../../server/logging.js";
6
+ import { StatementRunner } from "../../db/statements.js";
7
+ import { readReplicaBackfills } from "./schema/backfilling.js";
8
+ import { getSubscriptionState } from "./schema/replication-state.js";
9
9
  import { deleteLiteDB } from "../../db/delete-lite-db.js";
10
10
  import { ChangeProcessor } from "./change-processor.js";
11
11
  import { LitestreamController } from "../litestream/litestream-controller.js";
@@ -6,9 +6,9 @@ import { hashOfAST } from "../../../zero-protocol/src/query-hash.js";
6
6
  import { buildPipeline } from "../../../zql/src/builder/builder.js";
7
7
  import { astToZQL } from "../../../ast-to-zql/src/ast-to-zql.js";
8
8
  import { formatOutput } from "../../../ast-to-zql/src/format.js";
9
+ import { transformAndHashQuery } from "../auth/read-authorizer.js";
9
10
  import { computeZqlSpecs } from "../db/lite-tables.js";
10
11
  import { resolveSimpleScalarSubqueries } from "../../../zqlite/src/resolve-scalar-subqueries.js";
11
- import { transformAndHashQuery } from "../auth/read-authorizer.js";
12
12
  import { fetchScalarValue, hydrate } from "./view-syncer/pipeline-driver.js";
13
13
  //#region ../zero-cache/src/services/run-ast.ts
14
14
  /**
@@ -2,9 +2,9 @@ import { BigIntJSON } from "../../../shared/src/bigint-json.js";
2
2
  import { getShardID, upstreamSchema } from "../types/shards.js";
3
3
  import { getOperatorAccess } from "../config/zero-config.js";
4
4
  import { Database } from "../../../zqlite/src/db.js";
5
- import { getReplicationState } from "./replicator/schema/replication-state.js";
6
- import { StatementRunner } from "../db/statements.js";
7
5
  import { pgClient } from "../types/pg.js";
6
+ import { StatementRunner } from "../db/statements.js";
7
+ import { getReplicationState } from "./replicator/schema/replication-state.js";
8
8
  import { fromStateVersionString } from "./change-source/pg/lsn.js";
9
9
  import os from "os";
10
10
  import fs from "fs";
@@ -13,7 +13,6 @@ import { startAsyncSpan } from "../../../../otel/src/span.js";
13
13
  import { version } from "../../../../otel/src/version.js";
14
14
  import { getOrCreateCounter, getOrCreateLatencyHistogram } from "../../observability/metrics.js";
15
15
  import { rowIDString } from "../../types/row-key.js";
16
- import { formatSignature, parseSignature } from "./row-set-signature.js";
17
16
  import { READONLY, READ_COMMITTED } from "../../db/mode-enum.js";
18
17
  import { runTx } from "../../db/run-transaction.js";
19
18
  import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
@@ -23,6 +22,7 @@ import { ttlClockAsNumber, ttlClockFromNumber } from "./ttl-clock.js";
23
22
  import { EMPTY_CVR_VERSION, cmpVersions, queryRecordToQueryRow, versionFromString, versionString } from "./schema/types.js";
24
23
  import "./schema/cvr.js";
25
24
  import { ProtocolErrorWithLevel } from "../../types/error-with-level.js";
25
+ import { formatSignature, parseSignature } from "./row-set-signature.js";
26
26
  import { CustomKeySet } from "../../../../shared/src/custom-key-set.js";
27
27
  import { RowRecordCache } from "./row-record-cache.js";
28
28
  import { trace } from "@opentelemetry/api";
@@ -4,9 +4,9 @@ import { must } from "../../../../shared/src/must.js";
4
4
  import { TDigest } from "../../../../shared/src/tdigest.js";
5
5
  import { getServerVersion, isAdminPasswordValid } from "../../config/zero-config.js";
6
6
  import { Database } from "../../../../zqlite/src/db.js";
7
- import { loadPermissions } from "../../auth/load-permissions.js";
8
7
  import { StatementRunner } from "../../db/statements.js";
9
8
  import _usingCtx from "../../../../_virtual/_@oxc-project_runtime@0.154.0/helpers/esm/usingCtx.js";
9
+ import { loadPermissions } from "../../auth/load-permissions.js";
10
10
  import { inspectAnalyzeQueryUpSchema, unparsedInspectUpBodySchema } from "../../../../zero-protocol/src/inspect-up.js";
11
11
  import { analyzeQuery } from "../analyze.js";
12
12
  //#region ../zero-cache/src/services/view-syncer/inspect-handler.ts
@@ -10,16 +10,16 @@ import { makeSourceChangeAdd, makeSourceChangeEdit, makeSourceChangeRemove } fro
10
10
  import { buildPipeline } from "../../../../zql/src/builder/builder.js";
11
11
  import { Debug, runtimeDebugFlags } from "../../../../zql/src/builder/debug-delegate.js";
12
12
  import { TableSource } from "../../../../zqlite/src/table-source.js";
13
+ import { randInt } from "../../../../shared/src/rand.js";
14
+ import { getOrCreateCounter, getOrCreateLatencyHistogram } from "../../observability/metrics.js";
13
15
  import { ZERO_VERSION_COLUMN_NAME } from "../replicator/schema/constants.js";
14
16
  import { computeZqlSpecs, mustGetTableSpec } from "../../db/lite-tables.js";
17
+ import "../../types/row-key.js";
18
+ import { getSubscriptionState } from "../replicator/schema/replication-state.js";
19
+ import { reloadPermissionsIfChanged } from "../../auth/load-permissions.js";
15
20
  import { resolveSimpleScalarSubqueries } from "../../../../zqlite/src/resolve-scalar-subqueries.js";
16
- import { randInt } from "../../../../shared/src/rand.js";
17
21
  import { createSQLiteCostModel } from "../../../../zqlite/src/sqlite-cost-model.js";
18
- import { reloadPermissionsIfChanged } from "../../auth/load-permissions.js";
19
22
  import { LogThrottle } from "../../observability/log-throttle.js";
20
- import { getOrCreateCounter, getOrCreateLatencyHistogram } from "../../observability/metrics.js";
21
- import "../../types/row-key.js";
22
- import { getSubscriptionState } from "../replicator/schema/replication-state.js";
23
23
  import { checkClientSchema } from "./client-schema.js";
24
24
  import { planWarningMessage } from "./plan-warnings.js";
25
25
  import { PushCutoff } from "./push-cutoff.js";
@@ -2,9 +2,9 @@ import { promiseVoid } from "../../../../shared/src/resolved-promises.js";
2
2
  import { must } from "../../../../shared/src/must.js";
3
3
  import { cvrSchema } from "../../types/shards.js";
4
4
  import { startAsyncSpan } from "../../../../otel/src/span.js";
5
+ import "../../types/pg.js";
5
6
  import { getOrCreateCounter, getOrCreateLatencyHistogram } from "../../observability/metrics.js";
6
7
  import { rowIDString } from "../../types/row-key.js";
7
- import "../../types/pg.js";
8
8
  import { READONLY, READ_COMMITTED } from "../../db/mode-enum.js";
9
9
  import { runTx } from "../../db/run-transaction.js";
10
10
  import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
@@ -5,10 +5,10 @@ import { Database } from "../../../../zqlite/src/db.js";
5
5
  import { recordIndexUsage } from "../../../../zqlite/src/index-usage.js";
6
6
  import { fromSQLiteTypes } from "../../../../zqlite/src/table-source.js";
7
7
  import "../replicator/schema/constants.js";
8
+ import { StatementRunner } from "../../db/statements.js";
8
9
  import { normalizedKeyOrder } from "../../types/row-key.js";
9
10
  import { parseChangeLogEntry } from "../replicator/schema/change-log.js";
10
11
  import { getReplicationState } from "../replicator/schema/replication-state.js";
11
- import { StatementRunner } from "../../db/statements.js";
12
12
  import { id } from "../../types/sql.js";
13
13
  //#region ../zero-cache/src/services/view-syncer/snapshotter.ts
14
14
  /**
@@ -9,23 +9,23 @@ import { ProtocolError, isProtocolError } from "../../../../zero-protocol/src/er
9
9
  import { normalizeClientSchema } from "../../../../zero-protocol/src/client-schema.js";
10
10
  import { MAX_TTL_MS, clampTTL } from "../../../../zql/src/query/ttl.js";
11
11
  import { manualSpan, startAsyncSpan, startSpan } from "../../../../otel/src/span.js";
12
- import { ZERO_VERSION_COLUMN_NAME } from "../replicator/schema/constants.js";
13
12
  import { transformAndHashQuery } from "../../auth/read-authorizer.js";
14
13
  import { randInt } from "../../../../shared/src/rand.js";
15
- import { LogThrottle } from "../../observability/log-throttle.js";
16
14
  import { getOrCreateCounter, getOrCreateLatencyHistogram, getOrCreateNativeHistogram, getOrCreateUpDownCounter, getOrCreateValueHistogram } from "../../observability/metrics.js";
15
+ import { ZERO_VERSION_COLUMN_NAME } from "../replicator/schema/constants.js";
17
16
  import { rowIDString } from "../../types/row-key.js";
18
17
  import "../replicator/schema/replication-state.js";
19
- import { planWarningMessage } from "./plan-warnings.js";
20
- import { shouldCutOffPush } from "./push-cutoff.js";
21
- import { queryShape } from "./query-shape.js";
22
- import { ResetPipelinesSignal } from "./snapshotter.js";
23
18
  import { Subscription } from "../../types/subscription.js";
24
19
  import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
25
20
  import { ttlClockAsNumber, ttlClockFromNumber } from "./ttl-clock.js";
26
21
  import { EMPTY_CVR_VERSION, cmpVersions, versionFromString, versionString, versionToCookie } from "./schema/types.js";
27
22
  import { ProtocolErrorWithLevel, getLogLevel, wrapWithProtocolError } from "../../types/error-with-level.js";
28
23
  import { isAuthErrorBody } from "../../auth/auth.js";
24
+ import { LogThrottle } from "../../observability/log-throttle.js";
25
+ import { planWarningMessage } from "./plan-warnings.js";
26
+ import { shouldCutOffPush } from "./push-cutoff.js";
27
+ import { queryShape } from "./query-shape.js";
28
+ import { ResetPipelinesSignal } from "./snapshotter.js";
29
29
  import { AdvancementMetrics } from "./advancement-metrics.js";
30
30
  import { ClientHandler, startPoke } from "./client-handler.js";
31
31
  import { tracer } from "./tracer.js";
@@ -1,6 +1,6 @@
1
1
  import { tmpdir } from "node:os";
2
- import { join } from "node:path";
3
2
  import { writeFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
4
  import { Session } from "node:inspector/promises";
5
5
  //#region ../zero-cache/src/types/profiler.ts
6
6
  /**
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * The current version of Zero.
4
4
  */
5
- var version = "1.12.0-canary.4";
5
+ var version = "1.12.0-canary.6";
6
6
  //#endregion
7
7
  export { version };
8
8