pub mod connect;
pub mod inputs;
pub mod pump;
pub mod routes;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::{Duration, Instant};
use astrs_time::{HlcClock, HlcTimestamp};
use astrs_wire::common::stream::LogFrame;
use astrs_wire::{
DataId, DataflowId, ExtensionKey, FrameLimits, LogRecord, NodeId, NodeRequest, NodeSpawnSpec,
SessionId, SubscriptionId,
};
use tokio::sync::{mpsc, oneshot};
use crate::error::{NodeError, Result};
use crate::events::EventSource;
use crate::runtime::NodeRuntime;
pub use connect::{LinkDuplex, LinkReader, LinkWriter, NodeLink};
pub use inputs::{InputPlaneStats, InputRouteTable, MAX_CONSECUTIVE_LAGS, RECV_SLICE};
pub use routes::{RoutePlane, RouteSlot, RouteTable, RouteUpdate};
pub const OUTGOING_CAPACITY: usize = 256;
pub const DEFAULT_REPLY_TIMEOUT: Duration = Duration::from_secs(5);
pub type ExtReply = oneshot::Sender<Option<Vec<u8>>>;
type ExtWaiters = HashMap<String, Vec<ExtReply>>;
#[derive(Debug)]
pub enum Outgoing {
Request(Box<NodeRequest>),
Log(Box<LogFrame>),
Shutdown,
}
impl Outgoing {
#[must_use]
pub fn request(request: NodeRequest) -> Self {
Self::Request(Box::new(request))
}
#[must_use]
pub fn log(subscription: SubscriptionId, record: LogRecord) -> Self {
Self::Log(Box::new(LogFrame::new(subscription, record)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct SessionStats {
pub requests_sent: u64,
pub events_received: u64,
pub sends_inline: u64,
pub sends_zero_copy: u64,
pub shm_fallbacks: u64,
pub logs_sent: u64,
}
pub struct SessionShared {
pub dataflow: DataflowId,
pub node: NodeId,
pub generation: u64,
pub session: SessionId,
pub spec: Arc<NodeSpawnSpec>,
outgoing: mpsc::Sender<Outgoing>,
pub source: Arc<EventSource>,
hlc: Mutex<HlcClock>,
closed: AtomicBool,
ext_waiters: Mutex<ExtWaiters>,
pub routes: RouteTable,
pub inputs: InputRouteTable,
pub zero_copy_threshold: u64,
pub shm_broker: Option<std::path::PathBuf>,
pub limits: FrameLimits,
pub runtime: NodeRuntime,
pub log_subscription: SubscriptionId,
log_seq: AtomicU64,
requests_sent: AtomicU64,
events_received: AtomicU64,
sends_inline: AtomicU64,
sends_zero_copy: AtomicU64,
shm_fallbacks: AtomicU64,
logs_sent: AtomicU64,
}
impl core::fmt::Debug for SessionShared {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("SessionShared")
.field("dataflow", &self.dataflow)
.field("node", &self.node)
.field("generation", &self.generation)
.field("session", &self.session)
.field("closed", &self.is_closed())
.field("stats", &self.stats())
.finish_non_exhaustive()
}
}
impl SessionShared {
#[must_use]
pub fn new(
spec: Arc<NodeSpawnSpec>,
session: SessionId,
outgoing: mpsc::Sender<Outgoing>,
source: Arc<EventSource>,
runtime: NodeRuntime,
zero_copy_threshold: u64,
limits: FrameLimits,
) -> Self {
Self {
dataflow: spec.dataflow,
node: spec.node.clone(),
generation: spec.generation,
session,
spec,
outgoing,
source,
hlc: Mutex::new(HlcClock::system()),
closed: AtomicBool::new(false),
ext_waiters: Mutex::new(HashMap::new()),
routes: RouteTable::new(),
inputs: InputRouteTable::new(),
zero_copy_threshold,
shm_broker: None,
limits,
runtime,
log_subscription: SubscriptionId::new(0),
log_seq: AtomicU64::new(0),
requests_sent: AtomicU64::new(0),
events_received: AtomicU64::new(0),
sends_inline: AtomicU64::new(0),
sends_zero_copy: AtomicU64::new(0),
shm_fallbacks: AtomicU64::new(0),
logs_sent: AtomicU64::new(0),
}
}
#[must_use]
pub fn with_shm_broker(mut self, path: Option<std::path::PathBuf>) -> Self {
self.shm_broker = path;
self
}
#[must_use]
pub fn is_closed(&self) -> bool {
self.closed.load(Ordering::Acquire)
}
pub fn request_drain(&self) -> Result<()> {
self.try_send(Outgoing::Shutdown)
}
pub fn close(&self) {
self.closed.store(true, Ordering::Release);
self.source.close();
self.inputs.close();
let waiters = std::mem::take(&mut *lock(&self.ext_waiters));
for (_key, senders) in waiters {
for sender in senders {
let _ = sender.send(None);
}
}
}
#[must_use]
pub fn hlc_now(&self) -> HlcTimestamp {
lock(&self.hlc).now()
}
pub fn observe_remote(&self, remote: HlcTimestamp) -> bool {
lock(&self.hlc).update_with(remote).is_ok()
}
#[must_use]
pub fn stats(&self) -> SessionStats {
SessionStats {
requests_sent: self.requests_sent.load(Ordering::Relaxed),
events_received: self.events_received.load(Ordering::Relaxed),
sends_inline: self.sends_inline.load(Ordering::Relaxed),
sends_zero_copy: self.sends_zero_copy.load(Ordering::Relaxed),
shm_fallbacks: self.shm_fallbacks.load(Ordering::Relaxed),
logs_sent: self.logs_sent.load(Ordering::Relaxed),
}
}
pub fn count_inline_send(&self) {
self.sends_inline.fetch_add(1, Ordering::Relaxed);
}
pub fn count_zero_copy_send(&self) {
self.sends_zero_copy.fetch_add(1, Ordering::Relaxed);
}
pub fn count_shm_fallback(&self) {
self.shm_fallbacks.fetch_add(1, Ordering::Relaxed);
}
pub fn count_event(&self) {
self.events_received.fetch_add(1, Ordering::Relaxed);
}
pub fn next_log_seq(&self) -> u64 {
self.log_seq.fetch_add(1, Ordering::Relaxed)
}
pub fn try_send(&self, message: Outgoing) -> Result<()> {
if self.is_closed() {
return Err(NodeError::DaemonGone);
}
let is_log = matches!(message, Outgoing::Log(_));
match self.outgoing.try_send(message) {
Ok(()) => {
self.count_outgoing(is_log);
Ok(())
}
Err(mpsc::error::TrySendError::Full(_)) => Err(NodeError::Backpressure {
depth: OUTGOING_CAPACITY,
}),
Err(mpsc::error::TrySendError::Closed(_)) => Err(NodeError::DaemonGone),
}
}
pub async fn send_async(&self, message: Outgoing) -> Result<()> {
if self.is_closed() {
return Err(NodeError::DaemonGone);
}
let is_log = matches!(message, Outgoing::Log(_));
self.outgoing
.send(message)
.await
.map_err(|_| NodeError::DaemonGone)?;
self.count_outgoing(is_log);
Ok(())
}
pub fn send_blocking(&self, message: Outgoing) -> Result<()> {
if self.is_closed() {
return Err(NodeError::DaemonGone);
}
let is_log = matches!(message, Outgoing::Log(_));
let pending = match self.outgoing.try_send(message) {
Ok(()) => {
self.count_outgoing(is_log);
return Ok(());
}
Err(mpsc::error::TrySendError::Full(message)) => message,
Err(mpsc::error::TrySendError::Closed(_)) => return Err(NodeError::DaemonGone),
};
let sender = self.outgoing.clone();
self.runtime
.block_on("Node::send", "Node::send_async", async move {
sender.send(pending).await
})?
.map_err(|_| NodeError::DaemonGone)?;
self.count_outgoing(is_log);
Ok(())
}
pub fn send_request(&self, request: NodeRequest) -> Result<()> {
let is_publish = matches!(request, NodeRequest::SendMessage { .. });
self.send_blocking(Outgoing::request(request))?;
if is_publish {
self.source.finish_deadlines(Instant::now());
}
Ok(())
}
pub async fn send_request_async(&self, request: NodeRequest) -> Result<()> {
let is_publish = matches!(request, NodeRequest::SendMessage { .. });
self.send_async(Outgoing::request(request)).await?;
if is_publish {
self.source.finish_deadlines(Instant::now());
}
Ok(())
}
fn count_outgoing(&self, is_log: bool) {
if is_log {
self.logs_sent.fetch_add(1, Ordering::Relaxed);
} else {
self.requests_sent.fetch_add(1, Ordering::Relaxed);
}
}
#[must_use]
pub fn watch_extension(&self, key: &ExtensionKey) -> oneshot::Receiver<Option<Vec<u8>>> {
let (sender, receiver) = oneshot::channel();
lock(&self.ext_waiters)
.entry(key.to_string())
.or_default()
.push(sender);
receiver
}
pub fn resolve_extension(&self, key: &ExtensionKey, value: Option<Vec<u8>>) -> bool {
let mut waiters = lock(&self.ext_waiters);
let Some(senders) = waiters.get_mut(&key.to_string()) else {
return false;
};
if senders.is_empty() {
return false;
}
let sender = senders.remove(0);
if senders.is_empty() {
let _ = waiters.remove(&key.to_string());
}
drop(waiters);
sender.send(value).is_ok()
}
#[must_use]
pub fn pending_extensions(&self) -> usize {
lock(&self.ext_waiters).values().map(Vec::len).sum()
}
#[must_use]
pub fn input_source(&self, input: &DataId) -> Option<astrs_wire::PortRef> {
self.spec.input(input).map(|spec| spec.source.clone())
}
#[must_use]
pub fn declares_output(&self, output: &DataId) -> bool {
self.spec.output(output).is_some()
}
}
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::*;
use astrs_wire::{LogLevel, NodeSource};
fn spec() -> Arc<NodeSpawnSpec> {
Arc::new(NodeSpawnSpec::new(
DataflowId::from_u128(1),
NodeId::new("camera").unwrap(),
2,
NodeSource::Dynamic,
))
}
fn shared() -> (Arc<SessionShared>, mpsc::Receiver<Outgoing>) {
let (sender, receiver) = mpsc::channel(OUTGOING_CAPACITY);
let shared = Arc::new(SessionShared::new(
spec(),
SessionId::from_u128(7),
sender,
Arc::new(EventSource::new()),
NodeRuntime::acquire().unwrap(),
4096,
FrameLimits::uds(),
));
(shared, receiver)
}
#[test]
fn requests_queue_and_are_counted() {
let (shared, mut receiver) = shared();
shared
.send_request(NodeRequest::EventStreamDropped)
.unwrap();
assert_eq!(shared.stats().requests_sent, 1);
let queued = receiver.try_recv().unwrap();
assert!(matches!(queued, Outgoing::Request(_)));
}
#[test]
fn a_full_channel_reports_backpressure_rather_than_growing() {
let (shared, _receiver) = shared();
let mut sent = 0;
loop {
match shared.try_send(Outgoing::request(NodeRequest::EventStreamDropped)) {
Ok(()) => sent += 1,
Err(NodeError::Backpressure { depth }) => {
assert_eq!(depth, OUTGOING_CAPACITY);
break;
}
Err(error) => panic!("unexpected {error}"),
}
assert!(sent <= OUTGOING_CAPACITY + 1, "the channel is unbounded");
}
assert_eq!(sent, OUTGOING_CAPACITY);
}
#[test]
fn a_blocking_send_waits_for_room_rather_than_dropping_the_message() {
let (shared, mut receiver) = shared();
for _ in 0..OUTGOING_CAPACITY {
shared
.try_send(Outgoing::request(NodeRequest::EventStreamDropped))
.unwrap();
}
let drainer = std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(20));
let mut drained = 0;
while receiver.blocking_recv().is_some() {
drained += 1;
if drained == OUTGOING_CAPACITY + 1 {
break;
}
}
drained
});
shared
.send_request(NodeRequest::EventStreamDropped)
.unwrap();
drop(shared);
assert!(drainer.join().unwrap() >= 1);
}
#[test]
fn a_closed_session_refuses_further_requests() {
let (shared, _receiver) = shared();
shared.close();
assert!(shared.is_closed());
assert!(matches!(
shared.send_request(NodeRequest::EventStreamDropped),
Err(NodeError::DaemonGone)
));
assert!(shared.source.is_closed());
}
#[test]
fn extension_waiters_are_matched_by_key() {
let (shared, _receiver) = shared();
let key = ExtensionKey::user("calibration").unwrap();
let receiver = shared.watch_extension(&key);
assert_eq!(shared.pending_extensions(), 1);
assert!(shared.resolve_extension(&key, Some(vec![1, 2, 3])));
assert_eq!(shared.pending_extensions(), 0);
assert_eq!(receiver.blocking_recv().unwrap(), Some(vec![1, 2, 3]));
assert!(!shared.resolve_extension(&key, Some(vec![9])));
}
#[test]
fn closing_releases_extension_waiters() {
let (shared, _receiver) = shared();
let key = ExtensionKey::user("calibration").unwrap();
let receiver = shared.watch_extension(&key);
shared.close();
assert_eq!(receiver.blocking_recv().unwrap(), None);
assert_eq!(shared.pending_extensions(), 0);
}
#[test]
fn the_clock_advances_and_absorbs_remote_readings() {
let (shared, _receiver) = shared();
let first = shared.hlc_now();
let second = shared.hlc_now();
assert!(second >= first);
assert!(shared.observe_remote(second));
}
#[test]
fn log_frames_are_counted_separately() {
let (shared, mut receiver) = shared();
let record = LogRecord::new(shared.hlc_now(), LogLevel::Info, "hello");
shared
.try_send(Outgoing::log(shared.log_subscription, record))
.unwrap();
assert!(matches!(receiver.try_recv().unwrap(), Outgoing::Log(_)));
assert_eq!(shared.stats().logs_sent, 1);
assert_eq!(shared.stats().requests_sent, 0, "logs are not requests");
}
#[test]
fn counters_track_the_two_send_planes() {
let (shared, _receiver) = shared();
shared.count_inline_send();
shared.count_zero_copy_send();
shared.count_zero_copy_send();
shared.count_shm_fallback();
shared.count_event();
let stats = shared.stats();
assert_eq!(stats.sends_inline, 1);
assert_eq!(stats.sends_zero_copy, 2);
assert_eq!(stats.shm_fallbacks, 1);
assert_eq!(stats.events_received, 1);
}
#[test]
fn the_specification_answers_wiring_questions() {
let (shared, _receiver) = shared();
assert!(!shared.declares_output(&DataId::new("image").unwrap()));
assert!(
shared
.input_source(&DataId::new("frames").unwrap())
.is_none()
);
assert_eq!(shared.generation, 2);
assert!(format!("{shared:?}").contains("camera"));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn the_async_sender_waits_for_room() {
let (shared, mut receiver) = shared();
shared
.send_async(Outgoing::request(NodeRequest::EventStreamDropped))
.await
.unwrap();
assert!(receiver.recv().await.is_some());
}
}