当前位置:网站首页>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()
}
边栏推荐
- Go language learning notes - Gorm use - native SQL, named parameters, rows, tosql | web framework gin (IX)
- Find duplicate email addresses
- 骑士战胜魔王(背包&dp)
- Redisl garbled code and expiration time configuration
- JMeter's own functions are not enough? Why don't you develop one yourself
- PTA ladder game exercise set l2-002 linked list de duplication
- Database notes 04
- 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
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
- 话说SQLyog欺骗了我!
猜你喜欢
![[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core](/img/19/1a6d43c39f2cf810ba754ea9674426.png)
[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core

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

Interview skills of software testing

You don't know the complete collection of recruitment slang of Internet companies

JVM命令之 jstat:查看JVM统计信息

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

高并发大流量秒杀方案思路

一个简单的代数问题的求解

软件测试知识储备:关于「登录安全」的基础知识,你了解多少?

Go language learning notes - Gorm use - Gorm processing errors | web framework gin (10)
随机推荐
搞懂fastjson 对泛型的反序列化原理
JVM monitoring and diagnostic tools - command line
测试开发基础,教你做一个完整功能的Web平台之环境准备
Swagger3 configuration
QT console output in GUI applications- Console output in a Qt GUI app?
Dc-7 target
MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
PTA ladder game exercise set l2-004 search tree judgment
Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
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]
Solve pod install error: FFI is an incompatible architecture
【SQL实战】一条SQL统计全国各地疫情分布情况
Rk3399 platform development series explanation (interruption) 13.10, workqueue work queue
如何在Touch Designer 2022版中设置解决Leap Motion不识别的问题?
DC-7靶机
SAP Spartacus checkout 流程的扩展(extend)实现介绍
The boss always asks me about my progress. Don't you trust me? (what do you think)
Cf:c. column swapping [sort + simulate]
JMeter function assistant - random value, random string, fixed value random extraction
JVM命令之 jstack:打印JVM中线程快照