在 ETCD 源码学习过程,不会讲解太多的源码知识,只讲解相关的实现机制,需要关注源码细节的朋友可以自行根据文章中的提示,找到相关源码进行学习。
Raft log 主要有两部分组成,一是已提交但未被上层模块处理的 unstable 消息,另一部分是已被上层处理的 stable 消息。
主要文件
/raft/log.go raft log 对 stable 和 unstable 的封装
/raft/log_unstable.log unstable 存储的实现
/raft/storage.log stable 存储的实现
log
log 主要维护几个主要状态来维护节点的 log 的信息,committed (已提交的 Index)、applied(已被上层应用的 Index)、storage(已被上层模块处理的entries消息) 和 unstable(已提交未处理的消息)
数据结构:
type raftLog struct {
storage Storage //持久化存储
unstable unstable //未持久化存储
committed uint64 //当前节点已提交的最大Index
applied uint64 //当前节点已应用的最大Index
logger Logger
maxNextEntsSize uint64
}
追加:
func (l *raftLog) maybeAppend(index, logTerm, committed uint64, ents ...pb.Entry) (lastnewi uint64, ok bool) {
if l.matchTerm(index, logTerm) { //查看 index 的 term 与 logTerm 是否