Skip to main content

nodedb_cluster/subsystem/
context.rs

1// SPDX-License-Identifier: BUSL-1.1
2
3//! `BootstrapCtx` — shared context passed to every subsystem at start time.
4//!
5//! This struct is a plain value-bag of `Arc`-wrapped cluster components.
6//! Subsystems read from it during `start()` to obtain the handles they need.
7//! Nothing is spawned from here.
8
9use std::sync::{Arc, Mutex, RwLock};
10
11use crate::multi_raft::MultiRaft;
12use crate::routing::RoutingTable;
13use crate::topology::ClusterTopology;
14use crate::transport::NexarTransport;
15
16use super::health::ClusterHealth;
17
18/// Context available to every subsystem at startup.
19///
20/// All fields are `Arc`-wrapped so they can be cheaply cloned into
21/// long-lived subsystem tasks without requiring `BootstrapCtx` itself
22/// to be `'static`.
23///
24/// `topology` and `routing` are behind `RwLock` so subsystems that
25/// receive live state updates (e.g. `DecommissionObserver`,
26/// `RoutingLivenessHook`) can observe mutations without copying the
27/// full snapshot on every tick.
28///
29/// `multi_raft` is behind a `Mutex` because `MultiRaft::tick` takes
30/// `&mut self` — the same type used by `MigrationExecutor`.
31pub struct BootstrapCtx {
32    /// Cluster topology — current node/group membership view.
33    pub topology: Arc<RwLock<ClusterTopology>>,
34
35    /// Routing table — maps vShards to Raft groups and peer addresses.
36    pub routing: Arc<RwLock<RoutingTable>>,
37
38    /// QUIC transport layer — used by subsystems that need to send RPCs
39    /// to peers (e.g. SWIM, decommission coordinator).
40    pub transport: Arc<NexarTransport>,
41
42    /// Multi-Raft handle — subsystems that need to propose or read log
43    /// entries obtain a reference here.
44    pub multi_raft: Arc<Mutex<MultiRaft>>,
45
46    /// Shared health aggregator — subsystems write their own health state
47    /// here so the registry and monitoring layer can observe it.
48    pub health: ClusterHealth,
49}
50
51impl BootstrapCtx {
52    /// Construct a new context from its component parts.
53    pub fn new(
54        topology: Arc<RwLock<ClusterTopology>>,
55        routing: Arc<RwLock<RoutingTable>>,
56        transport: Arc<NexarTransport>,
57        multi_raft: Arc<Mutex<MultiRaft>>,
58        health: ClusterHealth,
59    ) -> Self {
60        Self {
61            topology,
62            routing,
63            transport,
64            multi_raft,
65            health,
66        }
67    }
68}