@ultimat3/testing 31.28.0 → 31.29.0
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/README.md +2 -1
- package/package.json +22 -22
- package/src/drill-fleet.ts +56 -31
- package/src/drill-output.ts +41 -0
- package/src/drill-rollout-verdict.ts +82 -0
- package/src/drill-rollout.ts +91 -0
- package/src/drill-run.ts +2 -0
- package/src/drill-verdict.ts +12 -1
- package/src/drills.ts +24 -8
- package/src/index.ts +5 -2
package/README.md
CHANGED
|
@@ -58,8 +58,9 @@ unitTest('the clock moves only when the test moves it', ({ clock }) => {
|
|
|
58
58
|
| `job-crash-proof.ts` | `crashProof(handle, input, { context?, seed? })` — one clean run, then the worker killed at every step boundary it crossed, then each answer lost; `{ boundaries, divergences, reruns }`, a divergence carrying its `X_JOB_CRASH_DIVERGED` |
|
|
59
59
|
| `job-simulate.ts` | `simulateJobs({ seed?, seeds, jobs, workers, faults, invariant })` — seeded races on the shared simulated driver; `X_JOB_SIM_INVARIANT`, `X_JOB_SIM_NONDETERMINISTIC`; `x test job --seed <n>` replays one |
|
|
60
60
|
| `fault-proxy.ts` | `faultProxy({ target })` — a Bun TCP proxy whose mode switches mid-run: `pass`, `delay(ms)`, `blackhole` (bytes held, never dropped), `reset`, `refuse` |
|
|
61
|
-
| `drills.ts` · `drill-verdict.ts` | the
|
|
61
|
+
| `drills.ts` · `drill-verdict.ts` | the nine fault drills (`x drill`) and the verdict over evidence alone; a property with no evidence is red, and an answer may reach the load `DRILL_ANSWER_SLACK_MS` (500) past the deadline it was given by |
|
|
62
62
|
| `drill-fleet.ts` · `drill-run.ts` | `startDrillFleet` — `web`, `worker` (and `sync` for `nats-down`) as processes on a scratch database, every service behind a proxy; `runDrill` — baseline, fault, recovery, judged |
|
|
63
|
+
| `drill-rollout.ts` · `drill-rollout-verdict.ts` · `drill-output.ts` | `roll-before-migrate`: `startDrillFleet({ beforeMigrate: { rewind, watch } })` migrates, runs the caller's `rewind`, starts `web`, `worker` and `scheduler` unready and counts each output line naming `watch`; `runRollout` waits for every role to meet the schema, runs `fleet.rollout.migrate()`, asks each `/readyz` — `roles-wait`, `serves-after-migrate`, `one-wait-line` |
|
|
63
64
|
| `drill-load.ts` · `drill-writes.ts` | the load's lanes; `drillWriteTargets` — every action an anonymous POST reaches, each request a fresh seeded draw with the app's `exercise.ts` override, so a job enqueued over HTTP is on the table for `worker-sigkill`/`worker-sigterm` |
|
|
64
65
|
| `drill-sync.ts` | `startDrillSync` — the sync-client lane: `hello`, resubscribe, redial on a drop, every `replay-gap` counted; `sync-resumes` reads it |
|
|
65
66
|
| `tenant-proof-report.ts` | `tenantProofFindings` — one coded finding per crossing (`X_TENANT_LEAK`, `X_TENANT_WRITE_CROSSED`), `tenantFindingLines` the human lines. Paths, tables and seeded ids, never a bound value |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/testing",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.29.0",
|
|
4
4
|
"description": "Test harness: cloned template DBs per worker, frozen clock, sealed network, 6 test types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -47,27 +47,27 @@
|
|
|
47
47
|
"test": "bun test"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@ultimat3/action": "31.
|
|
51
|
-
"@ultimat3/ai": "31.
|
|
52
|
-
"@ultimat3/auth": "31.
|
|
53
|
-
"@ultimat3/cache": "31.
|
|
54
|
-
"@ultimat3/core": "31.
|
|
55
|
-
"@ultimat3/db": "31.
|
|
56
|
-
"@ultimat3/entity": "31.
|
|
57
|
-
"@ultimat3/http": "31.
|
|
58
|
-
"@ultimat3/i18n": "31.
|
|
59
|
-
"@ultimat3/jobs": "31.
|
|
60
|
-
"@ultimat3/mail": "31.
|
|
61
|
-
"@ultimat3/notify": "31.
|
|
62
|
-
"@ultimat3/policy": "31.
|
|
63
|
-
"@ultimat3/pwa": "31.
|
|
64
|
-
"@ultimat3/query": "31.
|
|
65
|
-
"@ultimat3/realtime": "31.
|
|
66
|
-
"@ultimat3/render": "31.
|
|
67
|
-
"@ultimat3/schema": "31.
|
|
68
|
-
"@ultimat3/scraping": "31.
|
|
69
|
-
"@ultimat3/seo": "31.
|
|
70
|
-
"@ultimat3/time": "31.
|
|
50
|
+
"@ultimat3/action": "31.29.0",
|
|
51
|
+
"@ultimat3/ai": "31.29.0",
|
|
52
|
+
"@ultimat3/auth": "31.29.0",
|
|
53
|
+
"@ultimat3/cache": "31.29.0",
|
|
54
|
+
"@ultimat3/core": "31.29.0",
|
|
55
|
+
"@ultimat3/db": "31.29.0",
|
|
56
|
+
"@ultimat3/entity": "31.29.0",
|
|
57
|
+
"@ultimat3/http": "31.29.0",
|
|
58
|
+
"@ultimat3/i18n": "31.29.0",
|
|
59
|
+
"@ultimat3/jobs": "31.29.0",
|
|
60
|
+
"@ultimat3/mail": "31.29.0",
|
|
61
|
+
"@ultimat3/notify": "31.29.0",
|
|
62
|
+
"@ultimat3/policy": "31.29.0",
|
|
63
|
+
"@ultimat3/pwa": "31.29.0",
|
|
64
|
+
"@ultimat3/query": "31.29.0",
|
|
65
|
+
"@ultimat3/realtime": "31.29.0",
|
|
66
|
+
"@ultimat3/render": "31.29.0",
|
|
67
|
+
"@ultimat3/schema": "31.29.0",
|
|
68
|
+
"@ultimat3/scraping": "31.29.0",
|
|
69
|
+
"@ultimat3/seo": "31.29.0",
|
|
70
|
+
"@ultimat3/time": "31.29.0"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
73
|
"solid-js": "^1.9.18"
|
package/src/drill-fleet.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// A drill's fleet: the app's `web` and `worker` roles as separate processes (`apps/web/server.ts`
|
|
2
2
|
// under `ROLE`), on a scratch database it creates and drops, each dialling Postgres, Redis and
|
|
3
3
|
// NATS through a fault proxy of its own. The proxies are what the drill switches; the roles are
|
|
4
|
-
// what it signals. Nothing here judges
|
|
4
|
+
// what it signals; `beforeMigrate` starts the roles ahead of migrate instead. Nothing here judges.
|
|
5
5
|
|
|
6
6
|
// why: Bun ships no temp-directory primitive or recursive remove; `tmpdir()` is node:os's alone.
|
|
7
7
|
import { mkdtemp, rm } from 'node:fs/promises';
|
|
@@ -9,6 +9,7 @@ import { mkdtemp, rm } from 'node:fs/promises';
|
|
|
9
9
|
import { tmpdir } from 'node:os';
|
|
10
10
|
// why: Bun exposes no path API — the state dir is joined, not concatenated.
|
|
11
11
|
import { join } from 'node:path';
|
|
12
|
+
import { tailOf } from './drill-output';
|
|
12
13
|
import { probeStatus } from './drill-probe';
|
|
13
14
|
import type { DrillRole, DrillService } from './drills';
|
|
14
15
|
import { E2eAppFailedError } from './e2e-errors';
|
|
@@ -33,6 +34,22 @@ export interface DrillFleetOptions {
|
|
|
33
34
|
readonly readyTimeoutMs: number;
|
|
34
35
|
/** Extra environment for every role. */
|
|
35
36
|
readonly env?: Readonly<Record<string, string>> | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The rollout-order drill: after the first migrate, `rewind` runs on the scratch database (the
|
|
39
|
+
* caller's statement — the stamp is the CLI's to write), and the roles start WITHOUT waiting for
|
|
40
|
+
* readiness, each line of their output naming `watch` counted. The fleet then has a `rollout`.
|
|
41
|
+
*/
|
|
42
|
+
readonly beforeMigrate?: { readonly rewind: string; readonly watch: string } | undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What a fleet started `beforeMigrate` adds: this release's migrate, and what each role did. */
|
|
46
|
+
export interface DrillRollout {
|
|
47
|
+
/** `ROLE=migrate` on the direct URL; resolves once it exited 0, else the refusal is thrown. */
|
|
48
|
+
migrate(): Promise<void>;
|
|
49
|
+
/** The role's exit code, or `null` while its process runs. */
|
|
50
|
+
exitCode(role: DrillRole): number | null;
|
|
51
|
+
/** Output lines naming the fleet's `watch`, counted as they streamed — never cut by a tail. */
|
|
52
|
+
watched(role: DrillRole): number;
|
|
36
53
|
}
|
|
37
54
|
|
|
38
55
|
export interface DrillRoleProcess {
|
|
@@ -55,11 +72,12 @@ export interface DrillFleet {
|
|
|
55
72
|
readonly databaseUrl: string;
|
|
56
73
|
readonly proxies: ReadonlyMap<DrillService, FaultProxy>;
|
|
57
74
|
readonly roles: ReadonlyMap<DrillRole, DrillRoleProcess>;
|
|
75
|
+
/** Present when started with `beforeMigrate`. */
|
|
76
|
+
readonly rollout?: DrillRollout | undefined;
|
|
58
77
|
stop(): Promise<void>;
|
|
59
78
|
}
|
|
60
79
|
|
|
61
80
|
const SERVER = 'apps/web/server.ts';
|
|
62
|
-
const TAIL_CHARS = 8_000;
|
|
63
81
|
const KILL_GRACE_MS = 5_000;
|
|
64
82
|
|
|
65
83
|
/** This process's environment minus every binding the fleet sets itself, and minus `NODE_ENV`. */
|
|
@@ -74,19 +92,6 @@ const inheritedEnv = (): Record<string, string | undefined> => {
|
|
|
74
92
|
return rest;
|
|
75
93
|
};
|
|
76
94
|
|
|
77
|
-
function tailOf(child: Bun.Subprocess<'ignore', 'pipe', 'pipe'>): () => string {
|
|
78
|
-
let text = '';
|
|
79
|
-
for (const stream of [child.stdout, child.stderr]) {
|
|
80
|
-
void (async () => {
|
|
81
|
-
const decoder = new TextDecoder();
|
|
82
|
-
for await (const chunk of stream) {
|
|
83
|
-
text = (text + decoder.decode(chunk, { stream: true })).slice(-TAIL_CHARS);
|
|
84
|
-
}
|
|
85
|
-
})().catch(() => undefined);
|
|
86
|
-
}
|
|
87
|
-
return () => text;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
95
|
const refuse = (step: string, output: string): E2eAppFailedError =>
|
|
91
96
|
new E2eAppFailedError({
|
|
92
97
|
step,
|
|
@@ -175,16 +180,24 @@ export async function startDrillFleet(options: DrillFleetOptions): Promise<Drill
|
|
|
175
180
|
return child;
|
|
176
181
|
};
|
|
177
182
|
// The release phase on the DIRECT url: migrations are not what any drill faults.
|
|
178
|
-
const migrate =
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
183
|
+
const migrate = async (): Promise<void> => {
|
|
184
|
+
const job = spawnRole({ ROLE: 'migrate', DATABASE_URL: database.url });
|
|
185
|
+
const output = tailOf([job.stdout, job.stderr]);
|
|
186
|
+
// Bounded like a role's readiness: a wedged migration is a refusal, and `stop` SIGKILLs
|
|
187
|
+
// it — it is still in `children`.
|
|
188
|
+
const migrated = await Promise.race([
|
|
189
|
+
job.exited,
|
|
190
|
+
Bun.sleep(options.readyTimeoutMs).then(() => null),
|
|
191
|
+
]);
|
|
192
|
+
if (migrated !== 0) throw refuse('ROLE=migrate', output.log());
|
|
193
|
+
children.delete(job);
|
|
194
|
+
};
|
|
195
|
+
await migrate();
|
|
196
|
+
const before = options.beforeMigrate;
|
|
197
|
+
if (before !== undefined) {
|
|
198
|
+
const direct = new Bun.SQL(database.url);
|
|
199
|
+
await direct.unsafe(before.rewind).finally(() => direct.close());
|
|
200
|
+
}
|
|
188
201
|
|
|
189
202
|
const taken: number[] = [...[...proxies.values()].map((proxy) => proxy.port)];
|
|
190
203
|
const take = (): number => {
|
|
@@ -193,6 +206,8 @@ export async function startDrillFleet(options: DrillFleetOptions): Promise<Drill
|
|
|
193
206
|
return port;
|
|
194
207
|
};
|
|
195
208
|
let webBase = '';
|
|
209
|
+
// Each role's CURRENT process, read through: what the rollout reports on.
|
|
210
|
+
const outputs = new Map<DrillRole, { exitCode(): number | null; watched(): number }>();
|
|
196
211
|
for (const role of options.roles) {
|
|
197
212
|
const port = take();
|
|
198
213
|
// The sync node listens on PORT + 1 (`syncPortFor`): held too, so no other role is handed it.
|
|
@@ -209,16 +224,18 @@ export async function startDrillFleet(options: DrillFleetOptions): Promise<Drill
|
|
|
209
224
|
const readyz =
|
|
210
225
|
role === 'web' ? `${origin}/readyz` : `http://127.0.0.1:${String(metricsPort)}/readyz`;
|
|
211
226
|
let child = spawnRole(env);
|
|
212
|
-
let
|
|
227
|
+
let output = tailOf([child.stdout, child.stderr], before?.watch);
|
|
213
228
|
const boot = async (): Promise<void> => {
|
|
214
229
|
const ready = await waitReady(
|
|
215
230
|
readyz,
|
|
216
231
|
options.readyTimeoutMs,
|
|
217
232
|
() => child.exitCode === null,
|
|
218
233
|
);
|
|
219
|
-
if (!ready) throw refuse(`ROLE=${role} never answered ${readyz}`, log());
|
|
234
|
+
if (!ready) throw refuse(`ROLE=${role} never answered ${readyz}`, output.log());
|
|
220
235
|
};
|
|
221
|
-
|
|
236
|
+
// Before migrate, a role is not ready by design: what it does instead is the drill's evidence.
|
|
237
|
+
if (before === undefined) await boot();
|
|
238
|
+
outputs.set(role, { exitCode: () => child.exitCode, watched: () => output.watched() });
|
|
222
239
|
roles.set(role, {
|
|
223
240
|
role,
|
|
224
241
|
origin,
|
|
@@ -228,13 +245,21 @@ export async function startDrillFleet(options: DrillFleetOptions): Promise<Drill
|
|
|
228
245
|
async respawn() {
|
|
229
246
|
children.delete(child);
|
|
230
247
|
child = spawnRole(env);
|
|
231
|
-
|
|
248
|
+
output = tailOf([child.stdout, child.stderr], before?.watch);
|
|
232
249
|
await boot();
|
|
233
250
|
},
|
|
234
|
-
log: () => log(),
|
|
251
|
+
log: () => output.log(),
|
|
235
252
|
});
|
|
236
253
|
}
|
|
237
|
-
|
|
254
|
+
const rollout: DrillRollout | undefined =
|
|
255
|
+
before === undefined
|
|
256
|
+
? undefined
|
|
257
|
+
: {
|
|
258
|
+
migrate,
|
|
259
|
+
exitCode: (role) => outputs.get(role)?.exitCode() ?? null,
|
|
260
|
+
watched: (role) => outputs.get(role)?.watched() ?? 0,
|
|
261
|
+
};
|
|
262
|
+
return { base: webBase, databaseUrl: database.url, proxies, roles, rollout, stop };
|
|
238
263
|
} catch (error) {
|
|
239
264
|
await stop();
|
|
240
265
|
throw error;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// A spawned role's output, as the drill reads it: the last of it, for a refusal's evidence, and a
|
|
2
|
+
// count of the whole lines naming one string, kept as they stream so no tail can cut one off.
|
|
3
|
+
|
|
4
|
+
const TAIL_CHARS = 8_000;
|
|
5
|
+
|
|
6
|
+
export interface DrillOutput {
|
|
7
|
+
log(): string;
|
|
8
|
+
/** Whole lines naming `watch`, per stream, the last unterminated one counted at its end. */
|
|
9
|
+
watched(): number;
|
|
10
|
+
/** Settles once every stream has been read to its end. */
|
|
11
|
+
ended(): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Read every stream to its end in the background; the answer reads what has arrived so far. */
|
|
15
|
+
export function tailOf(
|
|
16
|
+
streams: readonly ReadableStream<Uint8Array>[],
|
|
17
|
+
watch?: string,
|
|
18
|
+
): DrillOutput {
|
|
19
|
+
let text = '';
|
|
20
|
+
let watched = 0;
|
|
21
|
+
const count = (line: string): void => {
|
|
22
|
+
if (watch !== undefined && line.includes(watch)) watched += 1;
|
|
23
|
+
};
|
|
24
|
+
const readers = streams.map((stream) =>
|
|
25
|
+
(async () => {
|
|
26
|
+
const decoder = new TextDecoder();
|
|
27
|
+
let partial = '';
|
|
28
|
+
for await (const chunk of stream) {
|
|
29
|
+
const decoded = decoder.decode(chunk, { stream: true });
|
|
30
|
+
text = (text + decoded).slice(-TAIL_CHARS);
|
|
31
|
+
const lines = (partial + decoded).split('\n');
|
|
32
|
+
partial = lines.pop() ?? '';
|
|
33
|
+
for (const line of lines) count(line);
|
|
34
|
+
}
|
|
35
|
+
count(partial);
|
|
36
|
+
// A stream that errors ends the read: what arrived before it is still the evidence.
|
|
37
|
+
})().catch(() => undefined),
|
|
38
|
+
);
|
|
39
|
+
const ended = Promise.all(readers).then(() => undefined);
|
|
40
|
+
return { log: () => text, watched: () => watched, ended: () => ended };
|
|
41
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// The rollout-order drill's three properties, judged over what `runRollout` measured per role: it
|
|
2
|
+
// stayed up while the schema was behind, it served once migrate ran, and it said it waited exactly
|
|
3
|
+
// once. Pure, beside `drill-verdict.ts`, which holds every judge in its one table.
|
|
4
|
+
|
|
5
|
+
import type { DrillEvidence, DrillFailure } from './drill-verdict';
|
|
6
|
+
|
|
7
|
+
/** One role through the rollout: started before migrate, asked for `/readyz` after it. */
|
|
8
|
+
export interface DrillRolloutRole {
|
|
9
|
+
readonly role: string;
|
|
10
|
+
/** The exit code when the process ended before it answered ready; `null` while it stayed up. */
|
|
11
|
+
readonly exit: number | null;
|
|
12
|
+
/** `/readyz` answered 200 once migrate had run. */
|
|
13
|
+
readonly ready: boolean;
|
|
14
|
+
/** Output lines naming the drill's wait code, counted as they streamed. */
|
|
15
|
+
readonly waitLines: number;
|
|
16
|
+
/** The last of the role's output: where a refusal is. */
|
|
17
|
+
readonly logTail: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface DrillRolloutEvidence {
|
|
21
|
+
readonly roles: readonly DrillRolloutRole[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type RolloutJudge = (evidence: DrillEvidence) => DrillFailure | undefined;
|
|
25
|
+
|
|
26
|
+
const NO_ROLES = 'no evidence: no role was started before migrate';
|
|
27
|
+
|
|
28
|
+
/** The line a role exited on: its last line naming a code, else its last line. */
|
|
29
|
+
const lastWords = (log: string): string => {
|
|
30
|
+
const lines = log
|
|
31
|
+
.split('\n')
|
|
32
|
+
.map((line) => line.trim())
|
|
33
|
+
.filter((line) => line !== '');
|
|
34
|
+
return lines.findLast((line) => line.includes('X_')) ?? lines.at(-1) ?? '(no output)';
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** The roles measured, or `undefined` when there is nothing to judge. */
|
|
38
|
+
const rolesOf = (evidence: DrillEvidence): readonly DrillRolloutRole[] | undefined => {
|
|
39
|
+
const roles = evidence.rollout?.roles;
|
|
40
|
+
return roles === undefined || roles.length === 0 ? undefined : roles;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const judgeRolesWait: RolloutJudge = (evidence) => {
|
|
44
|
+
const expected = 'every role stays up while the schema is behind (no crash-loop)';
|
|
45
|
+
const roles = rolesOf(evidence);
|
|
46
|
+
if (roles === undefined) return { property: 'roles-wait', expected, observed: NO_ROLES };
|
|
47
|
+
const exited = roles.filter((role) => role.exit !== null);
|
|
48
|
+
if (exited.length === 0) return undefined;
|
|
49
|
+
return {
|
|
50
|
+
property: 'roles-wait',
|
|
51
|
+
expected,
|
|
52
|
+
observed: exited
|
|
53
|
+
.map((role) => `ROLE=${role.role} exited ${String(role.exit)}: ${lastWords(role.logTail)}`)
|
|
54
|
+
.join('; '),
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const judgeServesAfterMigrate: RolloutJudge = (evidence) => {
|
|
59
|
+
const expected = 'every role answers /readyz 200 once ROLE=migrate has run';
|
|
60
|
+
const roles = rolesOf(evidence);
|
|
61
|
+
if (roles === undefined)
|
|
62
|
+
return { property: 'serves-after-migrate', expected, observed: NO_ROLES };
|
|
63
|
+
const unready = roles.filter((role) => !role.ready);
|
|
64
|
+
if (unready.length === 0) return undefined;
|
|
65
|
+
return {
|
|
66
|
+
property: 'serves-after-migrate',
|
|
67
|
+
expected,
|
|
68
|
+
observed: `never ready: ${unready.map((role) => `ROLE=${role.role}`).join(', ')}`,
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const judgeOneWaitLine: RolloutJudge = (evidence) => {
|
|
73
|
+
const expected = 'exactly one log line per role naming the wait code while it waits';
|
|
74
|
+
const roles = rolesOf(evidence);
|
|
75
|
+
if (roles === undefined) return { property: 'one-wait-line', expected, observed: NO_ROLES };
|
|
76
|
+
if (roles.every((role) => role.waitLines === 1)) return undefined;
|
|
77
|
+
return {
|
|
78
|
+
property: 'one-wait-line',
|
|
79
|
+
expected,
|
|
80
|
+
observed: roles.map((role) => `ROLE=${role.role} ${String(role.waitLines)}`).join(', '),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// The rollout-order drill, played on a fleet started `beforeMigrate`: every role meets the behind
|
|
2
|
+
// schema — says so, or dies of it — is held a moment, then this release's migrate runs and each
|
|
3
|
+
// `/readyz` is asked until it answers. No load: the evidence is the roles', judged with the rest.
|
|
4
|
+
|
|
5
|
+
import { finiteCount } from '@ultimat3/core';
|
|
6
|
+
import type { DrillFleet } from './drill-fleet';
|
|
7
|
+
import { probeStatus } from './drill-probe';
|
|
8
|
+
import type { DrillRolloutRole } from './drill-rollout-verdict';
|
|
9
|
+
import type { DrillReport } from './drill-run';
|
|
10
|
+
import type { DrillBudgets, DrillEvidence } from './drill-verdict';
|
|
11
|
+
import { judgeDrill } from './drill-verdict';
|
|
12
|
+
import type { Drill } from './drills';
|
|
13
|
+
|
|
14
|
+
export interface DrillRolloutOptions {
|
|
15
|
+
readonly drill: Drill;
|
|
16
|
+
readonly fleet: DrillFleet;
|
|
17
|
+
readonly budgets: DrillBudgets;
|
|
18
|
+
/** A cold boot's bound: on the roles meeting the schema, and on each answering after migrate. */
|
|
19
|
+
readonly readyTimeoutMs: number;
|
|
20
|
+
/** How long every role must stay up once each has met the schema, before migrate starts. */
|
|
21
|
+
readonly holdMs?: number | undefined;
|
|
22
|
+
readonly probe?: ((url: string) => Promise<number | null>) | undefined;
|
|
23
|
+
readonly sleep?: ((ms: number) => Promise<void>) | undefined;
|
|
24
|
+
/** A monotonic clock, in ms: what every wait's bound is measured on. */
|
|
25
|
+
readonly now?: (() => number) | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const POLL_MS = 250;
|
|
29
|
+
const PROBE_BOUND_MS = 1_000;
|
|
30
|
+
/** How much of a role's output the evidence carries: its last lines, where a refusal is. */
|
|
31
|
+
const LOG_TAIL_CHARS = 4_000;
|
|
32
|
+
|
|
33
|
+
const NOTHING: Omit<DrillEvidence, 'rollout'> = {
|
|
34
|
+
requests: undefined,
|
|
35
|
+
readyz: undefined,
|
|
36
|
+
recoveryMs: undefined,
|
|
37
|
+
jobs: undefined,
|
|
38
|
+
exit: undefined,
|
|
39
|
+
sync: undefined,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** Play the rollout order on `fleet` and judge it. Never throws for a property that did not hold. */
|
|
43
|
+
export async function runRollout(options: DrillRolloutOptions): Promise<DrillReport> {
|
|
44
|
+
const { drill, fleet, budgets } = options;
|
|
45
|
+
const sleep = options.sleep ?? ((ms: number) => Bun.sleep(ms));
|
|
46
|
+
const now = options.now ?? (() => performance.now());
|
|
47
|
+
const probe = options.probe ?? ((url: string) => probeStatus(url, PROBE_BOUND_MS));
|
|
48
|
+
const boundMs = finiteCount('runRollout', 'readyTimeoutMs', options.readyTimeoutMs, 1);
|
|
49
|
+
// Elapsed on the clock, never a count of polls: a probe may itself take `PROBE_BOUND_MS`.
|
|
50
|
+
const until = async (done: () => boolean | Promise<boolean>): Promise<void> => {
|
|
51
|
+
const end = now() + boundMs;
|
|
52
|
+
while (now() < end) {
|
|
53
|
+
if (await done()) return;
|
|
54
|
+
await sleep(POLL_MS);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const report = (evidence: DrillEvidence): DrillReport => ({
|
|
58
|
+
name: drill.name,
|
|
59
|
+
verdict: judgeDrill(drill.holds, evidence, budgets),
|
|
60
|
+
evidence,
|
|
61
|
+
budgets,
|
|
62
|
+
baseline: { ok: 0, failed: 0 },
|
|
63
|
+
});
|
|
64
|
+
const rollout = fleet.rollout;
|
|
65
|
+
// A fleet started without `beforeMigrate` had its roles migrated first: nothing to measure.
|
|
66
|
+
if (rollout === undefined) return report(NOTHING);
|
|
67
|
+
const roles = [...fleet.roles.values()];
|
|
68
|
+
const exitOf = (role: (typeof roles)[number]) => rollout.exitCode(role.role);
|
|
69
|
+
await until(() => roles.every((role) => exitOf(role) !== null || rollout.watched(role.role) > 0));
|
|
70
|
+
await sleep(finiteCount('runRollout', 'holdMs', options.holdMs ?? 3_000));
|
|
71
|
+
await rollout.migrate();
|
|
72
|
+
const measured: DrillRolloutRole[] = [];
|
|
73
|
+
for (const role of roles) {
|
|
74
|
+
// A dead role ends its own wait: it will never answer.
|
|
75
|
+
let ready = false;
|
|
76
|
+
await until(async () => {
|
|
77
|
+
if (exitOf(role) !== null) return true;
|
|
78
|
+
ready = (await probe(role.readyz)) === 200;
|
|
79
|
+
return ready;
|
|
80
|
+
});
|
|
81
|
+
measured.push({
|
|
82
|
+
role: role.role,
|
|
83
|
+
// Read after the wait: a role that died during migrate crash-looped all the same.
|
|
84
|
+
exit: ready ? null : exitOf(role),
|
|
85
|
+
ready,
|
|
86
|
+
waitLines: rollout.watched(role.role),
|
|
87
|
+
logTail: role.log().slice(-LOG_TAIL_CHARS),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return report({ ...NOTHING, rollout: { roles: measured } });
|
|
91
|
+
}
|
package/src/drill-run.ts
CHANGED
|
@@ -66,6 +66,8 @@ async function play(
|
|
|
66
66
|
await proxy.set({ kind: 'pass' });
|
|
67
67
|
return { clearedMs: now(), exit: undefined };
|
|
68
68
|
}
|
|
69
|
+
// The rollout order is `runRollout`'s to play; here it measures nothing, so it is red.
|
|
70
|
+
if (fault.kind === 'order') return { clearedMs: undefined, exit: undefined };
|
|
69
71
|
const role = fleet.roles.get(fault.role);
|
|
70
72
|
if (role === undefined) return { clearedMs: undefined, exit: undefined };
|
|
71
73
|
const signalled = now();
|
package/src/drill-verdict.ts
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
// violates it without a service running. A property with no evidence is red too: a drill that
|
|
4
4
|
// measured nothing is the vacuous pass this file exists to refuse.
|
|
5
5
|
|
|
6
|
+
import type { DrillRolloutEvidence } from './drill-rollout-verdict';
|
|
7
|
+
import { judgeOneWaitLine, judgeRolesWait, judgeServesAfterMigrate } from './drill-rollout-verdict';
|
|
8
|
+
|
|
6
9
|
/** Every property a drill may state. The table (`drills.ts`) picks; this file judges. */
|
|
7
10
|
export type DrillProperty =
|
|
8
11
|
| 'requests-coded'
|
|
@@ -14,7 +17,10 @@ export type DrillProperty =
|
|
|
14
17
|
| 'jobs-terminal'
|
|
15
18
|
| 'jobs-once'
|
|
16
19
|
| 'role-exits'
|
|
17
|
-
| 'sync-resumes'
|
|
20
|
+
| 'sync-resumes'
|
|
21
|
+
| 'roles-wait'
|
|
22
|
+
| 'serves-after-migrate'
|
|
23
|
+
| 'one-wait-line';
|
|
18
24
|
|
|
19
25
|
/** The thresholds, each derived from the app's config (`drillBudgets`), never typed per drill. */
|
|
20
26
|
export interface DrillBudgets {
|
|
@@ -58,6 +64,8 @@ export interface DrillEvidence {
|
|
|
58
64
|
/** The signalled role: ms from the signal to its exit, `null` when it never exited. */
|
|
59
65
|
readonly exit: { readonly role: string; readonly afterMs: number | null } | undefined;
|
|
60
66
|
readonly sync: { readonly reconnected: boolean; readonly gaps: number } | undefined;
|
|
67
|
+
/** The rollout-order drill's roles (`drill-rollout.ts`); absent for every other drill. */
|
|
68
|
+
readonly rollout?: DrillRolloutEvidence | undefined;
|
|
61
69
|
}
|
|
62
70
|
|
|
63
71
|
export interface DrillFailure {
|
|
@@ -203,6 +211,9 @@ const JUDGES: ReadonlyMap<DrillProperty, Judge> = new Map<DrillProperty, Judge>(
|
|
|
203
211
|
: fail('sync-resumes', expected, `${String(sync.gaps)} sequence gap(s)`);
|
|
204
212
|
},
|
|
205
213
|
],
|
|
214
|
+
['roles-wait', judgeRolesWait],
|
|
215
|
+
['serves-after-migrate', judgeServesAfterMigrate],
|
|
216
|
+
['one-wait-line', judgeOneWaitLine],
|
|
206
217
|
]);
|
|
207
218
|
|
|
208
219
|
/**
|
package/src/drills.ts
CHANGED
|
@@ -10,9 +10,9 @@ export type DrillService = 'postgres' | 'redis' | 'nats';
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A role the fleet runs as its own process (`ROLE`, `apps/web/server.ts`); `sync` only for a drill
|
|
13
|
-
* whose sync client needs a node to dial.
|
|
13
|
+
* whose sync client needs a node to dial, `scheduler` only for the rollout-order drill.
|
|
14
14
|
*/
|
|
15
|
-
export type DrillRole = 'web' | 'worker' | 'sync';
|
|
15
|
+
export type DrillRole = 'web' | 'worker' | 'sync' | 'scheduler';
|
|
16
16
|
|
|
17
17
|
export type DrillName =
|
|
18
18
|
| 'pg-blackhole'
|
|
@@ -22,9 +22,14 @@ export type DrillName =
|
|
|
22
22
|
| 'nats-down'
|
|
23
23
|
| 'worker-sigkill'
|
|
24
24
|
| 'worker-sigterm'
|
|
25
|
-
| 'web-sigterm'
|
|
25
|
+
| 'web-sigterm'
|
|
26
|
+
| 'roll-before-migrate';
|
|
26
27
|
|
|
27
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* The fault itself: a proxy mode per phase, a signal to one role's process, or the rollout order —
|
|
30
|
+
* the roles started before this release's migrate, on a schema an older build stamped, each to say
|
|
31
|
+
* so in one line naming `waitCode` (`drill-rollout.ts`).
|
|
32
|
+
*/
|
|
28
33
|
export type DrillFault =
|
|
29
34
|
| {
|
|
30
35
|
readonly kind: 'service';
|
|
@@ -32,7 +37,8 @@ export type DrillFault =
|
|
|
32
37
|
/** Played in order, each for one phase; `pass` follows, and recovery is timed from it. */
|
|
33
38
|
readonly phases: (budgets: DrillBudgets) => readonly FaultMode[];
|
|
34
39
|
}
|
|
35
|
-
| { readonly kind: 'signal'; readonly role: DrillRole; readonly signal: 'SIGKILL' | 'SIGTERM' }
|
|
40
|
+
| { readonly kind: 'signal'; readonly role: DrillRole; readonly signal: 'SIGKILL' | 'SIGTERM' }
|
|
41
|
+
| { readonly kind: 'order'; readonly waitCode: string };
|
|
36
42
|
|
|
37
43
|
export interface Drill {
|
|
38
44
|
readonly name: DrillName;
|
|
@@ -92,6 +98,11 @@ export const DRILLS: readonly Drill[] = [
|
|
|
92
98
|
fault: { kind: 'signal', role: 'web', signal: 'SIGTERM' },
|
|
93
99
|
holds: ['requests-ok', 'role-exits'],
|
|
94
100
|
},
|
|
101
|
+
{
|
|
102
|
+
name: 'roll-before-migrate',
|
|
103
|
+
fault: { kind: 'order', waitCode: 'X_FRAMEWORK_SCHEMA_UNAPPLIED' },
|
|
104
|
+
holds: ['roles-wait', 'serves-after-migrate', 'one-wait-line'],
|
|
105
|
+
},
|
|
95
106
|
];
|
|
96
107
|
|
|
97
108
|
export const DRILL_NAMES: readonly DrillName[] = DRILLS.map((drill) => drill.name);
|
|
@@ -100,9 +111,14 @@ export const DRILL_NAMES: readonly DrillName[] = DRILLS.map((drill) => drill.nam
|
|
|
100
111
|
export const drillNamed = (name: string): Drill | undefined =>
|
|
101
112
|
DRILLS.find((drill) => drill.name === name);
|
|
102
113
|
|
|
103
|
-
/**
|
|
104
|
-
|
|
105
|
-
|
|
114
|
+
/**
|
|
115
|
+
* The roles a drill's fleet runs: a sync client needs the sync node it dials; the rollout order is
|
|
116
|
+
* played on every role Postgres alone serves (`replicator` needs a logical server, `sync` a bus).
|
|
117
|
+
*/
|
|
118
|
+
export const drillRoles = (drill: Drill): readonly DrillRole[] => {
|
|
119
|
+
if (drill.fault.kind === 'order') return ['web', 'worker', 'scheduler'];
|
|
120
|
+
return drill.holds.includes('sync-resumes') ? ['web', 'worker', 'sync'] : ['web', 'worker'];
|
|
121
|
+
};
|
|
106
122
|
|
|
107
123
|
/** The service a drill needs running, when it needs one beyond Postgres. */
|
|
108
124
|
export const drillService = (drill: Drill): DrillService | undefined =>
|
package/src/index.ts
CHANGED
|
@@ -76,14 +76,17 @@ export {
|
|
|
76
76
|
setFrozenClock,
|
|
77
77
|
withFrozenClock,
|
|
78
78
|
} from './determinism';
|
|
79
|
-
// `x drill` (plan 101 / 40): the
|
|
79
|
+
// `x drill` (plan 101 / 40): the nine fault drills, the fleet they run on, the verdict over evidence.
|
|
80
80
|
export type { DrillConfig, DrillTiming } from './drill-budgets';
|
|
81
81
|
export { drillBudgets } from './drill-budgets';
|
|
82
82
|
export { DrillFailedError, DrillServiceMissingError } from './drill-errors';
|
|
83
|
-
export type { DrillFleet, DrillRoleProcess, DrillServiceUrls } from './drill-fleet';
|
|
83
|
+
export type { DrillFleet, DrillRoleProcess, DrillRollout, DrillServiceUrls } from './drill-fleet';
|
|
84
84
|
export { startDrillFleet } from './drill-fleet';
|
|
85
85
|
export { bunDrillSql } from './drill-jobs';
|
|
86
86
|
export type { DrillTarget } from './drill-load';
|
|
87
|
+
export type { DrillRolloutOptions } from './drill-rollout';
|
|
88
|
+
export { runRollout } from './drill-rollout';
|
|
89
|
+
export type { DrillRolloutEvidence, DrillRolloutRole } from './drill-rollout-verdict';
|
|
87
90
|
export type { DrillReport } from './drill-run';
|
|
88
91
|
export { runDrill } from './drill-run';
|
|
89
92
|
export type { DrillDeclarations } from './drill-services';
|