当前位置:网站首页>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()
}
边栏推荐
- Jmeter自带函数不够用?不如自己动手开发一个
- Change the original style of UI components
- Experience sharing of contribution of "management world"
- Three updates to build applications for different types of devices | 2022 i/o key review
- 360织语发布7.0新品 为党政军、央国企打造专属“统一数字工作空间”
- 屏幕程序用串口无法调试情况
- C语言面试 写一个函数查找两个字符串中的第一个公共字符串
- Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
- 测试开发基础,教你做一个完整功能的Web平台之环境准备
- UIC(组态UI工程)公版文件库新增7款行业素材
猜你喜欢

693. Travel sequencing

LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标

可极大提升编程思想与能力的书有哪些?

window下面如何安装swoole

基于FPGA的VGA协议实现

Shared memory for interprocess communication

Ctfshow-- common posture

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

JMeter function assistant - random value, random string, fixed value random extraction

Open the blue screen after VMware installation
随机推荐
Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
Matlab / envi principal component analysis implementation and result analysis
基于FPGA的VGA协议实现
Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
UIC(组态UI工程)公版文件库新增7款行业素材
软件测试知识储备:关于「登录安全」的基础知识,你了解多少?
【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽
JWT 认证
LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标
3428. 放苹果
缓存在高并发场景下的常见问题
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
Subghz, lorawan, Nb IOT, Internet of things
Swagger3 configuration
直击2022ECDC萤石云开发者大会:携手千百行业加速智能升级
JMeter's own functions are not enough? Why don't you develop one yourself
骑士战胜魔王(背包&dp)
ICML 2022 | explore the best architecture and training method of language model
Ctfshow-- common posture
Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!