use core::fmt;
use std::collections::BTreeMap;
use astrs_time::HlcTimestamp;
use oxicode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use crate::common::log::LogRecord;
use crate::common::metrics::{DaemonStats, NodeIoSample, NodeMetricsSample};
use crate::common::status::NodeExitCause;
use crate::common::stream::DataFrame;
use crate::frame::FrameKind;
use crate::ids::{BuildId, DataflowId, NodeId, SubscriptionId};
use crate::messages::coordinator_daemon::types::{BuildOutcome, DaemonRegistration, SpawnOutcome};
use crate::messages::impl_wire_message;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum DaemonEvent {
#[oxicode(variant = 0)]
Register(DaemonRegistration),
#[oxicode(variant = 1)]
Heartbeat {
seq: u64,
sent_at: HlcTimestamp,
stats: DaemonStats,
},
#[oxicode(variant = 2)]
BuildResult {
build: BuildId,
dataflow: DataflowId,
outcome: BuildOutcome,
},
#[oxicode(variant = 3)]
SpawnResult {
dataflow: DataflowId,
node: NodeId,
generation: u64,
outcome: SpawnOutcome,
},
#[oxicode(variant = 4)]
AllNodesReady {
dataflow: DataflowId,
nodes: Vec<NodeId>,
},
#[oxicode(variant = 5)]
AllNodesFinished {
dataflow: DataflowId,
results: BTreeMap<NodeId, NodeExitCause>,
},
#[oxicode(variant = 6)]
NodeStopped {
dataflow: DataflowId,
node: NodeId,
generation: u64,
cause: NodeExitCause,
restarting: bool,
},
#[oxicode(variant = 7)]
NodeMetrics {
dataflow: DataflowId,
samples: Vec<NodeMetricsSample>,
},
#[oxicode(variant = 8)]
Log {
request: Option<u64>,
records: Vec<LogRecord>,
truncated: bool,
},
#[oxicode(variant = 9)]
TopicTapData {
frame: Box<DataFrame>,
dropped: u64,
},
#[oxicode(variant = 10)]
StateCatchUpAck {
seq: u64,
applied: u32,
},
#[oxicode(variant = 11)]
Exit {
graceful: bool,
message: String,
},
#[oxicode(variant = 12)]
NodeIoMetrics {
dataflow: DataflowId,
samples: Vec<NodeIoSample>,
},
}
impl DaemonEvent {
#[must_use]
pub fn dataflow(&self) -> Option<DataflowId> {
match self {
Self::BuildResult { dataflow, .. }
| Self::SpawnResult { dataflow, .. }
| Self::AllNodesReady { dataflow, .. }
| Self::AllNodesFinished { dataflow, .. }
| Self::NodeStopped { dataflow, .. }
| Self::NodeMetrics { dataflow, .. }
| Self::NodeIoMetrics { dataflow, .. } => Some(*dataflow),
Self::TopicTapData { frame, .. } => Some(frame.dataflow),
Self::Register(_)
| Self::Heartbeat { .. }
| Self::Log { .. }
| Self::StateCatchUpAck { .. }
| Self::Exit { .. } => None,
}
}
#[must_use]
pub const fn node(&self) -> Option<&NodeId> {
match self {
Self::SpawnResult { node, .. } | Self::NodeStopped { node, .. } => Some(node),
_ => None,
}
}
#[must_use]
pub const fn is_liveness(&self) -> bool {
matches!(self, Self::Heartbeat { .. })
}
#[must_use]
pub const fn is_terminal(&self) -> bool {
matches!(
self,
Self::AllNodesFinished { .. } | Self::NodeStopped { .. } | Self::Exit { .. }
)
}
#[must_use]
pub fn is_failure(&self) -> bool {
match self {
Self::NodeStopped { cause, .. } => cause.is_failure(),
Self::SpawnResult { outcome, .. } => {
matches!(outcome, SpawnOutcome::Failed { .. })
}
Self::BuildResult { outcome, .. } => matches!(outcome, BuildOutcome::Failed { .. }),
Self::AllNodesFinished { results, .. } => {
results.values().any(NodeExitCause::is_failure)
}
Self::Exit { graceful, .. } => !*graceful,
_ => false,
}
}
#[must_use]
pub fn subscription(&self) -> Option<SubscriptionId> {
match self {
Self::TopicTapData { frame, .. } => Some(frame.subscription),
_ => None,
}
}
#[must_use]
pub fn payload_len(&self) -> usize {
match self {
Self::TopicTapData { frame, .. } => frame.payload.len(),
_ => 0,
}
}
#[must_use]
pub fn bitwise_eq(&self, other: &Self) -> bool {
match (self, other) {
(
Self::Heartbeat {
seq: left_seq,
sent_at: left_sent,
stats: left_stats,
},
Self::Heartbeat {
seq: right_seq,
sent_at: right_sent,
stats: right_stats,
},
) => {
left_seq == right_seq
&& left_sent == right_sent
&& left_stats.bitwise_eq(right_stats)
}
(
Self::TopicTapData {
frame: left_frame,
dropped: left_dropped,
},
Self::TopicTapData {
frame: right_frame,
dropped: right_dropped,
},
) => left_dropped == right_dropped && left_frame.bitwise_eq(right_frame),
(
Self::NodeMetrics {
dataflow: left_dataflow,
samples: left_samples,
},
Self::NodeMetrics {
dataflow: right_dataflow,
samples: right_samples,
},
) => {
left_dataflow == right_dataflow
&& left_samples.len() == right_samples.len()
&& left_samples.iter().zip(right_samples).all(|(left, right)| {
left.node == right.node
&& left.timestamp == right.timestamp
&& left.cpu_percent.to_bits() == right.cpu_percent.to_bits()
&& left.rss_bytes == right.rss_bytes
&& left.queue_depths == right.queue_depths
&& left.sent_total == right.sent_total
&& left.received_total == right.received_total
&& left.dropped_total == right.dropped_total
&& left.shm_slots_in_use == right.shm_slots_in_use
})
}
_ => self == other,
}
}
}
impl fmt::Display for DaemonEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Register(registration) => write!(f, "{registration}"),
Self::Heartbeat { seq, stats, .. } => {
write!(f, "heartbeat #{seq} ({} node(s))", stats.node_count)
}
Self::BuildResult { build, outcome, .. } => write!(f, "build {build}: {outcome}"),
Self::SpawnResult {
dataflow,
node,
outcome,
..
} => write!(f, "{dataflow}/{node}: {outcome}"),
Self::AllNodesReady { dataflow, nodes } => {
write!(f, "{} node(s) of {dataflow} ready", nodes.len())
}
Self::AllNodesFinished { dataflow, results } => {
write!(f, "{} node(s) of {dataflow} finished", results.len())
}
Self::NodeStopped {
dataflow,
node,
cause,
restarting,
..
} => write!(
f,
"{dataflow}/{node} {cause}{}",
if *restarting { " (restarting)" } else { "" }
),
Self::NodeMetrics { dataflow, samples } => {
write!(f, "{} metric sample(s) for {dataflow}", samples.len())
}
Self::NodeIoMetrics { dataflow, samples } => {
write!(f, "{} bandwidth sample(s) for {dataflow}", samples.len())
}
Self::Log {
request, records, ..
} => match request {
Some(request) => write!(f, "{} log record(s) for request {request}", records.len()),
None => write!(f, "{} log record(s)", records.len()),
},
Self::TopicTapData { frame, dropped } => write!(
f,
"tap {} on {} ({} byte(s), {dropped} dropped)",
frame.subscription,
frame.source,
frame.payload.len()
),
Self::StateCatchUpAck { seq, applied } => {
write!(f, "applied {applied} catch-up entries up to #{seq}")
}
Self::Exit { graceful, message } => write!(
f,
"daemon exiting {}: {message}",
if *graceful { "cleanly" } else { "on failure" }
),
}
}
}
impl_wire_message!(
DaemonEvent,
FrameKind::DaemonEvent,
[
"Register",
"Heartbeat",
"BuildResult",
"SpawnResult",
"AllNodesReady",
"AllNodesFinished",
"NodeStopped",
"NodeMetrics",
"Log",
"TopicTapData",
"StateCatchUpAck",
"Exit",
"NodeIoMetrics",
],
fn variant_index(&self) -> u16 {
match self {
Self::Register(_) => 0,
Self::Heartbeat { .. } => 1,
Self::BuildResult { .. } => 2,
Self::SpawnResult { .. } => 3,
Self::AllNodesReady { .. } => 4,
Self::AllNodesFinished { .. } => 5,
Self::NodeStopped { .. } => 6,
Self::NodeMetrics { .. } => 7,
Self::Log { .. } => 8,
Self::TopicTapData { .. } => 9,
Self::StateCatchUpAck { .. } => 10,
Self::Exit { .. } => 11,
Self::NodeIoMetrics { .. } => 12,
}
}
);
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::codec::{WireDecode, WireEncode};
use crate::frame::{FrameFlags, FrameLimits};
use crate::messages::WireMessage;
use crate::messages::samples::daemon_events;
#[test]
fn the_family_keeps_its_twelve_frozen_variants_and_its_tail_append() {
assert_eq!(DaemonEvent::VARIANT_NAMES.len(), 13);
assert_eq!(DaemonEvent::VARIANT_NAMES[0], "Register");
assert_eq!(DaemonEvent::VARIANT_NAMES[11], "Exit");
assert_eq!(DaemonEvent::VARIANT_NAMES[12], "NodeIoMetrics");
}
#[test]
fn every_variant_reports_and_encodes_its_frozen_index() {
let samples = daemon_events().unwrap();
assert_eq!(samples.len(), DaemonEvent::VARIANT_NAMES.len());
for (index, sample) in samples.iter().enumerate() {
let index = u16::try_from(index).unwrap();
assert_eq!(sample.variant_index(), index, "{sample:?}");
assert_eq!(u16::from(sample.encode_to_vec().unwrap()[0]), index);
}
}
#[test]
fn every_variant_round_trips_through_a_frame() {
let limits = FrameLimits::network();
for sample in daemon_events().unwrap() {
let bytes = sample.to_frame(FrameFlags::CRC, &limits).unwrap();
let decoded = DaemonEvent::from_bytes(&bytes, &limits).unwrap();
assert!(decoded.bitwise_eq(&sample), "{sample:?}");
}
}
#[test]
fn trailing_bytes_after_an_event_are_refused() {
for sample in daemon_events().unwrap() {
let mut bytes = sample.encode_to_vec().unwrap();
bytes.push(2);
assert!(matches!(
DaemonEvent::decode_exact(&bytes),
Err(crate::error::WireError::TrailingBytes { .. })
));
}
}
#[test]
fn failure_reporting_covers_every_way_a_daemon_reports_bad_news() {
let dataflow = DataflowId::from_u128(1);
let node = NodeId::new("camera").unwrap();
assert!(
DaemonEvent::NodeStopped {
dataflow,
node: node.clone(),
generation: 1,
cause: NodeExitCause::ExitCode { code: 1 },
restarting: false,
}
.is_failure()
);
assert!(
!DaemonEvent::NodeStopped {
dataflow,
node: node.clone(),
generation: 1,
cause: NodeExitCause::Success,
restarting: false,
}
.is_failure()
);
assert!(
DaemonEvent::SpawnResult {
dataflow,
node: node.clone(),
generation: 1,
outcome: SpawnOutcome::Failed {
message: "no such file".to_owned(),
errno: Some(2),
},
}
.is_failure()
);
assert!(
DaemonEvent::BuildResult {
build: BuildId::from_u128(1),
dataflow,
outcome: BuildOutcome::Failed {
node: None,
exit_code: Some(101),
message: "failed".to_owned(),
output: String::new(),
},
}
.is_failure()
);
assert!(
DaemonEvent::AllNodesFinished {
dataflow,
results: BTreeMap::from([(
node,
NodeExitCause::Panic {
message: "boom".to_owned()
}
)]),
}
.is_failure()
);
assert!(
DaemonEvent::Exit {
graceful: false,
message: "socket closed".to_owned(),
}
.is_failure()
);
assert!(
!DaemonEvent::Exit {
graceful: true,
message: "asked to".to_owned(),
}
.is_failure()
);
}
#[test]
fn a_tap_reports_its_subscription_and_payload_size() {
let sample = daemon_events()
.unwrap()
.into_iter()
.find(|event| matches!(event, DaemonEvent::TopicTapData { .. }))
.expect("the sample table covers TopicTapData");
assert!(sample.subscription().is_some());
assert!(sample.payload_len() > 0);
assert!(sample.dataflow().is_some());
assert_eq!(
DaemonEvent::StateCatchUpAck { seq: 1, applied: 0 }.payload_len(),
0
);
}
#[test]
fn float_gauges_survive_the_wire_bit_for_bit() {
let mut sample =
NodeMetricsSample::new(NodeId::new("camera").unwrap(), HlcTimestamp::new(1_000, 0));
sample.cpu_percent = f32::NAN;
let event = DaemonEvent::NodeMetrics {
dataflow: DataflowId::from_u128(1),
samples: vec![sample],
};
let bytes = event.encode_to_vec().unwrap();
let decoded = DaemonEvent::decode_exact(&bytes).unwrap();
assert_ne!(decoded, event);
assert!(decoded.bitwise_eq(&event));
}
#[test]
fn terminal_and_liveness_predicates_classify_the_family() {
for sample in daemon_events().unwrap() {
if sample.is_liveness() {
assert!(!sample.is_terminal());
}
}
assert!(
DaemonEvent::Exit {
graceful: true,
message: String::new()
}
.is_terminal()
);
}
#[test]
fn display_names_every_variant_without_panicking() {
for sample in daemon_events().unwrap() {
assert!(!sample.to_string().is_empty());
}
}
}