omni_orchestrator/
state.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4
5/// Represents the shared state of a node in the OmniOrchestrator cluster.
6///
7/// This structure contains essential information about the current node's role and
8/// position within the distributed cluster. It's designed to be thread-safe and
9/// is typically wrapped in synchronization primitives for concurrent access.
10///
11/// # Fields
12///
13/// * `node_id` - Unique identifier for this node in the cluster
14/// * `is_leader` - Whether this node is currently the cluster leader
15/// * `cluster_size` - Total number of nodes currently in the cluster
16/// * `leader_id` - Identifier of the current cluster leader, if known
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct SharedState {
19    /// Unique identifier for this node in the cluster
20    pub node_id: Arc<str>,
21    /// Whether this node is currently the cluster leader
22    pub is_leader: bool,
23    /// Total number of nodes currently in the cluster
24    pub cluster_size: usize,
25    /// Identifier of the current cluster leader, if known
26    pub leader_id: Option<Arc<str>>,
27}
28
29impl SharedState {
30    /// Creates a new SharedState instance for a given node.
31    ///
32    /// Initializes the state with default values:
33    /// - Node is not a leader initially
34    /// - Cluster size starts at 1 (just this node)
35    /// - No leader is known initially
36    ///
37    /// # Arguments
38    ///
39    /// * `node_id` - Unique identifier for this node
40    ///
41    /// # Returns
42    ///
43    /// A new SharedState instance with default values
44    pub fn new(node_id: Arc<str>) -> Self {
45        Self {
46            node_id,
47            is_leader: false,
48            cluster_size: 1,
49            leader_id: None,
50        }
51    }
52}