use super::*;
use rand::Rng;
use std::time::Instant;
struct Inner {
watch_id: NodeAddress,
detector: phi_detector::PingWindow,
last_ping: Instant,
}
impl Inner {
fn watch(id: NodeAddress) -> Self {
Self {
watch_id: id,
detector: phi_detector::PingWindow::new(Duration::from_secs(1)),
last_ping: Instant::now(),
}
}
}
pub struct FailureDetector {
inner: spin::RwLock<Inner>,
}
impl FailureDetector {
pub fn new() -> Self {
let null_node_id = NodeAddress(Uri::from_static("http://null-node"));
let inner = Inner::watch(null_node_id);
Self {
inner: inner.into(),
}
}
pub fn receive_heartbeat(&self, leader_id: NodeAddress) {
let mut inner = self.inner.write();
let cur_watch_id = inner.watch_id.clone();
if cur_watch_id != leader_id {
*inner = Inner::watch(leader_id);
}
let now = Instant::now();
let du = now - inner.last_ping;
inner.detector.add_ping(du);
inner.last_ping = now;
}
pub fn get_election_timeout(&self) -> Option<Duration> {
let inner = self.inner.read();
let fd = &inner.detector;
let normal_dist = fd.normal_dist();
let detected = {
let du = Instant::now() - inner.last_ping;
let phi = normal_dist.phi(du);
let threshold = 12.;
phi > threshold
};
if !detected {
return None;
}
let mut rng = rand::thread_rng();
let mu = normal_dist.mu().as_millis() as u64;
let random_timeout = Duration::from_millis(rng.gen_range(0..=(3 * mu)));
Some(random_timeout)
}
}