当前位置:网站首页>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()
}
边栏推荐
- [FPGA] EEPROM based on I2C
- c面试 加密程序:由键盘输入明文,通过加密程序转换成密文并输出到屏幕上。
- Qt多线程的多种方法之一 QThread
- 为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
- Implementation of VGA protocol based on FPGA
- Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
- string(讲解)
- Rk3399 platform development series explanation (WiFi) 5.52. Introduction to WiFi framework composition
- 程序员的日常 | 每日趣闻
- 3531. 哈夫曼树
猜你喜欢

缓存在高并发场景下的常见问题

JVM monitoring and diagnostic tools - command line

Common problems of caching in high concurrency scenarios

laravel 使用腾讯云 COS5全教程

693. Travel sequencing

Open the blue screen after VMware installation

Overview of FlexRay communication protocol

matlab / ENVI 主成分分析实现及结果分析

Audio distortion analysis of DSP and DAC based on adau1452

Redis(二)—Redis通用命令
随机推荐
QT console output in GUI applications- Console output in a Qt GUI app?
软件测试知识储备:关于「登录安全」的基础知识,你了解多少?
ETCD数据库源码分析——从raftNode的start函数说起
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
Developers don't miss it! Oar hacker marathon phase III chain oar track registration opens
k8s运行oracle
Ant manor safety helmet 7.8 ant manor answer
3428. Put apples
HKUST & MsrA new research: on image to image conversion, fine tuning is all you need
你不知道的互联网公司招聘黑话大全
Markdown 并排显示图片
安装VMmare时候提示hyper-v / device defender 侧通道安全性
693. 行程排序
面试中有哪些经典的数据库问题?
JWT 认证
A very good JVM interview question article (74 questions and answers)
力扣62 不同路径(从矩阵左上到右下的所有路径数量) (动态规划)
Redis (I) -- getting to know redis for the first time
Jmeter自带函数不够用?不如自己动手开发一个
dolphinscheduler3.x本地启动