Skip to content

Commit f78b6f8

Browse files
committed
feat(compute)!: distinguish hard limits from sampled budgets
Expose enforcement kind and scope, require acknowledgement before budget writes, and reject changed remote guarantees before lifecycle mutation. Closes #5111 BREAKING CHANGE: Raw Health and resource messages gain acknowledgement, enforcement and sampled-budget fields. Upgrade complete literals and gateway writers in the next minor release. Signed-off-by: Shiju <[email protected]>
1 parent c92c2c7 commit f78b6f8

60 files changed

Lines changed: 3326 additions & 909 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/openshell-cli/src/commands/gateway.rs‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct ComputeDriverInfoView {
4646
struct ComputeDriverCapabilitiesView {
4747
driver_name: String,
4848
driver_version: String,
49+
resources: Option<openshell_core::proto::ResourceCapabilities>,
4950
}
5051

5152
#[derive(Debug, Clone)]
@@ -417,6 +418,7 @@ pub async fn gateway_info(
417418
capabilities: ComputeDriverCapabilitiesView {
418419
driver_name: capabilities.driver_name,
419420
driver_version: capabilities.driver_version,
421+
resources: capabilities.resource_capabilities,
420422
},
421423
}
422424
})
@@ -514,9 +516,125 @@ fn print_compute_driver_info(drivers: &[ComputeDriverInfoView]) {
514516
"Driver version:".dimmed(),
515517
driver.capabilities.driver_version
516518
);
519+
if let Some(resources) = &driver.capabilities.resources {
520+
for (name, supported, limit, request) in [
521+
(
522+
"CPU",
523+
resources.cpu.as_ref().map(|c| c.limit_supported),
524+
resources
525+
.cpu
526+
.as_ref()
527+
.and_then(|c| c.limit_enforcement.as_ref()),
528+
resources
529+
.cpu
530+
.as_ref()
531+
.and_then(|c| c.request_enforcement.as_ref()),
532+
),
533+
(
534+
"Memory",
535+
resources.memory.as_ref().map(|c| c.limit_supported),
536+
resources
537+
.memory
538+
.as_ref()
539+
.and_then(|c| c.limit_enforcement.as_ref()),
540+
resources
541+
.memory
542+
.as_ref()
543+
.and_then(|c| c.request_enforcement.as_ref()),
544+
),
545+
] {
546+
let guarantee = if supported == Some(false) {
547+
"unsupported".into()
548+
} else {
549+
enforcement_label(limit)
550+
};
551+
println!(
552+
" {name} limits: {guarantee}; requests: {}",
553+
enforcement_label(request)
554+
);
555+
}
556+
if let Some(sampled) = &resources.sampled_budget {
557+
println!(
558+
" Sampled budgets: CPU time={}, resident bytes={}, scope={}; terminate on excess={}",
559+
sampled.cpu_time_supported,
560+
sampled.resident_bytes_supported,
561+
scope_name(sampled.scope),
562+
sampled.terminate_on_excess
563+
);
564+
let interval = sampled.sampling_interval.map_or_else(
565+
|| "unreported".into(),
566+
|d| format!("{}.{:09} s", d.seconds, d.nanos),
567+
);
568+
println!(
569+
" Sampling interval: {interval} (plus collection time; overshoot unbounded)"
570+
);
571+
let resolution = sampled.cpu_time_resolution.map_or_else(
572+
|| "unreported".into(),
573+
|d| format!("{}.{:09} s", d.seconds, d.nanos),
574+
);
575+
println!(
576+
" CPU-time resolution: {resolution}; per-process CPU limit: {}",
577+
sampled.per_process_cpu_limit
578+
);
579+
}
580+
} else {
581+
println!(" Resource guarantees: unreported");
582+
}
583+
}
584+
}
585+
586+
fn scope_name(scope: i32) -> &'static str {
587+
use openshell_core::proto::sandbox::v1::ResourceEnforcementScope as Scope;
588+
match Scope::try_from(scope) {
589+
Ok(Scope::Process) => "process",
590+
Ok(Scope::Container) => "container",
591+
Ok(Scope::SandboxAccount) => "sandbox account",
592+
Ok(Scope::VirtualMachine) => "virtual machine",
593+
_ => "unreported",
517594
}
518595
}
519596

