leader - OmniCloudOrg/OmniOrchestrator GitHub Wiki
Path: src/leader.rs
pub struct LeaderElection {
/// Unique identifier for the current node
node_id: Arc<str>,
/// Shared state that tracks leadership status and cluster information
state: Arc<RwLock<SharedState>>,
/// Timestamp of the last heartbeat received
/// This can be used for more sophisticated leader election algorithms
/// that take into account node responsiveness
#[allow(unused)]
last_heartbeat: Arc<RwLock<std::time::Instant>>,
}
Manages leader election in the OmniOrchestrator cluster. The LeaderElection module is responsible for determining which node in the cluster should act as the leader. It implements a simple deterministic leader election algorithm based on node IDs to ensure that exactly one node assumes the leader role. Leader election is a critical component in distributed systems that ensures: - Coordination responsibilities are clearly assigned - A single point of truth exists for cluster-wide decisions - System stability is maintained through consistent leadership The election process runs periodically to accommodate cluster changes such as nodes joining or leaving the system.
pub fn new(node_id: Arc<str>, state: Arc<RwLock<SharedState>>) -> Self {
Self {
// ... function body
}
Creates a new LeaderElection instance. Initializes the leader election module with the current node's identity and a reference to the shared state. The last_heartbeat is initialized to the current time.
-
node_id
- Unique identifier for the current node -
state
- Shared state for tracking leadership status
A new LeaderElection instance ready to begin the election process
pub async fn start(&self) {
// Create a timer that ticks every 5 seconds
let mut interval = time::interval(Duration::from_secs(5));
Starts the leader election process. This method begins a continuous cycle of leader elections at a fixed interval. Once started, it will periodically execute the election_cycle method to determine the current leader based on the existing cluster composition. The election happens every 5 seconds, which provides a balance between responsiveness to cluster changes and system overhead.
This method runs indefinitely in a loop and should typically be spawned in its own task or thread.