Skip to main content

moq_net/stats/
routes.rs

1//! What a label's moq-lite-07 sessions did on the route layer.
2
3use serde::{Deserialize, Serialize};
4use std::sync::atomic::{AtomicU64, Ordering};
5
6/// A kind enum, the readout counting each kind, and its atomic backing.
7macro_rules! kinds {
8	(
9		$(#[$kind_doc:meta])* $kind:ident,
10		$(#[$counts_doc:meta])* $counts:ident,
11		$atomics:ident,
12		{ $($(#[$doc:meta])* $variant:ident => $field:ident),+ $(,)? }
13	) => {
14		$(#[$kind_doc])*
15		#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16		#[non_exhaustive]
17		pub enum $kind {
18			$($(#[$doc])* $variant,)+
19		}
20
21		impl $kind {
22			/// Every currently defined kind, in display order.
23			pub const ALL: &'static [Self] = &[$(Self::$variant),+];
24
25			/// The stable stats and metrics name.
26			pub const fn as_str(self) -> &'static str {
27				match self {
28					$(Self::$variant => stringify!($field),)+
29				}
30			}
31		}
32
33		$(#[$counts_doc])*
34		#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
35		#[serde(default)]
36		#[non_exhaustive]
37		pub struct $counts {
38			$($(#[$doc])* pub $field: u64,)+
39		}
40
41		impl $counts {
42			/// The cumulative count for this kind.
43			pub fn count(&self, kind: $kind) -> u64 {
44				match kind {
45					$($kind::$variant => self.$field,)+
46				}
47			}
48
49			/// The sum over every kind.
50			pub fn total(&self) -> u64 {
51				0u64 $(.saturating_add(self.$field))+
52			}
53
54			/// Add every kind from another readout.
55			pub fn add(&mut self, other: Self) {
56				$(self.$field = self.$field.saturating_add(other.$field);)+
57			}
58
59			/// Whether any cumulative count went backwards.
60			pub fn regressed(&self, previous: &Self) -> bool {
61				false $(|| self.$field < previous.$field)+
62			}
63		}
64
65		#[derive(Debug, Default)]
66		pub(super) struct $atomics {
67			$($field: AtomicU64,)+
68		}
69
70		impl $atomics {
71			pub(super) fn add(&self, kind: $kind, count: u64) {
72				let counter = match kind {
73					$($kind::$variant => &self.$field,)+
74				};
75				counter.fetch_add(count, Ordering::Relaxed);
76			}
77
78			pub(super) fn snapshot(&self) -> $counts {
79				$counts {
80					$($field: self.$field.load(Ordering::Relaxed),)+
81				}
82			}
83		}
84	};
85}
86
87kinds!(
88	/// A message that withdraws routes.
89	Withdrawal,
90	/// Route withdrawals by kind.
91	Withdrawals,
92	WithdrawalCounters,
93	{
94		/// DEAD: a session between two relays closed, ending every route through it.
95		Dead => dead,
96		/// DRAIN: a relay sent GOAWAY, so every route through it ranks as draining.
97		Drain => drain,
98	}
99);
100
101kinds!(
102	/// Why this node refused a route or claim a peer forwarded, keeping the session.
103	RouteRefusal,
104	/// Refused routes and claims by reason.
105	RouteRefusals,
106	RouteRefusalCounters,
107	{
108		/// It names a path this node cannot hold.
109		Path => path,
110		/// It lies outside the session's root or grant, or what the origin serves.
111		Scope => scope,
112		/// Its way already crosses this relay, or more relays than the hop limit allows.
113		Looped => looped,
114	}
115);
116
117kinds!(
118	/// Why a session closed for its route layer.
119	RouteClose,
120	/// Sessions closed for their route layer, by kind.
121	RouteCloses,
122	RouteCloseCounters,
123	{
124		/// The peer stopped reading its route stream.
125		Stalled => stalled,
126		/// The peer read its route stream too slowly for what changed.
127		Overrun => overrun,
128		/// The peer sent a route, claim, request, or prefix the rule never builds, or broke
129		/// its route stream.
130		Malformed => malformed,
131		/// The peer sent what its role never sends, such as a routing peer asking for
132		/// part of the tree.
133		Role => role,
134		/// The peer's stream fell out of step: a claim outside what was asked, or under a
135		/// route it never offered.
136		Unasked => unasked,
137		/// Any other reason, such as the peer announcing past the session's cap.
138		Other => other,
139	}
140);
141
142/// What a label's moq-lite-07 sessions did on the route layer, cumulative.
143///
144/// Each count lands on the label of the session whose peer caused it, never per
145/// broadcast or per node, so its size stays flat as a mesh grows.
146#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
147#[serde(default)]
148#[non_exhaustive]
149pub struct Routes {
150	/// Times the routes to a relay changed what they give the ways to its claims, on what
151	/// the peer sent or how its session ended. A move to another path of the same rank
152	/// changes nothing, and a burst of messages changing one relay's routes before the
153	/// node acts on them counts once.
154	pub changed: u64,
155	/// Broadcasts rewritten into the origin behind those changes: every broadcast the
156	/// relay still claims as the node acts on the change, each once per pass. A route
157	/// that ended takes its claims with it, which are rewritten as claims ending instead.
158	pub rewritten: u64,
159	/// Withdrawals sent to the peer, by kind.
160	pub sent: Withdrawals,
161	/// Withdrawals received from the peer, by kind.
162	pub received: Withdrawals,
163	/// Routes and claims the peer forwarded that this node refused, keeping the session.
164	/// Their total is [`Presence::routes_refused`](super::Presence::routes_refused).
165	pub refused: RouteRefusals,
166	/// Sessions closed for their route layer, by kind.
167	pub closed: RouteCloses,
168}
169
170impl Routes {
171	/// Whether nothing has been counted.
172	pub(crate) fn is_zero(&self) -> bool {
173		*self == Self::default()
174	}
175
176	/// Add every counter from another readout.
177	pub fn add(&mut self, other: Self) {
178		self.changed = self.changed.saturating_add(other.changed);
179		self.rewritten = self.rewritten.saturating_add(other.rewritten);
180		self.sent.add(other.sent);
181		self.received.add(other.received);
182		self.refused.add(other.refused);
183		self.closed.add(other.closed);
184	}
185
186	/// Whether any cumulative count went backwards.
187	pub fn regressed(&self, previous: &Self) -> bool {
188		self.changed < previous.changed
189			|| self.rewritten < previous.rewritten
190			|| self.sent.regressed(&previous.sent)
191			|| self.received.regressed(&previous.received)
192			|| self.refused.regressed(&previous.refused)
193			|| self.closed.regressed(&previous.closed)
194	}
195}
196
197#[derive(Debug, Default)]
198pub(super) struct RouteCounters {
199	changed: AtomicU64,
200	rewritten: AtomicU64,
201	pub(super) sent: WithdrawalCounters,
202	pub(super) received: WithdrawalCounters,
203	pub(super) refused: RouteRefusalCounters,
204	pub(super) closed: RouteCloseCounters,
205}
206
207impl RouteCounters {
208	pub(super) fn changed(&self, rewritten: u64) {
209		self.changed.fetch_add(1, Ordering::Relaxed);
210		self.rewritten.fetch_add(rewritten, Ordering::Relaxed);
211	}
212
213	pub(super) fn snapshot(&self) -> Routes {
214		Routes {
215			changed: self.changed.load(Ordering::Relaxed),
216			rewritten: self.rewritten.load(Ordering::Relaxed),
217			sent: self.sent.snapshot(),
218			received: self.received.snapshot(),
219			refused: self.refused.snapshot(),
220			closed: self.closed.snapshot(),
221		}
222	}
223}