pub mod cli;
pub mod runner;
pub mod usage;
use crate::sandbox::{Amount, Budget, HostResources, Limits};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
pub const MAIN: &str = "main";
pub const ID_ENV: &str = "ZAKURO_NODE_ID";
pub const NODE_LABEL: &str = "zakuro.node";
pub const MAX_EXTRA: u32 = 4;
pub const MAX_WORKERS: u32 = 8;
const GIB: u64 = 1024 * 1024 * 1024;
pub const MAIN_MIN_CPU_MILLIS: u32 = 1000;
pub const MAIN_MIN_MEMORY: u64 = 2 * GIB;
pub const NODE_MIN_CPU_MILLIS: u32 = 500;
pub const NODE_MIN_MEMORY: u64 = GIB;
pub const NODE_MIN_DISK: u64 = 5 * GIB;
pub fn ask_to_stop(child: &mut std::process::Child) {
#[cfg(unix)]
unsafe {
libc::kill(child.id() as libc::pid_t, libc::SIGTERM);
}
#[cfg(not(unix))]
{
let _ = child.kill();
}
}
pub fn explicit_id() -> Option<String> {
std::env::var(ID_ENV)
.ok()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty() && v != MAIN)
}
pub fn current_id() -> String {
explicit_id().unwrap_or_else(|| MAIN.to_string())
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NodeSpec {
pub id: String,
pub index: u32,
#[serde(default)]
pub environment: Option<String>,
#[serde(default)]
pub environment_name: Option<String>,
pub workers: u32,
pub cpu_millis: u32,
pub memory_bytes: u64,
pub disk_bytes: u64,
pub created: String,
}
impl NodeSpec {
pub fn broker_port(&self) -> u16 {
9000 + 2 * self.index as u16
}
pub fn worker_port(&self, i: u32) -> u16 {
3960 + 40 * self.index as u16 + i as u16
}
pub fn scan_range(&self) -> String {
format!(
"{}-{}",
self.worker_port(0),
self.worker_port(MAX_WORKERS + 3)
)
}
pub fn project(&self) -> String {
format!("zk-node-{}", self.id)
}
pub fn dir(&self, home: &Path) -> PathBuf {
node_dir(home, &self.id)
}
pub fn state_dir(&self, home: &Path) -> PathBuf {
self.dir(home).join("state")
}
pub fn limits(&self) -> Limits {
Limits {
cpu_millis: self.cpu_millis,
memory_bytes: self.memory_bytes,
gpus: crate::sandbox::Gpus::None,
}
}
pub fn workers_share(&self) -> Limits {
let l = self.limits();
if self.environment.is_some() {
Limits {
cpu_millis: l.cpu_millis / 2,
memory_bytes: l.memory_bytes / 2,
gpus: l.gpus,
}
} else {
l
}
}
pub fn deploy_slice(&self) -> &'static str {
match (self.environment.is_some(), self.workers) {
(true, 0) => "100%",
(true, _) => "50%",
(false, _) => "25%",
}
}
}
pub fn nodes_dir(home: &Path) -> PathBuf {
home.join("nodes")
}
pub fn node_dir(home: &Path, id: &str) -> PathBuf {
nodes_dir(home).join(id)
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Reserved {
pub cpu_millis: u32,
pub memory_bytes: u64,
pub disk_bytes: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Registry {
#[serde(default)]
pub v: u32,
#[serde(default)]
pub nodes: Vec<NodeSpec>,
}
impl Registry {
pub fn path(home: &Path) -> PathBuf {
nodes_dir(home).join("nodes.json")
}
pub fn load(home: &Path) -> Registry {
let mut reg: Registry = std::fs::read(Self::path(home))
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or_default();
reg.nodes
.retain(|n| valid_id(&n.id) && (1..=MAX_EXTRA).contains(&n.index));
reg
}
pub fn save(&self, home: &Path) -> Result<(), String> {
let path = Self::path(home);
let dir = nodes_dir(home);
std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
let mut out = self.clone();
out.v = 1;
let tmp = dir.join(format!(".nodes.json.{}", std::process::id()));
let body = serde_json::to_vec_pretty(&out).map_err(|e| e.to_string())?;
std::fs::write(&tmp, body).map_err(|e| format!("{}: {e}", tmp.display()))?;
std::fs::rename(&tmp, &path).map_err(|e| format!("{}: {e}", path.display()))
}
pub fn get(&self, id: &str) -> Option<&NodeSpec> {
self.nodes.iter().find(|n| n.id == id)
}
pub fn reserved(&self) -> Reserved {
self.reserved_except(None)
}
fn reserved_except(&self, skip: Option<&str>) -> Reserved {
let mut r = Reserved::default();
for n in self.nodes.iter().filter(|n| Some(n.id.as_str()) != skip) {
r.cpu_millis += n.cpu_millis;
r.memory_bytes += n.memory_bytes;
r.disk_bytes += n.disk_bytes;
}
r
}
fn free_index(&self) -> Option<u32> {
(1..=MAX_EXTRA).find(|i| self.nodes.iter().all(|n| n.index != *i))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MachineLimit {
pub cpu_millis: u32,
pub memory_bytes: u64,
pub disk_bytes: u64,
}
impl MachineLimit {
pub fn from_budget(b: &Budget, host: &HostResources) -> Result<Self, String> {
let l = b.total(host)?;
Ok(Self {
cpu_millis: l.cpu_millis,
memory_bytes: l.memory_bytes,
disk_bytes: b.disk_bytes,
})
}
pub fn load(home: &Path) -> Result<Self, String> {
let b = crate::policy::machine_budget(Some(home), &|k| std::env::var(k).ok())?;
Self::from_budget(&b, &HostResources::detect())
}
pub fn main_share(&self, r: &Reserved) -> MachineLimit {
MachineLimit {
cpu_millis: self.cpu_millis.saturating_sub(r.cpu_millis),
memory_bytes: self.memory_bytes.saturating_sub(r.memory_bytes),
disk_bytes: self.disk_bytes.saturating_sub(r.disk_bytes),
}
}
}
pub fn minus_reserved(budget: Budget, r: &Reserved, host: &HostResources) -> Budget {
if *r == Reserved::default() {
return budget;
}
let Ok(total) = MachineLimit::from_budget(&budget, host) else {
return budget;
};
let left = total.main_share(r);
Budget {
cpu: Amount::Absolute(f64::from(left.cpu_millis.max(100)) / 1000.0),
memory: Amount::Absolute(left.memory_bytes.max(256 * 1024 * 1024) as f64),
gpu: budget.gpu,
disk_bytes: left.disk_bytes,
}
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct NewNode {
#[serde(default)]
pub id: Option<String>,
#[serde(default)]
pub environment: Option<String>,
#[serde(default)]
pub environment_name: Option<String>,
#[serde(default)]
pub workers: Option<u32>,
pub cpu_cores: f64,
pub memory_bytes: u64,
#[serde(default)]
pub disk_bytes: Option<u64>,
}
pub fn slug(raw: &str) -> String {
let mut s = String::new();
for c in raw.trim().to_ascii_lowercase().chars() {
if c.is_ascii_lowercase() || c.is_ascii_digit() {
s.push(c);
} else if !s.ends_with('-') && !s.is_empty() {
s.push('-');
}
}
let s: String = s.trim_end_matches('-').chars().take(24).collect();
let s = s.trim_end_matches('-').to_string();
if s.is_empty() || s == MAIN {
"node".into()
} else {
s
}
}
pub fn valid_id(id: &str) -> bool {
!id.is_empty() && id != MAIN && slug(id) == id
}
fn unique_id(reg: &Registry, base: &str) -> String {
if reg.get(base).is_none() {
return base.to_string();
}
(2..)
.map(|n| {
let tail = format!("-{n}");
let head: String = base.chars().take(24 - tail.len()).collect();
format!("{head}{tail}")
})
.find(|c| reg.get(c).is_none())
.unwrap_or_else(|| base.to_string())
}
fn check_size(
reg: &Registry,
skip: Option<&str>,
cpu_millis: u32,
memory_bytes: u64,
disk_bytes: u64,
workers: u32,
machine: &MachineLimit,
) -> Result<(), String> {
if workers > MAX_WORKERS {
return Err(format!("a node runs at most {MAX_WORKERS} workers"));
}
if cpu_millis < NODE_MIN_CPU_MILLIS {
return Err("a node needs at least half a core".into());
}
if memory_bytes < NODE_MIN_MEMORY {
return Err("a node needs at least 1 GB of memory".into());
}
if disk_bytes < NODE_MIN_DISK {
return Err("a node needs at least 5 GB of disk".into());
}
let mut r = reg.reserved_except(skip);
r.cpu_millis += cpu_millis;
r.memory_bytes += memory_bytes;
r.disk_bytes += disk_bytes;
let left = machine.main_share(&r);
let gb = |b: u64| b as f64 / GIB as f64;
if left.cpu_millis < MAIN_MIN_CPU_MILLIS || r.cpu_millis > machine.cpu_millis {
return Err(format!(
"not enough CPU inside Zakuro's limit: the limit is {:.1} cores, and the main node keeps at least 1. Raise the limit on Resources, or make a node smaller.",
f64::from(machine.cpu_millis) / 1000.0
));
}
if left.memory_bytes < MAIN_MIN_MEMORY || r.memory_bytes > machine.memory_bytes {
return Err(format!(
"not enough memory inside Zakuro's limit: the limit is {:.1} GB, and the main node keeps at least 2. Raise the limit on Resources, or make a node smaller.",
gb(machine.memory_bytes)
));
}
if r.disk_bytes > machine.disk_bytes {
return Err(format!(
"not enough disk inside Zakuro's limit ({:.0} GB). Raise the limit on Resources, or make a node smaller.",
gb(machine.disk_bytes)
));
}
Ok(())
}
pub fn plan_add(
reg: &Registry,
req: &NewNode,
machine: &MachineLimit,
now: &str,
) -> Result<NodeSpec, String> {
let Some(index) = reg.free_index() else {
return Err(format!(
"this machine already runs {MAX_EXTRA} extra nodes, the most it can"
));
};
let environment = req
.environment
.as_ref()
.map(|e| e.trim().to_string())
.filter(|e| !e.is_empty());
if let Some(e) = &environment {
if let Some(n) = reg
.nodes
.iter()
.find(|n| n.environment.as_deref() == Some(e))
{
return Err(format!("node {} already hosts {e}", n.id));
}
}
let base = match (&req.id, &environment) {
(Some(id), _) if !id.trim().is_empty() => {
let s = slug(id);
if s != id.trim() {
return Err("a node id is lowercase letters, digits and dashes (24 at most), and not \"main\"".into());
}
if reg.get(&s).is_some() {
return Err(format!("there is already a node {s}"));
}
s
}
(_, Some(e)) => slug(e),
_ => "jobs".to_string(),
};
let workers = req
.workers
.unwrap_or(if environment.is_some() { 0 } else { 1 });
if environment.is_none() && workers == 0 {
return Err("a node with no environment needs at least one worker".into());
}
if !(req.cpu_cores.is_finite() && req.cpu_cores > 0.0 && req.cpu_cores <= 1024.0) {
return Err("cpu_cores must be a positive number".into());
}
let cpu_millis = (req.cpu_cores * 1000.0).round() as u32;
let disk_bytes = req.disk_bytes.unwrap_or(10 * GIB);
check_size(
reg,
None,
cpu_millis,
req.memory_bytes,
disk_bytes,
workers,
machine,
)?;
Ok(NodeSpec {
id: unique_id(reg, &base),
index,
environment,
environment_name: req
.environment_name
.clone()
.filter(|n| !n.trim().is_empty()),
workers,
cpu_millis,
memory_bytes: req.memory_bytes,
disk_bytes,
created: now.to_string(),
})
}
#[derive(Debug, Clone, Default, PartialEq, Deserialize)]
pub struct NodePatch {
#[serde(default)]
pub workers: Option<u32>,
#[serde(default)]
pub cpu_cores: Option<f64>,
#[serde(default)]
pub memory_bytes: Option<u64>,
#[serde(default)]
pub disk_bytes: Option<u64>,
}
pub fn plan_patch(
reg: &Registry,
id: &str,
patch: &NodePatch,
machine: &MachineLimit,
) -> Result<NodeSpec, String> {
let mut n = reg
.get(id)
.cloned()
.ok_or_else(|| format!("no node {id}"))?;
if let Some(w) = patch.workers {
n.workers = w;
}
if let Some(c) = patch.cpu_cores {
if !(c.is_finite() && c > 0.0 && c <= 1024.0) {
return Err("cpu_cores must be a positive number".into());
}
n.cpu_millis = (c * 1000.0).round() as u32;
}
if let Some(m) = patch.memory_bytes {
n.memory_bytes = m;
}
if let Some(d) = patch.disk_bytes {
n.disk_bytes = d;
}
if n.environment.is_none() && n.workers == 0 {
return Err("a node with no environment needs at least one worker".into());
}
check_size(
reg,
Some(id),
n.cpu_millis,
n.memory_bytes,
n.disk_bytes,
n.workers,
machine,
)?;
Ok(n)
}
pub fn worker_name(fp8: &str, i: u32) -> String {
format!("{fp8}-w{i}")
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComposeWorker {
pub service: String,
pub name: String,
pub container: String,
pub port: u16,
}
pub fn compose_workers(spec: &NodeSpec, fp8: &str) -> Vec<ComposeWorker> {
(0..spec.workers)
.map(|i| {
let name = worker_name(fp8, i);
ComposeWorker {
service: format!("w{i}"),
container: crate::up::worker_container_name(&name),
name,
port: spec.worker_port(i),
}
})
.collect()
}
pub fn compose_yaml(spec: &NodeSpec, fp8: &str, image: &str) -> String {
let workers = compose_workers(spec, fp8);
let per = spec.workers_share().split(spec.workers.max(1));
let q = |s: &str| serde_json::to_string(s).unwrap_or_else(|_| "\"\"".into());
let mut y = format!(
"# Written by zc for node {} (ADR ZC-0027). Do not edit: zc rewrites it.\nname: {}\n",
spec.id,
spec.project()
);
if workers.is_empty() {
y.push_str("services: {}\n");
return y;
}
y.push_str("services:\n");
for w in &workers {
y.push_str(&format!(
" {svc}:\n image: {image}\n container_name: {container}\n restart: \"no\"\n cpus: {cpus}\n mem_limit: {mem}\n memswap_limit: {mem}\n environment:\n {s6}: \"0\"\n ports:\n - \"127.0.0.1:{port}:{port}\"\n labels:\n zakuro.share: \"1\"\n zakuro.worker: {name}\n {node_label}: {node}\n {budget_label}: {budget}\n",
svc = w.service,
image = q(image),
container = q(&w.container),
cpus = per.cpus_flag(),
mem = per.memory_bytes,
s6 = crate::up::S6_WAIT_ENV,
port = w.port,
name = q(&w.name),
node_label = NODE_LABEL,
node = q(&spec.id),
budget_label = crate::sandbox::BUDGET_LABEL,
budget = q(&per.budget_label()),
));
}
y
}
pub fn apply_policy(p: &mut crate::policy::Policy, spec: &NodeSpec) {
p.budget.cpu = format!("{}", f64::from(spec.cpu_millis) / 1000.0);
p.budget.memory = format!("{}", spec.memory_bytes);
p.budget.disk = format!("{}G", (spec.disk_bytes / GIB).max(1));
p.budget.gpu = false;
let env = spec.environment.is_some();
p.workloads.environments = env;
p.workloads.spaces = !env;
p.workloads.hub_serving = !env;
p.workloads.atelier = crate::policy::Atelier::Off;
}
pub const REMOVED_MARKER: &str = "removed";
pub fn add(home: &Path, req: &NewNode) -> Result<NodeSpec, String> {
let _guard = crate::policy::lock(home)?;
let mut reg = Registry::load(home);
let machine = MachineLimit::load(home)?;
let now = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
let spec = plan_add(®, req, &machine, &now)?;
let dir = spec.dir(home);
if dir.join(REMOVED_MARKER).exists() {
return Err(format!(
"node {} is still being removed; try again in a moment",
spec.id
));
}
runner::prepare(home, &spec)?;
reg.nodes.push(spec.clone());
reg.save(home)?;
Ok(spec)
}
pub fn patch(home: &Path, id: &str, patch: &NodePatch) -> Result<NodeSpec, String> {
let _guard = crate::policy::lock(home)?;
let mut reg = Registry::load(home);
let machine = MachineLimit::load(home)?;
let spec = plan_patch(®, id, patch, &machine)?;
for n in reg.nodes.iter_mut().filter(|n| n.id == id) {
*n = spec.clone();
}
reg.save(home)?;
Ok(spec)
}
pub fn remove(home: &Path, id: &str) -> Result<NodeSpec, String> {
let _guard = crate::policy::lock(home)?;
let mut reg = Registry::load(home);
let spec = reg
.get(id)
.cloned()
.ok_or_else(|| format!("no node {id}"))?;
reg.nodes.retain(|n| n.id != id);
let dir = spec.dir(home);
let _ = std::fs::create_dir_all(&dir);
std::fs::write(
dir.join(REMOVED_MARKER),
serde_json::to_vec(&spec).unwrap_or_default(),
)
.map_err(|e| format!("{}: {e}", dir.display()))?;
reg.save(home)?;
Ok(spec)
}
pub fn removed(home: &Path) -> Vec<NodeSpec> {
let Ok(rd) = std::fs::read_dir(nodes_dir(home)) else {
return Vec::new();
};
rd.flatten()
.filter_map(|e| {
let name = e.file_name().to_string_lossy().into_owned();
let b = std::fs::read(e.path().join(REMOVED_MARKER)).ok()?;
let spec: NodeSpec = serde_json::from_slice(&b).ok()?;
(valid_id(&spec.id) && spec.id == name).then_some(spec)
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn machine() -> MachineLimit {
MachineLimit {
cpu_millis: 5000,
memory_bytes: 8 * GIB,
disk_bytes: 50 * GIB,
}
}
fn req(env: Option<&str>, cpu: f64, mem_gb: u64) -> NewNode {
NewNode {
id: None,
environment: env.map(str::to_string),
environment_name: None,
workers: None,
cpu_cores: cpu,
memory_bytes: mem_gb * GIB,
disk_bytes: None,
}
}
fn add(reg: &mut Registry, r: &NewNode) -> Result<NodeSpec, String> {
let n = plan_add(reg, r, &machine(), "2026-11-12T00:00:00Z")?;
reg.nodes.push(n.clone());
Ok(n)
}
#[test]
fn nodes_get_their_own_ports_and_never_the_main_nodes() {
let mut reg = Registry::default();
let a = add(&mut reg, &req(Some("pytorch"), 1.0, 2)).unwrap();
let b = add(&mut reg, &req(None, 1.0, 2)).unwrap();
assert_eq!(
(a.id.as_str(), a.index, a.broker_port()),
("pytorch", 1, 9002)
);
assert_eq!((b.id.as_str(), b.index, b.broker_port()), ("jobs", 2, 9004));
assert_eq!((a.worker_port(0), b.worker_port(1)), (4000, 4041));
assert_eq!(a.scan_range(), "4000-4011");
assert_eq!((a.workers, b.workers), (0, 1));
reg.nodes.remove(0);
let c = add(&mut reg, &req(None, 0.5, 1)).unwrap();
assert_eq!((c.id.as_str(), c.index), ("jobs-2", 1));
}
#[test]
fn the_nodes_together_stay_inside_the_owners_limit() {
let mut reg = Registry::default();
add(&mut reg, &req(Some("a"), 2.0, 3)).unwrap();
add(&mut reg, &req(Some("b"), 2.0, 3)).unwrap();
let e = add(&mut reg, &req(Some("c"), 0.5, 1)).unwrap_err();
assert!(e.contains("not enough CPU"), "{e}");
assert_eq!(
reg.reserved(),
Reserved {
cpu_millis: 4000,
memory_bytes: 6 * GIB,
disk_bytes: 20 * GIB
}
);
reg.nodes[1].cpu_millis = 500;
let e = add(&mut reg, &req(Some("c"), 0.5, 1)).unwrap_err();
assert!(e.contains("not enough memory"), "{e}");
}
#[test]
fn one_node_per_environment_and_at_most_four() {
let mut reg = Registry::default();
add(&mut reg, &req(Some("pytorch"), 0.5, 1)).unwrap();
let e = add(&mut reg, &req(Some("pytorch"), 0.5, 1)).unwrap_err();
assert!(e.contains("already hosts pytorch"), "{e}");
for t in ["b", "c", "d"] {
add(&mut reg, &req(Some(t), 0.5, 1)).unwrap();
}
let e = add(&mut reg, &req(Some("e"), 0.5, 1)).unwrap_err();
assert!(e.contains("already runs 4"), "{e}");
}
#[test]
fn sizes_and_ids_are_checked() {
let reg = Registry::default();
let m = machine();
let at = "t";
assert!(plan_add(®, &req(None, 0.2, 2), &m, at).is_err());
assert!(plan_add(®, &req(None, 1.0, 0), &m, at).is_err());
let mut r = req(None, 1.0, 2);
r.workers = Some(0);
assert!(plan_add(®, &r, &m, at)
.unwrap_err()
.contains("at least one worker"));
r.workers = Some(9);
assert!(plan_add(®, &r, &m, at).is_err());
r.workers = Some(2);
r.id = Some("Main Node".into());
assert!(plan_add(®, &r, &m, at).is_err());
r.id = Some("main".into());
assert!(plan_add(®, &r, &m, at).is_err());
r.id = Some("gpu-box".into());
assert_eq!(plan_add(®, &r, &m, at).unwrap().id, "gpu-box");
assert_eq!(slug("PyTorch 2.5 (CUDA)"), "pytorch-2-5-cuda");
assert_eq!(slug("///"), "node");
}
#[test]
fn a_patch_is_checked_against_the_other_nodes_only() {
let mut reg = Registry::default();
add(&mut reg, &req(Some("a"), 2.0, 3)).unwrap();
add(&mut reg, &req(None, 1.0, 2)).unwrap();
let p = |c: f64| NodePatch {
cpu_cores: Some(c),
..Default::default()
};
assert_eq!(
plan_patch(®, "a", &p(3.0), &machine())
.unwrap()
.cpu_millis,
3000
);
assert!(plan_patch(®, "a", &p(4.0), &machine()).is_err());
let w = NodePatch {
workers: Some(0),
..Default::default()
};
assert!(plan_patch(®, "jobs", &w, &machine()).is_err());
assert_eq!(plan_patch(®, "a", &w, &machine()).unwrap().workers, 0);
assert!(plan_patch(®, "nope", &w, &machine()).is_err());
}
#[test]
fn the_main_node_lends_what_the_others_leave() {
let host = HostResources {
logical_cpus: 10,
memory_bytes: Some(16 * GIB),
};
let b = Budget::default(); assert_eq!(minus_reserved(b.clone(), &Reserved::default(), &host), b);
let r = Reserved {
cpu_millis: 2000,
memory_bytes: 3 * GIB,
disk_bytes: 10 * GIB,
};
let left = minus_reserved(b.clone(), &r, &host);
let l = left.total(&host).unwrap();
assert_eq!((l.cpu_millis, l.memory_bytes), (3000, 5 * GIB));
assert_eq!(left.disk_bytes, b.disk_bytes - 10 * GIB);
let over = Reserved {
cpu_millis: 9000,
memory_bytes: 64 * GIB,
disk_bytes: 900 * GIB,
};
let l = minus_reserved(b, &over, &host).total(&host).unwrap();
assert!(l.cpu_millis > 0 && l.memory_bytes > 0);
}
#[test]
fn compose_gives_each_worker_its_slice_and_labels() {
let mut reg = Registry::default();
let mut r = req(None, 2.0, 4);
r.workers = Some(2);
let n = add(&mut reg, &r).unwrap();
let y = compose_yaml(&n, "abcd1234", "zakuroai/compute:cpu");
assert!(y.contains("name: zk-node-jobs\n"), "{y}");
assert!(
y.contains("container_name: \"zc-share-abcd1234-w1\""),
"{y}"
);
assert!(y.contains("- \"127.0.0.1:4001:4001\""), "{y}");
assert!(y.contains("cpus: 1\n") || y.contains("cpus: 1.0\n"), "{y}");
assert!(y.contains(&format!("mem_limit: {}\n", 2 * GIB)), "{y}");
assert!(y.contains("zakuro.node: \"jobs\""), "{y}");
assert!(
y.contains(&format!("zakuro.budget: \"1000m,{}\"", 2 * GIB)),
"{y}"
);
assert!(!y.contains("0.0.0.0"), "never published beyond loopback");
let mut e = req(Some("pytorch"), 1.0, 2);
e.workers = Some(1);
let n = add(&mut reg, &e).unwrap();
assert_eq!(n.deploy_slice(), "50%");
assert!(compose_yaml(&n, "ffff0000", "img").contains(&format!("mem_limit: {}\n", GIB)));
let mut e = req(Some("jupyter"), 0.5, 1);
e.workers = Some(0);
let n = plan_add(&Registry::default(), &e, &machine(), "t").unwrap();
assert_eq!(n.deploy_slice(), "100%");
assert!(compose_yaml(&n, "ffff0000", "img").contains("services: {}"));
}
#[test]
fn a_nodes_policy_is_its_share_and_its_binding() {
let mut reg = Registry::default();
let env = add(&mut reg, &req(Some("pytorch"), 1.5, 3)).unwrap();
let mut p = crate::policy::Policy::default();
apply_policy(&mut p, &env);
assert_eq!(
(p.budget.cpu.as_str(), p.budget.disk.as_str()),
("1.5", "10G")
);
assert_eq!(p.budget.memory, (3 * GIB).to_string());
assert!(p.workloads.environments && !p.workloads.spaces && !p.workloads.hub_serving);
p.validate().unwrap();
let jobs = add(&mut reg, &req(None, 1.0, 2)).unwrap();
let mut p = crate::policy::Policy::default();
apply_policy(&mut p, &jobs);
assert!(!p.workloads.environments && p.workloads.spaces && p.workloads.hub_serving);
p.validate().unwrap();
}
#[test]
fn the_registry_round_trips_and_a_missing_one_is_empty() {
let dir = std::env::temp_dir().join(format!("zc-nodes-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(Registry::load(&dir), Registry::default());
let mut reg = Registry::default();
add(&mut reg, &req(Some("pytorch"), 1.0, 2)).unwrap();
reg.save(&dir).unwrap();
let back = Registry::load(&dir);
assert_eq!(back.nodes, reg.nodes);
assert_eq!(back.v, 1);
let _ = std::fs::remove_dir_all(&dir);
}
}