use crate::error::{MetricsError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RaftConsensus {
node_id: String,
current_term: u64,
voted_for: Option<String>,
log: Vec<LogEntry>,
state: RaftState,
peers: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RaftState {
Follower,
Candidate,
Leader,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
term: u64,
index: u64,
data: Vec<u8>,
}
impl RaftConsensus {
pub fn new(node_id: String, peers: Vec<String>) -> Self {
Self {
node_id,
current_term: 0,
voted_for: None,
log: Vec::new(),
state: RaftState::Follower,
peers,
}
}
pub fn start_election(&mut self) -> Result<()> {
self.current_term += 1;
self.state = RaftState::Candidate;
self.voted_for = Some(self.node_id.clone());
Ok(())
}
pub fn append_entries(&mut self, entries: Vec<LogEntry>) -> Result<bool> {
self.log.extend(entries);
Ok(true)
}
pub fn request_vote(&mut self, candidate_id: String, term: u64) -> Result<bool> {
if term > self.current_term {
self.current_term = term;
self.voted_for = Some(candidate_id);
Ok(true)
} else {
Ok(false)
}
}
}