use std::net::SocketAddr;
use std::time::Duration;
use std::sync::{Arc, Mutex, RwLock};
use crate::multi_raft::MultiRaft;
use crate::routing::RoutingTable;
use crate::topology::ClusterTopology;
#[derive(Debug, Clone, Copy)]
pub struct JoinRetryPolicy {
pub max_attempts: u32,
pub max_backoff_secs: u64,
}
impl Default for JoinRetryPolicy {
fn default() -> Self {
Self {
max_attempts: 8,
max_backoff_secs: 32,
}
}
}
impl JoinRetryPolicy {
pub fn backoff_for(&self, attempt: u32) -> Duration {
if attempt == 0 || attempt > self.max_attempts {
return Duration::ZERO;
}
let exp = self.max_attempts - attempt;
let max_ms = self.max_backoff_secs.saturating_mul(1_000);
let ms = max_ms >> exp;
Duration::from_millis(ms.max(1))
}
}
#[derive(Debug, Clone)]
pub struct ClusterConfig {
pub node_id: u64,
pub listen_addr: SocketAddr,
pub seed_nodes: Vec<SocketAddr>,
pub num_groups: u64,
pub replication_factor: usize,
pub data_dir: std::path::PathBuf,
pub force_bootstrap: bool,
pub join_retry: JoinRetryPolicy,
pub swim_udp_addr: Option<SocketAddr>,
pub election_timeout_min: Duration,
pub election_timeout_max: Duration,
pub install_snapshot_chunk_bytes: u64,
pub orphan_partial_max_age_secs: u64,
}
pub struct ClusterState {
pub topology: Arc<RwLock<ClusterTopology>>,
pub routing: Arc<RwLock<RoutingTable>>,
pub multi_raft: Arc<Mutex<MultiRaft>>,
}