当前位置:网站首页>ETCD数据库源码分析——从raftNode的start函数说起
ETCD数据库源码分析——从raftNode的start函数说起
2022-07-07 01:39:00 【肥叔菌】

如上图所示,raftNode是真正操纵ETCD RAFT API的模块,充当etcd-raft模块与上层模块之间交互的桥梁,其定义了如下成员。其中msgSnapC通道用于接收和发送快照、applyc通道用于发送待应用的Entry记录、readStateC用于向上层模块发送ReadState、ticker用于向raft模块发送定时脉冲tick、td用于检测发往同一节点的两次心跳消息是否超时。raftNodeConfig后续再说,该篇主要说明ticker模块。
type raftNode struct {
lg *zap.Logger
tickMu *sync.Mutex
raftNodeConfig
msgSnapC chan raftpb.Message // a chan to send/receive snapshot
applyc chan toApply // a chan to send out apply
readStateC chan raft.ReadState // a chan to send out readState
ticker *time.Ticker // utility
td *contention.TimeoutDetector // contention detectors for raft heartbeat message
stopped chan struct{
}
done chan struct{
}
}
初始化raftNode结构体的函数是newRaftNode,raftNodeConfig包含了已经初始化的网络transport、预写日志Storage、raftStorage以及最重要的raft模块,这些模块提供相应的接口给raftnode使用以协助完成raft log发送、持久化、多数复制等功能。
func newRaftNode(cfg raftNodeConfig) *raftNode {
raft.SetLogger(lg) // 初始化logger(略)
r := &raftNode{
lg: cfg.lg, tickMu: new(sync.Mutex),
raftNodeConfig: cfg, td: contention.NewTimeoutDetector(2 * cfg.heartbeat), // set up contention detectors for raft heartbeat message. expect to send a heartbeat within 2 heartbeat intervals.
readStateC: make(chan raft.ReadState, 1),
msgSnapC: make(chan raftpb.Message, maxInFlightMsgSnap),
applyc: make(chan toApply),
stopped: make(chan struct{
}), done: make(chan struct{
}),
}
// 这里是重点
if r.heartbeat == 0 {
r.ticker = &time.Ticker{
} // 如果heartbeat为零,则使用默认&time.Ticker{}
} else {
r.ticker = time.NewTicker(r.heartbeat) } // 否则,需要设定时间间隔为r.heartbeat
return r
}
start函数会启动一个协程运行主要的业务逻辑,这里我们会看到如果定时器超时时,select会检测到该通道,调用raftNode接口的tick函数。该tick函数会获取tickMu锁,然后调用Node接口提供的Tick函数,也就是图中的raft.Node提供的Tick函数。
func (r *raftNode) start(rh *raftReadyHandler) {
internalTimeout := time.Second
go func() {
defer r.onStop()
islead := false
for {
select {
case <-r.ticker.C:
r.tick()
// raft.Node does not have locks in Raft package
func (r *raftNode) tick() {
r.tickMu.Lock()
r.Tick()
r.tickMu.Unlock()
}
raft.Node提供的Tick函数会向n.tickc通道中写入struct{}{},而在raft.Node中会有协程执行run函数,如图中raft.Node的run协程所示,其会执行监听n.tickc通道的代码,然后执行n.rn.Tick()。而这里n.rn.Tick()执行的是rawnode所包含成员raft结构体的Tick成员函数,该函数会根据raft角色执行不同的tick函数:tickElection或tickHeartbeat。
// Tick advances the internal logical clock by a single tick.
func (rn *RawNode) Tick() {
rn.raft.tick()
}
边栏推荐
- k8s运行oracle
- Classic questions about data storage
- Qt多线程的多种方法之一 QThread
- [FPGA tutorial case 13] design and implementation of CIC filter based on vivado core
- Experience of Niuke SQL
- JVM命令之- jmap:导出内存映像文件&内存使用情况
- Cf:c. column swapping [sort + simulate]
- Vscode for code completion
- @Detailed differences between pathvariable and @requestparam
- Three updates to build applications for different types of devices | 2022 i/o key review
猜你喜欢

Loss function and positive and negative sample allocation in target detection: retinanet and focal loss

Interview skills of software testing

Jinfo of JVM command: view and modify JVM configuration parameters in real time

go-microservice-simple(2) go-Probuffer

Jstack of JVM command: print thread snapshots in JVM

POI excel export, one of my template methods

jvm命令之 jcmd:多功能命令行

JVM监控及诊断工具-命令行篇

When we talk about immutable infrastructure, what are we talking about

JVM命令之- jmap:导出内存映像文件&内存使用情况
随机推荐
职场经历反馈给初入职场的程序员
JMeter function assistant - random value, random string, fixed value random extraction
测试开发基础,教你做一个完整功能的Web平台之环境准备
window下面如何安装swoole
Swagger3 configuration
693. Travel sequencing
Deep clustering: joint optimization of depth representation learning and clustering
Ideas of high concurrency and high traffic seckill scheme
Check Point:企业部署零信任网络(ZTNA)的核心要素
苹果cms V10模板/MXone Pro自适应影视电影网站模板
牙齿干细胞的存储问题(未完待续)
QT console output in GUI applications- Console output in a Qt GUI app?
[InstallShield] Introduction
Cloud acceleration helps you effectively solve attack problems!
The boss always asks me about my progress. Don't you trust me? (what do you think)
[solved] record an error in easyexcel [when reading the XLS file, no error will be reported when reading the whole table, and an error will be reported when reading the specified sheet name]
老板总问我进展,是不信任我吗?(你觉得呢)
POI excel export, one of my template methods
搞懂fastjson 对泛型的反序列化原理
JVM命令之 jinfo:实时查看和修改JVM配置参数