@smoothbricks/nx-plugin 0.7.17 → 0.7.19
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 +315 -114
- package/dist/.build-inputs.json +47 -38
- package/dist/built-output-packages.js +1 -1
- package/dist/bun-lock-scope.d.ts +128 -20
- package/dist/bun-lock-scope.d.ts.map +1 -1
- package/dist/bun-lock-scope.js +267 -44
- package/dist/cargo-closure-input.d.ts +8 -6
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +15 -12
- package/dist/cargo-source-hash.d.ts +26 -25
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +84 -105
- package/dist/cargo-toolchain-paths.d.ts +2 -2
- package/dist/cargo-toolchain-paths.js +2 -2
- package/dist/cargo-workspace.d.ts +20 -2
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +43 -22
- package/dist/ensure-built-receipt.d.ts +3 -2
- package/dist/ensure-built-receipt.d.ts.map +1 -1
- package/dist/ensure-built-receipt.js +3 -2
- package/dist/eslint-config-closure.d.ts +8 -3
- package/dist/eslint-config-closure.d.ts.map +1 -1
- package/dist/eslint-config-closure.js +24 -5
- package/dist/executors/bounded-exec/admission-queue.d.ts +3 -1
- package/dist/executors/bounded-exec/admission-queue.d.ts.map +1 -1
- package/dist/executors/bounded-exec/admission-queue.js +9 -10
- package/dist/executors/bounded-exec/awake-clock.d.ts +5 -0
- package/dist/executors/bounded-exec/awake-clock.d.ts.map +1 -0
- package/dist/executors/bounded-exec/awake-clock.js +22 -0
- package/dist/executors/bounded-exec/bounds.d.ts +19 -6
- package/dist/executors/bounded-exec/bounds.d.ts.map +1 -1
- package/dist/executors/bounded-exec/bounds.js +22 -15
- package/dist/executors/bounded-exec/case-gate.d.ts +114 -0
- package/dist/executors/bounded-exec/case-gate.d.ts.map +1 -0
- package/dist/executors/bounded-exec/case-gate.js +342 -0
- package/dist/executors/bounded-exec/cpu-pool-thread.d.ts +1 -0
- package/dist/executors/bounded-exec/cpu-pool-thread.d.ts.map +1 -1
- package/dist/executors/bounded-exec/cpu-pool-thread.js +2 -2
- package/dist/executors/bounded-exec/cpu-pool.d.ts +28 -1
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/cpu-pool.js +102 -16
- package/dist/executors/bounded-exec/cpu-rise.d.ts +73 -0
- package/dist/executors/bounded-exec/cpu-rise.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-rise.js +111 -0
- package/dist/executors/bounded-exec/endpoint-life.d.ts +146 -0
- package/dist/executors/bounded-exec/endpoint-life.d.ts.map +1 -0
- package/dist/executors/bounded-exec/endpoint-life.js +228 -0
- package/dist/executors/bounded-exec/executor.d.ts +1 -1
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +256 -86
- package/dist/executors/bounded-exec/gateway-lease.d.ts +1 -1
- package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -1
- package/dist/executors/bounded-exec/gateway-lease.js +8 -1
- package/dist/executors/bounded-exec/go-run-summary.d.ts +51 -0
- package/dist/executors/bounded-exec/go-run-summary.d.ts.map +1 -0
- package/dist/executors/bounded-exec/go-run-summary.js +98 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts +11 -3
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -1
- package/dist/executors/bounded-exec/hang-diagnostics.js +125 -47
- package/dist/executors/bounded-exec/lease-wait.d.ts +1 -0
- package/dist/executors/bounded-exec/lease-wait.d.ts.map +1 -1
- package/dist/executors/bounded-exec/lease-wait.js +1 -0
- package/dist/executors/bounded-exec/mbx-pool.d.ts +42 -7
- package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/mbx-pool.js +45 -8
- package/dist/executors/bounded-exec/mbx-reservations.d.ts +12 -6
- package/dist/executors/bounded-exec/mbx-reservations.d.ts.map +1 -1
- package/dist/executors/bounded-exec/mbx-reservations.js +190 -284
- package/dist/executors/bounded-exec/parent-grant.d.ts +2 -0
- package/dist/executors/bounded-exec/parent-grant.d.ts.map +1 -1
- package/dist/executors/bounded-exec/parent-grant.js +1 -0
- package/dist/executors/bounded-exec/pool-admission.d.ts +29 -3
- package/dist/executors/bounded-exec/pool-admission.d.ts.map +1 -1
- package/dist/executors/bounded-exec/pool-admission.js +47 -1
- package/dist/executors/bounded-exec/progress-channel.d.ts +38 -0
- package/dist/executors/bounded-exec/progress-channel.d.ts.map +1 -0
- package/dist/executors/bounded-exec/progress-channel.js +113 -0
- package/dist/executors/bounded-exec/progress.d.ts +5 -1
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
- package/dist/executors/bounded-exec/progress.js +31 -1
- package/dist/executors/bounded-exec/ram-temp.d.ts +24 -1
- package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
- package/dist/executors/bounded-exec/ram-temp.js +123 -16
- package/dist/executors/bounded-exec/reservation-connection.d.ts +90 -0
- package/dist/executors/bounded-exec/reservation-connection.d.ts.map +1 -0
- package/dist/executors/bounded-exec/reservation-connection.js +248 -0
- package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/run-cpu-pool.js +3 -0
- package/dist/executors/bounded-exec/runner-sizing.d.ts +9 -5
- package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
- package/dist/executors/bounded-exec/runner-sizing.js +13 -9
- package/dist/executors/bounded-exec/schema.d.ts +7 -2
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
- package/dist/executors/bounded-exec/schema.js +17 -1
- package/dist/executors/bounded-exec/suite-minimum.d.ts +21 -0
- package/dist/executors/bounded-exec/suite-minimum.d.ts.map +1 -0
- package/dist/executors/bounded-exec/suite-minimum.js +85 -0
- package/dist/executors/bounded-exec/test-result-writer.d.ts +2 -0
- package/dist/executors/bounded-exec/test-result-writer.d.ts.map +1 -1
- package/dist/executors/bounded-exec/test-result-writer.js +1 -0
- package/dist/executors/bounded-exec/test-results.d.ts +5 -0
- package/dist/executors/bounded-exec/test-results.d.ts.map +1 -1
- package/dist/executors/bounded-exec/test-results.js +2 -2
- package/dist/executors/bounded-exec/verdict.d.ts +39 -10
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.js +60 -14
- package/dist/executors/cargo-zigbuild/executor.d.ts +10 -8
- package/dist/executors/cargo-zigbuild/executor.d.ts.map +1 -1
- package/dist/executors/cargo-zigbuild/executor.js +32 -19
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +294 -152
- package/dist/managed-files/ci-workflow.d.ts +4 -1
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +9 -14
- package/dist/managed-files/files.d.ts +3 -3
- package/dist/managed-files/files.js +3 -3
- package/dist/managed-files/generator.d.ts +5 -1
- package/dist/managed-files/generator.d.ts.map +1 -1
- package/dist/managed-files/generator.js +22 -7
- package/dist/managed-files/render-memo.d.ts +13 -0
- package/dist/managed-files/render-memo.d.ts.map +1 -0
- package/dist/managed-files/render-memo.js +94 -0
- package/dist/task-results.js +2 -1
- package/dist/test-program.d.ts +11 -6
- package/dist/test-program.d.ts.map +1 -1
- package/dist/test-program.js +39 -35
- package/dist/tool-bins.d.ts +12 -0
- package/dist/tool-bins.d.ts.map +1 -1
- package/dist/tool-bins.js +31 -6
- package/dist/typecheck-test-policy.d.ts +10 -1
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +40 -24
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +135 -42
- package/dist/workspace-index.d.ts +14 -1
- package/dist/workspace-index.d.ts.map +1 -1
- package/dist/workspace-index.js +36 -4
- package/executors.json +2 -0
- package/managed/raw/envrc +24 -0
- package/managed/raw/patches/nx@24.0.3.patch +1851 -221
- package/managed/raw/patches/ttsc@0.37.1.patch +1055 -0
- package/managed/raw/tooling/direnv/developer-links.ts +122 -14
- package/managed/raw/tooling/direnv/devenv.smoo.nix +20 -19
- package/managed/templates/github/actions/save-nix-devenv/action.yml +19 -1
- package/managed/templates/github/actions/setup-devenv/action.yml +2 -0
- package/package.json +68 -11
- package/runtime/nx-publish/.build-inputs.json +2 -2
- package/runtime/nx-publish/cache_adapter.js +293 -3
- package/runtime/nx-publish/nx_publish.js +36 -14
- package/src/built-output-packages.ts +1 -1
- package/src/bun-lock-scope.ts +293 -40
- package/src/cargo-closure-input.ts +20 -12
- package/src/cargo-source-hash.ts +119 -145
- package/src/cargo-toolchain-paths.ts +2 -2
- package/src/cargo-workspace.ts +59 -21
- package/src/ensure-built-receipt.ts +3 -2
- package/src/eslint-config-closure.ts +27 -6
- package/src/executors/bounded-exec/admission-queue.ts +11 -10
- package/src/executors/bounded-exec/awake-clock.ts +20 -0
- package/src/executors/bounded-exec/bounds.ts +41 -19
- package/src/executors/bounded-exec/case-gate.ts +365 -0
- package/src/executors/bounded-exec/cpu-pool-thread.ts +3 -3
- package/src/executors/bounded-exec/cpu-pool.ts +139 -12
- package/src/executors/bounded-exec/cpu-rise.ts +156 -0
- package/src/executors/bounded-exec/endpoint-life.ts +279 -0
- package/src/executors/bounded-exec/executor.ts +341 -123
- package/src/executors/bounded-exec/gateway-lease.ts +6 -0
- package/src/executors/bounded-exec/go-run-summary.ts +137 -0
- package/src/executors/bounded-exec/hang-diagnostics.ts +150 -50
- package/src/executors/bounded-exec/lease-wait.ts +1 -0
- package/src/executors/bounded-exec/mbx-pool.ts +57 -9
- package/src/executors/bounded-exec/mbx-reservations.ts +216 -183
- package/src/executors/bounded-exec/parent-grant.ts +3 -0
- package/src/executors/bounded-exec/pool-admission.ts +59 -2
- package/src/executors/bounded-exec/progress-channel.ts +129 -0
- package/src/executors/bounded-exec/progress.ts +39 -3
- package/src/executors/bounded-exec/ram-temp.ts +137 -17
- package/src/executors/bounded-exec/reservation-connection.ts +221 -0
- package/src/executors/bounded-exec/run-cpu-pool.ts +2 -0
- package/src/executors/bounded-exec/runner-sizing.ts +13 -8
- package/src/executors/bounded-exec/schema.json +1 -1
- package/src/executors/bounded-exec/schema.ts +21 -2
- package/src/executors/bounded-exec/suite-minimum.ts +81 -0
- package/src/executors/bounded-exec/test-result-writer.ts +3 -0
- package/src/executors/bounded-exec/test-results.ts +5 -0
- package/src/executors/bounded-exec/verdict.ts +83 -22
- package/src/executors/cargo-zigbuild/executor.ts +30 -19
- package/src/index.ts +357 -166
- package/src/managed-files/ci-workflow.ts +13 -17
- package/src/managed-files/files.ts +3 -3
- package/src/managed-files/generator.ts +21 -6
- package/src/managed-files/render-memo.ts +135 -0
- package/src/test-program.ts +36 -41
- package/src/tool-bins.ts +35 -6
- package/src/typecheck-test-policy.ts +49 -24
- package/src/workspace-config-policy.ts +161 -43
- package/src/workspace-index.ts +40 -5
- package/test-timings.json +157 -141
- package/managed/raw/patches/ttsc@0.36.3.patch +0 -1660
|
@@ -6,10 +6,19 @@ import {
|
|
|
6
6
|
grantedSizing,
|
|
7
7
|
invocationSpan,
|
|
8
8
|
type ReservedDemand,
|
|
9
|
+
runnerDemand,
|
|
9
10
|
WANT_ENV,
|
|
10
11
|
} from './runner-sizing.js';
|
|
11
12
|
import { shellQuote } from './verdict.js';
|
|
12
13
|
|
|
14
|
+
/**
|
|
15
|
+
* mbx's pool priorities, as `MBX_SCHEDULER_PRIORITY` and a reservation name them: interactive work's `interactive`,
|
|
16
|
+
* which mbx ranks first and decays as its job ages, a gate's `normal`, every other job's `low` (cowshed's
|
|
17
|
+
* `PoolPriority`).
|
|
18
|
+
*/
|
|
19
|
+
export const MBX_PRIORITIES = ['interactive', 'normal', 'low'] as const;
|
|
20
|
+
export type MbxPriority = (typeof MBX_PRIORITIES)[number];
|
|
21
|
+
|
|
13
22
|
/**
|
|
14
23
|
* mbx's machine-wide pool as a leaf runner meets it (specs/cowshed/05_gateway.md, "Host scheduling", "Parallelism inside
|
|
15
24
|
* a job"). With the mbx capability the canonical environment names the one pool (`MBX_SCHEDULER=1` beside the shared
|
|
@@ -17,7 +26,7 @@ import { shellQuote } from './verdict.js';
|
|
|
17
26
|
* serves a pool of its own beside it.
|
|
18
27
|
*/
|
|
19
28
|
export interface MbxPool {
|
|
20
|
-
readonly priority:
|
|
29
|
+
readonly priority: MbxPriority;
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
/**
|
|
@@ -32,7 +41,17 @@ export function mbxPool(environment: Readonly<NodeJS.ProcessEnv>): MbxPool | nul
|
|
|
32
41
|
if (environment.MBX_SCHEDULER !== '1' || !environment.MBX_CACHE_DIR || environment.MBX_SCHED_DISABLE === '1') {
|
|
33
42
|
return null;
|
|
34
43
|
}
|
|
35
|
-
|
|
44
|
+
const named = MBX_PRIORITIES.find((priority) => priority === environment.MBX_SCHEDULER_PRIORITY);
|
|
45
|
+
return { priority: named ?? 'low' };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether the environment names mbx's pool from inside a hold on it: what a held cowshed job, `mbx reserve`'s command
|
|
50
|
+
* and a reserved leaf's shell see (`MBX_SCHED_DISABLE=1` beside the canonical environment). mbx's test shim still
|
|
51
|
+
* starts every nextest case there, so a run under it may gate its cases (mbx-reservations.ts, `learnHeldGate`).
|
|
52
|
+
*/
|
|
53
|
+
export function mbxHold(environment: Readonly<NodeJS.ProcessEnv>): boolean {
|
|
54
|
+
return environment.MBX_SCHEDULER === '1' && !!environment.MBX_CACHE_DIR && environment.MBX_SCHED_DISABLE === '1';
|
|
36
55
|
}
|
|
37
56
|
|
|
38
57
|
/**
|
|
@@ -51,8 +70,13 @@ export type MbxLeaf =
|
|
|
51
70
|
| { readonly kind: 'cargo' }
|
|
52
71
|
| {
|
|
53
72
|
readonly kind: 'reserve';
|
|
54
|
-
readonly priority:
|
|
73
|
+
readonly priority: MbxPriority;
|
|
55
74
|
readonly demand: ReservedDemand;
|
|
75
|
+
/**
|
|
76
|
+
* Whether its runner starts every thread of its ask and each case takes a core at the leaf's gate (case-gate.ts):
|
|
77
|
+
* a nextest leaf of a run whose mbx gates its cases. Otherwise its runner is sized to its grant.
|
|
78
|
+
*/
|
|
79
|
+
readonly gated: boolean;
|
|
56
80
|
};
|
|
57
81
|
|
|
58
82
|
/** A leaf mbx's pool reserves for. */
|
|
@@ -73,11 +97,12 @@ export function runsPrebuiltNextest(command: string): boolean {
|
|
|
73
97
|
* kept every reservation queued behind it waiting while the pool drained. On 2026-10-31 a downstream gate ran 4.9 of 18
|
|
74
98
|
* cores busy while 25 of its tasks queued a minute or more, and a gate's Go test leaf waited 401 s for all 18 cores.
|
|
75
99
|
*/
|
|
76
|
-
export function mbxLeaf(pool: MbxPool, demand: Demand, command: string): MbxLeaf | null {
|
|
100
|
+
export function mbxLeaf(pool: MbxPool, demand: Demand, command: string, testGate: boolean): MbxLeaf | null {
|
|
77
101
|
const reserve = (reserved: ReservedDemand): MbxLeaf => ({
|
|
78
102
|
kind: 'reserve',
|
|
79
103
|
priority: pool.priority,
|
|
80
104
|
demand: reserved,
|
|
105
|
+
gated: testGate && reserved.kind === 'nextest',
|
|
81
106
|
});
|
|
82
107
|
switch (demand.kind) {
|
|
83
108
|
case 'vitest':
|
|
@@ -94,6 +119,17 @@ export function mbxLeaf(pool: MbxPool, demand: Demand, command: string): MbxLeaf
|
|
|
94
119
|
return reserve(demand);
|
|
95
120
|
}
|
|
96
121
|
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Whether a run of `command` under mbx's pool is a Cargo leaf: the one leaf whose admission wait the run reads from
|
|
125
|
+
* mbx's activity report and its waiters' native lineage (mbx-queue.ts; specs/cowshed/11_shell.md, "Cargo test-runner
|
|
126
|
+
* boundaries"), through cowshed's process-lineage entry. The command alone decides it: the pool's priority, the core
|
|
127
|
+
* count and the environment size a leaf, never its kind.
|
|
128
|
+
*/
|
|
129
|
+
export function runsAsCargoLeaf(command: string): boolean {
|
|
130
|
+
return mbxLeaf({ priority: 'low' }, runnerDemand(command, undefined, 1, {}), command, false)?.kind === 'cargo';
|
|
131
|
+
}
|
|
132
|
+
|
|
97
133
|
/**
|
|
98
134
|
* The value an in-place runner gives `--target-dir-remap`: smoo-nx-nextest-binaries' answer, quoted. The plugin infers
|
|
99
135
|
* the native helper; a repository's own runner may name its script through bun, which prints the same path.
|
|
@@ -167,7 +203,8 @@ export const MBX_GRANT_FD = 5;
|
|
|
167
203
|
|
|
168
204
|
/**
|
|
169
205
|
* `command` as the shell runs it under its leaf's admission, or as it is for a leaf in no pool (`null`): a reserved
|
|
170
|
-
* leaf asks for its runner's count (`reservation`).
|
|
206
|
+
* leaf asks for its runner's count (`reservation`). A reserved leaf's fixture is never part of `command`
|
|
207
|
+
* (`withReservationAfterFixture`).
|
|
171
208
|
*/
|
|
172
209
|
export function withMbxLeaf(command: string, leaf: MbxLeaf | null): string {
|
|
173
210
|
if (leaf === null || leaf.kind === 'cargo') {
|
|
@@ -176,11 +213,22 @@ export function withMbxLeaf(command: string, leaf: MbxLeaf | null): string {
|
|
|
176
213
|
return `${WANT_ENV}=${leaf.demand.want} && ${reservation(command, leaf)}`;
|
|
177
214
|
}
|
|
178
215
|
|
|
216
|
+
/**
|
|
217
|
+
* A reserved leaf with a `fixture`: the fixture runs first, outside the reservation, as pooled work like any other
|
|
218
|
+
* build, and the leaf asks for its runner's count only once it has exited 0 (specs/cowshed/05_gateway.md, "A leaf's
|
|
219
|
+
* build precedes its reservation"). A reservation holds cores only while its command can use them: a fixture inside
|
|
220
|
+
* one waits on Cargo's build-directory lock holding its grant, and when reserved leaves hold every core the lock's
|
|
221
|
+
* holder, a pooled build whose compiler waits for one, never gets it. A fixture that fails fails the run before
|
|
222
|
+
* anything is reserved.
|
|
223
|
+
*/
|
|
224
|
+
export function withReservationAfterFixture(fixture: string, command: string, leaf: ReservedLeaf): string {
|
|
225
|
+
return `${fixture} && ${withMbxLeaf(command, leaf)}`;
|
|
226
|
+
}
|
|
179
227
|
/**
|
|
180
228
|
* A reserved leaf whose `fixture` segment ends by printing one line per unit its `command` runs (`unitsCommand`): the
|
|
181
|
-
* segment runs first, outside the reservation
|
|
182
|
-
*
|
|
183
|
-
*
|
|
229
|
+
* segment runs first, outside the reservation as every fixture does (`withReservationAfterFixture`), and the leaf asks
|
|
230
|
+
* for its units, at most its runner's count and at least one. A segment that fails fails the run before anything is
|
|
231
|
+
* reserved.
|
|
184
232
|
*/
|
|
185
233
|
export function withCountedReservation(fixture: string, command: string, leaf: ReservedLeaf): string {
|
|
186
234
|
const most = leaf.demand.want;
|
|
@@ -201,7 +249,7 @@ function reservation(command: string, leaf: ReservedLeaf): string {
|
|
|
201
249
|
const said = `printf 'cowshed: mbx reserve running cpus=%s asked=%s\\n' "$${CPU_TOKENS_ENV}" "$${WANT_ENV}" >&2; `;
|
|
202
250
|
const ask = `printf '%s\\n' "$${WANT_ENV}" >&${MBX_GRANT_FD} && IFS= read -r ${GRANTED_CPUS_ENV} <&${MBX_GRANT_FD} && exec ${MBX_GRANT_FD}>&-`;
|
|
203
251
|
const held = `export ${GRANTED_CPUS_ENV} MBX_SCHED_DISABLE=1 MBX_SCHED_DIR=`;
|
|
204
|
-
return `export ${WANT_ENV} && ${ask} && ${held} && eval ${shellQuote(`${grantedSizing(leaf.demand)}${said}${command}`)}`;
|
|
252
|
+
return `export ${WANT_ENV} && ${ask} && ${held} && eval ${shellQuote(`${grantedSizing(leaf.demand, leaf.gated)}${said}${command}`)}`;
|
|
205
253
|
}
|
|
206
254
|
|
|
207
255
|
/**
|
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
2
|
import { rm } from 'node:fs/promises';
|
|
3
3
|
import { createConnection, type Socket } from 'node:net';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import type { Readable, Writable } from 'node:stream';
|
|
6
5
|
import { logger } from 'nx/src/devkit-exports.js';
|
|
7
6
|
import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src/project-graph/plugins/public-api.js';
|
|
8
|
-
import typia, { type tags } from 'typia';
|
|
9
7
|
import { ownedSocketDirectory } from '../../testing.js';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
8
|
+
import { TEST_GATE_SERVED_ENV } from './case-gate.js';
|
|
9
|
+
import { type Ended, type EndpointEffect, type EndpointEvent, endpointStep, spawned } from './endpoint-life.js';
|
|
10
|
+
import { type MbxPriority, mbxHold, mbxPool } from './mbx-pool.js';
|
|
11
|
+
import {
|
|
12
|
+
type Connection,
|
|
13
|
+
type ConnectionEffect,
|
|
14
|
+
type ConnectionEvent,
|
|
15
|
+
connectionStep,
|
|
16
|
+
opened,
|
|
17
|
+
type ReservationRefusal,
|
|
18
|
+
} from './reservation-connection.js';
|
|
12
19
|
|
|
13
20
|
/**
|
|
14
21
|
* mbx's reservation endpoint of one Nx run (specs/cowshed/05_gateway.md, "Parallelism inside a job"). A reserved leaf
|
|
@@ -16,7 +23,9 @@ import { mbxPool } from './mbx-pool.js';
|
|
|
16
23
|
* table recycling. With mbx's pool, the run's `preTasksExecution` starts `mbx reservations <socket>` once and publishes
|
|
17
24
|
* the socket in `RESERVATIONS_ENV`, which every task inherits; each task's executor asks it for its leaf's cores over
|
|
18
25
|
* one connection per process (`reserveCores`), and the run's `postTasksExecution` closes the endpoint's standard input,
|
|
19
|
-
* which ends it.
|
|
26
|
+
* which ends it. Nx runs both hooks in the process that runs the tasks, never in a daemon (patches/README.md, "A run's
|
|
27
|
+
* task hooks run in its own process"), so the endpoint is a child of the run and ends with it, whatever becomes of the
|
|
28
|
+
* run's daemon. Admission is `mbx reserve`'s: the same pool, priority and order. A grant lasts until its leaf ends, or
|
|
20
29
|
* until its connection closes, so an executor that dies gives back what it held.
|
|
21
30
|
*/
|
|
22
31
|
export const RESERVATIONS_ENV = 'MBX_RESERVATIONS_SOCKET';
|
|
@@ -51,17 +60,16 @@ export function describeUnserved(endpoint: Exclude<RunEndpoint, { kind: 'served'
|
|
|
51
60
|
|
|
52
61
|
const ENDPOINT_DIRECTORY = 'mbx-r-';
|
|
53
62
|
const ENDPOINT_SOCKET = 'r.sock';
|
|
54
|
-
/** What is kept of the endpoint's stderr to say why it ended: its last bytes. */
|
|
55
|
-
const SAID_BYTES = 4096;
|
|
56
63
|
|
|
57
|
-
/** A run's endpoint
|
|
64
|
+
/** A run's endpoint once it serves. */
|
|
58
65
|
interface Endpoint {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
readonly ended: Promise<string>;
|
|
66
|
+
/** The run's end, which ends the endpoint: how it ended, once its socket's directory is removed. */
|
|
67
|
+
end(): Promise<Ended>;
|
|
62
68
|
}
|
|
63
69
|
|
|
64
|
-
type Started =
|
|
70
|
+
type Started =
|
|
71
|
+
| { readonly ok: true; readonly endpoint: Endpoint; readonly socket: string; readonly gates: boolean }
|
|
72
|
+
| { readonly ok: false; readonly error: string };
|
|
65
73
|
|
|
66
74
|
/** The endpoints this process serves, by Nx run id, until that run's `postTasksExecution`. */
|
|
67
75
|
const endpoints = new Map<string, Endpoint>();
|
|
@@ -69,13 +77,15 @@ const endpoints = new Map<string, Endpoint>();
|
|
|
69
77
|
/**
|
|
70
78
|
* Serve `context`'s run an mbx reservation endpoint and publish it in `env`, which every task inherits, when the
|
|
71
79
|
* environment names mbx's pool (`mbxPool`). An endpoint that does not start is said, and each reserved leaf of the run
|
|
72
|
-
* then refuses to run, naming `RESERVATIONS_ENV`; a leaf in no pool, or one mbx admits itself, runs as before.
|
|
80
|
+
* then refuses to run, naming `RESERVATIONS_ENV`; a leaf in no pool, or one mbx admits itself, runs as before. A run
|
|
81
|
+
* under a hold on mbx's pool is served no endpoint, and learns whether its mbx gates test cases (`learnHeldGate`).
|
|
73
82
|
*/
|
|
74
83
|
export async function serveRunReservations(
|
|
75
84
|
context: Pick<PreTasksExecutionContext, 'id'>,
|
|
76
85
|
env: NodeJS.ProcessEnv = process.env,
|
|
77
86
|
): Promise<void> {
|
|
78
87
|
if (mbxPool(env) === null) {
|
|
88
|
+
if (mbxHold(env)) await learnHeldGate(env);
|
|
79
89
|
return;
|
|
80
90
|
}
|
|
81
91
|
const started = await startEndpoint(env);
|
|
@@ -89,6 +99,33 @@ export async function serveRunReservations(
|
|
|
89
99
|
endpoints.set(context.id, started.endpoint);
|
|
90
100
|
env[RESERVATIONS_ENV] = started.socket;
|
|
91
101
|
delete env[RESERVATIONS_UNSERVED_ENV];
|
|
102
|
+
// A leaf's runner starts every thread of its ask only where the installed mbx's shim takes each case through its gate.
|
|
103
|
+
if (started.gates) env[TEST_GATE_SERVED_ENV] = '1';
|
|
104
|
+
else delete env[TEST_GATE_SERVED_ENV];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A run under a hold on mbx's pool reserves nothing and serves its own token pool (run-cpu-pool.ts), and its nextest
|
|
109
|
+
* leaves still start every case through mbx's shim. Whether that shim gates is the endpoint's own answer, so the run
|
|
110
|
+
* starts the endpoint once, outside the hold's `MBX_SCHED_DISABLE` and `MBX_SCHED_DIR`, which it refuses to serve
|
|
111
|
+
* under, asks, and ends it; no request is made of it (specs/cowshed/05_gateway.md, "A case starts on a core", Inside a
|
|
112
|
+
* hold). An mbx that does not start it, or does not answer, gates nothing: the run's leaves are sized to their grants.
|
|
113
|
+
*/
|
|
114
|
+
async function learnHeldGate(env: NodeJS.ProcessEnv): Promise<void> {
|
|
115
|
+
const unheld = { ...env };
|
|
116
|
+
delete unheld.MBX_SCHED_DISABLE;
|
|
117
|
+
delete unheld.MBX_SCHED_DIR;
|
|
118
|
+
const started = await startEndpoint(unheld);
|
|
119
|
+
if (!started.ok) {
|
|
120
|
+
logger.warn(
|
|
121
|
+
`bounded-exec: the installed mbx did not say whether its test shim gates cases (${started.error}); this run's nextest leaves are sized to their grants`,
|
|
122
|
+
);
|
|
123
|
+
delete env[TEST_GATE_SERVED_ENV];
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
await started.endpoint.end();
|
|
127
|
+
if (started.gates) env[TEST_GATE_SERVED_ENV] = '1';
|
|
128
|
+
else delete env[TEST_GATE_SERVED_ENV];
|
|
92
129
|
}
|
|
93
130
|
|
|
94
131
|
/** Close the endpoint `context`'s run was served, if it was served one: its standard input's end ends it. */
|
|
@@ -98,86 +135,111 @@ export async function closeRunReservations(context: Pick<PostTasksExecutionConte
|
|
|
98
135
|
return;
|
|
99
136
|
}
|
|
100
137
|
endpoints.delete(context.id);
|
|
101
|
-
endpoint.
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (ended !== 'mbx reservations ended with status 0') {
|
|
105
|
-
logger.warn(`bounded-exec: this run's mbx reservation endpoint did not close cleanly: ${ended}`);
|
|
138
|
+
const ended = await endpoint.end();
|
|
139
|
+
if (!ended.clean) {
|
|
140
|
+
logger.warn(`bounded-exec: this run's mbx reservation endpoint did not close cleanly: ${ended.how}`);
|
|
106
141
|
}
|
|
107
142
|
}
|
|
108
143
|
|
|
109
|
-
/**
|
|
144
|
+
/**
|
|
145
|
+
* Start `mbx reservations` by its PATH name in an owned socket directory, and answer once its life answers its start
|
|
146
|
+
* (endpoint-life.ts). This is that machine's shell: it feeds the child's and the probe's events to `endpointStep` and
|
|
147
|
+
* executes the effects each step answers, and decides nothing itself.
|
|
148
|
+
*/
|
|
110
149
|
async function startEndpoint(env: NodeJS.ProcessEnv): Promise<Started> {
|
|
111
150
|
const directory = await ownedSocketDirectory(ENDPOINT_DIRECTORY, ENDPOINT_SOCKET);
|
|
112
151
|
const socket = join(directory, ENDPOINT_SOCKET);
|
|
113
152
|
const server = spawn('mbx', ['reservations', socket], { env, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
114
|
-
|
|
115
|
-
server.
|
|
116
|
-
|
|
117
|
-
|
|
153
|
+
const remove = (): Promise<void> => rm(directory, { recursive: true, force: true });
|
|
154
|
+
let life = spawned(server.pid);
|
|
155
|
+
let probe: Socket | undefined;
|
|
156
|
+
let timer: NodeJS.Timeout | undefined;
|
|
157
|
+
// `Promise.withResolvers` would read better but needs lib es2024; this package inherits lib es2022.
|
|
158
|
+
let answer!: (started: Started) => void;
|
|
159
|
+
let conclude!: (ended: Ended) => void;
|
|
160
|
+
let fail!: (error: unknown) => void;
|
|
161
|
+
let failEnd!: (error: unknown) => void;
|
|
162
|
+
const started = new Promise<Started>((resolve, reject) => {
|
|
163
|
+
answer = resolve;
|
|
164
|
+
fail = reject;
|
|
118
165
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
server.once('close', (code, signal) => {
|
|
123
|
-
const last = said.trim().split('\n').at(-1) ?? '';
|
|
124
|
-
resolve(
|
|
125
|
-
`mbx reservations ended ${signal === null ? `with status ${code}` : `on ${signal}`}${last === '' ? '' : `: ${last}`}`,
|
|
126
|
-
);
|
|
127
|
-
});
|
|
166
|
+
const ended = new Promise<Ended>((resolve, reject) => {
|
|
167
|
+
conclude = resolve;
|
|
168
|
+
failEnd = reject;
|
|
128
169
|
});
|
|
129
|
-
const endpoint: Endpoint = {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
}
|
|
170
|
+
const endpoint: Endpoint = {
|
|
171
|
+
end() {
|
|
172
|
+
feed({ type: 'run-end' });
|
|
173
|
+
return ended;
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
const execute = (effect: EndpointEffect): void => {
|
|
177
|
+
switch (effect.do) {
|
|
178
|
+
case 'probe': {
|
|
179
|
+
// eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: a Unix-socket connect completes or fails at once, and the probe ends at its answer, its connection's end or the child's end (endpoint-life.ts), which the endpoint's death brings: it is this run's child
|
|
180
|
+
const connection = createConnection(socket);
|
|
181
|
+
probe = connection;
|
|
182
|
+
connection.setEncoding('utf8');
|
|
183
|
+
connection.on('connect', () => connection.write('{"type":"capabilities"}\n'));
|
|
184
|
+
connection.on('data', (text: string) => feed({ type: 'probe', text }));
|
|
185
|
+
connection.on('error', () => feed({ type: 'probe-end' }));
|
|
186
|
+
connection.on('close', () => feed({ type: 'probe-end' }));
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
case 'hang-up':
|
|
190
|
+
probe?.destroy();
|
|
191
|
+
return;
|
|
192
|
+
case 'arm':
|
|
193
|
+
timer = setTimeout(() => feed({ type: 'expired' }), effect.ms);
|
|
194
|
+
return;
|
|
195
|
+
case 'disarm':
|
|
196
|
+
clearTimeout(timer);
|
|
197
|
+
return;
|
|
198
|
+
case 'kill':
|
|
199
|
+
server.kill('SIGKILL');
|
|
200
|
+
return;
|
|
163
201
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
202
|
+
case 'unref-child': {
|
|
203
|
+
server.unref();
|
|
204
|
+
// Node exposes its pipes as sockets; Bun's readable pipes also unref but are not sockets.
|
|
205
|
+
type Pipe = (typeof server.stdin | typeof server.stdout) & { unref?: () => void };
|
|
206
|
+
const pipes: readonly Pipe[] = [server.stdin, server.stdout, server.stderr];
|
|
207
|
+
for (const pipe of pipes) pipe.unref?.();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
case 'close-stdin':
|
|
211
|
+
server.stdin.end();
|
|
212
|
+
return;
|
|
213
|
+
case 'serve':
|
|
214
|
+
answer({ ok: true, endpoint, socket, gates: effect.gates });
|
|
215
|
+
return;
|
|
216
|
+
case 'refuse':
|
|
217
|
+
void remove().then(() => answer({ ok: false, error: effect.why }), fail);
|
|
218
|
+
return;
|
|
219
|
+
case 'conclude':
|
|
220
|
+
void remove().then(() => conclude(effect.ended), failEnd);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const feed = (event: EndpointEvent): void => {
|
|
225
|
+
const [next, effects] = endpointStep(life, event);
|
|
226
|
+
life = next;
|
|
227
|
+
for (const effect of effects) execute(effect);
|
|
228
|
+
};
|
|
229
|
+
server.stdout.setEncoding('utf8');
|
|
230
|
+
server.stdout.on('data', (text: string) => feed({ type: 'stdout', text }));
|
|
231
|
+
server.stdout.on('end', () => feed({ type: 'stdout-end' }));
|
|
232
|
+
server.stderr.setEncoding('utf8');
|
|
233
|
+
server.stderr.on('data', (text: string) => feed({ type: 'stderr', text }));
|
|
234
|
+
server.once('error', (error) => feed({ type: 'error', message: error.message }));
|
|
235
|
+
server.once('close', (code, signal) => feed({ type: 'close', code, signal }));
|
|
236
|
+
return started;
|
|
175
237
|
}
|
|
176
238
|
|
|
177
239
|
/** The pool's answer to one reservation: the cores it holds and a promise of why it was lost, or why there are none. */
|
|
178
240
|
export type Granted =
|
|
179
241
|
| { readonly ok: true; readonly cpus: number; readonly lost: Promise<string> }
|
|
180
|
-
| { readonly ok: false; readonly
|
|
242
|
+
| { readonly ok: false; readonly refusal: ReservationRefusal };
|
|
181
243
|
|
|
182
244
|
/** One request for cores on a run's endpoint. */
|
|
183
245
|
export interface Reservation {
|
|
@@ -187,144 +249,115 @@ export interface Reservation {
|
|
|
187
249
|
release(): void;
|
|
188
250
|
}
|
|
189
251
|
|
|
190
|
-
type Uint = number & tags.Type<'uint32'>;
|
|
191
|
-
|
|
192
|
-
/** The lines a client writes (mbx's `docs/scheduling.md`, "Serve reservations to many commands"). */
|
|
193
|
-
type ReservationRequest =
|
|
194
|
-
| { type: 'reserve'; id: Uint; cpus: Uint & tags.Minimum<1>; priority: 'normal' | 'low' }
|
|
195
|
-
| { type: 'release'; id: Uint };
|
|
196
|
-
|
|
197
|
-
/** The lines the endpoint answers; an added field is mbx's to add. */
|
|
198
|
-
type ReservationReply =
|
|
199
|
-
| { type: 'granted'; id: Uint; cpus: Uint & tags.Minimum<1> }
|
|
200
|
-
| { type: 'refused'; id: Uint; error: string }
|
|
201
|
-
| { type: 'error'; error: string };
|
|
202
|
-
|
|
203
|
-
const stringifyRequest = typia.json.createStringify<ReservationRequest>();
|
|
204
|
-
const parseReply = typia.json.createValidateParse<ReservationReply>();
|
|
205
|
-
|
|
206
|
-
/** A request's place on its connection: waiting for its answer, or held until released or lost. */
|
|
207
|
-
type Entry =
|
|
208
|
-
| { readonly kind: 'waiting'; readonly answer: (granted: Granted) => void }
|
|
209
|
-
| { readonly kind: 'held'; readonly lose: (why: string) => void };
|
|
210
|
-
|
|
211
252
|
/**
|
|
212
|
-
*
|
|
213
|
-
*
|
|
253
|
+
* The socket of one connection to an endpoint, shared by every reservation this process makes there. It is the shell
|
|
254
|
+
* of that connection's machine (reservation-connection.ts): it feeds its callers' requests and its socket's events to
|
|
255
|
+
* `connectionStep` and executes the effects each step answers, keeping each request's answer and each grant's loss to
|
|
256
|
+
* settle; it decides nothing itself.
|
|
214
257
|
*/
|
|
215
|
-
class
|
|
258
|
+
class EndpointSocket {
|
|
216
259
|
readonly #socket: Socket;
|
|
217
|
-
readonly #
|
|
260
|
+
readonly #answers = new Map<number, (granted: Granted) => void>();
|
|
261
|
+
readonly #losses = new Map<number, (why: string) => void>();
|
|
262
|
+
#connection: Connection;
|
|
218
263
|
#nextId = 1;
|
|
219
|
-
#unread = '';
|
|
220
|
-
#closed: string | null = null;
|
|
221
264
|
|
|
222
265
|
constructor(
|
|
223
|
-
|
|
266
|
+
endpoint: string,
|
|
224
267
|
private readonly forget: () => void,
|
|
225
268
|
) {
|
|
269
|
+
this.#connection = opened(endpoint);
|
|
226
270
|
// eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: a Unix-socket connect completes or fails at once, and a reservation's answer waits as long as the pool's admission holds it, as `mbx reserve`'s did; the connection's end answers every request still waiting
|
|
227
271
|
this.#socket = createConnection(endpoint);
|
|
228
272
|
this.#socket.setEncoding('utf8');
|
|
229
273
|
this.#socket.unref();
|
|
230
|
-
this.#socket.on('data', (text: string) => this.#
|
|
231
|
-
this.#socket.on('error', (error) =>
|
|
232
|
-
|
|
274
|
+
this.#socket.on('data', (text: string) => this.#feed({ type: 'data', text }));
|
|
275
|
+
this.#socket.on('error', (error) =>
|
|
276
|
+
this.#feed({
|
|
277
|
+
type: 'error',
|
|
278
|
+
message: error.message,
|
|
279
|
+
code: 'code' in error && typeof error.code === 'string' ? error.code : null,
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
this.#socket.on('close', () => this.#feed({ type: 'close' }));
|
|
233
283
|
}
|
|
234
284
|
|
|
235
|
-
reserve(cpus: number, priority:
|
|
236
|
-
if (this.#closed !== null) {
|
|
237
|
-
return { granted: Promise.resolve({ ok: false, error: this.#closed }), release() {} };
|
|
238
|
-
}
|
|
285
|
+
reserve(cpus: number, priority: MbxPriority, min: number, name?: string): Reservation {
|
|
239
286
|
const id = this.#nextId++;
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
this.#entries.set(id, { kind: 'waiting', answer });
|
|
243
|
-
this.#socket.ref();
|
|
244
|
-
this.#socket.write(`${stringifyRequest({ type: 'reserve', id, cpus, priority })}\n`);
|
|
245
|
-
let released = false;
|
|
287
|
+
const granted = new Promise<Granted>((resolve) => this.#answers.set(id, resolve));
|
|
288
|
+
this.#feed({ type: 'reserve', id, cpus, priority, min, name });
|
|
246
289
|
return {
|
|
247
290
|
granted,
|
|
248
291
|
release: () => {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
if (entry === undefined) return;
|
|
253
|
-
this.#entries.delete(id);
|
|
254
|
-
if (entry.kind === 'waiting') entry.answer({ ok: false, error: 'released while it waited' });
|
|
255
|
-
this.#socket.write(`${stringifyRequest({ type: 'release', id })}\n`);
|
|
256
|
-
if (this.#entries.size === 0) this.#socket.unref();
|
|
292
|
+
// A grant given back is no longer one to lose.
|
|
293
|
+
this.#losses.delete(id);
|
|
294
|
+
this.#feed({ type: 'release', id });
|
|
257
295
|
},
|
|
258
296
|
};
|
|
259
297
|
}
|
|
260
298
|
|
|
261
|
-
#
|
|
262
|
-
this.#
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
this.#unread = this.#unread.slice(end + 1);
|
|
266
|
-
const parsed = parseReply(line);
|
|
267
|
-
if (!parsed.success) {
|
|
268
|
-
this.#close(`mbx reservation endpoint ${this.endpoint} answered ${JSON.stringify(line)}, which is no reply`);
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
this.#answer(parsed.data);
|
|
272
|
-
if (this.#closed !== null) return;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
#answer(reply: ReservationReply): void {
|
|
277
|
-
if (reply.type === 'error') {
|
|
278
|
-
this.#close(`mbx reservation endpoint ${this.endpoint} closed this connection: ${reply.error}`);
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
const entry = this.#entries.get(reply.id);
|
|
282
|
-
// Released before its answer arrived: the endpoint drops what it won.
|
|
283
|
-
if (entry === undefined) return;
|
|
284
|
-
if (entry.kind === 'held') {
|
|
285
|
-
this.#close(`mbx reservation endpoint ${this.endpoint} answered reservation ${reply.id} twice`);
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
if (reply.type === 'granted') {
|
|
289
|
-
let lose!: (why: string) => void;
|
|
290
|
-
const lost = new Promise<string>((resolve) => (lose = resolve));
|
|
291
|
-
this.#entries.set(reply.id, { kind: 'held', lose });
|
|
292
|
-
entry.answer({ ok: true, cpus: reply.cpus, lost });
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
this.#entries.delete(reply.id);
|
|
296
|
-
entry.answer({ ok: false, error: reply.error });
|
|
297
|
-
if (this.#entries.size === 0) this.#socket.unref();
|
|
299
|
+
#feed(event: ConnectionEvent): void {
|
|
300
|
+
const [next, effects] = connectionStep(this.#connection, event);
|
|
301
|
+
this.#connection = next;
|
|
302
|
+
for (const effect of effects) this.#execute(effect);
|
|
298
303
|
}
|
|
299
304
|
|
|
300
|
-
#
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
305
|
+
#execute(effect: ConnectionEffect): void {
|
|
306
|
+
switch (effect.do) {
|
|
307
|
+
case 'ref':
|
|
308
|
+
this.#socket.ref();
|
|
309
|
+
return;
|
|
310
|
+
case 'unref':
|
|
311
|
+
this.#socket.unref();
|
|
312
|
+
return;
|
|
313
|
+
case 'write':
|
|
314
|
+
this.#socket.write(effect.line);
|
|
315
|
+
return;
|
|
316
|
+
case 'answer': {
|
|
317
|
+
const settle = this.#answers.get(effect.id);
|
|
318
|
+
this.#answers.delete(effect.id);
|
|
319
|
+
if (!effect.answer.ok) {
|
|
320
|
+
settle?.(effect.answer);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const lost = new Promise<string>((resolve) => this.#losses.set(effect.id, resolve));
|
|
324
|
+
settle?.({ ok: true, cpus: effect.answer.cpus, lost });
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
case 'lose':
|
|
328
|
+
this.#losses.get(effect.id)?.(effect.why);
|
|
329
|
+
this.#losses.delete(effect.id);
|
|
330
|
+
return;
|
|
331
|
+
case 'hang-up':
|
|
332
|
+
this.forget();
|
|
333
|
+
this.#socket.destroy();
|
|
334
|
+
return;
|
|
307
335
|
}
|
|
308
|
-
this.#entries.clear();
|
|
309
|
-
this.#socket.destroy();
|
|
310
336
|
}
|
|
311
337
|
}
|
|
312
338
|
|
|
313
339
|
/** This process's connection to each endpoint it has asked. */
|
|
314
|
-
const connections = new Map<string,
|
|
340
|
+
const connections = new Map<string, EndpointSocket>();
|
|
315
341
|
|
|
316
342
|
/**
|
|
317
|
-
* Ask the endpoint at `socket` for at most `cpus` cores at `priority`,
|
|
318
|
-
* opened by its first request and kept while another one
|
|
343
|
+
* Ask the endpoint at `socket` for at most `cpus` cores at `priority`, starting on no fewer than `min` of them until
|
|
344
|
+
* mbx's aging bound, over this process's one connection to it, opened by its first request and kept while another one
|
|
345
|
+
* may come. `name` is the class mbx predicts the reservation by; absent, mbx's own for an unnamed one.
|
|
319
346
|
*/
|
|
320
|
-
export function reserveCores(
|
|
347
|
+
export function reserveCores(
|
|
348
|
+
socket: string,
|
|
349
|
+
cpus: number,
|
|
350
|
+
priority: MbxPriority,
|
|
351
|
+
min: number,
|
|
352
|
+
name?: string,
|
|
353
|
+
): Reservation {
|
|
321
354
|
let connection = connections.get(socket);
|
|
322
355
|
if (connection === undefined) {
|
|
323
|
-
const
|
|
324
|
-
if (connections.get(socket) ===
|
|
356
|
+
const fresh: EndpointSocket = new EndpointSocket(socket, () => {
|
|
357
|
+
if (connections.get(socket) === fresh) connections.delete(socket);
|
|
325
358
|
});
|
|
326
|
-
connections.set(socket,
|
|
327
|
-
connection =
|
|
359
|
+
connections.set(socket, fresh);
|
|
360
|
+
connection = fresh;
|
|
328
361
|
}
|
|
329
|
-
return connection.reserve(cpus, priority);
|
|
362
|
+
return connection.reserve(cpus, priority, min, name);
|
|
330
363
|
}
|