use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use astrs_shm::{Producer, Segment, SegmentClient, SegmentKey};
use astrs_wire::{DataId, DataflowId, NodeId, PortRef, RouteDowngradeReason, ShmSegmentSpec};
use crate::error::{NodeError, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RoutePlane {
Daemon,
Shm,
}
impl RoutePlane {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Daemon => "daemon",
Self::Shm => "shm",
}
}
#[must_use]
pub const fn is_zero_copy(self) -> bool {
matches!(self, Self::Shm)
}
}
impl core::fmt::Display for RoutePlane {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug)]
pub enum RouteUpdate {
Idle,
Upgrade {
producer: Box<Producer>,
segment: ShmSegmentSpec,
consumers: Vec<PortRef>,
},
Downgrade {
reason: RouteDowngradeReason,
},
}
impl RouteUpdate {
#[must_use]
pub const fn is_idle(&self) -> bool {
matches!(self, Self::Idle)
}
}
#[derive(Debug)]
pub struct RouteSlot {
pending: Mutex<RouteUpdate>,
plane: Mutex<RoutePlane>,
upgrades: AtomicU64,
downgrades: AtomicU64,
}
impl Default for RouteSlot {
fn default() -> Self {
Self::new()
}
}
impl RouteSlot {
#[must_use]
pub fn new() -> Self {
Self {
pending: Mutex::new(RouteUpdate::Idle),
plane: Mutex::new(RoutePlane::Daemon),
upgrades: AtomicU64::new(0),
downgrades: AtomicU64::new(0),
}
}
pub fn offer_upgrade(
&self,
producer: Producer,
segment: ShmSegmentSpec,
consumers: Vec<PortRef>,
) {
*lock(&self.pending) = RouteUpdate::Upgrade {
producer: Box::new(producer),
segment,
consumers,
};
self.upgrades.fetch_add(1, Ordering::Relaxed);
}
pub fn offer_downgrade(&self, reason: RouteDowngradeReason) {
*lock(&self.pending) = RouteUpdate::Downgrade { reason };
self.downgrades.fetch_add(1, Ordering::Relaxed);
}
#[must_use]
pub fn take(&self) -> RouteUpdate {
core::mem::replace(&mut lock(&self.pending), RouteUpdate::Idle)
}
#[must_use]
pub fn has_update(&self) -> bool {
!lock(&self.pending).is_idle()
}
#[must_use]
pub fn plane(&self) -> RoutePlane {
*lock(&self.plane)
}
pub fn set_plane(&self, plane: RoutePlane) {
*lock(&self.plane) = plane;
}
#[must_use]
pub fn transitions(&self) -> (u64, u64) {
(
self.upgrades.load(Ordering::Relaxed),
self.downgrades.load(Ordering::Relaxed),
)
}
}
#[derive(Debug, Default)]
pub struct RouteTable {
slots: Mutex<HashMap<DataId, Arc<RouteSlot>>>,
}
impl RouteTable {
#[must_use]
pub fn new() -> Self {
Self {
slots: Mutex::new(HashMap::new()),
}
}
#[must_use]
pub fn slot(&self, output: &DataId) -> Arc<RouteSlot> {
let mut slots = lock(&self.slots);
Arc::clone(
slots
.entry(output.clone())
.or_insert_with(|| Arc::new(RouteSlot::new())),
)
}
#[must_use]
pub fn existing(&self, output: &DataId) -> Option<Arc<RouteSlot>> {
lock(&self.slots).get(output).map(Arc::clone)
}
#[must_use]
pub fn planes(&self) -> Vec<(DataId, RoutePlane)> {
let mut planes: Vec<(DataId, RoutePlane)> = lock(&self.slots)
.iter()
.map(|(id, slot)| (id.clone(), slot.plane()))
.collect();
planes.sort_by(|left, right| left.0.cmp(&right.0));
planes
}
#[must_use]
pub fn upgraded_count(&self) -> usize {
lock(&self.slots)
.values()
.filter(|slot| slot.plane().is_zero_copy())
.count()
}
}
pub fn open_producer(
dataflow: DataflowId,
node: &NodeId,
output: &DataId,
segment: &ShmSegmentSpec,
broker: Option<&Path>,
) -> Result<Producer> {
let key = SegmentKey::new(dataflow, node.clone(), output.clone(), segment.generation);
let canonical = key.canonical();
if canonical != segment.name {
return Err(NodeError::Pattern(format!(
"route upgrade for `{output}` names segment `{}`, but this node's own key is `{canonical}`",
segment.name,
)));
}
let mapped = match broker {
Some(path) => SegmentClient::connect(path)?.attach(&key)?,
None => {
let name = key.os_name();
Segment::open_named(&name, Some(&key))?
}
};
Producer::new(Arc::new(mapped)).map_err(NodeError::Shm)
}
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
mutex
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
fn output() -> DataId {
DataId::new("image").unwrap()
}
#[test]
fn a_fresh_route_is_on_the_daemon_path() {
let table = RouteTable::new();
let slot = table.slot(&output());
assert_eq!(slot.plane(), RoutePlane::Daemon);
assert!(!slot.has_update());
assert!(slot.take().is_idle());
assert_eq!(slot.transitions(), (0, 0));
assert_eq!(table.upgraded_count(), 0);
}
#[test]
fn the_table_hands_out_one_slot_per_output() {
let table = RouteTable::new();
let first = table.slot(&output());
let second = table.slot(&output());
assert!(Arc::ptr_eq(&first, &second));
assert!(table.existing(&output()).is_some());
assert!(table.existing(&DataId::new("other").unwrap()).is_none());
}
#[test]
fn downgrades_are_offered_and_taken_once() {
let table = RouteTable::new();
let slot = table.slot(&output());
slot.offer_downgrade(RouteDowngradeReason::ConsumerDetached {
consumer: PortRef::from_parts("detect", "frames").unwrap(),
});
assert!(slot.has_update());
assert_eq!(slot.transitions(), (0, 1));
let update = slot.take();
assert!(matches!(update, RouteUpdate::Downgrade { .. }));
assert!(!slot.has_update(), "an update is delivered once");
assert!(slot.take().is_idle());
}
#[test]
fn the_plane_a_handle_reports_is_remembered() {
let table = RouteTable::new();
let slot = table.slot(&output());
slot.set_plane(RoutePlane::Shm);
assert_eq!(slot.plane(), RoutePlane::Shm);
assert_eq!(table.upgraded_count(), 1);
assert_eq!(table.planes(), vec![(output(), RoutePlane::Shm)]);
slot.set_plane(RoutePlane::Daemon);
assert_eq!(table.upgraded_count(), 0);
}
#[test]
fn planes_describe_themselves() {
assert_eq!(RoutePlane::Daemon.as_str(), "daemon");
assert_eq!(RoutePlane::Shm.to_string(), "shm");
assert!(RoutePlane::Shm.is_zero_copy());
assert!(!RoutePlane::Daemon.is_zero_copy());
}
#[test]
fn a_segment_name_that_does_not_match_the_key_is_refused() {
let spec = ShmSegmentSpec::new("astrs-not-our-segment", 3, 8, 4096);
let error = open_producer(
DataflowId::from_u128(1),
&NodeId::new("camera").unwrap(),
&output(),
&spec,
None,
)
.unwrap_err();
assert!(matches!(error, NodeError::Pattern(_)), "{error}");
assert!(error.to_string().contains("this node's own key is"));
}
#[test]
fn the_canonical_name_is_what_the_daemon_sends() {
let dataflow = DataflowId::from_u128(0x7777);
let node = NodeId::new("camera").unwrap();
let key = SegmentKey::new(dataflow, node.clone(), output(), 2);
assert!(key.canonical().ends_with("/camera/image/2"));
let hashed = ShmSegmentSpec::new(key.os_name().as_str(), 2, 8, 4096);
assert!(open_producer(dataflow, &node, &output(), &hashed, None).is_err());
}
#[cfg(unix)]
#[test]
fn a_real_segment_is_opened_verified_and_claimed() {
use astrs_shm::{Backing, SegmentConfig};
let dataflow = DataflowId::from_u128(0x5150);
let node = NodeId::new("camera").unwrap();
let key = SegmentKey::new(dataflow, node.clone(), output(), 4);
let config = SegmentConfig::new(4, 1024)
.unwrap()
.with_backing(Backing::Named);
let Ok(segment) = Segment::create(key.clone(), config) else {
return;
};
let spec = ShmSegmentSpec::new(key.canonical(), 4, 4, 1024);
let producer = open_producer(dataflow, &node, &output(), &spec, None);
assert!(producer.is_ok(), "{:?}", producer.err());
let stale = ShmSegmentSpec::new(key.canonical(), 5, 4, 1024);
assert!(open_producer(dataflow, &node, &output(), &stale, None).is_err());
drop(producer);
let _ = segment.unlink();
}
}