1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use super::*;
impl_veilid_log_facility!("net");
/// Distinct previously-live targets that must fail on the stream framing type, with no
/// intervening success, for the local node to be considered offline.
pub const OFFLINE_DETECTION_FAILURE_THRESHOLD_CONNECTION: usize = 4;
/// Same, but for the datagram socket class.
pub const OFFLINE_DETECTION_FAILURE_THRESHOLD_MESSAGE: usize = 4;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub enum OnlineState {
#[default]
Online,
Offline,
}
/// Failure count threshold for a socket class.
fn failure_threshold(framing_type: FramingType) -> usize {
match framing_type {
FramingType::Connection => OFFLINE_DETECTION_FAILURE_THRESHOLD_CONNECTION,
FramingType::Message => OFFLINE_DETECTION_FAILURE_THRESHOLD_MESSAGE,
}
}
/// Events that drive the online/offline state machine.
enum OnlineEvent {
/// Received something from outside.
Success,
/// Failures for previously-live nodes on one framing type.
Failure {
live_node_ids: Vec<NodeId>,
sent_ts: Timestamp,
framing_type: FramingType,
},
/// The local network address changed.
NetworkChange,
/// The node detached from the network (a deliberate offline period).
Detached,
}
/// Per-routing-domain online/offline tracking.
#[derive(Debug)]
struct OnlineDetectionPerDomain {
/// Previously-live nodes that failed on each framing type since the last success.
pending_failures: BTreeMap<FramingType, BTreeSet<NodeId>>,
/// When we last switched online/offline.
last_transition_ts: Timestamp,
/// Current state.
online_state: OnlineState,
/// Flap detector over online_state.
flap_detector: FlapDetector<OnlineState>,
/// Start of the current offline period, if currently offline.
offline_started_ts: Option<Timestamp>,
/// Recently-completed offline intervals (start, end), pruned to
/// `RELIABLE_PING_INTERVAL_MAX` old. Credits node reliability for time we
/// couldn't test.
recent_offline_intervals: VecDeque<(Timestamp, Timestamp)>,
}
impl Default for OnlineDetectionPerDomain {
fn default() -> Self {
Self {
pending_failures: BTreeMap::new(),
last_transition_ts: Timestamp::new(0),
online_state: OnlineState::default(),
// 4 online/offline flips within ~60s = flapping
flap_detector: FlapDetector::new_secs(4.0, 60),
offline_started_ts: None,
recent_offline_intervals: VecDeque::new(),
}
}
}
/// Detects if our node is online or offline.
///
/// In order to determine if network errors are 'our fault 'or 'the other side's fault', we need to
/// generally know if we are online or offline, before assigning blame to another node and changing
/// its state. This
pub struct OnlineDetector {
registry: VeilidComponentRegistry,
inner: Mutex<BTreeMap<RoutingDomain, OnlineDetectionPerDomain>>,
/// Framing types the network uses; only these gate offline detection.
enabled_framing_types: Mutex<FramingTypeSet>,
}
impl_veilid_component_accessors!(OnlineDetector);
impl fmt::Debug for OnlineDetector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OnlineDetector")
.field("inner", &*self.inner.lock())
.field("enabled_framing_types", &*self.enabled_framing_types.lock())
.finish()
}
}
impl OnlineDetector {
pub fn new(registry: VeilidComponentRegistry) -> Self {
Self {
registry,
inner: Mutex::new(BTreeMap::new()),
// Require both classes until the network reports its protocol config.
enabled_framing_types: Mutex::new(FramingTypeSet::all()),
}
}
/// Whether this routing domain uses ping-based online detection.
fn uses_ping_detection(routing_domain: RoutingDomain) -> bool {
// Other domains will plug in their own signals (LocalNetwork: interface link state).
matches!(routing_domain, RoutingDomain::PublicInternet)
}
/// Set which framing types offline detection requires, from the network protocol config.
pub fn set_protocol_config(&self, outbound: ProtocolTypeSet, inbound: ProtocolTypeSet) {
let mut framing_types = FramingTypeSet::new();
for pt in outbound | inbound {
framing_types.insert(pt.framing_type());
}
*self.enabled_framing_types.lock() = framing_types;
}
/// Record a successful round-trip.
pub fn record_success(&self, routing_domain: RoutingDomain) {
self.process_event(routing_domain, OnlineEvent::Success);
}
/// Record RPC failures against the attempted nodes (direct target, or routed hops).
pub fn record_failure(
&self,
routing_domain: RoutingDomain,
node_ids: impl IntoIterator<Item = NodeId>,
sent_ts: Timestamp,
transport: TransportType,
) {
if !Self::uses_ping_detection(routing_domain) {
return;
}
// Only previously-live nodes count: a dead node we probe timing out isn't evidence
// that *we* are offline. Read liveness now, before the caller's lost-answer /
// failed-to-send handler clears first_steady_answer_ts.
let routing_table = self.routing_table();
let live_node_ids: Vec<NodeId> = node_ids
.into_iter()
.filter(|node_id| {
routing_table
.lookup_node_id(node_id.clone())
.ok()
.flatten()
.is_some_and(|nr| nr.rpc_stats().first_steady_answer_ts.is_some())
})
.collect();
if live_node_ids.is_empty() {
return;
}
self.process_event(
routing_domain,
OnlineEvent::Failure {
live_node_ids,
sent_ts,
framing_type: transport.framing_type(),
},
);
}
/// Local network addresses changed: presume offline until a new RPC succeeds.
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), expect(dead_code))]
pub fn network_address_change(&self, routing_domain: RoutingDomain) {
self.process_event(routing_domain, OnlineEvent::NetworkChange);
}
/// Re-arm for a new attach; online/offline history persists across detach.
pub fn startup(&self) {
*self.enabled_framing_types.lock() = FramingTypeSet::all();
}
/// Detached: mark every ping-detected domain offline so the detached period
/// is credited to node reliability until we reconnect.
pub fn shutdown(&self) {
for routing_domain in RoutingDomain::all() {
self.process_event(routing_domain, OnlineEvent::Detached);
}
}
/// Current online state for a routing domain.
pub fn online_state(&self, routing_domain: RoutingDomain) -> OnlineState {
// Domains without ping detection are always considered online.
if !Self::uses_ping_detection(routing_domain) {
return OnlineState::Online;
}
self.inner
.lock()
.get(&routing_domain)
.map(|pd| pd.online_state)
.unwrap_or_default()
}
/// When this domain last came back online, if within the reliable ping span.
/// None if currently offline or no recent reattach.
pub fn online_since(&self, routing_domain: RoutingDomain) -> Option<Timestamp> {
if !Self::uses_ping_detection(routing_domain) {
return None;
}
let inner = self.inner.lock();
let pd = inner.get(&routing_domain)?;
if pd.offline_started_ts.is_some() {
return None;
}
let (_, end) = pd.recent_offline_intervals.back()?;
// Protection from a reattach only lasts the reliable ping span.
if Timestamp::now().duration_since(*end) > crate::routing_table::RELIABLE_PING_INTERVAL_MAX
{
return None;
}
Some(*end)
}
/// Total time within `[from, to]` this domain was offline (couldn't ping).
pub fn offline_overlap(
&self,
routing_domain: RoutingDomain,
from: Timestamp,
to: Timestamp,
) -> TimestampDuration {
if !Self::uses_ping_detection(routing_domain) || to <= from {
return TimestampDuration::new(0);
}
let inner = self.inner.lock();
let Some(pd) = inner.get(&routing_domain) else {
return TimestampDuration::new(0);
};
let mut overlap = 0u64;
// Completed intervals plus any ongoing offline period extended to `to`.
for (s, e) in pd
.recent_offline_intervals
.iter()
.copied()
.chain(pd.offline_started_ts.map(|s| (s, to)))
{
let lo = s.max(from);
let hi = e.min(to);
if hi > lo {
overlap += hi.duration_since(lo).as_u64();
}
}
TimestampDuration::new(overlap)
}
/// Drive the state machine with an event, dispatching to the current state's handler.
fn process_event(&self, routing_domain: RoutingDomain, event: OnlineEvent) {
if !Self::uses_ping_detection(routing_domain) {
return;
}
let enabled_framing_types = *self.enabled_framing_types.lock();
let now = Timestamp::now();
let (opt_new_state, opt_flap_penalty) = {
let mut inner = self.inner.lock();
let pd = inner.entry(routing_domain).or_default();
let opt_new_state = match pd.online_state {
OnlineState::Offline => Self::process_offline_event(&event),
OnlineState::Online => {
Self::process_online_event(pd, &event, enabled_framing_types)
}
};
let mut opt_flap_penalty = None;
if let Some(new_state) = opt_new_state {
match new_state {
OnlineState::Offline => pd.offline_started_ts = Some(now),
OnlineState::Online => {
if let Some(start) = pd.offline_started_ts.take() {
pd.recent_offline_intervals.push_back((start, now));
// Prune intervals older than the reliable ping span.
while pd.recent_offline_intervals.front().is_some_and(|(_, e)| {
now.duration_since(*e)
> crate::routing_table::RELIABLE_PING_INTERVAL_MAX
}) {
pd.recent_offline_intervals.pop_front();
}
}
}
}
pd.online_state = new_state;
pd.last_transition_ts = now;
// A deliberate detach is a clean break, not network flapping: reset the
// flap baseline so pre-detach history doesn't combine with reattach turbulence.
if matches!(event, OnlineEvent::Detached) {
pd.flap_detector.reset();
} else {
opt_flap_penalty = pd.flap_detector.record(now.as_u64(), new_state);
}
}
(opt_new_state, opt_flap_penalty)
};
// Log the transition (and any flapping) outside the lock.
if let Some(new_state) = opt_new_state {
veilid_log!(self info "{:?} {} detected", routing_domain,
if new_state == OnlineState::Online { "online" } else { "offline" });
}
if let Some(penalty) = opt_flap_penalty {
veilid_log!(self debugwarn "{:?} online state FLAPPING (penalty={:.1})", routing_domain, penalty);
}
}
/// Handle an event while offline.
fn process_offline_event(event: &OnlineEvent) -> Option<OnlineState> {
match event {
// Any success means we're back online.
OnlineEvent::Success => Some(OnlineState::Online),
OnlineEvent::Failure { .. } | OnlineEvent::NetworkChange | OnlineEvent::Detached => {
None
}
}
}
/// Handle an event while online.
fn process_online_event(
pd: &mut OnlineDetectionPerDomain,
event: &OnlineEvent,
enabled_framing_types: FramingTypeSet,
) -> Option<OnlineState> {
match event {
OnlineEvent::Success => {
pd.pending_failures.clear();
None
}
OnlineEvent::Failure {
live_node_ids,
sent_ts,
framing_type,
} => {
// Ignore failures from RPCs sent before this online period began.
if *sent_ts < pd.last_transition_ts {
return None;
}
let bucket = pd.pending_failures.entry(*framing_type).or_default();
for node_id in live_node_ids {
bucket.insert(node_id.clone());
}
// Offline only when every enabled socket class has crossed its threshold, so
// localized trouble on one class doesn't drive offline while another works.
let all_failed = !enabled_framing_types.is_empty()
&& enabled_framing_types.iter().all(|ft| {
pd.pending_failures.get(&ft).map_or(0, BTreeSet::len)
>= failure_threshold(ft)
});
if all_failed {
pd.pending_failures.clear();
return Some(OnlineState::Offline);
}
None
}
OnlineEvent::NetworkChange | OnlineEvent::Detached => {
pd.pending_failures.clear();
Some(OnlineState::Offline)
}
}
}
}