use crate::health::{ConsumerHealth, Gap, GapKind, OutletHealth, MAX_GAPS};
use crate::history::{History, HistoryLimits, HistoryRead};
use labstream_proto::meta::MetadataUpdate;
use labstream_wire::{PackedSample, Sample, TimestampDeducer, IRREGULAR_RATE};
use std::collections::{BTreeMap, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};
const MAX_META_LOG: usize = 4096;
#[derive(Debug)]
struct Ring {
q: VecDeque<PackedSample>,
back: u64,
deducer: Option<TimestampDeducer>,
last_popped_ts: Option<f64>,
pending_drop_count: u64,
pending_drop_start: Option<f64>,
drop_gaps: VecDeque<Gap>,
skips: VecDeque<(u64, u64)>,
}
impl std::ops::Deref for Ring {
type Target = VecDeque<PackedSample>;
fn deref(&self) -> &VecDeque<PackedSample> {
&self.q
}
}
impl std::ops::DerefMut for Ring {
fn deref_mut(&mut self) -> &mut VecDeque<PackedSample> {
&mut self.q
}
}
impl Ring {
fn front_number(&self) -> u64 {
self.back.wrapping_sub(self.q.len() as u64)
}
fn note_drop(&mut self, dropped_ts: f64, count: u64) {
if count == 0 {
return;
}
if self.pending_drop_count == 0 {
self.pending_drop_start = self.last_popped_ts.or(Some(dropped_ts));
}
self.pending_drop_count = self.pending_drop_count.saturating_add(count);
}
fn note_pop(&mut self, first_ts: f64, last_ts: f64) {
if self.pending_drop_count > 0 {
let count = self.pending_drop_count;
let start = self.pending_drop_start.unwrap_or(first_ts);
self.pending_drop_count = 0;
self.pending_drop_start = None;
if start.is_finite() && first_ts.is_finite() {
if self.drop_gaps.len() >= MAX_GAPS {
self.drop_gaps.pop_front();
}
self.drop_gaps.push_back(Gap {
start,
end: first_ts,
count,
kind: GapKind::Dropped,
});
}
}
self.last_popped_ts = Some(last_ts);
}
fn push_skip(&mut self, pos: u64, count: u64) {
if count == 0 {
return;
}
if let Some((last_pos, last_count)) = self.skips.back_mut() {
if *last_pos == pos {
*last_count = last_count.saturating_add(count);
return;
}
if *last_pos < pos {
self.skips.push_back((pos, count));
return;
}
} else {
self.skips.push_back((pos, count));
return;
}
if let Some((first_pos, first_count)) = self.skips.front_mut() {
if *first_pos == pos {
*first_count = first_count.saturating_add(count);
return;
}
if pos < *first_pos {
self.skips.push_front((pos, count));
return;
}
}
for i in 0..self.skips.len() {
let (p, c) = &mut self.skips[i];
if *p == pos {
*c = c.saturating_add(count);
return;
}
if pos < *p {
self.skips.insert(i, (pos, count));
return;
}
}
self.skips.push_back((pos, count));
}
fn take_skips_at(&mut self, ring_pos: u64) -> u64 {
if self.skips.is_empty() {
return 0;
}
let mut total = 0u64;
while let Some(&(pos, count)) = self.skips.front() {
if pos <= ring_pos {
total = total.saturating_add(count);
self.skips.pop_front();
} else {
break;
}
}
total
}
fn drop_front(&mut self) {
let front_pos = self.front_number();
if let Some(old) = self.q.pop_front() {
self.note_drop(old.timestamp, 1);
self.push_skip(front_pos.wrapping_add(1), 1);
if let Some(d) = self.deducer.as_mut() {
d.apply(old.timestamp);
}
}
}
}
#[derive(Debug)]
pub struct SampleQueue {
inner: Mutex<Ring>,
room: Condvar,
capacity: usize,
dropped: AtomicU64,
closed: Mutex<bool>,
woken: std::sync::atomic::AtomicBool,
}
impl SampleQueue {
pub fn new(capacity: usize) -> Self {
Self::with_first_number(capacity, 0)
}
pub fn with_first_number(capacity: usize, first: u64) -> Self {
Self::build(capacity, first, None)
}
pub fn with_deducer(capacity: usize, first: u64, deducer: TimestampDeducer) -> Self {
Self::build(capacity, first, Some(deducer))
}
fn build(capacity: usize, first: u64, deducer: Option<TimestampDeducer>) -> Self {
let capacity = capacity.max(1);
SampleQueue {
inner: Mutex::new(Ring {
q: VecDeque::with_capacity(capacity.min(4096)),
back: first,
deducer,
last_popped_ts: None,
pending_drop_count: 0,
pending_drop_start: None,
drop_gaps: VecDeque::new(),
skips: VecDeque::new(),
}),
room: Condvar::new(),
capacity,
dropped: AtomicU64::new(0),
closed: Mutex::new(false),
woken: std::sync::atomic::AtomicBool::new(false),
}
}
pub fn capacity(&self) -> usize {
self.capacity
}
pub fn dropped(&self) -> u64 {
self.dropped.load(Ordering::SeqCst)
}
pub fn len(&self) -> usize {
self.inner.lock().unwrap().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn push(&self, s: Sample) {
self.push_packed(PackedSample::from(s));
}
pub fn push_packed(&self, s: PackedSample) {
let mut q = self.inner.lock().unwrap();
if q.len() >= self.capacity {
q.drop_front();
self.dropped.fetch_add(1, Ordering::SeqCst);
}
q.push_back(s);
q.back = q.back.wrapping_add(1);
drop(q);
self.room.notify_one();
}
pub fn push_many(&self, samples: &[Sample]) {
if samples.is_empty() {
return;
}
let packed: Vec<PackedSample> = samples.iter().map(PackedSample::from_sample).collect();
self.push_many_packed(&packed);
}
pub fn push_many_packed(&self, samples: &[PackedSample]) {
if samples.is_empty() {
return;
}
let mut q = self.inner.lock().unwrap();
let mut lost = 0u64;
for s in samples {
if q.len() >= self.capacity {
q.drop_front();
lost += 1;
}
q.push_back(s.clone());
}
q.back = q.back.wrapping_add(samples.len() as u64);
drop(q);
if lost > 0 {
self.dropped.fetch_add(lost, Ordering::SeqCst);
}
if samples.len() == 1 {
self.room.notify_one();
} else {
self.room.notify_all();
}
}
pub fn push_owned(&self, samples: &mut Vec<Sample>) {
self.push_owned_with_skips(samples, &[]);
}
pub fn push_owned_packed(&self, samples: &mut Vec<PackedSample>) {
self.push_owned_packed_with_skips(samples, &[]);
}
pub(crate) fn push_owned_with_skips(&self, samples: &mut Vec<Sample>, skips: &[(usize, u64)]) {
let mut packed: Vec<PackedSample> = samples.drain(..).map(PackedSample::from).collect();
self.push_owned_packed_with_skips(&mut packed, skips);
}
pub(crate) fn push_owned_packed_with_skips(
&self,
samples: &mut Vec<PackedSample>,
skips: &[(usize, u64)],
) {
if samples.is_empty() {
if !skips.is_empty() {
let mut q = self.inner.lock().unwrap();
let pos = q.back;
for &(_, count) in skips {
q.push_skip(pos, count);
}
}
return;
}
let batch_len = samples.len();
let mut q = self.inner.lock().unwrap();
debug_assert!(q.deducer.is_none());
let base_pos = q.back;
for &(offset, count) in skips {
q.push_skip(base_pos.wrapping_add(offset as u64), count);
}
let mut lost = 0u64;
let skip = batch_len.saturating_sub(self.capacity);
q.back = q.back.wrapping_add(batch_len as u64);
if skip > 0 {
let old_len = q.len() as u64;
if let Some(old) = q.q.front() {
let ts = old.timestamp;
q.note_drop(ts, old_len);
lost += old_len;
q.q.clear();
}
let skipped_ts = samples[0].timestamp;
q.note_drop(skipped_ts, skip as u64);
lost += skip as u64;
let first_kept_pos = base_pos.wrapping_add(skip as u64);
q.push_skip(first_kept_pos, lost);
for s in samples.drain(..).skip(skip) {
q.push_back(s);
}
} else {
for s in samples.drain(..) {
if q.len() >= self.capacity {
q.drop_front();
lost += 1;
}
q.push_back(s);
}
}
drop(q);
if lost > 0 {
self.dropped.fetch_add(lost, Ordering::SeqCst);
}
if batch_len == 1 {
self.room.notify_one();
} else {
self.room.notify_all();
}
}
pub(crate) fn record_skipped(&self, count: u64) {
if count == 0 {
return;
}
let mut q = self.inner.lock().unwrap();
let pos = q.back;
q.push_skip(pos, count);
}
pub(crate) fn clear_skips(&self) {
self.inner.lock().unwrap().skips.clear();
}
pub fn pop(&self, timeout: Duration) -> Option<Sample> {
self.pop_packed_with_skip(timeout)
.map(|(s, _)| s.to_sample())
}
pub fn pop_packed(&self, timeout: Duration) -> Option<PackedSample> {
self.pop_packed_with_skip(timeout).map(|(s, _)| s)
}
#[cfg(test)]
pub(crate) fn pop_with_skip(&self, timeout: Duration) -> Option<(Sample, u64)> {
self.pop_packed_with_skip(timeout)
.map(|(s, sk)| (s.to_sample(), sk))
}
pub(crate) fn pop_packed_with_skip(&self, timeout: Duration) -> Option<(PackedSample, u64)> {
let mut end: Option<Instant> = None;
let mut q = self.inner.lock().unwrap();
loop {
if !q.is_empty() {
let pos = q.front_number();
let skipped = q.take_skips_at(pos);
let s = q.pop_front().unwrap();
q.note_pop(s.timestamp, s.timestamp);
return Some((s, skipped));
}
if timeout.is_zero() || *self.closed.lock().unwrap() {
return None;
}
let deadline = *end.get_or_insert_with(|| Instant::now() + timeout);
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
return None;
}
let (guard, result) = self
.room
.wait_timeout(q, left.min(Duration::from_millis(100)))
.unwrap();
q = guard;
if result.timed_out() && Instant::now() >= deadline {
if !q.is_empty() {
let pos = q.front_number();
let skipped = q.take_skips_at(pos);
let s = q.pop_front().unwrap();
q.note_pop(s.timestamp, s.timestamp);
return Some((s, skipped));
}
return None;
}
}
}
pub fn pop_many(&self, out: &mut Vec<Sample>, max: usize, timeout: Duration) -> usize {
self.pop_many_with_skips(out, None, max, timeout)
}
pub fn pop_many_packed(
&self,
out: &mut Vec<PackedSample>,
max: usize,
timeout: Duration,
) -> usize {
self.pop_many_packed_with_skips(out, None, max, timeout)
}
pub(crate) fn pop_many_with_skips(
&self,
out: &mut Vec<Sample>,
skips_out: Option<&mut Vec<(usize, u64)>>,
max: usize,
timeout: Duration,
) -> usize {
let mut packed = Vec::new();
let got = self.pop_many_packed_with_skips(&mut packed, skips_out, max, timeout);
out.extend(packed.iter().map(PackedSample::to_sample));
got
}
pub(crate) fn pop_many_packed_with_skips(
&self,
out: &mut Vec<PackedSample>,
mut skips_out: Option<&mut Vec<(usize, u64)>>,
max: usize,
timeout: Duration,
) -> usize {
if max == 0 {
return 0;
}
let mut end: Option<Instant> = None;
let mut q = self.inner.lock().unwrap();
let drain_into = |q: &mut Ring,
out: &mut Vec<PackedSample>,
skips_out: &mut Option<&mut Vec<(usize, u64)>>,
max: usize|
-> usize {
let take = q.len().min(max);
if take == 0 {
return 0;
}
let first_pos = q.front_number();
if !q.skips.is_empty() {
if let Some(dst) = skips_out.as_mut() {
let lead = q.take_skips_at(first_pos);
if lead > 0 {
dst.push((0, lead));
}
let end_pos = first_pos.wrapping_add(take as u64);
while let Some(&(pos, count)) = q.skips.front() {
if pos < end_pos {
let idx = (pos.wrapping_sub(first_pos)) as usize;
dst.push((idx, count));
q.skips.pop_front();
} else {
break;
}
}
} else {
let end_pos = first_pos.wrapping_add(take as u64);
while let Some(&(pos, _)) = q.skips.front() {
if pos < end_pos {
q.skips.pop_front();
} else {
break;
}
}
}
}
let first_ts = q.q[0].timestamp;
let last_ts = q.q[take - 1].timestamp;
out.extend(q.drain(..take));
q.note_pop(first_ts, last_ts);
take
};
loop {
if !q.is_empty() {
return drain_into(&mut q, out, &mut skips_out, max);
}
if timeout.is_zero() || *self.closed.lock().unwrap() {
return 0;
}
let deadline = *end.get_or_insert_with(|| Instant::now() + timeout);
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
return 0;
}
let (guard, result) = self
.room
.wait_timeout(q, left.min(Duration::from_millis(100)))
.unwrap();
q = guard;
if result.timed_out() && Instant::now() >= deadline {
return drain_into(&mut q, out, &mut skips_out, max);
}
}
}
pub fn pop_many_numbered(
&self,
out: &mut Vec<Sample>,
max: usize,
timeout: Duration,
) -> (usize, u64) {
let mut packed = Vec::new();
let res = self.pop_many_packed_inner(&mut packed, None, max, timeout);
out.extend(packed.iter().map(PackedSample::to_sample));
res
}
pub fn pop_many_numbered_packed(
&self,
out: &mut Vec<PackedSample>,
max: usize,
timeout: Duration,
) -> (usize, u64) {
self.pop_many_packed_inner(out, None, max, timeout)
}
pub fn pop_many_full(
&self,
out: &mut Vec<Sample>,
full: &mut Vec<f64>,
max: usize,
timeout: Duration,
) -> (usize, u64) {
let mut packed = Vec::new();
let res = self.pop_many_packed_inner(&mut packed, Some(full), max, timeout);
out.extend(packed.iter().map(PackedSample::to_sample));
res
}
pub fn pop_many_full_packed(
&self,
out: &mut Vec<PackedSample>,
full: &mut Vec<f64>,
max: usize,
timeout: Duration,
) -> (usize, u64) {
self.pop_many_packed_inner(out, Some(full), max, timeout)
}
fn pop_many_packed_inner(
&self,
out: &mut Vec<PackedSample>,
mut full: Option<&mut Vec<f64>>,
max: usize,
timeout: Duration,
) -> (usize, u64) {
let mut end: Option<Instant> = None;
let mut q = self.inner.lock().unwrap();
loop {
if max == 0
|| !q.is_empty()
|| timeout.is_zero()
|| *self.closed.lock().unwrap()
|| self.woken.swap(false, Ordering::SeqCst)
{
let first = q.front_number();
let take = q.len().min(max);
let ring = &mut *q;
for s in ring.q.drain(..take) {
let t = match ring.deducer.as_mut() {
Some(d) => d.apply(s.timestamp),
None => s.timestamp,
};
if let Some(f) = full.as_mut() {
f.push(t);
}
out.push(s);
}
return (take, first);
}
let deadline = *end.get_or_insert_with(|| Instant::now() + timeout);
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
return (0, q.front_number());
}
let (guard, _) = self
.room
.wait_timeout(q, left.min(Duration::from_millis(100)))
.unwrap();
q = guard;
}
}
pub fn wake(&self) {
let _q = self.inner.lock().unwrap();
self.woken.store(true, Ordering::SeqCst);
self.room.notify_all();
}
pub fn close(&self) {
*self.closed.lock().unwrap() = true;
self.room.notify_all();
}
pub fn is_closed(&self) -> bool {
*self.closed.lock().unwrap()
}
pub(crate) fn drop_gap_count(&self) -> usize {
let q = self.inner.lock().unwrap();
let pending = usize::from(q.pending_drop_count > 0 && q.q.front().is_some());
q.drop_gaps.len() + pending
}
pub(crate) fn drop_gaps(&self, shift: f64) -> Vec<Gap> {
let q = self.inner.lock().unwrap();
let mut out: Vec<Gap> = q
.drop_gaps
.iter()
.map(|g| Gap {
start: g.start + shift,
end: g.end + shift,
count: g.count,
kind: g.kind,
})
.collect();
if q.pending_drop_count > 0 {
if let Some(front) = q.q.front() {
let start = q.pending_drop_start.unwrap_or(front.timestamp);
if start.is_finite() && front.timestamp.is_finite() {
out.push(Gap {
start: start + shift,
end: front.timestamp + shift,
count: q.pending_drop_count,
kind: GapKind::Dropped,
});
}
}
}
out
}
pub(crate) fn take_drop_gaps(&self, shift: f64) -> Vec<Gap> {
let mut q = self.inner.lock().unwrap();
q.drop_gaps
.drain(..)
.map(|g| Gap {
start: g.start + shift,
end: g.end + shift,
count: g.count,
kind: g.kind,
})
.collect()
}
}
#[derive(Debug)]
struct ConsumerEntry {
id: u64,
peer: String,
queue: Arc<SampleQueue>,
}
#[derive(Debug)]
struct ShmConsumerEntry {
id: u64,
peer: String,
writer: Arc<crate::shm::ShmWriter>,
channels: Option<Vec<u16>>,
}
#[derive(Debug, Default)]
pub struct Fanout {
consumers: Mutex<Consumers>,
meta_gen: AtomicU64,
meta: Mutex<MetadataLog>,
}
#[derive(Debug, Default)]
struct MetadataLog {
next_id: u64,
entries: VecDeque<(u64, MetadataUpdate)>,
latest: BTreeMap<String, MetadataUpdate>,
}
#[derive(Debug)]
struct Consumers {
list: Vec<ConsumerEntry>,
shm_list: Vec<ShmConsumerEntry>,
ts_scratch: Vec<f64>,
next_consumer_id: u64,
next: u64,
deducer: TimestampDeducer,
srate: f64,
limits: HistoryLimits,
history: Option<History>,
}
impl Default for Consumers {
fn default() -> Self {
Consumers {
list: Vec::new(),
shm_list: Vec::new(),
ts_scratch: Vec::new(),
next_consumer_id: 1,
next: 0,
deducer: TimestampDeducer::new(IRREGULAR_RATE),
srate: IRREGULAR_RATE,
limits: HistoryLimits::DEFAULT,
history: None,
}
}
}
impl Consumers {
fn record_packed_with_full(&mut self, s: &PackedSample, full: f64) {
if let Some(h) = self.history.as_mut() {
debug_assert_eq!(h.next(), self.next);
h.push_packed(s.with_timestamp(full));
}
self.next = self.next.wrapping_add(1);
}
}
impl Fanout {
pub fn new() -> Self {
Fanout::default()
}
pub fn with_rate(srate: f64, limits: HistoryLimits) -> Self {
Fanout {
consumers: Mutex::new(Consumers {
deducer: TimestampDeducer::new(srate),
srate,
limits,
..Consumers::default()
}),
meta_gen: AtomicU64::new(0),
meta: Mutex::new(MetadataLog::default()),
}
}
pub fn push_metadata(&self, key: String, value: String, timestamp: f64) -> MetadataUpdate {
let (seq, queues): (u64, Vec<Arc<SampleQueue>>) = {
let set = self.consumers.lock().unwrap();
let qs = set.list.iter().map(|c| Arc::clone(&c.queue)).collect();
(set.next, qs)
};
let update = MetadataUpdate {
seq,
timestamp,
key: key.clone(),
value,
};
{
let mut log = self.meta.lock().unwrap();
log.next_id = log.next_id.wrapping_add(1);
let id = log.next_id;
if log.entries.len() >= MAX_META_LOG {
log.entries.pop_front();
}
log.entries.push_back((id, update.clone()));
log.latest.insert(key, update.clone());
self.meta_gen.store(id, Ordering::Release);
}
for q in queues {
q.wake();
}
update
}
pub fn metadata_generation(&self) -> u64 {
self.meta_gen.load(Ordering::Acquire)
}
pub fn metadata_snapshot(&self) -> (u64, BTreeMap<String, MetadataUpdate>) {
if self.meta_gen.load(Ordering::Acquire) == 0 {
return (0, BTreeMap::new());
}
let log = self.meta.lock().unwrap();
(log.next_id, log.latest.clone())
}
pub fn read_metadata_since(&self, cursor: &mut u64, out: &mut Vec<MetadataUpdate>) {
let gen = self.meta_gen.load(Ordering::Acquire);
if gen == *cursor {
return;
}
let log = self.meta.lock().unwrap();
for (id, upd) in &log.entries {
if *id > *cursor {
out.push(upd.clone());
}
}
*cursor = log.next_id;
}
pub fn start_history(&self) -> bool {
let mut set = self.consumers.lock().unwrap();
if set.history.is_none() && set.limits.allow_history() {
let h = History::new(set.limits.clone(), set.srate, set.next);
set.history = Some(h);
}
set.history.is_some()
}
pub fn history_len(&self) -> usize {
let set = self.consumers.lock().unwrap();
set.history.as_ref().map_or(0, History::len)
}
pub fn history_ram_bytes(&self) -> usize {
let set = self.consumers.lock().unwrap();
set.history.as_ref().map_or(0, History::bytes)
}
pub fn history_disk_bytes(&self) -> u64 {
let set = self.consumers.lock().unwrap();
set.history.as_ref().map_or(0, History::disk_bytes)
}
pub fn history_spool_dir(&self) -> Option<std::path::PathBuf> {
let set = self.consumers.lock().unwrap();
set.history.as_ref().and_then(History::spool_dir)
}
pub fn read_history(
&self,
from: u64,
until: u64,
max: usize,
out: &mut Vec<Sample>,
) -> Option<(u64, usize)> {
let mut packed = Vec::new();
let res = self.read_history_packed(from, until, max, &mut packed)?;
out.extend(packed.iter().map(PackedSample::to_sample));
Some(res)
}
pub fn read_history_packed(
&self,
from: u64,
until: u64,
max: usize,
out: &mut Vec<PackedSample>,
) -> Option<(u64, usize)> {
let plan = {
let set = self.consumers.lock().unwrap();
set.history
.as_ref()?
.plan_read_packed(from, until, max, out)
};
match plan {
HistoryRead::Empty => None,
HistoryRead::Ready { first, count } => Some((first, count)),
HistoryRead::Disk(disk_plan) => disk_plan.execute(out),
}
}
pub fn add(&self, capacity: usize) -> Arc<SampleQueue> {
self.add_with_peer(capacity, String::new())
}
pub(crate) fn add_with_peer(&self, capacity: usize, peer: String) -> Arc<SampleQueue> {
let mut set = self.consumers.lock().unwrap();
let id = set.next_consumer_id;
set.next_consumer_id = id.wrapping_add(1);
let q = Arc::new(SampleQueue::with_first_number(capacity, set.next));
set.list.push(ConsumerEntry {
id,
peer,
queue: Arc::clone(&q),
});
q
}
pub fn add_resumable(&self, capacity: usize) -> (Arc<SampleQueue>, u64) {
self.add_resumable_with_peer(capacity, String::new())
}
pub(crate) fn add_resumable_with_peer(
&self,
capacity: usize,
peer: String,
) -> (Arc<SampleQueue>, u64) {
let mut set = self.consumers.lock().unwrap();
let id = set.next_consumer_id;
set.next_consumer_id = id.wrapping_add(1);
let n = set.next;
let q = Arc::new(SampleQueue::with_deducer(capacity, n, set.deducer.clone()));
set.list.push(ConsumerEntry {
id,
peer,
queue: Arc::clone(&q),
});
(q, n)
}
pub fn next_number(&self) -> u64 {
self.consumers.lock().unwrap().next
}
pub(crate) fn add_shm_with_peer(
&self,
writer: Arc<crate::shm::ShmWriter>,
peer: String,
channels: Option<Vec<u16>>,
) -> u64 {
let mut set = self.consumers.lock().unwrap();
let id = set.next_consumer_id;
set.next_consumer_id = id.wrapping_add(1);
let n = set.next;
set.shm_list.push(ShmConsumerEntry {
id,
peer,
writer,
channels,
});
n
}
pub(crate) fn remove_shm(&self, writer: &Arc<crate::shm::ShmWriter>) {
self.consumers
.lock()
.unwrap()
.shm_list
.retain(|c| !Arc::ptr_eq(&c.writer, writer));
}
pub fn remove(&self, q: &Arc<SampleQueue>) {
self.consumers
.lock()
.unwrap()
.list
.retain(|c| !Arc::ptr_eq(&c.queue, q));
}
pub fn count(&self) -> usize {
let set = self.consumers.lock().unwrap();
set.list.len() + set.shm_list.len()
}
pub fn health(&self) -> OutletHealth {
let mut set = self.consumers.lock().unwrap();
set.list.retain(|c| !c.queue.is_closed());
set.shm_list.retain(|c| !c.writer.is_closed());
let mut consumers: Vec<ConsumerHealth> = set
.list
.iter()
.map(|c| ConsumerHealth {
id: c.id,
peer: c.peer.clone(),
fill: c.queue.len(),
capacity: c.queue.capacity(),
dropped: c.queue.dropped(),
})
.collect();
consumers.extend(set.shm_list.iter().map(|c| ConsumerHealth {
id: c.id,
peer: c.peer.clone(),
fill: c.writer.fill(),
capacity: c.writer.requested_capacity(),
dropped: c.writer.outlet_dropped(),
}));
OutletHealth {
consumers,
history_len: set.history.as_ref().map_or(0, History::len),
pushed: set.next,
}
}
pub fn push(&self, s: &Sample) {
self.push_packed(PackedSample::from_sample(s));
}
pub(crate) fn push_numeric_raw(
&self,
timestamp: f64,
format: labstream_wire::Format,
bytes: &[u8],
) {
let mut set = self.consumers.lock().unwrap();
let seq = set.next;
let full = set.deducer.apply(timestamp);
if !set.shm_list.is_empty() {
set.shm_list.retain(|c| !c.writer.is_closed());
for c in set.shm_list.iter() {
c.writer
.push_raw_one(seq, full, bytes, c.channels.as_deref());
}
}
if !set.list.is_empty() || set.history.is_some() {
let s = PackedSample::from_raw_bytes(timestamp, format, Arc::from(bytes));
set.list.retain(|c| !c.queue.is_closed());
for c in set.list.iter() {
c.queue.push_packed(s.clone());
}
set.record_packed_with_full(&s, full);
} else {
set.next = set.next.wrapping_add(1);
}
}
pub fn push_packed(&self, s: PackedSample) {
let mut set = self.consumers.lock().unwrap();
let seq = set.next;
let full = set.deducer.apply(s.timestamp);
if !set.shm_list.is_empty() {
set.shm_list.retain(|c| !c.writer.is_closed());
if let labstream_wire::PackedValues::Numeric { bytes, .. } = &s.values {
for c in set.shm_list.iter() {
c.writer
.push_raw_one(seq, full, bytes, c.channels.as_deref());
}
}
}
set.list.retain(|c| !c.queue.is_closed());
for c in set.list.iter() {
c.queue.push_packed(s.clone());
}
set.record_packed_with_full(&s, full);
}
pub fn push_many(&self, samples: &[Sample]) {
if samples.is_empty() {
return;
}
let packed: Vec<PackedSample> = samples.iter().map(PackedSample::from_sample).collect();
self.push_many_packed(&packed);
}
pub fn push_many_packed(&self, samples: &[PackedSample]) {
if samples.is_empty() {
return;
}
let mut set = self.consumers.lock().unwrap();
let first_seq = set.next;
let mut full_ts = std::mem::take(&mut set.ts_scratch);
full_ts.clear();
full_ts.reserve(samples.len());
for s in samples {
full_ts.push(set.deducer.apply(s.timestamp));
}
if !set.shm_list.is_empty() {
set.shm_list.retain(|c| !c.writer.is_closed());
for c in set.shm_list.iter() {
c.writer
.push_batch(first_seq, samples, &full_ts, c.channels.as_deref());
}
}
set.list.retain(|c| !c.queue.is_closed());
for c in set.list.iter() {
c.queue.push_many_packed(samples);
}
for (s, &full) in samples.iter().zip(&full_ts) {
set.record_packed_with_full(s, full);
}
set.ts_scratch = full_ts;
}
pub(crate) fn push_chunk_numeric_raw(
&self,
format: labstream_wire::Format,
raw: &[u8],
row_bytes: usize,
timestamps: &[f64],
) {
let n = timestamps.len();
if n == 0 || row_bytes == 0 || raw.len() < n * row_bytes {
return;
}
let mut set = self.consumers.lock().unwrap();
let first_seq = set.next;
let mut full_ts = std::mem::take(&mut set.ts_scratch);
full_ts.clear();
full_ts.reserve(n);
for &t in timestamps {
full_ts.push(set.deducer.apply(t));
}
if !set.shm_list.is_empty() {
set.shm_list.retain(|c| !c.writer.is_closed());
for c in set.shm_list.iter() {
c.writer
.push_chunk_raw(first_seq, raw, row_bytes, &full_ts, c.channels.as_deref());
}
}
if !set.list.is_empty() || set.history.is_some() {
let block: Vec<PackedSample> = (0..n)
.map(|k| {
PackedSample::from_raw_bytes(
timestamps[k],
format,
Arc::from(&raw[k * row_bytes..(k + 1) * row_bytes]),
)
})
.collect();
set.list.retain(|c| !c.queue.is_closed());
for c in set.list.iter() {
c.queue.push_many_packed(&block);
}
for (s, &full) in block.iter().zip(&full_ts) {
set.record_packed_with_full(s, full);
}
} else {
set.next = set.next.wrapping_add(n as u64);
}
set.ts_scratch = full_ts;
}
pub fn close(&self) {
let set = self.consumers.lock().unwrap();
for c in set.list.iter() {
c.queue.close();
}
for c in set.shm_list.iter() {
c.writer.close();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use labstream_wire::Value;
fn sample(i: i32) -> Sample {
Sample {
timestamp: i as f64,
values: vec![Value::I32(i)],
}
}
fn index(s: &Sample) -> i32 {
match s.values[0] {
Value::I32(v) => v,
_ => panic!("wrong format"),
}
}
#[test]
fn a_full_queue_keeps_the_newest() {
let q = SampleQueue::new(100);
for i in 0..600 {
q.push(sample(i));
}
assert_eq!(q.len(), 100);
assert_eq!(q.dropped(), 500);
let first = q.pop(Duration::from_millis(10)).expect("a sample");
assert_eq!(index(&first), 500, "the oldest surviving sample");
let mut last = first;
while let Some(s) = q.pop(Duration::from_millis(10)) {
last = s;
}
assert_eq!(index(&last), 599, "the newest sample");
}
#[test]
fn a_block_write_gives_what_single_writes_give() {
let one = SampleQueue::new(100);
let many = SampleQueue::new(100);
let block: Vec<Sample> = (0..600).map(sample).collect();
for s in &block {
one.push(s.clone());
}
many.push_many(&block);
assert_eq!(one.len(), many.len());
assert_eq!(one.dropped(), many.dropped());
while let Some(a) = one.pop(Duration::from_millis(10)) {
let b = many.pop(Duration::from_millis(10)).expect("the same count");
assert_eq!(index(&a), index(&b));
}
assert!(many.pop(Duration::from_millis(10)).is_none());
}
#[test]
fn an_owned_block_write_gives_what_single_writes_give() {
for (before, block_len) in [(0, 50), (30, 50), (30, 600), (0, 100), (99, 1)] {
let one = SampleQueue::new(100);
let owned = SampleQueue::new(100);
for i in 0..before {
one.push(sample(i));
owned.push(sample(i));
}
let mut block: Vec<Sample> = (1000..1000 + block_len).map(sample).collect();
for s in &block {
one.push(s.clone());
}
owned.push_owned(&mut block);
assert!(block.is_empty(), "the call takes every sample");
assert_eq!(one.len(), owned.len());
assert_eq!(one.dropped(), owned.dropped(), "{before} then {block_len}");
while let Some(a) = one.pop(Duration::from_millis(10)) {
let b = owned
.pop(Duration::from_millis(10))
.expect("the same count");
assert_eq!(index(&a), index(&b));
}
assert!(owned.pop(Duration::from_millis(10)).is_none());
}
}
#[test]
fn a_block_read_takes_what_waits_in_order() {
let q = SampleQueue::new(100);
q.push_many(&(0..10).map(sample).collect::<Vec<_>>());
let mut out = Vec::new();
let got = q.pop_many(&mut out, 4, Duration::from_millis(10));
assert_eq!(got, 4, "the call stops at max");
assert_eq!(out.iter().map(index).collect::<Vec<_>>(), [0, 1, 2, 3]);
let got = q.pop_many(&mut out, 100, Duration::from_millis(10));
assert_eq!(got, 6, "the call takes what waits, and not more");
assert_eq!(out.len(), 10, "the samples go to the end of the buffer");
assert_eq!(index(&out[9]), 9);
}
#[test]
fn a_block_read_of_an_empty_queue_gives_nothing() {
let q = SampleQueue::new(10);
let mut out = Vec::new();
assert_eq!(q.pop_many(&mut out, 8, Duration::from_millis(5)), 0);
assert!(out.is_empty());
q.close();
assert_eq!(q.pop_many(&mut out, 8, Duration::from_millis(50)), 0);
}
#[test]
fn a_block_read_waits_for_the_first_sample_only() {
let q = Arc::new(SampleQueue::new(10));
let writer = Arc::clone(&q);
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(20));
writer.push_many(&[sample(1), sample(2)]);
});
let mut out = Vec::new();
let start = Instant::now();
let got = q.pop_many(&mut out, 1000, Duration::from_secs(5));
assert_eq!(got, 2, "the call takes the block that arrived");
assert!(
start.elapsed() < Duration::from_secs(1),
"the call returned"
);
}
#[test]
fn a_block_reaches_every_consumer() {
let fan = Fanout::new();
let a = fan.add(10);
let b = fan.add(10);
fan.push_many(&[sample(7), sample(8)]);
for q in [&a, &b] {
let mut out = Vec::new();
assert_eq!(q.pop_many(&mut out, 10, Duration::from_millis(10)), 2);
assert_eq!(out.iter().map(index).collect::<Vec<_>>(), [7, 8]);
}
}
#[test]
fn the_writer_never_waits() {
let q = SampleQueue::new(1);
let start = Instant::now();
for i in 0..1_000_000 {
q.push(sample(i));
}
assert!(
start.elapsed() < Duration::from_secs(10),
"the writer waited"
);
assert_eq!(q.len(), 1);
assert_eq!(index(&q.pop(Duration::from_millis(10)).unwrap()), 999_999);
}
#[test]
fn a_capacity_of_zero_floors_at_one() {
let q = SampleQueue::new(0);
assert_eq!(q.capacity(), 1);
q.push(sample(7));
assert_eq!(index(&q.pop(Duration::from_millis(10)).unwrap()), 7);
}
#[test]
fn an_empty_queue_reports_nothing_after_the_timeout() {
let q = SampleQueue::new(4);
let start = Instant::now();
assert!(q.pop(Duration::from_millis(120)).is_none());
assert!(start.elapsed() >= Duration::from_millis(100));
}
#[test]
fn a_closed_queue_stops_a_reader_at_once() {
let q = Arc::new(SampleQueue::new(4));
let c = Arc::clone(&q);
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(30));
c.close();
});
let start = Instant::now();
assert!(q.pop(Duration::from_secs(10)).is_none());
assert!(
start.elapsed() < Duration::from_secs(2),
"the reader waited out the timeout"
);
}
#[test]
fn a_reader_wakes_when_a_sample_arrives() {
let q = Arc::new(SampleQueue::new(4));
let c = Arc::clone(&q);
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(30));
c.push(sample(42));
});
let got = q.pop(Duration::from_secs(2)).expect("a sample");
assert_eq!(index(&got), 42);
}
#[test]
fn a_slow_consumer_never_holds_back_a_fast_one() {
let f = Fanout::new();
let fast = f.add(1000);
let slow = f.add(10);
for i in 0..500 {
f.push(&sample(i));
}
assert_eq!(fast.len(), 500, "the fast consumer lost samples");
assert_eq!(fast.dropped(), 0);
assert_eq!(slow.len(), 10);
assert_eq!(slow.dropped(), 490);
}
#[test]
fn a_closed_consumer_leaves_the_set() {
let f = Fanout::new();
let a = f.add(10);
let b = f.add(10);
assert_eq!(f.count(), 2);
b.close();
f.push(&sample(1));
assert_eq!(f.count(), 1, "a closed consumer must be removed");
assert_eq!(a.len(), 1);
}
#[test]
fn removing_a_consumer_stops_its_samples() {
let f = Fanout::new();
let a = f.add(10);
f.push(&sample(1));
f.remove(&a);
f.push(&sample(2));
assert_eq!(a.len(), 1, "a removed consumer must receive nothing more");
}
struct Steps(u64);
impl Steps {
fn next(&mut self, below: u64) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0 % below
}
}
#[test]
fn the_numbers_follow_a_model_with_one_number_for_each_sample() {
for seed in 1..40u64 {
let mut steps = Steps(seed.wrapping_mul(0x9E37_79B9_7F4A_7C15));
let capacity = 1 + steps.next(12) as usize;
let first = steps.next(1000);
let q = SampleQueue::with_first_number(capacity, first);
let mut model: VecDeque<u64> = VecDeque::new();
let mut next = first;
for _ in 0..400 {
match steps.next(5) {
0 => {
q.push(sample(next as i32));
model.push_back(next);
next += 1;
}
1 | 2 => {
let k = steps.next(30) as usize;
let block: Vec<Sample> =
(0..k).map(|i| sample((next + i as u64) as i32)).collect();
if steps.next(2) == 0 {
q.push_many(&block);
} else {
let mut owned = block;
q.push_owned(&mut owned);
}
for i in 0..k as u64 {
model.push_back(next + i);
}
next += k as u64;
}
3 => {
let got = q.pop(Duration::ZERO);
let want = model.pop_front();
assert_eq!(got.map(|s| index(&s) as u64), want);
}
_ => {
let max = 1 + steps.next(20) as usize;
let mut out = Vec::new();
let (n, number) = q.pop_many_numbered(&mut out, max, Duration::ZERO);
assert_eq!(n, out.len());
let want_first = model.front().copied().unwrap_or(next);
assert_eq!(number, want_first, "seed {seed}");
for (i, s) in out.iter().enumerate() {
assert_eq!(index(s) as u64, number + i as u64, "seed {seed}");
assert_eq!(model.pop_front(), Some(number + i as u64));
}
}
}
while model.len() > capacity {
model.pop_front();
}
assert_eq!(q.len(), model.len(), "seed {seed}");
}
}
}
#[test]
fn a_late_consumer_starts_at_the_current_number() {
let f = Fanout::new();
let a = f.add(100);
for i in 0..5 {
f.push(&sample(i));
}
let b = f.add(100);
assert_eq!(f.next_number(), 5);
f.push_many(&[sample(5), sample(6)]);
let mut out = Vec::new();
assert_eq!(a.pop_many_numbered(&mut out, 100, Duration::ZERO), (7, 0));
out.clear();
assert_eq!(b.pop_many_numbered(&mut out, 100, Duration::ZERO), (2, 5));
assert_eq!(index(&out[0]), 5);
}
fn chunk(first: i32, len: i32, t: f64) -> Vec<Sample> {
(first..first + len)
.map(|i| Sample {
timestamp: if i == first {
t
} else {
labstream_wire::DEDUCED_TIMESTAMP
},
values: vec![Value::I32(i)],
})
.collect()
}
#[test]
fn the_history_is_off_until_a_consumer_asks_for_it() {
let f = Fanout::with_rate(100.0, HistoryLimits::DEFAULT);
let q = f.add(100);
f.push_many(&chunk(0, 10, 1.0));
assert_eq!(f.history_len(), 0);
assert_eq!(f.read_history(0, 10, 256, &mut Vec::new()), None);
assert!(f.start_history());
assert!(f.start_history(), "a second call changes nothing");
assert_eq!(
f.history_len(),
0,
"the history holds no sample from before"
);
f.remove(&q);
f.push_many(&chunk(10, 5, 2.0));
f.push(&sample(15));
assert_eq!(f.history_len(), 6);
let mut out = Vec::new();
assert_eq!(
f.read_history(0, f.next_number(), 256, &mut out),
Some((10, 6))
);
assert_eq!(
out.iter().map(index).collect::<Vec<_>>(),
(10..16).collect::<Vec<_>>()
);
}
#[test]
fn limits_that_turn_the_history_off_keep_it_off() {
let f = Fanout::with_rate(100.0, HistoryLimits::OFF);
assert!(!f.start_history());
f.push(&sample(0));
assert_eq!(f.history_len(), 0);
}
#[test]
fn the_history_and_the_queues_use_the_same_numbers() {
let f = Fanout::with_rate(100.0, HistoryLimits::DEFAULT);
f.push_many(&chunk(0, 7, 1.0));
f.start_history();
let q = f.add(100);
f.push_many(&chunk(7, 20, 2.0));
let mut live = Vec::new();
let (_, first) = q.pop_many_numbered(&mut live, 100, Duration::ZERO);
assert_eq!(first, 7);
let mut old = Vec::new();
assert_eq!(
f.read_history(first, u64::MAX / 4, 256, &mut old),
Some((7, 20))
);
let a: Vec<i32> = live.iter().map(index).collect();
let b: Vec<i32> = old.iter().map(index).collect();
assert_eq!(a, b);
assert_eq!(f.read_history(0, first, 256, &mut Vec::new()), None);
let q2 = f.add(100);
let n = f.next_number();
f.push_many(&chunk(27, 5, 3.0));
let mut resent = Vec::new();
assert_eq!(f.read_history(10, n, 256, &mut resent), Some((10, 17)));
assert_eq!(index(resent.last().unwrap()) as u64, n - 1);
let mut live = Vec::new();
assert_eq!(q2.pop_many_numbered(&mut live, 100, Duration::ZERO), (5, n));
}
#[test]
fn the_history_holds_the_timestamps_that_an_inlet_calculates() {
let srate = 300.0;
let f = Fanout::with_rate(srate, HistoryLimits::DEFAULT);
let mut pushed = chunk(0, 9, 1.234_567);
pushed.extend(chunk(9, 9, 1.5));
let second = chunk(18, 9, 2.0);
f.push_many(&pushed);
f.push_many(&second[..4]);
f.start_history();
f.push_many(&second[4..]);
f.push(&sample(27));
pushed.extend(second);
pushed.push(sample(27));
let mut inlet = TimestampDeducer::new(srate);
let want: Vec<u64> = pushed
.iter()
.map(|s| inlet.apply(s.timestamp).to_bits())
.collect();
let mut got = Vec::new();
assert_eq!(f.read_history(0, 28, 256, &mut got), Some((22, 6)));
assert_eq!(got.len(), 6);
for s in &got {
assert_ne!(s.timestamp, labstream_wire::DEDUCED_TIMESTAMP);
let i = index(s) as usize;
assert_eq!(s.timestamp.to_bits(), want[i], "sample {i}");
}
}
#[test]
fn a_read_takes_one_block() {
let f = Fanout::with_rate(1000.0, HistoryLimits::DEFAULT);
f.start_history();
for i in 0..1000 {
f.push(&sample(i));
}
let mut out = Vec::new();
let mut from = 0;
let mut blocks = 0;
while let Some((first, count)) = f.read_history(from, 1000, 256, &mut out) {
from = first + count as u64;
blocks += 1;
}
assert_eq!(blocks, 4);
assert_eq!(out.len(), 1000);
assert_eq!(
out.iter().map(index).collect::<Vec<_>>(),
(0..1000).collect::<Vec<_>>()
);
}
#[test]
fn a_full_queue_records_a_coalesced_drop_gap() {
let q = SampleQueue::new(4);
q.push(sample(10));
let first = q.pop(Duration::from_millis(10)).unwrap();
assert_eq!(first.timestamp, 10.0);
let mut batch: Vec<Sample> = (11..=20).map(sample).collect();
q.push_owned(&mut batch);
assert_eq!(q.dropped(), 6);
let pending = q.drop_gaps(0.0);
assert_eq!(pending.len(), 1);
assert_eq!(pending[0].start, 10.0);
assert_eq!(pending[0].end, 17.0);
assert_eq!(pending[0].count, 6);
assert_eq!(pending[0].kind, GapKind::Dropped);
let next = q.pop(Duration::from_millis(10)).unwrap();
assert_eq!(next.timestamp, 17.0);
let taken = q.take_drop_gaps(50.0);
assert_eq!(taken.len(), 1);
assert_eq!(taken[0].start, 60.0);
assert_eq!(taken[0].end, 67.0);
assert_eq!(taken[0].count, 6);
assert!(q.drop_gaps(0.0).is_empty());
}
#[test]
fn fanout_health_reports_fill_capacity_and_drops_per_consumer() {
let fan = Fanout::new();
let fast = fan.add_with_peer(10, "127.0.0.1:40001".to_string());
let slow = fan.add_with_peer(3, "127.0.0.1:40002".to_string());
for i in 0..8 {
fan.push(&sample(i));
let _ = fast.pop(Duration::from_millis(1));
}
let h = fan.health();
assert_eq!(h.pushed, 8);
assert_eq!(h.consumers.len(), 2);
assert_eq!(h.consumers[0].id, 1);
assert_eq!(h.consumers[0].peer, "127.0.0.1:40001");
assert_eq!(h.consumers[0].fill, 0);
assert_eq!(h.consumers[0].capacity, 10);
assert_eq!(h.consumers[0].dropped, 0);
assert_eq!(h.consumers[1].id, 2);
assert_eq!(h.consumers[1].peer, "127.0.0.1:40002");
assert_eq!(h.consumers[1].fill, 3);
assert_eq!(h.consumers[1].capacity, 3);
assert_eq!(h.consumers[1].dropped, 5);
drop(slow);
}
#[test]
fn queue_delivers_exact_skip_positions_on_pop_and_pop_many() {
let q = SampleQueue::new(32);
let mut batch: Vec<Sample> = (0..6).map(sample).collect();
q.push_owned_with_skips(&mut batch, &[(3, 100)]);
let mut out = Vec::new();
let mut skips = Vec::new();
let got = q.pop_many_with_skips(&mut out, Some(&mut skips), 32, Duration::ZERO);
assert_eq!(got, 6);
assert_eq!(skips, vec![(3, 100)]);
let q2 = SampleQueue::new(3);
let mut b1: Vec<Sample> = vec![sample(0), sample(1), sample(2)];
q2.push_owned_with_skips(&mut b1, &[(2, 50)]);
q2.push(sample(3));
let (s1, sk1) = q2.pop_with_skip(Duration::ZERO).unwrap();
assert_eq!(index(&s1), 1);
assert_eq!(sk1, 1);
let (s2, sk2) = q2.pop_with_skip(Duration::ZERO).unwrap();
assert_eq!(index(&s2), 2);
assert_eq!(sk2, 50);
}
}