@nosana/node 1.1.84-rc → 1.1.85-rc

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.
Files changed (180) hide show
  1. package/dist/.env +2 -1
  2. package/dist/package.json +1 -1
  3. package/dist/packages/job-runtime/src/host/HostClient.d.ts +20 -4
  4. package/dist/packages/job-runtime/src/host/HostClient.js +35 -2
  5. package/dist/packages/job-runtime/src/host/contract.d.ts +27 -7
  6. package/dist/packages/job-runtime/src/host/contract.js +43 -7
  7. package/dist/packages/job-runtime/src/runtime/runJob.d.ts +8 -5
  8. package/dist/packages/job-runtime/src/runtime/runJob.js +44 -17
  9. package/dist/packages/vm-orchestrator/src/CidRegistry.d.ts +67 -0
  10. package/dist/packages/vm-orchestrator/src/CidRegistry.js +168 -0
  11. package/dist/packages/vm-orchestrator/src/GpuPool.d.ts +27 -0
  12. package/dist/packages/vm-orchestrator/src/GpuPool.js +41 -0
  13. package/dist/packages/vm-orchestrator/src/Hypervisor.d.ts +66 -0
  14. package/dist/packages/vm-orchestrator/src/Hypervisor.js +7 -0
  15. package/dist/packages/vm-orchestrator/src/QemuHypervisor.d.ts +62 -0
  16. package/dist/packages/vm-orchestrator/src/QemuHypervisor.js +185 -0
  17. package/dist/packages/vm-orchestrator/src/QemuHypervisorConfig.d.ts +61 -0
  18. package/dist/packages/vm-orchestrator/src/QemuHypervisorConfig.js +37 -0
  19. package/dist/packages/vm-orchestrator/src/QemuVmInstance.d.ts +37 -0
  20. package/dist/packages/vm-orchestrator/src/QemuVmInstance.js +44 -0
  21. package/dist/packages/vm-orchestrator/src/QmpClient.d.ts +43 -0
  22. package/dist/packages/vm-orchestrator/src/QmpClient.js +103 -0
  23. package/dist/packages/vm-orchestrator/src/VmSpec.d.ts +21 -0
  24. package/dist/packages/vm-orchestrator/src/VmSpec.js +30 -0
  25. package/dist/packages/vm-orchestrator/src/VmStateManager.d.ts +16 -0
  26. package/dist/packages/vm-orchestrator/src/VmStateManager.js +30 -0
  27. package/dist/packages/vm-orchestrator/src/bootAttempt.d.ts +31 -0
  28. package/dist/packages/vm-orchestrator/src/bootAttempt.js +193 -0
  29. package/dist/packages/vm-orchestrator/src/bootErrors.d.ts +33 -0
  30. package/dist/packages/vm-orchestrator/src/bootErrors.js +67 -0
  31. package/dist/packages/vm-orchestrator/src/buildCloudInit.d.ts +33 -0
  32. package/dist/packages/vm-orchestrator/src/buildCloudInit.js +55 -0
  33. package/dist/packages/vm-orchestrator/src/buildQemuArgs.d.ts +33 -0
  34. package/dist/packages/vm-orchestrator/src/buildQemuArgs.js +127 -0
  35. package/dist/packages/vm-orchestrator/src/diskSizing.d.ts +18 -0
  36. package/dist/packages/vm-orchestrator/src/diskSizing.js +41 -0
  37. package/dist/packages/vm-orchestrator/src/firmware.d.ts +5 -0
  38. package/dist/packages/vm-orchestrator/src/firmware.js +18 -0
  39. package/dist/packages/vm-orchestrator/src/hostAccess/VmHostAccess.d.ts +22 -0
  40. package/dist/packages/vm-orchestrator/src/hostAccess/VmHostAccess.js +33 -0
  41. package/dist/packages/vm-orchestrator/src/hostAccess/deviceProblems.d.ts +3 -0
  42. package/dist/packages/vm-orchestrator/src/hostAccess/deviceProblems.js +17 -0
  43. package/dist/packages/vm-orchestrator/src/hostAccess/findVmHostAccessProblems.d.ts +9 -0
  44. package/dist/packages/vm-orchestrator/src/hostAccess/findVmHostAccessProblems.js +17 -0
  45. package/dist/packages/vm-orchestrator/src/hostAccess/gpuProblems.d.ts +6 -0
  46. package/dist/packages/vm-orchestrator/src/hostAccess/gpuProblems.js +42 -0
  47. package/dist/packages/vm-orchestrator/src/hostAccess/index.d.ts +1 -0
  48. package/dist/packages/vm-orchestrator/src/hostAccess/index.js +1 -0
  49. package/dist/packages/vm-orchestrator/src/hostAccess/memlockProblems.d.ts +6 -0
  50. package/dist/packages/vm-orchestrator/src/hostAccess/memlockProblems.js +18 -0
  51. package/dist/packages/vm-orchestrator/src/hostAccess/workRootProblems.d.ts +7 -0
  52. package/dist/packages/vm-orchestrator/src/hostAccess/workRootProblems.js +30 -0
  53. package/dist/packages/vm-orchestrator/src/hostDevices.d.ts +12 -0
  54. package/dist/packages/vm-orchestrator/src/hostDevices.js +12 -0
  55. package/dist/packages/vm-orchestrator/src/hostTools.d.ts +25 -0
  56. package/dist/packages/vm-orchestrator/src/hostTools.js +39 -0
  57. package/dist/packages/vm-orchestrator/src/imageCache.d.ts +25 -0
  58. package/dist/packages/vm-orchestrator/src/imageCache.js +73 -0
  59. package/dist/packages/vm-orchestrator/src/imageDownload.d.ts +31 -0
  60. package/dist/packages/vm-orchestrator/src/imageDownload.js +85 -0
  61. package/dist/packages/vm-orchestrator/src/index.d.ts +19 -0
  62. package/dist/packages/vm-orchestrator/src/index.js +15 -0
  63. package/dist/packages/vm-orchestrator/src/memlockLimit.d.ts +5 -0
  64. package/dist/packages/vm-orchestrator/src/memlockLimit.js +23 -0
  65. package/dist/packages/vm-orchestrator/src/nvidiaHolders.d.ts +11 -0
  66. package/dist/packages/vm-orchestrator/src/nvidiaHolders.js +74 -0
  67. package/dist/packages/vm-orchestrator/src/pciNuma.d.ts +7 -0
  68. package/dist/packages/vm-orchestrator/src/pciNuma.js +18 -0
  69. package/dist/packages/vm-orchestrator/src/pciSysfs.d.ts +21 -0
  70. package/dist/packages/vm-orchestrator/src/pciSysfs.js +47 -0
  71. package/dist/packages/vm-orchestrator/src/processes.d.ts +37 -0
  72. package/dist/packages/vm-orchestrator/src/processes.js +102 -0
  73. package/dist/packages/vm-orchestrator/src/sockets.d.ts +21 -0
  74. package/dist/packages/vm-orchestrator/src/sockets.js +58 -0
  75. package/dist/packages/vm-orchestrator/src/sweepVmImageCache.d.ts +19 -0
  76. package/dist/packages/vm-orchestrator/src/sweepVmImageCache.js +132 -0
  77. package/dist/packages/vm-orchestrator/src/util.d.ts +14 -0
  78. package/dist/packages/vm-orchestrator/src/util.js +33 -0
  79. package/dist/packages/vm-orchestrator/src/vfioBinding.d.ts +27 -0
  80. package/dist/packages/vm-orchestrator/src/vfioBinding.js +163 -0
  81. package/dist/packages/vm-orchestrator/src/vmImageResolver.d.ts +20 -0
  82. package/dist/packages/vm-orchestrator/src/vmImageResolver.js +62 -0
  83. package/dist/packages/vm-orchestrator/src/vsockBridge.d.ts +20 -0
  84. package/dist/packages/vm-orchestrator/src/vsockBridge.js +27 -0
  85. package/dist/packages/vm-orchestrator/src/workDirs.d.ts +49 -0
  86. package/dist/packages/vm-orchestrator/src/workDirs.js +79 -0
  87. package/dist/src/NodeManager/configs/configs.d.ts +4 -0
  88. package/dist/src/NodeManager/configs/configs.js +3 -0
  89. package/dist/src/NodeManager/db/index.d.ts +10 -0
  90. package/dist/src/NodeManager/db/index.js +4 -0
  91. package/dist/src/NodeManager/monitoring/log/NodeLog.d.ts +2 -0
  92. package/dist/src/NodeManager/monitoring/log/NodeLog.js +21 -0
  93. package/dist/src/NodeManager/monitoring/log/console/ConsoleLogger.d.ts +8 -0
  94. package/dist/src/NodeManager/monitoring/log/console/ConsoleLogger.js +29 -5
  95. package/dist/src/NodeManager/monitoring/log/vmSetupLog.d.ts +11 -0
  96. package/dist/src/NodeManager/monitoring/log/vmSetupLog.js +33 -0
  97. package/dist/src/NodeManager/node/Node.d.ts +2 -0
  98. package/dist/src/NodeManager/node/Node.js +12 -5
  99. package/dist/src/NodeManager/node/job/ContainerJobRuntime.d.ts +141 -0
  100. package/dist/src/NodeManager/node/job/ContainerJobRuntime.js +419 -0
  101. package/dist/src/NodeManager/node/job/JobRuntimeEnvironment.d.ts +74 -0
  102. package/dist/src/NodeManager/node/job/JobRuntimeEnvironment.js +1 -0
  103. package/dist/src/NodeManager/node/job/JobRuntimeHost.d.ts +6 -2
  104. package/dist/src/NodeManager/node/job/JobRuntimeHost.js +13 -2
  105. package/dist/src/NodeManager/node/job/JobRuntimeSession.d.ts +12 -9
  106. package/dist/src/NodeManager/node/job/JobRuntimeSession.js +42 -71
  107. package/dist/src/NodeManager/node/job/VmJobRuntime.d.ts +109 -0
  108. package/dist/src/NodeManager/node/job/VmJobRuntime.js +258 -0
  109. package/dist/src/NodeManager/node/job/VmJobRuntimeConfig.d.ts +33 -0
  110. package/dist/src/NodeManager/node/job/VmJobRuntimeConfig.js +1 -0
  111. package/dist/src/NodeManager/node/job/buildJobRuntimeEnv.d.ts +41 -0
  112. package/dist/src/NodeManager/node/job/buildJobRuntimeEnv.js +25 -0
  113. package/dist/src/NodeManager/node/job/followFile.d.ts +14 -0
  114. package/dist/src/NodeManager/node/job/followFile.js +57 -0
  115. package/dist/src/NodeManager/node/job/jobHandler.d.ts +3 -3
  116. package/dist/src/NodeManager/node/job/jobHandler.js +7 -7
  117. package/dist/src/NodeManager/node/job/keepFailedConsole.d.ts +7 -0
  118. package/dist/src/NodeManager/node/job/keepFailedConsole.js +52 -0
  119. package/dist/src/NodeManager/node/job/selectJobRuntimeEnvironment.d.ts +50 -0
  120. package/dist/src/NodeManager/node/job/selectJobRuntimeEnvironment.js +117 -0
  121. package/dist/src/NodeManager/node/market/benchmark.d.ts +3 -3
  122. package/dist/src/NodeManager/node/market/benchmark.js +3 -3
  123. package/dist/src/NodeManager/node/market/marketHandler.d.ts +3 -3
  124. package/dist/src/NodeManager/node/market/marketHandler.js +3 -3
  125. package/dist/src/NodeManager/node/resource/resourceManager.d.ts +1 -1
  126. package/dist/src/NodeManager/node/resource/resourceManager.js +1 -1
  127. package/dist/src/NodeManager/provider/Provider.d.ts +3 -121
  128. package/dist/src/NodeManager/provider/Provider.js +3 -369
  129. package/dist/src/NodeManager/provider/vmHost/VmNodeOptions.d.ts +14 -0
  130. package/dist/src/NodeManager/provider/vmHost/VmNodeOptions.js +1 -0
  131. package/dist/src/NodeManager/provider/vmHost/capacity.d.ts +16 -0
  132. package/dist/src/NodeManager/provider/vmHost/capacity.js +47 -0
  133. package/dist/src/NodeManager/provider/vmHost/gpus.d.ts +19 -0
  134. package/dist/src/NodeManager/provider/vmHost/gpus.js +83 -0
  135. package/dist/src/NodeManager/provider/vmHost/index.d.ts +5 -0
  136. package/dist/src/NodeManager/provider/vmHost/index.js +4 -0
  137. package/dist/src/NodeManager/provider/vmHost/readVmSpec.d.ts +11 -0
  138. package/dist/src/NodeManager/provider/vmHost/readVmSpec.js +22 -0
  139. package/dist/src/NodeManager/provider/vmHost/setup/PrepareVmHostSystem.d.ts +27 -0
  140. package/dist/src/NodeManager/provider/vmHost/setup/PrepareVmHostSystem.js +25 -0
  141. package/dist/src/NodeManager/provider/vmHost/setup/fileOwnership.d.ts +6 -0
  142. package/dist/src/NodeManager/provider/vmHost/setup/fileOwnership.js +22 -0
  143. package/dist/src/NodeManager/provider/vmHost/setup/hostUser.d.ts +15 -0
  144. package/dist/src/NodeManager/provider/vmHost/setup/hostUser.js +25 -0
  145. package/dist/src/NodeManager/provider/vmHost/setup/index.d.ts +2 -0
  146. package/dist/src/NodeManager/provider/vmHost/setup/index.js +2 -0
  147. package/dist/src/NodeManager/provider/vmHost/setup/memlockLimits.d.ts +6 -0
  148. package/dist/src/NodeManager/provider/vmHost/setup/memlockLimits.js +12 -0
  149. package/dist/src/NodeManager/provider/vmHost/setup/prepareVmHost.d.ts +17 -0
  150. package/dist/src/NodeManager/provider/vmHost/setup/prepareVmHost.js +90 -0
  151. package/dist/src/NodeManager/provider/vmHost/setup/sudoCaller.d.ts +5 -0
  152. package/dist/src/NodeManager/provider/vmHost/setup/sudoCaller.js +35 -0
  153. package/dist/src/NodeManager/provider/vmHost/vmHostEnv.d.ts +20 -0
  154. package/dist/src/NodeManager/provider/vmHost/vmHostEnv.js +24 -0
  155. package/dist/src/NodeManager/provider/vmHost/workRoot.d.ts +14 -0
  156. package/dist/src/NodeManager/provider/vmHost/workRoot.js +84 -0
  157. package/dist/src/NodeManager/repository/NodeRepository.d.ts +12 -2
  158. package/dist/src/NodeManager/repository/NodeRepository.js +21 -1
  159. package/dist/src/cli/commands/prune/command.js +2 -1
  160. package/dist/src/cli/commands/run/action.d.ts +2 -2
  161. package/dist/src/cli/commands/run/action.js +15 -34
  162. package/dist/src/cli/commands/run/command.js +7 -2
  163. package/dist/src/cli/commands/sharedOptions/--config.d.ts +2 -0
  164. package/dist/src/cli/commands/sharedOptions/--config.js +2 -0
  165. package/dist/src/cli/commands/sharedOptions/--environment.d.ts +6 -0
  166. package/dist/src/cli/commands/sharedOptions/--environment.js +6 -0
  167. package/dist/src/cli/commands/sharedOptions/--gpu.js +5 -2
  168. package/dist/src/cli/commands/sharedOptions/--node-limits.d.ts +9 -0
  169. package/dist/src/cli/commands/sharedOptions/--node-limits.js +24 -0
  170. package/dist/src/cli/commands/sharedOptions/index.d.ts +3 -0
  171. package/dist/src/cli/commands/sharedOptions/index.js +3 -0
  172. package/dist/src/cli/commands/start/command.js +7 -2
  173. package/dist/src/cli/commands/vm-setup/action.d.ts +10 -0
  174. package/dist/src/cli/commands/vm-setup/action.js +14 -0
  175. package/dist/src/cli/commands/vm-setup/command.d.ts +3 -0
  176. package/dist/src/cli/commands/vm-setup/command.js +11 -0
  177. package/dist/src/cli/createNosanaCli.d.ts +1 -0
  178. package/dist/src/cli/createNosanaCli.js +4 -1
  179. package/npm-shrinkwrap.json +10 -2
  180. package/package.json +1 -1
