Crate phi_accrual_detector

Crate phi_accrual_detector 

Source
Expand description

This is a pluggable implementation of the Phi Accrual Failure Detector.

The simplest implementation to use it in your system has been shown in the examples/monitor.rs

use phi_accrual_detector::{Detector};
use async_trait::async_trait;
use std::sync::{Arc};
use chrono::{DateTime, Local};

struct Monitor {
   detector: Arc<Detector>,
}

#[async_trait]
trait MonitorInteraction {
   // For inserting heartbeat arrival time
   async fn ping(&self);
   // For calculating suspicion level
   async fn suspicion(&self) -> f64;
}

#[async_trait]
impl MonitorInteraction for Monitor {
    async fn ping(&self) {
        let current_time = Local::now();
        self.detector.insert(current_time).await.expect("Some panic occurred");
   }

   async fn suspicion(&self) -> f64 {
       let current_time = Local::now();
       let last_arrived_at = self.detector.last_arrived_at().await.expect("Some panic occurred");
       // you can determine an acceptable threshold (ex:0.5) for phi after which you can take action.
       let phi = self.detector.phi(current_time).await.unwrap();
       phi
   }
}

fn main() {
  let detector = Arc::new(Detector::new(1000));
  let monitor = Monitor { detector: Arc::clone(&detector) };
}

The above example gives you an implementation of a Monitor struct which can be used to interact with the Detector struct. However, if you want to give your process some leeway to recover from a failure or account in the network latencies, you can set an acceptable pause duration during which the detector will not raise suspicion. You can tweak the detector in the above example like this:

use phi_accrual_detector::{Detector};
use async_trait::async_trait;
use std::sync::{Arc};
use chrono::{TimeDelta};

struct Monitor {
   detector: Arc<Detector>,
}

// implementation and traits remain the same.

fn main() {
  let detector = Arc::new(Detector::with_acceptable_pause(1000, TimeDelta::milliseconds(1000)));
  let monitor = Monitor { detector: Arc::clone(&detector) };
}

Structs§

Detector
Detector meant for abstraction over Statistics
Statistics
Statistics of last window_length intervals

Traits§

PhiInteraction
PhiInteraction trait for Detector