net-mesh 0.21.0

High-performance, schema-agnostic, backend-agnostic event bus
Documentation
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
//! Source converters — adapt subsystem-native signals into
//! [`super::event::MeshOsEvent`] and publish through a
//! [`super::event_loop::MeshOsHandle`].
//!
//! Locked decision #1: existing reactors become *event
//! sources*, not independent reactors. This module is where
//! each subsystem's signal stream attaches to the canonical
//! event loop.
//!
//! First converter: [`MeshOsDaemonLifecycleSink`] — adapts the
//! [`crate::adapter::net::compute::DaemonLifecycleObserver`]
//! trait to MeshOS event publishing. Install via
//! `DaemonRegistry::set_lifecycle_observer(sink)`; the
//! registry's register / replace / unregister paths fire
//! through it; the sink translates each
//! [`DaemonLifecycleEvent`] to the matching
//! [`super::event::MeshOsEvent::DaemonLifecycle`] and pushes
//! it onto the loop's channel.
//!
//! On `try_publish` failure (queue full / loop closed), the
//! sink either drops the event or records to a leaky counter
//! — never blocks, never panics. The drop counter is sampled
//! via [`MeshOsDaemonLifecycleSink::dropped_count`] for
//! diagnostics.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use crate::adapter::net::compute::{DaemonLifecycleEvent, DaemonLifecycleObserver};
use crate::adapter::net::redex::{ReplicaTransitionEvent, ReplicaTransitionObserver};

use super::event::{DaemonLifecycleSignal, DaemonRef, MeshOsEvent, NodeId, ReplicaUpdate};
use super::event_loop::{MeshOsHandle, MeshOsHandleError};

/// Adapts compute-side lifecycle events into MeshOS events. Hold
/// behind `Arc` (the trait is `Send + Sync + 'static`); install
/// on `DaemonRegistry` via `set_lifecycle_observer`.
#[derive(Debug)]
pub struct MeshOsDaemonLifecycleSink {
    handle: MeshOsHandle,
    dropped: AtomicU64,
}

impl MeshOsDaemonLifecycleSink {
    /// Construct a sink that publishes to `handle`. Cheap —
    /// just clones the handle's sender.
    pub fn new(handle: MeshOsHandle) -> Self {
        Self {
            handle,
            dropped: AtomicU64::new(0),
        }
    }

    /// Total events the sink couldn't publish (queue full or
    /// loop closed). Increments on every drop; reads via
    /// `Relaxed`.
    pub fn dropped_count(&self) -> u64 {
        self.dropped.load(Ordering::Relaxed)
    }
}

impl DaemonLifecycleObserver for MeshOsDaemonLifecycleSink {
    fn observe(&self, event: DaemonLifecycleEvent) {
        let mesh_event = match event {
            DaemonLifecycleEvent::Registered { id, name, at } => MeshOsEvent::DaemonLifecycle {
                daemon: DaemonRef { id, name },
                signal: DaemonLifecycleSignal::Started { at },
            },
            DaemonLifecycleEvent::Unregistered { id, name, at } => MeshOsEvent::DaemonLifecycle {
                daemon: DaemonRef { id, name },
                signal: DaemonLifecycleSignal::ExitedCleanly { at },
            },
            DaemonLifecycleEvent::Crashed {
                id,
                name,
                at,
                reason,
            } => MeshOsEvent::DaemonLifecycle {
                daemon: DaemonRef { id, name },
                signal: DaemonLifecycleSignal::Crashed { at, reason },
            },
            DaemonLifecycleEvent::HealthChanged {
                id,
                name,
                at,
                health,
            } => MeshOsEvent::DaemonLifecycle {
                daemon: DaemonRef { id, name },
                signal: DaemonLifecycleSignal::HealthChanged { at, health },
            },
            DaemonLifecycleEvent::SaturationChanged {
                id,
                name,
                at,
                saturation,
            } => MeshOsEvent::DaemonLifecycle {
                daemon: DaemonRef { id, name },
                signal: DaemonLifecycleSignal::SaturationChanged { at, saturation },
            },
        };

        match self.handle.try_publish(mesh_event) {
            Ok(()) => {}
            Err(MeshOsHandleError::QueueFull) | Err(MeshOsHandleError::LoopClosed) => {
                self.dropped.fetch_add(1, Ordering::Relaxed);
            }
        }
    }
}