@@ -1,134 +1,16 @@
1
- import { NodeRepository } from '../repository/NodeRepository.js';
2
1
  import { ResourceManager } from '../node/resource/resourceManager.js';
3
2
  import { ContainerOrchestrationInterface } from '../../../packages/container-orchestrator/src/index.js';
4
- import { type JobRuntimeConsole, type JobRuntimeKind } from '../../../packages/job-runtime/src/host/index.js';
5
3
  /**
6
- * The node's containers: its API proxy, and each job's runtime sidecar, which
7
- * runs the job's own.
4
+ * The node's own containers: its API proxy. Each job's runtime is the job
5
+ * runtime environment's (see ContainerJobRuntime).
8
6
  */
9
7
  export declare class Provider {
10
8
  containerOrchestration: ContainerOrchestrationInterface;
11
- private repository;
12
- /** The node's image and resource cache, which a job's sidecar starts from. */
13
9
  readonly resourceManager: ResourceManager;
14
10
  private readonly frpcImage;
15
- private jobRuntimeVolumesSwept;
16
- private readonly jobRuntimeUrls;
17
11
  private proxyStartupAbortController;
18
- constructor(containerOrchestration: ContainerOrchestrationInterface, repository: NodeRepository,
19
- /** The node's image and resource cache, which a job's sidecar starts from. */
20
- resourceManager: ResourceManager);
12
+ constructor(containerOrchestration: ContainerOrchestrationInterface, resourceManager: ResourceManager);
21
13
  stopReverseProxyApi(address: string): Promise<boolean>;
22
14
  setUpReverseProxyApi(address: string, port: string): Promise<boolean>;
23
15
  generateApiFrpcProxies(address: string, port: string): any[];
24
- /**
25
- * Starts the job's runtime sidecar (packages/job-runtime) with the job's
26
- * address and the host it runs on, nothing about the job itself: an
27
- * onchain job it resolves onchain, runs, publishes its job API for at
28
- * https://<jobId>.<frp server> and pins the result of; an inline run's
29
- * definition is in the seed in `hostDir`, the job's directory, where the
30
- * node also serves it its socket (JobRuntimeHost).
31
- *
32
- * A sidecar a previous node left behind is replaced; the new one resumes
33
- * the job from the data volume they share. The sidecar has no restart
34
- * policy: the node starts it again if it exits before its result (see
35
- * JobRuntimeSession).
36
- */
37
- startJobRuntime(jobAddress: string, hostDir: string, kind: JobRuntimeKind,
38
- /** What its console carries; `entries` for `nosana node run` to render. */
39
- consoleOutput?: JobRuntimeConsole): Promise<void>;
40
- /**
41
- * The group a sidecar needs to use the runtime socket: the socket's own,
42
- * which is opened to its group where it is root's alone (podman serves its
43
- * socket that way; docker's is its `docker` group's already). The socket
44
- * is the one the node itself uses, bound into its container read-write,
45
- * so the change reaches the socket the sidecar gets bound. Undefined when
46
- * the socket cannot be read or changed (a TCP endpoint, a dev socket the
47
- * user does not own), in which case the sidecar runs as root, as it did
48
- * before, rather than not at all.
49
- */
50
- private jobRuntimeSocketGroup;
51
- /**
52
- * Makes the job's data volume the sidecar user's. A volume created as the
53
- * sidecar starts takes the ownership of the image's mount point, which is
54
- * the user's; one that is already there (a sidecar a previous node left,
55
- * one that may have run as root) is chowned first, by a one-shot container
56
- * on the same image, so that the sidecar can write its records.
57
- */
58
- private ownJobRuntimeVolume;
59
- /**
60
- * Whether the job has a sidecar, running or exited: one that outlived the
61
- * node's last process, to take over. Only the runtime saying there is no
62
- * such container answers false; a runtime that fails to answer (its daemon
63
- * still starting after a host restart, say) is asked again, for about two
64
- * minutes, before its last error is thrown.
65
- */
66
- hasJobRuntime(jobAddress: string): Promise<boolean>;
67
- /**
68
- * Whether the job's sidecar runs on an image other than the configured one
69
- * as the node has it locally (pulled by pullJobRuntimeImage before the job
70
- * loop): a node updated mid-job meets the sidecar of the version before it.
71
- * An exited sidecar is not outdated, nor is one whose image or the
72
- * configured one cannot be read: an image the node does not have is no
73
- * reason to replace a sidecar that runs.
74
- */
75
- isJobRuntimeOutdated(jobAddress: string): Promise<boolean>;
76
- /**
77
- * Waits for the job's sidecar to exit, and answers its exit code;
78
- * undefined once it is gone. A wait the runtime drops (its daemon
79
- * restarting) is waited again. Only the runtime saying there is no such
80
- * container means gone: one that fails to answer is asked again for as
81
- * long as it takes, since answering gone would have the node remove a
82
- * sidecar that may still run. The job's expiry is enforced apart from
83
- * this, and removing the sidecar ends the wait.
84
- */
85
- waitForJobRuntime(jobAddress: string): Promise<{
86
- exitCode: number;
87
- } | undefined>;
88
- /** Starts the job's exited sidecar again, as it was. */
89
- restartJobRuntime(jobAddress: string): Promise<void>;
90
- /**
91
- * Removes what job sidecars of this node left behind, the sidecars
92
- * themselves included, for every job but `liveJob`, which this node still
93
- * runs. A sidecar stamps its job and this node on everything it starts, so
94
- * this finds what a sidecar that died never removed, and no other node's.
95
- */
96
- removeOrphanedJobContainers(liveJob?: string): Promise<void>;
97
- /**
98
- * Where the job's sidecar serves its job API: its address on the gateway
99
- * network, which a node on the network and one on the host both reach.
100
- * Undefined while it is not running. An address is looked up again after
101
- * a few seconds, since a restarted sidecar may come back with another; a
102
- * miss is not kept, so a sidecar that is starting is found once it is up.
103
- */
104
- jobRuntimeUrl(jobAddress: string): Promise<string | undefined>;
105
- /**
106
- * Pulls the job runtime image, so the next job's sidecar finds it local.
107
- * Runs before each job loop; a cached image costs no registry round trip.
108
- */
109
- /**
110
- * Pulls the sidecar image `.env` names and records it in the image cache,
111
- * where the resync between jobs ages out what goes unused, so a bump's
112
- * predecessor goes the way of any other image.
113
- */
114
- pullJobRuntimeImage(): Promise<void>;
115
- /**
116
- * Removes the job's sidecar, then whatever else of the job is left: its
117
- * frpc, and any operation container a sidecar that died never removed.
118
- * Then the sidecar's data volume. Throws on failure; what will not go is
119
- * swept again by the next resync (removeOrphanedJobContainers).
120
- */
121
- stopJobRuntime(jobAddress: string): Promise<void>;
122
- /**
123
- * Removes job-runtime-<jobId> volumes left by a job whose stop never ran (the
124
- * node died mid-job). The volume resync does not know them, and a reaped
125
- * sidecar container leaves its named volume behind. Runs once, on the first
126
- * sidecar start after boot. The job starting keeps its volume, to resume
127
- * from, and a volume still in use fails to delete and is kept for the next
128
- * boot.
129
- */
130
- private removeOrphanedJobRuntimeVolumes;
131
- private hostSocketPath?;
132
- /** The runtime socket as a host path, which is what a bind source must be. */
133
- private resolveHostSocketPath;
134
16
  }
