etcd raft介绍
etcd raft是目前使用最广泛的raft库,如果想深入了解raft请直接阅读论文 “In Search of an Understandable Consensus Algorithm”(https://raft.github.io/raft.pdf), etcd raft在etcd, Kubernetes, Docker Swarm, Cloud Foundry Diego, CockroachDB, TiDB, Project Calico, Flannel等分布式系统中都有应用,在生成环境得到了验证。 传统raft库的实现都是单体设计(集成了存储层、消息序列化、网络层等), etcd raft继承了简约的设计理念,只实现了最核心的raft算法, 这样更加的灵活。etcd将网络、日志存储、快照等功能分开,通过独立的模块实现,用户可以在需要时调用。etcd自身实现了自己的一套raft配套库:etcd-wal(用于存储日志),snap(用于存储快照),MemoryStorage(用于存储当前日志、快照、状态等信息以供raft核心程序使用)。
![v2-1f9955460628379eab7345b1bc994e4d_b.jpg](https://i-blog.csdnimg.cn/blog_migrate/ea6026320f56135559c4a088d62dcc6d.jpeg)
etcd wal介绍
WAL是write ahead log的缩写,etcd使用wal模块来完成raft日志的持久化存储,etcd对wal的所有实现都放在wal目录中。
wal数据结构
type WAL struct {
lg *zap.Logger
dir string
// the living directory of the underlay files
// dirFile is a fd for the wal directory for syncing on Rename
dirFile *os.File
metadata []byte
// metadata recorded at the head of each WAL
state raftpb.HardState
// hardstate recorded at the head of WAL
start walpb.Snapshot
// snapshot to start reading
decoder *decoder