/// Adapts replication-coordinator transitions into MeshOS
/// events. Hold behind `Arc`; install on each `ReplicationCoordinator`
/// via `set_transition_observer`. Same drop-on-overflow posture
/// as [`MeshOsDaemonLifecycleSink`].
///
/// `this_node` is the local node's identity; the sink fills it
/// into every emitted `ReplicaUpdate::{Added, Removed}` so
/// MeshOS reconcile sees consistent holder identities.
#[derive(Debug)]
pub struct MeshOsReplicaTransitionSink {
    handle: MeshOsHandle,
    this_node: NodeId,
    dropped: AtomicU64,
}

impl MeshOsReplicaTransitionSink {
    /// Construct a sink publishing to `handle`. `this_node` is
    /// the local node's id, filled into emitted
    /// `ReplicaUpdate { holder: this_node }` payloads.
    pub fn new(handle: MeshOsHandle, this_node: NodeId) -> Self {
        Self {
            handle,
            this_node,
            dropped: AtomicU64::new(0),
        }
    }

    /// Total events the sink couldn't publish.
    pub fn dropped_count(&self) -> u64 {
        self.dropped.load(Ordering::Relaxed)
    }
}

impl ReplicaTransitionObserver for MeshOsReplicaTransitionSink {
    fn observe(&self, event: ReplicaTransitionEvent) {
        let mesh_event = match event {
            ReplicaTransitionEvent::BecameHolder { origin_hash, .. } => {
                MeshOsEvent::ReplicaUpdate(ReplicaUpdate::Added {
                    chain: origin_hash,
                    holder: self.this_node,
                })
            }
            ReplicaTransitionEvent::Idled { origin_hash, .. } => {
                MeshOsEvent::ReplicaUpdate(ReplicaUpdate::Removed {
                    chain: origin_hash,
                    holder: self.this_node,
                })
            }
            ReplicaTransitionEvent::LeaderChanged { origin_hash, .. } => {
                MeshOsEvent::ReplicaLeaderUpdate {
                    chain: origin_hash,
                    leader: Some(self.this_node),
                }
            }
            ReplicaTransitionEvent::LeaderLost { origin_hash, .. } => {
                MeshOsEvent::ReplicaLeaderUpdate {
                    chain: origin_hash,
                    leader: None,
                }
            }
            ReplicaTransitionEvent::LeaderLostAndIdled { origin_hash, .. } => {
                // Atomic pair: bundled into one MeshOsEvent so a
                // queue-full drop can't split it into a phantom
                // leader + holderless set.
                MeshOsEvent::ReplicaLeaderLostAndRemoved {
                    chain: origin_hash,
                    holder: self.this_node,
                }
            }
            ReplicaTransitionEvent::BecameHolderAndLeader { origin_hash, .. } => {
                // Symmetric atomic pair on the promotion side.
                MeshOsEvent::ReplicaBecameHolderAndLeader {
                    chain: origin_hash,
                    holder: self.this_node,
                }
            }
        };

        match self.handle.try_publish(mesh_event) {
            Ok(()) => {}
            Err(MeshOsHandleError::QueueFull) | Err(MeshOsHandleError::LoopClosed) => {
                self.dropped.fetch_add(1, Ordering::Relaxed);
            }
        }
    }
}

/// One-line wire-up: install the replica-transition sink on a
/// `ReplicationCoordinator`. Returns the prior observer, if
/// any. `this_node` rides into every emitted `ReplicaUpdate`.
pub fn attach_to_replication_coordinator(
    coord: &crate::adapter::net::redex::ReplicationCoordinator,
    handle: MeshOsHandle,
    this_node: NodeId,
) -> Option<Arc<dyn ReplicaTransitionObserver>> {
    coord.set_transition_observer(Some(Arc::new(MeshOsReplicaTransitionSink::new(
        handle, this_node,
    ))))
}

/// Helper: install the sink on a `DaemonRegistry` in one call.
/// Returns the prior observer (if any), matching
/// `DaemonRegistry::set_lifecycle_observer`'s return type.
///
/// Lives here (not on the registry) so the wiring concern
/// stays in the meshos module — the registry doesn't need to
/// know about MeshOS specifics.
pub fn attach_to_daemon_registry(
    registry: &crate::adapter::net::compute::DaemonRegistry,
    handle: MeshOsHandle,
) -> Option<Arc<dyn DaemonLifecycleObserver>> {
    registry.set_lifecycle_observer(Some(Arc::new(MeshOsDaemonLifecycleSink::new(handle))))
}

#[cfg(test)]
mod tests {
    use std::time::Instant;

    use super::super::config::MeshOsConfig;
    use super::super::event_loop::{MeshOsLoop, MeshOsLoopParts};
    use super::*;
    use crate::adapter::net::compute::DaemonHealth;

