@rocicorp/zero 1.11.0-canary.4 → 1.11.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 (123) hide show
  1. package/out/replicache/src/kv/expo-sqlite/store.d.ts.map +1 -1
  2. package/out/replicache/src/kv/expo-sqlite/store.js +27 -4
  3. package/out/replicache/src/kv/expo-sqlite/store.js.map +1 -1
  4. package/out/replicache/src/kv/sqlite-store.d.ts +11 -0
  5. package/out/replicache/src/kv/sqlite-store.d.ts.map +1 -1
  6. package/out/replicache/src/kv/sqlite-store.js +43 -3
  7. package/out/replicache/src/kv/sqlite-store.js.map +1 -1
  8. package/out/shared/src/error.d.ts +11 -0
  9. package/out/shared/src/error.d.ts.map +1 -1
  10. package/out/shared/src/error.js +35 -1
  11. package/out/shared/src/error.js.map +1 -1
  12. package/out/shared/src/map.d.ts +7 -2
  13. package/out/shared/src/map.d.ts.map +1 -1
  14. package/out/shared/src/map.js.map +1 -1
  15. package/out/zero/package.js +2 -2
  16. package/out/zero/package.js.map +1 -1
  17. package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
  18. package/out/zero-cache/src/config/normalize.js +7 -1
  19. package/out/zero-cache/src/config/normalize.js.map +1 -1
  20. package/out/zero-cache/src/config/zero-config.d.ts +9 -0
  21. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  22. package/out/zero-cache/src/config/zero-config.js +21 -0
  23. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  24. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  25. package/out/zero-cache/src/server/change-streamer.js +15 -6
  26. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  27. package/out/zero-cache/src/server/main.d.ts.map +1 -1
  28. package/out/zero-cache/src/server/main.js +8 -1
  29. package/out/zero-cache/src/server/main.js.map +1 -1
  30. package/out/zero-cache/src/server/mutator.js +2 -0
  31. package/out/zero-cache/src/server/mutator.js.map +1 -1
  32. package/out/zero-cache/src/server/reaper.d.ts.map +1 -1
  33. package/out/zero-cache/src/server/reaper.js +2 -0
  34. package/out/zero-cache/src/server/reaper.js.map +1 -1
  35. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  36. package/out/zero-cache/src/server/replicator.js +2 -0
  37. package/out/zero-cache/src/server/replicator.js.map +1 -1
  38. package/out/zero-cache/src/server/runner/zero-dispatcher.d.ts.map +1 -1
  39. package/out/zero-cache/src/server/runner/zero-dispatcher.js +3 -0
  40. package/out/zero-cache/src/server/runner/zero-dispatcher.js.map +1 -1
  41. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  42. package/out/zero-cache/src/server/syncer.js +2 -0
  43. package/out/zero-cache/src/server/syncer.js.map +1 -1
  44. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +1 -1
  45. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts.map +1 -1
  46. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js +68 -8
  47. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js.map +1 -1
  48. package/out/zero-cache/src/services/change-streamer/change-log-codec.d.ts +19 -4
  49. package/out/zero-cache/src/services/change-streamer/change-log-codec.d.ts.map +1 -1
  50. package/out/zero-cache/src/services/change-streamer/change-log-codec.js +28 -8
  51. package/out/zero-cache/src/services/change-streamer/change-log-codec.js.map +1 -1
  52. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +3 -0
  53. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
  54. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +3 -1
  55. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  56. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +6 -0
  57. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  58. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +86 -51
  59. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  60. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts +1 -1
  61. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts.map +1 -1
  62. package/out/zero-cache/src/services/change-streamer/schema/tables.js +4 -4
  63. package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
  64. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.d.ts +6 -1
  65. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.d.ts.map +1 -1
  66. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js +27 -9
  67. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js.map +1 -1
  68. package/out/zero-cache/src/services/change-streamer/storer.d.ts +2 -2
  69. package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
  70. package/out/zero-cache/src/services/change-streamer/storer.js +6 -5
  71. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  72. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js +50 -23
  73. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js.map +1 -1
  74. package/out/zero-cache/src/services/profz.d.ts +7 -0
  75. package/out/zero-cache/src/services/profz.d.ts.map +1 -0
  76. package/out/zero-cache/src/services/profz.js +99 -0
  77. package/out/zero-cache/src/services/profz.js.map +1 -0
  78. package/out/zero-cache/src/services/replicator/change-log-stream-writer.d.ts +5 -4
  79. package/out/zero-cache/src/services/replicator/change-log-stream-writer.d.ts.map +1 -1
  80. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js +20 -22
  81. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js.map +1 -1
  82. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts +6 -6
  83. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts.map +1 -1
  84. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js +19 -14
  85. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js.map +1 -1
  86. package/out/zero-cache/src/services/view-syncer/cvr.d.ts +17 -0
  87. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  88. package/out/zero-cache/src/services/view-syncer/cvr.js +59 -0
  89. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  90. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts +51 -0
  91. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts.map +1 -0
  92. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js +78 -0
  93. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js.map +1 -0
  94. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  95. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +18 -6
  96. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  97. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  98. package/out/zero-cache/src/services/view-syncer/view-syncer.js +154 -6
  99. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  100. package/out/zero-cache/src/types/processes.d.ts +24 -0
  101. package/out/zero-cache/src/types/processes.d.ts.map +1 -1
  102. package/out/zero-cache/src/types/processes.js +25 -1
  103. package/out/zero-cache/src/types/processes.js.map +1 -1
  104. package/out/zero-cache/src/types/profiler.d.ts +12 -0
  105. package/out/zero-cache/src/types/profiler.d.ts.map +1 -1
  106. package/out/zero-cache/src/types/profiler.js +46 -1
  107. package/out/zero-cache/src/types/profiler.js.map +1 -1
  108. package/out/zero-client/src/client/version.js +1 -1
  109. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  110. package/out/zero-client/src/client/zero.js +12 -8
  111. package/out/zero-client/src/client/zero.js.map +1 -1
  112. package/out/zero-protocol/src/down.d.ts +27 -27
  113. package/out/zero-react/src/use-query.d.ts.map +1 -1
  114. package/out/zero-react/src/use-query.js +50 -3
  115. package/out/zero-react/src/use-query.js.map +1 -1
  116. package/out/zero-solid/src/use-query.js +1 -2
  117. package/out/zero-solid/src/use-query.js.map +1 -1
  118. package/out/zql/src/ivm/data.d.ts.map +1 -1
  119. package/out/zql/src/ivm/data.js +8 -3
  120. package/out/zql/src/ivm/data.js.map +1 -1
  121. package/out/zql/src/ivm/view-apply-change.js +6 -1
  122. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  123. package/package.json +2 -2
@@ -455,6 +455,65 @@ var CVRQueryDrivenUpdater = class extends CVRUpdater {
455
455
  return patches;
456
456
  }
