@ship.zone/runner 5.1.0 → 5.3.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/changelog.md +15 -0
- package/dist_rust/runnerworkspace_linux_amd64_musl.tsrust-build.json +3 -3
- package/dist_rust/runnerworkspace_linux_arm64_musl.tsrust-build.json +3 -3
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/api-client.js +11 -5
- package/dist_ts/cli.js +51 -3
- package/dist_ts/decommission.d.ts +19 -0
- package/dist_ts/decommission.js +122 -0
- package/dist_ts/errors.d.ts +4 -1
- package/dist_ts/errors.js +7 -2
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +2 -1
- package/dist_ts/interfaces.d.ts +9 -0
- package/dist_ts/microvm-executor.js +18 -2
- package/dist_ts/resource-budget.d.ts +10 -2
- package/dist_ts/resource-budget.js +49 -6
- package/dist_ts/runner.js +17 -3
- package/package.json +2 -2
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/api-client.ts +14 -3
- package/ts/cli.ts +61 -2
- package/ts/decommission.ts +167 -0
- package/ts/errors.ts +10 -1
- package/ts/index.ts +1 -0
- package/ts/interfaces.ts +10 -0
- package/ts/microvm-executor.ts +20 -1
- package/ts/resource-budget.ts +58 -6
- package/ts/runner.ts +16 -2
package/ts/cli.ts
CHANGED
|
@@ -9,7 +9,12 @@ import {
|
|
|
9
9
|
validateRunnerConfig,
|
|
10
10
|
withRunnerCredential,
|
|
11
11
|
} from './config.js';
|
|
12
|
-
import {
|
|
12
|
+
import { decommissionRunnerHost } from './decommission.js';
|
|
13
|
+
import {
|
|
14
|
+
ConfigurationError,
|
|
15
|
+
isRunnerDecommissionedError,
|
|
16
|
+
RUNNER_DECOMMISSIONED_EXIT_CODE,
|
|
17
|
+
} from './errors.js';
|
|
13
18
|
import type {
|
|
14
19
|
IRegisterRunnerRequest,
|
|
15
20
|
IRegisterRunnerResponse,
|
|
@@ -32,13 +37,14 @@ interface IParsedArguments {
|
|
|
32
37
|
|
|
33
38
|
const cliInvocation = 'ship-zone-runner';
|
|
34
39
|
|
|
35
|
-
const BOOLEAN_FLAGS = new Set<string>(['allow-insecure-loopback']);
|
|
40
|
+
const BOOLEAN_FLAGS = new Set<string>(['allow-insecure-loopback', 'from-systemd']);
|
|
36
41
|
const VALUE_OPTIONS = new Set<string>([
|
|
37
42
|
'config',
|
|
38
43
|
'control-plane',
|
|
39
44
|
'name',
|
|
40
45
|
'labels',
|
|
41
46
|
'state-directory',
|
|
47
|
+
'service-name',
|
|
42
48
|
'kernel-path',
|
|
43
49
|
'builder-rootfs-path',
|
|
44
50
|
'builder-image',
|
|
@@ -58,6 +64,7 @@ Usage:
|
|
|
58
64
|
${cliInvocation} register [--config <path>]
|
|
59
65
|
${cliInvocation} run [--config <path>]
|
|
60
66
|
${cliInvocation} once [--config <path>]
|
|
67
|
+
${cliInvocation} decommission-host [--config <path>] [--state-directory <path>] [--service-name <name>] [--from-systemd]
|
|
61
68
|
${cliInvocation} config [--config <path>]
|
|
62
69
|
${cliInvocation} version | --version
|
|
63
70
|
|
|
@@ -317,6 +324,7 @@ const runDaemon = async (
|
|
|
317
324
|
const signals = ['SIGINT', 'SIGTERM'] as const;
|
|
318
325
|
for (const signal of signals) process.on(signal, shutdownListener);
|
|
319
326
|
const executor = new MicroVmExecutor(config, stateOwner);
|
|
327
|
+
let decommissionedError: unknown;
|
|
320
328
|
try {
|
|
321
329
|
const api = apiFor(config);
|
|
322
330
|
const reportMessage = (message: string): void => console.error(message);
|
|
@@ -336,11 +344,59 @@ const runDaemon = async (
|
|
|
336
344
|
});
|
|
337
345
|
const result = await runner.run({ once, signal: shutdown.signal });
|
|
338
346
|
if (once) console.log(result.claimed ? `Job ${result.status}` : 'No job available');
|
|
347
|
+
} catch (error) {
|
|
348
|
+
if (isRunnerDecommissionedError(error)) {
|
|
349
|
+
decommissionedError = error;
|
|
350
|
+
} else {
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
339
353
|
} finally {
|
|
340
354
|
for (const signal of signals) process.off(signal, shutdownListener);
|
|
341
355
|
await executor.close().catch(() => {});
|
|
342
356
|
await stateOwner.release();
|
|
343
357
|
}
|
|
358
|
+
|
|
359
|
+
if (isRunnerDecommissionedError(decommissionedError)) {
|
|
360
|
+
const isRoot = typeof process.getuid === 'function' && process.getuid() === 0;
|
|
361
|
+
const result = await decommissionRunnerHost({
|
|
362
|
+
configPath: path,
|
|
363
|
+
stateDirectory: config.stateDirectory,
|
|
364
|
+
removeSystemdService: isRoot,
|
|
365
|
+
});
|
|
366
|
+
const reason = decommissionedError.code === 'runner_deleted' ? 'deleted' : 'revoked';
|
|
367
|
+
console.error(
|
|
368
|
+
`Runner was ${reason} by the control plane; removed ${result.removedPaths.length} local path(s)${result.removedService ? ' and systemd service' : ''}.`,
|
|
369
|
+
);
|
|
370
|
+
process.exitCode = RUNNER_DECOMMISSIONED_EXIT_CODE;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const runDecommissionHost = async (
|
|
375
|
+
options: Map<string, string[]>,
|
|
376
|
+
flags: Set<string>,
|
|
377
|
+
): Promise<void> => {
|
|
378
|
+
if (flags.has('from-systemd')) {
|
|
379
|
+
const exitCode = process.env.EXIT_CODE;
|
|
380
|
+
const exitStatus = process.env.EXIT_STATUS;
|
|
381
|
+
if (exitCode !== 'exited' || exitStatus !== String(RUNNER_DECOMMISSIONED_EXIT_CODE)) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const path = configPath(options);
|
|
386
|
+
const stateDirectory = options.get('state-directory')?.at(-1);
|
|
387
|
+
const serviceName = options.get('service-name')?.at(-1);
|
|
388
|
+
const result = await decommissionRunnerHost({
|
|
389
|
+
configPath: path,
|
|
390
|
+
stateDirectory,
|
|
391
|
+
removeSystemdService: true,
|
|
392
|
+
serviceName,
|
|
393
|
+
});
|
|
394
|
+
console.log(
|
|
395
|
+
`Decommissioned runner host: removed ${result.removedPaths.length} path(s)${result.removedService ? ' and systemd service' : ''}`,
|
|
396
|
+
);
|
|
397
|
+
for (const warning of result.warnings) {
|
|
398
|
+
console.error(`Warning: ${warning}`);
|
|
399
|
+
}
|
|
344
400
|
};
|
|
345
401
|
|
|
346
402
|
export const runCli = async (arguments_: string[]): Promise<void> => {
|
|
@@ -362,6 +418,9 @@ export const runCli = async (arguments_: string[]): Promise<void> => {
|
|
|
362
418
|
case 'once':
|
|
363
419
|
await runDaemon(options, true);
|
|
364
420
|
break;
|
|
421
|
+
case 'decommission-host':
|
|
422
|
+
await runDecommissionHost(options, flags);
|
|
423
|
+
break;
|
|
365
424
|
case 'config': {
|
|
366
425
|
const config = await loadRunnerConfig(configPath(options));
|
|
367
426
|
console.log(JSON.stringify(maskRunnerConfig(config), null, 2));
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
|
|
3
|
+
export interface IDecommissionHostOptions {
|
|
4
|
+
configPath?: string;
|
|
5
|
+
stateDirectory?: string;
|
|
6
|
+
removeSystemdService?: boolean;
|
|
7
|
+
serviceName?: string;
|
|
8
|
+
systemdUnitDir?: string;
|
|
9
|
+
runSystemctl?: (args: string[]) => Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface IDecommissionHostResult {
|
|
13
|
+
removedPaths: string[];
|
|
14
|
+
removedService: boolean;
|
|
15
|
+
warnings: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const isSafeRemovalPath = (targetPath: string): boolean => {
|
|
19
|
+
const resolved = plugins.path.resolve(targetPath);
|
|
20
|
+
if (!resolved || resolved === '/' || resolved === '/etc' || resolved === '/var' || resolved === '/var/lib') {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const defaultRunSystemctl = async (args: string[]): Promise<void> => {
|
|
27
|
+
await new Promise<void>((resolve, reject) => {
|
|
28
|
+
const child = plugins.childProcess.spawn('systemctl', args, {
|
|
29
|
+
stdio: 'ignore',
|
|
30
|
+
});
|
|
31
|
+
child.once('error', reject);
|
|
32
|
+
child.once('exit', (code) => {
|
|
33
|
+
if (code === 0) resolve();
|
|
34
|
+
else reject(new Error(`systemctl ${args.join(' ')} exited with status ${code ?? 'null'}`));
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const removeIfExists = async (
|
|
40
|
+
targetPath: string,
|
|
41
|
+
options: { recursive?: boolean } = {},
|
|
42
|
+
removedPaths: string[],
|
|
43
|
+
warnings: string[],
|
|
44
|
+
): Promise<void> => {
|
|
45
|
+
if (!isSafeRemovalPath(targetPath)) {
|
|
46
|
+
warnings.push(`Refused to remove unsafe path: ${targetPath}`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const resolved = plugins.path.resolve(targetPath);
|
|
50
|
+
try {
|
|
51
|
+
await plugins.fs.lstat(resolved);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if ((error as NodeJS.ErrnoException)?.code === 'ENOENT') return;
|
|
54
|
+
warnings.push(
|
|
55
|
+
`Could not inspect ${resolved}: ${error instanceof Error ? error.message : String(error)}`,
|
|
56
|
+
);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
await plugins.fs.rm(resolved, {
|
|
61
|
+
recursive: Boolean(options.recursive),
|
|
62
|
+
force: true,
|
|
63
|
+
});
|
|
64
|
+
removedPaths.push(resolved);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
warnings.push(
|
|
67
|
+
`Could not remove ${resolved}: ${error instanceof Error ? error.message : String(error)}`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const removeEmptyDirIfNamed = async (
|
|
73
|
+
dirPath: string,
|
|
74
|
+
expectedBasename: string,
|
|
75
|
+
removedPaths: string[],
|
|
76
|
+
): Promise<void> => {
|
|
77
|
+
const resolved = plugins.path.resolve(dirPath);
|
|
78
|
+
if (!isSafeRemovalPath(resolved) || plugins.path.basename(resolved) !== expectedBasename) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const entries = await plugins.fs.readdir(resolved);
|
|
83
|
+
if (entries.length === 0) {
|
|
84
|
+
await plugins.fs.rmdir(resolved);
|
|
85
|
+
removedPaths.push(resolved);
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
// Best-effort cleanup of empty parent directory.
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Removes local runner credentials, configuration, state, and (when executed with host privileges)
|
|
94
|
+
* disables and deletes the systemd service unit so a revoked or deleted runner is completely
|
|
95
|
+
* removed from the host. Leaves shared host infrastructure like `smartvm-helper` untouched.
|
|
96
|
+
*/
|
|
97
|
+
export const decommissionRunnerHost = async (
|
|
98
|
+
options: IDecommissionHostOptions,
|
|
99
|
+
): Promise<IDecommissionHostResult> => {
|
|
100
|
+
const removedPaths: string[] = [];
|
|
101
|
+
const warnings: string[] = [];
|
|
102
|
+
let removedService = false;
|
|
103
|
+
|
|
104
|
+
let effectiveStateDir = options.stateDirectory;
|
|
105
|
+
if (!effectiveStateDir && options.configPath) {
|
|
106
|
+
try {
|
|
107
|
+
const raw = await plugins.fs.readFile(options.configPath, 'utf8');
|
|
108
|
+
const parsed = JSON.parse(raw) as { stateDirectory?: unknown };
|
|
109
|
+
if (typeof parsed.stateDirectory === 'string' && parsed.stateDirectory.trim()) {
|
|
110
|
+
effectiveStateDir = parsed.stateDirectory.trim();
|
|
111
|
+
}
|
|
112
|
+
} catch {
|
|
113
|
+
// Config may already be removed or unreadable.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (effectiveStateDir) {
|
|
118
|
+
await removeIfExists(effectiveStateDir, { recursive: true }, removedPaths, warnings);
|
|
119
|
+
await removeEmptyDirIfNamed(
|
|
120
|
+
plugins.path.dirname(effectiveStateDir),
|
|
121
|
+
'ship-zone-runner',
|
|
122
|
+
removedPaths,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (options.configPath) {
|
|
127
|
+
await removeIfExists(options.configPath, { recursive: false }, removedPaths, warnings);
|
|
128
|
+
await removeEmptyDirIfNamed(
|
|
129
|
+
plugins.path.dirname(options.configPath),
|
|
130
|
+
'ship-zone-runner',
|
|
131
|
+
removedPaths,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (options.removeSystemdService) {
|
|
136
|
+
const serviceName = (options.serviceName?.trim() || 'ship-zone-runner').replace(/\.service$/, '');
|
|
137
|
+
const unitDir = options.systemdUnitDir || '/etc/systemd/system';
|
|
138
|
+
const unitPath = plugins.path.join(unitDir, `${serviceName}.service`);
|
|
139
|
+
const runSystemctl = options.runSystemctl ?? defaultRunSystemctl;
|
|
140
|
+
|
|
141
|
+
let unitExists = false;
|
|
142
|
+
try {
|
|
143
|
+
await plugins.fs.lstat(unitPath);
|
|
144
|
+
unitExists = true;
|
|
145
|
+
} catch {
|
|
146
|
+
unitExists = false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (unitExists) {
|
|
150
|
+
await runSystemctl(['disable', `${serviceName}.service`]).catch((error) => {
|
|
151
|
+
warnings.push(
|
|
152
|
+
`systemctl disable failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
await removeIfExists(unitPath, { recursive: false }, removedPaths, warnings);
|
|
156
|
+
await runSystemctl(['daemon-reload']).catch((error) => {
|
|
157
|
+
warnings.push(
|
|
158
|
+
`systemctl daemon-reload failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
await runSystemctl(['reset-failed', `${serviceName}.service`]).catch(() => {});
|
|
162
|
+
removedService = true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return { removedPaths, removedService, warnings };
|
|
167
|
+
};
|
package/ts/errors.ts
CHANGED
|
@@ -46,13 +46,22 @@ export class CoordinatorError extends RunnerError {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
export const RUNNER_DECOMMISSIONED_EXIT_CODE = 42;
|
|
50
|
+
|
|
49
51
|
export class AuthenticationError extends CoordinatorError {
|
|
50
|
-
constructor(
|
|
52
|
+
constructor(
|
|
53
|
+
message = 'Runner authentication was rejected',
|
|
54
|
+
public readonly code?: string,
|
|
55
|
+
) {
|
|
51
56
|
super(message, 401);
|
|
52
57
|
this.name = 'AuthenticationError';
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
|
|
61
|
+
export const isRunnerDecommissionedError = (error: unknown): error is AuthenticationError =>
|
|
62
|
+
error instanceof AuthenticationError &&
|
|
63
|
+
(error.code === 'runner_revoked' || error.code === 'runner_deleted');
|
|
64
|
+
|
|
56
65
|
export class LeaseLostError extends CoordinatorError {
|
|
57
66
|
constructor(message = 'The job lease is no longer valid') {
|
|
58
67
|
super(message, 409);
|
package/ts/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './api-client.js';
|
|
|
2
2
|
export * from './artifact-manager.js';
|
|
3
3
|
export * from './cache-manager.js';
|
|
4
4
|
export * from './config.js';
|
|
5
|
+
export * from './decommission.js';
|
|
5
6
|
export * from './egress-proxy.js';
|
|
6
7
|
export * from './errors.js';
|
|
7
8
|
export * from './interfaces.js';
|
package/ts/interfaces.ts
CHANGED
|
@@ -123,9 +123,17 @@ export interface IStartSessionResponse {
|
|
|
123
123
|
export interface IAvailableResources {
|
|
124
124
|
cpus: number;
|
|
125
125
|
memoryBytes: number;
|
|
126
|
+
diskBytes?: number;
|
|
126
127
|
slots: number;
|
|
127
128
|
}
|
|
128
129
|
|
|
130
|
+
export interface IResourceUsage {
|
|
131
|
+
peakMemoryBytes?: number;
|
|
132
|
+
peakDiskBytes?: number;
|
|
133
|
+
peakCpus?: number;
|
|
134
|
+
durationMs?: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
129
137
|
export interface IRunnerHeartbeatRequest {
|
|
130
138
|
runnerId: string;
|
|
131
139
|
sessionId: string;
|
|
@@ -252,6 +260,7 @@ export interface ICompleteJobRequest {
|
|
|
252
260
|
completedAt: string;
|
|
253
261
|
cancellationRequestId?: string;
|
|
254
262
|
error?: string;
|
|
263
|
+
resourceUsage?: IResourceUsage;
|
|
255
264
|
}
|
|
256
265
|
|
|
257
266
|
export interface ICompleteJobResponse {
|
|
@@ -710,6 +719,7 @@ export interface IExecutionResult {
|
|
|
710
719
|
stopReason?: TExecutionStopReason;
|
|
711
720
|
producedImage?: IProducedImageManifest;
|
|
712
721
|
workspace?: IRuntimeWorkspaceExporter;
|
|
722
|
+
resourceUsage?: IResourceUsage;
|
|
713
723
|
}
|
|
714
724
|
|
|
715
725
|
export interface IJobExecutor {
|
package/ts/microvm-executor.ts
CHANGED
|
@@ -1001,10 +1001,13 @@ export class MicroVmExecutor implements IJobExecutor {
|
|
|
1001
1001
|
});
|
|
1002
1002
|
}
|
|
1003
1003
|
|
|
1004
|
-
const
|
|
1004
|
+
const executionStartedAt = this.now();
|
|
1005
|
+
const attemptDeadline = executionStartedAt + context.timeoutMs;
|
|
1005
1006
|
let lastExitCode: number | null = 0;
|
|
1006
1007
|
let stopReason: TExecutionStopReason | undefined;
|
|
1007
1008
|
let producedImage: IExecutionResult['producedImage'];
|
|
1009
|
+
let peakWorkspaceBytes = packed.tarBytes.byteLength;
|
|
1010
|
+
const selectedShape = runtime.shapes.find((s) => s.id === context.preparedLease.shapeId);
|
|
1008
1011
|
|
|
1009
1012
|
if ('build' in job) {
|
|
1010
1013
|
const pushGrant = context.lease.grants.find((g) => g.access === 'push');
|
|
@@ -1227,6 +1230,14 @@ export class MicroVmExecutor implements IJobExecutor {
|
|
|
1227
1230
|
}
|
|
1228
1231
|
}
|
|
1229
1232
|
|
|
1233
|
+
if (
|
|
1234
|
+
typeof stepResult.workspaceBytes === 'number' &&
|
|
1235
|
+
Number.isFinite(stepResult.workspaceBytes) &&
|
|
1236
|
+
stepResult.workspaceBytes > peakWorkspaceBytes
|
|
1237
|
+
) {
|
|
1238
|
+
peakWorkspaceBytes = Math.floor(stepResult.workspaceBytes);
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1230
1241
|
if (logFailure) {
|
|
1231
1242
|
stopReason = 'log_delivery_failed';
|
|
1232
1243
|
lastExitCode = null;
|
|
@@ -1296,6 +1307,14 @@ export class MicroVmExecutor implements IJobExecutor {
|
|
|
1296
1307
|
...(stopReason ? { stopReason } : {}),
|
|
1297
1308
|
...(producedImage ? { producedImage } : {}),
|
|
1298
1309
|
workspace: exporter,
|
|
1310
|
+
resourceUsage: {
|
|
1311
|
+
peakMemoryBytes: selectedShape
|
|
1312
|
+
? selectedShape.memMib * 1024 * 1024
|
|
1313
|
+
: memoryBytes,
|
|
1314
|
+
peakDiskBytes: Math.max(0, Math.floor(peakWorkspaceBytes)),
|
|
1315
|
+
peakCpus: selectedShape ? selectedShape.vcpus : cpus,
|
|
1316
|
+
durationMs: Math.max(0, Math.floor(this.now() - executionStartedAt)),
|
|
1317
|
+
},
|
|
1299
1318
|
};
|
|
1300
1319
|
} catch (error) {
|
|
1301
1320
|
try {
|
package/ts/resource-budget.ts
CHANGED
|
@@ -8,22 +8,27 @@ import type {
|
|
|
8
8
|
} from './interfaces.js';
|
|
9
9
|
|
|
10
10
|
const mib = 1024 * 1024;
|
|
11
|
+
const gib = 1024 * mib;
|
|
11
12
|
const maxWireCpus = 1024;
|
|
12
13
|
const maxWireMemoryBytes = 4_398_046_511_104;
|
|
14
|
+
const maxWireDiskBytes = 109_951_162_777_600;
|
|
13
15
|
const maxWireSlots = 1024;
|
|
14
16
|
const defaultHostMemoryReserveRatio = 0.15;
|
|
15
17
|
const minimumHostMemoryReserveBytes = 512 * mib;
|
|
18
|
+
const defaultHostDiskReserveBytes = 4 * gib;
|
|
16
19
|
|
|
17
20
|
export interface IResourceReservation {
|
|
18
21
|
readonly attemptId: string;
|
|
19
22
|
readonly cpus: number;
|
|
20
23
|
readonly memoryBytes: number;
|
|
24
|
+
readonly diskBytes: number;
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
export interface IResourceBudgetHostProbe {
|
|
24
28
|
totalCpus(): number;
|
|
25
29
|
totalMemoryBytes(): number;
|
|
26
30
|
availableMemoryBytes(): number | undefined;
|
|
31
|
+
availableDiskBytes?(): number | undefined;
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
/**
|
|
@@ -43,7 +48,29 @@ export const readLinuxMemAvailableBytes = (): number | undefined => {
|
|
|
43
48
|
}
|
|
44
49
|
};
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Reads live free disk bytes on the microVM storage volume (`/var/lib/smartvm` if present,
|
|
53
|
+
* otherwise `fallbackDir`). Returns `undefined` when `statfsSync` is unavailable.
|
|
54
|
+
*/
|
|
55
|
+
export const readAvailableDiskBytes = (fallbackDir: string): number | undefined => {
|
|
56
|
+
const candidates = ['/var/lib/smartvm', fallbackDir];
|
|
57
|
+
for (const dir of candidates) {
|
|
58
|
+
try {
|
|
59
|
+
if (!dir) continue;
|
|
60
|
+
const stat = plugins.fsSync.statfsSync(dir);
|
|
61
|
+
const bavail = Number(stat.bavail);
|
|
62
|
+
const bsize = Number(stat.bsize);
|
|
63
|
+
if (Number.isFinite(bavail) && Number.isFinite(bsize) && bavail >= 0 && bsize > 0) {
|
|
64
|
+
return Math.floor(bavail * bsize);
|
|
65
|
+
}
|
|
66
|
+
} catch {
|
|
67
|
+
// Try next candidate
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const defaultHostProbe = (stateDirectory: string): IResourceBudgetHostProbe => ({
|
|
47
74
|
totalCpus: () => {
|
|
48
75
|
const raw = typeof plugins.os.availableParallelism === 'function'
|
|
49
76
|
? plugins.os.availableParallelism()
|
|
@@ -52,6 +79,7 @@ const defaultHostProbe = (): IResourceBudgetHostProbe => ({
|
|
|
52
79
|
},
|
|
53
80
|
totalMemoryBytes: () => Math.max(0, plugins.os.totalmem()),
|
|
54
81
|
availableMemoryBytes: readLinuxMemAvailableBytes,
|
|
82
|
+
availableDiskBytes: () => readAvailableDiskBytes(stateDirectory),
|
|
55
83
|
});
|
|
56
84
|
|
|
57
85
|
const clampCpus = (value: number): number =>
|
|
@@ -60,11 +88,14 @@ const clampCpus = (value: number): number =>
|
|
|
60
88
|
const clampBytes = (value: number): number =>
|
|
61
89
|
Math.max(0, Math.min(maxWireMemoryBytes, Math.floor(value)));
|
|
62
90
|
|
|
91
|
+
const clampDiskBytes = (value: number): number =>
|
|
92
|
+
Math.max(0, Math.min(maxWireDiskBytes, Math.floor(value)));
|
|
93
|
+
|
|
63
94
|
const clampSlots = (value: number): number =>
|
|
64
95
|
Math.max(0, Math.min(maxWireSlots, Math.floor(value)));
|
|
65
96
|
|
|
66
97
|
/**
|
|
67
|
-
* Tracks live unreserved CPU, memory, and VM slot capacity for dynamic multi-job concurrency.
|
|
98
|
+
* Tracks live unreserved CPU, memory, disk, and VM slot capacity for dynamic multi-job concurrency.
|
|
68
99
|
* Reservations are taken when a lease is claimed and released immediately when its microVM exits.
|
|
69
100
|
*/
|
|
70
101
|
export class RunnerResourceBudget {
|
|
@@ -81,7 +112,7 @@ export class RunnerResourceBudget {
|
|
|
81
112
|
constructor(
|
|
82
113
|
private readonly config: IRunnerConfig,
|
|
83
114
|
runtime: IOciRuntimeFacts,
|
|
84
|
-
probe: IResourceBudgetHostProbe = defaultHostProbe(),
|
|
115
|
+
probe: IResourceBudgetHostProbe = defaultHostProbe(config.stateDirectory),
|
|
85
116
|
) {
|
|
86
117
|
this.probe = probe;
|
|
87
118
|
this.shapes = runtime.shapes;
|
|
@@ -137,9 +168,11 @@ export class RunnerResourceBudget {
|
|
|
137
168
|
public available(): IAvailableResources {
|
|
138
169
|
let reservedCpus = 0;
|
|
139
170
|
let reservedMemoryBytes = 0;
|
|
171
|
+
let reservedDiskBytes = 0;
|
|
140
172
|
for (const reservation of this.reservations.values()) {
|
|
141
173
|
reservedCpus += reservation.cpus;
|
|
142
174
|
reservedMemoryBytes += reservation.memoryBytes;
|
|
175
|
+
reservedDiskBytes += reservation.diskBytes;
|
|
143
176
|
}
|
|
144
177
|
const slots = clampSlots(this.totalSlots - this.reservations.size);
|
|
145
178
|
const cpus = clampCpus(this.totalCpus - reservedCpus);
|
|
@@ -151,7 +184,18 @@ export class RunnerResourceBudget {
|
|
|
151
184
|
// throttles new claims on the fly without double-counting already-running microVMs.
|
|
152
185
|
memoryBytes = clampBytes(Math.min(memoryBytes, liveUsable));
|
|
153
186
|
}
|
|
154
|
-
|
|
187
|
+
const liveDiskAvailable = this.probe.availableDiskBytes?.();
|
|
188
|
+
const diskBytes = liveDiskAvailable !== undefined
|
|
189
|
+
? clampDiskBytes(
|
|
190
|
+
Math.max(0, liveDiskAvailable - defaultHostDiskReserveBytes - reservedDiskBytes),
|
|
191
|
+
)
|
|
192
|
+
: undefined;
|
|
193
|
+
return {
|
|
194
|
+
cpus,
|
|
195
|
+
memoryBytes,
|
|
196
|
+
...(diskBytes !== undefined ? { diskBytes } : {}),
|
|
197
|
+
slots,
|
|
198
|
+
};
|
|
155
199
|
}
|
|
156
200
|
|
|
157
201
|
public canClaimAnother(): boolean {
|
|
@@ -167,11 +211,16 @@ export class RunnerResourceBudget {
|
|
|
167
211
|
return current.memoryBytes >= minNeededMemory;
|
|
168
212
|
}
|
|
169
213
|
|
|
170
|
-
/** Computes the shape-rounded CPU and
|
|
171
|
-
public estimateLeaseFootprint(
|
|
214
|
+
/** Computes the shape-rounded CPU, memory, and disk footprint a claimed lease will occupy. */
|
|
215
|
+
public estimateLeaseFootprint(
|
|
216
|
+
lease: IOciJobLease,
|
|
217
|
+
): { cpus: number; memoryBytes: number; diskBytes: number } {
|
|
172
218
|
const requestedCpus = lease.job.resources?.cpus ?? this.config.oci.defaultCpus;
|
|
173
219
|
const requestedMemory = (lease.job.resources?.memoryBytes ?? this.config.oci.defaultMemoryBytes) +
|
|
174
220
|
this.config.oci.guestMemoryOverheadBytes;
|
|
221
|
+
const diskBytes = clampDiskBytes(
|
|
222
|
+
lease.job.requirements.limits.maximumWorkspaceBytes || this.config.oci.workspaceSizeBytes,
|
|
223
|
+
);
|
|
175
224
|
|
|
176
225
|
// Match the smallest VM shape that fits, mirroring MicroVmExecutor.selectShapeForJob.
|
|
177
226
|
const fitting = this.shapes
|
|
@@ -181,11 +230,13 @@ export class RunnerResourceBudget {
|
|
|
181
230
|
return {
|
|
182
231
|
cpus: fitting[0].vcpus,
|
|
183
232
|
memoryBytes: fitting[0].memMib * mib,
|
|
233
|
+
diskBytes,
|
|
184
234
|
};
|
|
185
235
|
}
|
|
186
236
|
return {
|
|
187
237
|
cpus: requestedCpus,
|
|
188
238
|
memoryBytes: requestedMemory,
|
|
239
|
+
diskBytes,
|
|
189
240
|
};
|
|
190
241
|
}
|
|
191
242
|
|
|
@@ -195,6 +246,7 @@ export class RunnerResourceBudget {
|
|
|
195
246
|
attemptId,
|
|
196
247
|
cpus: footprint.cpus,
|
|
197
248
|
memoryBytes: footprint.memoryBytes,
|
|
249
|
+
diskBytes: footprint.diskBytes,
|
|
198
250
|
};
|
|
199
251
|
this.reservations.set(attemptId, reservation);
|
|
200
252
|
return reservation;
|
package/ts/runner.ts
CHANGED
|
@@ -751,7 +751,8 @@ export class ShipRunner {
|
|
|
751
751
|
heartbeatState,
|
|
752
752
|
heartbeatLifecycle.signal,
|
|
753
753
|
);
|
|
754
|
-
const
|
|
754
|
+
const startedAtMs = this.now();
|
|
755
|
+
const startedAt = new Date(startedAtMs).toISOString();
|
|
755
756
|
const redactionSet = SecretRedactionSet.forLease(lease);
|
|
756
757
|
const logs = new LogSender(
|
|
757
758
|
this.dependencies.api,
|
|
@@ -763,6 +764,7 @@ export class ShipRunner {
|
|
|
763
764
|
);
|
|
764
765
|
let stagingWorkspace: IPreparedWorkspace | undefined;
|
|
765
766
|
let runtimeWorkspace: Awaited<ReturnType<IJobExecutor['execute']>>['workspace'];
|
|
767
|
+
let executionResourceUsage: ICompleteJobRequest['resourceUsage'] | undefined;
|
|
766
768
|
let status: TJobCompletionStatus = 'system_error';
|
|
767
769
|
let exitCode: number | null = null;
|
|
768
770
|
let systemFailure: unknown;
|
|
@@ -848,6 +850,9 @@ export class ShipRunner {
|
|
|
848
850
|
onLog: (stream, data) => logs.write(stream, data),
|
|
849
851
|
});
|
|
850
852
|
runtimeWorkspace = result.workspace;
|
|
853
|
+
if (result.resourceUsage) {
|
|
854
|
+
executionResourceUsage = result.resourceUsage;
|
|
855
|
+
}
|
|
851
856
|
if (isImageBuild) {
|
|
852
857
|
exitCode = null;
|
|
853
858
|
if (result.stopReason) {
|
|
@@ -1030,19 +1035,28 @@ export class ShipRunner {
|
|
|
1030
1035
|
exitCode = null;
|
|
1031
1036
|
}
|
|
1032
1037
|
|
|
1038
|
+
const completedAtMs = this.now();
|
|
1033
1039
|
const completion: ICompleteJobRequest = {
|
|
1034
1040
|
completionRequestId: plugins.crypto.randomUUID(),
|
|
1035
1041
|
identity,
|
|
1036
1042
|
status,
|
|
1037
1043
|
exitCode,
|
|
1038
1044
|
startedAt,
|
|
1039
|
-
completedAt: new Date(
|
|
1045
|
+
completedAt: new Date(completedAtMs).toISOString(),
|
|
1040
1046
|
...(status === 'canceled'
|
|
1041
1047
|
? { cancellationRequestId: heartbeatState.cancellation!.requestId }
|
|
1042
1048
|
: {}),
|
|
1043
1049
|
...(status === 'system_error' || systemFailure !== undefined
|
|
1044
1050
|
? { error: genericSystemError }
|
|
1045
1051
|
: {}),
|
|
1052
|
+
...(executionResourceUsage
|
|
1053
|
+
? {
|
|
1054
|
+
resourceUsage: {
|
|
1055
|
+
...executionResourceUsage,
|
|
1056
|
+
durationMs: executionResourceUsage.durationMs ?? Math.max(0, Math.floor(completedAtMs - startedAtMs)),
|
|
1057
|
+
},
|
|
1058
|
+
}
|
|
1059
|
+
: {}),
|
|
1046
1060
|
};
|
|
1047
1061
|
try {
|
|
1048
1062
|
await this.dependencies.api.completeJob(completion);
|