当前位置:网站首页>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()
}
边栏推荐
- vim映射大K
- cf:C. Column Swapping【排序 + 模拟】
- Jstat pour la commande JVM: voir les statistiques JVM
- [InstallShield] Introduction
- Sequential storage of stacks
- MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
- 【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现
- 360 Zhiyu released 7.0 new products to create an exclusive "unified digital workspace" for the party, government and army, and central and state-owned enterprises
- PowerPivot - DAX (function)
- Mac version PHP installed Xdebug environment (M1 version)
猜你喜欢
![[cloud native] what is the microservice architecture?](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[cloud native] what is the microservice architecture?

Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou

Introduction to yarn (one article is enough)

DC-7靶机

Deep clustering: joint optimization of depth representation learning and clustering

PTA ladder game exercise set l2-004 search tree judgment

window下面如何安装swoole
![[InstallShield] Introduction](/img/df/4522d06510ff918d00659b8358368f.jpg)
[InstallShield] Introduction
![A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]](/img/fd/7223d78fff54c574260ec0da5f41d5.png)
A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]

Markdown 并排显示图片
随机推荐
cf:C. Column Swapping【排序 + 模擬】
[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]
laravel 使用腾讯云 COS5全教程
How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
Flask1.1.4 werkzeug1.0.1 source code analysis: start the process
一个简单的代数问题的求解
深度聚类:将深度表示学习和聚类联合优化
window下面如何安装swoole
Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
JVM命令之- jmap:导出内存映像文件&内存使用情况
Subghz, lorawan, Nb IOT, Internet of things
Talking about reading excel with POI
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
从“跑分神器”到数据平台,鲁大师开启演进之路
3428. Put apples
Rk3399 platform development series explanation (interruption) 13.10, workqueue work queue
Red hat install kernel header file
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
win系统下安装redis以及windows扩展方法
Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?