1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! Background health checker for service instances.
//!
//! Respects `ProbeConfig` from liveness configuration (interval, timeout,
//! failure threshold). Falls back to defaults when no liveness probe is set.
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tracing::{error, info, warn};
use orca_core::config::ProbeConfig;
use orca_core::runtime::{Runtime, WorkloadHandle};
use orca_core::types::{HealthState, RuntimeKind, WorkloadStatus};
use crate::routes::{service_config_to_spec, update_container_routes};
use crate::state::AppState;
const DEFAULT_FAILURES: u32 = 3;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(3);
const DEFAULT_INTERVAL: Duration = Duration::from_secs(10);
/// Spawn the health checker as a background tokio task.
pub fn spawn_health_checker(state: Arc<AppState>) {
tokio::spawn(async move {
let checker = HealthChecker::new(state);
checker.run(DEFAULT_INTERVAL).await;
});
}
/// Runs periodic health checks against service instances and restarts failed ones.
pub struct HealthChecker {
state: Arc<AppState>,
client: reqwest::Client,
}
impl HealthChecker {
/// Create a new health checker.
pub fn new(state: Arc<AppState>) -> Self {
let client = reqwest::Client::builder()
.timeout(DEFAULT_TIMEOUT)
.no_proxy()
.build()
.expect("failed to build reqwest client");
Self { state, client }
}
/// Run the health check loop at the given interval.
///
/// This is intended to be spawned as a background task.
pub async fn run(&self, interval: Duration) {
info!("Health checker started (interval: {}s)", interval.as_secs());
let mut failure_counts: HashMap<String, u32> = HashMap::new();
loop {
self.check_all(&mut failure_counts).await;
tokio::time::sleep(interval).await;
}
}
/// Check all services and their instances.
pub async fn check_all(&self, failure_counts: &mut HashMap<String, u32>) {
let check_targets: Vec<CheckTarget> = {
let services = self.state.services.read().await;
services
.values()
.filter_map(|svc| {
// Use liveness probe path, fall back to health path.
// Services with neither get runtime-only checks (no HTTP probe).
let (health_path, probe) = if let Some(lp) = &svc.config.liveness {
(Some(lp.path.clone()), Some(lp.clone()))
} else if let Some(path) = svc.config.health.as_deref() {
(Some(path.to_string()), None)
} else {
(None, None)
};
let initial_delay = probe
.as_ref()
.map(|p| Duration::from_secs(p.initial_delay_secs))
.unwrap_or(Duration::from_secs(5));
let targets: Vec<InstanceTarget> = svc
.instances
.iter()
.filter(|inst| inst.status == WorkloadStatus::Running)
.filter(|inst| inst.started_at.elapsed() >= initial_delay)
.map(|inst| InstanceTarget {
runtime_id: inst.handle.runtime_id.clone(),
handle: inst.handle.clone(),
host_port: inst.host_port,
})
.collect();
if targets.is_empty() {
return None;
}
Some(CheckTarget {
service_name: svc.config.name.clone(),
health_path,
probe_config: probe,
runtime_kind: svc.config.runtime,
targets,
})
})
.collect()
};
for target in &check_targets {
let threshold = target
.probe_config
.as_ref()
.map_or(DEFAULT_FAILURES, |p| p.failure_threshold);
let timeout = target
.probe_config
.as_ref()
.map_or(DEFAULT_TIMEOUT, |p| Duration::from_secs(p.timeout_secs));
let runtime: &dyn Runtime = match target.runtime_kind {
RuntimeKind::Container => self.state.container_runtime.as_ref(),
RuntimeKind::Wasm => match &self.state.wasm_runtime {
Some(r) => r.as_ref(),
None => continue,
},
};
for inst in &target.targets {
if inst.runtime_id.starts_with("remote-") {
continue;
}
let healthy = if let Some(path) = &target.health_path {
if let Some(port) = inst.host_port {
self.probe_with_timeout(port, path, timeout).await
} else {
runtime
.status(&inst.handle)
.await
.unwrap_or(WorkloadStatus::Failed)
== WorkloadStatus::Running
}
} else {
// No HTTP endpoint — runtime status is the health signal.
// Catches databases stuck after events like disk-full.
runtime
.status(&inst.handle)
.await
.unwrap_or(WorkloadStatus::Failed)
== WorkloadStatus::Running
};
let count = failure_counts.entry(inst.runtime_id.clone()).or_insert(0);
if healthy {
let was_unhealthy = *count > 0;
if was_unhealthy {
info!(
runtime_id = %inst.runtime_id,
service = %target.service_name,
"Instance recovered, resetting failure count"
);
}
*count = 0;
self.set_health(&target.service_name, &inst.runtime_id, HealthState::Healthy)
.await;
// Refresh routes when an instance becomes healthy.
self.refresh_routes(&target.service_name).await;
} else {
*count += 1;
warn!(
runtime_id = %inst.runtime_id,
service = %target.service_name,
consecutive_failures = *count,
"Health check failed"
);
self.set_health(
&target.service_name,
&inst.runtime_id,
HealthState::Unhealthy,
)
.await;
// Refresh routes so unhealthy backend is removed from rotation.
self.refresh_routes(&target.service_name).await;
if *count >= threshold {
info!(
runtime_id = %inst.runtime_id,
service = %target.service_name,
"Restarting after {} consecutive failures",
*count
);
self.restart_instance(
&target.service_name,
&inst.runtime_id,
target.runtime_kind,
)
.await;
failure_counts.remove(&inst.runtime_id);
}
}
}
}
}
/// Send an HTTP GET probe with a custom timeout.
async fn probe_with_timeout(&self, port: u16, path: &str, timeout: Duration) -> bool {
let url = format!("http://127.0.0.1:{port}{path}");
let client = reqwest::Client::builder()
.timeout(timeout)
.no_proxy()
.build()
.unwrap_or_else(|_| self.client.clone());
match client.get(&url).send().await {
Ok(resp) => resp.status().is_success(),
Err(_) => false,
}
}
/// Update the health state of a specific instance, looked up by runtime_id.
async fn set_health(&self, service_name: &str, runtime_id: &str, health: HealthState) {
let mut services = self.state.services.write().await;
if let Some(svc) = services.get_mut(service_name)
&& let Some(inst) = svc
.instances
.iter_mut()
.find(|i| i.handle.runtime_id == runtime_id)
{
inst.health = health;
}
}
/// Restart a failed instance by stopping/removing the old one and creating a new one.
async fn restart_instance(
&self,
service_name: &str,
runtime_id: &str,
runtime_kind: RuntimeKind,
) {
let runtime: &dyn Runtime = match runtime_kind {
RuntimeKind::Container => self.state.container_runtime.as_ref(),
RuntimeKind::Wasm => match &self.state.wasm_runtime {
Some(r) => r.as_ref(),
None => {
error!(service = %service_name, "Wasm runtime not available for restart");
return;
}
},
};
// Extract the old handle and config under a read lock, then drop it.
let (old_handle, spec, port) = {
let services = self.state.services.read().await;
let Some(svc) = services.get(service_name) else {
return;
};
let Some(inst) = svc
.instances
.iter()
.find(|i| i.handle.runtime_id == runtime_id)
else {
return;
};
let spec = match service_config_to_spec(&svc.config) {
Ok(s) => s,
Err(e) => {
error!(service = %service_name, "Failed to build spec: {e}");
return;
}
};
(inst.handle.clone(), spec, svc.config.port)
};
// Stop and remove the old container.
if let Err(e) = runtime.stop(&old_handle, Duration::from_secs(10)).await {
warn!(service = %service_name, "Failed to stop old instance: {e}");
}
if let Err(e) = runtime.remove(&old_handle).await {
warn!(service = %service_name, "Failed to remove old instance: {e}");
}
// Create and start a new one.
match runtime.create(&spec).await {
Ok(new_handle) => {
if let Err(e) = runtime.start(&new_handle).await {
error!(service = %service_name, "Failed to start new instance: {e}");
return;
}
let host_port = if let Some(p) = port {
runtime
.resolve_host_port(&new_handle, p)
.await
.ok()
.flatten()
} else {
None
};
{
let mut services = self.state.services.write().await;
if let Some(svc) = services.get_mut(service_name) {
// Look up by old runtime_id — index is unreliable after watchdog pruning.
if let Some(inst) = svc
.instances
.iter_mut()
.find(|i| i.handle.runtime_id == old_handle.runtime_id)
{
inst.handle = new_handle;
inst.status = WorkloadStatus::Running;
inst.host_port = host_port;
inst.started_at = std::time::Instant::now();
inst.health = HealthState::Healthy;
}
}
}
// Refresh routes with the new host_port.
self.refresh_routes(service_name).await;
info!(service = %service_name, "Instance restarted successfully");
}
Err(e) => {
error!(service = %service_name, "Failed to create replacement instance: {e}");
}
}
}
/// Recompute and update routes for a service after health/instance change.
async fn refresh_routes(&self, service_name: &str) {
let config = {
let services = self.state.services.read().await;
services.get(service_name).map(|s| s.config.clone())
};
if let Some(cfg) = config
&& cfg.runtime == RuntimeKind::Container
{
update_container_routes(&self.state, &cfg).await;
}
}
}
/// Internal struct for collecting check targets without holding locks.
struct CheckTarget {
service_name: String,
health_path: Option<String>,
probe_config: Option<ProbeConfig>,
runtime_kind: RuntimeKind,
targets: Vec<InstanceTarget>,
}
/// Internal struct for a single instance to probe.
struct InstanceTarget {
runtime_id: String,
handle: WorkloadHandle,
host_port: Option<u16>,
}