457
457
  /**
458
+ * Aborts queries that were tracked as executed but whose hydration was cut
459
+ * short (see the view-syncer's hydration circuit breaker).
460
+ *
461
+ * Rows received for an aborted query in this update have that query's
462
+ * references stripped: a row left with no references is deleted, and the
463
+ * 'del' patch cancels the 'put' the client has already received, while a
464
+ * row still referenced by other queries keeps its patch version. The query
465
+ * is then removed from the CVR. It stays tracked as executed, so
466
+ * {@link deleteUnreferencedRows} also drops the references held by rows that
467
+ * were synced for it before this update, exactly as for a removed query.
468
+ *
469
+ * Like {@link removeTrackedQueries}, this must be called after
470
+ * {@link trackQueries} has established the final version, and after all
471
+ * rows of the aborted queries have been {@link received}.
472
+ */
473
+ async abortExecutedQueries(lc, queryIDs) {
474
+ assert(this.#existingRows !== void 0, "trackQueries() was not called");
475
+ assert(cmpVersions(this._orig.version, this._cvr.version) < 0, "A final CVR version must be set before aborting executed queries");
476
+ const aborted = new Set(queryIDs);
477
+ for (const queryID of aborted) {
478
+ assert(this.#executedQueryIDs.has(queryID), `Query ${queryID} was not tracked as executed`);
479
+ assertNotInternal(must(this._cvr.queries[queryID]));
480
+ }
481
+ const existingRows = await this._cvrStore.getRowRecords();
482
+ const unrefs = new CustomKeyMap(rowIDString);
483
+ for (const [id, refCounts] of this.#receivedRows) {
484
+ if (refCounts === null) continue;
485
+ let unref;
486
+ for (const queryID of aborted) {
487
+ const count = refCounts[queryID];
488
+ if (!count) continue;
489
+ if (!unref) {
490
+ const version = this.#lastPatches.get(id)?.rowVersion ?? existingRows.get(id)?.rowVersion;
491
+ unref = {
492
+ refCounts: {},
493
+ ...version && { version }
494
+ };
495
+ unrefs.set(id, unref);
496
+ }
497
+ unref.refCounts[queryID] = -count;
498
+ }
499
+ }
500
+ const patches = unrefs.size > 0 ? await this.received(lc, unrefs) : [];
501
+ for (const queryID of aborted) {
502
+ delete this._cvr.queries[queryID];
503
+ const patch = {
504
+ type: "query",
505
+ op: "del",
506
+ id: queryID
507
+ };
508
+ this._cvrStore.markQueryAsDeleted(this._cvr.version, patch);
509
+ patches.push({
510
+ patch,
511
+ toVersion: this._cvr.version
512
+ });
513
+ }
514
+ return patches;
515
+ }
516
+ /**
458
517
  * Tracks a query removed from the "gotten" set. In addition to producing the
459
518
  * appropriate patches for deleting the query, the removed query is taken into
460
519
  * account when computing the final row records in
@@ -1 +1 @@
1
- {"version":3,"file":"cvr.js","names":["#shard","#deleteQueries","#removedOrExecutedQueryIDs","#executedQueryIDs","#receivedRows","#lastPatches","#rowSetSignature","#existingRows","#trackExecuted","#trackRemoved","#lookupRowsForExecutedAndRemovedQueries","#assertNewVersion","#deleteUnreferencedRow"],"sources":["../../../../../../zero-cache/src/services/view-syncer/cvr.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {startAsyncSpan, startSpan} from '../../../../otel/src/span.ts';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {type JSONObject} from '../../../../shared/src/bigint-json.ts';\nimport {CustomKeyMap} from '../../../../shared/src/custom-key-map.ts';\nimport {toSorted} from '../../../../shared/src/iterables.ts';\nimport {\n deepEqual,\n type ReadonlyJSONValue,\n} from '../../../../shared/src/json.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {\n difference,\n intersection,\n union,\n} from '../../../../shared/src/set-utils.ts';\nimport {stringCompare} from '../../../../shared/src/string-compare.ts';\nimport type {AST} from '../../../../zero-protocol/src/ast.ts';\nimport type {ClientSchema} from '../../../../zero-protocol/src/client-schema.ts';\nimport {ErrorOrigin} from '../../../../zero-protocol/src/error-origin.ts';\nimport {ProtocolError} from '../../../../zero-protocol/src/error.ts';\nimport {\n clampTTL,\n compareTTL,\n DEFAULT_TTL_MS,\n} from '../../../../zql/src/query/ttl.ts';\nimport {recordQuery} from '../../server/anonymous-otel-start.ts';\nimport type {LexiVersion} from '../../types/lexi-version.ts';\nimport {rowIDString} from '../../types/row-key.ts';\nimport {upstreamSchema, type ShardID} from '../../types/shards.ts';\nimport type {Patch, PatchToVersion} from './client-handler.ts';\nimport {type CVRFlushStats, type CVRStore} from './cvr-store.ts';\nimport {formatSignature, parseSignature} from './row-set-signature.ts';\nimport {\n cmpVersions,\n maxVersion,\n oneAfter,\n versionString,\n type ClientQueryRecord,\n type ClientRecord,\n type CustomQueryRecord,\n type CVRVersion,\n type InternalQueryRecord,\n type QueryRecord,\n type RowID,\n type RowRecord,\n} from './schema/types.ts';\nimport {tracer} from './tracer.ts';\nimport {ttlClockAsNumber, type TTLClock} from './ttl-clock.ts';\n\nexport type RowUpdate = {\n version?: string; // Undefined for an unref.\n contents?: JSONObject; // Undefined for an unref.\n refCounts: {[hash: string]: number}; // Counts are negative when a row is unrefed.\n};\n\n/** Internally used mutable CVR type. */\nexport type CVR = {\n id: string;\n version: CVRVersion;\n lastActive: number;\n ttlClock: TTLClock;\n replicaVersion: string | null;\n clients: Record<string, ClientRecord>;\n queries: Record<string, QueryRecord>;\n clientSchema: ClientSchema | null;\n profileID: string | null;\n};\n\n/** Exported immutable CVR type. */\n// TODO: Use Immutable<CVR> when the AST is immutable.\nexport type CVRSnapshot = {\n readonly id: string;\n readonly version: CVRVersion;\n readonly lastActive: number;\n readonly ttlClock: TTLClock;\n readonly replicaVersion: string | null;\n readonly clients: Readonly<Record<string, ClientRecord>>;\n readonly queries: Readonly<Record<string, QueryRecord>>;\n readonly clientSchema: ClientSchema | null;\n readonly profileID: string | null;\n};\n\nconst CLIENT_LMID_QUERY_ID = 'lmids';\nconst CLIENT_MUTATION_RESULTS_QUERY_ID = 'mutationResults';\n\nfunction assertNotInternal(\n query: QueryRecord,\n): asserts query is ClientQueryRecord {\n if (query.type === 'internal') {\n // This should never happen for behaving clients, as query ids should be hashes.\n throw new Error(`Query ID ${query.id} is reserved for internal use`);\n }\n}\n\nexport function getMutationResultsQuery(\n upstreamSchema: string,\n clientGroupID: string,\n): InternalQueryRecord {\n return {\n id: CLIENT_MUTATION_RESULTS_QUERY_ID,\n type: 'internal',\n ast: {\n schema: '',\n table: `${upstreamSchema}.mutations`,\n where: {\n type: 'and',\n conditions: [\n {\n type: 'simple',\n left: {\n type: 'column',\n name: 'clientGroupID',\n },\n op: '=',\n right: {\n type: 'literal',\n value: clientGroupID,\n },\n },\n ],\n },\n orderBy: [\n ['clientGroupID', 'asc'],\n ['clientID', 'asc'],\n ['mutationID', 'asc'],\n ],\n },\n };\n}\n\n/**\n * The base CVRUpdater contains logic common to the {@link CVRConfigDrivenUpdater} and\n * {@link CVRQueryDrivenUpdater}. The CVRUpdater class itself is exported for updating\n * the `lastActive` time of the CVR in the absence of any changes to the CVR contents.\n * Although activity is automatically tracked when the CVR contents change, there may be\n * edge cases in which a client actively connects to a CVR that doesn't itself change.\n * Calling `new CVRUpdater(...).flush()` will explicitly update the active index and\n * prevent the CVR from being garbage collected.\n */\nexport class CVRUpdater {\n protected readonly _orig: CVRSnapshot;\n protected readonly _cvr: CVR;\n\n protected readonly _cvrStore: CVRStore;\n\n /**\n * @param cvrStore The CVRStore to use for storage\n * @param cvr The current CVR\n */\n constructor(\n cvrStore: CVRStore,\n cvr: CVRSnapshot,\n replicaVersion: string | null,\n ) {\n this._cvrStore = cvrStore;\n this._orig = cvr;\n this._cvr = structuredClone(cvr) as CVR; // mutable deep copy\n this._cvr.replicaVersion = replicaVersion;\n }\n\n protected _setVersion(version: CVRVersion) {\n assert(\n cmpVersions(this._cvr.version, version) < 0,\n 'Expected new version to be greater than current version',\n );\n this._cvr.version = version;\n return version;\n }\n\n /**\n * Ensures that the new CVR has a higher version than the original.\n * This method is idempotent in that it will always return the same\n * (possibly bumped) version.\n */\n protected _ensureNewVersion(): CVRVersion {\n if (cmpVersions(this._orig.version, this._cvr.version) === 0) {\n this._setVersion(oneAfter(this._cvr.version));\n }\n return this._cvr.version;\n }\n\n async flush(\n lc: LogContext,\n lastConnectTime: number,\n lastActive: number,\n ttlClock: TTLClock,\n ): Promise<{\n cvr: CVRSnapshot;\n flushed: CVRFlushStats | false;\n }> {\n this._cvr.ttlClock = ttlClock;\n this._cvr.lastActive = lastActive;\n const flushed = await this._cvrStore.flush(\n lc,\n this._orig.version,\n this._cvr,\n lastConnectTime,\n );\n if (!flushed) {\n return {cvr: this._orig, flushed: false};\n }\n return {cvr: this._cvr, flushed};\n }\n}\n\n/**\n * A {@link CVRConfigDrivenUpdater} is used for updating a CVR with config-driven\n * changes. Note that this may result in row deletion (e.g. if queries get dropped),\n * but the `stateVersion` of the CVR does not change.\n */\nexport class CVRConfigDrivenUpdater extends CVRUpdater {\n readonly #shard: ShardID;\n\n constructor(cvrStore: CVRStore, cvr: CVRSnapshot, shard: ShardID) {\n super(cvrStore, cvr, cvr.replicaVersion);\n this.#shard = shard;\n }\n\n ensureClient(id: string): ClientRecord {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.ensureClient', () => {\n let client = this._cvr.clients[id];\n if (client) {\n return client;\n }\n // Add the ClientRecord and PutPatch\n client = {id, desiredQueryIDs: []};\n this._cvr.clients[id] = client;\n\n this._ensureNewVersion();\n this._cvrStore.insertClient(client);\n\n if (!this._cvr.queries[CLIENT_LMID_QUERY_ID]) {\n const lmidsQuery: InternalQueryRecord = {\n id: CLIENT_LMID_QUERY_ID,\n ast: {\n schema: '',\n table: `${upstreamSchema(this.#shard)}.clients`,\n where: {\n type: 'simple',\n left: {\n type: 'column',\n name: 'clientGroupID',\n },\n op: '=',\n right: {\n type: 'literal',\n value: this._cvr.id,\n },\n },\n orderBy: [\n ['clientGroupID', 'asc'],\n ['clientID', 'asc'],\n ],\n },\n type: 'internal',\n };\n this._cvr.queries[CLIENT_LMID_QUERY_ID] = lmidsQuery;\n this._cvrStore.putQuery(lmidsQuery);\n }\n if (!this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID]) {\n const mutationResultsQuery: InternalQueryRecord =\n getMutationResultsQuery(upstreamSchema(this.#shard), this._cvr.id);\n this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID] =\n mutationResultsQuery;\n this._cvrStore.putQuery(mutationResultsQuery);\n }\n\n return client;\n });\n }\n\n setClientSchema(lc: LogContext, clientSchema: ClientSchema) {\n startSpan(tracer, 'CVRConfigDrivenUpdater.setClientSchema', () => {\n if (this._cvr.clientSchema === null) {\n this._cvr.clientSchema = clientSchema;\n this._cvrStore.putInstance(this._cvr);\n } else if (!deepEqual(this._cvr.clientSchema, clientSchema)) {\n // This should not be possible with a correct Zero client, as clients\n // of a CVR should all have the same schema (given that the schema hash\n // is part of the idb key). In fact, clients joining an existing group\n // (i.e. non-empty baseCookie) do not send the clientSchema message.\n lc.warn?.(\n `New schema ${JSON.stringify(\n clientSchema,\n )} does not match existing schema ${JSON.stringify(\n this._cvr.clientSchema,\n )}`,\n );\n throw new ProtocolError({\n kind: 'InvalidConnectionRequest',\n message: `Provided schema does not match previous schema`,\n origin: ErrorOrigin.ZeroCache,\n });\n }\n });\n }\n\n setProfileID(lc: LogContext, profileID: string) {\n if (this._cvr.profileID !== profileID) {\n if (\n this._cvr.profileID !== null &&\n !this._cvr.profileID.startsWith('cg')\n ) {\n // We expect profile ID's to change from null or from the back-filled\n // \"cg...\" value. Log a warning otherwise to surface unexpected or\n // pathological conditions.\n lc.warn?.(\n `changing profile ID from ${this._cvr.profileID} to ${profileID}`,\n );\n }\n this._cvr.profileID = profileID;\n this._cvrStore.putInstance(this._cvr);\n }\n }\n\n putDesiredQueries(\n clientID: string,\n queries: Readonly<{\n hash: string;\n ast?: AST | undefined;\n name?: string | undefined;\n args?: readonly ReadonlyJSONValue[] | undefined;\n ttl?: number | undefined;\n }>[],\n ): PatchToVersion[] {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.putDesiredQueries', () => {\n const patches: PatchToVersion[] = [];\n const client = this.ensureClient(clientID);\n const current = new Set(client.desiredQueryIDs);\n\n // Find the new/changed desired queries.\n const needed: Set<string> = new Set();\n\n const recordQueryForTelemetry = (q: (typeof queries)[0]) => {\n const {ast, name, args} = q;\n if (ast) {\n recordQuery('crud');\n } else if (name && args) {\n recordQuery('custom');\n }\n };\n\n for (const q of queries) {\n const {hash, ttl = DEFAULT_TTL_MS} = q;\n const query = this._cvr.queries[hash];\n if (!query) {\n // New query - record for telemetry\n recordQueryForTelemetry(q);\n needed.add(hash);\n continue;\n }\n if (query.type === 'internal') {\n continue;\n }\n\n const oldClientState = query.clientState[clientID];\n // Old query was inactivated or never desired by this client.\n if (!oldClientState || oldClientState.inactivatedAt !== undefined) {\n // Reactivated query - record for telemetry\n recordQueryForTelemetry(q);\n needed.add(hash);\n continue;\n }\n\n if (compareTTL(ttl, oldClientState.ttl) > 0) {\n // TTL update only - don't record for telemetry\n needed.add(hash);\n }\n }\n\n if (needed.size === 0) {\n return patches;\n }\n const newVersion = this._ensureNewVersion();\n client.desiredQueryIDs = toSorted(union(current, needed), stringCompare);\n\n for (const id of needed) {\n const q = must(queries.find(({hash}) => hash === id));\n const {ast, name, args} = q;\n\n const ttl = clampTTL(q.ttl ?? DEFAULT_TTL_MS);\n const query =\n this._cvr.queries[id] ?? newQueryRecord(id, ast, name, args);\n assertNotInternal(query);\n\n const inactivatedAt = undefined;\n\n query.clientState[clientID] = {\n inactivatedAt,\n ttl,\n version: newVersion,\n };\n this._cvr.queries[id] = query;\n patches.push({\n toVersion: newVersion,\n patch: {type: 'query', op: 'put', id, clientID},\n });\n\n this._cvrStore.putQuery(query);\n this._cvrStore.putDesiredQuery(\n newVersion,\n query,\n client,\n false,\n inactivatedAt,\n ttl,\n );\n }\n return patches;\n });\n }\n\n markDesiredQueriesAsInactive(\n clientID: string,\n queryHashes: string[],\n ttlClock: TTLClock,\n ): PatchToVersion[] {\n return this.#deleteQueries(clientID, queryHashes, ttlClock);\n }\n\n deleteDesiredQueries(\n clientID: string,\n queryHashes: string[],\n ): PatchToVersion[] {\n return this.#deleteQueries(clientID, queryHashes, undefined);\n }\n\n #deleteQueries(\n clientID: string,\n queryHashes: string[],\n inactivatedAt: TTLClock | undefined,\n ): PatchToVersion[] {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.#deleteQueries', () => {\n const patches: PatchToVersion[] = [];\n const client = this.ensureClient(clientID);\n const current = new Set(client.desiredQueryIDs);\n const unwanted = new Set(queryHashes);\n const remove = intersection(unwanted, current);\n if (remove.size === 0) {\n return patches;\n }\n\n const newVersion = this._ensureNewVersion();\n client.desiredQueryIDs = toSorted(\n difference(current, remove),\n stringCompare,\n );\n\n for (const id of remove) {\n const query = this._cvr.queries[id];\n if (!query) {\n continue; // Query itself has already been removed. Should not happen?\n }\n assertNotInternal(query);\n\n let ttl = DEFAULT_TTL_MS;\n if (inactivatedAt === undefined) {\n delete query.clientState[clientID];\n } else {\n // client state can be missing if the query never transformed so we never\n // recorded it.\n const clientState = query.clientState[clientID];\n if (clientState !== undefined) {\n assert(\n clientState.inactivatedAt === undefined,\n `Query ${id} is already inactivated`,\n );\n // Clamp TTL to ensure we don't propagate historical unclamped values.\n ttl = clampTTL(clientState.ttl);\n query.clientState[clientID] = {\n inactivatedAt,\n ttl,\n version: newVersion,\n };\n }\n }\n\n this._cvrStore.putQuery(query);\n this._cvrStore.putDesiredQuery(\n newVersion,\n query,\n client,\n true,\n inactivatedAt,\n ttl,\n );\n patches.push({\n toVersion: newVersion,\n patch: {type: 'query', op: 'del', id, clientID},\n });\n }\n return patches;\n });\n }\n\n clearDesiredQueries(clientID: string): PatchToVersion[] {\n const client = this.ensureClient(clientID);\n return this.#deleteQueries(clientID, client.desiredQueryIDs, undefined);\n }\n\n deleteClient(clientID: string, ttlClock: TTLClock): PatchToVersion[] {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.deleteClient', () => {\n // clientID might not be part of this client group but if it is, this delete\n // may generate changes to the desired queries.\n\n const client = this._cvr.clients[clientID];\n if (!client) {\n // Clients in different client groups are no longer deleted, leaving\n // cleanup to inactive CVR purging logic.\n return [];\n }\n\n // When a client is deleted we mark all of its desired queries as inactive.\n // They will then be removed when the queries expire.\n const patches = this.markDesiredQueriesAsInactive(\n clientID,\n client.desiredQueryIDs,\n ttlClock,\n );\n delete this._cvr.clients[clientID];\n this._cvrStore.deleteClient(clientID);\n\n return patches;\n });\n }\n}\n\ntype Hash = string;\nexport type Column = string;\nexport type RefCounts = Record<Hash, number>;\n\ntype RowPatchInfo = {\n rowVersion: string | null; // null for a row-del\n toVersion: CVRVersion; // patchVersion\n};\n\n/**\n * Callback used by {@link CVRQueryDrivenUpdater.flush} to retrieve the\n * current row-set signature maintained by the pipeline driver for a given\n * query. Returning `undefined` means \"no pipeline for this query right now\" —\n * the stored signature on disk is left alone.\n */\nexport type RowSetSignatureProvider = (queryID: string) => bigint | undefined;\n\n/**\n * A {@link CVRQueryDrivenUpdater} is used for updating a CVR after making queries.\n * The caller should invoke:\n *\n * * {@link trackQueries} for queries that are being executed or removed.\n * * {@link received} for all rows received from the executed queries\n * * {@link removeTrackedQueries} for optional queries that did not execute\n * * {@link deleteUnreferencedRows} to remove any rows that have\n * fallen out of the query result view.\n * * {@link flush}\n *\n * After flushing, the caller should perform any necessary catchup of\n * config and row patches for clients that are behind. See\n * {@link CVRStore.catchupConfigPatches} and {@link CVRStore.catchupRowPatches}.\n */\nexport class CVRQueryDrivenUpdater extends CVRUpdater {\n readonly #removedOrExecutedQueryIDs = new Set<string>();\n readonly #executedQueryIDs = new Set<string>();\n readonly #receivedRows = new CustomKeyMap<RowID, RefCounts | null>(\n rowIDString,\n );\n readonly #lastPatches = new CustomKeyMap<RowID, RowPatchInfo>(rowIDString);\n readonly #rowSetSignature: RowSetSignatureProvider | undefined;\n\n #existingRows: Promise<Iterable<RowRecord>> | undefined = undefined;\n\n /**\n * @param stateVersion The `stateVersion` at which the queries were executed.\n * @param rowSetSignature Optional callback that returns the current row-set\n * signature for a query, typically backed by\n * `PipelineDriver.rowSetSignature`. When provided, {@link flush}\n * persists any signature deltas it observes.\n */\n constructor(\n cvrStore: CVRStore,\n cvr: CVRSnapshot,\n stateVersion: LexiVersion,\n replicaVersion: string,\n rowSetSignature?: RowSetSignatureProvider,\n ) {\n super(cvrStore, cvr, replicaVersion);\n this.#rowSetSignature = rowSetSignature;\n\n assert(\n // We should either be setting the cvr.replicaVersion for the first time, or it should\n // be something newer than the current cvr.replicaVersion. Otherwise, the CVR should\n // have been rejected by the ViewSyncer.\n (cvr.replicaVersion ?? replicaVersion) <= replicaVersion,\n `Cannot sync from an older replicaVersion: CVR=${cvr.replicaVersion}, DB=${replicaVersion}`,\n );\n assert(\n stateVersion >= cvr.version.stateVersion,\n () =>\n `stateVersion (${stateVersion}) must be >= cvr.version.stateVersion (${cvr.version.stateVersion})`,\n );\n if (stateVersion > cvr.version.stateVersion) {\n this._setVersion({stateVersion});\n }\n }\n\n /**\n * Initiates the tracking of the specified `executed` and `removed` queries.\n * This kicks of a lookup of existing {@link RowRecord}s currently associated\n * with those queries, which will be used to reconcile the rows to keep\n * after all rows have been {@link received()}.\n *\n * \"transformed\" queries are queries that are currently\n * gotten and running in the pipeline driver but\n * received a new transformation hash due to an auth token\n * update.\n *\n * @returns The new CVRVersion to be used when all changes are committed.\n */\n trackQueries(\n lc: LogContext,\n executed: {id: string; transformationHash: string}[],\n removed: {id: string}[],\n ): {newVersion: CVRVersion; queryPatches: PatchToVersion[]} {\n return startSpan(tracer, 'CVRQueryDrivenUpdater.trackQueries', () => {\n assert(this.#existingRows === undefined, `trackQueries already called`);\n\n const queryPatches: Patch[] = [\n executed.map(q => this.#trackExecuted(q.id, q.transformationHash)),\n removed.map(q => this.#trackRemoved(q.id)),\n ].flat(2);\n\n this.#existingRows = this.#lookupRowsForExecutedAndRemovedQueries(lc);\n // Immediately attach a rejection handler to avoid unhandled rejections.\n // The error will surface when this.#existingRows is awaited.\n void this.#existingRows.then(() => {});\n\n const versionBumped =\n cmpVersions(this._orig.version, this._cvr.version) < 0;\n lc.info?.(\n `trackQueries: ${executed.length} executed, ${removed.length} removed, ` +\n `version ${versionBumped ? 'bumped' : 'unchanged'}`,\n );\n\n return {\n newVersion: this._cvr.version,\n queryPatches: queryPatches.map(patch => ({\n patch,\n toVersion: this._cvr.version,\n })),\n };\n });\n }\n\n #lookupRowsForExecutedAndRemovedQueries(\n lc: LogContext,\n ): Promise<Iterable<RowRecord>> {\n return startAsyncSpan(\n tracer,\n 'CVRQueryDrivenUpdater.#lookupRowsForExecutedAndRemovedQueries',\n async () => {\n const results = new CustomKeyMap<RowID, RowRecord>(rowIDString);\n\n if (this.#removedOrExecutedQueryIDs.size === 0) {\n // Query-less update. This can happen for config only changes.\n return [];\n }\n\n // Utilizes the in-memory RowCache.\n const allRowRecords = (await this._cvrStore.getRowRecords()).values();\n let total = 0;\n for (const existing of allRowRecords) {\n total++;\n assert(\n existing.refCounts !== null,\n 'allRowRecords should not include null refCounts',\n );\n for (const id of Object.keys(existing.refCounts)) {\n if (this.#removedOrExecutedQueryIDs.has(id)) {\n results.set(existing.id, existing);\n break;\n }\n }\n }\n\n lc.debug?.(\n `found ${\n results.size\n } (of ${total}) rows for executed / removed queries ${[\n ...this.#removedOrExecutedQueryIDs,\n ]}`,\n );\n return results.values();\n },\n );\n }\n\n /**\n * Tracks an executed query, ensures that it is marked as \"gotten\",\n * updating the CVR and creating put patches if necessary.\n *\n * This must be called for all executed queries.\n */\n #trackExecuted(queryID: string, transformationHash: string): Patch[] {\n return startSpan(tracer, 'CVRQueryDrivenUpdater.#trackExecuted', () => {\n assert(\n !this.#removedOrExecutedQueryIDs.has(queryID),\n () => `Query ${queryID} already tracked as executed or removed`,\n );\n this.#removedOrExecutedQueryIDs.add(queryID);\n this.#executedQueryIDs.add(queryID);\n\n let gotQueryPatch: Patch | undefined;\n const query = this._cvr.queries[queryID];\n if (query.transformationHash !== transformationHash) {\n const transformationVersion = this._ensureNewVersion();\n\n if (query.type !== 'internal' && query.patchVersion === undefined) {\n // client query: desired -> gotten\n query.patchVersion = transformationVersion;\n gotQueryPatch = {\n type: 'query',\n op: 'put',\n id: query.id,\n };\n }\n\n query.transformationHash = transformationHash;\n query.transformationVersion = transformationVersion;\n this._cvrStore.updateQuery(query);\n }\n return gotQueryPatch ? [gotQueryPatch] : [];\n });\n }\n\n /**\n * Converts queries that were tracked as executed into normal query\n * removals. This is used when a hydration budget expires after\n * `trackQueries()` has established the final poke version but before an\n * optional query starts hydration.\n *\n * The query ID remains in `#removedOrExecutedQueryIDs`, so its row\n * references are dropped by {@link deleteUnreferencedRows} exactly as they\n * would be for a query removed up front. Never un-track a query here: rows\n * already flushed by {@link received} have had its refCounts stripped and\n * cannot be restored.\n */\n removeTrackedQueries(queryIDs: Iterable<string>): PatchToVersion[] {\n assert(this.#existingRows !== undefined, 'trackQueries() was not called');\n assert(\n cmpVersions(this._orig.version, this._cvr.version) < 0,\n 'A final CVR version must be set before removing tracked queries',\n );\n\n const patches: PatchToVersion[] = [];\n for (const queryID of queryIDs) {\n assert(\n this.#executedQueryIDs.has(queryID),\n () => `Query ${queryID} was not tracked as executed`,\n );\n const query = must(this._cvr.queries[queryID]);\n assertNotInternal(query);\n assert(\n Object.values(query.clientState).every(\n ({inactivatedAt}) => inactivatedAt !== undefined,\n ),\n () => `Query ${queryID} is active`,\n );\n assert(\n query.patchVersion !== undefined,\n `Query ${queryID} is not gotten`,\n );\n\n delete this._cvr.queries[queryID];\n const patch = {type: 'query', op: 'del', id: queryID} as const;\n this._cvrStore.markQueryAsDeleted(this._cvr.version, patch);\n patches.push({patch, toVersion: this._cvr.version});\n }\n return patches;\n }\n\n /**\n * Tracks a query removed from the \"gotten\" set. In addition to producing the\n * appropriate patches for deleting the query, the removed query is taken into\n * account when computing the final row records in\n * {@link deleteUnreferencedRows}.\n * Namely, any rows with columns that are no longer referenced by a\n * query are deleted.\n *\n * This must only be called on queries that are not \"desired\" by any client.\n */\n #trackRemoved(queryID: string): Patch[] {\n return startSpan(tracer, 'CVRQueryDrivenUpdater.#trackRemoved', () => {\n const query = this._cvr.queries[queryID];\n assertNotInternal(query);\n\n assert(\n !this.#removedOrExecutedQueryIDs.has(queryID),\n () => `Query ${queryID} already tracked as executed or removed`,\n );\n this.#removedOrExecutedQueryIDs.add(queryID);\n delete this._cvr.queries[queryID];\n\n const newVersion = this._ensureNewVersion();\n const queryPatch = {type: 'query', op: 'del', id: queryID} as const;\n this._cvrStore.markQueryAsDeleted(newVersion, queryPatch);\n return [queryPatch];\n });\n }\n\n /**\n * Asserts that a new version has already been set.\n *\n * After {@link #executed} and {@link #removed} are called, we must have properly\n * decided on the final CVR version because the poke-start message declares the\n * final cookie (i.e. version), and that must be sent before any poke parts\n * generated from {@link received} are sent.\n */\n #assertNewVersion(\n rowID: RowID,\n existingVersion: string | undefined,\n newVersion: string | undefined,\n refCounts: RefCounts,\n ): CVRVersion {\n assert(\n cmpVersions(this._orig.version, this._cvr.version) < 0,\n () =>\n `Expected CVR version to have been bumped above original` +\n ` (orig=${versionString(this._orig.version)},` +\n ` curr=${versionString(this._cvr.version)}).` +\n ` Row ${JSON.stringify(rowID)}:` +\n ` existing=${existingVersion},` +\n ` new=${newVersion},` +\n ` queries=[${Object.keys(refCounts).join(',')}]`,\n );\n return this._cvr.version;\n }\n\n updatedVersion(): CVRVersion {\n return this._cvr.version;\n }\n\n /**\n * Public alias for {@link _ensureNewVersion}. Forces a `configVersion` bump\n * when the caller needs a new CVR version for reasons outside the built-in\n * tracking (e.g. rowSetSignature drift re-execution).\n */\n ensureNewVersion(): CVRVersion {\n return this._ensureNewVersion();\n }\n\n override flush(\n lc: LogContext,\n lastConnectTime: number,\n lastActive: number,\n ttlClock: TTLClock,\n ): Promise<{cvr: CVRSnapshot; flushed: CVRFlushStats | false}> {\n if (this.#rowSetSignature) {\n // Persist the per-query row-set signature for any query whose\n // pipeline-driver signature differs from what's on disk. Queries\n // without an active pipeline (provider returns undefined) keep their\n // stored value.\n for (const [queryID, query] of Object.entries(this._cvr.queries)) {\n const sig = this.#rowSetSignature(queryID);\n if (sig === undefined) {\n continue;\n }\n const stored = parseSignature(query.rowSetSignature);\n if (stored === sig) {\n continue;\n }\n const hex = formatSignature(sig);\n query.rowSetSignature = hex;\n this._cvrStore.updateRowSetSignature(queryID, hex);\n }\n }\n return super.flush(lc, lastConnectTime, lastActive, ttlClock);\n }\n\n /**\n * Tracks rows received from executing queries. This will update row records\n * and row patches if the received rows have a new version. The method also\n * returns (put) patches to be returned to update their state, versioned by\n * patchVersion so that only the patches new to the clients are sent.\n */\n received(\n _lc: LogContext,\n rows: Map<RowID, RowUpdate>,\n ): Promise<PatchToVersion[]> {\n return startAsyncSpan(\n tracer,\n 'CVRQueryDrivenUpdater.received',\n async () => {\n const patches: PatchToVersion[] = [];\n const existingRows = await this._cvrStore.getRowRecords();\n\n for (const [id, update] of rows.entries()) {\n const {contents, version, refCounts} = update;\n\n let existing = existingRows.get(id);\n // Accumulate all received refCounts to determine which rows to prune.\n const previouslyReceived = this.#receivedRows.get(id);\n\n const merged =\n previouslyReceived !== undefined\n ? mergeRefCounts(previouslyReceived, refCounts)\n : mergeRefCounts(\n existing?.refCounts,\n refCounts,\n this.#removedOrExecutedQueryIDs,\n );\n\n this.#receivedRows.set(id, merged);\n\n const newRowVersion = merged === null ? undefined : version;\n const patchVersion =\n existing && existing.rowVersion === newRowVersion\n ? existing.patchVersion // existing row is unchanged\n : this.#assertNewVersion(\n id,\n existing?.rowVersion,\n newRowVersion,\n refCounts,\n );\n\n // Note: for determining what to commit to the CVR store, use the\n // `version` of the update even if `merged` is null (i.e. don't\n // use `newRowVersion`). This will be deduped by the cvr-store flush\n // if it is redundant. In rare cases--namely, if the row key has\n // changed--we _do_ want to add row-put for the new row key with\n // `refCounts: null` in order to correctly record a delete patch\n // for that row, as the row with the old key will be removed.\n const rowVersion = version ?? existing?.rowVersion;\n if (rowVersion) {\n this._cvrStore.putRowRecord({\n id,\n rowVersion,\n patchVersion,\n refCounts: merged,\n });\n } else {\n // This means that a row that was not in the CVR was added during\n // this update, and then subsequently removed. Since there's no\n // corresponding row in the CVR itself, cancel the previous put.\n // Note that we still send a 'del' patch to the client in order to\n // cancel the previous 'put' patch.\n this._cvrStore.delRowRecord(id);\n }\n\n // Dedupe against the lastPatch sent for the row, and ensure that\n // toVersion never backtracks (lest it be undesirably filtered).\n const lastPatch = this.#lastPatches.get(id);\n const toVersion = maxVersion(patchVersion, lastPatch?.toVersion);\n\n if (merged === null) {\n // All refCounts have gone to zero, if row was previously synced\n // delete it.\n if (existing || previouslyReceived) {\n // dedupe\n if (lastPatch?.rowVersion !== null) {\n patches.push({\n patch: {\n type: 'row',\n op: 'del',\n id,\n },\n toVersion,\n });\n this.#lastPatches.set(id, {rowVersion: null, toVersion});\n }\n }\n } else if (contents) {\n assert(\n rowVersion,\n 'rowVersion is required when contents is present',\n );\n // dedupe\n if (!lastPatch?.rowVersion || lastPatch.rowVersion < rowVersion) {\n patches.push({\n patch: {\n type: 'row',\n op: 'put',\n id,\n contents,\n },\n toVersion,\n });\n this.#lastPatches.set(id, {rowVersion, toVersion});\n }\n }\n }\n return patches;\n },\n );\n }\n\n /**\n * Computes and updates the row records based on:\n * * The {@link #executed} queries\n * * The {@link #removed} queries\n * * The {@link received} rows\n *\n * Returns the final delete and patch ops that must be sent to the client\n * to delete rows that are no longer referenced by any query.\n *\n * This is Step [5] of the\n * [CVR Sync Algorithm](https://www.notion.so/replicache/Sync-and-Client-View-Records-CVR-a18e02ec3ec543449ea22070855ff33d?pvs=4#7874f9b80a514be2b8cd5cf538b88d37).\n */\n deleteUnreferencedRows(lc?: LogContext): Promise<PatchToVersion[]> {\n return startAsyncSpan(\n tracer,\n 'CVRQueryDrivenUpdater.deleteUnreferencedRows',\n async () => {\n if (this.#removedOrExecutedQueryIDs.size === 0) {\n // Query-less update. This can happen for config-only changes.\n assert(\n this.#receivedRows.size === 0,\n () =>\n `Expected no received rows for query-less update, got ${this.#receivedRows.size}`,\n );\n return [];\n }\n\n // patches to send to the client.\n const patches: PatchToVersion[] = [];\n\n const start = Date.now();\n assert(this.#existingRows, `trackQueries() was not called`);\n for (const existing of await this.#existingRows) {\n const deletedID = this.#deleteUnreferencedRow(existing);\n if (deletedID === null) {\n continue;\n }\n const lastPatch = this.#lastPatches.get(deletedID);\n if (lastPatch !== undefined && lastPatch.rowVersion === null) {\n continue;\n }\n const toVersion = maxVersion(this._cvr.version, lastPatch?.toVersion);\n patches.push({\n toVersion,\n patch: {type: 'row', op: 'del', id: deletedID},\n });\n this.#lastPatches.set(deletedID, {rowVersion: null, toVersion});\n }\n lc?.debug?.(\n `computed ${patches.length} delete patches (${Date.now() - start} ms)`,\n );\n\n return patches;\n },\n );\n }\n\n #deleteUnreferencedRow(existing: RowRecord): RowID | null {\n return startSpan(\n tracer,\n 'CVRQueryDrivenUpdater.#deleteUnreferencedRow',\n () => {\n if (this.#receivedRows.get(existing.id)) {\n return null;\n }\n\n const newRefCounts = mergeRefCounts(\n existing.refCounts,\n undefined,\n this.#removedOrExecutedQueryIDs,\n );\n\n // If a row is still referenced, we update the refCounts but not the\n // patchVersion (as the existence and contents of the row have not\n // changed from the clients' perspective). If the row is deleted, it\n // gets a new patchVersion (and corresponding poke).\n const patchVersion = newRefCounts\n ? existing.patchVersion\n : this.#assertNewVersion(\n existing.id,\n existing.rowVersion,\n undefined,\n existing.refCounts ?? {},\n );\n const rowRecord: RowRecord = {\n ...existing,\n patchVersion,\n refCounts: newRefCounts,\n };\n\n this._cvrStore.putRowRecord(rowRecord);\n\n // Return the id to delete if no longer referenced.\n return newRefCounts ? null : existing.id;\n },\n );\n }\n}\n\nfunction mergeRefCounts(\n existing: RefCounts | null | undefined,\n received: RefCounts | null | undefined,\n removeHashes?: Set<string>,\n): RefCounts | null {\n let merged: RefCounts = {};\n if (!existing) {\n merged = received ?? {};\n } else {\n [existing, received].forEach((refCounts, i) => {\n if (!refCounts) {\n return;\n }\n for (const [hash, count] of Object.entries(refCounts)) {\n if (i === 0 /* existing */ && removeHashes?.has(hash)) {\n continue; // removeHashes from existing row.\n }\n merged[hash] = (merged[hash] ?? 0) + count;\n if (merged[hash] === 0) {\n delete merged[hash];\n }\n }\n\n return merged;\n });\n }\n\n return Object.values(merged).some(v => v > 0) ? merged : null;\n}\n\n/**\n * The query must be inactive for all clients to be considered inactive.\n * This is because expiration is defined that way: a query is expired for a client group\n * only if it is expired for all clients in the group.\n *\n * If all clients have inactivated the query, we return\n * the one with the expiration furthest in the future.\n */\nexport function getInactiveQueries(cvr: CVR): {\n hash: string;\n inactivatedAt: TTLClock;\n ttl: number;\n}[] {\n // We no longer support a TTL larger than 10 minutes.\n const inactive: Map<\n string,\n {\n hash: string;\n inactivatedAt: TTLClock;\n ttl: number;\n }\n > = new Map();\n for (const [queryID, query] of Object.entries(cvr.queries)) {\n if (query.type === 'internal') {\n continue;\n }\n for (const clientState of Object.values(query.clientState)) {\n // 1. Take the longest TTL\n // 2. If the query is not inactivated (for any client), do not return it\n const {inactivatedAt, ttl} = clientState;\n const existing = inactive.get(queryID);\n if (inactivatedAt === undefined) {\n if (existing) {\n inactive.delete(queryID);\n }\n break;\n }\n\n const clampedTTL = clampTTL(ttl);\n if (existing) {\n // The stored one might be too large because from a previous version of\n // zero\n const existingTTL = clampTTL(existing.ttl);\n // Use the last eviction time.\n if (\n existingTTL + ttlClockAsNumber(existing.inactivatedAt) <\n ttlClockAsNumber(inactivatedAt) + clampedTTL\n ) {\n existing.ttl = clampedTTL;\n existing.inactivatedAt = inactivatedAt;\n }\n } else {\n inactive.set(queryID, {\n hash: queryID,\n inactivatedAt,\n ttl: clampedTTL,\n });\n }\n }\n }\n\n // First sort all the queries that have TTL. Oldest first.\n return toSorted(inactive.values(), (a, b) => {\n if (a.ttl === b.ttl) {\n return (\n ttlClockAsNumber(a.inactivatedAt) - ttlClockAsNumber(b.inactivatedAt)\n );\n }\n return (\n ttlClockAsNumber(a.inactivatedAt) +\n a.ttl -\n ttlClockAsNumber(b.inactivatedAt) -\n b.ttl\n );\n });\n}\n\nexport type HydrationQueryGroups<T extends QueryRecord = QueryRecord> = {\n required: T[];\n /**\n * Never contains an internal query: those are always required, so callers\n * can rely on `patchVersion` being present on the type.\n */\n optional: Exclude<T, InternalQueryRecord>[];\n};\n\n/**\n * Splits hydration candidates into required and optional queries. Optional\n * queries are ordered from latest to earliest effective expiration; an\n * ownerless query has the lowest priority.\n */\nexport function classifyQueriesForHydration<T extends QueryRecord>(\n queries: Iterable<T>,\n): HydrationQueryGroups<T> {\n type Optional = Exclude<T, InternalQueryRecord>;\n const required: T[] = [];\n const optionalWithExpiration: {\n query: Optional;\n expiration: number | undefined;\n }[] = [];\n\n for (const query of queries) {\n if (\n query.type === 'internal' ||\n Object.values(query.clientState).some(\n state => state.inactivatedAt === undefined,\n )\n ) {\n required.push(query);\n continue;\n }\n\n let expiration: number | undefined;\n for (const {inactivatedAt, ttl} of Object.values(query.clientState)) {\n assert(inactivatedAt !== undefined, 'inactive query has active client');\n const clientExpiration = ttlClockAsNumber(inactivatedAt) + clampTTL(ttl);\n expiration = Math.max(expiration ?? -Infinity, clientExpiration);\n }\n // Narrowed by the `type === 'internal'` branch above.\n optionalWithExpiration.push({query: query as Optional, expiration});\n }\n\n optionalWithExpiration.sort((a, b) => {\n if (a.expiration === undefined) {\n return b.expiration === undefined ? 0 : 1;\n }\n if (b.expiration === undefined) {\n return -1;\n }\n return b.expiration - a.expiration;\n });\n\n return {\n required,\n optional: optionalWithExpiration.map(({query}) => query),\n };\n}\n\nexport function nextEvictionTime(cvr: CVR): TTLClock | undefined {\n let next: number | undefined;\n for (const {inactivatedAt, ttl} of getInactiveQueries(cvr)) {\n const expire = ttlClockAsNumber(inactivatedAt) + ttl;\n if (next === undefined || expire < next) {\n next = expire;\n }\n }\n return next as TTLClock | undefined;\n}\n\nfunction newQueryRecord(\n id: string,\n ast: AST | undefined,\n name: string | undefined,\n args: readonly ReadonlyJSONValue[] | undefined,\n): ClientQueryRecord | CustomQueryRecord {\n if (ast !== undefined) {\n assert(\n name === undefined && args === undefined,\n 'Cannot provide name or args with ast',\n );\n return {\n id,\n type: 'client',\n ast,\n clientState: {},\n } satisfies ClientQueryRecord;\n }\n\n assert(\n name !== undefined && args !== undefined,\n 'Must provide name and args',\n );\n return {\n id,\n type: 'custom',\n name,\n args,\n clientState: {},\n } satisfies CustomQueryRecord;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmFA,IAAM,uBAAuB;AAC7B,IAAM,mCAAmC;AAEzC,SAAS,kBACP,OACoC;CACpC,IAAI,MAAM,SAAS,YAEjB,MAAM,IAAI,MAAM,YAAY,MAAM,GAAG,8BAA8B;AAEvE;AAEA,SAAgB,wBACd,gBACA,eACqB;CACrB,OAAO;EACL,IAAI;EACJ,MAAM;EACN,KAAK;GACH,QAAQ;GACR,OAAO,GAAG,eAAe;GACzB,OAAO;IACL,MAAM;IACN,YAAY,CACV;KACE,MAAM;KACN,MAAM;MACJ,MAAM;MACN,MAAM;KACR;KACA,IAAI;KACJ,OAAO;MACL,MAAM;MACN,OAAO;KACT;IACF,CACF;GACF;GACA,SAAS;IACP,CAAC,iBAAiB,KAAK;IACvB,CAAC,YAAY,KAAK;IAClB,CAAC,cAAc,KAAK;GACtB;EACF;CACF;AACF;;;;;;;;;;AAWA,IAAa,aAAb,MAAwB;CACtB;CACA;CAEA;;;;;CAMA,YACE,UACA,KACA,gBACA;EACA,KAAK,YAAY;EACjB,KAAK,QAAQ;EACb,KAAK,OAAO,gBAAgB,GAAG;EAC/B,KAAK,KAAK,iBAAiB;CAC7B;CAEA,YAAsB,SAAqB;EACzC,OACE,YAAY,KAAK,KAAK,SAAS,OAAO,IAAI,GAC1C,yDACF;EACA,KAAK,KAAK,UAAU;EACpB,OAAO;CACT;;;;;;CAOA,oBAA0C;EACxC,IAAI,YAAY,KAAK,MAAM,SAAS,KAAK,KAAK,OAAO,MAAM,GACzD,KAAK,YAAY,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A,GAAW,kCAAkC;GACtE,MAAM,mBAAmB,iBAAiB,aAAa,IAAI,SAAS,GAAG;GACvE,aAAa,KAAK,IAAI,cAAc,WAAW,gBAAgB;EACjE;EAEA,uBAAuB,KAAK;GAAQ;GAAmB;EAAU,CAAC;CACpE;CAEA,uBAAuB,MAAM,GAAG,MAAM;EACpC,IAAI,EAAE,eAAe,KAAA,GACnB,OAAO,EAAE,eAAe,KAAA,IAAY,IAAI;EAE1C,IAAI,EAAE,eAAe,KAAA,GACnB,OAAO;EAET,OAAO,EAAE,aAAa,EAAE;CAC1B,CAAC;CAED,OAAO;EACL;EACA,UAAU,uBAAuB,KAAK,EAAC,YAAW,KAAK;CACzD;AACF;AAEA,SAAgB,iBAAiB,KAAgC;CAC/D,IAAI;CACJ,KAAK,MAAM,EAAC,eAAe,SAAQ,mBAAmB,GAAG,GAAG;EAC1D,MAAM,SAAS,iBAAiB,aAAa,IAAI;EACjD,IAAI,SAAS,KAAA,KAAa,SAAS,MACjC,OAAO;CAEX;CACA,OAAO;AACT;AAEA,SAAS,eACP,IACA,KACA,MACA,MACuC;CACvC,IAAI,QAAQ,KAAA,GAAW;EACrB,OACE,SAAS,KAAA,KAAa,SAAS,KAAA,GAC/B,sCACF;EACA,OAAO;GACL;GACA,MAAM;GACN;GACA,aAAa,CAAC;EAChB;CACF;CAEA,OACE,SAAS,KAAA,KAAa,SAAS,KAAA,GAC/B,4BACF;CACA,OAAO;EACL;EACA,MAAM;EACN;EACA;EACA,aAAa,CAAC;CAChB;AACF"}
1
+ {"version":3,"file":"cvr.js","names":["#shard","#deleteQueries","#removedOrExecutedQueryIDs","#executedQueryIDs","#receivedRows","#lastPatches","#rowSetSignature","#existingRows","#trackExecuted","#trackRemoved","#lookupRowsForExecutedAndRemovedQueries","#assertNewVersion","#deleteUnreferencedRow"],"sources":["../../../../../../zero-cache/src/services/view-syncer/cvr.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {startAsyncSpan, startSpan} from '../../../../otel/src/span.ts';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {type JSONObject} from '../../../../shared/src/bigint-json.ts';\nimport {CustomKeyMap} from '../../../../shared/src/custom-key-map.ts';\nimport {toSorted} from '../../../../shared/src/iterables.ts';\nimport {\n deepEqual,\n type ReadonlyJSONValue,\n} from '../../../../shared/src/json.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {\n difference,\n intersection,\n union,\n} from '../../../../shared/src/set-utils.ts';\nimport {stringCompare} from '../../../../shared/src/string-compare.ts';\nimport type {AST} from '../../../../zero-protocol/src/ast.ts';\nimport type {ClientSchema} from '../../../../zero-protocol/src/client-schema.ts';\nimport {ErrorOrigin} from '../../../../zero-protocol/src/error-origin.ts';\nimport {ProtocolError} from '../../../../zero-protocol/src/error.ts';\nimport {\n clampTTL,\n compareTTL,\n DEFAULT_TTL_MS,\n} from '../../../../zql/src/query/ttl.ts';\nimport {recordQuery} from '../../server/anonymous-otel-start.ts';\nimport type {LexiVersion} from '../../types/lexi-version.ts';\nimport {rowIDString} from '../../types/row-key.ts';\nimport {upstreamSchema, type ShardID} from '../../types/shards.ts';\nimport type {Patch, PatchToVersion} from './client-handler.ts';\nimport {type CVRFlushStats, type CVRStore} from './cvr-store.ts';\nimport {formatSignature, parseSignature} from './row-set-signature.ts';\nimport {\n cmpVersions,\n maxVersion,\n oneAfter,\n versionString,\n type ClientQueryRecord,\n type ClientRecord,\n type CustomQueryRecord,\n type CVRVersion,\n type InternalQueryRecord,\n type QueryRecord,\n type RowID,\n type RowRecord,\n} from './schema/types.ts';\nimport {tracer} from './tracer.ts';\nimport {ttlClockAsNumber, type TTLClock} from './ttl-clock.ts';\n\nexport type RowUpdate = {\n version?: string; // Undefined for an unref.\n contents?: JSONObject; // Undefined for an unref.\n refCounts: {[hash: string]: number}; // Counts are negative when a row is unrefed.\n};\n\n/** Internally used mutable CVR type. */\nexport type CVR = {\n id: string;\n version: CVRVersion;\n lastActive: number;\n ttlClock: TTLClock;\n replicaVersion: string | null;\n clients: Record<string, ClientRecord>;\n queries: Record<string, QueryRecord>;\n clientSchema: ClientSchema | null;\n profileID: string | null;\n};\n\n/** Exported immutable CVR type. */\n// TODO: Use Immutable<CVR> when the AST is immutable.\nexport type CVRSnapshot = {\n readonly id: string;\n readonly version: CVRVersion;\n readonly lastActive: number;\n readonly ttlClock: TTLClock;\n readonly replicaVersion: string | null;\n readonly clients: Readonly<Record<string, ClientRecord>>;\n readonly queries: Readonly<Record<string, QueryRecord>>;\n readonly clientSchema: ClientSchema | null;\n readonly profileID: string | null;\n};\n\nconst CLIENT_LMID_QUERY_ID = 'lmids';\nconst CLIENT_MUTATION_RESULTS_QUERY_ID = 'mutationResults';\n\nfunction assertNotInternal(\n query: QueryRecord,\n): asserts query is ClientQueryRecord {\n if (query.type === 'internal') {\n // This should never happen for behaving clients, as query ids should be hashes.\n throw new Error(`Query ID ${query.id} is reserved for internal use`);\n }\n}\n\nexport function getMutationResultsQuery(\n upstreamSchema: string,\n clientGroupID: string,\n): InternalQueryRecord {\n return {\n id: CLIENT_MUTATION_RESULTS_QUERY_ID,\n type: 'internal',\n ast: {\n schema: '',\n table: `${upstreamSchema}.mutations`,\n where: {\n type: 'and',\n conditions: [\n {\n type: 'simple',\n left: {\n type: 'column',\n name: 'clientGroupID',\n },\n op: '=',\n right: {\n type: 'literal',\n value: clientGroupID,\n },\n },\n ],\n },\n orderBy: [\n ['clientGroupID', 'asc'],\n ['clientID', 'asc'],\n ['mutationID', 'asc'],\n ],\n },\n };\n}\n\n/**\n * The base CVRUpdater contains logic common to the {@link CVRConfigDrivenUpdater} and\n * {@link CVRQueryDrivenUpdater}. The CVRUpdater class itself is exported for updating\n * the `lastActive` time of the CVR in the absence of any changes to the CVR contents.\n * Although activity is automatically tracked when the CVR contents change, there may be\n * edge cases in which a client actively connects to a CVR that doesn't itself change.\n * Calling `new CVRUpdater(...).flush()` will explicitly update the active index and\n * prevent the CVR from being garbage collected.\n */\nexport class CVRUpdater {\n protected readonly _orig: CVRSnapshot;\n protected readonly _cvr: CVR;\n\n protected readonly _cvrStore: CVRStore;\n\n /**\n * @param cvrStore The CVRStore to use for storage\n * @param cvr The current CVR\n */\n constructor(\n cvrStore: CVRStore,\n cvr: CVRSnapshot,\n replicaVersion: string | null,\n ) {\n this._cvrStore = cvrStore;\n this._orig = cvr;\n this._cvr = structuredClone(cvr) as CVR; // mutable deep copy\n this._cvr.replicaVersion = replicaVersion;\n }\n\n protected _setVersion(version: CVRVersion) {\n assert(\n cmpVersions(this._cvr.version, version) < 0,\n 'Expected new version to be greater than current version',\n );\n this._cvr.version = version;\n return version;\n }\n\n /**\n * Ensures that the new CVR has a higher version than the original.\n * This method is idempotent in that it will always return the same\n * (possibly bumped) version.\n */\n protected _ensureNewVersion(): CVRVersion {\n if (cmpVersions(this._orig.version, this._cvr.version) === 0) {\n this._setVersion(oneAfter(this._cvr.version));\n }\n return this._cvr.version;\n }\n\n async flush(\n lc: LogContext,\n lastConnectTime: number,\n lastActive: number,\n ttlClock: TTLClock,\n ): Promise<{\n cvr: CVRSnapshot;\n flushed: CVRFlushStats | false;\n }> {\n this._cvr.ttlClock = ttlClock;\n this._cvr.lastActive = lastActive;\n const flushed = await this._cvrStore.flush(\n lc,\n this._orig.version,\n this._cvr,\n lastConnectTime,\n );\n if (!flushed) {\n return {cvr: this._orig, flushed: false};\n }\n return {cvr: this._cvr, flushed};\n }\n}\n\n/**\n * A {@link CVRConfigDrivenUpdater} is used for updating a CVR with config-driven\n * changes. Note that this may result in row deletion (e.g. if queries get dropped),\n * but the `stateVersion` of the CVR does not change.\n */\nexport class CVRConfigDrivenUpdater extends CVRUpdater {\n readonly #shard: ShardID;\n\n constructor(cvrStore: CVRStore, cvr: CVRSnapshot, shard: ShardID) {\n super(cvrStore, cvr, cvr.replicaVersion);\n this.#shard = shard;\n }\n\n ensureClient(id: string): ClientRecord {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.ensureClient', () => {\n let client = this._cvr.clients[id];\n if (client) {\n return client;\n }\n // Add the ClientRecord and PutPatch\n client = {id, desiredQueryIDs: []};\n this._cvr.clients[id] = client;\n\n this._ensureNewVersion();\n this._cvrStore.insertClient(client);\n\n if (!this._cvr.queries[CLIENT_LMID_QUERY_ID]) {\n const lmidsQuery: InternalQueryRecord = {\n id: CLIENT_LMID_QUERY_ID,\n ast: {\n schema: '',\n table: `${upstreamSchema(this.#shard)}.clients`,\n where: {\n type: 'simple',\n left: {\n type: 'column',\n name: 'clientGroupID',\n },\n op: '=',\n right: {\n type: 'literal',\n value: this._cvr.id,\n },\n },\n orderBy: [\n ['clientGroupID', 'asc'],\n ['clientID', 'asc'],\n ],\n },\n type: 'internal',\n };\n this._cvr.queries[CLIENT_LMID_QUERY_ID] = lmidsQuery;\n this._cvrStore.putQuery(lmidsQuery);\n }\n if (!this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID]) {\n const mutationResultsQuery: InternalQueryRecord =\n getMutationResultsQuery(upstreamSchema(this.#shard), this._cvr.id);\n this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID] =\n mutationResultsQuery;\n this._cvrStore.putQuery(mutationResultsQuery);\n }\n\n return client;\n });\n }\n\n setClientSchema(lc: LogContext, clientSchema: ClientSchema) {\n startSpan(tracer, 'CVRConfigDrivenUpdater.setClientSchema', () => {\n if (this._cvr.clientSchema === null) {\n this._cvr.clientSchema = clientSchema;\n this._cvrStore.putInstance(this._cvr);\n } else if (!deepEqual(this._cvr.clientSchema, clientSchema)) {\n // This should not be possible with a correct Zero client, as clients\n // of a CVR should all have the same schema (given that the schema hash\n // is part of the idb key). In fact, clients joining an existing group\n // (i.e. non-empty baseCookie) do not send the clientSchema message.\n lc.warn?.(\n `New schema ${JSON.stringify(\n clientSchema,\n )} does not match existing schema ${JSON.stringify(\n this._cvr.clientSchema,\n )}`,\n );\n throw new ProtocolError({\n kind: 'InvalidConnectionRequest',\n message: `Provided schema does not match previous schema`,\n origin: ErrorOrigin.ZeroCache,\n });\n }\n });\n }\n\n setProfileID(lc: LogContext, profileID: string) {\n if (this._cvr.profileID !== profileID) {\n if (\n this._cvr.profileID !== null &&\n !this._cvr.profileID.startsWith('cg')\n ) {\n // We expect profile ID's to change from null or from the back-filled\n // \"cg...\" value. Log a warning otherwise to surface unexpected or\n // pathological conditions.\n lc.warn?.(\n `changing profile ID from ${this._cvr.profileID} to ${profileID}`,\n );\n }\n this._cvr.profileID = profileID;\n this._cvrStore.putInstance(this._cvr);\n }\n }\n\n putDesiredQueries(\n clientID: string,\n queries: Readonly<{\n hash: string;\n ast?: AST | undefined;\n name?: string | undefined;\n args?: readonly ReadonlyJSONValue[] | undefined;\n ttl?: number | undefined;\n }>[],\n ): PatchToVersion[] {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.putDesiredQueries', () => {\n const patches: PatchToVersion[] = [];\n const client = this.ensureClient(clientID);\n const current = new Set(client.desiredQueryIDs);\n\n // Find the new/changed desired queries.\n const needed: Set<string> = new Set();\n\n const recordQueryForTelemetry = (q: (typeof queries)[0]) => {\n const {ast, name, args} = q;\n if (ast) {\n recordQuery('crud');\n } else if (name && args) {\n recordQuery('custom');\n }\n };\n\n for (const q of queries) {\n const {hash, ttl = DEFAULT_TTL_MS} = q;\n const query = this._cvr.queries[hash];\n if (!query) {\n // New query - record for telemetry\n recordQueryForTelemetry(q);\n needed.add(hash);\n continue;\n }\n if (query.type === 'internal') {\n continue;\n }\n\n const oldClientState = query.clientState[clientID];\n // Old query was inactivated or never desired by this client.\n if (!oldClientState || oldClientState.inactivatedAt !== undefined) {\n // Reactivated query - record for telemetry\n recordQueryForTelemetry(q);\n needed.add(hash);\n continue;\n }\n\n if (compareTTL(ttl, oldClientState.ttl) > 0) {\n // TTL update only - don't record for telemetry\n needed.add(hash);\n }\n }\n\n if (needed.size === 0) {\n return patches;\n }\n const newVersion = this._ensureNewVersion();\n client.desiredQueryIDs = toSorted(union(current, needed), stringCompare);\n\n for (const id of needed) {\n const q = must(queries.find(({hash}) => hash === id));\n const {ast, name, args} = q;\n\n const ttl = clampTTL(q.ttl ?? DEFAULT_TTL_MS);\n const query =\n this._cvr.queries[id] ?? newQueryRecord(id, ast, name, args);\n assertNotInternal(query);\n\n const inactivatedAt = undefined;\n\n query.clientState[clientID] = {\n inactivatedAt,\n ttl,\n version: newVersion,\n };\n this._cvr.queries[id] = query;\n patches.push({\n toVersion: newVersion,\n patch: {type: 'query', op: 'put', id, clientID},\n });\n\n this._cvrStore.putQuery(query);\n this._cvrStore.putDesiredQuery(\n newVersion,\n query,\n client,\n false,\n inactivatedAt,\n ttl,\n );\n }\n return patches;\n });\n }\n\n markDesiredQueriesAsInactive(\n clientID: string,\n queryHashes: string[],\n ttlClock: TTLClock,\n ): PatchToVersion[] {\n return this.#deleteQueries(clientID, queryHashes, ttlClock);\n }\n\n deleteDesiredQueries(\n clientID: string,\n queryHashes: string[],\n ): PatchToVersion[] {\n return this.#deleteQueries(clientID, queryHashes, undefined);\n }\n\n #deleteQueries(\n clientID: string,\n queryHashes: string[],\n inactivatedAt: TTLClock | undefined,\n ): PatchToVersion[] {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.#deleteQueries', () => {\n const patches: PatchToVersion[] = [];\n const client = this.ensureClient(clientID);\n const current = new Set(client.desiredQueryIDs);\n const unwanted = new Set(queryHashes);\n const remove = intersection(unwanted, current);\n if (remove.size === 0) {\n return patches;\n }\n\n const newVersion = this._ensureNewVersion();\n client.desiredQueryIDs = toSorted(\n difference(current, remove),\n stringCompare,\n );\n\n for (const id of remove) {\n const query = this._cvr.queries[id];\n if (!query) {\n continue; // Query itself has already been removed. Should not happen?\n }\n assertNotInternal(query);\n\n let ttl = DEFAULT_TTL_MS;\n if (inactivatedAt === undefined) {\n delete query.clientState[clientID];\n } else {\n // client state can be missing if the query never transformed so we never\n // recorded it.\n const clientState = query.clientState[clientID];\n if (clientState !== undefined) {\n assert(\n clientState.inactivatedAt === undefined,\n `Query ${id} is already inactivated`,\n );\n // Clamp TTL to ensure we don't propagate historical unclamped values.\n ttl = clampTTL(clientState.ttl);\n query.clientState[clientID] = {\n inactivatedAt,\n ttl,\n version: newVersion,\n };\n }\n }\n\n this._cvrStore.putQuery(query);\n this._cvrStore.putDesiredQuery(\n newVersion,\n query,\n client,\n true,\n inactivatedAt,\n ttl,\n );\n patches.push({\n toVersion: newVersion,\n patch: {type: 'query', op: 'del', id, clientID},\n });\n }\n return patches;\n });\n }\n\n clearDesiredQueries(clientID: string): PatchToVersion[] {\n const client = this.ensureClient(clientID);\n return this.#deleteQueries(clientID, client.desiredQueryIDs, undefined);\n }\n\n deleteClient(clientID: string, ttlClock: TTLClock): PatchToVersion[] {\n return startSpan(tracer, 'CVRConfigDrivenUpdater.deleteClient', () => {\n // clientID might not be part of this client group but if it is, this delete\n // may generate changes to the desired queries.\n\n const client = this._cvr.clients[clientID];\n if (!client) {\n // Clients in different client groups are no longer deleted, leaving\n // cleanup to inactive CVR purging logic.\n return [];\n }\n\n // When a client is deleted we mark all of its desired queries as inactive.\n // They will then be removed when the queries expire.\n const patches = this.markDesiredQueriesAsInactive(\n clientID,\n client.desiredQueryIDs,\n ttlClock,\n );\n delete this._cvr.clients[clientID];\n this._cvrStore.deleteClient(clientID);\n\n return patches;\n });\n }\n}\n\ntype Hash = string;\nexport type Column = string;\nexport type RefCounts = Record<Hash, number>;\n\ntype RowPatchInfo = {\n rowVersion: string | null; // null for a row-del\n toVersion: CVRVersion; // patchVersion\n};\n\n/**\n * Callback used by {@link CVRQueryDrivenUpdater.flush} to retrieve the\n * current row-set signature maintained by the pipeline driver for a given\n * query. Returning `undefined` means \"no pipeline for this query right now\" —\n * the stored signature on disk is left alone.\n */\nexport type RowSetSignatureProvider = (queryID: string) => bigint | undefined;\n\n/**\n * A {@link CVRQueryDrivenUpdater} is used for updating a CVR after making queries.\n * The caller should invoke:\n *\n * * {@link trackQueries} for queries that are being executed or removed.\n * * {@link received} for all rows received from the executed queries\n * * {@link removeTrackedQueries} for optional queries that did not execute\n * * {@link deleteUnreferencedRows} to remove any rows that have\n * fallen out of the query result view.\n * * {@link flush}\n *\n * After flushing, the caller should perform any necessary catchup of\n * config and row patches for clients that are behind. See\n * {@link CVRStore.catchupConfigPatches} and {@link CVRStore.catchupRowPatches}.\n */\nexport class CVRQueryDrivenUpdater extends CVRUpdater {\n readonly #removedOrExecutedQueryIDs = new Set<string>();\n readonly #executedQueryIDs = new Set<string>();\n readonly #receivedRows = new CustomKeyMap<RowID, RefCounts | null>(\n rowIDString,\n );\n readonly #lastPatches = new CustomKeyMap<RowID, RowPatchInfo>(rowIDString);\n readonly #rowSetSignature: RowSetSignatureProvider | undefined;\n\n #existingRows: Promise<Iterable<RowRecord>> | undefined = undefined;\n\n /**\n * @param stateVersion The `stateVersion` at which the queries were executed.\n * @param rowSetSignature Optional callback that returns the current row-set\n * signature for a query, typically backed by\n * `PipelineDriver.rowSetSignature`. When provided, {@link flush}\n * persists any signature deltas it observes.\n */\n constructor(\n cvrStore: CVRStore,\n cvr: CVRSnapshot,\n stateVersion: LexiVersion,\n replicaVersion: string,\n rowSetSignature?: RowSetSignatureProvider,\n ) {\n super(cvrStore, cvr, replicaVersion);\n this.#rowSetSignature = rowSetSignature;\n\n assert(\n // We should either be setting the cvr.replicaVersion for the first time, or it should\n // be something newer than the current cvr.replicaVersion. Otherwise, the CVR should\n // have been rejected by the ViewSyncer.\n (cvr.replicaVersion ?? replicaVersion) <= replicaVersion,\n `Cannot sync from an older replicaVersion: CVR=${cvr.replicaVersion}, DB=${replicaVersion}`,\n );\n assert(\n stateVersion >= cvr.version.stateVersion,\n () =>\n `stateVersion (${stateVersion}) must be >= cvr.version.stateVersion (${cvr.version.stateVersion})`,\n );\n if (stateVersion > cvr.version.stateVersion) {\n this._setVersion({stateVersion});\n }\n }\n\n /**\n * Initiates the tracking of the specified `executed` and `removed` queries.\n * This kicks of a lookup of existing {@link RowRecord}s currently associated\n * with those queries, which will be used to reconcile the rows to keep\n * after all rows have been {@link received()}.\n *\n * \"transformed\" queries are queries that are currently\n * gotten and running in the pipeline driver but\n * received a new transformation hash due to an auth token\n * update.\n *\n * @returns The new CVRVersion to be used when all changes are committed.\n */\n trackQueries(\n lc: LogContext,\n executed: {id: string; transformationHash: string}[],\n removed: {id: string}[],\n ): {newVersion: CVRVersion; queryPatches: PatchToVersion[]} {\n return startSpan(tracer, 'CVRQueryDrivenUpdater.trackQueries', () => {\n assert(this.#existingRows === undefined, `trackQueries already called`);\n\n const queryPatches: Patch[] = [\n executed.map(q => this.#trackExecuted(q.id, q.transformationHash)),\n removed.map(q => this.#trackRemoved(q.id)),\n ].flat(2);\n\n this.#existingRows = this.#lookupRowsForExecutedAndRemovedQueries(lc);\n // Immediately attach a rejection handler to avoid unhandled rejections.\n // The error will surface when this.#existingRows is awaited.\n void this.#existingRows.then(() => {});\n\n const versionBumped =\n cmpVersions(this._orig.version, this._cvr.version) < 0;\n lc.info?.(\n `trackQueries: ${executed.length} executed, ${removed.length} removed, ` +\n `version ${versionBumped ? 'bumped' : 'unchanged'}`,\n );\n\n return {\n newVersion: this._cvr.version,\n queryPatches: queryPatches.map(patch => ({\n patch,\n toVersion: this._cvr.version,\n })),\n };\n });\n }\n\n #lookupRowsForExecutedAndRemovedQueries(\n lc: LogContext,\n ): Promise<Iterable<RowRecord>> {\n return startAsyncSpan(\n tracer,\n 'CVRQueryDrivenUpdater.#lookupRowsForExecutedAndRemovedQueries',\n async () => {\n const results = new CustomKeyMap<RowID, RowRecord>(rowIDString);\n\n if (this.#removedOrExecutedQueryIDs.size === 0) {\n // Query-less update. This can happen for config only changes.\n return [];\n }\n\n // Utilizes the in-memory RowCache.\n const allRowRecords = (await this._cvrStore.getRowRecords()).values();\n let total = 0;\n for (const existing of allRowRecords) {\n total++;\n assert(\n existing.refCounts !== null,\n 'allRowRecords should not include null refCounts',\n );\n for (const id of Object.keys(existing.refCounts)) {\n if (this.#removedOrExecutedQueryIDs.has(id)) {\n results.set(existing.id, existing);\n break;\n }\n }\n }\n\n lc.debug?.(\n `found ${\n results.size\n } (of ${total}) rows for executed / removed queries ${[\n ...this.#removedOrExecutedQueryIDs,\n ]}`,\n );\n return results.values();\n },\n );\n }\n\n /**\n * Tracks an executed query, ensures that it is marked as \"gotten\",\n * updating the CVR and creating put patches if necessary.\n *\n * This must be called for all executed queries.\n */\n #trackExecuted(queryID: string, transformationHash: string): Patch[] {\n return startSpan(tracer, 'CVRQueryDrivenUpdater.#trackExecuted', () => {\n assert(\n !this.#removedOrExecutedQueryIDs.has(queryID),\n () => `Query ${queryID} already tracked as executed or removed`,\n );\n this.#removedOrExecutedQueryIDs.add(queryID);\n this.#executedQueryIDs.add(queryID);\n\n let gotQueryPatch: Patch | undefined;\n const query = this._cvr.queries[queryID];\n if (query.transformationHash !== transformationHash) {\n const transformationVersion = this._ensureNewVersion();\n\n if (query.type !== 'internal' && query.patchVersion === undefined) {\n // client query: desired -> gotten\n query.patchVersion = transformationVersion;\n gotQueryPatch = {\n type: 'query',\n op: 'put',\n id: query.id,\n };\n }\n\n query.transformationHash = transformationHash;\n query.transformationVersion = transformationVersion;\n this._cvrStore.updateQuery(query);\n }\n return gotQueryPatch ? [gotQueryPatch] : [];\n });\n }\n\n /**\n * Converts queries that were tracked as executed into normal query\n * removals. This is used when a hydration budget expires after\n * `trackQueries()` has established the final poke version but before an\n * optional query starts hydration.\n *\n * The query ID remains in `#removedOrExecutedQueryIDs`, so its row\n * references are dropped by {@link deleteUnreferencedRows} exactly as they\n * would be for a query removed up front. Never un-track a query here: rows\n * already flushed by {@link received} have had its refCounts stripped and\n * cannot be restored.\n */\n removeTrackedQueries(queryIDs: Iterable<string>): PatchToVersion[] {\n assert(this.#existingRows !== undefined, 'trackQueries() was not called');\n assert(\n cmpVersions(this._orig.version, this._cvr.version) < 0,\n 'A final CVR version must be set before removing tracked queries',\n );\n\n const patches: PatchToVersion[] = [];\n for (const queryID of queryIDs) {\n assert(\n this.#executedQueryIDs.has(queryID),\n () => `Query ${queryID} was not tracked as executed`,\n );\n const query = must(this._cvr.queries[queryID]);\n assertNotInternal(query);\n assert(\n Object.values(query.clientState).every(\n ({inactivatedAt}) => inactivatedAt !== undefined,\n ),\n () => `Query ${queryID} is active`,\n );\n assert(\n query.patchVersion !== undefined,\n `Query ${queryID} is not gotten`,\n );\n\n delete this._cvr.queries[queryID];\n const patch = {type: 'query', op: 'del', id: queryID} as const;\n this._cvrStore.markQueryAsDeleted(this._cvr.version, patch);\n patches.push({patch, toVersion: this._cvr.version});\n }\n return patches;\n }\n\n /**\n * Aborts queries that were tracked as executed but whose hydration was cut\n * short (see the view-syncer's hydration circuit breaker).\n *\n * Rows received for an aborted query in this update have that query's\n * references stripped: a row left with no references is deleted, and the\n * 'del' patch cancels the 'put' the client has already received, while a\n * row still referenced by other queries keeps its patch version. The query\n * is then removed from the CVR. It stays tracked as executed, so\n * {@link deleteUnreferencedRows} also drops the references held by rows that\n * were synced for it before this update, exactly as for a removed query.\n *\n * Like {@link removeTrackedQueries}, this must be called after\n * {@link trackQueries} has established the final version, and after all\n * rows of the aborted queries have been {@link received}.\n */\n async abortExecutedQueries(\n lc: LogContext,\n queryIDs: readonly string[],\n ): Promise<PatchToVersion[]> {\n assert(this.#existingRows !== undefined, 'trackQueries() was not called');\n assert(\n cmpVersions(this._orig.version, this._cvr.version) < 0,\n 'A final CVR version must be set before aborting executed queries',\n );\n const aborted = new Set(queryIDs);\n for (const queryID of aborted) {\n assert(\n this.#executedQueryIDs.has(queryID),\n `Query ${queryID} was not tracked as executed`,\n );\n const query = must(this._cvr.queries[queryID]);\n assertNotInternal(query);\n }\n\n // Unref the rows received for the aborted queries. The version of each\n // unref is the version the row currently has in this update, so a row that\n // stays referenced does not get a spurious patch version bump.\n const existingRows = await this._cvrStore.getRowRecords();\n const unrefs = new CustomKeyMap<RowID, RowUpdate>(rowIDString);\n for (const [id, refCounts] of this.#receivedRows) {\n if (refCounts === null) {\n continue;\n }\n let unref: RowUpdate | undefined;\n for (const queryID of aborted) {\n const count = refCounts[queryID];\n if (!count) {\n continue;\n }\n if (!unref) {\n const version =\n this.#lastPatches.get(id)?.rowVersion ??\n existingRows.get(id)?.rowVersion;\n unref = {refCounts: {}, ...(version && {version})};\n unrefs.set(id, unref);\n }\n unref.refCounts[queryID] = -count;\n }\n }\n const patches =\n unrefs.size > 0\n ? await this.received(lc, unrefs)\n : ([] as PatchToVersion[]);\n\n for (const queryID of aborted) {\n delete this._cvr.queries[queryID];\n const patch = {type: 'query', op: 'del', id: queryID} as const;\n this._cvrStore.markQueryAsDeleted(this._cvr.version, patch);\n patches.push({patch, toVersion: this._cvr.version});\n }\n return patches;\n }\n\n /**\n * Tracks a query removed from the \"gotten\" set. In addition to producing the\n * appropriate patches for deleting the query, the removed query is taken into\n * account when computing the final row records in\n * {@link deleteUnreferencedRows}.\n * Namely, any rows with columns that are no longer referenced by a\n * query are deleted.\n *\n * This must only be called on queries that are not \"desired\" by any client.\n */\n #trackRemoved(queryID: string): Patch[] {\n return startSpan(tracer, 'CVRQueryDrivenUpdater.#trackRemoved', () => {\n const query = this._cvr.queries[queryID];\n assertNotInternal(query);\n\n assert(\n !this.#removedOrExecutedQueryIDs.has(queryID),\n () => `Query ${queryID} already tracked as executed or removed`,\n );\n this.#removedOrExecutedQueryIDs.add(queryID);\n delete this._cvr.queries[queryID];\n\n const newVersion = this._ensureNewVersion();\n const queryPatch = {type: 'query', op: 'del', id: queryID} as const;\n this._cvrStore.markQueryAsDeleted(newVersion, queryPatch);\n return [queryPatch];\n });\n }\n\n /**\n * Asserts that a new version has already been set.\n *\n * After {@link #executed} and {@link #removed} are called, we must have properly\n * decided on the final CVR version because the poke-start message declares the\n * final cookie (i.e. version), and that must be sent before any poke parts\n * generated from {@link received} are sent.\n */\n #assertNewVersion(\n rowID: RowID,\n existingVersion: string | undefined,\n newVersion: string | undefined,\n refCounts: RefCounts,\n ): CVRVersion {\n assert(\n cmpVersions(this._orig.version, this._cvr.version) < 0,\n () =>\n `Expected CVR version to have been bumped above original` +\n ` (orig=${versionString(this._orig.version)},` +\n ` curr=${versionString(this._cvr.version)}).` +\n ` Row ${JSON.stringify(rowID)}:` +\n ` existing=${existingVersion},` +\n ` new=${newVersion},` +\n ` queries=[${Object.keys(refCounts).join(',')}]`,\n );\n return this._cvr.version;\n }\n\n updatedVersion(): CVRVersion {\n return this._cvr.version;\n }\n\n /**\n * Public alias for {@link _ensureNewVersion}. Forces a `configVersion` bump\n * when the caller needs a new CVR version for reasons outside the built-in\n * tracking (e.g. rowSetSignature drift re-execution).\n */\n ensureNewVersion(): CVRVersion {\n return this._ensureNewVersion();\n }\n\n override flush(\n lc: LogContext,\n lastConnectTime: number,\n lastActive: number,\n ttlClock: TTLClock,\n ): Promise<{cvr: CVRSnapshot; flushed: CVRFlushStats | false}> {\n if (this.#rowSetSignature) {\n // Persist the per-query row-set signature for any query whose\n // pipeline-driver signature differs from what's on disk. Queries\n // without an active pipeline (provider returns undefined) keep their\n // stored value.\n for (const [queryID, query] of Object.entries(this._cvr.queries)) {\n const sig = this.#rowSetSignature(queryID);\n if (sig === undefined) {\n continue;\n }\n const stored = parseSignature(query.rowSetSignature);\n if (stored === sig) {\n continue;\n }\n const hex = formatSignature(sig);\n query.rowSetSignature = hex;\n this._cvrStore.updateRowSetSignature(queryID, hex);\n }\n }\n return super.flush(lc, lastConnectTime, lastActive, ttlClock);\n }\n\n /**\n * Tracks rows received from executing queries. This will update row records\n * and row patches if the received rows have a new version. The method also\n * returns (put) patches to be returned to update their state, versioned by\n * patchVersion so that only the patches new to the clients are sent.\n */\n received(\n _lc: LogContext,\n rows: Map<RowID, RowUpdate>,\n ): Promise<PatchToVersion[]> {\n return startAsyncSpan(\n tracer,\n 'CVRQueryDrivenUpdater.received',\n async () => {\n const patches: PatchToVersion[] = [];\n const existingRows = await this._cvrStore.getRowRecords();\n\n for (const [id, update] of rows.entries()) {\n const {contents, version, refCounts} = update;\n\n let existing = existingRows.get(id);\n // Accumulate all received refCounts to determine which rows to prune.\n const previouslyReceived = this.#receivedRows.get(id);\n\n const merged =\n previouslyReceived !== undefined\n ? mergeRefCounts(previouslyReceived, refCounts)\n : mergeRefCounts(\n existing?.refCounts,\n refCounts,\n this.#removedOrExecutedQueryIDs,\n );\n\n this.#receivedRows.set(id, merged);\n\n const newRowVersion = merged === null ? undefined : version;\n const patchVersion =\n existing && existing.rowVersion === newRowVersion\n ? existing.patchVersion // existing row is unchanged\n : this.#assertNewVersion(\n id,\n existing?.rowVersion,\n newRowVersion,\n refCounts,\n );\n\n // Note: for determining what to commit to the CVR store, use the\n // `version` of the update even if `merged` is null (i.e. don't\n // use `newRowVersion`). This will be deduped by the cvr-store flush\n // if it is redundant. In rare cases--namely, if the row key has\n // changed--we _do_ want to add row-put for the new row key with\n // `refCounts: null` in order to correctly record a delete patch\n // for that row, as the row with the old key will be removed.\n const rowVersion = version ?? existing?.rowVersion;\n if (rowVersion) {\n this._cvrStore.putRowRecord({\n id,\n rowVersion,\n patchVersion,\n refCounts: merged,\n });\n } else {\n // This means that a row that was not in the CVR was added during\n // this update, and then subsequently removed. Since there's no\n // corresponding row in the CVR itself, cancel the previous put.\n // Note that we still send a 'del' patch to the client in order to\n // cancel the previous 'put' patch.\n this._cvrStore.delRowRecord(id);\n }\n\n // Dedupe against the lastPatch sent for the row, and ensure that\n // toVersion never backtracks (lest it be undesirably filtered).\n const lastPatch = this.#lastPatches.get(id);\n const toVersion = maxVersion(patchVersion, lastPatch?.toVersion);\n\n if (merged === null) {\n // All refCounts have gone to zero, if row was previously synced\n // delete it.\n if (existing || previouslyReceived) {\n // dedupe\n if (lastPatch?.rowVersion !== null) {\n patches.push({\n patch: {\n type: 'row',\n op: 'del',\n id,\n },\n toVersion,\n });\n this.#lastPatches.set(id, {rowVersion: null, toVersion});\n }\n }\n } else if (contents) {\n assert(\n rowVersion,\n 'rowVersion is required when contents is present',\n );\n // dedupe\n if (!lastPatch?.rowVersion || lastPatch.rowVersion < rowVersion) {\n patches.push({\n patch: {\n type: 'row',\n op: 'put',\n id,\n contents,\n },\n toVersion,\n });\n this.#lastPatches.set(id, {rowVersion, toVersion});\n }\n }\n }\n return patches;\n },\n );\n }\n\n /**\n * Computes and updates the row records based on:\n * * The {@link #executed} queries\n * * The {@link #removed} queries\n * * The {@link received} rows\n *\n * Returns the final delete and patch ops that must be sent to the client\n * to delete rows that are no longer referenced by any query.\n *\n * This is Step [5] of the\n * [CVR Sync Algorithm](https://www.notion.so/replicache/Sync-and-Client-View-Records-CVR-a18e02ec3ec543449ea22070855ff33d?pvs=4#7874f9b80a514be2b8cd5cf538b88d37).\n */\n deleteUnreferencedRows(lc?: LogContext): Promise<PatchToVersion[]> {\n return startAsyncSpan(\n tracer,\n 'CVRQueryDrivenUpdater.deleteUnreferencedRows',\n async () => {\n if (this.#removedOrExecutedQueryIDs.size === 0) {\n // Query-less update. This can happen for config-only changes.\n assert(\n this.#receivedRows.size === 0,\n () =>\n `Expected no received rows for query-less update, got ${this.#receivedRows.size}`,\n );\n return [];\n }\n\n // patches to send to the client.\n const patches: PatchToVersion[] = [];\n\n const start = Date.now();\n assert(this.#existingRows, `trackQueries() was not called`);\n for (const existing of await this.#existingRows) {\n const deletedID = this.#deleteUnreferencedRow(existing);\n if (deletedID === null) {\n continue;\n }\n const lastPatch = this.#lastPatches.get(deletedID);\n if (lastPatch !== undefined && lastPatch.rowVersion === null) {\n continue;\n }\n const toVersion = maxVersion(this._cvr.version, lastPatch?.toVersion);\n patches.push({\n toVersion,\n patch: {type: 'row', op: 'del', id: deletedID},\n });\n this.#lastPatches.set(deletedID, {rowVersion: null, toVersion});\n }\n lc?.debug?.(\n `computed ${patches.length} delete patches (${Date.now() - start} ms)`,\n );\n\n return patches;\n },\n );\n }\n\n #deleteUnreferencedRow(existing: RowRecord): RowID | null {\n return startSpan(\n tracer,\n 'CVRQueryDrivenUpdater.#deleteUnreferencedRow',\n () => {\n if (this.#receivedRows.get(existing.id)) {\n return null;\n }\n\n const newRefCounts = mergeRefCounts(\n existing.refCounts,\n undefined,\n this.#removedOrExecutedQueryIDs,\n );\n\n // If a row is still referenced, we update the refCounts but not the\n // patchVersion (as the existence and contents of the row have not\n // changed from the clients' perspective). If the row is deleted, it\n // gets a new patchVersion (and corresponding poke).\n const patchVersion = newRefCounts\n ? existing.patchVersion\n : this.#assertNewVersion(\n existing.id,\n existing.rowVersion,\n undefined,\n existing.refCounts ?? {},\n );\n const rowRecord: RowRecord = {\n ...existing,\n patchVersion,\n refCounts: newRefCounts,\n };\n\n this._cvrStore.putRowRecord(rowRecord);\n\n // Return the id to delete if no longer referenced.\n return newRefCounts ? null : existing.id;\n },\n );\n }\n}\n\nfunction mergeRefCounts(\n existing: RefCounts | null | undefined,\n received: RefCounts | null | undefined,\n removeHashes?: Set<string>,\n): RefCounts | null {\n let merged: RefCounts = {};\n if (!existing) {\n merged = received ?? {};\n } else {\n [existing, received].forEach((refCounts, i) => {\n if (!refCounts) {\n return;\n }\n for (const [hash, count] of Object.entries(refCounts)) {\n if (i === 0 /* existing */ && removeHashes?.has(hash)) {\n continue; // removeHashes from existing row.\n }\n merged[hash] = (merged[hash] ?? 0) + count;\n if (merged[hash] === 0) {\n delete merged[hash];\n }\n }\n\n return merged;\n });\n }\n\n return Object.values(merged).some(v => v > 0) ? merged : null;\n}\n\n/**\n * The query must be inactive for all clients to be considered inactive.\n * This is because expiration is defined that way: a query is expired for a client group\n * only if it is expired for all clients in the group.\n *\n * If all clients have inactivated the query, we return\n * the one with the expiration furthest in the future.\n */\nexport function getInactiveQueries(cvr: CVR): {\n hash: string;\n inactivatedAt: TTLClock;\n ttl: number;\n}[] {\n // We no longer support a TTL larger than 10 minutes.\n const inactive: Map<\n string,\n {\n hash: string;\n inactivatedAt: TTLClock;\n ttl: number;\n }\n > = new Map();\n for (const [queryID, query] of Object.entries(cvr.queries)) {\n if (query.type === 'internal') {\n continue;\n }\n for (const clientState of Object.values(query.clientState)) {\n // 1. Take the longest TTL\n // 2. If the query is not inactivated (for any client), do not return it\n const {inactivatedAt, ttl} = clientState;\n const existing = inactive.get(queryID);\n if (inactivatedAt === undefined) {\n if (existing) {\n inactive.delete(queryID);\n }\n break;\n }\n\n const clampedTTL = clampTTL(ttl);\n if (existing) {\n // The stored one might be too large because from a previous version of\n // zero\n const existingTTL = clampTTL(existing.ttl);\n // Use the last eviction time.\n if (\n existingTTL + ttlClockAsNumber(existing.inactivatedAt) <\n ttlClockAsNumber(inactivatedAt) + clampedTTL\n ) {\n existing.ttl = clampedTTL;\n existing.inactivatedAt = inactivatedAt;\n }\n } else {\n inactive.set(queryID, {\n hash: queryID,\n inactivatedAt,\n ttl: clampedTTL,\n });\n }\n }\n }\n\n // First sort all the queries that have TTL. Oldest first.\n return toSorted(inactive.values(), (a, b) => {\n if (a.ttl === b.ttl) {\n return (\n ttlClockAsNumber(a.inactivatedAt) - ttlClockAsNumber(b.inactivatedAt)\n );\n }\n return (\n ttlClockAsNumber(a.inactivatedAt) +\n a.ttl -\n ttlClockAsNumber(b.inactivatedAt) -\n b.ttl\n );\n });\n}\n\nexport type HydrationQueryGroups<T extends QueryRecord = QueryRecord> = {\n required: T[];\n /**\n * Never contains an internal query: those are always required, so callers\n * can rely on `patchVersion` being present on the type.\n */\n optional: Exclude<T, InternalQueryRecord>[];\n};\n\n/**\n * Splits hydration candidates into required and optional queries. Optional\n * queries are ordered from latest to earliest effective expiration; an\n * ownerless query has the lowest priority.\n */\nexport function classifyQueriesForHydration<T extends QueryRecord>(\n queries: Iterable<T>,\n): HydrationQueryGroups<T> {\n type Optional = Exclude<T, InternalQueryRecord>;\n const required: T[] = [];\n const optionalWithExpiration: {\n query: Optional;\n expiration: number | undefined;\n }[] = [];\n\n for (const query of queries) {\n if (\n query.type === 'internal' ||\n Object.values(query.clientState).some(\n state => state.inactivatedAt === undefined,\n )\n ) {\n required.push(query);\n continue;\n }\n\n let expiration: number | undefined;\n for (const {inactivatedAt, ttl} of Object.values(query.clientState)) {\n assert(inactivatedAt !== undefined, 'inactive query has active client');\n const clientExpiration = ttlClockAsNumber(inactivatedAt) + clampTTL(ttl);\n expiration = Math.max(expiration ?? -Infinity, clientExpiration);\n }\n // Narrowed by the `type === 'internal'` branch above.\n optionalWithExpiration.push({query: query as Optional, expiration});\n }\n\n optionalWithExpiration.sort((a, b) => {\n if (a.expiration === undefined) {\n return b.expiration === undefined ? 0 : 1;\n }\n if (b.expiration === undefined) {\n return -1;\n }\n return b.expiration - a.expiration;\n });\n\n return {\n required,\n optional: optionalWithExpiration.map(({query}) => query),\n };\n}\n\nexport function nextEvictionTime(cvr: CVR): TTLClock | undefined {\n let next: number | undefined;\n for (const {inactivatedAt, ttl} of getInactiveQueries(cvr)) {\n const expire = ttlClockAsNumber(inactivatedAt) + ttl;\n if (next === undefined || expire < next) {\n next = expire;\n }\n }\n return next as TTLClock | undefined;\n}\n\nfunction newQueryRecord(\n id: string,\n ast: AST | undefined,\n name: string | undefined,\n args: readonly ReadonlyJSONValue[] | undefined,\n): ClientQueryRecord | CustomQueryRecord {\n if (ast !== undefined) {\n assert(\n name === undefined && args === undefined,\n 'Cannot provide name or args with ast',\n );\n return {\n id,\n type: 'client',\n ast,\n clientState: {},\n } satisfies ClientQueryRecord;\n }\n\n assert(\n name !== undefined && args !== undefined,\n 'Must provide name and args',\n );\n return {\n id,\n type: 'custom',\n name,\n args,\n clientState: {},\n } satisfies CustomQueryRecord;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmFA,IAAM,uBAAuB;AAC7B,IAAM,mCAAmC;AAEzC,SAAS,kBACP,OACoC;CACpC,IAAI,MAAM,SAAS,YAEjB,MAAM,IAAI,MAAM,YAAY,MAAM,GAAG,8BAA8B;AAEvE;AAEA,SAAgB,wBACd,gBACA,eACqB;CACrB,OAAO;EACL,IAAI;EACJ,MAAM;EACN,KAAK;GACH,QAAQ;GACR,OAAO,GAAG,eAAe;GACzB,OAAO;IACL,MAAM;IACN,YAAY,CACV;KACE,MAAM;KACN,MAAM;MACJ,MAAM;MACN,MAAM;KACR;KACA,IAAI;KACJ,OAAO;MACL,MAAM;MACN,OAAO;KACT;I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GAC/B,sCACF;EACA,OAAO;GACL;GACA,MAAM;GACN;GACA,aAAa,CAAC;EAChB;CACF;CAEA,OACE,SAAS,KAAA,KAAa,SAAS,KAAA,GAC/B,4BACF;CACA,OAAO;EACL;EACA,MAAM;EACN;EACA;EACA,aAAa,CAAC;CAChB;AACF"}
@@ -0,0 +1,51 @@
1
+ import type { MonotonicClock } from './hydration-budget.ts';
2
+ /**
3
+ * How long a tripped query stays rejected before a retry is allowed.
4
+ *
5
+ * A query that blows the timeout is almost always slow because of the shape
6
+ * of its data rather than because of transient load, so retrying it on every
7
+ * reconnect would burn a full timeout each time. The cooldown keeps that cost
8
+ * to at most one timeout per cooldown period per query.
9
+ */
10
+ export declare const DEFAULT_CIRCUIT_BREAKER_OPEN_MS: number;
11
+ /**
12
+ * A per-query circuit breaker for view-syncer hydration.
13
+ *
14
+ * Hydration of a single query is aborted once its processing time exceeds
15
+ * {@link timeoutMs}. Callers check {@link exceeded} at the yield points of a
16
+ * hydration, so a query is only ever aborted between time slices.
17
+ *
18
+ * Once a query has been aborted the breaker is "open" for its transformation
19
+ * hash: {@link isOpen} reports true for {@link openMs}, and callers reject the
20
+ * query up front instead of hydrating it again. After that the breaker is
21
+ * implicitly half-open: the next hydration attempt runs, and either succeeds
22
+ * or trips the breaker again.
23
+ *
24
+ * The breaker is keyed by transformation hash rather than query hash because
25
+ * the transformation (permissions, auth context) determines the pipeline that
26
+ * runs, and a different transformation of the same query may well be fast.
27
+ */
28
+ export declare class HydrationCircuitBreaker {
29
+ #private;
30
+ readonly timeoutMs: number;
31
+ readonly openMs: number;
32
+ constructor(timeoutMs: number, openMs?: number, now?: MonotonicClock);
33
+ /** Whether the breaker does anything at all. */
34
+ get enabled(): boolean;
35
+ /**
36
+ * Whether a hydration that has consumed `elapsedMs` of processing time must
37
+ * be aborted. Always false when the breaker is disabled.
38
+ */
39
+ exceeded(elapsedMs: number): boolean;
40
+ /**
41
+ * Records that hydrating `transformationHash` was aborted.
42
+ *
43
+ * Trips are rare, so this is also when expired entries are swept, which
44
+ * keeps the map bounded by the number of hashes tripped within one cooldown
45
+ * rather than by every hash ever tripped.
46
+ */
47
+ trip(transformationHash: string): void;
48
+ /** Whether `transformationHash` is currently rejected. */
49
+ isOpen(transformationHash: string): boolean;
50
+ }
51
+ //# sourceMappingURL=hydration-circuit-breaker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hydration-circuit-breaker.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/hydration-circuit-breaker.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,uBAAuB,CAAC;AAE1D;;;;;;;GAOG;AACH,eAAO,MAAM,+BAA+B,QAAa,CAAC;AAE1D;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,uBAAuB;;IAClC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAIxB,YACE,SAAS,EAAE,MAAM,EACjB,MAAM,GAAE,MAAwC,EAChD,GAAG,GAAE,cAAkD,EAUxD;IAED,gDAAgD;IAChD,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAEnC;IAED;;;;;;OAMG;IACH,IAAI,CAAC,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAQrC;IAED,0DAA0D;IAC1D,MAAM,CAAC,kBAAkB,EAAE,MAAM,GAAG,OAAO,CAU1C;CACF"}
@@ -0,0 +1,78 @@
1
+ import { assert } from "../../../../shared/src/asserts.js";
2
+ //#region ../zero-cache/src/services/view-syncer/hydration-circuit-breaker.ts
3
+ /**
4
+ * How long a tripped query stays rejected before a retry is allowed.
5
+ *
6
+ * A query that blows the timeout is almost always slow because of the shape
7
+ * of its data rather than because of transient load, so retrying it on every
8
+ * reconnect would burn a full timeout each time. The cooldown keeps that cost
9
+ * to at most one timeout per cooldown period per query.
10
+ */
11
+ var DEFAULT_CIRCUIT_BREAKER_OPEN_MS = 5 * 6e4;
12
+ /**
13
+ * A per-query circuit breaker for view-syncer hydration.
14
+ *
15
+ * Hydration of a single query is aborted once its processing time exceeds
16
+ * {@link timeoutMs}. Callers check {@link exceeded} at the yield points of a
17
+ * hydration, so a query is only ever aborted between time slices.
18
+ *
19
+ * Once a query has been aborted the breaker is "open" for its transformation
20
+ * hash: {@link isOpen} reports true for {@link openMs}, and callers reject the
21
+ * query up front instead of hydrating it again. After that the breaker is
22
+ * implicitly half-open: the next hydration attempt runs, and either succeeds
23
+ * or trips the breaker again.
24
+ *
25
+ * The breaker is keyed by transformation hash rather than query hash because
26
+ * the transformation (permissions, auth context) determines the pipeline that
27
+ * runs, and a different transformation of the same query may well be fast.
28
+ */
29
+ var HydrationCircuitBreaker = class {
30
+ timeoutMs;
31
+ openMs;
32
+ #now;
33
+ #openedAt = /* @__PURE__ */ new Map();
34
+ constructor(timeoutMs, openMs = DEFAULT_CIRCUIT_BREAKER_OPEN_MS, now = performance.now.bind(performance)) {
35
+ assert(Number.isSafeInteger(timeoutMs) && timeoutMs >= 0, "Hydration timeout must be a nonnegative integer");
36
+ assert(openMs >= 0, "Circuit breaker open duration must be nonnegative");
37
+ this.timeoutMs = timeoutMs;
38
+ this.openMs = openMs;
39
+ this.#now = now;
40
+ }
41
+ /** Whether the breaker does anything at all. */
42
+ get enabled() {
43
+ return this.timeoutMs > 0;
44
+ }
45
+ /**
46
+ * Whether a hydration that has consumed `elapsedMs` of processing time must
47
+ * be aborted. Always false when the breaker is disabled.
48
+ */
49
+ exceeded(elapsedMs) {
50
+ return this.enabled && elapsedMs >= this.timeoutMs;
51
+ }
52
+ /**
53
+ * Records that hydrating `transformationHash` was aborted.
54
+ *
55
+ * Trips are rare, so this is also when expired entries are swept, which
56
+ * keeps the map bounded by the number of hashes tripped within one cooldown
57
+ * rather than by every hash ever tripped.
58
+ */
59
+ trip(transformationHash) {
60
+ const now = this.#now();
61
+ for (const [hash, openedAt] of this.#openedAt) if (now - openedAt >= this.openMs) this.#openedAt.delete(hash);
62
+ this.#openedAt.set(transformationHash, now);
63
+ }
64
+ /** Whether `transformationHash` is currently rejected. */
65
+ isOpen(transformationHash) {
66
+ const openedAt = this.#openedAt.get(transformationHash);
67
+ if (openedAt === void 0) return false;
68
+ if (this.#now() - openedAt >= this.openMs) {
69
+ this.#openedAt.delete(transformationHash);
70
+ return false;
71
+ }
72
+ return true;
73
+ }
74
+ };
75
+ //#endregion
76
+ export { HydrationCircuitBreaker };
77
+
78
+ //# sourceMappingURL=hydration-circuit-breaker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hydration-circuit-breaker.js","names":["#now","#openedAt"],"sources":["../../../../../../zero-cache/src/services/view-syncer/hydration-circuit-breaker.ts"],"sourcesContent":["import {assert} from '../../../../shared/src/asserts.ts';\nimport type {MonotonicClock} from './hydration-budget.ts';\n\n/**\n * How long a tripped query stays rejected before a retry is allowed.\n *\n * A query that blows the timeout is almost always slow because of the shape\n * of its data rather than because of transient load, so retrying it on every\n * reconnect would burn a full timeout each time. The cooldown keeps that cost\n * to at most one timeout per cooldown period per query.\n */\nexport const DEFAULT_CIRCUIT_BREAKER_OPEN_MS = 5 * 60_000;\n\n/**\n * A per-query circuit breaker for view-syncer hydration.\n *\n * Hydration of a single query is aborted once its processing time exceeds\n * {@link timeoutMs}. Callers check {@link exceeded} at the yield points of a\n * hydration, so a query is only ever aborted between time slices.\n *\n * Once a query has been aborted the breaker is \"open\" for its transformation\n * hash: {@link isOpen} reports true for {@link openMs}, and callers reject the\n * query up front instead of hydrating it again. After that the breaker is\n * implicitly half-open: the next hydration attempt runs, and either succeeds\n * or trips the breaker again.\n *\n * The breaker is keyed by transformation hash rather than query hash because\n * the transformation (permissions, auth context) determines the pipeline that\n * runs, and a different transformation of the same query may well be fast.\n */\nexport class HydrationCircuitBreaker {\n readonly timeoutMs: number;\n readonly openMs: number;\n readonly #now: MonotonicClock;\n readonly #openedAt = new Map<string, number>();\n\n constructor(\n timeoutMs: number,\n openMs: number = DEFAULT_CIRCUIT_BREAKER_OPEN_MS,\n now: MonotonicClock = performance.now.bind(performance),\n ) {\n assert(\n Number.isSafeInteger(timeoutMs) && timeoutMs >= 0,\n 'Hydration timeout must be a nonnegative integer',\n );\n assert(openMs >= 0, 'Circuit breaker open duration must be nonnegative');\n this.timeoutMs = timeoutMs;\n this.openMs = openMs;\n this.#now = now;\n }\n\n /** Whether the breaker does anything at all. */\n get enabled(): boolean {\n return this.timeoutMs > 0;\n }\n\n /**\n * Whether a hydration that has consumed `elapsedMs` of processing time must\n * be aborted. Always false when the breaker is disabled.\n */\n exceeded(elapsedMs: number): boolean {\n return this.enabled && elapsedMs >= this.timeoutMs;\n }\n\n /**\n * Records that hydrating `transformationHash` was aborted.\n *\n * Trips are rare, so this is also when expired entries are swept, which\n * keeps the map bounded by the number of hashes tripped within one cooldown\n * rather than by every hash ever tripped.\n */\n trip(transformationHash: string): void {\n const now = this.#now();\n for (const [hash, openedAt] of this.#openedAt) {\n if (now - openedAt >= this.openMs) {\n this.#openedAt.delete(hash);\n }\n }\n this.#openedAt.set(transformationHash, now);\n }\n\n /** Whether `transformationHash` is currently rejected. */\n isOpen(transformationHash: string): boolean {\n const openedAt = this.#openedAt.get(transformationHash);\n if (openedAt === undefined) {\n return false;\n }\n if (this.#now() - openedAt >= this.openMs) {\n this.#openedAt.delete(transformationHash);\n return false;\n }\n return true;\n }\n}\n"],"mappings":";;;;;;;;;;AAWA,IAAa,kCAAkC,IAAI;;;;;;;;;;;;;;;;;;AAmBnD,IAAa,0BAAb,MAAqC;CACnC;CACA;CACA;CACA,4BAAqB,IAAI,IAAoB;CAE7C,YACE,WACA,SAAiB,iCACjB,MAAsB,YAAY,IAAI,KAAK,WAAW,GACtD;EACA,OACE,OAAO,cAAc,SAAS,KAAK,aAAa,GAChD,iDACF;EACA,OAAO,UAAU,GAAG,mDAAmD;EACvE,KAAK,YAAY;EACjB,KAAK,SAAS;EACd,KAAKA,OAAO;CACd;;CAGA,IAAI,UAAmB;EACrB,OAAO,KAAK,YAAY;CAC1B;;;;;CAMA,SAAS,WAA4B;EACnC,OAAO,KAAK,WAAW,aAAa,KAAK;CAC3C;;;;;;;;CASA,KAAK,oBAAkC;EACrC,MAAM,MAAM,KAAKA,KAAK;EACtB,KAAK,MAAM,CAAC,MAAM,aAAa,KAAKC,WAClC,IAAI,MAAM,YAAY,KAAK,QACzB,KAAKA,UAAU,OAAO,IAAI;EAG9B,KAAKA,UAAU,IAAI,oBAAoB,GAAG;CAC5C;;CAGA,OAAO,oBAAqC;EAC1C,MAAM,WAAW,KAAKA,UAAU,IAAI,kBAAkB;EACtD,IAAI,aAAa,KAAA,GACf,OAAO;EAET,IAAI,KAAKD,KAAK,IAAI,YAAY,KAAK,QAAQ;GACzC,KAAKC,UAAU,OAAO,kBAAkB;GACxC,OAAO;EACT;EACA,OAAO;CACT;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"pipeline-driver.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/pipeline-driver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAKjD,OAAO,KAAK,EAAC,GAAG,EAAe,MAAM,sCAAsC,CAAC;AAC5E,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,gDAAgD,CAAC;AACjF,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,uCAAuC,CAAC;AAC/D,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,8CAA8C,CAAC;AAO7E,OAAO,EAAC,UAAU,EAAC,MAAM,wCAAwC,CAAC;AAGlE,OAAO,EAIL,KAAK,KAAK,EAGX,MAAM,qCAAqC,CAAC;AAY7C,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,4CAA4C,CAAC;AASnF,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,E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ge() {},
245
245
  decorateFilterInput: (input) => input
246
246
  }, "scalar-subquery");
247
+ companionInputs.push(input);
247
248
  let node;
248
249
  for (const n of skipYields(input.fetch({}))) node ??= n;
249
- if (!node) {
250
- companionInputs.push(input);
251
- return;
252
- }
250
+ if (!node) return;
253
251
  companionRows.push({
254
252
  table: subqueryAST.table,
255
253
  row: node.row
256
254
  });
257
- companionInputs.push(input);
258
255
  return node.row[childField] ?? null;
259
256
  };
260
- const { ast: resolved, companions, ignoredScalarHints } = resolveSimpleScalarSubqueries(ast, this.#tableSpecs, executor);
257
+ let resolved;
258
+ let companions;
259
+ let ignoredScalarHints;
260
+ try {
261
+ ({ast: resolved, companions, ignoredScalarHints} = resolveSimpleScalarSubqueries(ast, this.#tableSpecs, executor));
262
+ } catch (e) {
263
+ for (const input of companionInputs) input.destroy();
264
+ throw e;
265
+ }
261
266
  return {
262
267
  ast: resolved,
263
268
  companionRows,
@@ -303,8 +308,10 @@ var PipelineDriver = class {
303
308
  let hydrationFinished = false;
304
309
  let hydrationFailed = false;
305
310
  let hydrationRowCount = 0;
311
+ let builtInputs = [];
306
312
  try {
307
313
  const { ast: resolvedQuery, companionRows, companions: companionMeta, companionInputs, ignoredScalarHints } = this.#resolveScalarSubqueries(query);
314
+ builtInputs = [...companionInputs];
308
315
  this.#warnIgnoredScalarHints(queryID, ignoredScalarHints);
309
316
  const input = buildPipeline(resolvedQuery, {
310
317
  debug: debugDelegate,
@@ -316,6 +323,7 @@ var PipelineDriver = class {
316
323
  addEdge() {},
317
324
  decorateFilterInput: (input) => input
318
325
  }, queryID, costModel);
326
+ builtInputs.push(input);
319
327
  const schema = input.getSchema();
320
328
  input.setOutput({ push: (change) => {
321
329
  const streamer = this.#streamer;
@@ -435,6 +443,10 @@ var PipelineDriver = class {
435
443
  hydrationTimeMs: timer.totalElapsed(),
436
444
  hydrationRowCount
437
445
  });
446
+ if (!hydrationFinished) {
447
+ for (const input of builtInputs) input.destroy();
448
+ this.#rowSetSignatures.delete(queryID);
449
+ }
438
450
  this.#hydrateContext = null;
439
451
  }
440
452
  }