use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use iceoryx2::prelude::*;
use crate::global::{Global, Locked};
use crate::types::{node_pid_to_u32, NodeId};
pub const LIVENESS_POLL_MS: u64 = 500;
const SLEEP_SLICE_MS: u64 = 100;
fn poll_interval_ms() -> u64 {
static INTERVAL: Global<u64> = Global::new();
*INTERVAL.get_or_init(|| {
std::env::var("ICE_RPC_LIVENESS_POLL_MS")
.ok()
.and_then(|v| v.parse().ok())
.filter(|v| *v > 0)
.unwrap_or(LIVENESS_POLL_MS)
})
}
fn sleep_slices() -> u64 {
poll_interval_ms().div_ceil(SLEEP_SLICE_MS)
}
static IS_PROVIDER: AtomicBool = AtomicBool::new(false);
pub fn mark_provider() {
IS_PROVIDER.store(true, Ordering::Relaxed);
}
pub fn is_provider() -> bool {
IS_PROVIDER.load(Ordering::Relaxed)
}
pub fn alive_pids() -> Option<HashSet<u32>> {
let config = crate::config::build_iceoryx2_config();
let mut pids = HashSet::new();
let result = Node::<ipc_threadsafe::Service>::list(&config, |state| {
if matches!(state, NodeState::Alive(_)) {
pids.insert(node_pid_to_u32(
state.node_id().pid::<ipc_threadsafe::Service>(),
));
}
CallbackProgression::Continue
});
match result {
Ok(()) => Some(pids),
Err(e) => {
log::warn!("[node_liveness] Node::list failed: {:?}", e);
None
}
}
}
pub fn is_pid_alive(pid: u32) -> bool {
alive_pids().map(|pids| pids.contains(&pid)).unwrap_or(true)
}
fn watched_nodes() -> &'static Locked<HashSet<u32>> {
static WATCHED: Locked<HashSet<u32>> = Locked::new();
&WATCHED
}
fn poller_started() -> &'static Global<()> {
static STARTED: Global<()> = Global::new();
&STARTED
}
pub fn register_node_liveness_watcher(node_id: NodeId) {
if register_watcher(node_id) {
ensure_poller();
}
}
fn register_watcher(node_id: NodeId) -> bool {
if node_id == NodeId::current() {
return false;
}
watched_nodes().with(|watched| watched.insert(node_id.0));
true
}
pub fn unregister_node_liveness_watcher(node_id: NodeId) {
watched_nodes().with(|watched| watched.remove(&node_id.0));
}
#[cfg(test)]
pub fn is_watched(node_id: NodeId) -> bool {
watched_nodes().with(|watched| watched.contains(&node_id.0))
}
fn ensure_poller() {
poller_started().get_or_init(|| {
let handle = crate::rt::spawn_blocking(poller_loop);
crate::locator::ServiceLocator::global().register_shutdown_handle(handle);
});
}
fn poller_loop() {
let cancel = crate::global_cancel_token().clone();
loop {
if cancel.is_cancelled() {
break;
}
let watched: Vec<u32> = watched_nodes().with(|nodes| nodes.iter().copied().collect());
if !watched.is_empty() {
if let Some(alive) = alive_pids() {
for pid in watched {
if alive.contains(&pid) {
continue;
}
if !is_confirmed_dead(pid) {
continue;
}
log::warn!("[node_liveness] CRASH DETECTED for Node {}", pid);
watched_nodes().with(|nodes| nodes.remove(&pid));
}
}
}
for _ in 0..sleep_slices() {
if cancel.is_cancelled() {
return;
}
std::thread::sleep(Duration::from_millis(SLEEP_SLICE_MS));
}
}
}
fn is_confirmed_dead(pid: u32) -> bool {
let config = crate::config::build_iceoryx2_config();
let mut found = false;
let mut crashed = false;
let result = Node::<ipc_threadsafe::Service>::list(&config, |state| {
if node_pid_to_u32(state.node_id().pid::<ipc_threadsafe::Service>()) != pid {
return CallbackProgression::Continue;
}
found = true;
crashed = matches!(state, NodeState::Dead(_));
CallbackProgression::Stop
});
if result.is_err() {
return false;
}
crashed || !found
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn provider_marker_roundtrip() {
IS_PROVIDER.store(false, Ordering::Relaxed);
assert!(!is_provider());
mark_provider();
assert!(is_provider());
IS_PROVIDER.store(false, Ordering::Relaxed);
assert!(!is_provider());
}
#[test]
fn register_and_unregister_are_tracked() {
let fake = NodeId(0x0D1E_0001);
assert_ne!(fake, NodeId::current());
unregister_node_liveness_watcher(fake);
assert!(!is_watched(fake));
assert!(register_watcher(fake));
assert!(is_watched(fake));
unregister_node_liveness_watcher(fake);
assert!(!is_watched(fake));
}
#[test]
fn a_process_never_watches_itself() {
assert!(!register_watcher(NodeId::current()));
register_node_liveness_watcher(NodeId::current());
assert!(!is_watched(NodeId::current()));
}
#[test]
fn poll_interval_is_stable_and_positive() {
let first = poll_interval_ms();
assert!(first > 0, "a zero interval would busy-loop");
assert_eq!(first, poll_interval_ms());
}
#[test]
fn sleep_slices_covers_at_least_the_interval() {
let slices = sleep_slices();
assert!(slices >= 1, "the loop must always sleep at least one slice");
assert!(
slices * SLEEP_SLICE_MS >= poll_interval_ms(),
"{slices} slice(s) of {SLEEP_SLICE_MS} ms do not cover {} ms",
poll_interval_ms()
);
}
}