1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::error::*;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;

pub type RSL = Arc<Box<dyn Resolver + Sync + Send + 'static>>;
pub type SM = Arc<Box<dyn StateMachine + Sync + Send + 'static>>;

#[async_trait]
pub trait StateMachine {
    /// this is a call execute method for leader, and no change log,
    /// example search method only run leader, now you can call it in replica , it forward to leader and return result .
    fn execute(&self, command: &[u8]) -> RaftResult<Vec<u8>>;
    fn apply_log(&self, term: u64, index: u64, command: &[u8]) -> RaftResult<()>;
    async fn apply_member_change(
        &self,
        term: u64,
        index: u64,
        node_id: u64,
        action: u8,
        exists: bool,
    ) -> RaftResult<()>;

    async fn apply_leader_change(&self, term: u64, index: u64, leader: u64) -> RaftResult<()>;
}

pub trait Resolver {
    fn heartbeat_addr(&self, node_id: &u64) -> RaftResult<String>;
    fn log_addr(&self, node_id: &u64) -> RaftResult<String>;
}

pub struct DefResolver {
    log_addrs: HashMap<u64, String>,
    internal_addrs: HashMap<u64, String>,
}

impl Resolver for DefResolver {
    fn heartbeat_addr(&self, node_id: &u64) -> RaftResult<String> {
        match self.internal_addrs.get(node_id) {
            Some(v) => Ok(v.to_string()),
            None => Err(RaftError::NotfoundAddr(*node_id)),
        }
    }

    fn log_addr(&self, node_id: &u64) -> RaftResult<String> {
        match self.log_addrs.get(node_id) {
            Some(v) => Ok(v.to_string()),
            None => Err(RaftError::NotfoundAddr(*node_id)),
        }
    }
}

impl DefResolver {
    pub fn new() -> Self {
        return DefResolver {
            log_addrs: HashMap::new(),
            internal_addrs: HashMap::new(),
        };
    }

    pub fn add_node(&mut self, node_id: u64, host: String, heartbeat_port: u16, log_port: u16) {
        if let Some(v) = self.log_addrs.remove(&node_id) {
            std::mem::forget(v);
        }

        if let Some(v) = self.internal_addrs.remove(&node_id) {
            std::mem::forget(v);
        }

        self.log_addrs
            .insert(node_id, format!("{}:{}", host, log_port));

        self.internal_addrs
            .insert(node_id, format!("{}:{}", host, heartbeat_port));
    }

    pub fn remove_node(&mut self, node_id: u64) {
        if let Some(v) = self.log_addrs.remove(&node_id) {
            std::mem::forget(v);
        }

        if let Some(v) = self.internal_addrs.remove(&node_id) {
            std::mem::forget(v);
        }
    }
}