@@ -1,44 +1,14 @@
1
- import fs from 'fs/promises';
2
1
  import { configs } from '../configs/configs.js';
3
2
  import { applyLoggingProxyToClass } from '../../../packages/common/src/index.js';
4
- import { JOB_LABEL, NODE_LABEL, SERVICE_LABEL, } from '../../../packages/container-orchestrator/src/index.js';
5
- import { JOB_RUNTIME_CONTAINER_PREFIX, JOB_RUNTIME_DATA_DIR, JOB_RUNTIME_GID, JOB_RUNTIME_HOST_DIR, JOB_RUNTIME_PORT, JOB_RUNTIME_UID, } from '../../../packages/job-runtime/src/host/index.js';
6
- import { getKit, nodeAddress } from '../sdk/index.js';
7
- import { pkg } from '../../static/index.js';
8
- /** How long a job runtime's address is used before it is looked up again. */
9
- const JOB_RUNTIME_URL_TTL_MS = 5_000;
10
- /** How long to wait before asking a container runtime that failed again. */
11
- const JOB_RUNTIME_RETRY_MS = 5_000;
12
3
  /**
13
- * How many times a job's sidecar is looked for before the runtime is given
14
- * up on: about two minutes, long enough for a daemon coming back after a
15
- * host restart.
16
- */
17
- const JOB_RUNTIME_LOOKUP_ATTEMPTS = 24;
18
- /**
19
- * Whether a container runtime error says there is no such container: a 404,
20
- * from Docker or from Podman's compatible API alike.
21
- */
22
- function isNoSuchContainer(error) {
23
- return error?.statusCode === 404;
24
- }
25
- function sleep(ms) {
26
- return new Promise((resolve) => setTimeout(resolve, ms));
27
- }
28
- /**
29
- * The node's containers: its API proxy, and each job's runtime sidecar, which
30
- * runs the job's own.
4
+ * The node's own containers: its API proxy. Each job's runtime is the job
5
+ * runtime environment's (see ContainerJobRuntime).
31
6
  */
