use super::*;
const DEFAULT_HEALTH_CADENCE: Duration = Duration::from_secs(30);
const DEFAULT_HEALTH_DEADLINE: Duration = Duration::from_secs(5);
const DEFAULT_HEALTH_FAILURE_THRESHOLD: u32 = 3;
const MAX_HEALTH_METRICS_BYTES: usize = 16 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthAction {
Report,
Restart,
Alert,
}
impl fmt::Display for HealthAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Report => "report",
Self::Restart => "restart",
Self::Alert => "alert",
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HealthConfig {
pub http: Option<String>,
pub cadence: Duration,
pub deadline: Duration,
pub failure_threshold: u32,
pub on_degraded: HealthAction,
pub on_failing: HealthAction,
pub critical: bool,
}
impl Default for HealthConfig {
fn default() -> Self {
Self {
http: None,
cadence: DEFAULT_HEALTH_CADENCE,
deadline: DEFAULT_HEALTH_DEADLINE,
failure_threshold: DEFAULT_HEALTH_FAILURE_THRESHOLD,
on_degraded: HealthAction::Report,
on_failing: HealthAction::Report,
critical: false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ModuleHealthStatus {
pub status: SupervisorHealthStatus,
pub last_probe_ms: Option<u64>,
pub detail: Option<String>,
pub metrics: Option<Value>,
pub consecutive_failures: u32,
pub late_answer_count: u64,
pub last_late_answer_latency_ms: Option<u64>,
pub last_action: Option<String>,
pub last_action_ms: Option<u64>,
}
impl Default for ModuleHealthStatus {
fn default() -> Self {
Self {
status: SupervisorHealthStatus::Unknown,
last_probe_ms: None,
detail: None,
metrics: None,
consecutive_failures: 0,
late_answer_count: 0,
last_late_answer_latency_ms: None,
last_action: None,
last_action_ms: None,
}
}
}
impl SupervisorHandle {
pub(crate) fn record_late_health_answer(
&self,
module_id: &str,
latency_ms: u64,
) -> Result<bool, SuperviseError> {
let Some(module) = self.get(module_id) else {
return Ok(false);
};
self.host_health.record_late_module(module_id);
update_snapshot(&module.inner.snapshot, Some(module_id), |state| {
state.health.late_answer_count = state.health.late_answer_count.saturating_add(1);
state.health.last_late_answer_latency_ms = Some(latency_ms);
state.health.consecutive_failures = 0;
})?;
Ok(true)
}
pub(crate) fn spawn_host_health_monitor(&self) -> JoinHandle<()> {
self.host_health.spawn_monitor()
}
}
impl SupervisedModule {
#[cfg(test)]
pub(crate) fn record_health_probe_failure_for_test(
&self,
detail: &str,
) -> Result<(), SuperviseError> {
update_snapshot(&self.inner.snapshot, Some(&self.inner.module_id), |state| {
state.health.consecutive_failures = state.health.consecutive_failures.saturating_add(1);
state.health.detail = Some(detail.to_string());
})
}
}
#[derive(Debug, Default)]
pub(super) struct HealthProbeRuntime {
spawn_generation: Option<u64>,
configured_health: Option<HealthConfig>,
pub(super) registered_connection: Option<crate::ConnectionId>,
pub(super) advertised: bool,
pub(super) next_probe_at: Option<Instant>,
probe_index: u64,
}
pub(super) fn running_protocol(spec: &ModuleSpec, snapshot: &SharedSnapshot) -> ModuleProtocol {
lock_snapshot(snapshot)
.ok()
.and_then(|state| state.spawned_protocol)
.unwrap_or(spec.protocol)
}
impl HealthProbeRuntime {
pub(super) fn refresh_registration(
&mut self,
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
registry: &Registry,
snapshot: &SharedSnapshot,
) {
let generation = lock_snapshot(snapshot)
.ok()
.map(|state| state.spawn_generation);
if self.spawn_generation != generation {
*self = Self {
spawn_generation: generation,
..Self::default()
};
}
if self.configured_health.as_ref() != Some(&runtime.health) {
self.configured_health = Some(runtime.health.clone());
self.next_probe_at = None;
self.registered_connection = None;
self.probe_index = 0;
}
if running_protocol(spec, snapshot) == ModuleProtocol::None {
self.registered_connection = None;
self.advertised = runtime.health.http.is_some();
if !self.advertised {
self.next_probe_at = None;
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
let unknown = ModuleHealthStatus::default();
if state.health != unknown {
state.health = unknown;
state.last_probe_evidence = None;
}
});
} else if self.next_probe_at.is_none() {
self.next_probe_at = Some(
Instant::now()
+ jittered_health_delay(&spec.module_id, 0, runtime.health.cadence),
);
}
return;
}
let registration = match registry.get_module(&spec.module_id) {
Ok(registration) => registration,
Err(err) => {
warn!(module_id = %spec.module_id, error = %err, "health prober could not read registry");
self.advertised = false;
self.next_probe_at = None;
return;
}
};
let Some(registration) = registration else {
self.registered_connection = None;
self.advertised = false;
self.next_probe_at = None;
return;
};
let advertised = registration
.control_ops
.iter()
.any(|op| op == MODULE_CONTROL_OP_HEALTH_CHECK);
if !advertised {
self.registered_connection = Some(registration.connection_id);
self.advertised = false;
self.next_probe_at = None;
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
if state.health.status != SupervisorHealthStatus::Unknown
|| state.health.consecutive_failures != 0
|| state.health.last_probe_ms.is_some()
|| state.health.detail.is_some()
|| state.health.metrics.is_some()
{
state.health.status = SupervisorHealthStatus::Unknown;
state.last_probe_evidence = None;
state.health.consecutive_failures = 0;
state.health.last_probe_ms = None;
state.health.detail = None;
state.health.metrics = None;
}
});
return;
}
let reregistered = self.registered_connection != Some(registration.connection_id);
self.registered_connection = Some(registration.connection_id);
self.advertised = true;
if reregistered || self.next_probe_at.is_none() {
self.probe_index = 0;
self.next_probe_at = Some(
Instant::now() + jittered_health_delay(&spec.module_id, 0, runtime.health.cadence),
);
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
state.health.status = SupervisorHealthStatus::Unknown;
state.last_probe_evidence = None;
state.health.consecutive_failures = 0;
state.health.detail = None;
state.health.metrics = None;
});
}
}
pub(super) fn wake_at(&self) -> Option<Instant> {
if !self.advertised {
return None;
}
self.next_probe_at
}
pub(super) fn due(&self) -> bool {
self.advertised
&& self
.next_probe_at
.is_some_and(|next| Instant::now() >= next)
}
pub(super) fn schedule_next(&mut self, spec: &ModuleSpec, cadence: Duration) {
self.probe_index = self.probe_index.wrapping_add(1);
self.next_probe_at = Some(
Instant::now() + jittered_health_delay(&spec.module_id, self.probe_index, cadence),
);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum HealthProbeEvidence {
LaneDead,
NoAnswer,
BadAnswer,
Misconfigured,
}
#[derive(Debug)]
pub(super) struct HealthProbeError {
pub(super) evidence: HealthProbeEvidence,
message: String,
}
impl HealthProbeError {
pub(super) fn lane_dead(message: impl Into<String>) -> Self {
Self::with(HealthProbeEvidence::LaneDead, message)
}
pub(super) fn no_answer(message: impl Into<String>) -> Self {
Self::with(HealthProbeEvidence::NoAnswer, message)
}
pub(super) fn bad_answer(message: impl Into<String>) -> Self {
Self::with(HealthProbeEvidence::BadAnswer, message)
}
pub(super) fn misconfigured(message: impl Into<String>) -> Self {
Self::with(HealthProbeEvidence::Misconfigured, message)
}
fn with(evidence: HealthProbeEvidence, message: impl Into<String>) -> Self {
Self {
evidence,
message: message.into(),
}
}
#[allow(dead_code)]
pub(super) fn is_proof_of_death(&self) -> bool {
matches!(self.evidence, HealthProbeEvidence::LaneDead)
}
pub(super) fn label(&self) -> &'static str {
match self.evidence {
HealthProbeEvidence::LaneDead => "lane-dead",
HealthProbeEvidence::NoAnswer => "no-answer",
HealthProbeEvidence::BadAnswer => "bad-answer",
HealthProbeEvidence::Misconfigured => "daemon-misconfigured",
}
}
}
impl fmt::Display for HealthProbeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
pub(super) async fn run_health_probe_cycle(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
registry: &Registry,
process_liveness: &SupervisorProcessLiveness,
snapshot: &SharedSnapshot,
child: &mut Option<SupervisedChild>,
) {
let now_ms = unix_ms_now();
let delivery_probe = match &runtime.config_delivery {
Some(delivery) => {
delivery
.health_probe(&spec.module_id, runtime.forwarding.as_deref(), registry)
.await
}
None => None,
};
let http = (running_protocol(spec, snapshot) == ModuleProtocol::None)
.then_some(runtime.health.http.as_deref())
.flatten();
let result = match http {
Some(url) => probe_http_health(url, runtime.health.deadline)
.await
.map(|report| (report, None)),
None => probe_module_health_from(&spec.module_id, runtime, None)
.await
.map(|(report, probed)| (report, Some(probed))),
};
match result {
Ok((report, probed)) => {
handle_health_report(
spec,
runtime,
registry,
process_liveness,
snapshot,
child,
report,
now_ms,
delivery_probe,
probed,
)
.await;
}
Err(err) => {
if http.is_some() {
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
state.health.status = SupervisorHealthStatus::Failing;
});
}
handle_health_probe_failure(
spec,
runtime,
registry,
process_liveness,
snapshot,
child,
err,
now_ms,
delivery_probe,
)
.await;
}
}
}
pub(crate) struct HttpProbeTarget<'a> {
address: std::net::SocketAddr,
localhost: bool,
authority: &'a str,
path: String,
}
pub(crate) fn parse_http_probe_url(url: &str) -> Result<HttpProbeTarget<'_>, String> {
if url.bytes().any(|b| b <= b' ' || b == 127) || url.contains('#') {
return Err("must not contain whitespace, controls, or a fragment".into());
}
let rest = url
.strip_prefix("http://")
.ok_or("must use plain http://")?;
let split = rest.find(['/', '?']).unwrap_or(rest.len());
let (authority, suffix) = rest.split_at(split);
let (host, port) = if let Some(rest) = authority.strip_prefix("[::1]") {
("::1", rest)
} else {
let split = authority.find(':').unwrap_or(authority.len());
authority.split_at(split)
};
let ip: std::net::IpAddr = match host {
"127.0.0.1" | "localhost" => std::net::Ipv4Addr::LOCALHOST.into(),
"::1" => std::net::Ipv6Addr::LOCALHOST.into(),
_ => return Err("host must be 127.0.0.1, [::1], or localhost".into()),
};
let port = if port.is_empty() {
80
} else {
port.strip_prefix(':')
.and_then(|p| p.parse::<u16>().ok())
.filter(|p| *p > 0)
.ok_or("must have a valid nonzero TCP port")?
};
let path = if suffix.is_empty() {
"/".into()
} else if suffix.starts_with('?') {
format!("/{suffix}")
} else {
suffix.into()
};
Ok(HttpProbeTarget {
address: std::net::SocketAddr::new(ip, port),
localhost: host == "localhost",
authority,
path,
})
}
pub(super) async fn probe_http_health(
url: &str,
deadline: Duration,
) -> Result<HealthReport, HealthProbeError> {
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
let target = parse_http_probe_url(url).map_err(HealthProbeError::misconfigured)?;
let mut response_status = String::new();
let mut body = Vec::new();
let probe = async {
let connection = match tokio::net::TcpStream::connect(target.address).await {
Err(_) if target.localhost => {
tokio::net::TcpStream::connect((
std::net::Ipv6Addr::LOCALHOST,
target.address.port(),
))
.await
}
result => result,
};
let mut stream = connection.map_err(|error| {
HealthProbeError::no_answer(format!("HTTP connect failed: {error}"))
})?;
stream.write_all(format!("GET {} HTTP/1.1\r\nHost: {}\r\nAccept-Encoding: identity\r\nConnection: close\r\n\r\n", target.path, target.authority).as_bytes()).await
.map_err(|error| HealthProbeError::no_answer(format!("HTTP write failed: {error}")))?;
let mut reader = BufReader::new(stream);
let mut budget = 16 * 1024;
let status = http_line(&mut reader, &mut budget).await?;
let mut words = status.split_ascii_whitespace();
let version = words.next();
let code = words
.next()
.filter(|word| word.len() == 3)
.and_then(|word| word.parse::<u16>().ok());
if !matches!(version, Some("HTTP/1.1" | "HTTP/1.0"))
|| !code.is_some_and(|code| (100..600).contains(&code))
{
return Err(HealthProbeError::bad_answer(format!(
"invalid HTTP status: {status}"
)));
}
let code = code.expect("validated status code");
response_status = status.clone();
let mut length = None;
let mut chunked = false;
loop {
let line = http_line(&mut reader, &mut budget).await?;
if line.is_empty() {
break;
}
if let Some((name, value)) = line.split_once(':') {
if name.eq_ignore_ascii_case("content-length") {
length = Some(value.trim().parse::<u64>().map_err(|_| {
HealthProbeError::bad_answer("invalid HTTP Content-Length")
})?);
} else if name.eq_ignore_ascii_case("transfer-encoding") {
chunked = value.trim().eq_ignore_ascii_case("chunked");
}
}
}
if chunked {
while body.len() < 200 {
let line = http_line(&mut reader, &mut budget).await?;
let size = u64::from_str_radix(line.split(';').next().unwrap_or("").trim(), 16)
.map_err(|_| HealthProbeError::bad_answer("invalid HTTP chunk size"))?;
if size == 0 {
break;
}
let count = size.min((200 - body.len()) as u64) as usize;
let start = body.len();
(&mut reader)
.take(count as u64)
.read_to_end(&mut body)
.await
.map_err(|error| {
HealthProbeError::no_answer(format!("HTTP body read failed: {error}"))
})?;
if body.len() - start != count {
return Err(HealthProbeError::bad_answer("truncated HTTP chunk"));
}
if size > count as u64 || body.len() == 200 {
break;
}
if !http_line(&mut reader, &mut budget).await?.is_empty() {
return Err(HealthProbeError::bad_answer("invalid HTTP chunk delimiter"));
}
}
} else {
reader
.take(length.unwrap_or(200).min(200))
.read_to_end(&mut body)
.await
.map_err(|error| {
HealthProbeError::no_answer(format!("HTTP body read failed: {error}"))
})?;
}
if (200..300).contains(&code) {
Ok(HealthReport::ok())
} else {
Err(HealthProbeError::bad_answer(
"HTTP health endpoint returned non-2xx",
))
}
};
let mut result = timeout(deadline, probe).await.unwrap_or_else(|_| {
Err(HealthProbeError::no_answer(format!(
"HTTP probe timed out after {deadline:?}"
)))
});
if let Err(error) = &mut result {
if !response_status.is_empty() {
error.message = format!(
"{}; {response_status}: {}",
error.message,
String::from_utf8_lossy(&body)
);
}
}
result
}
async fn http_line(
reader: &mut tokio::io::BufReader<tokio::net::TcpStream>,
remaining: &mut usize,
) -> Result<String, HealthProbeError> {
use tokio::io::{AsyncBufReadExt, AsyncReadExt};
let mut line = Vec::new();
(&mut *reader)
.take(*remaining as u64)
.read_until(b'\n', &mut line)
.await
.map_err(|error| {
HealthProbeError::no_answer(format!("HTTP header read failed: {error}"))
})?;
*remaining -= line.len();
if !line.ends_with(b"\r\n") {
return Err(HealthProbeError::bad_answer(
"HTTP headers are incomplete or exceed 16 KiB",
));
}
line.truncate(line.len() - 2);
String::from_utf8(line).map_err(|_| HealthProbeError::bad_answer("HTTP header is not UTF-8"))
}
pub(super) async fn probe_module_health(
module_id: &str,
runtime: &SupervisorRuntimeConfig,
drain_deadline: Option<Instant>,
) -> Result<HealthReport, HealthProbeError> {
probe_module_health_from(module_id, runtime, drain_deadline)
.await
.map(|(report, _)| report)
}
async fn probe_module_health_from(
module_id: &str,
runtime: &SupervisorRuntimeConfig,
drain_deadline: Option<Instant>,
) -> Result<(HealthReport, ConnectionId), HealthProbeError> {
let Some(forwarding) = runtime.forwarding.as_ref() else {
return Err(HealthProbeError::misconfigured(
"supervisor was not configured with a forwarding table",
));
};
let probe_started_at = Instant::now();
let mut deadline = probe_started_at + runtime.health.deadline;
if let Some(drain_deadline) = drain_deadline {
deadline = deadline.min(drain_deadline);
}
let pending = if drain_deadline.is_some() {
forwarding.begin_drain_health_probe_rpc_for(
module_id,
MODULE_CONTROL_OP_HEALTH_CHECK,
probe_started_at,
deadline,
)
} else {
forwarding.begin_health_probe_rpc_for(
module_id,
MODULE_CONTROL_OP_HEALTH_CHECK,
probe_started_at,
deadline,
)
}
.map_err(|err| {
HealthProbeError::lane_dead(format!("failed to begin health.check RPC: {err}"))
})?;
let probed = pending.endpoint.connection_id;
await_health_probe(forwarding, pending, deadline, runtime.health.deadline)
.await
.map(|report| (report, probed))
}
pub(super) async fn probe_endpoint_health(
endpoint: crate::ModuleEndpointId,
runtime: &SupervisorRuntimeConfig,
deadline_cap: Option<Instant>,
) -> Result<HealthReport, HealthProbeError> {
let Some(forwarding) = runtime.forwarding.as_ref() else {
return Err(HealthProbeError::misconfigured(
"supervisor was not configured with a forwarding table",
));
};
let probe_started_at = Instant::now();
let mut deadline = probe_started_at + runtime.health.deadline;
if let Some(cap) = deadline_cap {
deadline = deadline.min(cap);
}
let pending = forwarding
.begin_endpoint_health_probe_rpc_for(
endpoint,
MODULE_CONTROL_OP_HEALTH_CHECK,
probe_started_at,
deadline,
)
.map_err(|err| {
HealthProbeError::lane_dead(format!("failed to begin health.check RPC: {err}"))
})?;
await_health_probe(forwarding, pending, deadline, runtime.health.deadline).await
}
async fn await_health_probe(
forwarding: &ForwardingTable,
pending: PendingModuleControlRpc,
deadline: Instant,
probe_budget: Duration,
) -> Result<HealthReport, HealthProbeError> {
let PendingModuleControlRpc {
endpoint,
module_sink,
negotiated_ver,
corr,
receiver,
} = pending;
let body = serde_json::to_vec(&ModuleControlRequest::HealthCheck {}).map_err(|err| {
HealthProbeError::misconfigured(format!("failed to encode health.check: {err}"))
})?;
let frame = Frame::build_with_version(
negotiated_ver,
FrameType::Request,
control_flags(),
0,
0,
corr,
body,
)
.map_err(|err| {
HealthProbeError::misconfigured(format!("failed to build health.check frame: {err}"))
})?;
match timeout_at(deadline, module_sink.send(frame)).await {
Ok(Ok(())) => {}
Ok(Err(err)) => {
let _ = forwarding.cancel_module_control_rpc(endpoint, corr);
return Err(HealthProbeError::lane_dead(format!(
"failed to send health.check: {err}"
)));
}
Err(_elapsed) => {
let _ = forwarding.cancel_module_control_rpc(endpoint, corr);
return Err(HealthProbeError::no_answer(
"health.check send timed out before enqueue (module egress full)",
));
}
}
match timeout_at(deadline, receiver).await {
Ok(Ok(ModuleControlRpcOutcome::RawResponse(_))) => Err(HealthProbeError::bad_answer(
"health.check returned an unexpected config response",
)),
Ok(Ok(ModuleControlRpcOutcome::Response(response))) => {
response.health_report().ok_or_else(|| {
HealthProbeError::bad_answer("health.check RPC returned a non-health response")
})
}
Ok(Ok(ModuleControlRpcOutcome::Rejected(body))) => Err(HealthProbeError::bad_answer(
format!("health.check rejected: {}", body.message),
)),
Ok(Ok(ModuleControlRpcOutcome::ModuleGone(message))) => {
Err(HealthProbeError::lane_dead(message))
}
Ok(Ok(ModuleControlRpcOutcome::MalformedResponse(message))) => {
Err(HealthProbeError::bad_answer(message))
}
Ok(Ok(ModuleControlRpcOutcome::UnexpectedOp { expected, actual })) => {
Err(HealthProbeError::bad_answer(format!(
"expected module-control op '{expected}', got '{actual}'"
)))
}
Ok(Ok(ModuleControlRpcOutcome::DeadlineElapsed)) => Err(HealthProbeError::bad_answer(
"module answered health.check after its daemon deadline",
)),
Ok(Err(_)) => Err(HealthProbeError::misconfigured(
"health.check waiter was canceled before the module responded",
)),
Err(_) => {
let _ = forwarding.tombstone_health_probe_rpc(endpoint, corr);
Err(HealthProbeError::no_answer(format!(
"module did not answer health.check within {probe_budget:?}"
)))
}
}
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn handle_health_report(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
registry: &Registry,
process_liveness: &SupervisorProcessLiveness,
snapshot: &SharedSnapshot,
child: &mut Option<SupervisedChild>,
report: HealthReport,
now_ms: u64,
delivery_probe: Option<crate::config_gate::runtime::DeliveryProbe>,
probed: Option<ConnectionId>,
) {
let delivery_failed = match (&runtime.config_delivery, delivery_probe) {
(Some(delivery), Some(probe)) => {
delivery
.health_result(
&spec.module_id,
runtime.forwarding.as_deref(),
registry,
probe,
report.status == HealthStatus::Ok,
)
.await
}
_ => false,
};
if let (Some(delivery), Some(spawn)) = (
&runtime.config_delivery,
child
.as_ref()
.and_then(|child| child.config_delivery.as_ref()),
) {
delivery
.mark_readiness(
&spec.module_id,
runtime.forwarding.as_deref(),
probed,
spawn,
report.status == HealthStatus::Ok,
)
.await;
}
let status = supervisor_health_status(report.status);
let detail = report.detail.clone();
let metrics = truncate_health_metrics(report.metrics);
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
state.health.status = status;
state.last_probe_evidence = None;
state.health.last_probe_ms = Some(now_ms);
state.health.detail = detail.clone();
state.health.metrics = metrics.clone();
state.health.consecutive_failures = 0;
state.failed_retry.observe_health(
report.status == HealthStatus::Ok,
runtime.restart_policy.window,
);
});
if delivery_failed {
return;
}
let action = match report.status {
HealthStatus::Ok => return,
HealthStatus::Degraded => runtime.health.on_degraded,
HealthStatus::Failing => runtime.health.on_failing,
};
apply_l3_health_action(
spec,
runtime,
registry,
process_liveness,
snapshot,
child,
status,
detail.as_deref(),
action,
now_ms,
)
.await;
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn handle_health_probe_failure(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
registry: &Registry,
process_liveness: &SupervisorProcessLiveness,
snapshot: &SharedSnapshot,
child: &mut Option<SupervisedChild>,
err: HealthProbeError,
now_ms: u64,
delivery_probe: Option<crate::config_gate::runtime::DeliveryProbe>,
) {
let delivery_failed = match (&runtime.config_delivery, delivery_probe) {
(Some(delivery), Some(probe)) => {
delivery
.health_result(
&spec.module_id,
runtime.forwarding.as_deref(),
registry,
probe,
false,
)
.await
}
_ => false,
};
let threshold = runtime.health.failure_threshold.max(1);
let held = if err.evidence == HealthProbeEvidence::NoAnswer {
runtime.host_health.record_late_module(&spec.module_id)
} else {
false
};
let mut failures = 0;
let detail = if held {
format!("[{}] {err}; host overloaded", err.label())
} else {
format!("[{}] {err}", err.label())
};
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
state.last_probe_evidence = Some(err.evidence);
state
.failed_retry
.observe_health(false, runtime.restart_policy.window);
if held {
state.health.status = SupervisorHealthStatus::Degraded;
} else if state.spawned_protocol.unwrap_or(spec.protocol) == ModuleProtocol::Subc {
state.health.status = SupervisorHealthStatus::Unknown;
}
state.health.last_probe_ms = Some(now_ms);
state.health.consecutive_failures = if held {
0
} else {
state.health.consecutive_failures.saturating_add(1)
};
state.health.detail = Some(detail.clone());
state.health.metrics = None;
failures = state.health.consecutive_failures;
});
if delivery_failed {
return;
}
if held || failures < threshold {
warn!(
module_id = %spec.module_id,
consecutive_failures = failures,
threshold,
evidence = err.label(),
detail = %detail,
"health.check probe failed"
);
return;
}
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
state.state = ModuleState::Unresponsive;
state.health.status = SupervisorHealthStatus::Unresponsive;
});
if runtime.health.critical {
error!(
module_id = %spec.module_id,
status = "unresponsive",
evidence = err.label(),
detail = %detail,
"critical module health alert"
);
} else {
warn!(
module_id = %spec.module_id,
status = "unresponsive",
evidence = err.label(),
detail = %detail,
"module health threshold breached"
);
}
if let Err(err) = health_restart_child(
spec,
runtime,
registry,
process_liveness,
snapshot,
child,
SupervisorHealthStatus::Unresponsive,
Some(&detail),
now_ms,
)
.await
{
error!(module_id = %spec.module_id, error = %err, "health-triggered restart failed");
}
}
#[allow(clippy::too_many_arguments)]
async fn apply_l3_health_action(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
registry: &Registry,
process_liveness: &SupervisorProcessLiveness,
snapshot: &SharedSnapshot,
child: &mut Option<SupervisedChild>,
status: SupervisorHealthStatus,
detail: Option<&str>,
action: HealthAction,
now_ms: u64,
) {
record_health_action(snapshot, &spec.module_id, action.to_string(), now_ms);
match action {
HealthAction::Report => {
info!(
module_id = %spec.module_id,
status = ?status,
detail,
"module reported non-ok health"
);
}
HealthAction::Alert => {
error!(
module_id = %spec.module_id,
status = ?status,
detail,
"module health alert"
);
}
HealthAction::Restart => {
if let Err(err) = health_restart_child(
spec,
runtime,
registry,
process_liveness,
snapshot,
child,
status,
detail,
now_ms,
)
.await
{
error!(module_id = %spec.module_id, error = %err, "health-triggered restart failed");
}
}
}
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn health_restart_child(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
registry: &Registry,
process_liveness: &SupervisorProcessLiveness,
snapshot: &SharedSnapshot,
child: &mut Option<SupervisedChild>,
status: SupervisorHealthStatus,
detail: Option<&str>,
now_ms: u64,
) -> Result<(), SuperviseError> {
budgeted_restart_child(
spec,
runtime,
registry,
process_liveness,
snapshot,
child,
status,
detail,
now_ms,
true,
)
.await
}
pub(super) fn record_health_action(
snapshot: &SharedSnapshot,
module_id: &str,
action: String,
now_ms: u64,
) {
let _ = update_snapshot(snapshot, Some(module_id), |state| {
state.health.last_action = Some(action);
state.health.last_action_ms = Some(now_ms);
});
}
fn supervisor_health_status(status: HealthStatus) -> SupervisorHealthStatus {
match status {
HealthStatus::Ok => SupervisorHealthStatus::Ok,
HealthStatus::Degraded => SupervisorHealthStatus::Degraded,
HealthStatus::Failing => SupervisorHealthStatus::Failing,
}
}
fn truncate_health_metrics(metrics: Option<Value>) -> Option<Value> {
let metrics = metrics?;
match serde_json::to_vec(&metrics) {
Ok(encoded) if encoded.len() > MAX_HEALTH_METRICS_BYTES => Some(serde_json::json!({
"truncated": true,
"original_bytes": encoded.len(),
})),
Ok(_) | Err(_) => Some(metrics),
}
}
pub(super) fn jittered_health_delay(
module_id: &str,
probe_index: u64,
cadence: Duration,
) -> Duration {
if cadence.is_zero() {
return Duration::ZERO;
}
let cadence_ms = cadence.as_millis() as u64;
if cadence_ms == 0 {
return cadence;
}
let jitter_span = (cadence_ms / 10).max(1);
let hash = module_id.as_bytes().iter().fold(
probe_index.wrapping_mul(0x9E37_79B9_7F4A_7C15),
|acc, byte| {
acc.wrapping_mul(1099511628211)
.wrapping_add(u64::from(*byte))
},
);
cadence + Duration::from_millis(hash % jitter_span)
}