@rocicorp/zero 1.11.0-canary.35 → 1.11.0-canary.37

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 (130) hide show
  1. package/out/zero/package.js +1 -1
  2. package/out/zero/package.js.map +1 -1
  3. package/out/zero-cache/src/config/zero-config.d.ts +28 -0
  4. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  5. package/out/zero-cache/src/config/zero-config.js +83 -0
  6. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  7. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  8. package/out/zero-cache/src/server/change-streamer.js +2 -1
  9. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  10. package/out/zero-cache/src/server/main.d.ts.map +1 -1
  11. package/out/zero-cache/src/server/main.js +5 -1
  12. package/out/zero-cache/src/server/main.js.map +1 -1
  13. package/out/zero-cache/src/server/replicator.d.ts +4 -0
  14. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  15. package/out/zero-cache/src/server/replicator.js +18 -4
  16. package/out/zero-cache/src/server/replicator.js.map +1 -1
  17. package/out/zero-cache/src/server/runner/run-worker.d.ts.map +1 -1
  18. package/out/zero-cache/src/server/runner/run-worker.js +12 -6
  19. package/out/zero-cache/src/server/runner/run-worker.js.map +1 -1
  20. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  21. package/out/zero-cache/src/server/syncer.js +17 -1
  22. package/out/zero-cache/src/server/syncer.js.map +1 -1
  23. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +1 -1
  24. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
  25. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts +27 -7
  26. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  27. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +124 -62
  28. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  29. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +9 -3
  30. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
  31. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +19 -6
  32. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  33. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +8 -0
  34. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  35. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +131 -62
  36. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  37. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +124 -2
  38. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  39. package/out/zero-cache/src/services/change-streamer/change-streamer.js +33 -2
  40. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  41. package/out/zero-cache/src/services/change-streamer/drain-tracker.d.ts +24 -0
  42. package/out/zero-cache/src/services/change-streamer/drain-tracker.d.ts.map +1 -0
  43. package/out/zero-cache/src/services/change-streamer/drain-tracker.js +67 -0
  44. package/out/zero-cache/src/services/change-streamer/drain-tracker.js.map +1 -0
  45. package/out/zero-cache/src/services/change-streamer/restore-admission.d.ts +214 -0
  46. package/out/zero-cache/src/services/change-streamer/restore-admission.d.ts.map +1 -0
  47. package/out/zero-cache/src/services/change-streamer/restore-admission.js +294 -0
  48. package/out/zero-cache/src/services/change-streamer/restore-admission.js.map +1 -0
  49. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.d.ts +13 -1
  50. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.d.ts.map +1 -1
  51. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js +7 -6
  52. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js.map +1 -1
  53. package/out/zero-cache/src/services/change-streamer/snapshot.d.ts +2 -2
  54. package/out/zero-cache/src/services/change-streamer/snapshot.d.ts.map +1 -1
  55. package/out/zero-cache/src/services/change-streamer/snapshot.js +2 -2
  56. package/out/zero-cache/src/services/change-streamer/snapshot.js.map +1 -1
  57. package/out/zero-cache/src/services/change-streamer/subscribe.d.ts +39 -0
  58. package/out/zero-cache/src/services/change-streamer/subscribe.d.ts.map +1 -1
  59. package/out/zero-cache/src/services/change-streamer/subscribe.js +19 -2
  60. package/out/zero-cache/src/services/change-streamer/subscribe.js.map +1 -1
  61. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +22 -1
  62. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  63. package/out/zero-cache/src/services/change-streamer/subscriber.js +41 -15
  64. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  65. package/out/zero-cache/src/services/http-service.d.ts +6 -0
  66. package/out/zero-cache/src/services/http-service.d.ts.map +1 -1
  67. package/out/zero-cache/src/services/http-service.js +14 -2
  68. package/out/zero-cache/src/services/http-service.js.map +1 -1
  69. package/out/zero-cache/src/services/life-cycle.d.ts +33 -0
  70. package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
  71. package/out/zero-cache/src/services/life-cycle.js +56 -10
  72. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  73. package/out/zero-cache/src/services/litestream/commands.d.ts +8 -0
  74. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  75. package/out/zero-cache/src/services/litestream/commands.js +22 -1
  76. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  77. package/out/zero-cache/src/services/replicator/drain-handler.d.ts +74 -0
  78. package/out/zero-cache/src/services/replicator/drain-handler.d.ts.map +1 -0
  79. package/out/zero-cache/src/services/replicator/drain-handler.js +185 -0
  80. package/out/zero-cache/src/services/replicator/drain-handler.js.map +1 -0
  81. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts +2 -1
  82. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  83. package/out/zero-cache/src/services/replicator/incremental-sync.js +66 -13
  84. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  85. package/out/zero-cache/src/services/replicator/replicator.d.ts +2 -1
  86. package/out/zero-cache/src/services/replicator/replicator.d.ts.map +1 -1
  87. package/out/zero-cache/src/services/replicator/replicator.js +2 -2
  88. package/out/zero-cache/src/services/replicator/replicator.js.map +1 -1
  89. package/out/zero-cache/src/services/view-syncer/batch-pacer.d.ts +79 -0
  90. package/out/zero-cache/src/services/view-syncer/batch-pacer.d.ts.map +1 -0
  91. package/out/zero-cache/src/services/view-syncer/batch-pacer.js +249 -0
  92. package/out/zero-cache/src/services/view-syncer/batch-pacer.js.map +1 -0
  93. package/out/zero-cache/src/services/view-syncer/cvr.d.ts +27 -1
  94. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  95. package/out/zero-cache/src/services/view-syncer/cvr.js +107 -17
  96. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  97. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +85 -3
  98. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  99. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +301 -20
  100. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  101. package/out/zero-cache/src/services/view-syncer/push-cost.d.ts +132 -0
  102. package/out/zero-cache/src/services/view-syncer/push-cost.d.ts.map +1 -0
  103. package/out/zero-cache/src/services/view-syncer/push-cost.js +164 -0
  104. package/out/zero-cache/src/services/view-syncer/push-cost.js.map +1 -0
  105. package/out/zero-cache/src/services/view-syncer/push-cutoff.d.ts +55 -0
  106. package/out/zero-cache/src/services/view-syncer/push-cutoff.d.ts.map +1 -0
  107. package/out/zero-cache/src/services/view-syncer/push-cutoff.js +56 -0
  108. package/out/zero-cache/src/services/view-syncer/push-cutoff.js.map +1 -0
  109. package/out/zero-cache/src/services/view-syncer/query-stats.d.ts +7 -0
  110. package/out/zero-cache/src/services/view-syncer/query-stats.d.ts.map +1 -1
  111. package/out/zero-cache/src/services/view-syncer/query-stats.js +20 -1
  112. package/out/zero-cache/src/services/view-syncer/query-stats.js.map +1 -1
  113. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +136 -5
  114. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  115. package/out/zero-cache/src/services/view-syncer/snapshotter.js +246 -18
  116. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  117. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +8 -2
  118. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  119. package/out/zero-cache/src/services/view-syncer/view-syncer.js +381 -16
  120. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  121. package/out/zero-cache/src/types/processes.d.ts +8 -0
  122. package/out/zero-cache/src/types/processes.d.ts.map +1 -1
  123. package/out/zero-cache/src/types/processes.js.map +1 -1
  124. package/out/zero-client/src/client/version.js +1 -1
  125. package/out/zero-react/src/use-query.js +1 -1
  126. package/out/zero-react/src/use-query.js.map +1 -1
  127. package/out/zero-server/src/queries/process-queries.d.ts.map +1 -1
  128. package/out/zero-server/src/queries/process-queries.js +1 -1
  129. package/out/zero-server/src/queries/process-queries.js.map +1 -1
  130. package/package.json +1 -1
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Learns what it costs to push a change of each table through each query of
3
+ * a client group, and estimates from it what pushing a batch of changes
4
+ * would cost.
5
+ *
6
+ * The estimates only decide whether a query skips pushing and starts an
7
+ * advancement on rebuild (see {@link shouldStartOnRebuild}). Nothing depends
8
+ * on their accuracy: a query that starts on push when it should not have is
9
+ * cut off (see `PushCutoff`), and one that starts on rebuild costs its
10
+ * hydration time, as it would have at the cutoff.
11
+ */
12
+ export declare class PushCostEstimator {
13
+ #private;
14
+ /**
15
+ * Records what an advancement cost a query.
16
+ *
17
+ * @param tables The tables the query reads.
18
+ * @param timeMsByTable The time spent pushing the changes of each table
19
+ * to the query. Absent for a table whose changes cost it nothing,
20
+ * e.g. because the rows did not match its filters.
21
+ * @param changesByTable The entries of each table that the advancement
22
+ * read from the change log, whether or not they reached the query,
23
+ * or changed anything at all (a row that is inserted and deleted
24
+ * between two snapshots is an entry that changes nothing). These
25
+ * are the entries that {@link estimate} is given the count of.
26
+ */
27
+ record(queryID: string, tables: Iterable<string>, timeMsByTable: ReadonlyMap<string, number> | undefined, changesByTable: ReadonlyMap<string, number>): void;
28
+ /**
29
+ * The estimated time to push `changes` to the query, or `undefined` if it
30
+ * has never been pushed any change. A table that the query has never been
31
+ * pushed a change of is estimated to cost nothing.
32
+ *
33
+ * @param tables The tables the query reads.
34
+ * @param changes The number of changes to push, by table.
35
+ */
36
+ estimate(queryID: string, tables: Iterable<string>, changes: ReadonlyMap<string, number>): number | undefined;
37
+ /** Forgets what was learned about a query. */
38
+ forget(queryID: string): void;
39
+ }
40
+ /**
41
+ * Learns what it costs a client group to read a batch of changes, apart from
42
+ * pushing them through its queries, and estimates from it what reading the
43
+ * next one would cost.
44
+ *
45
+ * Reading has three parts, which do not cost the same entries (see
46
+ * {@link ReadParts}): walking the change log, parsing the entries that
47
+ * the walk returns, and looking up the rows of the entries of the tables
48
+ * that are read. What each costs is learned per entry that it was done for.
49
+ * A cost learned per entry of some other count is only right for a batch
50
+ * with the same mix of entries: charged to the entries that are pushed, for
51
+ * instance, the parsing of a batch that is mostly of tables that no query
52
+ * reads makes the next batch that is not look many times as expensive as it
53
+ * is.
54
+ *
55
+ * No query can be cut off for what this costs, as it is not a cost of any
56
+ * one query. It is what pushing costs all of them together, on top of what
57
+ * it costs each (see {@link shouldRebuildInsteadOfReading}).
58
+ */
59
+ export declare class ReadCostEstimator {
60
+ #private;
61
+ /**
62
+ * Records what reading a batch cost. Only for a batch that was read to
63
+ * its end.
64
+ *
65
+ * @param timeMs The time spent on each part of reading the batch.
66
+ * @param entries The entries that each part was done for.
67
+ */
68
+ record(timeMs: ReadParts, entries: ReadParts): void;
69
+ /**
70
+ * The estimated time to read a batch, or `undefined` if none has been
71
+ * read yet.
72
+ *
73
+ * @param entries The entries that each part of reading the batch would be
74
+ * done for.
75
+ */
76
+ estimate(entries: ReadParts): number | undefined;
77
+ }
78
+ /**
79
+ * A number for each part of reading a batch of changes: the entries of the
80
+ * change log that the part is done for, or the time that it takes.
81
+ */
82
+ export type ReadParts = {
83
+ /**
84
+ * Walking the change log, which is done for every entry since the
85
+ * previous snapshot, whether or not the entry is of a table that is read:
86
+ * the entries are in the order they were committed in, not grouped by
87
+ * table.
88
+ */
89
+ readonly walk: number;
90
+ /**
91
+ * Parsing, which is done for every entry that the walk returns. That is
92
+ * all of them, unless the walk is told which tables are read.
93
+ */
94
+ readonly parse: number;
95
+ /**
96
+ * Looking up the row in both snapshots, which is done for every entry of
97
+ * a table that is read. This includes the entries that then turn out to
98
+ * change nothing (e.g. a row inserted and deleted since the previous
99
+ * snapshot).
100
+ */
101
+ readonly lookup: number;
102
+ };
103
+ /**
104
+ * Whether the queries that are left to push a batch to should all be
105
+ * hydrated again instead, without the batch being read: when reading it and
106
+ * pushing it to them is estimated to cost at least `margin` times what
107
+ * hydrating them does.
108
+ *
109
+ * This is {@link shouldStartOnRebuild} for the queries together, with what
110
+ * reading the batch costs added to what pushing it costs each of them. A
111
+ * query that few of the batch's changes reach is cheap to push to, but the
112
+ * whole batch still has to be walked to find those changes.
113
+ *
114
+ * @param estimatedReadMs `undefined` if there is no estimate, in which case
115
+ * the batch is read.
116
+ * @param queries What pushing the batch is estimated to cost each query
117
+ * (nothing, if it has no estimate), and what hydrating it took.
118
+ */
119
+ export declare function shouldRebuildInsteadOfReading(estimatedReadMs: number | undefined, queries: Iterable<{
120
+ readonly estimatedPushMs: number | undefined;
121
+ readonly hydrationTimeMs: number;
122
+ }>, margin: number): boolean;
123
+ /**
124
+ * Whether a query should start an advancement on rebuild, i.e. be hydrated
125
+ * again without being pushed any of the advancement's changes: when pushing
126
+ * them is estimated to cost at least `margin` times what hydrating it does.
127
+ *
128
+ * The margin is for the imprecision of the estimate. A query without one
129
+ * starts on push.
130
+ */
131
+ export declare function shouldStartOnRebuild(estimatedPushMs: number | undefined, hydrationTimeMs: number, margin: number): boolean;
132
+ //# sourceMappingURL=push-cost.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-cost.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/push-cost.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;GAUG;AACH,qBAAa,iBAAiB;;IAI5B;;;;;;;;;;;;OAYG;IACH,MAAM,CACJ,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,EACxB,aAAa,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EACtD,cAAc,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAC1C,IAAI,CAmBN;IAED;;;;;;;OAOG;IACH,QAAQ,CACN,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,EACxB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GACnC,MAAM,GAAG,SAAS,CAUpB;IAED,8CAA8C;IAC9C,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAE5B;CACF;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,iBAAiB;;IAK5B;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,GAAG,IAAI,CAgBlD;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,GAAG,SAAS,CAS/C;CACF;AAED;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB,CAAC;AAgBF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,6BAA6B,CAC3C,eAAe,EAAE,MAAM,GAAG,SAAS,EACnC,OAAO,EAAE,QAAQ,CAAC;IAChB,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7C,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC,CAAC,EACF,MAAM,EAAE,MAAM,GACb,OAAO,CAWT;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,eAAe,EAAE,MAAM,GAAG,SAAS,EACnC,eAAe,EAAE,MAAM,EACvB,MAAM,EAAE,MAAM,GACb,OAAO,CAKT"}
@@ -0,0 +1,164 @@
1
+ //#region ../zero-cache/src/services/view-syncer/push-cost.ts
2
+ /**
3
+ * The weight of the latest advancement in a query's cost per change. The
4
+ * cost of a change depends on the row (one joined to many others costs more
5
+ * than one that is filtered out), so it varies from one advancement to the
6
+ * next, and workloads shift.
7
+ */
8
+ var LATEST_WEIGHT = .3;
9
+ /**
10
+ * Learns what it costs to push a change of each table through each query of
11
+ * a client group, and estimates from it what pushing a batch of changes
12
+ * would cost.
13
+ *
14
+ * The estimates only decide whether a query skips pushing and starts an
15
+ * advancement on rebuild (see {@link shouldStartOnRebuild}). Nothing depends
16
+ * on their accuracy: a query that starts on push when it should not have is
17
+ * cut off (see `PushCutoff`), and one that starts on rebuild costs its
18
+ * hydration time, as it would have at the cutoff.
19
+ */
20
+ var PushCostEstimator = class {
21
+ /** Milliseconds per change, by query ID and table. */
22
+ #costs = /* @__PURE__ */ new Map();
23
+ /**
24
+ * Records what an advancement cost a query.
25
+ *
26
+ * @param tables The tables the query reads.
27
+ * @param timeMsByTable The time spent pushing the changes of each table
28
+ * to the query. Absent for a table whose changes cost it nothing,
29
+ * e.g. because the rows did not match its filters.
30
+ * @param changesByTable The entries of each table that the advancement
31
+ * read from the change log, whether or not they reached the query,
32
+ * or changed anything at all (a row that is inserted and deleted
33
+ * between two snapshots is an entry that changes nothing). These
34
+ * are the entries that {@link estimate} is given the count of.
35
+ */
36
+ record(queryID, tables, timeMsByTable, changesByTable) {
37
+ let costs = this.#costs.get(queryID);
38
+ for (const table of tables) {
39
+ const changes = changesByTable.get(table);
40
+ if (!changes) continue;
41
+ if (costs === void 0) {
42
+ costs = /* @__PURE__ */ new Map();
43
+ this.#costs.set(queryID, costs);
44
+ }
45
+ const latest = (timeMsByTable?.get(table) ?? 0) / changes;
46
+ const previous = costs.get(table);
47
+ const cost = previous === void 0 ? latest : previous + LATEST_WEIGHT * (latest - previous);
48
+ costs.set(table, cost);
49
+ }
50
+ }
51
+ /**
52
+ * The estimated time to push `changes` to the query, or `undefined` if it
53
+ * has never been pushed any change. A table that the query has never been
54
+ * pushed a change of is estimated to cost nothing.
55
+ *
56
+ * @param tables The tables the query reads.
57
+ * @param changes The number of changes to push, by table.
58
+ */
59
+ estimate(queryID, tables, changes) {
60
+ const costs = this.#costs.get(queryID);
61
+ if (costs === void 0) return;
62
+ let total = 0;
63
+ for (const table of tables) total += (changes.get(table) ?? 0) * (costs.get(table) ?? 0);
64
+ return total;
65
+ }
66
+ /** Forgets what was learned about a query. */
67
+ forget(queryID) {
68
+ this.#costs.delete(queryID);
69
+ }
70
+ };
71
+ /**
72
+ * Learns what it costs a client group to read a batch of changes, apart from
73
+ * pushing them through its queries, and estimates from it what reading the
74
+ * next one would cost.
75
+ *
76
+ * Reading has three parts, which do not cost the same entries (see
77
+ * {@link ReadParts}): walking the change log, parsing the entries that
78
+ * the walk returns, and looking up the rows of the entries of the tables
79
+ * that are read. What each costs is learned per entry that it was done for.
80
+ * A cost learned per entry of some other count is only right for a batch
81
+ * with the same mix of entries: charged to the entries that are pushed, for
82
+ * instance, the parsing of a batch that is mostly of tables that no query
83
+ * reads makes the next batch that is not look many times as expensive as it
84
+ * is.
85
+ *
86
+ * No query can be cut off for what this costs, as it is not a cost of any
87
+ * one query. It is what pushing costs all of them together, on top of what
88
+ * it costs each (see {@link shouldRebuildInsteadOfReading}).
89
+ */
90
+ var ReadCostEstimator = class {
91
+ #walkMsPerEntry;
92
+ #parseMsPerEntry;
93
+ #lookupMsPerEntry;
94
+ /**
95
+ * Records what reading a batch cost. Only for a batch that was read to
96
+ * its end.
97
+ *
98
+ * @param timeMs The time spent on each part of reading the batch.
99
+ * @param entries The entries that each part was done for.
100
+ */
101
+ record(timeMs, entries) {
102
+ this.#walkMsPerEntry = learn(this.#walkMsPerEntry, timeMs.walk, entries.walk);
103
+ this.#parseMsPerEntry = learn(this.#parseMsPerEntry, timeMs.parse, entries.parse);
104
+ this.#lookupMsPerEntry = learn(this.#lookupMsPerEntry, timeMs.lookup, entries.lookup);
105
+ }
106
+ /**
107
+ * The estimated time to read a batch, or `undefined` if none has been
108
+ * read yet.
109
+ *
110
+ * @param entries The entries that each part of reading the batch would be
111
+ * done for.
112
+ */
113
+ estimate(entries) {
114
+ if (this.#walkMsPerEntry === void 0) return;
115
+ return entries.walk * this.#walkMsPerEntry + entries.parse * (this.#parseMsPerEntry ?? 0) + entries.lookup * (this.#lookupMsPerEntry ?? 0);
116
+ }
117
+ };
118
+ function learn(msPerEntry, timeMs, entries) {
119
+ return entries > 0 ? decay(msPerEntry, timeMs / entries) : msPerEntry;
120
+ }
121
+ function decay(previous, latest) {
122
+ return previous === void 0 ? latest : previous + LATEST_WEIGHT * (latest - previous);
123
+ }
124
+ /**
125
+ * Whether the queries that are left to push a batch to should all be
126
+ * hydrated again instead, without the batch being read: when reading it and
127
+ * pushing it to them is estimated to cost at least `margin` times what
128
+ * hydrating them does.
129
+ *
130
+ * This is {@link shouldStartOnRebuild} for the queries together, with what
131
+ * reading the batch costs added to what pushing it costs each of them. A
132
+ * query that few of the batch's changes reach is cheap to push to, but the
133
+ * whole batch still has to be walked to find those changes.
134
+ *
135
+ * @param estimatedReadMs `undefined` if there is no estimate, in which case
136
+ * the batch is read.
137
+ * @param queries What pushing the batch is estimated to cost each query
138
+ * (nothing, if it has no estimate), and what hydrating it took.
139
+ */
140
+ function shouldRebuildInsteadOfReading(estimatedReadMs, queries, margin) {
141
+ if (estimatedReadMs === void 0) return false;
142
+ let pushMs = estimatedReadMs;
143
+ let hydrationMs = 0;
144
+ for (const query of queries) {
145
+ pushMs += query.estimatedPushMs ?? 0;
146
+ hydrationMs += Math.max(query.hydrationTimeMs, 2);
147
+ }
148
+ return hydrationMs > 0 && pushMs >= margin * hydrationMs;
149
+ }
150
+ /**
151
+ * Whether a query should start an advancement on rebuild, i.e. be hydrated
152
+ * again without being pushed any of the advancement's changes: when pushing
153
+ * them is estimated to cost at least `margin` times what hydrating it does.
154
+ *
155
+ * The margin is for the imprecision of the estimate. A query without one
156
+ * starts on push.
157
+ */
158
+ function shouldStartOnRebuild(estimatedPushMs, hydrationTimeMs, margin) {
159
+ return estimatedPushMs !== void 0 && estimatedPushMs >= margin * Math.max(hydrationTimeMs, 2);
160
+ }
161
+ //#endregion
162
+ export { PushCostEstimator, ReadCostEstimator, shouldRebuildInsteadOfReading, shouldStartOnRebuild };
163
+
164
+ //# sourceMappingURL=push-cost.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-cost.js","names":["#costs","#walkMsPerEntry","#parseMsPerEntry","#lookupMsPerEntry"],"sources":["../../../../../../zero-cache/src/services/view-syncer/push-cost.ts"],"sourcesContent":["import {MIN_PUSH_CUTOFF_MS} from './push-cutoff.ts';\n\n/**\n * The weight of the latest advancement in a query's cost per change. The\n * cost of a change depends on the row (one joined to many others costs more\n * than one that is filtered out), so it varies from one advancement to the\n * next, and workloads shift.\n */\nconst LATEST_WEIGHT = 0.3;\n\n/**\n * Learns what it costs to push a change of each table through each query of\n * a client group, and estimates from it what pushing a batch of changes\n * would cost.\n *\n * The estimates only decide whether a query skips pushing and starts an\n * advancement on rebuild (see {@link shouldStartOnRebuild}). Nothing depends\n * on their accuracy: a query that starts on push when it should not have is\n * cut off (see `PushCutoff`), and one that starts on rebuild costs its\n * hydration time, as it would have at the cutoff.\n */\nexport class PushCostEstimator {\n /** Milliseconds per change, by query ID and table. */\n readonly #costs = new Map<string, Map<string, number>>();\n\n /**\n * Records what an advancement cost a query.\n *\n * @param tables The tables the query reads.\n * @param timeMsByTable The time spent pushing the changes of each table\n * to the query. Absent for a table whose changes cost it nothing,\n * e.g. because the rows did not match its filters.\n * @param changesByTable The entries of each table that the advancement\n * read from the change log, whether or not they reached the query,\n * or changed anything at all (a row that is inserted and deleted\n * between two snapshots is an entry that changes nothing). These\n * are the entries that {@link estimate} is given the count of.\n */\n record(\n queryID: string,\n tables: Iterable<string>,\n timeMsByTable: ReadonlyMap<string, number> | undefined,\n changesByTable: ReadonlyMap<string, number>,\n ): void {\n let costs = this.#costs.get(queryID);\n for (const table of tables) {\n const changes = changesByTable.get(table);\n if (!changes) {\n continue;\n }\n if (costs === undefined) {\n costs = new Map();\n this.#costs.set(queryID, costs);\n }\n const latest = (timeMsByTable?.get(table) ?? 0) / changes;\n const previous = costs.get(table);\n const cost =\n previous === undefined\n ? latest\n : previous + LATEST_WEIGHT * (latest - previous);\n costs.set(table, cost);\n }\n }\n\n /**\n * The estimated time to push `changes` to the query, or `undefined` if it\n * has never been pushed any change. A table that the query has never been\n * pushed a change of is estimated to cost nothing.\n *\n * @param tables The tables the query reads.\n * @param changes The number of changes to push, by table.\n */\n estimate(\n queryID: string,\n tables: Iterable<string>,\n changes: ReadonlyMap<string, number>,\n ): number | undefined {\n const costs = this.#costs.get(queryID);\n if (costs === undefined) {\n return undefined;\n }\n let total = 0;\n for (const table of tables) {\n total += (changes.get(table) ?? 0) * (costs.get(table) ?? 0);\n }\n return total;\n }\n\n /** Forgets what was learned about a query. */\n forget(queryID: string): void {\n this.#costs.delete(queryID);\n }\n}\n\n/**\n * Learns what it costs a client group to read a batch of changes, apart from\n * pushing them through its queries, and estimates from it what reading the\n * next one would cost.\n *\n * Reading has three parts, which do not cost the same entries (see\n * {@link ReadParts}): walking the change log, parsing the entries that\n * the walk returns, and looking up the rows of the entries of the tables\n * that are read. What each costs is learned per entry that it was done for.\n * A cost learned per entry of some other count is only right for a batch\n * with the same mix of entries: charged to the entries that are pushed, for\n * instance, the parsing of a batch that is mostly of tables that no query\n * reads makes the next batch that is not look many times as expensive as it\n * is.\n *\n * No query can be cut off for what this costs, as it is not a cost of any\n * one query. It is what pushing costs all of them together, on top of what\n * it costs each (see {@link shouldRebuildInsteadOfReading}).\n */\nexport class ReadCostEstimator {\n #walkMsPerEntry: number | undefined;\n #parseMsPerEntry: number | undefined;\n #lookupMsPerEntry: number | undefined;\n\n /**\n * Records what reading a batch cost. Only for a batch that was read to\n * its end.\n *\n * @param timeMs The time spent on each part of reading the batch.\n * @param entries The entries that each part was done for.\n */\n record(timeMs: ReadParts, entries: ReadParts): void {\n this.#walkMsPerEntry = learn(\n this.#walkMsPerEntry,\n timeMs.walk,\n entries.walk,\n );\n this.#parseMsPerEntry = learn(\n this.#parseMsPerEntry,\n timeMs.parse,\n entries.parse,\n );\n this.#lookupMsPerEntry = learn(\n this.#lookupMsPerEntry,\n timeMs.lookup,\n entries.lookup,\n );\n }\n\n /**\n * The estimated time to read a batch, or `undefined` if none has been\n * read yet.\n *\n * @param entries The entries that each part of reading the batch would be\n * done for.\n */\n estimate(entries: ReadParts): number | undefined {\n if (this.#walkMsPerEntry === undefined) {\n return undefined;\n }\n return (\n entries.walk * this.#walkMsPerEntry +\n entries.parse * (this.#parseMsPerEntry ?? 0) +\n entries.lookup * (this.#lookupMsPerEntry ?? 0)\n );\n }\n}\n\n/**\n * A number for each part of reading a batch of changes: the entries of the\n * change log that the part is done for, or the time that it takes.\n */\nexport type ReadParts = {\n /**\n * Walking the change log, which is done for every entry since the\n * previous snapshot, whether or not the entry is of a table that is read:\n * the entries are in the order they were committed in, not grouped by\n * table.\n */\n readonly walk: number;\n /**\n * Parsing, which is done for every entry that the walk returns. That is\n * all of them, unless the walk is told which tables are read.\n */\n readonly parse: number;\n /**\n * Looking up the row in both snapshots, which is done for every entry of\n * a table that is read. This includes the entries that then turn out to\n * change nothing (e.g. a row inserted and deleted since the previous\n * snapshot).\n */\n readonly lookup: number;\n};\n\nfunction learn(\n msPerEntry: number | undefined,\n timeMs: number,\n entries: number,\n): number | undefined {\n return entries > 0 ? decay(msPerEntry, timeMs / entries) : msPerEntry;\n}\n\nfunction decay(previous: number | undefined, latest: number): number {\n return previous === undefined\n ? latest\n : previous + LATEST_WEIGHT * (latest - previous);\n}\n\n/**\n * Whether the queries that are left to push a batch to should all be\n * hydrated again instead, without the batch being read: when reading it and\n * pushing it to them is estimated to cost at least `margin` times what\n * hydrating them does.\n *\n * This is {@link shouldStartOnRebuild} for the queries together, with what\n * reading the batch costs added to what pushing it costs each of them. A\n * query that few of the batch's changes reach is cheap to push to, but the\n * whole batch still has to be walked to find those changes.\n *\n * @param estimatedReadMs `undefined` if there is no estimate, in which case\n * the batch is read.\n * @param queries What pushing the batch is estimated to cost each query\n * (nothing, if it has no estimate), and what hydrating it took.\n */\nexport function shouldRebuildInsteadOfReading(\n estimatedReadMs: number | undefined,\n queries: Iterable<{\n readonly estimatedPushMs: number | undefined;\n readonly hydrationTimeMs: number;\n }>,\n margin: number,\n): boolean {\n if (estimatedReadMs === undefined) {\n return false;\n }\n let pushMs = estimatedReadMs;\n let hydrationMs = 0;\n for (const query of queries) {\n pushMs += query.estimatedPushMs ?? 0;\n hydrationMs += Math.max(query.hydrationTimeMs, MIN_PUSH_CUTOFF_MS);\n }\n return hydrationMs > 0 && pushMs >= margin * hydrationMs;\n}\n\n/**\n * Whether a query should start an advancement on rebuild, i.e. be hydrated\n * again without being pushed any of the advancement's changes: when pushing\n * them is estimated to cost at least `margin` times what hydrating it does.\n *\n * The margin is for the imprecision of the estimate. A query without one\n * starts on push.\n */\nexport function shouldStartOnRebuild(\n estimatedPushMs: number | undefined,\n hydrationTimeMs: number,\n margin: number,\n): boolean {\n return (\n estimatedPushMs !== undefined &&\n estimatedPushMs >= margin * Math.max(hydrationTimeMs, MIN_PUSH_CUTOFF_MS)\n );\n}\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AA,GAC3B;EAEF,OACE,QAAQ,OAAO,KAAKA,kBACpB,QAAQ,SAAS,KAAKC,oBAAoB,KAC1C,QAAQ,UAAU,KAAKC,qBAAqB;CAEhD;AACF;AA4BA,SAAS,MACP,YACA,QACA,SACoB;CACpB,OAAO,UAAU,IAAI,MAAM,YAAY,SAAS,OAAO,IAAI;AAC7D;AAEA,SAAS,MAAM,UAA8B,QAAwB;CACnE,OAAO,aAAa,KAAA,IAChB,SACA,WAAW,iBAAiB,SAAS;AAC3C;;;;;;;;;;;;;;;;;AAkBA,SAAgB,8BACd,iBACA,SAIA,QACS;CACT,IAAI,oBAAoB,KAAA,GACtB,OAAO;CAET,IAAI,SAAS;CACb,IAAI,cAAc;CAClB,KAAK,MAAM,SAAS,SAAS;EAC3B,UAAU,MAAM,mBAAmB;EACnC,eAAe,KAAK,IAAI,MAAM,iBAAA,CAAmC;CACnE;CACA,OAAO,cAAc,KAAK,UAAU,SAAS;AAC/C;;;;;;;;;AAUA,SAAgB,qBACd,iBACA,iBACA,QACS;CACT,OACE,oBAAoB,KAAA,KACpB,mBAAmB,SAAS,KAAK,IAAI,iBAAA,CAAmC;AAE5E"}
@@ -0,0 +1,55 @@
1
+ /** What an advancement has pushed to a query so far. */
2
+ export type QueryPushProgress = {
3
+ readonly queryID: string;
4
+ /** The time spent processing pushes to the query in this advancement. */
5
+ readonly pushTimeMs: number;
6
+ /** The number of changes pushed to the query in this advancement. */
7
+ readonly changes: number;
8
+ /** The query's hydration time, i.e. what rebuilding it is expected to cost. */
9
+ readonly hydrationTimeMs: number;
10
+ };
11
+ /** Whether to stop pushing an advancement's changes to a query. */
12
+ export type ShouldCutOffPush = (progress: QueryPushProgress) => boolean;
13
+ /**
14
+ * The least a query's hydration is taken to cost when deciding whether to
15
+ * stop pushing to it. Small queries hydrate in well under a millisecond,
16
+ * where one push can take longer than the hydration did on timer noise alone,
17
+ * and hydrating again saves too little to be worth its fixed costs.
18
+ */
19
+ export declare const MIN_PUSH_CUTOFF_MS = 2;
20
+ /**
21
+ * Whether to stop pushing an advancement's changes to a query and hydrate it
22
+ * again instead: once the pushes have taken as long as the hydration is
23
+ * expected to.
24
+ *
25
+ * This is the ski-rental strategy (rent until the rent paid equals the
26
+ * purchase price, then buy). Whatever the actual costs turn out to be, the
27
+ * query costs at most about twice the cheaper of pushing every change and
28
+ * hydrating from the start, plus the one change that crossed the line. It
29
+ * needs no estimate of what the remaining changes will cost.
30
+ */
31
+ export declare function shouldCutOffPush({ pushTimeMs, hydrationTimeMs, }: Pick<QueryPushProgress, 'pushTimeMs' | 'hydrationTimeMs'>): boolean;
32
+ /**
33
+ * Bounds the cost of one advancement query by query: each query is pushed
34
+ * changes until {@link ShouldCutOffPush} says to stop, and is then hydrated
35
+ * again once the advancement is done.
36
+ *
37
+ * An advancement with a cutoff is not subject to the client group's
38
+ * advancement time limit, which resets every pipeline of the client group.
39
+ * The two are alternatives and are never applied together.
40
+ */
41
+ export declare class PushCutoff {
42
+ #private;
43
+ constructor(shouldCutOff?: ShouldCutOffPush);
44
+ /** Called for each push to a query. */
45
+ recordPush(queryID: string): void;
46
+ /**
47
+ * Called between changes, when no push is in progress. Returns the queries
48
+ * to stop pushing to, of those pushed to since the last call.
49
+ *
50
+ * @param progressOf The progress of a query, or `undefined` if it is no
51
+ * longer being pushed to.
52
+ */
53
+ takeCutOff(progressOf: (queryID: string) => QueryPushProgress | undefined): QueryPushProgress[];
54
+ }
55
+ //# sourceMappingURL=push-cutoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-cutoff.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/push-cutoff.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,yEAAyE;IACzE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,qEAAqE;IACrE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,+EAA+E;IAC/E,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC,CAAC;AAEF,mEAAmE;AACnE,MAAM,MAAM,gBAAgB,GAAG,CAAC,QAAQ,EAAE,iBAAiB,KAAK,OAAO,CAAC;AAExE;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAEpC;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,EAC/B,UAAU,EACV,eAAe,GAChB,EAAE,IAAI,CAAC,iBAAiB,EAAE,YAAY,GAAG,iBAAiB,CAAC,GAAG,OAAO,CAErE;AAED;;;;;;;;GAQG;AACH,qBAAa,UAAU;;IAKrB,YAAY,YAAY,GAAE,gBAAmC,EAE5D;IAED,uCAAuC;IACvC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAEhC;IAED;;;;;;OAMG;IACH,UAAU,CACR,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,iBAAiB,GAAG,SAAS,GAC7D,iBAAiB,EAAE,CAarB;CACF"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Whether to stop pushing an advancement's changes to a query and hydrate it
3
+ * again instead: once the pushes have taken as long as the hydration is
4
+ * expected to.
5
+ *
6
+ * This is the ski-rental strategy (rent until the rent paid equals the
7
+ * purchase price, then buy). Whatever the actual costs turn out to be, the
8
+ * query costs at most about twice the cheaper of pushing every change and
9
+ * hydrating from the start, plus the one change that crossed the line. It
10
+ * needs no estimate of what the remaining changes will cost.
11
+ */
12
+ function shouldCutOffPush({ pushTimeMs, hydrationTimeMs }) {
13
+ return pushTimeMs >= Math.max(hydrationTimeMs, 2);
14
+ }
15
+ /**
16
+ * Bounds the cost of one advancement query by query: each query is pushed
17
+ * changes until {@link ShouldCutOffPush} says to stop, and is then hydrated
18
+ * again once the advancement is done.
19
+ *
20
+ * An advancement with a cutoff is not subject to the client group's
21
+ * advancement time limit, which resets every pipeline of the client group.
22
+ * The two are alternatives and are never applied together.
23
+ */
24
+ var PushCutoff = class {
25
+ #shouldCutOff;
26
+ /** The queries pushed to since the last {@link takeCutOff}. */
27
+ #pushed = /* @__PURE__ */ new Set();
28
+ constructor(shouldCutOff = shouldCutOffPush) {
29
+ this.#shouldCutOff = shouldCutOff;
30
+ }
31
+ /** Called for each push to a query. */
32
+ recordPush(queryID) {
33
+ this.#pushed.add(queryID);
34
+ }
35
+ /**
36
+ * Called between changes, when no push is in progress. Returns the queries
37
+ * to stop pushing to, of those pushed to since the last call.
38
+ *
39
+ * @param progressOf The progress of a query, or `undefined` if it is no
40
+ * longer being pushed to.
41
+ */
42
+ takeCutOff(progressOf) {
43
+ if (this.#pushed.size === 0) return [];
44
+ const cutOff = [];
45
+ for (const queryID of this.#pushed) {
46
+ const progress = progressOf(queryID);
47
+ if (progress !== void 0 && this.#shouldCutOff(progress)) cutOff.push(progress);
48
+ }
49
+ this.#pushed.clear();
50
+ return cutOff;
51
+ }
52
+ };
53
+ //#endregion
54
+ export { PushCutoff, shouldCutOffPush };
55
+
56
+ //# sourceMappingURL=push-cutoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-cutoff.js","names":["#shouldCutOff","#pushed"],"sources":["../../../../../../zero-cache/src/services/view-syncer/push-cutoff.ts"],"sourcesContent":["/** What an advancement has pushed to a query so far. */\nexport type QueryPushProgress = {\n readonly queryID: string;\n /** The time spent processing pushes to the query in this advancement. */\n readonly pushTimeMs: number;\n /** The number of changes pushed to the query in this advancement. */\n readonly changes: number;\n /** The query's hydration time, i.e. what rebuilding it is expected to cost. */\n readonly hydrationTimeMs: number;\n};\n\n/** Whether to stop pushing an advancement's changes to a query. */\nexport type ShouldCutOffPush = (progress: QueryPushProgress) => boolean;\n\n/**\n * The least a query's hydration is taken to cost when deciding whether to\n * stop pushing to it. Small queries hydrate in well under a millisecond,\n * where one push can take longer than the hydration did on timer noise alone,\n * and hydrating again saves too little to be worth its fixed costs.\n */\nexport const MIN_PUSH_CUTOFF_MS = 2;\n\n/**\n * Whether to stop pushing an advancement's changes to a query and hydrate it\n * again instead: once the pushes have taken as long as the hydration is\n * expected to.\n *\n * This is the ski-rental strategy (rent until the rent paid equals the\n * purchase price, then buy). Whatever the actual costs turn out to be, the\n * query costs at most about twice the cheaper of pushing every change and\n * hydrating from the start, plus the one change that crossed the line. It\n * needs no estimate of what the remaining changes will cost.\n */\nexport function shouldCutOffPush({\n pushTimeMs,\n hydrationTimeMs,\n}: Pick<QueryPushProgress, 'pushTimeMs' | 'hydrationTimeMs'>): boolean {\n return pushTimeMs >= Math.max(hydrationTimeMs, MIN_PUSH_CUTOFF_MS);\n}\n\n/**\n * Bounds the cost of one advancement query by query: each query is pushed\n * changes until {@link ShouldCutOffPush} says to stop, and is then hydrated\n * again once the advancement is done.\n *\n * An advancement with a cutoff is not subject to the client group's\n * advancement time limit, which resets every pipeline of the client group.\n * The two are alternatives and are never applied together.\n */\nexport class PushCutoff {\n readonly #shouldCutOff: ShouldCutOffPush;\n /** The queries pushed to since the last {@link takeCutOff}. */\n readonly #pushed = new Set<string>();\n\n constructor(shouldCutOff: ShouldCutOffPush = shouldCutOffPush) {\n this.#shouldCutOff = shouldCutOff;\n }\n\n /** Called for each push to a query. */\n recordPush(queryID: string): void {\n this.#pushed.add(queryID);\n }\n\n /**\n * Called between changes, when no push is in progress. Returns the queries\n * to stop pushing to, of those pushed to since the last call.\n *\n * @param progressOf The progress of a query, or `undefined` if it is no\n * longer being pushed to.\n */\n takeCutOff(\n progressOf: (queryID: string) => QueryPushProgress | undefined,\n ): QueryPushProgress[] {\n if (this.#pushed.size === 0) {\n return [];\n }\n const cutOff: QueryPushProgress[] = [];\n for (const queryID of this.#pushed) {\n const progress = progressOf(queryID);\n if (progress !== undefined && this.#shouldCutOff(progress)) {\n cutOff.push(progress);\n }\n }\n this.#pushed.clear();\n return cutOff;\n }\n}\n"],"mappings":";;;;;;;;;;;AAiCA,SAAgB,iBAAiB,EAC/B,YACA,mBACqE;CACrE,OAAO,cAAc,KAAK,IAAI,iBAAA,CAAmC;AACnE;;;;;;;;;;AAWA,IAAa,aAAb,MAAwB;CACtB;;CAEA,0BAAmB,IAAI,IAAY;CAEnC,YAAY,eAAiC,kBAAkB;EAC7D,KAAKA,gBAAgB;CACvB;;CAGA,WAAW,SAAuB;EAChC,KAAKC,QAAQ,IAAI,OAAO;CAC1B;;;;;;;;CASA,WACE,YACqB;EACrB,IAAI,KAAKA,QAAQ,SAAS,GACxB,OAAO,CAAC;EAEV,MAAM,SAA8B,CAAC;EACrC,KAAK,MAAM,WAAW,KAAKA,SAAS;GAClC,MAAM,WAAW,WAAW,OAAO;GACnC,IAAI,aAAa,KAAA,KAAa,KAAKD,cAAc,QAAQ,GACvD,OAAO,KAAK,QAAQ;EAExB;EACA,KAAKC,QAAQ,MAAM;EACnB,OAAO;CACT;AACF"}
@@ -24,6 +24,8 @@ export type AdvanceRecord = {
24
24
  /** Whether the advancement timed out and reset the pipelines. */
25
25
  readonly timedOut: boolean;
26
26
  };
27
+ /** Why an advancement stopped pushing to a query and hydrated it again. */
28
+ export type AbandonReason = 'estimate' | 'read' | 'cutoff';
27
29
  export type QueryStatsOptions = {
28
30
  /**
29
31
  * The most query shapes tracked per interval. Work on shapes beyond this is
@@ -68,6 +70,11 @@ export declare class QueryStats {
68
70
  constructor({ maxShapes, maxReported, now, takeIndexUsage: takeIndexUsageFn, }?: QueryStatsOptions);
69
71
  recordHydration(query: QueryIdentity, record: HydrationRecord): void;
70
72
  recordAdvance(query: QueryIdentity, record: AdvanceRecord): void;
73
+ /**
74
+ * Records that an advancement abandoned the query's pipeline, to hydrate
75
+ * the query again.
76
+ */
77
+ recordAbandoned(query: QueryIdentity, reason: AbandonReason): void;
71
78
  /**
72
79
  * Logs the stats of the interval since the previous flush, and starts a
73
80
  * new interval. Nothing is logged for an interval without any work.
@@ -1 +1 @@
1
- {"version":3,"file":"query-stats.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/query-stats.ts"],"names":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AI,CASnE;IAED,aAAa,CAAC,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,GAAG,IAAI,CAG/D;IAED;;;OAGG;IACH,eAAe,CAAC,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,GAAG,IAAI,CAMjE;IAgBD;;;OAGG;IACH,KAAK,CAAC,EAAE,EAAE,UAAU,GAAG,IAAI,CAuC1B;IAED;;;OAGG;IACH,KAAK,CAAC,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,IAAI,CAOpD;CACF"}
@@ -30,6 +30,15 @@ var Totals = class {
30
30
  advances = new Distribution();
31
31
  advanceChanges = 0;
32
32
  advanceTimeouts = 0;
33
+ /**
34
+ * The advancements that hydrated the query again instead of pushing (all
35
+ * of) their changes to it, by why. The hydrations are in `hydrations`.
36
+ */
37
+ advancesAbandoned = {
38
+ estimate: 0,
39
+ read: 0,
40
+ cutoff: 0
41
+ };
33
42
  get timeMs() {
34
43
  return this.hydrations.sumMs + this.advances.sumMs;
35
44
  }
@@ -55,7 +64,8 @@ var Totals = class {
55
64
  hydrationRowsRead: this.hydrationRowsRead,
56
65
  advances: this.advances.toJSON(),
57
66
  advanceChanges: this.advanceChanges,
58
- advanceTimeouts: this.advanceTimeouts
67
+ advanceTimeouts: this.advanceTimeouts,
68
+ ...Object.values(this.advancesAbandoned).some((n) => n > 0) && { advancesAbandoned: { ...this.advancesAbandoned } }
59
69
  };
60
70
  }
61
71
  };
@@ -138,6 +148,15 @@ var QueryStats = class {
138
148
  this.#totals.addAdvance(record);
139
149
  this.#stats(query)?.addAdvance(record);
140
150
  }
151
+ /**
152
+ * Records that an advancement abandoned the query's pipeline, to hydrate
153
+ * the query again.
154
+ */
155
+ recordAbandoned(query, reason) {
156
+ this.#totals.advancesAbandoned[reason]++;
157
+ const stats = this.#stats(query);
158
+ if (stats) stats.advancesAbandoned[reason]++;
159
+ }
141
160
  #stats(query) {
142
161
  const key = `${query.queryName ?? ""}:${query.shape.hash}`;
143
162
  let stats = this.#shapes.get(key);
@@ -1 +1 @@
1
- {"version":3,"file":"query-stats.js","names":["#buckets","#occupied","#maxShapes","#maxReported","#now","#takeIndexUsage","#intervalStart","#totals","#stats","#shapes","#overflowShapes"],"sources":["../../../../../../zero-cache/src/services/view-syncer/query-stats.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport type {PlanWarning} from '../../../../zql/src/planner/planner-warnings.ts';\nimport {takeIndexUsage} from '../../../../zqlite/src/index-usage.ts';\nimport type {QueryShape} from './query-shape.ts';\n\n/** The query that work is accounted to. */\nexport type QueryIdentity = {\n readonly queryName?: string | undefined;\n readonly shape: QueryShape;\n};\n\nexport type HydrationOutcome = 'finished' | 'aborted' | 'failed';\n\nexport type HydrationRecord = {\n readonly outcome: HydrationOutcome;\n readonly timeMs: number;\n /** Only for a finished hydration. */\n readonly rowCount?: number | undefined;\n /** Only for a finished hydration. */\n readonly rowsRead?: number | undefined;\n readonly planWarnings?: readonly PlanWarning[] | undefined;\n};\n\nexport type AdvanceRecord = {\n /** The time spent processing the pushes to the query. */\n readonly timeMs: number;\n /** The number of changes pushed to the query. */\n readonly changes: number;\n /** Whether the advancement timed out and reset the pipelines. */\n readonly timedOut: boolean;\n};\n\nexport type QueryStatsOptions = {\n /**\n * The most query shapes tracked per interval. Work on shapes beyond this is\n * accounted to the interval's totals only, which bounds memory when the\n * shape space is unbounded (e.g. dynamically built queries).\n */\n readonly maxShapes?: number | undefined;\n\n /**\n * The most query shapes logged per interval, those that took the most\n * time. The rest are included in the interval's totals.\n */\n readonly maxReported?: number | undefined;\n\n readonly now?: (() => number) | undefined;\n\n /** Returns the index uses since the last call. See `recordIndexUsage()`. */\n readonly takeIndexUsage?: (() => Map<string, number>) | undefined;\n};\n\nclass Distribution {\n count = 0;\n sumMs = 0;\n minMs = Infinity;\n maxMs = 0;\n\n add(ms: number) {\n this.count++;\n this.sumMs += ms;\n this.minMs = Math.min(this.minMs, ms);\n this.maxMs = Math.max(this.maxMs, ms);\n }\n\n toJSON() {\n return {\n count: this.count,\n sumMs: round(this.sumMs),\n minMs: round(this.count === 0 ? 0 : this.minMs),\n maxMs: round(this.maxMs),\n };\n }\n}\n\nclass Totals {\n readonly hydrations = new Distribution();\n hydrationsAborted = 0;\n hydrationsFailed = 0;\n hydrationRowCount = 0;\n hydrationRowsRead = 0;\n readonly advances = new Distribution();\n advanceChanges = 0;\n advanceTimeouts = 0;\n\n get timeMs() {\n return this.hydrations.sumMs + this.advances.sumMs;\n }\n\n addHydration({outcome, timeMs, rowCount, rowsRead}: HydrationRecord) {\n this.hydrations.add(timeMs);\n if (outcome === 'aborted') {\n this.hydrationsAborted++;\n } else if (outcome === 'failed') {\n this.hydrationsFailed++;\n }\n this.hydrationRowCount += rowCount ?? 0;\n this.hydrationRowsRead += rowsRead ?? 0;\n }\n\n addAdvance({timeMs, changes, timedOut}: AdvanceRecord) {\n this.advances.add(timeMs);\n this.advanceChanges += changes;\n if (timedOut) {\n this.advanceTimeouts++;\n }\n }\n\n toJSON() {\n return {\n timeMs: round(this.timeMs),\n hydrations: this.hydrations.toJSON(),\n hydrationsAborted: this.hydrationsAborted,\n hydrationsFailed: this.hydrationsFailed,\n hydrationRowCount: this.hydrationRowCount,\n hydrationRowsRead: this.hydrationRowsRead,\n advances: this.advances.toJSON(),\n advanceChanges: this.advanceChanges,\n advanceTimeouts: this.advanceTimeouts,\n };\n }\n}\n\nclass ShapeStats extends Totals {\n readonly query: QueryIdentity;\n planWarnings: readonly PlanWarning[] | undefined;\n\n constructor(query: QueryIdentity) {\n super();\n this.query = query;\n }\n}\n\n/** A fixed-size linear-counting sketch for shapes beyond the tracking limit. */\nclass OverflowShapeCounter {\n readonly #buckets = new Uint8Array(4096);\n #occupied = 0;\n\n add(key: string): void {\n // FNV-1a gives a stable bucket without retaining the key.\n let hash = 0x811c9dc5;\n for (let i = 0; i < key.length; i++) {\n hash = Math.imul(hash ^ key.charCodeAt(i), 0x01000193);\n }\n const bucket = (hash >>> 0) % this.#buckets.length;\n if (this.#buckets[bucket] === 0) {\n this.#buckets[bucket] = 1;\n this.#occupied++;\n }\n }\n\n get estimatedCount(): number {\n const empty = this.#buckets.length - this.#occupied;\n return empty === 0\n ? this.#buckets.length\n : Math.round(\n -this.#buckets.length * Math.log(empty / this.#buckets.length),\n );\n }\n}\n\n/**\n * Aggregates the work done for each query shape (the query with its values\n * redacted, see {@link QueryShape}) on a worker, and periodically logs it,\n * like `pg_stat_statements` does for Postgres.\n *\n * Unlike the slow query logs, which are throttled and only written above a\n * threshold, every hydration and advancement is counted, so the logs can be\n * summed over any time range and across workers to rank queries by the time\n * they take.\n *\n * Each {@link flush} logs the interval since the previous one:\n *\n * - a `query-stats` event per query shape, for the {@link\n * QueryStatsOptions.maxReported} shapes that took the most time, and\n * - a `query-stats-summary` event with the totals of all shapes, including\n * those not reported individually, and the `indexUses`: how often the\n * process read each index of the replica, like `pg_stat_user_indexes`. An\n * index that is never read is absent. The workers of a single-process\n * zero-cache share these counts, which the first of them to flush reports.\n *\n * The events carry counts, sums, minimums and maximums, which compose across\n * intervals and workers.\n */\nexport class QueryStats {\n readonly #maxShapes: number;\n readonly #maxReported: number;\n readonly #now: () => number;\n readonly #takeIndexUsage: () => Map<string, number>;\n\n #shapes = new Map<string, ShapeStats>();\n #totals = new Totals();\n #overflowShapes = new OverflowShapeCounter();\n #intervalStart: number;\n\n constructor({\n maxShapes = 1000,\n maxReported = 100,\n now = Date.now,\n takeIndexUsage: takeIndexUsageFn = takeIndexUsage,\n }: QueryStatsOptions = {}) {\n assert(maxShapes > 0, 'maxShapes must be positive');\n assert(maxReported > 0, 'maxReported must be positive');\n this.#maxShapes = maxShapes;\n this.#maxReported = maxReported;\n this.#now = now;\n this.#takeIndexUsage = takeIndexUsageFn;\n this.#intervalStart = now();\n }\n\n recordHydration(query: QueryIdentity, record: HydrationRecord): void {\n this.#totals.addHydration(record);\n const stats = this.#stats(query);\n if (stats) {\n stats.addHydration(record);\n if (record.planWarnings !== undefined) {\n stats.planWarnings = record.planWarnings;\n }\n }\n }\n\n recordAdvance(query: QueryIdentity, record: AdvanceRecord): void {\n this.#totals.addAdvance(record);\n this.#stats(query)?.addAdvance(record);\n }\n\n #stats(query: QueryIdentity): ShapeStats | undefined {\n const key = `${query.queryName ?? ''}:${query.shape.hash}`;\n let stats = this.#shapes.get(key);\n if (stats === undefined) {\n if (this.#shapes.size >= this.#maxShapes) {\n this.#overflowShapes.add(key);\n return undefined;\n }\n stats = new ShapeStats({queryName: query.queryName, shape: query.shape});\n this.#shapes.set(key, stats);\n }\n return stats;\n }\n\n /**\n * Logs the stats of the interval since the previous flush, and starts a\n * new interval. Nothing is logged for an interval without any work.\n */\n flush(lc: LogContext): void {\n const now = this.#now();\n const intervalMs = now - this.#intervalStart;\n const shapes = this.#shapes;\n const totals = this.#totals;\n const overflowShapes = this.#overflowShapes.estimatedCount;\n const indexUses = this.#takeIndexUsage();\n this.#shapes = new Map();\n this.#totals = new Totals();\n this.#overflowShapes = new OverflowShapeCounter();\n this.#intervalStart = now;\n\n if (shapes.size === 0 && overflowShapes === 0 && indexUses.size === 0) {\n return;\n }\n const reported = [...shapes.values()]\n .toSorted((a, b) => b.timeMs - a.timeMs)\n .slice(0, this.#maxReported);\n for (const stats of reported) {\n const {queryName, shape} = stats.query;\n lc.info?.('query stats', {\n zeroEvent: 'query-stats',\n intervalMs,\n ...(queryName !== undefined && {queryName}),\n queryShape: shape.hash,\n ...stats.toJSON(),\n ...(stats.planWarnings !== undefined &&\n stats.planWarnings.length > 0 && {planWarnings: stats.planWarnings}),\n zql: shape.zql,\n });\n }\n lc.info?.('query stats summary', {\n zeroEvent: 'query-stats-summary',\n intervalMs,\n shapes: shapes.size + overflowShapes,\n shapesReported: reported.length,\n ...totals.toJSON(),\n ...(indexUses.size > 0 && {indexUses: Object.fromEntries(indexUses)}),\n });\n }\n\n /**\n * Flushes every `intervalMs` until the returned function is called, which\n * flushes one last time.\n */\n start(lc: LogContext, intervalMs: number): () => void {\n const timer = setInterval(() => this.flush(lc), intervalMs);\n timer.unref?.();\n return () => {\n clearInterval(timer);\n this.flush(lc);\n };\n }\n}\n\nfunction round(ms: number) {\n return Math.round(ms * 100) / 100;\n}\n"],"mappings":";;;AAqDA,IAAM,eAAN,MAAmB;CACjB,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CAER,IAAI,IAAY;EACd,KAAK;EACL,KAAK,SAAS;EACd,KAAK,QAAQ,KAAK,IAAI,KAAK,OAAO,EAAE;EACpC,KAAK,QAAQ,KAAK,IAAI,KAAK,OAAO,EAAE;CACtC;CAEA,SAAS;EACP,OAAO;GACL,OAAO,KAAK;GACZ,OAAO,MAAM,KAAK,KAAK;GACvB,OAAO,MAAM,KAAK,UAAU,IAAI,IAAI,KAAK,KAAK;GAC9C,OAAO,MAAM,KAAK,KAAK;EACzB;CACF;AACF;AAEA,IAAM,SAAN,MAAa;CACX,aAAsB,IAAI,aAAa;CACvC,oBAAoB;CACpB,mBAAmB;CACnB,oBAAoB;CACpB,oBAAoB;CACpB,WAAoB,IAAI,aAAa;CACrC,iBAAiB;CACjB,kBAAkB;CAElB,IAAI,SAAS;EACX,OAAO,KAAK,WAAW,QAAQ,KAAK,SAAS;CAC/C;CAEA,aAAa,EAAC,SAAS,QAAQ,UAAU,YAA4B;EACnE,KAAK,WAAW,IAAI,MAAM;EAC1B,IAAI,YAAY,WACd,KAAK;OACA,IAAI,YAAY,UACrB,KAAK;EAEP,KAAK,qBAAqB,YAAY;EACtC,KAAK,qBAAqB,YAAY;CACxC;CAEA,WAAW,EAAC,QAAQ,SAAS,YAA0B;EACrD,KAAK,SAAS,IAAI,MAAM;EACxB,KAAK,kBAAkB;EACvB,IAAI,UACF,KAAK;CAET;CAEA,SAAS;EACP,OAAO;GACL,QAAQ,MAAM,KAAK,MAAM;GACzB,YAAY,KAAK,WAAW,OAAO;GACnC,mBAAmB,KAAK;GACxB,kBA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KAAK,MAAM,SAAS,UAAU;GAC5B,MAAM,EAAC,WAAW,UAAS,MAAM;GACjC,GAAG,OAAO,eAAe;IACvB,WAAW;IACX;IACA,GAAI,cAAc,KAAA,KAAa,EAAC,UAAS;IACzC,YAAY,MAAM;IAClB,GAAG,MAAM,OAAO;IAChB,GAAI,MAAM,iBAAiB,KAAA,KACzB,MAAM,aAAa,SAAS,KAAK,EAAC,cAAc,MAAM,aAAY;IACpE,KAAK,MAAM;GACb,CAAC;EACH;EACA,GAAG,OAAO,uBAAuB;GAC/B,WAAW;GACX;GACA,QAAQ,OAAO,OAAO;GACtB,gBAAgB,SAAS;GACzB,GAAG,OAAO,OAAO;GACjB,GAAI,UAAU,OAAO,KAAK,EAAC,WAAW,OAAO,YAAY,SAAS,EAAC;EACrE,CAAC;CACH;;;;;CAMA,MAAM,IAAgB,YAAgC;EACpD,MAAM,QAAQ,kBAAkB,KAAK,MAAM,EAAE,GAAG,UAAU;EAC1D,MAAM,QAAQ;EACd,aAAa;GACX,cAAc,KAAK;GACnB,KAAK,MAAM,EAAE;EACf;CACF;AACF;AAEA,SAAS,MAAM,IAAY;CACzB,OAAO,KAAK,MAAM,KAAK,GAAG,IAAI;AAChC"}
1
+ {"version":3,"file":"query-stats.js","names":["#buckets","#occupied","#maxShapes","#maxReported","#now","#takeIndexUsage","#intervalStart","#totals","#stats","#shapes","#overflowShapes"],"sources":["../../../../../../zero-cache/src/services/view-syncer/query-stats.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport type {PlanWarning} from '../../../../zql/src/planner/planner-warnings.ts';\nimport {takeIndexUsage} from '../../../../zqlite/src/index-usage.ts';\nimport type {QueryShape} from './query-shape.ts';\n\n/** The query that work is accounted to. */\nexport type QueryIdentity = {\n readonly queryName?: string | undefined;\n readonly shape: QueryShape;\n};\n\nexport type HydrationOutcome = 'finished' | 'aborted' | 'failed';\n\nexport type HydrationRecord = {\n readonly outcome: HydrationOutcome;\n readonly timeMs: number;\n /** Only for a finished hydration. */\n readonly rowCount?: number | undefined;\n /** Only for a finished hydration. */\n readonly rowsRead?: number | undefined;\n readonly planWarnings?: readonly PlanWarning[] | undefined;\n};\n\nexport type AdvanceRecord = {\n /** The time spent processing the pushes to the query. */\n readonly timeMs: number;\n /** The number of changes pushed to the query. */\n readonly changes: number;\n /** Whether the advancement timed out and reset the pipelines. */\n readonly timedOut: boolean;\n};\n\n/** Why an advancement stopped pushing to a query and hydrated it again. */\nexport type AbandonReason = 'estimate' | 'read' | 'cutoff';\n\nexport type QueryStatsOptions = {\n /**\n * The most query shapes tracked per interval. Work on shapes beyond this is\n * accounted to the interval's totals only, which bounds memory when the\n * shape space is unbounded (e.g. dynamically built queries).\n */\n readonly maxShapes?: number | undefined;\n\n /**\n * The most query shapes logged per interval, those that took the most\n * time. The rest are included in the interval's totals.\n */\n readonly maxReported?: number | undefined;\n\n readonly now?: (() => number) | undefined;\n\n /** Returns the index uses since the last call. See `recordIndexUsage()`. */\n readonly takeIndexUsage?: (() => Map<string, number>) | undefined;\n};\n\nclass Distribution {\n count = 0;\n sumMs = 0;\n minMs = Infinity;\n maxMs = 0;\n\n add(ms: number) {\n this.count++;\n this.sumMs += ms;\n this.minMs = Math.min(this.minMs, ms);\n this.maxMs = Math.max(this.maxMs, ms);\n }\n\n toJSON() {\n return {\n count: this.count,\n sumMs: round(this.sumMs),\n minMs: round(this.count === 0 ? 0 : this.minMs),\n maxMs: round(this.maxMs),\n };\n }\n}\n\nclass Totals {\n readonly hydrations = new Distribution();\n hydrationsAborted = 0;\n hydrationsFailed = 0;\n hydrationRowCount = 0;\n hydrationRowsRead = 0;\n readonly advances = new Distribution();\n advanceChanges = 0;\n advanceTimeouts = 0;\n /**\n * The advancements that hydrated the query again instead of pushing (all\n * of) their changes to it, by why. The hydrations are in `hydrations`.\n */\n readonly advancesAbandoned: Record<AbandonReason, number> = {\n estimate: 0,\n read: 0,\n cutoff: 0,\n };\n\n get timeMs() {\n return this.hydrations.sumMs + this.advances.sumMs;\n }\n\n addHydration({outcome, timeMs, rowCount, rowsRead}: HydrationRecord) {\n this.hydrations.add(timeMs);\n if (outcome === 'aborted') {\n this.hydrationsAborted++;\n } else if (outcome === 'failed') {\n this.hydrationsFailed++;\n }\n this.hydrationRowCount += rowCount ?? 0;\n this.hydrationRowsRead += rowsRead ?? 0;\n }\n\n addAdvance({timeMs, changes, timedOut}: AdvanceRecord) {\n this.advances.add(timeMs);\n this.advanceChanges += changes;\n if (timedOut) {\n this.advanceTimeouts++;\n }\n }\n\n toJSON() {\n return {\n timeMs: round(this.timeMs),\n hydrations: this.hydrations.toJSON(),\n hydrationsAborted: this.hydrationsAborted,\n hydrationsFailed: this.hydrationsFailed,\n hydrationRowCount: this.hydrationRowCount,\n hydrationRowsRead: this.hydrationRowsRead,\n advances: this.advances.toJSON(),\n advanceChanges: this.advanceChanges,\n advanceTimeouts: this.advanceTimeouts,\n // Only with cost-based sync, without which there are none.\n ...(Object.values(this.advancesAbandoned).some(n => n > 0) && {\n advancesAbandoned: {...this.advancesAbandoned},\n }),\n };\n }\n}\n\nclass ShapeStats extends Totals {\n readonly query: QueryIdentity;\n planWarnings: readonly PlanWarning[] | undefined;\n\n constructor(query: QueryIdentity) {\n super();\n this.query = query;\n }\n}\n\n/** A fixed-size linear-counting sketch for shapes beyond the tracking limit. */\nclass OverflowShapeCounter {\n readonly #buckets = new Uint8Array(4096);\n #occupied = 0;\n\n add(key: string): void {\n // FNV-1a gives a stable bucket without retaining the key.\n let hash = 0x811c9dc5;\n for (let i = 0; i < key.length; i++) {\n hash = Math.imul(hash ^ key.charCodeAt(i), 0x01000193);\n }\n const bucket = (hash >>> 0) % this.#buckets.length;\n if (this.#buckets[bucket] === 0) {\n this.#buckets[bucket] = 1;\n this.#occupied++;\n }\n }\n\n get estimatedCount(): number {\n const empty = this.#buckets.length - this.#occupied;\n return empty === 0\n ? this.#buckets.length\n : Math.round(\n -this.#buckets.length * Math.log(empty / this.#buckets.length),\n );\n }\n}\n\n/**\n * Aggregates the work done for each query shape (the query with its values\n * redacted, see {@link QueryShape}) on a worker, and periodically logs it,\n * like `pg_stat_statements` does for Postgres.\n *\n * Unlike the slow query logs, which are throttled and only written above a\n * threshold, every hydration and advancement is counted, so the logs can be\n * summed over any time range and across workers to rank queries by the time\n * they take.\n *\n * Each {@link flush} logs the interval since the previous one:\n *\n * - a `query-stats` event per query shape, for the {@link\n * QueryStatsOptions.maxReported} shapes that took the most time, and\n * - a `query-stats-summary` event with the totals of all shapes, including\n * those not reported individually, and the `indexUses`: how often the\n * process read each index of the replica, like `pg_stat_user_indexes`. An\n * index that is never read is absent. The workers of a single-process\n * zero-cache share these counts, which the first of them to flush reports.\n *\n * The events carry counts, sums, minimums and maximums, which compose across\n * intervals and workers.\n */\nexport class QueryStats {\n readonly #maxShapes: number;\n readonly #maxReported: number;\n readonly #now: () => number;\n readonly #takeIndexUsage: () => Map<string, number>;\n\n #shapes = new Map<string, ShapeStats>();\n #totals = new Totals();\n #overflowShapes = new OverflowShapeCounter();\n #intervalStart: number;\n\n constructor({\n maxShapes = 1000,\n maxReported = 100,\n now = Date.now,\n takeIndexUsage: takeIndexUsageFn = takeIndexUsage,\n }: QueryStatsOptions = {}) {\n assert(maxShapes > 0, 'maxShapes must be positive');\n assert(maxReported > 0, 'maxReported must be positive');\n this.#maxShapes = maxShapes;\n this.#maxReported = maxReported;\n this.#now = now;\n this.#takeIndexUsage = takeIndexUsageFn;\n this.#intervalStart = now();\n }\n\n recordHydration(query: QueryIdentity, record: HydrationRecord): void {\n this.#totals.addHydration(record);\n const stats = this.#stats(query);\n if (stats) {\n stats.addHydration(record);\n if (record.planWarnings !== undefined) {\n stats.planWarnings = record.planWarnings;\n }\n }\n }\n\n recordAdvance(query: QueryIdentity, record: AdvanceRecord): void {\n this.#totals.addAdvance(record);\n this.#stats(query)?.addAdvance(record);\n }\n\n /**\n * Records that an advancement abandoned the query's pipeline, to hydrate\n * the query again.\n */\n recordAbandoned(query: QueryIdentity, reason: AbandonReason): void {\n this.#totals.advancesAbandoned[reason]++;\n const stats = this.#stats(query);\n if (stats) {\n stats.advancesAbandoned[reason]++;\n }\n }\n\n #stats(query: QueryIdentity): ShapeStats | undefined {\n const key = `${query.queryName ?? ''}:${query.shape.hash}`;\n let stats = this.#shapes.get(key);\n if (stats === undefined) {\n if (this.#shapes.size >= this.#maxShapes) {\n this.#overflowShapes.add(key);\n return undefined;\n }\n stats = new ShapeStats({queryName: query.queryName, shape: query.shape});\n this.#shapes.set(key, stats);\n }\n return stats;\n }\n\n /**\n * Logs the stats of the interval since the previous flush, and starts a\n * new interval. Nothing is logged for an interval without any work.\n */\n flush(lc: LogContext): void {\n const now = this.#now();\n const intervalMs = now - this.#intervalStart;\n const shapes = this.#shapes;\n const totals = this.#totals;\n const overflowShapes = this.#overflowShapes.estimatedCount;\n const indexUses = this.#takeIndexUsage();\n this.#shapes = new Map();\n this.#totals = new Totals();\n this.#overflowShapes = new OverflowShapeCounter();\n this.#intervalStart = now;\n\n if (shapes.size === 0 && overflowShapes === 0 && indexUses.size === 0) {\n return;\n }\n const reported = [...shapes.values()]\n .toSorted((a, b) => b.timeMs - a.timeMs)\n .slice(0, this.#maxReported);\n for (const stats of reported) {\n const {queryName, shape} = stats.query;\n lc.info?.('query stats', {\n zeroEvent: 'query-stats',\n intervalMs,\n ...(queryName !== undefined && {queryName}),\n queryShape: shape.hash,\n ...stats.toJSON(),\n ...(stats.planWarnings !== undefined &&\n stats.planWarnings.length > 0 && {planWarnings: stats.planWarnings}),\n zql: shape.zql,\n });\n }\n lc.info?.('query stats summary', {\n zeroEvent: 'query-stats-summary',\n intervalMs,\n shapes: shapes.size + overflowShapes,\n shapesReported: reported.length,\n ...totals.toJSON(),\n ...(indexUses.size > 0 && {indexUses: Object.fromEntries(indexUses)}),\n });\n }\n\n /**\n * Flushes every `intervalMs` until the returned function is called, which\n * flushes one last time.\n */\n start(lc: LogContext, intervalMs: number): () => void {\n const timer = setInterval(() => this.flush(lc), intervalMs);\n timer.unref?.();\n return () => {\n clearInterval(timer);\n this.flush(lc);\n };\n }\n}\n\nfunction round(ms: number) {\n return Math.round(ms * 100) / 100;\n}\n"],"mappings":";;;AAwDA,IAAM,eAAN,MAAmB;CACjB,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CAER,IAAI,IAAY;EACd,KAAK;EACL,KAAK,SAAS;EACd,KAAK,QAAQ,KAAK,IAAI,KAAK,OAAO,EAAE;EACpC,KAAK,QAAQ,KAAK,IAAI,KAAK,OAAO,EAAE;CACtC;CAEA,SAAS;EACP,OAAO;GACL,OAAO,KAAK;GACZ,OAAO,MAAM,KAAK,KAAK;GACvB,OAAO,MAAM,KAAK,UAAU,IAAI,IAAI,KAAK,KAAK;GAC9C,OAAO,MAAM,KAAK,KAAK;EACzB;CACF;AACF;AAEA,IAAM,SAAN,MAAa;CACX,aAAsB,IAAI,aAAa;CACvC,oBAAoB;CACpB,mBAAmB;CACnB,oBAAoB;CACpB,oBAAoB;CACpB,WAAoB,IAAI,aAAa;CACrC,iBAAiB;CACjB,kBAAkB;;;;;CAKlB,oBAA4D;EAC1D,UAAU;EACV,MAAM;EACN,QAAQ;CACV;CAEA,IAAI,SAAS;EACX,OAAO,KAAK,WAAW,QAAQ,KAAK,SAAS;CAC/C;CAEA,aAAa,EAAC,SAAS,QAAQ,UAAU,YAA4B;EACnE,KAAK,WAAW,IAAI,MAAM;EAC1B,IAAI,YAAY,WACd,KAAK;OACA,IAAI,YAAY,UACrB,KAAK;EAEP,KAAK,qBAAqB,YAAY;EACtC,KAAK,qBAAqB,YAAY;CACxC;CAEA,WAAW,EAAC,QAAQ,SAAS,YAA0B;EACrD,KAAK,SAAS,IAAI,MAAM;EACxB,KAAK,kBAAkB;EACvB,IAAI,UACF,KAAK;CAET;CAEA,SAAS;EACP,OAAO;GACL,QAAQ,MAAM,KAAK,MAAM;GACzB,YAAY,KAAK,WAAW,OAAO;GACnC,mBAAmB,KAAK;G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