    fn fast_cfg() -> MeshOsConfig {
        MeshOsConfig {
            this_node: 1,
            tick_interval: std::time::Duration::from_millis(10),
            event_queue_capacity: 8,
            action_queue_capacity: 8,
            backpressure: Default::default(),
            locality: Default::default(),
            maintenance: Default::default(),
            scheduler: Default::default(),
        }
    }

    #[tokio::test]
    async fn registered_event_publishes_started_signal() {
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsDaemonLifecycleSink::new(handle.clone());
        let task = tokio::spawn(loop_.run());

        sink.observe(DaemonLifecycleEvent::Registered {
            id: 42,
            name: "telemetry".into(),
            at: Instant::now(),
        });

        // Shut down to harvest.
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;

        assert_eq!(sink.dropped_count(), 0);
    }

    #[tokio::test]
    async fn dropped_count_increments_when_loop_is_closed() {
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsDaemonLifecycleSink::new(handle.clone());
        // Tear the loop down first.
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = loop_.run().await;
        drop(handle);

        // Publishing into the dead handle now drops.
        sink.observe(DaemonLifecycleEvent::Crashed {
            id: 1,
            name: "telemetry".into(),
            at: Instant::now(),
            reason: "test".into(),
        });
        assert_eq!(sink.dropped_count(), 1);
    }

    #[tokio::test]
    async fn all_lifecycle_variants_translate_to_daemon_lifecycle_event() {
        // Compile-side coverage: every DaemonLifecycleEvent
        // variant maps to a MeshOsEvent::DaemonLifecycle with
        // the matching signal arm. Run all five and confirm
        // dropped_count stays at 0 (i.e. every publish
        // succeeded).
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsDaemonLifecycleSink::new(handle.clone());
        let task = tokio::spawn(loop_.run());

        let now = Instant::now();
        sink.observe(DaemonLifecycleEvent::Registered {
            id: 1,
            name: "a".into(),
            at: now,
        });
        sink.observe(DaemonLifecycleEvent::Unregistered {
            id: 1,
            name: "a".into(),
            at: now,
        });
        sink.observe(DaemonLifecycleEvent::Crashed {
            id: 1,
            name: "a".into(),
            at: now,
            reason: "x".into(),
        });
        sink.observe(DaemonLifecycleEvent::HealthChanged {
            id: 1,
            name: "a".into(),
            at: now,
            health: DaemonHealth::Healthy,
        });
        sink.observe(DaemonLifecycleEvent::SaturationChanged {
            id: 1,
            name: "a".into(),
            at: now,
            saturation: 0.5,
        });

        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;

        assert_eq!(sink.dropped_count(), 0);
    }

    #[tokio::test]
    async fn became_holder_event_publishes_replica_added() {
        const THIS_NODE: NodeId = 100;
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsReplicaTransitionSink::new(handle.clone(), THIS_NODE);
        let task = tokio::spawn(loop_.run());

        sink.observe(ReplicaTransitionEvent::BecameHolder {
            origin_hash: 0xCAFE,
            at: Instant::now(),
        });

        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;
        assert_eq!(sink.dropped_count(), 0);
    }

    #[tokio::test]
    async fn idled_event_publishes_replica_removed() {
        const THIS_NODE: NodeId = 100;
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsReplicaTransitionSink::new(handle.clone(), THIS_NODE);
        let task = tokio::spawn(loop_.run());

        sink.observe(ReplicaTransitionEvent::Idled {
            origin_hash: 0xBEEF,
            at: Instant::now(),
        });

        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;
        assert_eq!(sink.dropped_count(), 0);
    }

    #[tokio::test]
    async fn leader_changed_event_publishes_replica_leader_update() {
        const THIS_NODE: NodeId = 100;
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsReplicaTransitionSink::new(handle.clone(), THIS_NODE);
        let task = tokio::spawn(loop_.run());

        sink.observe(ReplicaTransitionEvent::LeaderChanged {
            origin_hash: 0xC0FFEE,
            at: Instant::now(),
        });

        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;
        assert_eq!(sink.dropped_count(), 0);
    }

