| name | rust-distributed |
| description | 分布式系统专家。处理 Raft, 2PC, 共识算法, 分布式事务, 分布式一致性, 分布式协调--- |
Raft 共识算法
Raft 核心概念
┌─────────────────────────────────────────────────────┐
│ Raft 集群 │
├─────────────────────────────────────────────────────┤
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Leader │ ◄──►│ Follower│ ◄──►│ Follower│ │
│ │ 节点 │ │ 节点 │ │ 节点 │ │
│ └────┬────┘ └─────────┘ └─────────┘ │
│ │ │
│ - 处理客户端请求 │
│ - 复制日志到 Follower │
│ - 管理心跳和选举 │
└─────────────────────────────────────────────────────┘
状态机
enum RaftState {
Follower,
Candidate,
Leader,
}
struct RaftNode {
state: RaftState,
current_term: u64,
voted_for: Option<u64>,
log: Vec<LogEntry>,
commit_index: usize,
last_applied: usize,
election_timeout: Duration,
last_heartbeat: Instant,
node_id: u64,
peers: Vec<u64>,
}
日志复制
struct LogEntry {
term: u64,
index: ,
command: <>,
}
{
(& , peer: ) {
= .(peer);
= .(peer);
: <LogEntry> = .log
[(prev_log_index + )..]
.();
= AppendEntriesRequest {
term: .current_term,
leader_id: .node_id,
prev_log_index,
prev_log_term,
entries,
leader_commit: .commit_index,
};
.(peer, rpc);
}
}