feat(#2754): hive-wide CPUWeight= / IOWeight= for agent containers
`CPUQuota=`/`MemoryMax=` are hard caps: they throttle an agent even when
the host is idle, so they are the wrong tool for "be polite under
contention". The cgroup v2 relative shares are, and neither was wired.
Adds `services.hyperhive.{agentCpuWeight,agentIoWeight}` (1..=10000,
default 80) threaded through the existing drop-in path: HiveEnv ->
write_dropins -> WriteResourceLimits -> hyperhive-limits.conf, next to
the caps already there. Hive-wide only, as the operator scoped it on the
issue: no per-agent override, no resource-limits.json field, no
dashboard form.
The default of 80 is below the kernel's 100, so agent containers yield
to everything *not* on this drop-in path -- host services and the infra
containers (hive-ci, hive-forge, hive-gateway, hive-matrix). It does not
rank agents against each other; they all carry the same weight.
`WriteResourceLimits` gains two `#[serde(default)]` fields, and the
writer treats weight 0 as "not configured" and omits the line, so an
older hive-c0re talking to a newer hive-priv still produces the exact
pre-weights drop-in. The body is extracted into `limits_dropin_body` so
that is covered by a test rather than asserted by eye.
This commit is contained in:
parent
80650041d9
commit
e407fa93df
8 changed files with 221 additions and 16 deletions
|
|
@ -223,7 +223,9 @@ async fn exec(req: PrivRequest, writer: &mut OwnedWriteHalf) -> Result<(String,
|
|||
ref container,
|
||||
ref memory_max,
|
||||
ref cpu_quota,
|
||||
} => write_resource_limits(container, memory_max, cpu_quota),
|
||||
cpu_weight,
|
||||
io_weight,
|
||||
} => write_resource_limits(container, memory_max, cpu_quota, cpu_weight, io_weight),
|
||||
|
||||
PrivRequest::RemoveServiceDropin { ref container } => remove_service_dropin(container),
|
||||
|
||||
|
|
@ -527,10 +529,17 @@ fn chmod_socket_dir(agent_name: &str, mode: u32) -> Result<(String, String)> {
|
|||
Ok((String::new(), String::new()))
|
||||
}
|
||||
|
||||
/// `WriteResourceLimits` — drop a systemd `MemoryMax`/`CPUQuota`
|
||||
/// override into the container service's drop-in dir, together with a
|
||||
/// `WriteResourceLimits` — drop the systemd resource settings into the
|
||||
/// container service's drop-in dir, together with a
|
||||
/// `ConditionPathIsDirectory=` guard on the agent's MCP runtime dir.
|
||||
///
|
||||
/// Two different kinds of setting land in the same file. `MemoryMax=` /
|
||||
/// `CPUQuota=` are hard caps that throttle even on an idle host;
|
||||
/// `CPUWeight=` / `IOWeight=` are cgroup v2 relative shares that only
|
||||
/// decide who yields *under contention*. A zero weight means "not
|
||||
/// configured" and omits the line, so a hive-c0re built before the weights
|
||||
/// existed keeps producing the old two-line drop-in.
|
||||
///
|
||||
/// The condition causes systemd to *skip* (not *fail*) the unit when the
|
||||
/// bind-mount source dir is absent — result is `condition`, which does not
|
||||
/// increment the start-limit counter. This is belt-and-braces on top of
|
||||
|
|
@ -541,6 +550,8 @@ fn write_resource_limits(
|
|||
container: &str,
|
||||
memory_max: &str,
|
||||
cpu_quota: &str,
|
||||
cpu_weight: u32,
|
||||
io_weight: u32,
|
||||
) -> Result<(String, String)> {
|
||||
validate_container_system_name(container)?;
|
||||
// Derive the logical agent name (strip h- prefix) to form the runtime
|
||||
|
|
@ -551,19 +562,48 @@ fn write_resource_limits(
|
|||
let dir = format!("/run/systemd/system/container@{container}.service.d");
|
||||
std::fs::create_dir_all(&dir).with_context(|| format!("create {dir}"))?;
|
||||
let path = format!("{dir}/hyperhive-limits.conf");
|
||||
// [Unit] section: condition checked at start time — skips (not fails)
|
||||
// the unit when the MCP socket dir is absent, avoiding restart loops.
|
||||
// [Service] section: resource caps.
|
||||
let content = format!(
|
||||
let content = limits_dropin_body(&runtime_dir, memory_max, cpu_quota, cpu_weight, io_weight);
|
||||
std::fs::write(&path, content).with_context(|| format!("write {path}"))?;
|
||||
Ok((String::new(), String::new()))
|
||||
}
|
||||
|
||||
/// Render the body of `hyperhive-limits.conf`.
|
||||
///
|
||||
/// `[Unit]`: the condition is checked at start time — it skips (not fails)
|
||||
/// the unit when the MCP socket dir is absent, avoiding restart loops.
|
||||
/// `[Service]`: the hard caps first, then the relative weights. A weight of
|
||||
/// `0` means "not configured" and omits its line entirely, so a request from
|
||||
/// a hive-c0re built before the weights existed reproduces the old
|
||||
/// two-setting drop-in byte for byte.
|
||||
fn limits_dropin_body(
|
||||
runtime_dir: &str,
|
||||
memory_max: &str,
|
||||
cpu_quota: &str,
|
||||
cpu_weight: u32,
|
||||
io_weight: u32,
|
||||
) -> String {
|
||||
// Built as two possibly-empty lines rather than pushed onto the
|
||||
// string: `format!` appended to a `String` trips clippy::pedantic's
|
||||
// `format_push_string`, and a `write!` would need an unwrap.
|
||||
let cpu_weight_line = if cpu_weight > 0 {
|
||||
format!("CPUWeight={cpu_weight}\n")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let io_weight_line = if io_weight > 0 {
|
||||
format!("IOWeight={io_weight}\n")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
format!(
|
||||
"[Unit]\n\
|
||||
ConditionPathIsDirectory={runtime_dir}\n\
|
||||
\n\
|
||||
[Service]\n\
|
||||
MemoryMax={memory_max}\n\
|
||||
CPUQuota={cpu_quota}\n"
|
||||
);
|
||||
std::fs::write(&path, content).with_context(|| format!("write {path}"))?;
|
||||
Ok((String::new(), String::new()))
|
||||
CPUQuota={cpu_quota}\n\
|
||||
{cpu_weight_line}{io_weight_line}"
|
||||
)
|
||||
}
|
||||
|
||||
/// `DaemonReload` — `systemctl daemon-reload` on the host.
|
||||
|
|
@ -2185,11 +2225,53 @@ async fn sync_agent_tmpfiles(agents: &[String]) -> Result<(String, String)> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
PAUSED_MARKER_FILE, redact_password_line, remove_marker_in, write_state_file_nofollow,
|
||||
PAUSED_MARKER_FILE, limits_dropin_body, redact_password_line, remove_marker_in,
|
||||
write_state_file_nofollow,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
/// A zero weight is "not configured": the drop-in must come out
|
||||
/// byte-identical to the pre-weights two-setting body, so a hive-c0re
|
||||
/// older than this field can't change what lands on disk.
|
||||
#[test]
|
||||
fn zero_weights_reproduce_the_pre_weights_dropin() {
|
||||
assert_eq!(
|
||||
limits_dropin_body("/run/hyperhive/agents/iris", "4G", "200%", 0, 0),
|
||||
"[Unit]\n\
|
||||
ConditionPathIsDirectory=/run/hyperhive/agents/iris\n\
|
||||
\n\
|
||||
[Service]\n\
|
||||
MemoryMax=4G\n\
|
||||
CPUQuota=200%\n"
|
||||
);
|
||||
}
|
||||
|
||||
/// Weights are appended to the `[Service]` section, each omitted
|
||||
/// independently when zero.
|
||||
#[test]
|
||||
fn weights_are_emitted_only_when_set() {
|
||||
let both = limits_dropin_body("/rt/x", "4G", "200%", 80, 80);
|
||||
assert!(
|
||||
both.ends_with("CPUQuota=200%\nCPUWeight=80\nIOWeight=80\n"),
|
||||
"{both}"
|
||||
);
|
||||
|
||||
let cpu_only = limits_dropin_body("/rt/x", "4G", "200%", 80, 0);
|
||||
assert!(
|
||||
cpu_only.ends_with("CPUQuota=200%\nCPUWeight=80\n"),
|
||||
"{cpu_only}"
|
||||
);
|
||||
assert!(!cpu_only.contains("IOWeight"), "{cpu_only}");
|
||||
|
||||
let io_only = limits_dropin_body("/rt/x", "4G", "200%", 0, 80);
|
||||
assert!(
|
||||
io_only.ends_with("CPUQuota=200%\nIOWeight=80\n"),
|
||||
"{io_only}"
|
||||
);
|
||||
assert!(!io_only.contains("CPUWeight"), "{io_only}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacts_lines_mentioning_password_case_insensitively() {
|
||||
assert_eq!(
|
||||
|
|
|
|||
Loading…
Reference in a new issue