    #[tokio::test]
    async fn leader_lost_event_clears_replica_leader_via_none_update() {
        // When this coordinator steps down, the sink must surface
        // a `ReplicaLeaderUpdate { leader: None }` so MeshOS
        // clears its mirror — otherwise the loop carries a stale
        // `replica_leader[chain] = THIS_NODE` until a different
        // node's LeaderChanged overwrites it.
        const THIS_NODE: NodeId = 100;
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsReplicaTransitionSink::new(handle.clone(), THIS_NODE);
        let task = tokio::spawn(loop_.run());
        // Seed the holders set first so the snapshot surfaces a
        // ReplicaSnapshot for the chain; otherwise an absent
        // entry could mask a regression where the fold drops the
        // LeaderLost translation.
        sink.observe(ReplicaTransitionEvent::BecameHolder {
            origin_hash: 0xBADC0DE,
            at: Instant::now(),
        });
        // Promote — leader is now Some(THIS_NODE).
        sink.observe(ReplicaTransitionEvent::LeaderChanged {
            origin_hash: 0xBADC0DE,
            at: Instant::now(),
        });
        // Step down — should publish leader: None.
        sink.observe(ReplicaTransitionEvent::LeaderLost {
            origin_hash: 0xBADC0DE,
            at: Instant::now(),
        });
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;
        let snap = reader.read();
        // Holders set has THIS_NODE; leader has been cleared.
        let entry = snap
            .replicas
            .get(&0xBADC0DE)
            .expect("replicas entry exists after BecameHolder");
        assert_eq!(entry.leader, None, "LeaderLost should clear leader");
    }

    #[tokio::test]
    async fn leader_to_idle_lands_as_one_event_clearing_leader_and_removing_holder() {
        // Regression: a Leader → Idle transition used to fire two
        // separate events (Idled + LeaderLost). If the channel
        // were full one could drop, leaving the snapshot with
        // either a phantom leader or a leader-less holder set.
        // The substrate now fires `LeaderLostAndIdled` as one
        // event; the sink translates it to one bundled
        // `MeshOsEvent::ReplicaLeaderLostAndRemoved`.
        const THIS_NODE: NodeId = 100;
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsReplicaTransitionSink::new(handle.clone(), THIS_NODE);
        let task = tokio::spawn(loop_.run());
        // Seed: become a holder + leader.
        sink.observe(ReplicaTransitionEvent::BecameHolder {
            origin_hash: 0xDEADBEEF,
            at: Instant::now(),
        });
        sink.observe(ReplicaTransitionEvent::LeaderChanged {
            origin_hash: 0xDEADBEEF,
            at: Instant::now(),
        });
        // Step down all the way to Idle — bundled event.
        sink.observe(ReplicaTransitionEvent::LeaderLostAndIdled {
            origin_hash: 0xDEADBEEF,
            at: Instant::now(),
        });
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;
        let snap = reader.read();
        // Either the chain entry is absent (holder set empty —
        // both holder removal and leader clear succeeded), or
        // the entry has no holders AND no leader. A "phantom
        // leader on no holder" or "holder without leader" is the
        // exact regression we're guarding against.
        if let Some(entry) = snap.replicas.get(&0xDEADBEEF) {
            assert!(
                !entry.holders.contains(&THIS_NODE),
                "after LeaderLostAndIdled, THIS_NODE must not be in holders; got {:?}",
                entry,
            );
            assert_eq!(
                entry.leader, None,
                "after LeaderLostAndIdled, leader must be cleared; got {:?}",
                entry,
            );
        }
    }

    #[tokio::test]
    async fn replica_sink_drops_increment_when_loop_is_closed() {
        const THIS_NODE: NodeId = 100;
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let sink = MeshOsReplicaTransitionSink::new(handle.clone(), THIS_NODE);
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = loop_.run().await;
        drop(handle);
        sink.observe(ReplicaTransitionEvent::BecameHolder {
            origin_hash: 1,
            at: Instant::now(),
        });
        assert_eq!(sink.dropped_count(), 1);
    }

    #[tokio::test]
    async fn attach_to_daemon_registry_installs_the_observer() {
        use crate::adapter::net::compute::DaemonRegistry;
        let MeshOsLoopParts {
            mesh_loop: loop_,
            handle,
            actions_rx: _,
            reader: _,
        } = MeshOsLoop::new(fast_cfg());
        let registry = DaemonRegistry::new();
        // Consume the loop into a task so the handle's
        // mpsc receiver stays live for the observer-install
        // path (try_publish would surface a closed channel
        // otherwise).
        let task = tokio::spawn(loop_.run());
        assert!(!registry.has_lifecycle_observer());
        let _prior = attach_to_daemon_registry(&registry, handle.clone());
        assert!(registry.has_lifecycle_observer());
        handle.publish(MeshOsEvent::Shutdown).await.unwrap();
        let _ = task.await;
    }
}