interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / spark-supervisor / examples / readiness_timing.rs

123 lines · 4570 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Measures time from supervisor start to each service being reported Ready (issue #534).
17//!
18//! Sample config: `bare` = `sleep 30`, no health_url; `web` = `sleep 30` with a health_url
19//! served in-process that answers 503 until BOOT_MS after start, then 200 (a service that
20//! needs BOOT_MS to come up). Prints one raw line per service per run.
21//!
22//! cargo run -p spark-supervisor --example readiness_timing -- [runs] [probe_interval_ms]
23
24use std::collections::HashMap;
25use std::sync::Arc;
26use std::time::{Duration, Instant};
27
28use spark_supervisor::config::{ServiceConfig, SupervisorConfig};
29use spark_supervisor::supervisor::{ServiceState, Supervisor, SupervisorOptions};
30use tokio::io::{AsyncReadExt, AsyncWriteExt};
31use tokio::net::TcpListener;
32
33const BOOT_MS: u128 = 500;
34
35async fn boot_delayed_responder(t0: Instant) -> String {
36 let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
37 let url = format!("http://{}/health", listener.local_addr().unwrap());
38 tokio::spawn(async move {
39 while let Ok((mut sock, _)) = listener.accept().await {
40 tokio::spawn(async move {
41 let mut buf = [0u8; 2048];
42 let _ = sock.read(&mut buf).await;
43 let code = if t0.elapsed().as_millis() >= BOOT_MS {
44 200
45 } else {
46 503
47 };
48 let resp =
49 format!("HTTP/1.1 {code} X\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
50 let _ = sock.write_all(resp.as_bytes()).await;
51 });
52 }
53 });
54 url
55}
56
57fn sleeper(health_url: Option<String>) -> ServiceConfig {
58 ServiceConfig {
59 command: "sleep".into(),
60 args: vec!["30".into()],
61 env: HashMap::new(),
62 restart: "always".into(),
63 health_url,
64 }
65}
66
67#[tokio::main]
68async fn main() {
69 let args: Vec<String> = std::env::args().collect();
70 let runs: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(3);
71 let mut options = SupervisorOptions::default();
72 if let Some(ms) = args.get(2).and_then(|s| s.parse().ok()) {
73 options.probe_interval = Duration::from_millis(ms);
74 }
75 println!(
76 "branch run probe_interval_ms={} boot_ms={BOOT_MS}",
77 options.probe_interval.as_millis()
78 );
79
80 for run in 1..=runs {
81 let t0 = Instant::now();
82 let url = boot_delayed_responder(t0).await;
83 let mut services = HashMap::new();
84 services.insert("bare".to_string(), sleeper(None));
85 services.insert("web".to_string(), sleeper(Some(url)));
86 let sup = Arc::new(Supervisor::with_options(
87 SupervisorConfig { services },
88 options.clone(),
89 ));
90 let (tx, rx) = tokio::sync::oneshot::channel::<()>();
91 let s = sup.clone();
92 let task = tokio::spawn(async move {
93 s.run_until(async {
94 let _ = rx.await;
95 })
96 .await
97 });
98
99 let mut spawned: HashMap<String, u128> = HashMap::new();
100 let mut ready: HashMap<String, u128> = HashMap::new();
101 while ready.len() < 2 && t0.elapsed() < Duration::from_secs(20) {
102 for (name, st) in sup.snapshot().await {
103 let now = t0.elapsed().as_millis();
104 if st.pid.is_some() {
105 spawned.entry(name.clone()).or_insert(now);
106 }
107 if st.state == ServiceState::Ready {
108 ready.entry(name).or_insert(now);
109 }
110 }
111 tokio::time::sleep(Duration::from_millis(1)).await;
112 }
113 let _ = tx.send(());
114 let _ = task.await;
115 for name in ["bare", "web"] {
116 println!(
117 "run={run} service={name} spawned_ms={:?} reported_ready_ms={:?}",
118 spawned.get(name),
119 ready.get(name)
120 );
121 }
122 }
123}