use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Debug)]
pub(crate) struct LatencyProbe {
sample_ts_bits: AtomicU64,
arrival_local_bits: AtomicU64,
}
impl Default for LatencyProbe {
fn default() -> Self {
Self {
sample_ts_bits: AtomicU64::new(f64::NAN.to_bits()),
arrival_local_bits: AtomicU64::new(f64::NAN.to_bits()),
}
}
}
impl LatencyProbe {
pub(crate) fn record(&self, sample_ts: f64) {
if !sample_ts.is_finite() {
return;
}
let arrival = crate::clock();
self.sample_ts_bits
.store(sample_ts.to_bits(), Ordering::Relaxed);
self.arrival_local_bits
.store(arrival.to_bits(), Ordering::Relaxed);
}
pub(crate) fn measure(&self, clock_offset: Option<f64>) -> Option<f64> {
let offset = clock_offset?;
let sample_ts = f64::from_bits(self.sample_ts_bits.load(Ordering::Relaxed));
let arrival = f64::from_bits(self.arrival_local_bits.load(Ordering::Relaxed));
if sample_ts.is_finite() && arrival.is_finite() {
Some((arrival - (sample_ts + offset)).max(0.0))
} else {
None
}
}
}
pub(crate) const MAX_GAPS: usize = 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GapKind {
Lost,
Dropped,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Gap {
pub start: f64,
pub end: f64,
pub count: u64,
pub kind: GapKind,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InletHealth {
pub latency: Option<f64>,
pub lost: Option<u64>,
pub dropped: u64,
pub resent: Option<u64>,
pub clock_uncertainty: Option<f64>,
pub clock_drift: Option<f64>,
pub recoveries: u64,
pub connected: bool,
pub on_udp: bool,
pub on_shm: bool,
pub compressed: bool,
pub encrypted: bool,
pub compression_ratio: Option<f64>,
pub buffered: usize,
pub buffer_capacity: usize,
pub gap_count: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumerHealth {
pub id: u64,
pub peer: String,
pub fill: usize,
pub capacity: usize,
pub dropped: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutletHealth {
pub consumers: Vec<ConsumerHealth>,
pub history_len: usize,
pub pushed: u64,
}
#[derive(Debug, Default)]
pub(crate) struct GapTracker {
last_ts: Option<f64>,
pending_count: u64,
pending_start: Option<f64>,
gaps: VecDeque<Gap>,
}
impl GapTracker {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn record_lost(&mut self, delta: u64) {
if delta == 0 {
return;
}
if self.pending_count == 0 {
self.pending_start = self.last_ts;
}
self.pending_count = self.pending_count.saturating_add(delta);
}
pub(crate) fn on_sample(&mut self, ts: f64) {
if self.pending_count > 0 {
let count = self.pending_count;
let start = self.pending_start.unwrap_or(ts);
self.pending_count = 0;
self.pending_start = None;
if start.is_finite() && ts.is_finite() {
if self.gaps.len() >= MAX_GAPS {
self.gaps.pop_front();
}
self.gaps.push_back(Gap {
start,
end: ts,
count,
kind: GapKind::Lost,
});
}
}
self.last_ts = Some(ts);
}
pub(crate) fn on_batch(&mut self, first_ts: f64, last_ts: f64) {
self.on_sample(first_ts);
self.last_ts = Some(last_ts);
}
pub(crate) fn reset_cursor(&mut self) {
self.last_ts = None;
self.pending_count = 0;
self.pending_start = None;
}
pub(crate) fn len(&self) -> usize {
self.gaps.len()
}
pub(crate) fn snapshot(&self, shift: f64) -> Vec<Gap> {
self.gaps
.iter()
.map(|g| Gap {
start: g.start + shift,
end: g.end + shift,
count: g.count,
kind: g.kind,
})
.collect()
}
pub(crate) fn take(&mut self, shift: f64) -> Vec<Gap> {
self.gaps
.drain(..)
.map(|g| Gap {
start: g.start + shift,
end: g.end + shift,
count: g.count,
kind: g.kind,
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tracker_records_exact_timestamp_pair_and_coalesces_consecutive_losses() {
let mut t = GapTracker::new();
t.on_sample(10.0);
t.on_sample(10.5);
t.record_lost(3);
t.record_lost(2);
t.on_sample(13.0);
let gaps = t.snapshot(0.0);
assert_eq!(gaps.len(), 1);
assert_eq!(
gaps[0],
Gap {
start: 10.5,
end: 13.0,
count: 5,
kind: GapKind::Lost,
}
);
let shifted = t.take(100.0);
assert_eq!(shifted[0].start, 110.5);
assert_eq!(shifted[0].end, 113.0);
assert!(t.snapshot(0.0).is_empty());
}
#[test]
fn tracker_bounds_ring_size_and_resets_cursor_on_new_run() {
let mut t = GapTracker::new();
t.on_sample(0.0);
for i in 1..=(MAX_GAPS + 20) {
t.record_lost(1);
t.on_sample(i as f64);
}
assert_eq!(t.len(), MAX_GAPS);
let first = t.snapshot(0.0)[0];
assert_eq!(first.end, 21.0);
t.record_lost(10);
t.reset_cursor();
t.on_sample(5000.0);
assert_eq!(t.len(), MAX_GAPS);
}
}