@camstack/system 1.2.318 → 1.2.319
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/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +2 -76
- package/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +2 -76
- package/dist/builtins/composer/composer-apply-waits.d.ts +21 -0
- package/dist/builtins/composer/composer-live-set.d.ts +22 -0
- package/dist/builtins/composer/composer-runtime-factory.d.ts +22 -0
- package/dist/builtins/composer/composer-seeds.d.ts +22 -0
- package/dist/builtins/composer/composer.addon.js +1903 -1807
- package/dist/builtins/composer/composer.addon.mjs +1903 -1807
- package/dist/builtins/composer/composer.d.ts +3 -15
- package/dist/builtins/sqlite-storage/shm-lock-canary.d.ts +60 -0
- package/dist/builtins/sqlite-storage/sqlite-settings.addon.d.ts +1 -0
- package/dist/builtins/sqlite-storage/sqlite-settings.addon.js +0 -0
- package/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +0 -0
- package/dist/builtins/sqlite-storage/wal-checkpoint-policy.d.ts +115 -34
- package/dist/builtins/sqlite-storage/wal-checkpointer.d.ts +6 -0
- package/dist/builtins/system-backup/system-backup.service.d.ts +2 -17
- package/dist/wal-checkpoint-policy-BahrTtyD.mjs +186 -0
- package/dist/wal-checkpoint-policy-d2z2qPqx.js +233 -0
- package/dist/wal-checkpoint-worker.js +63 -57
- package/dist/wal-checkpoint-worker.mjs +63 -57
- package/package.json +1 -1
- package/dist/wal-checkpoint-policy-CAcg63o-.mjs +0 -92
- package/dist/wal-checkpoint-policy-CCJZngds.js +0 -127
|
@@ -47,10 +47,8 @@ export declare class Composer {
|
|
|
47
47
|
private live;
|
|
48
48
|
/** The composed devices of `new`-target blocks, and the rows that exist. */
|
|
49
49
|
private readonly composed;
|
|
50
|
-
/**
|
|
51
|
-
private
|
|
52
|
-
/** Consecutive applies refused because the Blocks integration had not reconciled: warned once per run. */
|
|
53
|
-
private integrationWaitApplies;
|
|
50
|
+
/** The store / integration preconditions every apply waits on first (D49, R2). */
|
|
51
|
+
private readonly waits;
|
|
54
52
|
private readonly gate;
|
|
55
53
|
private readonly claimGate;
|
|
56
54
|
private readonly targets;
|
|
@@ -84,27 +82,17 @@ export declare class Composer {
|
|
|
84
82
|
/** Run `work` after every apply already queued, whether that settled or failed. */
|
|
85
83
|
private chained;
|
|
86
84
|
private applyNow;
|
|
87
|
-
private waiting;
|
|
88
85
|
private plan;
|
|
89
86
|
private rebuildRuntimes;
|
|
90
87
|
private ungraftGone;
|
|
91
|
-
/** Where a block's runtime writes: its composed device, or a claim-backed target on the existing device. */
|
|
92
|
-
private writerFor;
|
|
93
88
|
/** C-1/N-1: the tracker follows the block's OWN claim as the hub answers it; every answer re-reads the native once. */
|
|
94
89
|
private onClaimEvent;
|
|
95
90
|
private heldFieldsFor;
|
|
96
|
-
private newRuntime;
|
|
97
91
|
private applyName;
|
|
98
92
|
private syncSources;
|
|
99
93
|
/** A failed sync leaves the tracker as it was: the runtimes read what it holds. */
|
|
100
94
|
private syncTracker;
|
|
101
|
-
/**
|
|
102
|
-
* Rule 3: the claims every live customization WANTS this apply; everything
|
|
103
|
-
* else the gate knows is a release candidate, handed back once two applies
|
|
104
|
-
* agree. A block that cannot plan — waiting, unreadable, failed, or with its
|
|
105
|
-
* target gone — HOLDS its claims (rule 4, rule 5): they are wanted as they
|
|
106
|
-
* are. A disabled block wants none (rule 7).
|
|
107
|
-
*/
|
|
95
|
+
/** Rule 3: release what no live customization wants, once two applies agree (`wantedClaims`). */
|
|
108
96
|
private reconcileClaims;
|
|
109
97
|
private release;
|
|
110
98
|
/**
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { IScopedLogger } from '@camstack/types';
|
|
2
|
+
/** `UNIX_SHM_DMS`: the byte of the `-shm` a live connection holds shared. */
|
|
3
|
+
export declare const SQLITE_SHM_DMS_BYTE = 128;
|
|
4
|
+
/** How often the canary looks, in ms. One small procfs read. */
|
|
5
|
+
export declare const SHM_LOCK_CANARY_INTERVAL_MS: number;
|
|
6
|
+
/** One HELD lock from `/proc/locks`. Waiters (`->` lines) are not locks. */
|
|
7
|
+
export interface ProcLock {
|
|
8
|
+
readonly pid: number;
|
|
9
|
+
/** The inode, as the decimal string procfs prints — never rounded through a double. */
|
|
10
|
+
readonly inode: string;
|
|
11
|
+
readonly start: number;
|
|
12
|
+
/** `null` = to end of file (`EOF`). */
|
|
13
|
+
readonly end: number | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Parse `/proc/locks`. Line shape:
|
|
17
|
+
* `1: POSIX ADVISORY READ 3490531 08:31:3769403373 128 128`
|
|
18
|
+
* (`id: TYPE MODE ACCESS PID MAJ:MIN:INODE START END`). A blocked waiter is
|
|
19
|
+
* printed as `1: -> POSIX …` and holds nothing, so it is skipped; a line that
|
|
20
|
+
* does not parse is skipped rather than guessed at.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseProcLocks(text: string): readonly ProcLock[];
|
|
23
|
+
/** Does a lock owned by `pid` on `inode` cover the DMS byte? */
|
|
24
|
+
export declare function holdsDmsLock(locks: readonly ProcLock[], pid: number, inode: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* `unavailable` = this platform has no `/proc/locks` (said once, then the
|
|
27
|
+
* canary stops). `unreadable` = Linux, but this look failed (the `-shm` absent,
|
|
28
|
+
* procfs unreadable) — a different fact, and on the hub a worrying one.
|
|
29
|
+
*/
|
|
30
|
+
export type ShmLockState = 'held' | 'missing' | 'unavailable' | 'unreadable';
|
|
31
|
+
/** The canary's impure edges, injectable so a spec drives them. */
|
|
32
|
+
export interface ShmLockCanaryDeps {
|
|
33
|
+
readonly platform: NodeJS.Platform;
|
|
34
|
+
readonly pid: number;
|
|
35
|
+
/** The `-shm` inode as a decimal string; throws when the file is absent. */
|
|
36
|
+
readonly shmInode: (shmPath: string) => string;
|
|
37
|
+
readonly readProcLocks: () => string;
|
|
38
|
+
}
|
|
39
|
+
export interface ShmLockCanaryOptions {
|
|
40
|
+
readonly dbPath: string;
|
|
41
|
+
readonly logger: IScopedLogger;
|
|
42
|
+
readonly intervalMs?: number;
|
|
43
|
+
readonly deps?: Partial<ShmLockCanaryDeps>;
|
|
44
|
+
}
|
|
45
|
+
export declare class ShmLockCanary {
|
|
46
|
+
private readonly shmPath;
|
|
47
|
+
private readonly logger;
|
|
48
|
+
private readonly intervalMs;
|
|
49
|
+
private readonly deps;
|
|
50
|
+
private timer;
|
|
51
|
+
private last;
|
|
52
|
+
private lastError;
|
|
53
|
+
constructor(options: ShmLockCanaryOptions);
|
|
54
|
+
start(): void;
|
|
55
|
+
stop(): void;
|
|
56
|
+
/** One look. Logs only a change of state. Public for the spec. */
|
|
57
|
+
check(): ShmLockState;
|
|
58
|
+
private observe;
|
|
59
|
+
private report;
|
|
60
|
+
}
|
|
Binary file
|
|
Binary file
|
|
@@ -33,22 +33,64 @@
|
|
|
33
33
|
*
|
|
34
34
|
* The auto-checkpoint's own rule: fold when 1000 frames are pending. The
|
|
35
35
|
* point of moving the checkpoint is to change WHO waits on the disk, not how
|
|
36
|
-
* often the disk is asked to sync — a worker that
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
36
|
+
* often the disk is asked to sync — a worker that folded every second would
|
|
37
|
+
* multiply the fsync count on a drive the media plane already saturates.
|
|
38
|
+
*
|
|
39
|
+
* ## How the worker knows how much is pending — and how it must NOT learn it
|
|
40
|
+
*
|
|
41
|
+
* Until 2026-09-29 the worker read the wal-index header straight out of the
|
|
42
|
+
* `-shm` file, once a second, with `openSync` / `readSync` / `closeSync`. That
|
|
43
|
+
* `closeSync` was the SIGBUS of D468 (corrected by D679): on POSIX, closing
|
|
44
|
+
* ANY descriptor of a file drops EVERY fcntl lock the process holds on that
|
|
45
|
+
* file — including the shared lock on `-shm` byte 128 (the "DMS" lock) that
|
|
46
|
+
* SQLite takes for the life of a connection and that tells every OTHER
|
|
47
|
+
* process "a live connection is using this wal-index, do not rebuild it". With
|
|
48
|
+
* the lock gone, the next process to open the database believed it was
|
|
49
|
+
* first, truncated and re-initialised the `-shm` under the runner's mappings,
|
|
50
|
+
* and the runner's next write past a 32 KiB wal-index region hit a page
|
|
51
|
+
* beyond end-of-file.
|
|
52
|
+
*
|
|
53
|
+
* So the worker never touches a SQLite file except through its connection.
|
|
54
|
+
* What it knows about the WAL it learns from the checkpoint it just ran:
|
|
55
|
+
* `PRAGMA wal_checkpoint(PASSIVE)` answers `log` (frames in the WAL) and
|
|
56
|
+
* `checkpointed` (frames now backfilled). Two consecutive answers give the
|
|
57
|
+
* frames appended between them, hence a rate, hence WHEN the next 1000 frames
|
|
58
|
+
* will have accumulated. The next checkpoint is scheduled for then —
|
|
59
|
+
* {@link nextCheckpointPlan} — clamped between the worker's poll interval and
|
|
60
|
+
* {@link WAL_CHECKPOINT_MAX_DELAY_MS}. A PASSIVE with nothing to fold performs
|
|
61
|
+
* no fsync (the copy and both syncs sit behind `nBackfill < mxFrame` in
|
|
62
|
+
* `wal.c`), so the clamp costs a quiet database nothing, and it bounds how
|
|
63
|
+
* long a burst that follows a quiet spell goes unseen.
|
|
42
64
|
*/
|
|
43
65
|
/** The engine's own `wal_autocheckpoint` default, in frames. Reproduced, not changed. */
|
|
44
66
|
export declare const WAL_CHECKPOINT_THRESHOLD_FRAMES = 1000;
|
|
45
67
|
/**
|
|
46
|
-
* Longest the worker
|
|
47
|
-
*
|
|
48
|
-
*
|
|
68
|
+
* Longest the worker waits between two checkpoints, in ms, whatever the
|
|
69
|
+
* measured rate says. It is the bound on how late a burst after a steady or
|
|
70
|
+
* quiet spell is noticed, and it is sized against the engine's own fallback
|
|
71
|
+
* (`SQLITE_WAL_AUTOCHECKPOINT_BOUND_PAGES` = 10 000 frames), which runs INSIDE
|
|
72
|
+
* a settings-thread commit — the stall D452 moved off that thread. No burst
|
|
73
|
+
* rate has been measured on the hub (D452's 54 frames/s is the steady rate),
|
|
74
|
+
* so the design point is 1 000 frames/s, ~18× steady: 8 s of it is 8 000
|
|
75
|
+
* frames, under the fallback, so the worker looks before the engine has to.
|
|
76
|
+
* A pass that finds more than {@link WAL_CHECKPOINT_LARGE_PASS_FRAMES} is
|
|
77
|
+
* reported so this number can be tuned from the field.
|
|
78
|
+
*
|
|
79
|
+
* The cost, stated against D452: at the steady 54 frames/s the threshold
|
|
80
|
+
* would fold every ~18.5 s; this clamp folds every 8 s, ~2.3× the fsync pairs
|
|
81
|
+
* (≈ 432 frames per fold instead of 1 000). They are paid on the WORKER, a
|
|
82
|
+
* thread nobody waits on — the settings thread's commits stay appends. A pass
|
|
83
|
+
* with nothing pending does no fsync at all.
|
|
84
|
+
*/
|
|
85
|
+
export declare const WAL_CHECKPOINT_MAX_DELAY_MS = 8000;
|
|
86
|
+
/**
|
|
87
|
+
* A pass that folded more than this many appended frames is reported (WARN,
|
|
88
|
+
* sampled: the first of each summary window, the rest counted). Half the
|
|
89
|
+
* engine's fallback bound: a gap that let this much accumulate was within 2×
|
|
90
|
+
* of handing the checkpoint back to the settings thread.
|
|
49
91
|
*/
|
|
50
|
-
export declare const
|
|
51
|
-
/**
|
|
92
|
+
export declare const WAL_CHECKPOINT_LARGE_PASS_FRAMES = 5000;
|
|
93
|
+
/** Shortest gap between two checkpoints, in ms — the worker's poll interval. */
|
|
52
94
|
export declare const WAL_CHECKPOINT_POLL_MS = 1000;
|
|
53
95
|
/**
|
|
54
96
|
* A checkpoint that took this long is reported at WARN with its numbers.
|
|
@@ -56,38 +98,74 @@ export declare const WAL_CHECKPOINT_POLL_MS = 1000;
|
|
|
56
98
|
* the profiler used to file as a slow `set`, now on the thread that paid it.
|
|
57
99
|
*/
|
|
58
100
|
export declare const WAL_CHECKPOINT_SLOW_MS = 1000;
|
|
59
|
-
/**
|
|
60
|
-
export interface
|
|
61
|
-
/**
|
|
62
|
-
readonly
|
|
63
|
-
/**
|
|
64
|
-
readonly
|
|
101
|
+
/** What one `PRAGMA wal_checkpoint(PASSIVE)` answered, and when. */
|
|
102
|
+
export interface CheckpointObservation {
|
|
103
|
+
/** ms epoch at which the checkpoint returned. */
|
|
104
|
+
readonly at: number;
|
|
105
|
+
/** `log`: frames in the WAL when the checkpoint ran. */
|
|
106
|
+
readonly walFrames: number;
|
|
107
|
+
/** `checkpointed`: frames backfilled into the database file. */
|
|
108
|
+
readonly checkpointedFrames: number;
|
|
65
109
|
}
|
|
66
|
-
|
|
67
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Frames appended to the WAL between two checkpoints.
|
|
112
|
+
*
|
|
113
|
+
* When the previous checkpoint backfilled EVERYTHING, the next writer restarts
|
|
114
|
+
* the WAL from frame 1, so every frame now in it is new. When it did not (a
|
|
115
|
+
* reader pinned a snapshot), the WAL kept growing and the difference is what
|
|
116
|
+
* was added — unless it shrank, which only a restart can do.
|
|
117
|
+
*
|
|
118
|
+
* An IDLE WAL is the exception to "complete means restarted": the WAL restarts
|
|
119
|
+
* only at the next write, so a fully checkpointed WAL nobody writes answers
|
|
120
|
+
* the same `log == checkpointed` pass after pass. Identical answers after a
|
|
121
|
+
* complete checkpoint are therefore 0 appended, not the whole WAL again —
|
|
122
|
+
* otherwise an idle node would be scheduled at the poll interval forever and
|
|
123
|
+
* every summary would count the same frames once per pass. (A restart that
|
|
124
|
+
* appended exactly as many frames as the previous cycle is read as idle too;
|
|
125
|
+
* it is scheduled at the max delay, which is the bounded case anyway.)
|
|
126
|
+
*
|
|
127
|
+
* The one remaining ambiguity — a complete checkpoint followed by writes the
|
|
128
|
+
* writer could NOT restart the WAL for — counts old frames as new, which
|
|
129
|
+
* over-estimates the rate and checkpoints EARLY: the safe direction. `-1`
|
|
130
|
+
* (not a WAL database, or a busy pass) reads as 0.
|
|
131
|
+
*/
|
|
132
|
+
export declare function framesAppendedBetween(previous: CheckpointObservation, current: CheckpointObservation): number;
|
|
133
|
+
/**
|
|
134
|
+
* What the worker "saw" before its first checkpoint: nothing, at the moment it
|
|
135
|
+
* started. Against it, the first checkpoint counts every frame then in the WAL
|
|
136
|
+
* as appended since the start — which over-estimates the rate when the WAL
|
|
137
|
+
* held older frames, and so schedules the second checkpoint EARLY: the safe
|
|
138
|
+
* direction, and the second one measures the real rate.
|
|
139
|
+
*/
|
|
140
|
+
export declare function workerStartObservation(at: number): CheckpointObservation;
|
|
141
|
+
export type CheckpointReason = 'first' | 'scheduled';
|
|
142
|
+
export interface CheckpointPlan {
|
|
143
|
+
/** Frames appended since the previous checkpoint (or the worker's start). */
|
|
144
|
+
readonly appendedFrames: number;
|
|
145
|
+
/** How long until the next checkpoint, in ms. */
|
|
146
|
+
readonly nextDelayMs: number;
|
|
68
147
|
}
|
|
69
|
-
export
|
|
70
|
-
|
|
71
|
-
readonly
|
|
72
|
-
readonly
|
|
73
|
-
readonly
|
|
148
|
+
export interface CheckpointPlanInput {
|
|
149
|
+
readonly previous: CheckpointObservation;
|
|
150
|
+
readonly current: CheckpointObservation;
|
|
151
|
+
readonly minDelayMs: number;
|
|
152
|
+
readonly maxDelayMs: number;
|
|
74
153
|
}
|
|
75
|
-
/** Should the worker run `wal_checkpoint(PASSIVE)` now? */
|
|
76
|
-
export declare function decideCheckpoint(input: CheckpointDecisionInput): CheckpointDecision;
|
|
77
|
-
/** Bytes needed to read both fields. */
|
|
78
|
-
export declare const WAL_INDEX_HEADER_READ_BYTES: number;
|
|
79
154
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
155
|
+
* When to run the next checkpoint: the moment the measured rate says
|
|
156
|
+
* {@link WAL_CHECKPOINT_THRESHOLD_FRAMES} more frames will be pending,
|
|
157
|
+
* clamped to `[minDelayMs, maxDelayMs]`. Nothing appended since the last one
|
|
158
|
+
* is `maxDelayMs`.
|
|
84
159
|
*/
|
|
85
|
-
export declare function
|
|
160
|
+
export declare function nextCheckpointPlan(input: CheckpointPlanInput): CheckpointPlan;
|
|
86
161
|
/** What the worker tells the parent after one checkpoint. */
|
|
87
162
|
export interface CheckpointReport {
|
|
88
163
|
readonly kind: 'checkpointed';
|
|
89
164
|
readonly reason: CheckpointReason;
|
|
90
|
-
|
|
165
|
+
/** Frames appended since the previous checkpoint (or the worker's start). */
|
|
166
|
+
readonly appendedFrames: number;
|
|
167
|
+
/** When the worker will checkpoint next, in ms. */
|
|
168
|
+
readonly nextDelayMs: number;
|
|
91
169
|
/** `PRAGMA wal_checkpoint` row: frames in the WAL, frames now backfilled, busy flag. */
|
|
92
170
|
readonly walFrames: number;
|
|
93
171
|
readonly checkpointedFrames: number;
|
|
@@ -108,7 +186,10 @@ export interface WorkerStop {
|
|
|
108
186
|
/** What the worker is started with. */
|
|
109
187
|
export interface WalCheckpointWorkerData {
|
|
110
188
|
readonly dbPath: string;
|
|
189
|
+
/** Shortest gap between two checkpoints. */
|
|
111
190
|
readonly pollMs: number;
|
|
191
|
+
/** Longest gap between two checkpoints. */
|
|
192
|
+
readonly maxDelayMs: number;
|
|
112
193
|
}
|
|
113
194
|
/** Narrow a message off the worker port without a cast. */
|
|
114
195
|
export declare function isWorkerMessage(value: unknown): value is WorkerMessage;
|
|
@@ -10,6 +10,10 @@ export interface WalCheckpointStats {
|
|
|
10
10
|
readonly maxMs: number;
|
|
11
11
|
readonly totalMs: number;
|
|
12
12
|
readonly busy: number;
|
|
13
|
+
/** Passes that folded more than `WAL_CHECKPOINT_LARGE_PASS_FRAMES` appended frames. */
|
|
14
|
+
readonly large: number;
|
|
15
|
+
/** The most appended frames one pass found. */
|
|
16
|
+
readonly maxAppended: number;
|
|
13
17
|
}
|
|
14
18
|
/**
|
|
15
19
|
* The members of a `Worker` this class touches. Structural, so the unit spec
|
|
@@ -35,6 +39,7 @@ export interface WalCheckpointerOptions {
|
|
|
35
39
|
readonly dbPath: string;
|
|
36
40
|
readonly logger: IScopedLogger;
|
|
37
41
|
readonly pollMs?: number;
|
|
42
|
+
readonly maxDelayMs?: number;
|
|
38
43
|
readonly slowMs?: number;
|
|
39
44
|
readonly summaryIntervalMs?: number;
|
|
40
45
|
/** Overrides the built entry — a spec points this at the TypeScript source. */
|
|
@@ -47,6 +52,7 @@ export declare class WalCheckpointer {
|
|
|
47
52
|
private readonly dbPath;
|
|
48
53
|
private readonly logger;
|
|
49
54
|
private readonly pollMs;
|
|
55
|
+
private readonly maxDelayMs;
|
|
50
56
|
private readonly slowMs;
|
|
51
57
|
private readonly summaryIntervalMs;
|
|
52
58
|
private readonly spawn;
|
|
@@ -160,20 +160,6 @@ export declare class SystemBackupService {
|
|
|
160
160
|
signal?: AbortSignal;
|
|
161
161
|
onArchiveBytes?: (bytesWritten: number) => void;
|
|
162
162
|
}): Promise<CreateArchiveResult>;
|
|
163
|
-
/**
|
|
164
|
-
* Best-effort `wal_checkpoint(TRUNCATE)` on every live SQLite database
|
|
165
|
-
* under the locations about to be archived. Discovers a DB by its
|
|
166
|
-
* `-wal` sidecar (only WAL-mode DBs have one, and only those need
|
|
167
|
-
* folding), opens a short-lived second connection — SQLite permits
|
|
168
|
-
* concurrent connections, and a checkpoint is a supported concurrent
|
|
169
|
-
* operation against the hub's live writer — checkpoints, and closes.
|
|
170
|
-
*
|
|
171
|
-
* Every failure is swallowed: a `SQLITE_BUSY`, a locked file, a
|
|
172
|
-
* missing native binding, or a non-DB `.db` file must not fail the
|
|
173
|
-
* backup. A residual WAL still restores correctly; the checkpoint is a
|
|
174
|
-
* consistency *improvement*, not a precondition.
|
|
175
|
-
*/
|
|
176
|
-
private checkpointDatabasesUnder;
|
|
177
163
|
/**
|
|
178
164
|
* without extracting payload files. Streams the tar.gz, parses the
|
|
179
165
|
* one entry we care about, then aborts.
|
|
@@ -213,9 +199,8 @@ export declare class SystemBackupService {
|
|
|
213
199
|
private getRestoreMarkerPath;
|
|
214
200
|
/**
|
|
215
201
|
* Absolute-path form of {@link isExcludedFromBackup}, anchored on
|
|
216
|
-
* this service's dataDir. Shared by the manifest walk
|
|
217
|
-
* stats
|
|
218
|
-
* filter about what a backup contains.
|
|
202
|
+
* this service's dataDir. Shared by the manifest walk and the location
|
|
203
|
+
* stats so both agree with the tar filter about what a backup contains.
|
|
219
204
|
*/
|
|
220
205
|
private exclusionPredicate;
|
|
221
206
|
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
//#region src/builtins/sqlite-storage/wal-checkpoint-policy.ts
|
|
2
|
+
/**
|
|
3
|
+
* When the checkpoint worker folds the WAL — the judgement, pure.
|
|
4
|
+
*
|
|
5
|
+
* ## Why a worker checkpoints at all
|
|
6
|
+
*
|
|
7
|
+
* `hub/sqlite-settings` is one JS thread with a synchronous binding, and every
|
|
8
|
+
* configuration read in the cluster queues behind whatever statement it is
|
|
9
|
+
* inside. With `journal_mode = WAL` and the build default `synchronous =
|
|
10
|
+
* NORMAL`, an ordinary commit is an append to the WAL with NO fsync. The
|
|
11
|
+
* fsyncs — the only thing in a one-row write that can take seconds — happen
|
|
12
|
+
* at the CHECKPOINT: the WAL is synced before the pages are copied, the
|
|
13
|
+
* database file after. And SQLite runs the auto-checkpoint (1000 pages)
|
|
14
|
+
* INSIDE the commit of whichever writer crosses the threshold.
|
|
15
|
+
*
|
|
16
|
+
* Measured on the hub, 2026-09-11 10:45–10:55 CEST, from outside the process
|
|
17
|
+
* (`/proc/<pid>/syscall` at 10 Hz + the wal-index header at 1 Hz, D452): the
|
|
18
|
+
* WAL refilled at ~54 frames/s, the auto-checkpoint fired every 12–25 s (42
|
|
19
|
+
* in 9.5 min), and every D-state run on the settings thread that sat in a
|
|
20
|
+
* syscall sat in `fsync` (74), each within ±2 s of a checkpoint event —
|
|
21
|
+
* 12.6 s of the 569 s window, all of it inside writers' commits. In the two
|
|
22
|
+
* saturated bursts earlier that morning (08:35–08:40, 09:30–09:39) the same
|
|
23
|
+
* fsync took 1–10 s, once per checkpoint, and the profiler filed each as a
|
|
24
|
+
* slow `set`. `synchronous` was never the lever: it was already NORMAL, and
|
|
25
|
+
* `sqlite-pragmas.ts` had said so since 2026-08-08.
|
|
26
|
+
*
|
|
27
|
+
* So the checkpoint runs on a worker thread, over its own connection. The
|
|
28
|
+
* settings thread's commits stay appends. What stays on the settings thread
|
|
29
|
+
* is the WAL-header sync SQLite performs when a WAL is reused after a
|
|
30
|
+
* completed checkpoint — one small fsync per cycle — and that residual is
|
|
31
|
+
* named here so nobody reads "no fsync on the settings thread" into this.
|
|
32
|
+
*
|
|
33
|
+
* ## The rule this file reproduces
|
|
34
|
+
*
|
|
35
|
+
* The auto-checkpoint's own rule: fold when 1000 frames are pending. The
|
|
36
|
+
* point of moving the checkpoint is to change WHO waits on the disk, not how
|
|
37
|
+
* often the disk is asked to sync — a worker that folded every second would
|
|
38
|
+
* multiply the fsync count on a drive the media plane already saturates.
|
|
39
|
+
*
|
|
40
|
+
* ## How the worker knows how much is pending — and how it must NOT learn it
|
|
41
|
+
*
|
|
42
|
+
* Until 2026-09-29 the worker read the wal-index header straight out of the
|
|
43
|
+
* `-shm` file, once a second, with `openSync` / `readSync` / `closeSync`. That
|
|
44
|
+
* `closeSync` was the SIGBUS of D468 (corrected by D679): on POSIX, closing
|
|
45
|
+
* ANY descriptor of a file drops EVERY fcntl lock the process holds on that
|
|
46
|
+
* file — including the shared lock on `-shm` byte 128 (the "DMS" lock) that
|
|
47
|
+
* SQLite takes for the life of a connection and that tells every OTHER
|
|
48
|
+
* process "a live connection is using this wal-index, do not rebuild it". With
|
|
49
|
+
* the lock gone, the next process to open the database believed it was
|
|
50
|
+
* first, truncated and re-initialised the `-shm` under the runner's mappings,
|
|
51
|
+
* and the runner's next write past a 32 KiB wal-index region hit a page
|
|
52
|
+
* beyond end-of-file.
|
|
53
|
+
*
|
|
54
|
+
* So the worker never touches a SQLite file except through its connection.
|
|
55
|
+
* What it knows about the WAL it learns from the checkpoint it just ran:
|
|
56
|
+
* `PRAGMA wal_checkpoint(PASSIVE)` answers `log` (frames in the WAL) and
|
|
57
|
+
* `checkpointed` (frames now backfilled). Two consecutive answers give the
|
|
58
|
+
* frames appended between them, hence a rate, hence WHEN the next 1000 frames
|
|
59
|
+
* will have accumulated. The next checkpoint is scheduled for then —
|
|
60
|
+
* {@link nextCheckpointPlan} — clamped between the worker's poll interval and
|
|
61
|
+
* {@link WAL_CHECKPOINT_MAX_DELAY_MS}. A PASSIVE with nothing to fold performs
|
|
62
|
+
* no fsync (the copy and both syncs sit behind `nBackfill < mxFrame` in
|
|
63
|
+
* `wal.c`), so the clamp costs a quiet database nothing, and it bounds how
|
|
64
|
+
* long a burst that follows a quiet spell goes unseen.
|
|
65
|
+
*/
|
|
66
|
+
/** The engine's own `wal_autocheckpoint` default, in frames. Reproduced, not changed. */
|
|
67
|
+
var WAL_CHECKPOINT_THRESHOLD_FRAMES = 1e3;
|
|
68
|
+
/**
|
|
69
|
+
* Longest the worker waits between two checkpoints, in ms, whatever the
|
|
70
|
+
* measured rate says. It is the bound on how late a burst after a steady or
|
|
71
|
+
* quiet spell is noticed, and it is sized against the engine's own fallback
|
|
72
|
+
* (`SQLITE_WAL_AUTOCHECKPOINT_BOUND_PAGES` = 10 000 frames), which runs INSIDE
|
|
73
|
+
* a settings-thread commit — the stall D452 moved off that thread. No burst
|
|
74
|
+
* rate has been measured on the hub (D452's 54 frames/s is the steady rate),
|
|
75
|
+
* so the design point is 1 000 frames/s, ~18× steady: 8 s of it is 8 000
|
|
76
|
+
* frames, under the fallback, so the worker looks before the engine has to.
|
|
77
|
+
* A pass that finds more than {@link WAL_CHECKPOINT_LARGE_PASS_FRAMES} is
|
|
78
|
+
* reported so this number can be tuned from the field.
|
|
79
|
+
*
|
|
80
|
+
* The cost, stated against D452: at the steady 54 frames/s the threshold
|
|
81
|
+
* would fold every ~18.5 s; this clamp folds every 8 s, ~2.3× the fsync pairs
|
|
82
|
+
* (≈ 432 frames per fold instead of 1 000). They are paid on the WORKER, a
|
|
83
|
+
* thread nobody waits on — the settings thread's commits stay appends. A pass
|
|
84
|
+
* with nothing pending does no fsync at all.
|
|
85
|
+
*/
|
|
86
|
+
var WAL_CHECKPOINT_MAX_DELAY_MS = 8e3;
|
|
87
|
+
/**
|
|
88
|
+
* A pass that folded more than this many appended frames is reported (WARN,
|
|
89
|
+
* sampled: the first of each summary window, the rest counted). Half the
|
|
90
|
+
* engine's fallback bound: a gap that let this much accumulate was within 2×
|
|
91
|
+
* of handing the checkpoint back to the settings thread.
|
|
92
|
+
*/
|
|
93
|
+
var WAL_CHECKPOINT_LARGE_PASS_FRAMES = 5e3;
|
|
94
|
+
/** Shortest gap between two checkpoints, in ms — the worker's poll interval. */
|
|
95
|
+
var WAL_CHECKPOINT_POLL_MS = 1e3;
|
|
96
|
+
/**
|
|
97
|
+
* A checkpoint that took this long is reported at WARN with its numbers.
|
|
98
|
+
* Matched to `SQLITE_SLOW_CALL_MS`: it is the same "the disk held us" event
|
|
99
|
+
* the profiler used to file as a slow `set`, now on the thread that paid it.
|
|
100
|
+
*/
|
|
101
|
+
var WAL_CHECKPOINT_SLOW_MS = 1e3;
|
|
102
|
+
/**
|
|
103
|
+
* Frames appended to the WAL between two checkpoints.
|
|
104
|
+
*
|
|
105
|
+
* When the previous checkpoint backfilled EVERYTHING, the next writer restarts
|
|
106
|
+
* the WAL from frame 1, so every frame now in it is new. When it did not (a
|
|
107
|
+
* reader pinned a snapshot), the WAL kept growing and the difference is what
|
|
108
|
+
* was added — unless it shrank, which only a restart can do.
|
|
109
|
+
*
|
|
110
|
+
* An IDLE WAL is the exception to "complete means restarted": the WAL restarts
|
|
111
|
+
* only at the next write, so a fully checkpointed WAL nobody writes answers
|
|
112
|
+
* the same `log == checkpointed` pass after pass. Identical answers after a
|
|
113
|
+
* complete checkpoint are therefore 0 appended, not the whole WAL again —
|
|
114
|
+
* otherwise an idle node would be scheduled at the poll interval forever and
|
|
115
|
+
* every summary would count the same frames once per pass. (A restart that
|
|
116
|
+
* appended exactly as many frames as the previous cycle is read as idle too;
|
|
117
|
+
* it is scheduled at the max delay, which is the bounded case anyway.)
|
|
118
|
+
*
|
|
119
|
+
* The one remaining ambiguity — a complete checkpoint followed by writes the
|
|
120
|
+
* writer could NOT restart the WAL for — counts old frames as new, which
|
|
121
|
+
* over-estimates the rate and checkpoints EARLY: the safe direction. `-1`
|
|
122
|
+
* (not a WAL database, or a busy pass) reads as 0.
|
|
123
|
+
*/
|
|
124
|
+
function framesAppendedBetween(previous, current) {
|
|
125
|
+
const prevWal = Math.max(0, previous.walFrames);
|
|
126
|
+
const prevDone = Math.max(0, previous.checkpointedFrames);
|
|
127
|
+
const curWal = Math.max(0, current.walFrames);
|
|
128
|
+
const curDone = Math.max(0, current.checkpointedFrames);
|
|
129
|
+
if (prevDone >= prevWal) {
|
|
130
|
+
if (curWal === prevWal && curDone === prevDone) return 0;
|
|
131
|
+
return curWal;
|
|
132
|
+
}
|
|
133
|
+
return curWal >= prevWal ? curWal - prevWal : curWal;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* What the worker "saw" before its first checkpoint: nothing, at the moment it
|
|
137
|
+
* started. Against it, the first checkpoint counts every frame then in the WAL
|
|
138
|
+
* as appended since the start — which over-estimates the rate when the WAL
|
|
139
|
+
* held older frames, and so schedules the second checkpoint EARLY: the safe
|
|
140
|
+
* direction, and the second one measures the real rate.
|
|
141
|
+
*/
|
|
142
|
+
function workerStartObservation(at) {
|
|
143
|
+
return {
|
|
144
|
+
at,
|
|
145
|
+
walFrames: 0,
|
|
146
|
+
checkpointedFrames: 0
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* When to run the next checkpoint: the moment the measured rate says
|
|
151
|
+
* {@link WAL_CHECKPOINT_THRESHOLD_FRAMES} more frames will be pending,
|
|
152
|
+
* clamped to `[minDelayMs, maxDelayMs]`. Nothing appended since the last one
|
|
153
|
+
* is `maxDelayMs`.
|
|
154
|
+
*/
|
|
155
|
+
function nextCheckpointPlan(input) {
|
|
156
|
+
const { previous, current, minDelayMs, maxDelayMs } = input;
|
|
157
|
+
const appendedFrames = framesAppendedBetween(previous, current);
|
|
158
|
+
const elapsedMs = Math.max(1, current.at - previous.at);
|
|
159
|
+
if (appendedFrames === 0) return {
|
|
160
|
+
appendedFrames,
|
|
161
|
+
nextDelayMs: maxDelayMs
|
|
162
|
+
};
|
|
163
|
+
const untilThresholdMs = WAL_CHECKPOINT_THRESHOLD_FRAMES / (appendedFrames / elapsedMs);
|
|
164
|
+
return {
|
|
165
|
+
appendedFrames,
|
|
166
|
+
nextDelayMs: Math.round(Math.min(maxDelayMs, Math.max(minDelayMs, untilThresholdMs)))
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function isRecord(value) {
|
|
170
|
+
return typeof value === "object" && value !== null;
|
|
171
|
+
}
|
|
172
|
+
/** Narrow a message off the worker port without a cast. */
|
|
173
|
+
function isWorkerMessage(value) {
|
|
174
|
+
if (!isRecord(value)) return false;
|
|
175
|
+
switch (value["kind"]) {
|
|
176
|
+
case "ready": return true;
|
|
177
|
+
case "failed": return typeof value["error"] === "string";
|
|
178
|
+
case "checkpointed": return (value["reason"] === "first" || value["reason"] === "scheduled") && typeof value["appendedFrames"] === "number" && typeof value["nextDelayMs"] === "number" && typeof value["walFrames"] === "number" && typeof value["checkpointedFrames"] === "number" && typeof value["busy"] === "boolean" && typeof value["ms"] === "number";
|
|
179
|
+
default: return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function isWorkerStop(value) {
|
|
183
|
+
return isRecord(value) && value["kind"] === "stop";
|
|
184
|
+
}
|
|
185
|
+
//#endregion
|
|
186
|
+
export { isWorkerMessage as a, workerStartObservation as c, WAL_CHECKPOINT_SLOW_MS as i, WAL_CHECKPOINT_MAX_DELAY_MS as n, isWorkerStop as o, WAL_CHECKPOINT_POLL_MS as r, nextCheckpointPlan as s, WAL_CHECKPOINT_LARGE_PASS_FRAMES as t };
|