当前位置:网站首页>Etcd database source code analysis -- starting from the start function of raftnode
Etcd database source code analysis -- starting from the start function of raftnode
2022-07-07 06:26:00 【Tertium FATUM】

As shown in the figure above ,raftNode It's real manipulation ETCD RAFT API Module , act as etcd-raft The bridge between modules and upper modules , It defines the following members . among msgSnapC Channels are used to receive and send snapshots 、applyc The channel is used to send... To be applied Entry Record 、readStateC It is used to send ReadState、ticker Used to direct to raft The module sends timing pulses tick、td It is used to detect whether two heartbeat messages sent to the same node timeout .raftNodeConfig Let's talk about it later , This article mainly explains ticker modular .
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{
}
}
initialization raftNode The function of the structure is newRaftNode,raftNodeConfig Contains the initialized network transport、 Prewrite log Storage、raftStorage And most importantly raft modular , These modules provide corresponding interfaces to raftnode Use to help complete raft log send out 、 Persistence 、 Most functions such as replication .
func newRaftNode(cfg raftNodeConfig) *raftNode {
raft.SetLogger(lg) // initialization logger( A little )
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{
}),
}
// Here's the point
if r.heartbeat == 0 {
r.ticker = &time.Ticker{
} // If heartbeat zero , Use default &time.Ticker{}
} else {
r.ticker = time.NewTicker(r.heartbeat) } // otherwise , You need to set the time interval as r.heartbeat
return r
}
start Function will start a coroutine to run the main business logic , Here we will see if the timer expires ,select This channel will be detected , call raftNode Interface tick function . The tick Function gets tickMu lock , And then call Node Interface provided Tick function , That's what this is raft.Node Provided Tick function .
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 Provided Tick The function will be directed to n.tickc Write struct{}{}, And in the raft.Node There will be coordination execution run function , As shown in figure, raft.Node Of run As shown in the process , It will perform monitoring n.tickc Channel code , And then execute n.rn.Tick(). And here n.rn.Tick() Execution is rawnode Included members raft Structure of the Tick Member functions , This function will be based on raft Roles perform different tick function :tickElection or tickHeartbeat.
// Tick advances the internal logical clock by a single tick.
func (rn *RawNode) Tick() {
rn.raft.tick()
}
边栏推荐
- win系统下安装redis以及windows扩展方法
- Audio distortion analysis of DSP and DAC based on adau1452
- [opencv] morphological filtering (2): open operation, morphological gradient, top hat, black hat
- 开发者别错过!飞桨黑客马拉松第三期链桨赛道报名开启
- Redis(一)——初识Redis
- 进程间通信之共享内存
- Swagger3 configuration
- 3428. Put apples
- tkinter窗口选择pcd文件并显示点云(open3d)
- A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
猜你喜欢

Redis (II) - redis General Command

【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽

Redis(二)—Redis通用命令

进程间通信之共享内存

Test the foundation of development, and teach you to prepare for a fully functional web platform environment

Go language learning notes - Gorm use - native SQL, named parameters, rows, tosql | web framework gin (IX)

如何在Touch Designer 2022版中设置解决Leap Motion不识别的问题?

Peripheral driver library development notes 43: GPIO simulation SPI driver
![[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

港科大&MSRA新研究:关于图像到图像转换,Fine-tuning is all you need
随机推荐
进程间通信之共享内存
Array proof during st table preprocessing
你不知道的互联网公司招聘黑话大全
谷歌 Chrome 浏览器发布 103.0.5060.114 补丁修复 0-day 漏洞
Audio distortion analysis of DSP and DAC based on adau1452
港科大&MSRA新研究:关于图像到图像转换,Fine-tuning is all you need
骑士战胜魔王(背包&dp)
rt-thread 中对 hardfault 的处理
693. Travel sequencing
HKUST & MsrA new research: on image to image conversion, fine tuning is all you need
Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
[Shell]常用shell命令及测试判断语句总结
go-microservice-simple(2) go-Probuffer
ICML 2022 | explore the best architecture and training method of language model
生活中的开销,怎么记账合适
LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标
laravel 使用腾讯云 COS5全教程
[SOC FPGA] peripheral PIO button lights up
拼多多败诉:“砍价免费拿”侵犯知情权但不构成欺诈,被判赔400元
JVM in-depth