32
7
  export class Provider {
33
- constructor(containerOrchestration, repository,
34
- /** The node's image and resource cache, which a job's sidecar starts from. */
35
- resourceManager) {
8
+ constructor(containerOrchestration, resourceManager) {
36
9
  this.containerOrchestration = containerOrchestration;
37
- this.repository = repository;
38
10
  this.resourceManager = resourceManager;
39
11
  this.frpcImage = configs().frp.containerImage;
40
- this.jobRuntimeVolumesSwept = false;
41
- this.jobRuntimeUrls = new Map();
42
12
  this.proxyStartupAbortController = undefined;
43
13
  applyLoggingProxyToClass(this);
44
14
  }
@@ -151,340 +121,4 @@ export class Provider {
151
121
  ];
152
122
  return proxies;
153
123
  }
154
- /**
155
- * Starts the job's runtime sidecar (packages/job-runtime) with the job's
156
- * address and the host it runs on, nothing about the job itself: an
157
- * onchain job it resolves onchain, runs, publishes its job API for at
158
- * https://<jobId>.<frp server> and pins the result of; an inline run's
159
- * definition is in the seed in `hostDir`, the job's directory, where the
160
- * node also serves it its socket (JobRuntimeHost).
161
- *
162
- * A sidecar a previous node left behind is replaced; the new one resumes
163
- * the job from the data volume they share. The sidecar has no restart
164
- * policy: the node starts it again if it exits before its result (see
165
- * JobRuntimeSession).
166
- */
167
- async startJobRuntime(jobAddress, hostDir, kind,
168
- /** What its console carries; `entries` for `nosana node run` to render. */
169
- consoleOutput = 'text') {
170
- const { jobRuntime, frp, network } = configs();
171
- const name = JOB_RUNTIME_CONTAINER_PREFIX + jobAddress;
172
- // A sidecar replaced comes back at another address.
173
- this.jobRuntimeUrls.delete(jobAddress);
174
- if (!this.jobRuntimeVolumesSwept) {
175
- await this.removeOrphanedJobRuntimeVolumes(jobAddress);
176
- this.jobRuntimeVolumesSwept = true;
177
- }
178
- await Promise.all([
179
- // The sidecar's network, which a node runs on but `nosana node run`
180
- // may be the first to need.
181
- this.containerOrchestration.createNetwork('NOSANA_GATEWAY'),
182
- this.containerOrchestration.stopAndDeleteContainer(name),
183
- // Cached by pullJobRuntimeImage before the job loop, so normally no pull.
184
- this.containerOrchestration.pullImage(jobRuntime.containerImage),
185
- ]);
186
- // A use, so the cache's resync keeps the image while jobs run on it.
187
- await this.resourceManager.images.setImage(jobRuntime.containerImage);
188
- // The same socket path as the SSH sidecar (see withSshSidecar).
189
- const socketDest = this.containerOrchestration.name === 'podman'
190
- ? '/run/podman/podman.sock'
191
- : '/var/run/docker.sock';
192
- const rpc = getKit().config.solana.rpcEndpoint;
193
- const node = nodeAddress();
194
- const { teeRuntime, gpu } = this.containerOrchestration;
195
- const labels = {
196
- [JOB_LABEL]: jobAddress,
197
- [NODE_LABEL]: node,
198
- [SERVICE_LABEL]: 'job-runtime',
199
- };
200
- const socketGroup = await this.jobRuntimeSocketGroup();
201
- await this.ownJobRuntimeVolume(name, jobRuntime.containerImage, labels);
202
- await this.containerOrchestration.runFlowContainer(jobRuntime.containerImage, {
203
- name,
204
- // The image's unprivileged user, in the runtime socket's group so it
205
- // can use the socket; root only where the socket cannot be shared.
206
- ...(socketGroup === undefined
207
- ? { user: '0:0' }
208
- : { group_add: [String(socketGroup)] }),
209
- // GPU stats need the device; a node without GPUs (which may have no
210
- // GPU driver either) gives its sidecar none.
211
- ...(this.repository.getNodeInfo().gpus.devices.length > 0 ? { gpu: true } : {}),
212
- networks: { NOSANA_GATEWAY: {} },
213
- aliases: [name],
214
- env: {
215
- JOB_ADDRESS: jobAddress,
216
- JOB_KIND: kind,
217
- NODE_ADDRESS: node,
218
- NOSANA_NETWORK: network,
219
- ...(rpc ? { SOLANA_RPC: rpc } : {}),
220
- CONTAINER_RUNTIME: this.containerOrchestration.name,
221
- DOCKER_SOCKET: socketDest,
222
- GPU: gpu,
223
- ...(teeRuntime ? { TEE_RUNTIME: teeRuntime } : {}),
224
- FRP_SERVER_ADDR: frp.serverAddr,
225
- FRP_SERVER_PORT: frp.serverPort.toString(),
226
- FRP_SERVER_IMAGE: this.frpcImage,
227
- RESOURCE_MANAGER_IMAGE: this.resourceManager.volumes.resourceManagerImage,
228
- NOSANA_NODE_VERSION: pkg.version,
229
- JOB_RUNTIME_PORT: JOB_RUNTIME_PORT.toString(),
230
- // The entries carry their colours; the container has no terminal.
231
- ...(consoleOutput === 'entries'
232
- ? { JOB_RUNTIME_CONSOLE: 'entries', FORCE_COLOR: '1' }
233
- : {}),
234
- },
235
- volumes: [
236
- {
237
- type: 'bind',
238
- name: await this.resolveHostSocketPath(),
239
- dest: socketDest,
240
- readonly: true,
241
- },
242
- { name, dest: JOB_RUNTIME_DATA_DIR },
243
- {
244
- type: 'bind',
245
- name: await this.containerOrchestration.resolveHostPath(hostDir),
246
- dest: JOB_RUNTIME_HOST_DIR,
247
- },
248
- ],
249
- labels,
250
- // The socket makes a compromised sidecar root on the host whatever
251
- // runs inside it, so the container itself gets nothing it does not
252
- // need: no capabilities (the API, nvidia-smi and /proc need none), no
253
- // privilege escalation, and a pid budget that fits node plus its
254
- // nvidia-smi spawns.
255
- security: {
256
- capDrop: ['ALL'],
257
- noNewPrivileges: true,
258
- pidsLimit: 256,
259
- },
260
- });
261
- }
262
- /**
263
- * The group a sidecar needs to use the runtime socket: the socket's own,
264
- * which is opened to its group where it is root's alone (podman serves its
265
- * socket that way; docker's is its `docker` group's already). The socket
266
- * is the one the node itself uses, bound into its container read-write,
267
- * so the change reaches the socket the sidecar gets bound. Undefined when
268
- * the socket cannot be read or changed (a TCP endpoint, a dev socket the
269
- * user does not own), in which case the sidecar runs as root, as it did
270
- * before, rather than not at all.
271
- */
272
- async jobRuntimeSocketGroup() {
273
- const { socketPath } = this.containerOrchestration;
274
- try {
275
- const { gid, mode } = await fs.stat(socketPath);
276
- if ((mode & 0o060) !== 0o060) {
277
- await fs.chmod(socketPath, (mode & 0o777) | 0o060);
278
- }
279
- return gid;
280
- }
281
- catch (error) {
282
- console.warn(`The runtime socket at ${socketPath} cannot be shared with the job runtime (${error}); the job runtime runs as root.`);
283
- return undefined;
284
- }
285
- }
286
- /**
287
- * Makes the job's data volume the sidecar user's. A volume created as the
288
- * sidecar starts takes the ownership of the image's mount point, which is
289
- * the user's; one that is already there (a sidecar a previous node left,
290
- * one that may have run as root) is chowned first, by a one-shot container
291
- * on the same image, so that the sidecar can write its records.
292
- */
293
- async ownJobRuntimeVolume(volume, image, labels) {
294
- if (!(await this.containerOrchestration.hasVolume(volume)))
295
- return;
296
- const name = `${volume}-chown`;
297
- await this.containerOrchestration.stopAndDeleteContainer(name);
298
- const container = await this.containerOrchestration.runFlowContainer(image, {
299
- name,
300
- user: '0:0',
301
- entrypoint: [
302
- 'chown',
303
- '-R',
304
- `${JOB_RUNTIME_UID}:${JOB_RUNTIME_GID}`,
305
- JOB_RUNTIME_DATA_DIR,
306
- ],
307
- volumes: [{ name: volume, dest: JOB_RUNTIME_DATA_DIR }],
308
- labels: { ...labels, [SERVICE_LABEL]: 'job-runtime-chown' },
309
- });
310
- const { StatusCode } = await container.wait({ condition: 'not-running' });
311
- await this.containerOrchestration.stopAndDeleteContainer(name);
312
- if (StatusCode !== 0) {
313
- throw new Error(`The data volume ${volume} could not be made the job runtime's (chown exited ${StatusCode})`);
314
- }
315
- }
316
- /**
317
- * Whether the job has a sidecar, running or exited: one that outlived the
318
- * node's last process, to take over. Only the runtime saying there is no
319
- * such container answers false; a runtime that fails to answer (its daemon
320
- * still starting after a host restart, say) is asked again, for about two
321
- * minutes, before its last error is thrown.
322
- */
323
- async hasJobRuntime(jobAddress) {
324
- const name = JOB_RUNTIME_CONTAINER_PREFIX + jobAddress;
325
- for (let attempt = 1;; attempt++) {
326
- try {
327
- return await this.containerOrchestration.doesContainerExist(name);
328
- }
329
- catch (error) {
330
- if (attempt >= JOB_RUNTIME_LOOKUP_ATTEMPTS)
331
- throw error;
332
- if (attempt === 1) {
333
- console.error(`The container runtime failed to say whether job ${jobAddress} has a runtime, asking again: ${error}`);
334
- }
335
- await sleep(JOB_RUNTIME_RETRY_MS);
336
- }
337
- }
338
- }
339
- /**
340
- * Whether the job's sidecar runs on an image other than the configured one
341
- * as the node has it locally (pulled by pullJobRuntimeImage before the job
342
- * loop): a node updated mid-job meets the sidecar of the version before it.
343
- * An exited sidecar is not outdated, nor is one whose image or the
344
- * configured one cannot be read: an image the node does not have is no
345
- * reason to replace a sidecar that runs.
346
- */
347
- async isJobRuntimeOutdated(jobAddress) {
348
- try {
349
- const [sidecar, image] = await Promise.all([
350
- this.containerOrchestration
351
- .getContainer(JOB_RUNTIME_CONTAINER_PREFIX + jobAddress)
352
- .inspect(),
353
- this.containerOrchestration
354
- .getImage(configs().jobRuntime.containerImage)
355
- .then((image) => image.inspect()),
356
- ]);
357
- return sidecar.State.Running && sidecar.Image !== image.Id;
358
- }
359
- catch {
360
- return false;
361
- }
362
- }
363
- /**
364
- * Waits for the job's sidecar to exit, and answers its exit code;
365
- * undefined once it is gone. A wait the runtime drops (its daemon
366
- * restarting) is waited again. Only the runtime saying there is no such
367
- * container means gone: one that fails to answer is asked again for as
368
- * long as it takes, since answering gone would have the node remove a
369
- * sidecar that may still run. The job's expiry is enforced apart from
370
- * this, and removing the sidecar ends the wait.
371
- */
372
- async waitForJobRuntime(jobAddress) {
373
- const container = this.containerOrchestration.getContainer(JOB_RUNTIME_CONTAINER_PREFIX + jobAddress);
374
- let failing = false;
375
- for (;;) {
376
- let info;
377
- try {
378
- info = await container.inspect();
379
- }
380
- catch (error) {
381
- if (isNoSuchContainer(error))
382
- return undefined;
383
- if (!failing) {
384
- console.error(`The container runtime failed to inspect the runtime of job ${jobAddress}, asking again: ${error}`);
385
- }
386
- failing = true;
387
- await sleep(JOB_RUNTIME_RETRY_MS);
388
- continue;
389
- }
390
- failing = false;
391
- if (!info.State.Running && !info.State.Restarting) {
392
- return { exitCode: info.State.ExitCode };
393
- }
394
- await container
395
- .wait({ condition: 'not-running' })
396
- .catch(() => new Promise((resolve) => setTimeout(resolve, 1_000)));
397
- }
398
- }
399
- /** Starts the job's exited sidecar again, as it was. */
400
- async restartJobRuntime(jobAddress) {
401
- await this.containerOrchestration
402
- .getContainer(JOB_RUNTIME_CONTAINER_PREFIX + jobAddress)
403
- .start();
404
- }
405
- /**
406
- * Removes what job sidecars of this node left behind, the sidecars
407
- * themselves included, for every job but `liveJob`, which this node still
408
- * runs. A sidecar stamps its job and this node on everything it starts, so
409
- * this finds what a sidecar that died never removed, and no other node's.
410
- */
411
- async removeOrphanedJobContainers(liveJob) {
412
- const node = nodeAddress();
413
- const containers = await this.containerOrchestration.listContainersByLabels([`${NODE_LABEL}=${node}`], { all: true });
414
- await Promise.allSettled(containers
415
- .filter(({ Labels }) => Labels?.[JOB_LABEL] !== liveJob)
416
- .map(({ Id }) => this.containerOrchestration.stopAndDeleteContainer(Id)));
417
- }
418
- /**
419
- * Where the job's sidecar serves its job API: its address on the gateway
420
- * network, which a node on the network and one on the host both reach.
421
- * Undefined while it is not running. An address is looked up again after
422
- * a few seconds, since a restarted sidecar may come back with another; a
423
- * miss is not kept, so a sidecar that is starting is found once it is up.
424
- */
425
- async jobRuntimeUrl(jobAddress) {
426
- const cached = this.jobRuntimeUrls.get(jobAddress);
427
- if (cached && Date.now() - cached.at < JOB_RUNTIME_URL_TTL_MS)
428
- return cached.url;
429
- const info = await this.containerOrchestration
430
- .getContainer(JOB_RUNTIME_CONTAINER_PREFIX + jobAddress)
431
- .inspect()
432
- .catch(() => undefined);
433
- const ip = info?.State?.Running
434
- ? info.NetworkSettings?.Networks?.NOSANA_GATEWAY?.IPAddress
435
- : undefined;
436
- if (!ip)
437
- return undefined;
438
- const url = `http://${ip}:${JOB_RUNTIME_PORT}`;
439
- this.jobRuntimeUrls.set(jobAddress, { url, at: Date.now() });
440
- return url;
441
- }
442
- /**
443
- * Pulls the job runtime image, so the next job's sidecar finds it local.
444
- * Runs before each job loop; a cached image costs no registry round trip.
445
- */
446
- /**
447
- * Pulls the sidecar image `.env` names and records it in the image cache,
448
- * where the resync between jobs ages out what goes unused, so a bump's
449
- * predecessor goes the way of any other image.
450
- */
451
- async pullJobRuntimeImage() {
452
- const image = configs().jobRuntime.containerImage;
453
- await this.containerOrchestration.pullImage(image);
454
- await this.resourceManager.images.setImage(image);
455
- }
456
- /**
457
- * Removes the job's sidecar, then whatever else of the job is left: its
458
- * frpc, and any operation container a sidecar that died never removed.
459
- * Then the sidecar's data volume. Throws on failure; what will not go is
460
- * swept again by the next resync (removeOrphanedJobContainers).
461
- */
462
- async stopJobRuntime(jobAddress) {
463
- this.jobRuntimeUrls.delete(jobAddress);
464
- const name = JOB_RUNTIME_CONTAINER_PREFIX + jobAddress;
465
- await this.containerOrchestration.stopAndDeleteContainer(name);
466
- const leftovers = await this.containerOrchestration.getContainersByLabels([`${JOB_LABEL}=${jobAddress}`], { all: true });
467
- await Promise.all(leftovers.map((container) => this.containerOrchestration.stopAndDeleteContainer(container.id)));
468
- await this.containerOrchestration.deleteVolume(name);
469
- }
470
- /**
471
- * Removes job-runtime-<jobId> volumes left by a job whose stop never ran (the
472
- * node died mid-job). The volume resync does not know them, and a reaped
473
- * sidecar container leaves its named volume behind. Runs once, on the first
474
- * sidecar start after boot. The job starting keeps its volume, to resume
475
- * from, and a volume still in use fails to delete and is kept for the next
476
- * boot.
477
- */
478
- async removeOrphanedJobRuntimeVolumes(jobAddress) {
479
- const orphans = (await this.containerOrchestration.listVolumes())
480
- .map(({ Name }) => Name)
481
- .filter((name) => name?.startsWith(JOB_RUNTIME_CONTAINER_PREFIX) &&
482
- name !== JOB_RUNTIME_CONTAINER_PREFIX + jobAddress);
483
- // One in use or already gone fails to delete, and is left.
484
- await Promise.allSettled(orphans.map((name) => this.containerOrchestration.deleteVolume(name)));
485
- }
486
- /** The runtime socket as a host path, which is what a bind source must be. */
487
- resolveHostSocketPath() {
488
- return (this.hostSocketPath ??= this.containerOrchestration.resolveHostPath(this.containerOrchestration.socketPath));
489
- }
490
124
  }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Per-node limits from the CLI (`nosana node start --cores 6 --ram 32 --disk 800 --gpu 0`),