597+
fn enforcement_label(
598+
enforcement: Option<&openshell_core::proto::sandbox::v1::ResourceEnforcement>,
599+
) -> String {
600+
use openshell_core::proto::sandbox::v1::ResourceEnforcementKind as Kind;
601+
let Some(enforcement) = enforcement else {
602+
return "unreported".into();
603+
};
604+
let kind = match Kind::try_from(enforcement.kind) {
605+
Ok(Kind::KernelLimit) => "kernel limit",
606+
Ok(Kind::SchedulerRequest) => "scheduler request",
607+
Ok(Kind::Unsupported) => "unsupported",
608+
_ => "unreported",
609+
};
610+
format!("{kind} ({})", scope_name(enforcement.scope))
611+
}
612+
613+
fn resource_capabilities_json(
614+
resources: &openshell_core::proto::ResourceCapabilities,
615+
) -> serde_json::Value {
616+
fn enforcement(
617+
e: Option<&openshell_core::proto::sandbox::v1::ResourceEnforcement>,
618+
) -> serde_json::Value {
619+
e.map_or(
620+
serde_json::Value::Null,
621+
|e| serde_json::json!({"kind": e.kind, "scope": e.scope}),
622+
)
623+
}
624+
fn duration(d: Option<prost_types::Duration>) -> serde_json::Value {
625+
d.map_or(
626+
serde_json::Value::Null,
627+
|d| serde_json::json!({"seconds": d.seconds.to_string(), "nanos": d.nanos}),
628+
)
629+
}
630+
serde_json::json!({
631+
"cpu": resources.cpu.as_ref().map(|c| serde_json::json!({"limit_supported": c.limit_supported, "limit_enforcement": enforcement(c.limit_enforcement.as_ref()), "request_enforcement": enforcement(c.request_enforcement.as_ref())})),
632+
"memory": resources.memory.as_ref().map(|c| serde_json::json!({"limit_supported": c.limit_supported, "limit_enforcement": enforcement(c.limit_enforcement.as_ref()), "request_enforcement": enforcement(c.request_enforcement.as_ref())})),
633+
"gpu": resources.gpu.as_ref().map(|c| serde_json::json!({"default_selection_supported": c.default_selection_supported, "count_selection_supported": c.count_selection_supported})),
634+
"sampled_budget": resources.sampled_budget.as_ref().map(|c| serde_json::json!({"cpu_time_supported": c.cpu_time_supported, "resident_bytes_supported": c.resident_bytes_supported, "scope": c.scope, "sampling_interval": duration(c.sampling_interval), "terminate_on_excess": c.terminate_on_excess, "cpu_time_resolution": duration(c.cpu_time_resolution), "per_process_cpu_limit": c.per_process_cpu_limit})),
635+
})
636+
}
637+
520638
fn gateway_info_to_json(view: &GatewayInfoView) -> serde_json::Value {
521639
serde_json::json!({
522640
"gateway": &view.gateway,
@@ -532,6 +650,7 @@ fn gateway_info_to_json(view: &GatewayInfoView) -> serde_json::Value {
532650
"capabilities": {
533651
"driver_name": &driver.capabilities.driver_name,
534652
"driver_version": &driver.capabilities.driver_version,
653+
"resource_capabilities": driver.capabilities.resources.as_ref().map(resource_capabilities_json),
535654
},
536655
}))
537656
.collect::<Vec<_>>(),
@@ -1905,6 +2024,31 @@ mod tests {
19052024
capabilities: ComputeDriverCapabilitiesView {
19062025
driver_name: "podman".to_string(),
19072026
driver_version: "0.0.75".to_string(),
2027+
resources: Some(openshell_core::proto::ResourceCapabilities {
2028+
cpu: Some(openshell_core::proto::CpuResourceCapabilities {
2029+
limit_supported: true,
2030+
limit_enforcement: Some(
2031+
openshell_core::proto::sandbox::v1::ResourceEnforcement {
2032+
kind: 2,
2033+
scope: 2,
2034+
},
2035+
),
2036+
..Default::default()
2037+
}),
2038+
sampled_budget: Some(
2039+
openshell_core::proto::sandbox::v1::SampledResourceBudgetCapabilities {
2040+
resident_bytes_supported: true,
2041+
scope: 3,
2042+
sampling_interval: Some(prost_types::Duration {
2043+
seconds: 0,
2044+
nanos: 100_000_000,
2045+
}),
2046+
terminate_on_excess: true,
2047+
..Default::default()
2048+
},
2049+
),
2050+
..Default::default()
2051+
}),
19082052
},
19092053
}],
19102054
extensions: vec![ExtensionInfoView {
@@ -1928,6 +2072,16 @@ mod tests {
19282072
json["compute_drivers"][0]["capabilities"]["driver_name"],
19292073
"podman"
19302074
);
2075+
let resources = &json["compute_drivers"][0]["capabilities"]["resource_capabilities"];
2076+
assert_eq!(
2077+
resources["cpu"]["limit_enforcement"],
2078+
serde_json::json!({"kind": 2, "scope": 2})
2079+
);
2080+
assert_eq!(
2081+
resources["sampled_budget"]["sampling_interval"]["nanos"],
2082+
100_000_000
2083+
);
2084+
assert_eq!(resources["sampled_budget"]["terminate_on_excess"], true);
19312085
assert_eq!(json["extensions"][0]["protocol_version"], "1.0");
19322086
assert_eq!(
19332087
json["compute_drivers"][0]["capabilities"]["driver_version"],

‎crates/openshell-cli/src/main.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,16 @@ enum SandboxTemplateCommands {
19971997
#[arg(long)]
19981998
memory: Option<String>,
19991999

2000+
/// Sampled cumulative CPU-time budget (for example: 30s). Terminates on
2001+
/// observed excess; this is not a CPU-rate limit.
2002+
#[arg(long, value_name = "DURATION")]
2003+
cpu_time_budget: Option<String>,
2004+
2005+
/// Sampled account-wide resident-memory budget in bytes. Terminates on
2006+
/// observed excess; this is not a hard memory limit.
2007+
#[arg(long, value_name = "BYTES", value_parser = clap::value_parser!(u64).range(1..))]
2008+
resident_memory_budget_bytes: Option<u64>,
2009+
20002010
/// Request GPU resources for sandboxes created from this template.
20012011
///
20022012
/// Omit COUNT for the driver's default GPU selection, or pass COUNT
@@ -3764,6 +3774,8 @@ async fn run_async() -> Result<()> {
37643774
image,
37653775
cpu,
37663776
memory,
3777+
cpu_time_budget,
3778+
resident_memory_budget_bytes,
37673779
gpu,
37683780
driver_config_json,
37693781
ready_within,
@@ -3786,6 +3798,10 @@ async fn run_async() -> Result<()> {
37863798
image.as_deref(),
37873799
cpu.as_deref(),
37883800
memory.as_deref(),
3801+
run::sampled_resource_budget(
3802+
cpu_time_budget.as_deref(),
3803+
resident_memory_budget_bytes,
3804+
)?,
37893805
gpu_requirements,
37903806
driver_config_json.as_deref(),
37913807
ready_within.as_deref(),
@@ -6657,6 +6673,10 @@ mod tests {
66576673
"2",
66586674
"--memory",
66596675
"4Gi",
6676+
"--cpu-time-budget",
6677+
"30s",
6678+
"--resident-memory-budget-bytes",
6679+
"1073741824",
66606680
"--gpu",
66616681
"1",
66626682
"--driver-config-json",
@@ -6685,6 +6705,8 @@ mod tests {
66856705
image,
66866706
cpu,
66876707
memory,
6708+
cpu_time_budget,
6709+
resident_memory_budget_bytes,
66886710
gpu,
66896711
driver_config_json,
66906712
ready_within,
@@ -6697,6 +6719,8 @@ mod tests {
66976719
})),
66986720
..
66996721
}) => {
6722+
assert_eq!(cpu_time_budget.as_deref(), Some("30s"));
6723+
assert_eq!(resident_memory_budget_bytes, Some(1_073_741_824));
67006724
assert_eq!(name, "gpu-kata");
67016725
assert_eq!(image.as_deref(), Some("registry.example.com/agent:latest"));
67026726
assert_eq!(cpu.as_deref(), Some("2"));

0 commit comments

Comments
 (0)