@bastani/atomic 0.10.18-alpha.1 → 0.10.18
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.
- package/CHANGELOG.md +41 -0
- package/dist/_shared/dist-KOELCESV.js +2 -2
- package/dist/_shared/fork-row-index-UAUAQRD5.js +2 -2
- package/dist/_shared/lib-4PES5P4V.js +2 -2
- package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js +2 -2
- package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +2 -2
- package/dist/_shared/mode-shared-2QJSCUM6.js +2 -2
- package/dist/_shared/p1a-prewarm-XIU57JHF.js +3 -3
- package/dist/_shared/sdk-OHKMDJ74.js +9 -10
- package/dist/_shared/sdk-OHKMDJ74.js.map +1 -1
- package/dist/_shared/session-data-AHTU7IS6.js +2 -2
- package/dist/_shared/startup-0-0-OV6RAEAP.js +2 -2
- package/dist/_shared/startup-0-1-EZAJI6RE.js +2 -2
- package/dist/_shared/startup-0-2-YZZOC22D.js +2 -2
- package/dist/_shared/startup-0-3-DE2NCS6A.js +2 -2
- package/dist/_shared/startup-0-4-ZMJV547D.js +4 -3
- package/dist/_shared/startup-0-4-ZMJV547D.js.map +1 -1
- package/dist/_shared/startup-0-5-HZX3ZPTP.js +3 -5
- package/dist/_shared/startup-0-5-HZX3ZPTP.js.map +1 -1
- package/dist/_shared/startup-10-0-TDQNBDDL.js +2 -2
- package/dist/_shared/startup-15-1-4S7FLMNG.js +2 -2
- package/dist/_shared/startup-8-0-34CBHPZH.js +2 -2
- package/dist/_shared/startup-8-1-LZFXSD3Z.js +2 -2
- package/dist/_shared/storage-command-7MGXLJGZ.js +72 -8
- package/dist/_shared/storage-command-7MGXLJGZ.js.map +1 -1
- package/dist/_shared/storage-status-K7PX2RXA.js +2 -2
- package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +2 -2
- package/dist/_shared/tree-outline-MUEPK5B7.js +2 -2
- package/dist/builtin/intercom/broker/broker.bundle.mjs +158 -78
- package/dist/builtin/intercom/broker/multinode-router.ts +41 -8
- package/dist/builtin/intercom/broker/multinode-state-store.ts +6 -1
- package/dist/builtin/intercom/broker/sql-lease-store.ts +89 -80
- package/dist/builtin/intercom/broker/sql-pending-ask-store.ts +65 -6
- package/dist/builtin/intercom/index.bundle.mjs +1 -1
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/index.bundle.mjs +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/src/extension/index.bundle.mjs +1 -1
- package/dist/builtin/web-access/index.bundle.mjs +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/cli/auth-cli.impl.js +3 -3
- package/dist/cli/auth-cli.js +3 -3
- package/dist/cli/blueprint-command.impl.js +3 -3
- package/dist/cli/blueprint-command.js +3 -3
- package/dist/cli/intercom-command.code.js +2 -2
- package/dist/cli/intercom-command.impl.js +3 -3
- package/dist/cli/intercom-command.js +3 -3
- package/dist/cli/list-models.impl.js +3 -3
- package/dist/cli/list-models.js +3 -3
- package/dist/cli/session-picker.impl.js +3 -3
- package/dist/cli/session-picker.js +3 -3
- package/dist/cli/startup-ui.impl.js +3 -3
- package/dist/cli/startup-ui.js +3 -3
- package/dist/cli/storage-command.impl.js +3 -3
- package/dist/cli/storage-command.js +3 -3
- package/dist/cli/storage-search-session.impl.js +3 -3
- package/dist/cli/storage-search-session.js +3 -3
- package/dist/cli/team-command.impl.js +3 -3
- package/dist/cli/team-command.js +3 -3
- package/dist/cli.js +2 -2
- package/dist/core/debug.impl.js +3 -3
- package/dist/core/debug.js +3 -3
- package/dist/core/export-html/card-projection.impl.js +3 -3
- package/dist/core/export-html/card-projection.js +3 -3
- package/dist/core/export-html/index.code.js +2 -2
- package/dist/core/export-html/index.impl.js +3 -3
- package/dist/core/export-html/index.js +3 -3
- package/dist/core/export-html/json-spans.impl.js +3 -3
- package/dist/core/export-html/json-spans.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.impl.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.js +3 -3
- package/dist/core/export-html/tool-renderer.impl.js +3 -3
- package/dist/core/export-html/tool-renderer.js +3 -3
- package/dist/core/extensions/jiti-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-loader.js +3 -3
- package/dist/core/extensions/jiti-static-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-static-loader.js +3 -3
- package/dist/core/extensions/loader-host-modules.impl.js +3 -3
- package/dist/core/extensions/loader-host-modules.js +3 -3
- package/dist/core/intercom-broker-control-host.impl.js +3 -3
- package/dist/core/intercom-broker-control-host.js +3 -3
- package/dist/core/intercom-broker-control.code.js +2 -2
- package/dist/core/intercom-broker-control.impl.js +3 -3
- package/dist/core/intercom-broker-control.js +3 -3
- package/dist/core/package-manager-storage-adapters.impl.js +3 -3
- package/dist/core/package-manager-storage-adapters.js +3 -3
- package/dist/core/sdk.impl.js +3 -3
- package/dist/core/sdk.js +3 -3
- package/dist/core/session-manager-core.js +2 -2
- package/dist/core/session-manager-list.impl.js +3 -3
- package/dist/core/session-manager-list.js +3 -3
- package/dist/core/session-manager.impl.js +3 -3
- package/dist/core/session-manager.js +3 -3
- package/dist/core/session-storage.impl.js +3 -3
- package/dist/core/session-storage.js +3 -3
- package/dist/core/storage/database.d.ts +6 -2
- package/dist/core/storage/database.js.map +1 -1
- package/dist/core/storage/fenced-paths.d.ts +15 -0
- package/dist/core/storage/fenced-paths.js +25 -0
- package/dist/core/storage/fenced-paths.js.map +1 -1
- package/dist/core/storage/index.js +2 -2
- package/dist/core/storage/migrations.d.ts +2 -11
- package/dist/core/storage/migrations.js +2 -9
- package/dist/core/storage/migrations.js.map +1 -1
- package/dist/core/storage/node-sqlite-thread.js +2 -2
- package/dist/core/storage/node-sqlite.js +2 -2
- package/dist/core/storage/offline-snapshot-file.impl.js +3 -3
- package/dist/core/storage/offline-snapshot-file.js +3 -3
- package/dist/core/storage/postgres-locks.d.ts +11 -0
- package/dist/core/storage/postgres-locks.js +9 -0
- package/dist/core/storage/postgres-locks.js.map +1 -0
- package/dist/core/storage/provider.js +2 -2
- package/dist/core/storage/selection.js +2 -2
- package/dist/core/storage/session/branch-fork-source.code.js +2 -2
- package/dist/core/storage/session/branch-fork-source.impl.js +3 -3
- package/dist/core/storage/session/branch-fork-source.js +3 -3
- package/dist/core/storage/session/card-view.impl.js +3 -3
- package/dist/core/storage/session/card-view.js +3 -3
- package/dist/core/storage/session/child-reader.code.js +2 -2
- package/dist/core/storage/session/child-reader.impl.js +3 -3
- package/dist/core/storage/session/child-reader.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.impl.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.js +3 -3
- package/dist/core/storage/session/expanded-child-details.code.js +2 -2
- package/dist/core/storage/session/expanded-child-details.impl.js +3 -3
- package/dist/core/storage/session/expanded-child-details.js +3 -3
- package/dist/core/storage/session/fork-row-bytes.impl.js +3 -3
- package/dist/core/storage/session/fork-row-bytes.js +3 -3
- package/dist/core/storage/session/fork-row-index.impl.js +3 -3
- package/dist/core/storage/session/fork-row-index.js +3 -3
- package/dist/core/storage/session/fork-staging.impl.js +3 -3
- package/dist/core/storage/session/fork-staging.js +3 -3
- package/dist/core/storage/session/fork-window.code.js +2 -2
- package/dist/core/storage/session/fork-window.impl.js +3 -3
- package/dist/core/storage/session/fork-window.js +3 -3
- package/dist/core/storage/session/history-pages.impl.js +3 -3
- package/dist/core/storage/session/history-pages.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.impl.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.js +3 -3
- package/dist/core/storage/session/jsonl-session-store.impl.js +3 -3
- package/dist/core/storage/session/jsonl-session-store.js +3 -3
- package/dist/core/storage/session/memory-session-store.impl.js +3 -3
- package/dist/core/storage/session/memory-session-store.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.code.js +2 -2
- package/dist/core/storage/session/pinned-branch-resume.impl.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.js +3 -3
- package/dist/core/storage/session/released-history-files.impl.js +3 -3
- package/dist/core/storage/session/released-history-files.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.impl.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.js +3 -3
- package/dist/core/storage/session/session-resume.impl.js +3 -3
- package/dist/core/storage/session/session-resume.js +3 -3
- package/dist/core/storage/session/snapshot-cut.impl.js +3 -3
- package/dist/core/storage/session/snapshot-cut.js +3 -3
- package/dist/core/storage/session/snapshot-history.code.js +2 -2
- package/dist/core/storage/session/snapshot-history.impl.js +3 -3
- package/dist/core/storage/session/snapshot-history.js +3 -3
- package/dist/core/storage/session/sql-session-store.d.ts +1 -1
- package/dist/core/storage/session/sql-session-store.impl.js +3 -3
- package/dist/core/storage/session/sql-session-store.js +3 -3
- package/dist/core/storage/session/tree-outline.impl.js +3 -3
- package/dist/core/storage/session/tree-outline.js +3 -3
- package/dist/core/storage/session/tree-presentation.impl.js +3 -3
- package/dist/core/storage/session/tree-presentation.js +3 -3
- package/dist/core/storage/sqlite-facade.js +6 -4
- package/dist/core/storage/sqlite-facade.js.map +1 -1
- package/dist/core/storage/storage-status.impl.js +3 -3
- package/dist/core/storage/storage-status.js +3 -3
- package/dist/core/storage/team.impl.js +3 -3
- package/dist/core/storage/team.js +3 -3
- package/dist/core/structured-output/index.impl.js +3 -3
- package/dist/core/structured-output/index.js +3 -3
- package/dist/core/tasks/heartbeat-notice.d.ts +0 -1
- package/dist/core/tasks/task-completion-entry.impl.js +3 -3
- package/dist/core/tasks/task-completion-entry.js +3 -3
- package/dist/core/tasks/task-file-history.code.js +2 -2
- package/dist/core/tasks/task-file-history.impl.js +3 -3
- package/dist/core/tasks/task-file-history.js +3 -3
- package/dist/core/tasks/task-reference-archive.code.js +2 -2
- package/dist/core/tasks/task-reference-archive.js +3 -3
- package/dist/core/tasks/task-reference-identity.js +3 -3
- package/dist/core/tasks/task-reference-stream.impl.js +3 -3
- package/dist/core/tasks/task-reference-stream.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.impl.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.js +3 -3
- package/dist/core/tasks/task-view-source.impl.js +3 -3
- package/dist/core/tasks/task-view-source.js +3 -3
- package/dist/core/tools/read-url.impl.js +3 -3
- package/dist/core/tools/read-url.js +3 -3
- package/dist/core/worktree-snapshot/blob-store.d.ts +14 -16
- package/dist/core/worktree-snapshot/blob-store.js +194 -81
- package/dist/core/worktree-snapshot/blob-store.js.map +1 -1
- package/dist/core/worktree-snapshot/collection-claim.js +8 -0
- package/dist/core/worktree-snapshot/collection-claim.js.map +1 -0
- package/dist/core/worktree-snapshot/errors.impl.js +3 -3
- package/dist/core/worktree-snapshot/errors.js +3 -3
- package/dist/core/worktree-snapshot/session-index-reset.impl.js +3 -3
- package/dist/core/worktree-snapshot/session-index-reset.js +3 -3
- package/dist/core/worktree-snapshot/snapshot-epoch-migration.js +209 -0
- package/dist/core/worktree-snapshot/snapshot-epoch-migration.js.map +1 -0
- package/dist/core/worktree-snapshot/snapshot-epoch.js +100 -0
- package/dist/core/worktree-snapshot/snapshot-epoch.js.map +1 -0
- package/dist/extensions/codemode/execute.impl.js +3 -3
- package/dist/extensions/codemode/execute.js +3 -3
- package/dist/extensions/codemode/renderer.impl.js +3 -3
- package/dist/extensions/codemode/renderer.js +3 -3
- package/dist/extensions/mcp/cli.impl.js +3 -3
- package/dist/extensions/mcp/cli.js +3 -3
- package/dist/extensions/mcp/cli.lazy.code.js +2 -2
- package/dist/extensions/mcp/cli.lazy.impl.js +3 -3
- package/dist/extensions/mcp/cli.lazy.js +3 -3
- package/dist/extensions/mcp/runtime.impl.js +3 -3
- package/dist/extensions/mcp/runtime.js +3 -3
- package/dist/main-first-time-setup.impl.js +3 -3
- package/dist/main-first-time-setup.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.code.js +2 -2
- package/dist/modes/interactive/components/stored-tree-view.impl.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.impl.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.js +3 -3
- package/dist/package-manager-cli.code.js +2 -2
- package/dist/package-manager-cli.impl.js +3 -3
- package/dist/package-manager-cli.js +3 -3
- package/dist/startup-files.json +1 -1
- package/dist/utils/startup-database-spare.impl.js +3 -3
- package/dist/utils/startup-database-spare.js +3 -3
- package/dist/utils/tools-manager.impl.js +3 -3
- package/dist/utils/tools-manager.js +3 -3
- package/docs/background-tasks/reference.md +1 -1
- package/docs/background-tasks.md +1 -1
- package/docs/changelog.mdx +17 -0
- package/docs/reference/cli.md +3 -2
- package/docs/storage/postgres/tuning.md +2 -0
- package/docs/storage/postgres.md +17 -0
- package/docs/worktree-snapshots/reference.md +1 -1
- package/package.json +8 -8
|
@@ -8,8 +8,11 @@ import { renderStorageIdentifiers, storageMigrationTable, validateStoragePrefix
|
|
|
8
8
|
import { schemaCatalogOf, withStorageSchemaCatalog } from "../storage/schema-catalog.js";
|
|
9
9
|
import { queueSqliteSchemaSetup } from "../storage/sqlite-open-batch.js";
|
|
10
10
|
import { statementBatch } from "../storage/statement-batches.js";
|
|
11
|
+
import { SnapshotCollectionClaimLost } from "./collection-claim.js";
|
|
11
12
|
import { forkInheritedBlobGuard, forkInheritedBlobGuardName } from "./fork-blob-guard.js";
|
|
12
13
|
import { entriesIndexComplete, forkLinksCheck, forkSegmentsTable, forkSessionLink, legacyForkLinksTriggerName, materializedForkLinks, ownDuplicate, ownParentSeq, parentColumns, snapshotEntryCondition, } from "./fork-snapshot-index.js";
|
|
14
|
+
import { snapshotEpochActive, snapshotEpochTransaction, snapshotReferenceTransaction } from "./snapshot-epoch.js";
|
|
15
|
+
import { changeSnapshotEpoch, snapshotSourceObjects } from "./snapshot-epoch-migration.js";
|
|
13
16
|
/**
|
|
14
17
|
* The key under which a failed rename's error carries the trigger or view that stopped it, once confirmed in the
|
|
15
18
|
* database (#5622); `WorkspaceCollectionError` names it in its hint. A `Symbol.for` key, shared without an import.
|
|
@@ -280,6 +283,20 @@ function collectableAge(graceSeconds) {
|
|
|
280
283
|
* milliseconds for a large session whose pages are not cached, so each session is counted in a statement of its own.
|
|
281
284
|
*/
|
|
282
285
|
const SWEEP_TURN_MS = 8;
|
|
286
|
+
/** Aim for short deletes even when a batch's blobs are chunked; an oversized blob is deleted by itself. */
|
|
287
|
+
const SNAPSHOT_COLLECTION_DELETE_BYTES = 16 * SNAPSHOT_BLOB_CHUNK_BYTES;
|
|
288
|
+
/**
|
|
289
|
+
* Deadline for a granted PostgreSQL cleanup transaction where supported (17+). PostgreSQL 16 has no transaction
|
|
290
|
+
* deadline: cap each statement and each pause between requests there too. A normal delete does at most four SQL
|
|
291
|
+
* statements between the grant and commit, without sleeps, so the limits leave room under the 2 s writer budget.
|
|
292
|
+
*/
|
|
293
|
+
const POSTGRES_COLLECTION_LIMITS = {
|
|
294
|
+
sql: `SELECT set_config('statement_timeout', '250ms', true),
|
|
295
|
+
set_config('idle_in_transaction_session_timeout', '100ms', true),
|
|
296
|
+
CASE WHEN current_setting('transaction_timeout', true) IS NOT NULL
|
|
297
|
+
THEN set_config('transaction_timeout', '1s', true) END`,
|
|
298
|
+
params: [],
|
|
299
|
+
};
|
|
283
300
|
/**
|
|
284
301
|
* How long a sweep goes without a write before it renews its claim in a transaction of its own: a quarter of the claim
|
|
285
302
|
* lease, so pages with nothing to delete, which write nothing else, never let the lease run out.
|
|
@@ -376,16 +393,21 @@ class SweepSessions {
|
|
|
376
393
|
let batch = [];
|
|
377
394
|
let sessions = new Set();
|
|
378
395
|
let total = 0;
|
|
396
|
+
let bytes = 0;
|
|
379
397
|
const added = (candidate) => candidate.sessions.reduce((sum, session) => (sessions.has(session) ? sum : sum + (this.costs.get(session) ?? 1)), 0);
|
|
380
398
|
for (const candidate of candidates) {
|
|
381
399
|
if (batch.length === SNAPSHOT_COLLECTION_DELETE_BATCH ||
|
|
382
|
-
(batch.length > 0 &&
|
|
400
|
+
(batch.length > 0 &&
|
|
401
|
+
(total + added(candidate) > SNAPSHOT_COLLECTION_SESSION_REFS ||
|
|
402
|
+
bytes + candidate.bytes > SNAPSHOT_COLLECTION_DELETE_BYTES))) {
|
|
383
403
|
batches.push({ hashes: batch, sessions: [...sessions] });
|
|
384
404
|
batch = [];
|
|
385
405
|
sessions = new Set();
|
|
386
406
|
total = 0;
|
|
407
|
+
bytes = 0;
|
|
387
408
|
}
|
|
388
409
|
total += added(candidate);
|
|
410
|
+
bytes += candidate.bytes;
|
|
389
411
|
batch.push(candidate.hash);
|
|
390
412
|
for (const session of candidate.sessions)
|
|
391
413
|
sessions.add(session);
|
|
@@ -1194,12 +1216,14 @@ async function readSourceStates(executor, dialect, t, sources, listRetired = fal
|
|
|
1194
1216
|
: source.triggers.map((trigger) => ({ trigger: trigger.name, text: source.marker })))
|
|
1195
1217
|
: [];
|
|
1196
1218
|
const holds = (checks) => checks.length === 0 ? "NULL" : `CASE ${checks.map(() => "WHEN name = ? THEN instr(sql, ?) > 0").join(" ")} END`;
|
|
1197
|
-
const objects =
|
|
1198
|
-
? dialect.schemaObjects(names)
|
|
1199
|
-
:
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1219
|
+
const objects = !sqlite
|
|
1220
|
+
? snapshotSourceObjects(dialect.schemaObjects(names))
|
|
1221
|
+
: signatures.length + markers.length === 0
|
|
1222
|
+
? dialect.schemaObjects(names)
|
|
1223
|
+
: {
|
|
1224
|
+
sql: `SELECT name, ${holds(signatures)} AS signed, ${holds(markers)} AS marked FROM sqlite_schema WHERE name IN (${names.map(() => "?").join(", ")})`,
|
|
1225
|
+
params: [...signatures, ...markers].flatMap((check) => [check.trigger, check.text]).concat(names),
|
|
1226
|
+
};
|
|
1203
1227
|
const progressRead = sourceProgressRead(t);
|
|
1204
1228
|
// Read together: the progress rows do not depend on the objects.
|
|
1205
1229
|
const [objectRows, progressRows] = await statementBatch(executor, [], [
|
|
@@ -1305,6 +1329,31 @@ export class SqlSnapshotBlobStore {
|
|
|
1305
1329
|
this.forkSegments = forkSegments;
|
|
1306
1330
|
this.table = tables.blobs;
|
|
1307
1331
|
}
|
|
1332
|
+
/** Explicit owner operation; automatic cleanup never changes the compatibility mode. */
|
|
1333
|
+
async setRecordCoordination(active, minimumVersion) {
|
|
1334
|
+
if (this.dialect.name !== "postgres")
|
|
1335
|
+
throw new Error("Snapshot cleanup coordination is only configurable for PostgreSQL.");
|
|
1336
|
+
if ((await snapshotEpochActive(this.database, this.table)) === active)
|
|
1337
|
+
return false;
|
|
1338
|
+
if (active) {
|
|
1339
|
+
await this.completeIndex();
|
|
1340
|
+
await this.stampLegacyAges({});
|
|
1341
|
+
}
|
|
1342
|
+
const entries = this.sources.find((source) => source.name === "session-entries").table;
|
|
1343
|
+
return changeSnapshotEpoch(this.database, {
|
|
1344
|
+
tables: this.tables,
|
|
1345
|
+
entries,
|
|
1346
|
+
forkSegments: this.forkSegments,
|
|
1347
|
+
formerSources: this.sources.filter(({ table }) => table !== entries).map(({ table }) => table),
|
|
1348
|
+
sessionHeads: this.sessionHeads("session_id IN (SELECT value FROM jsonb_array_elements_text(touched_sessions))"),
|
|
1349
|
+
}, active, async (tx) => {
|
|
1350
|
+
const states = await readSourceStates(tx, this.dialect, this.tables, this.sources);
|
|
1351
|
+
if (!states.every(({ installed, backfilled }) => installed && backfilled))
|
|
1352
|
+
throw new Error("Snapshot references changed during the switch; rerun the snapshot cleanup migration.");
|
|
1353
|
+
if (await tx.get(`SELECT 1 FROM ${this.table} WHERE created_at = 0 OR (chunks = 0 AND length(data) > ${SNAPSHOT_BLOB_CHUNK_BYTES}) LIMIT 1`))
|
|
1354
|
+
throw new Error("Legacy snapshots changed during the switch; rerun the snapshot cleanup migration.");
|
|
1355
|
+
}, minimumVersion);
|
|
1356
|
+
}
|
|
1308
1357
|
/** Which table this store collects, for telling stores of one database and prefix apart from others. */
|
|
1309
1358
|
get identity() {
|
|
1310
1359
|
return `${databaseIdentity(this.database.descriptor)}${this.table}`;
|
|
@@ -1525,7 +1574,7 @@ export class SqlSnapshotBlobStore {
|
|
|
1525
1574
|
if (!linksHot)
|
|
1526
1575
|
await ensureHotSessionLinks(database, t, prefix);
|
|
1527
1576
|
const store = new SqlSnapshotBlobStore(database, dialect, t, indexSources(prefix, t, dialect, (await shared) ? segments : undefined), segments);
|
|
1528
|
-
await store.installIndex(
|
|
1577
|
+
await store.installIndex({}, false, await schemaCatalogOf(database));
|
|
1529
1578
|
return store;
|
|
1530
1579
|
}
|
|
1531
1580
|
/**
|
|
@@ -1677,26 +1726,34 @@ export class SqlSnapshotBlobStore {
|
|
|
1677
1726
|
}
|
|
1678
1727
|
statements.push({ sql: header, params: [hash, data.byteLength, new Uint8Array(), chunks] });
|
|
1679
1728
|
}
|
|
1680
|
-
if (!chunked && this.database.batch) {
|
|
1729
|
+
if (!chunked && this.dialect.name !== "postgres" && this.database.batch) {
|
|
1681
1730
|
await this.database.batch(statements);
|
|
1682
1731
|
return;
|
|
1683
1732
|
}
|
|
1684
1733
|
const lead = statements.length < MAX_SEQUENCE_REQUEST_STATEMENTS && totalBytes <= SNAPSHOT_BLOB_CHUNK_BYTES;
|
|
1685
|
-
|
|
1734
|
+
// Chunk inserts read the header before the upsert. Lock it first, in a separate READ COMMITTED statement, so
|
|
1735
|
+
// a put that waits for a collector deleting the old copy also sees that deletion when deciding to insert bytes.
|
|
1736
|
+
const locked = chunked && this.dialect.name === "postgres"
|
|
1737
|
+
? {
|
|
1738
|
+
sql: `SELECT hash FROM ${this.table} WHERE hash IN (SELECT value FROM ${this.dialect.jsonValues("?")})
|
|
1739
|
+
ORDER BY hash${this.dialect.byteOrderCollation} FOR NO KEY UPDATE`,
|
|
1740
|
+
params: [JSON.stringify(blobs.map(({ hash }) => hash))],
|
|
1741
|
+
}
|
|
1742
|
+
: undefined;
|
|
1743
|
+
await snapshotReferenceTransaction(this.database, this.table, async (transaction) => {
|
|
1744
|
+
if (locked !== undefined)
|
|
1745
|
+
await transaction.all(locked.sql, ...locked.params);
|
|
1686
1746
|
// On a pipelined connection the statements go out with the commit: one round trip after the locks.
|
|
1687
1747
|
if (transactionFinisher(transaction)?.(statements) === true)
|
|
1688
1748
|
return;
|
|
1689
1749
|
for (const statement of statements)
|
|
1690
1750
|
await transaction.run(statement.sql, ...statement.params);
|
|
1691
|
-
}, this.blobWrite(lead ? statements : undefined, lead));
|
|
1751
|
+
}, this.blobWrite(lead ? statements : locked === undefined ? undefined : [locked], lead));
|
|
1692
1752
|
}
|
|
1693
1753
|
/**
|
|
1694
|
-
*
|
|
1695
|
-
*
|
|
1696
|
-
*
|
|
1697
|
-
* database-wide lock shared with their session's domain, run beside it. Rows it writes again (`created_at`) are locked
|
|
1698
|
-
* in hash order, as an append's check of the blobs it records locks them (`missingBlob`), so neither waits on the
|
|
1699
|
-
* other in a cycle. Its callback only uses the transaction, so a transient failure runs it again.
|
|
1754
|
+
* A put or refresh retains the blob writer's domain and locks only its hashes, in hash order. In PostgreSQL
|
|
1755
|
+
* compatibility mode it also takes the legacy shared key; in record mode cleanup coordinates on the row locks
|
|
1756
|
+
* instead. Its callback only uses the transaction, so a transient failure runs it again.
|
|
1700
1757
|
*/
|
|
1701
1758
|
blobWrite(leading, leadingOnly = false) {
|
|
1702
1759
|
return {
|
|
@@ -1737,7 +1794,7 @@ export class SqlSnapshotBlobStore {
|
|
|
1737
1794
|
const params = [JSON.stringify(unique)];
|
|
1738
1795
|
const update = `UPDATE ${this.table} SET created_at = ${this.dialect.nowSeconds} WHERE ${requested} AND created_at > 0`;
|
|
1739
1796
|
const present = `SELECT hash FROM ${this.table} WHERE ${requested}`;
|
|
1740
|
-
if (this.database.batch) {
|
|
1797
|
+
if (this.dialect.name !== "postgres" && this.database.batch) {
|
|
1741
1798
|
const [, rows] = await this.database.batch([
|
|
1742
1799
|
{ sql: update, params },
|
|
1743
1800
|
{ sql: present, params },
|
|
@@ -1750,7 +1807,7 @@ export class SqlSnapshotBlobStore {
|
|
|
1750
1807
|
sql: `${present} ORDER BY hash${this.dialect.byteOrderCollation}${this.dialect.lockClause("update")}`,
|
|
1751
1808
|
params,
|
|
1752
1809
|
};
|
|
1753
|
-
return this.database.
|
|
1810
|
+
return snapshotReferenceTransaction(this.database, this.table, async (transaction) => {
|
|
1754
1811
|
const rows = await transaction.all(locked.sql, ...locked.params);
|
|
1755
1812
|
if (transactionFinisher(transaction)?.([{ sql: update, params }]) !== true)
|
|
1756
1813
|
await transaction.run(update, ...params);
|
|
@@ -1822,7 +1879,7 @@ export class SqlSnapshotBlobStore {
|
|
|
1822
1879
|
* above it, indexed by the triggers. A table that has none yet, an empty one included, costs one watermark of 0.
|
|
1823
1880
|
* Resolves with the state of every referencing table.
|
|
1824
1881
|
*/
|
|
1825
|
-
async installIndex(pace, includeDeferred = false, catalog) {
|
|
1882
|
+
async installIndex({ pace, holder }, includeDeferred = false, catalog) {
|
|
1826
1883
|
// The fork links trigger earlier builds of #6560 installed, asked for in the same read: dropped where found, in the
|
|
1827
1884
|
// first write below, so a database that has it runs it no longer once a store of this build opened it.
|
|
1828
1885
|
const legacy = this.legacyForkLinks();
|
|
@@ -1833,8 +1890,7 @@ export class SqlSnapshotBlobStore {
|
|
|
1833
1890
|
if (drop === undefined)
|
|
1834
1891
|
return;
|
|
1835
1892
|
const { name, table } = drop;
|
|
1836
|
-
await
|
|
1837
|
-
drop = undefined;
|
|
1893
|
+
await this.repairSchema(transaction, pace, table, () => transaction.exec(this.dialect.dropTrigger(name, table, true)));
|
|
1838
1894
|
};
|
|
1839
1895
|
const missing = states.filter((state) => !state.installed && (includeDeferred || !state.deferred));
|
|
1840
1896
|
for (const { source } of missing) {
|
|
@@ -1842,16 +1898,23 @@ export class SqlSnapshotBlobStore {
|
|
|
1842
1898
|
? await SqlSnapshotBlobStore.prepareSessionIndexReset(this.database)
|
|
1843
1899
|
: undefined;
|
|
1844
1900
|
await this.write(pace, async (transaction) => {
|
|
1901
|
+
if (holder !== undefined)
|
|
1902
|
+
await this.renewClaim(transaction, holder);
|
|
1845
1903
|
await dropLegacy(transaction);
|
|
1846
1904
|
const [current] = await readSourceStates(transaction, this.dialect, this.tables, [source]);
|
|
1847
1905
|
if (current === undefined || current.installed || (current.deferred && !includeDeferred))
|
|
1848
1906
|
return;
|
|
1849
|
-
|
|
1850
|
-
|
|
1907
|
+
await this.repairSchema(transaction, pace, source.table, () => installSource(transaction, this.dialect, this.tables, source, reset));
|
|
1908
|
+
});
|
|
1909
|
+
drop = undefined;
|
|
1910
|
+
}
|
|
1911
|
+
if (drop !== undefined) {
|
|
1912
|
+
await this.write(pace, async (transaction) => {
|
|
1913
|
+
if (holder !== undefined)
|
|
1914
|
+
await this.renewClaim(transaction, holder);
|
|
1915
|
+
await dropLegacy(transaction);
|
|
1851
1916
|
});
|
|
1852
1917
|
}
|
|
1853
|
-
if (drop !== undefined)
|
|
1854
|
-
await this.write(pace, dropLegacy);
|
|
1855
1918
|
return missing.length === 0 ? states : this.readStates(includeDeferred);
|
|
1856
1919
|
}
|
|
1857
1920
|
/** The fork links trigger earlier builds of #6560 installed on the session entries, and its table. */
|
|
@@ -1877,7 +1940,9 @@ export class SqlSnapshotBlobStore {
|
|
|
1877
1940
|
readOnly: true,
|
|
1878
1941
|
retryTransientFailures: true,
|
|
1879
1942
|
leading: [
|
|
1880
|
-
...(present === undefined
|
|
1943
|
+
...(present === undefined
|
|
1944
|
+
? [snapshotSourceObjects(this.dialect.schemaObjects([...names, ...(also?.names ?? [])]))]
|
|
1945
|
+
: []),
|
|
1881
1946
|
sourceProgressRead(this.tables),
|
|
1882
1947
|
],
|
|
1883
1948
|
});
|
|
@@ -1891,7 +1956,7 @@ export class SqlSnapshotBlobStore {
|
|
|
1891
1956
|
* before each batch and rejects with its reason.
|
|
1892
1957
|
*/
|
|
1893
1958
|
async completeIndex(options = {}) {
|
|
1894
|
-
const states = await this.installIndex(options
|
|
1959
|
+
const states = await this.installIndex(options, true);
|
|
1895
1960
|
for (const { source, backfilled } of states) {
|
|
1896
1961
|
if (backfilled)
|
|
1897
1962
|
continue;
|
|
@@ -1913,10 +1978,9 @@ export class SqlSnapshotBlobStore {
|
|
|
1913
1978
|
await this.betweenBatches(options.pace);
|
|
1914
1979
|
options.signal?.throwIfAborted();
|
|
1915
1980
|
const done = await this.write(options.pace, async (tx) => {
|
|
1916
|
-
const finished = await drainRetiredSessionIndex(tx, this.tables, receipt);
|
|
1917
1981
|
if (options.holder !== undefined)
|
|
1918
1982
|
await this.renewClaim(tx, options.holder);
|
|
1919
|
-
return
|
|
1983
|
+
return drainRetiredSessionIndex(tx, this.tables, receipt);
|
|
1920
1984
|
});
|
|
1921
1985
|
if (done)
|
|
1922
1986
|
break;
|
|
@@ -1928,17 +1992,19 @@ export class SqlSnapshotBlobStore {
|
|
|
1928
1992
|
const t = this.tables;
|
|
1929
1993
|
const row = this.dialect.rowOrder();
|
|
1930
1994
|
const withholds = withholdsVersion(this.dialect, source);
|
|
1931
|
-
return this.write(pace, async (transaction) => {
|
|
1995
|
+
return this.write(pace, async (transaction, recordMode) => {
|
|
1996
|
+
if (holder !== undefined)
|
|
1997
|
+
await this.renewClaim(transaction, holder);
|
|
1932
1998
|
const [state] = await readSourceStates(transaction, this.dialect, t, [source]);
|
|
1933
1999
|
if (state === undefined || state.backfilled)
|
|
1934
2000
|
return true;
|
|
2001
|
+
if (recordMode)
|
|
2002
|
+
this.refuseRecordModeRepair();
|
|
1935
2003
|
if (state.foreign) {
|
|
1936
2004
|
// A build before 0.9.88 installed its own triggers since the loop read the states: the source is installed again
|
|
1937
2005
|
// in this transaction, its old rows removed, as at the start of a collection. Left as it is, that build could
|
|
1938
2006
|
// index it its way, and builds 0.9.88 and 0.9.89 trust their version row (#5558).
|
|
1939
|
-
await
|
|
1940
|
-
if (holder !== undefined)
|
|
1941
|
-
await this.renewClaim(transaction, holder);
|
|
2007
|
+
await this.repairSchema(transaction, pace, source.table, () => installSource(transaction, this.dialect, t, source, reset));
|
|
1942
2008
|
return false;
|
|
1943
2009
|
}
|
|
1944
2010
|
// A source that lost triggers meanwhile is indexed on, as its rows still need this build's statements; the sweep
|
|
@@ -2001,8 +2067,6 @@ export class SqlSnapshotBlobStore {
|
|
|
2001
2067
|
await transaction.run(`UPDATE ${t.sources} SET backfilled_through = ? WHERE source = ? AND backfilled_through < ?`, upper, source.name, upper);
|
|
2002
2068
|
if (own !== undefined)
|
|
2003
2069
|
await transaction.run(`UPDATE ${t.sources} SET watermark = ?, backfilled_through = ? WHERE source = ?`, progress.watermark, upper, source.own);
|
|
2004
|
-
if (holder !== undefined)
|
|
2005
|
-
await this.renewClaim(transaction, holder);
|
|
2006
2070
|
const done = upper >= progress.watermark;
|
|
2007
2071
|
// Both progress rows reach the watermark: builds 0.9.88 and 0.9.89 may trust the index now (#5558).
|
|
2008
2072
|
if (done && publish)
|
|
@@ -2047,7 +2111,7 @@ export class SqlSnapshotBlobStore {
|
|
|
2047
2111
|
await this.write(options.pace, async (transaction) => {
|
|
2048
2112
|
if (options.holder !== undefined)
|
|
2049
2113
|
await this.renewClaim(transaction, options.holder);
|
|
2050
|
-
await
|
|
2114
|
+
await this.repairSchema(transaction, options.pace, t.blobs, async () => {
|
|
2051
2115
|
if (!present.has(trigger))
|
|
2052
2116
|
await addBlobChunks(transaction, t, this.dialect);
|
|
2053
2117
|
await addBlobMaintenanceIndexes(transaction, t);
|
|
@@ -2068,11 +2132,11 @@ export class SqlSnapshotBlobStore {
|
|
|
2068
2132
|
// Pages that hold nothing to delete write nothing else; the claim is renewed when the sweep starts and then
|
|
2069
2133
|
// at least once per quarter lease.
|
|
2070
2134
|
const holder = options.holder;
|
|
2071
|
-
await this.write(options.pace, (transaction) => this.renewClaim(transaction, holder));
|
|
2135
|
+
await this.write(options.pace, (transaction) => this.renewClaim(transaction, holder), "claim");
|
|
2072
2136
|
renewedAt = performance.now();
|
|
2073
2137
|
}
|
|
2074
2138
|
const page = await this.page(minimumAge, cursor);
|
|
2075
|
-
const candidates = await this.unretainedOf(page
|
|
2139
|
+
const candidates = await this.unretainedOf(page, sessions, turns, forks);
|
|
2076
2140
|
let incomplete = false;
|
|
2077
2141
|
for (const batch of sessions.batches(candidates)) {
|
|
2078
2142
|
// Every delete batch starts on a turn of its own and ends with one: never a turn while it holds the write lock.
|
|
@@ -2096,7 +2160,8 @@ export class SqlSnapshotBlobStore {
|
|
|
2096
2160
|
continue;
|
|
2097
2161
|
}
|
|
2098
2162
|
if (page.length < SNAPSHOT_COLLECTION_BATCH) {
|
|
2099
|
-
await this.
|
|
2163
|
+
if (this.dialect.name !== "postgres" || !(await snapshotEpochActive(this.database, this.table)))
|
|
2164
|
+
await this.stampLegacyAges(options);
|
|
2100
2165
|
await this.moveChunksToRowTable(options);
|
|
2101
2166
|
return deleted;
|
|
2102
2167
|
}
|
|
@@ -2107,16 +2172,17 @@ export class SqlSnapshotBlobStore {
|
|
|
2107
2172
|
}
|
|
2108
2173
|
}
|
|
2109
2174
|
/**
|
|
2110
|
-
* Which
|
|
2175
|
+
* Which blobs in `page` no retaining run, no carried work and no session retains, as far as these reads see: the sweep's
|
|
2111
2176
|
* candidates, which a delete batch checks again in its transaction. Finds the retained blobs of each session that
|
|
2112
2177
|
* references one of them and that `sessions` has not walked yet, in statements of at most
|
|
2113
2178
|
* {@link SNAPSHOT_COLLECTION_SESSION_REFS} snapshot entries, with a turn between them.
|
|
2114
2179
|
*/
|
|
2115
|
-
async unretainedOf(
|
|
2116
|
-
if (
|
|
2180
|
+
async unretainedOf(page, sessions, turns, forks) {
|
|
2181
|
+
if (page.length === 0)
|
|
2117
2182
|
return [];
|
|
2183
|
+
const sizes = new Map(page.map(({ hash, bytes }) => [hash, Number(bytes)]));
|
|
2118
2184
|
const t = this.tables;
|
|
2119
|
-
const free = await this.database.all(`SELECT value AS hash FROM ${this.dialect.jsonValues("?")} WHERE ${this.unreferencedByRuns("value")}`, JSON.stringify(
|
|
2185
|
+
const free = await this.database.all(`SELECT value AS hash FROM ${this.dialect.jsonValues("?")} WHERE ${this.unreferencedByRuns("value")}`, JSON.stringify([...sizes.keys()]));
|
|
2120
2186
|
if (free.length === 0)
|
|
2121
2187
|
return [];
|
|
2122
2188
|
const referencing = new Map(free.map((row) => [row.hash, []]));
|
|
@@ -2134,7 +2200,7 @@ export class SqlSnapshotBlobStore {
|
|
|
2134
2200
|
}
|
|
2135
2201
|
return [...referencing]
|
|
2136
2202
|
.filter(([hash]) => !sessions.retains(hash))
|
|
2137
|
-
.map(([hash, referencedBy]) => ({ hash, sessions: referencedBy }));
|
|
2203
|
+
.map(([hash, referencedBy]) => ({ hash, sessions: referencedBy, bytes: sizes.get(hash) }));
|
|
2138
2204
|
}
|
|
2139
2205
|
/**
|
|
2140
2206
|
* Deletes the blobs of `batch` that are still old and that no retaining run, no carried work and no session retains,
|
|
@@ -2145,15 +2211,12 @@ export class SqlSnapshotBlobStore {
|
|
|
2145
2211
|
* since, is kept; the next collection walks that session. Nothing in the transaction waits for a turn of the event
|
|
2146
2212
|
* loop (#5530).
|
|
2147
2213
|
*
|
|
2148
|
-
*
|
|
2149
|
-
*
|
|
2150
|
-
* session write that records a blob meanwhile holds the blob's row only until it commits its reference; the delete
|
|
2151
|
-
* waits for it, then goes ahead without looking at the references again, and removes a blob a session has just
|
|
2152
|
-
* recorded. The same goes for the references retaining runs and carried work write.
|
|
2214
|
+
* In record mode, lock existing candidate blobs before starting the retention recheck. The recheck must be a
|
|
2215
|
+
* separate READ COMMITTED command: neither EvalPlanQual nor SKIP LOCKED refreshes a DELETE's join snapshot.
|
|
2153
2216
|
*/
|
|
2154
2217
|
deleteUnretained(batch, minimumAge, options) {
|
|
2155
2218
|
const t = this.tables;
|
|
2156
|
-
return this.write(options.pace, async (transaction) => {
|
|
2219
|
+
return this.write(options.pace, async (transaction, recordMode) => {
|
|
2157
2220
|
const found = new Set();
|
|
2158
2221
|
const states = await readSourceStates(transaction, this.dialect, this.tables, this.sources, false, undefined, {
|
|
2159
2222
|
names: [this.forkSegments],
|
|
@@ -2163,11 +2226,24 @@ export class SqlSnapshotBlobStore {
|
|
|
2163
2226
|
return undefined;
|
|
2164
2227
|
if (options.holder !== undefined)
|
|
2165
2228
|
await this.renewClaim(transaction, options.holder);
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2229
|
+
let unretained = batch.hashes;
|
|
2230
|
+
if (recordMode) {
|
|
2231
|
+
const locked = await transaction.all(`SELECT hash FROM ${t.blobs} WHERE hash IN (SELECT value FROM ${this.dialect.jsonValues("?")})
|
|
2232
|
+
ORDER BY hash${this.dialect.byteOrderCollation} FOR UPDATE SKIP LOCKED`, JSON.stringify(unretained));
|
|
2233
|
+
unretained = locked.map(({ hash }) => hash);
|
|
2234
|
+
if (unretained.length === 0)
|
|
2235
|
+
return [];
|
|
2236
|
+
}
|
|
2237
|
+
const kept = this.keptBySessions({ ...batch, hashes: unretained }, found.has(this.forkSegments));
|
|
2238
|
+
// Keep the recheck and delete in one server call on PostgreSQL, to shorten the exclusive hold.
|
|
2239
|
+
const inDelete = this.dialect.name === "postgres";
|
|
2240
|
+
if (!inDelete) {
|
|
2241
|
+
const retained = new Set((await transaction.all(kept.sql, ...kept.params)).map(({ hash }) => hash));
|
|
2242
|
+
unretained = unretained.filter((hash) => !retained.has(hash));
|
|
2243
|
+
if (unretained.length === 0)
|
|
2244
|
+
return [];
|
|
2245
|
+
}
|
|
2246
|
+
const rows = await transaction.all(`DELETE FROM ${t.blobs} WHERE hash IN (SELECT value FROM ${this.dialect.jsonValues("?")}) AND created_at > 0 AND created_at <= ${this.dialect.nowSeconds} - ? AND ${this.unreferencedByRuns(`${t.blobs}.hash`)}${inDelete ? ` AND hash NOT IN (${kept.sql})` : ""} RETURNING hash`, JSON.stringify(unretained), minimumAge, ...(inDelete ? kept.params : []));
|
|
2171
2247
|
return rows.map((row) => row.hash);
|
|
2172
2248
|
});
|
|
2173
2249
|
}
|
|
@@ -2232,19 +2308,21 @@ export class SqlSnapshotBlobStore {
|
|
|
2232
2308
|
* of `batch.sessions` retain, and those a session not among them records, whose snapshots the sweep did not walk.
|
|
2233
2309
|
* Walks only `batch.sessions`, whose snapshot entries the sweep bounded when it made the batch (#5530).
|
|
2234
2310
|
*/
|
|
2235
|
-
|
|
2311
|
+
keptBySessions(batch, forks) {
|
|
2236
2312
|
const t = this.tables;
|
|
2237
2313
|
const ids = JSON.stringify(batch.sessions);
|
|
2238
2314
|
const walked = `(SELECT value FROM ${this.dialect.jsonValues("?")})`;
|
|
2239
2315
|
const inherited = this.inheritedRecord(forks);
|
|
2240
|
-
|
|
2316
|
+
return {
|
|
2317
|
+
sql: `SELECT DISTINCT record.hash FROM ${t.sessionRecords} AS record
|
|
2241
2318
|
WHERE record.hash IN (SELECT value FROM ${this.dialect.jsonValues("?")})
|
|
2242
2319
|
AND (
|
|
2243
2320
|
record.session_id NOT IN ${walked}
|
|
2244
2321
|
OR (record.session_id, record.snapshot_id) IN (${this.sessionHeads(`session_id IN ${walked}`)})
|
|
2245
2322
|
OR EXISTS (SELECT 1 FROM ${t.sessionLinks} AS broken WHERE broken.session_id = record.session_id AND broken.broken = 1)${inherited}
|
|
2246
|
-
)`,
|
|
2247
|
-
|
|
2323
|
+
)`,
|
|
2324
|
+
params: [JSON.stringify(batch.hashes), ids, ids],
|
|
2325
|
+
};
|
|
2248
2326
|
}
|
|
2249
2327
|
/**
|
|
2250
2328
|
* Stamps every blob of unknown age with the database clock, in hash order, in write transactions of at most
|
|
@@ -2290,7 +2368,9 @@ export class SqlSnapshotBlobStore {
|
|
|
2290
2368
|
/** Stamps the blobs `hashes` name that still have an unknown age, in their rows, in one write transaction. */
|
|
2291
2369
|
async stampInRows(hashes, options) {
|
|
2292
2370
|
options.signal?.throwIfAborted();
|
|
2293
|
-
await this.write(options.pace, async (transaction) => {
|
|
2371
|
+
await this.write(options.pace, async (transaction, recordMode) => {
|
|
2372
|
+
if (recordMode)
|
|
2373
|
+
return;
|
|
2294
2374
|
if (options.holder !== undefined)
|
|
2295
2375
|
await this.renewClaim(transaction, options.holder);
|
|
2296
2376
|
await transaction.run(`UPDATE ${this.table} SET created_at = ${this.dialect.nowSeconds} WHERE hash IN (SELECT value FROM ${this.dialect.jsonValues("?")}) AND created_at = 0`, JSON.stringify(hashes));
|
|
@@ -2317,7 +2397,9 @@ export class SqlSnapshotBlobStore {
|
|
|
2317
2397
|
const perTransaction = SNAPSHOT_LEGACY_STAMP_BYTES / SNAPSHOT_BLOB_CHUNK_BYTES;
|
|
2318
2398
|
for (let first = 0; first < chunks; first += perTransaction) {
|
|
2319
2399
|
options.signal?.throwIfAborted();
|
|
2320
|
-
const pending = await this.write(options.pace, async (transaction) => {
|
|
2400
|
+
const pending = await this.write(options.pace, async (transaction, recordMode) => {
|
|
2401
|
+
if (recordMode)
|
|
2402
|
+
return false;
|
|
2321
2403
|
if (options.holder !== undefined)
|
|
2322
2404
|
await this.renewClaim(transaction, options.holder);
|
|
2323
2405
|
if (!(await transaction.get(`SELECT 1 FROM ${t.blobs} WHERE ${unmoved}`, hash))) {
|
|
@@ -2334,7 +2416,9 @@ export class SqlSnapshotBlobStore {
|
|
|
2334
2416
|
return;
|
|
2335
2417
|
}
|
|
2336
2418
|
options.signal?.throwIfAborted();
|
|
2337
|
-
await this.write(options.pace, async (transaction) => {
|
|
2419
|
+
await this.write(options.pace, async (transaction, recordMode) => {
|
|
2420
|
+
if (recordMode)
|
|
2421
|
+
return;
|
|
2338
2422
|
if (options.holder !== undefined)
|
|
2339
2423
|
await this.renewClaim(transaction, options.holder);
|
|
2340
2424
|
const { changes } = await transaction.run(`UPDATE ${t.blobs} SET data = ?, chunks = ?, created_at = CASE WHEN created_at = 0 THEN ${this.dialect.nowSeconds} ELSE created_at END WHERE ${unmoved} AND (SELECT count(*) FROM ${t.blobChunks} WHERE hash = ?) = ?`, new Uint8Array(), chunks, hash, hash, chunks);
|
|
@@ -2620,8 +2704,8 @@ export class SqlSnapshotBlobStore {
|
|
|
2620
2704
|
page(minimumAge, after) {
|
|
2621
2705
|
const t = this.tables;
|
|
2622
2706
|
return after === undefined
|
|
2623
|
-
? this.database.all(`SELECT created_at, hash FROM ${t.blobs} WHERE created_at > 0 AND created_at <= ${this.dialect.nowSeconds} - ? ORDER BY created_at, hash LIMIT ?`, minimumAge, SNAPSHOT_COLLECTION_BATCH)
|
|
2624
|
-
: this.database.all(`SELECT created_at, hash FROM ${t.blobs} WHERE created_at <= ${this.dialect.nowSeconds} - ? AND (created_at, hash) > (?, ?) ORDER BY created_at, hash LIMIT ?`, minimumAge, after.created_at, after.hash, SNAPSHOT_COLLECTION_BATCH);
|
|
2707
|
+
? this.database.all(`SELECT created_at, hash, bytes FROM ${t.blobs} WHERE created_at > 0 AND created_at <= ${this.dialect.nowSeconds} - ? ORDER BY created_at, hash LIMIT ?`, minimumAge, SNAPSHOT_COLLECTION_BATCH)
|
|
2708
|
+
: this.database.all(`SELECT created_at, hash, bytes FROM ${t.blobs} WHERE created_at <= ${this.dialect.nowSeconds} - ? AND (created_at, hash) > (?, ?) ORDER BY created_at, hash LIMIT ?`, minimumAge, after.created_at, after.hash, SNAPSHOT_COLLECTION_BATCH);
|
|
2625
2709
|
}
|
|
2626
2710
|
/**
|
|
2627
2711
|
* Claims this table's next collection for `holder` when it is due on the database clock: none was ever claimed, or
|
|
@@ -2635,36 +2719,65 @@ export class SqlSnapshotBlobStore {
|
|
|
2635
2719
|
const lease = SNAPSHOT_COLLECTION_LEASE_SECONDS;
|
|
2636
2720
|
if (!(await this.database.get(`SELECT id FROM ${this.tables.collection} WHERE ${due}`, lease, intervalSeconds)))
|
|
2637
2721
|
return false;
|
|
2638
|
-
const { changes } = await this.write(pace, (transaction) => transaction.run(`UPDATE ${this.tables.collection} SET claimed_at = ${this.dialect.nowSeconds}, holder = ?, finished_at = NULL, deleted = NULL, error_code = NULL WHERE ${due}`, holder, lease, intervalSeconds));
|
|
2722
|
+
const { changes } = await this.write(pace, (transaction) => transaction.run(`UPDATE ${this.tables.collection} SET claimed_at = ${this.dialect.nowSeconds}, holder = ?, finished_at = NULL, deleted = NULL, error_code = NULL WHERE ${due}`, holder, lease, intervalSeconds), "claim");
|
|
2639
2723
|
return changes === 1;
|
|
2640
2724
|
}
|
|
2641
|
-
renewClaim(transaction, holder) {
|
|
2642
|
-
|
|
2725
|
+
async renewClaim(transaction, holder) {
|
|
2726
|
+
const { changes } = await transaction.run(`UPDATE ${this.tables.collection} SET claimed_at = ${this.dialect.nowSeconds} WHERE id = 1 AND holder = ? AND finished_at IS NULL`, holder);
|
|
2727
|
+
if (changes !== 1)
|
|
2728
|
+
throw new SnapshotCollectionClaimLost();
|
|
2643
2729
|
}
|
|
2644
2730
|
/**
|
|
2645
2731
|
* Records how `holder`'s claimed collection ended; a later claim by another holder is left alone. Waits for the
|
|
2646
2732
|
* write lock on `pace` when one is given.
|
|
2647
2733
|
*/
|
|
2648
2734
|
async recordCollection(holder, outcome, pace) {
|
|
2649
|
-
await this.write(pace, (transaction) => transaction.run(`UPDATE ${this.tables.collection} SET finished_at = ${this.dialect.nowSeconds}, deleted = ?, error_code = ? WHERE id = 1 AND holder = ?`, outcome.deleted, outcome.errorCode, holder));
|
|
2735
|
+
await this.write(pace, (transaction) => transaction.run(`UPDATE ${this.tables.collection} SET finished_at = ${this.dialect.nowSeconds}, deleted = ?, error_code = ? WHERE id = 1 AND holder = ?`, outcome.deleted, outcome.errorCode, holder), "claim");
|
|
2736
|
+
}
|
|
2737
|
+
/** A background repair must not wait for a table lock or sleep while it holds the database lock. */
|
|
2738
|
+
async repairSchema(transaction, pace, table, setup) {
|
|
2739
|
+
if (this.dialect.name === "postgres" && (await snapshotEpochActive(transaction, this.table)))
|
|
2740
|
+
this.refuseRecordModeRepair();
|
|
2741
|
+
if (pace === undefined || this.dialect.name !== "postgres")
|
|
2742
|
+
return withShortSchemaLockWaits(transaction, this.dialect, setup);
|
|
2743
|
+
// Dropping the old trigger can need ACCESS EXCLUSIVE. If a reader has the table, fail this transaction and
|
|
2744
|
+
// let write() back off, rather than retrying in a savepoint with the database lock still held.
|
|
2745
|
+
await transaction.exec(`LOCK TABLE ${table} IN ACCESS EXCLUSIVE MODE NOWAIT`);
|
|
2746
|
+
await setup();
|
|
2747
|
+
}
|
|
2748
|
+
refuseRecordModeRepair() {
|
|
2749
|
+
throw new Error("The worktree snapshot index needs repair. Disable faster snapshot cleanup with `atomic storage migrate --snapshot-cleanup disable`, then run storage migration again.");
|
|
2650
2750
|
}
|
|
2651
2751
|
/** Waits between two batches: on `pace` in the background, otherwise until the event loop has run once. */
|
|
2652
2752
|
betweenBatches(pace) {
|
|
2653
2753
|
return pace === undefined ? yieldToEventLoop() : pace.wait(0);
|
|
2654
2754
|
}
|
|
2655
2755
|
/**
|
|
2656
|
-
*
|
|
2657
|
-
*
|
|
2658
|
-
*
|
|
2659
|
-
* up to {@link SNAPSHOT_COLLECTION_LEASE_SECONDS}, it rejects with the refusal.
|
|
2756
|
+
* Background PostgreSQL batches try their advisory locks without joining a queue. In compatibility mode that
|
|
2757
|
+
* includes the legacy key. After the switch, only other collectors share its advisory scope. SQLite uses its
|
|
2758
|
+
* existing no-wait writer admission. Retry outside the transaction, with jitter.
|
|
2660
2759
|
*/
|
|
2661
|
-
async write(pace, body) {
|
|
2760
|
+
async write(pace, body, scope = "data") {
|
|
2761
|
+
const limits = this.dialect.name === "postgres" && scope === "data" && pace !== undefined
|
|
2762
|
+
? POSTGRES_COLLECTION_LIMITS
|
|
2763
|
+
: undefined;
|
|
2764
|
+
const attempt = () => snapshotEpochTransaction(this.database, this.table, {
|
|
2765
|
+
kind: "collector",
|
|
2766
|
+
claim: scope === "claim",
|
|
2767
|
+
scope: this.tables.collection,
|
|
2768
|
+
background: pace !== undefined,
|
|
2769
|
+
}, async (transaction, recordMode) => {
|
|
2770
|
+
if (limits !== undefined)
|
|
2771
|
+
await transaction.get(limits.sql, ...limits.params);
|
|
2772
|
+
return body(transaction, recordMode);
|
|
2773
|
+
}, pace === undefined ? {} : { lockWaitMs: 0, ...(limits === undefined ? {} : { leading: [limits] }) });
|
|
2662
2774
|
if (pace === undefined)
|
|
2663
|
-
return
|
|
2775
|
+
return attempt();
|
|
2664
2776
|
let waited = 0;
|
|
2665
2777
|
for (let delay = SNAPSHOT_COLLECTION_LOCK_RETRY_FIRST_MS;;) {
|
|
2778
|
+
pace.signal?.throwIfAborted();
|
|
2666
2779
|
try {
|
|
2667
|
-
return await
|
|
2780
|
+
return await attempt();
|
|
2668
2781
|
}
|
|
2669
2782
|
catch (error) {
|
|
2670
2783
|
const lockRefused = error instanceof StorageWriteLockTimeoutError ||
|
|
@@ -2674,9 +2787,9 @@ export class SqlSnapshotBlobStore {
|
|
|
2674
2787
|
if (!lockRefused || waited >= SNAPSHOT_COLLECTION_LEASE_SECONDS * 1_000)
|
|
2675
2788
|
throw error;
|
|
2676
2789
|
}
|
|
2677
|
-
|
|
2678
|
-
pace.
|
|
2679
|
-
waited +=
|
|
2790
|
+
const jittered = delay * (0.5 + Math.random() / 2);
|
|
2791
|
+
await pace.wait(jittered);
|
|
2792
|
+
waited += jittered;
|
|
2680
2793
|
delay = Math.min(delay * 2, SNAPSHOT_COLLECTION_LOCK_RETRY_MAX_MS);
|
|
2681
2794
|
}
|
|
2682
2795
|
}
|
|
@@ -2688,7 +2801,7 @@ function __atomicOnDemandFailed(error, feature) {
|
|
|
2688
2801
|
return (async () => {
|
|
2689
2802
|
const root = new URL("../../", import.meta.url);
|
|
2690
2803
|
const releases = (globalThis[Symbol.for("atomic.in-place-update-release.v1")] ??= new Map());
|
|
2691
|
-
const key = `${root.href}#0.10.18
|
|
2804
|
+
const key = `${root.href}#0.10.18`;
|
|
2692
2805
|
let release = releases.get(key);
|
|
2693
2806
|
if (release === undefined) {
|
|
2694
2807
|
const current = (async () => {
|
|
@@ -2716,7 +2829,7 @@ function __atomicOnDemandFailed(error, feature) {
|
|
|
2716
2829
|
} catch {
|
|
2717
2830
|
// No installation to read, as in a bundle of the package: the release the process runs decides.
|
|
2718
2831
|
}
|
|
2719
|
-
const updatedTo = typeof installed === "string" && installed !== "0.10.18
|
|
2832
|
+
const updatedTo = typeof installed === "string" && installed !== "0.10.18" ? installed : running !== undefined && running.version !== "0.10.18" ? "0.10.18" : undefined;
|
|
2720
2833
|
if (updatedTo === undefined) throw error;
|
|
2721
2834
|
const application = typeof running?.name === "string" ? running.name : "atomic";
|
|
2722
2835
|
const restart = new Error(`${application} was updated to ${updatedTo} while this session was running; restart it to keep using ${feature}.`, {
|