3
+ * read into the VmSpec every job's VM boots (see readVmSpec).
4
+ */
5
+ export interface VmNodeOptions {
6
+ /** vCPU budget for this node's VMs (`--cores`); else `VM_NODE_CPUS`, else detected. */
7
+ cores?: number;
8
+ /** Memory budget in GiB (`--ram`); else `VM_NODE_MEMORY`, else detected. */
9
+ ram?: number;
10
+ /** Disk size in GiB (`--disk`); else sized from the work root's free space. */
11
+ disk?: number;
12
+ /** GPU selection: NVIDIA indices and PCI addresses (`'0,0000:81:00.0'`) or `'all'` (`--gpu`). */
13
+ gpu?: string;
14
+ }
@@ -0,0 +1,16 @@
1
+ import { type VmSpec } from '../../../../packages/vm-orchestrator/src/index.js';
2
+ import type { VmNodeOptions } from './VmNodeOptions.js';
3
+ import type { VmHostEnv } from './vmHostEnv.js';
4
+ /** The vCPUs and memory every VM of the node gets. */
5
+ export type VmCapacity = Pick<VmSpec, 'vcpus' | 'memoryMiB'>;
6
+ /**
7
+ * The node's vCPU/memory budget: what one node may give its VMs, the default for an unsized
8
+ * job and the ceiling for a sized one. Each is the CLI flag (`--cores`, `--ram` in GiB), else
9
+ * its env var (`VM_NODE_CPUS`, `VM_NODE_MEMORY`), used as-is so several nodes can split one
10
+ * host; else the host's total minus what the host keeps (`VM_HOST_RESERVE_CPUS`,
11
+ * `VM_HOST_RESERVE_MEMORY`), floored at 1 vCPU and 512 MiB.
12
+ *
13
+ * Detection reads the host through `os`, so a node in a cgroup-limited container sees the
14
+ * machine's totals rather than its share; such a host pins its budget.
15
+ */
16
+ export declare function detectVmCapacity(options: VmNodeOptions, env: VmHostEnv): VmCapacity;
@@ -0,0 +1,47 @@
1
+ import os from 'node:os';
2
+ import { parseMemoryToMiB } from '../../../../packages/vm-orchestrator/src/index.js';
3
+ /** Logical CPUs kept for the host when the node's budget is detected. */
4
+ const DEFAULT_RESERVE_CPUS = 1;
5
+ /** The least memory kept for the host when the node's budget is detected. */
6
+ const MIN_RESERVE_MEMORY_MIB = 2048;
7
+ /** The share of the host's memory kept for it, when more than MIN_RESERVE_MEMORY_MIB. */
8
+ const RESERVE_MEMORY_FRACTION = 0.1;
9
+ /** The smallest budget detection answers, however much the host keeps. */
10
+ const MIN_VCPUS = 1;
11
+ const MIN_MEMORY_MIB = 512;
12
+ const MIB_PER_GIB = 1024;
13
+ const BYTES_PER_MIB = 1024 * 1024;
14
+ /**
15
+ * The node's vCPU/memory budget: what one node may give its VMs, the default for an unsized
16
+ * job and the ceiling for a sized one. Each is the CLI flag (`--cores`, `--ram` in GiB), else
17
+ * its env var (`VM_NODE_CPUS`, `VM_NODE_MEMORY`), used as-is so several nodes can split one
18
+ * host; else the host's total minus what the host keeps (`VM_HOST_RESERVE_CPUS`,
19
+ * `VM_HOST_RESERVE_MEMORY`), floored at 1 vCPU and 512 MiB.
20
+ *
21
+ * Detection reads the host through `os`, so a node in a cgroup-limited container sees the
22
+ * machine's totals rather than its share; such a host pins its budget.
23
+ */
24
+ export function detectVmCapacity(options, env) {
25
+ return { vcpus: detectVcpus(options, env), memoryMiB: detectMemoryMiB(options, env) };
26
+ }
27
+ function detectVcpus({ cores }, env) {
28
+ if (cores && cores > 0)
29
+ return Math.floor(cores);
30
+ if (env.nodeCpus !== undefined)
31
+ return env.nodeCpus;
32
+ const totalCpus = os.cpus().length || 1;
33
+ return Math.max(MIN_VCPUS, totalCpus - (env.hostReserveCpus ?? DEFAULT_RESERVE_CPUS));
34
+ }
35
+ function detectMemoryMiB({ ram }, env) {
36
+ if (ram && ram > 0)
37
+ return Math.floor(ram * MIB_PER_GIB);
38
+ if (env.nodeMemory !== undefined)
39
+ return parseMemoryToMiB(env.nodeMemory);
40
+ const totalMiB = Math.floor(os.totalmem() / BYTES_PER_MIB);
41
+ return Math.max(MIN_MEMORY_MIB, totalMiB - hostReserveMemoryMiB(totalMiB, env));
42
+ }
43
+ function hostReserveMemoryMiB(totalMiB, env) {
44
+ if (env.hostReserveMemory !== undefined)
45
+ return parseMemoryToMiB(env.hostReserveMemory);
46
+ return Math.max(MIN_RESERVE_MEMORY_MIB, Math.floor(totalMiB * RESERVE_MEMORY_FRACTION));
47
+ }
@@ -0,0 +1,19 @@
1
+ /** Where resolveGpuSelection looks; overridable for tests. */
2
+ export interface GpuSelectionOptions {
3
+ /** The sysfs PCI root; `/sys/bus/pci` by default. */
4
+ root?: string;
5
+ }
6
+ /**
7
+ * The BDFs of the GPUs a `--gpu` selection passes through, whatever driver they are on now:
8
+ * each VM's boot moves its GPUs to vfio-pci. `'all'` (or none) is every NVIDIA GPU on the host.
9
+ * Otherwise each comma-separated entry is an NVIDIA index or a PCI address. An index counts the
10
+ * host's NVIDIA GPUs in PCI-bus order, the order `nvidia-smi` numbers them in while they are
11
+ * on its driver; it is read from sysfs, which lists a GPU on any driver, so an index names the
12
+ * same GPU after its move to vfio-pci. An entry that names no NVIDIA GPU throws, naming it.
13
+ */
14
+ export declare function resolveGpuSelection(gpuSelection: string | undefined, options?: GpuSelectionOptions): string[];
15
+ /**
16
+ * Whether a `--gpu` selection names a GPU by PCI address: only a `--vm` node can, as the
17
+ * container runtime selects GPUs by index.
18
+ */
19
+ export declare function hasPciAddress(gpuSelection: string | undefined): boolean;