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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use super::*;
use futures_util::future::{select, Either};
use futures_util::stream::{FuturesUnordered, StreamExt};
use stop_token::future::FutureExt as _;
impl_veilid_log_facility!("rtab");
/// Number of consecutive successful pings required to consider a route tested
const ROUTE_TEST_PING_COUNT: usize = 3;
/////////////////////////////////////
// Priority Groups
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PingValidationGroup {
RouteTest = 0,
Keepalive = 1,
Reliability = 2,
}
impl PingValidationGroup {
fn max_parallel(&self) -> usize {
match self {
Self::RouteTest => 4,
Self::Keepalive => 4,
Self::Reliability => 16,
}
}
}
/////////////////////////////////////
// Ping Validation Entry Types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PingValidationKey {
Destination(Destination),
RouteTest {
route_id: RouteId,
ping_index: usize,
},
}
/// Callback invoked when a ping completes.
/// Receives the registry and the ping result.
/// Returns follow-up entries to re-enqueue (e.g., next ping in a route test chain).
pub type PingCompletionCallback = Box<
dyn FnOnce(
VeilidComponentRegistry,
Result<NetworkResult<Answer<StatusResult>>, RPCError>,
) -> Vec<PingValidationEntry>
+ Send
+ 'static,
>;
pub struct PingValidationEntry {
pub group: PingValidationGroup,
pub priority: usize, // 0 = highest
pub key: PingValidationKey,
pub dest: Destination,
pub on_complete: Option<PingCompletionCallback>,
}
/////////////////////////////////////
// Processor State (internal, not shared)
struct PingCompletionResult {
group: PingValidationGroup,
key: PingValidationKey,
result: Result<NetworkResult<Answer<StatusResult>>, RPCError>,
on_complete: Option<PingCompletionCallback>,
}
#[derive(Default)]
struct PingValidationProcessorState {
/// Per-group queued items, sorted by priority (BTreeMap key = priority number, 0 = highest)
groups: BTreeMap<PingValidationGroup, BTreeMap<usize, VecDeque<PingValidationEntry>>>,
/// Per-group in-flight counts
group_in_flight: BTreeMap<PingValidationGroup, usize>,
/// Keys currently queued (for deduplication)
queued_keys: HashSet<PingValidationKey>,
/// Keys currently in-flight (for deduplication)
in_process_keys: HashSet<PingValidationKey>,
}
impl PingValidationProcessorState {
fn new() -> Self {
Self::default()
}
/// Add an entry to the priority queue, deduplicating against queued and in-process keys
fn enqueue(&mut self, entry: PingValidationEntry) -> bool {
if self.in_process_keys.contains(&entry.key) || self.queued_keys.contains(&entry.key) {
return false;
}
self.queued_keys.insert(entry.key.clone());
self.groups
.entry(entry.group)
.or_default()
.entry(entry.priority)
.or_default()
.push_back(entry);
true
}
/// Start as much pending work as possible, respecting per-group parallelism limits
fn start_pending_work(
&mut self,
registry: &VeilidComponentRegistry,
in_flight: &mut FuturesUnordered<PinBoxFutureStatic<PingCompletionResult>>,
) {
// Iterate groups in order (RouteTest=0 first, then Keepalive=1, then Reliability=2)
let groups: Vec<PingValidationGroup> = self.groups.keys().copied().collect();
for group in groups {
let max_parallel = group.max_parallel();
let current_in_flight = *self.group_in_flight.entry(group).or_insert(0);
let available = max_parallel.saturating_sub(current_in_flight);
if available == 0 {
continue;
}
let Some(priority_map) = self.groups.get_mut(&group) else {
continue;
};
let mut started = 0usize;
// Take items from lowest priority number first (BTreeMap iterates in ascending order)
let priorities: Vec<usize> = priority_map.keys().copied().collect();
for priority in priorities {
if started >= available {
break;
}
let Some(queue) = priority_map.get_mut(&priority) else {
continue;
};
while started < available {
let Some(entry) = queue.pop_front() else {
break;
};
self.queued_keys.remove(&entry.key);
self.in_process_keys.insert(entry.key.clone());
let fut_group = entry.group;
let fut_key = entry.key.clone();
let fut_dest = entry.dest.clone();
let fut_on_complete = entry.on_complete;
let fut_registry = registry.clone();
in_flight.push(Box::pin(async move {
#[cfg(feature = "verbose-tracing")]
veilid_log!(fut_registry debug "--> validator ping ({:?}) to {:?}", fut_group, fut_dest);
let rpc_processor = fut_registry.rpc_processor();
let result =
Box::pin(rpc_processor.rpc_call_status(fut_dest)).await;
PingCompletionResult {
group: fut_group,
key: fut_key,
result,
on_complete: fut_on_complete,
}
}));
started += 1;
}
// Clean up empty queues
if queue.is_empty() {
priority_map.remove(&priority);
}
}
// Clean up empty priority maps
if priority_map.is_empty() {
self.groups.remove(&group);
}
*self.group_in_flight.entry(group).or_insert(0) += started;
}
}
/// Handle a completed ping: update tracking, invoke callback, re-enqueue follow-ups
fn handle_completion(
&mut self,
result: PingCompletionResult,
registry: &VeilidComponentRegistry,
) {
// Update tracking
self.in_process_keys.remove(&result.key);
if let Some(count) = self.group_in_flight.get_mut(&result.group) {
*count = count.saturating_sub(1);
}
// Invoke callback and enqueue follow-up entries
if let Some(callback) = result.on_complete {
let follow_ups = callback(registry.clone(), result.result);
for entry in follow_ups {
self.enqueue(entry);
}
}
}
/// Returns the total number of queued items
fn queued_count(&self) -> usize {
self.groups
.values()
.flat_map(|pm| pm.values())
.map(|q| q.len())
.sum()
}
/// Returns the total number of in-flight items
fn in_flight_count(&self) -> usize {
self.group_in_flight.values().sum()
}
}
/////////////////////////////////////
// Background Processor
impl RoutingTable {
/// Long-lived background processor for the ping validation queue.
/// Spawned at startup, processes items immediately as they are enqueued.
pub(crate) async fn ping_validation_processor(
registry: VeilidComponentRegistry,
stop_token: StopToken,
rx: flume::Receiver<Vec<PingValidationEntry>>,
) {
let mut state = PingValidationProcessorState::new();
let mut in_flight = FuturesUnordered::<PinBoxFutureStatic<PingCompletionResult>>::new();
loop {
// Start as much pending work as possible from the priority queue
state.start_pending_work(®istry, &mut in_flight);
if in_flight.is_empty() {
// Nothing in-flight: block on channel recv
match rx.recv_async().timeout_at(stop_token.clone()).await {
Ok(Ok(entries)) => {
for entry in entries {
state.enqueue(entry);
}
}
_ => break, // channel closed or stop token
}
} else {
// Stuff in-flight: race channel recv vs future completion,
// but also respect the stop token so shutdown isn't blocked
// waiting for in-flight RPC timeouts.
let recv_fut = rx.recv_async();
let next_fut = in_flight.next();
match select(recv_fut, next_fut)
.timeout_at(stop_token.clone())
.await
{
Ok(Either::Left((Ok(entries), _))) => {
// New entries received from channel
for entry in entries {
state.enqueue(entry);
}
}
Ok(Either::Left((Err(_), _))) => {
// Channel closed, abandon in-flight items
break;
}
Ok(Either::Right((Some(result), _))) => {
// A ping completed
state.handle_completion(result, ®istry);
}
Ok(Either::Right((None, _))) => {
// FuturesUnordered empty (shouldn't happen since we checked)
continue;
}
Err(_) => {
// Stop token fired, shut down immediately
break;
}
}
}
}
veilid_log!(registry debug "Ping validation processor stopped. {} queued, {} in-flight remaining.",
state.queued_count(), state.in_flight_count());
}
/////////////////////////////////////
// Enqueue APIs
/// Enqueue ping validation entries with full control (callbacks, groups, priorities)
pub fn enqueue_ping_validation_entries(&self, entries: Vec<PingValidationEntry>) {
if entries.is_empty() {
return;
}
if let Err(e) = self.ping_validation_sender.lock().send(entries) {
veilid_log!(self warn "Failed to enqueue ping validations: channel closed ({} entries dropped)", e.into_inner().len());
}
}
/// Convenience wrapper for simple destination pings (Destination key, no callback)
pub fn enqueue_ping_validations(
&self,
purpose: String,
group: PingValidationGroup,
priority: usize,
ping_validations: Vec<Destination>,
) {
if ping_validations.is_empty() {
return;
}
let entries: Vec<PingValidationEntry> = ping_validations
.into_iter()
.map(|dest| PingValidationEntry {
group,
priority,
key: PingValidationKey::Destination(dest.clone()),
dest,
on_complete: None,
})
.collect();
let count = entries.len();
veilid_log!(self debug "[{}]: Enqueuing {} pings (group={:?}, priority={})", purpose, count, group, priority);
self.enqueue_ping_validation_entries(entries);
}
/////////////////////////////////////
// Reliability Ping Validations
/// Ping each node in the routing table if they need to be pinged to determine their reliability
#[cfg_attr(feature = "instrument", instrument(level = "trace", skip(self), err))]
pub fn add_reliability_ping_validations(
&self,
cur_ts: Timestamp,
routing_domain: RoutingDomain,
) -> EyreResult<()> {
let priority_nodes = self.get_relability_ping_nodes(routing_domain, cur_ts);
let name = format!("Reliability({})", routing_domain);
for (priority, node_refs) in priority_nodes {
let mut validations = vec![];
for nr in node_refs {
let nr = nr.with_sequencing(Sequencing::PreferOrdered);
validations.push(Destination::direct(nr, None));
}
self.enqueue_ping_validations(
name.clone(),
PingValidationGroup::Reliability,
priority,
validations,
);
}
Ok(())
}
/////////////////////////////////////
// Route Test Integration
/// Enqueue route test pings for a set of routes.
/// Builds loopback destinations and creates callback chains for multi-ping testing.
pub async fn enqueue_route_tests(&self, route_ids: Vec<RouteId>, priority: usize) {
let rss = self.route_spec_store();
let mut entries = Vec::new();
for route_id in route_ids {
let is_remote = rss.is_route_id_remote(&route_id);
if is_remote {
// Remote route: single ping, simpler setup
match Self::build_remote_route_test_entry(rss, &route_id, priority) {
Some(entry) => entries.push(entry),
None => {
veilid_log!(self debug "Could not build remote route test for {}", route_id);
}
}
} else {
// Allocated route: multi-ping with callback chain
match Self::build_allocated_route_test_entry(rss, &route_id, priority).await {
Some(entry) => entries.push(entry),
None => {
veilid_log!(self debug "Could not build allocated route test for {}", route_id);
}
}
}
}
if !entries.is_empty() {
self.enqueue_ping_validation_entries(entries);
}
}
/// Build a PingValidationEntry for testing an allocated (local) route
async fn build_allocated_route_test_entry(
rss: &RouteSpecStore,
route_id: &RouteId,
priority: usize,
) -> Option<PingValidationEntry> {
// Get the best route key and hop refs
let Some(AllocatedRouteTestInfo { key, hops }) =
rss.get_allocated_route_test_info(route_id)
else {
// Route doesn't exist or has no best key
return None;
};
// Assemble the private route
let private_route = match rss.assemble_single_private_route(&key, None).await {
Ok(v) => v,
Err(VeilidAPIError::InvalidTarget { message: _ }) => {
// Route missing means it's dead
veilid_log!(rss debug "Route {} is dead (invalid target), releasing", route_id);
rss.release_route(route_id.clone());
return None;
}
Err(VeilidAPIError::TryAgain { message: _ }) => {
// Can't assemble right now, skip
return None;
}
Err(e) => {
veilid_log!(rss debug "Error assembling route {}: {}", route_id, e);
return None;
}
};
// Build the loopback destination
let safety_spec = SafetySpec {
preferred_route: Some(route_id.clone()),
hop_count: hops.len(),
stability: Stability::Reliable,
sequencing: Sequencing::PreferOrdered,
};
let safety_selection = SafetySelection::Safe(safety_spec);
let dest = Destination::PrivateRoute {
private_route,
safety_selection,
};
let route_id_clone = route_id.clone();
let dest_clone = dest.clone();
Some(PingValidationEntry {
group: PingValidationGroup::RouteTest,
priority,
key: PingValidationKey::RouteTest {
route_id: route_id.clone(),
ping_index: 0,
},
dest,
on_complete: Some(Self::make_route_test_callback(
route_id_clone,
dest_clone,
hops,
0,
ROUTE_TEST_PING_COUNT,
)),
})
}
/// Build a PingValidationEntry for testing a remote route
fn build_remote_route_test_entry(
rss: &RouteSpecStore,
route_id: &RouteId,
priority: usize,
) -> Option<PingValidationEntry> {
let private_route = rss.best_remote_private_route(route_id)?;
let safety_spec = SafetySpec {
preferred_route: None,
hop_count: rss.get_default_route_hop_count_safe(),
stability: Stability::Reliable,
sequencing: Sequencing::PreferOrdered,
};
let safety_selection = SafetySelection::Safe(safety_spec);
let dest = Destination::PrivateRoute {
private_route,
safety_selection,
};
let route_id_clone = route_id.clone();
// Remote routes: single ping, callback just handles success/failure
Some(PingValidationEntry {
group: PingValidationGroup::RouteTest,
priority,
key: PingValidationKey::RouteTest {
route_id: route_id.clone(),
ping_index: 0,
},
dest,
on_complete: Some(Box::new(
move |registry: VeilidComponentRegistry,
result: Result<NetworkResult<Answer<StatusResult>>, RPCError>|
-> Vec<PingValidationEntry> {
match result {
Ok(NetworkResult::Value(_)) => {
// Success - route is alive
veilid_log!(registry trace "Remote route test PASSED: {}", route_id_clone);
}
Ok(_) => {
// Network result but not a value - route failed
veilid_log!(registry debug "Remote route test failed (no response): {}", route_id_clone);
let routing_table = registry.routing_table();
let rss = routing_table.route_spec_store();
rss.release_route(route_id_clone);
}
Err(e) => {
veilid_log!(registry debug "Remote route test error: {} - {}", route_id_clone, e);
}
}
Vec::new()
},
)),
})
}
/// Create a callback for the route test chain.
/// On success with more pings needed: returns next entry in the chain.
/// On failure: reports failed route test on hop nodes, releases route.
/// On final success: returns empty (RPC processor already set last_known_valid_ts).
fn make_route_test_callback(
route_id: RouteId,
dest: Destination,
hops: Vec<NodeRef>,
ping_index: usize,
total_pings: usize,
) -> PingCompletionCallback {
Box::new(
move |registry: VeilidComponentRegistry,
result: Result<NetworkResult<Answer<StatusResult>>, RPCError>|
-> Vec<PingValidationEntry> {
match result {
Ok(NetworkResult::Value(_)) => {
// Ping succeeded
let next_index = ping_index + 1;
veilid_log!(registry trace "Route test ping {}/{} succeeded: {}", ping_index + 1, total_pings, route_id);
if next_index < total_pings {
// More pings needed - return next entry in chain
vec![PingValidationEntry {
group: PingValidationGroup::RouteTest,
priority: 0, // Keep highest priority for chain continuation
key: PingValidationKey::RouteTest {
route_id: route_id.clone(),
ping_index: next_index,
},
dest: dest.clone(),
on_complete: Some(Self::make_route_test_callback(
route_id,
dest,
hops,
next_index,
total_pings,
)),
}]
} else {
// All pings passed - route is verified
// Note: last_known_valid_ts is already set by record_answer_received
// in the RPC processor on the first successful response
veilid_log!(registry trace "Route test PASSED ({} pings): {}", total_pings, route_id);
Vec::new()
}
}
Ok(_) => {
// Network result but not a value - route failed
veilid_log!(registry debug "Route test failed at ping {}/{}: {} (no response)",
ping_index + 1, total_pings, route_id);
// Report failed route test on hop nodes
for hop in &hops {
hop.report_failed_route_test();
}
// Release the dead route
let routing_table = registry.routing_table();
let rss = routing_table.route_spec_store();
rss.release_route(route_id);
Vec::new()
}
Err(e) => {
veilid_log!(registry debug "Route test error at ping {}/{}: {} - {}",
ping_index + 1, total_pings, route_id, e);
// Don't release on RPC error (could be transient)
Vec::new()
}
}
},
)
}
}