当前位置:网站首页>ETCD数据库源码分析——处理Entry记录简要流程
ETCD数据库源码分析——处理Entry记录简要流程
2022-07-04 22:33:00 【肥叔菌】
(1)当客户端向etcd集群发送了一次请求之后,请求中的封装Entry记录会先被提交给etcd-raft模块进行处理,其中,etcd-raft模块会先将Entry记录保存到raftLog.unstable中。
我们以ETCD数据库源码分析——服务端PUT流程为例,kvServer结构体Put函数处理流程如下:首先会对请求消息进行各方面的检查,检查完之后会将所有的请求交给其内封装的RaftKV接口进行处理,待处理完成得到响应消息之后,会通过header.fill()方法填充响应的头信息,最后将完整的响应消息返回给客户端。因此,往下追踪 srv.(KVServer).Put(ctx, in)其实就是调用(s *EtcdServer) Put()函数。
从raftRequestOnce流程看出最终调用的是 (s *EtcdServer) processInternalRaftRequestOnce(…)函数,在该函数里面有一句关键调用 s.r.Propose(cctx, data)。s是EtcdServer, r是其里面的成员变量raftNode, 这就是进入raft协议相关的节奏了。通过对Propose函数的追踪,我们可以看到最终函数走到了stepWithWaitOption
// raft/node.go
func (n *node) Propose(ctx context.Context, data []byte) error {
return n.stepWait(ctx, pb.Message{
Type: pb.MsgProp, Entries: []pb.Entry{
{
Data: data}}}) } // 这里给Message加上了Type为pb.MsgProp,Entry为初入的PutRequest
func (n *node) stepWait(ctx context.Context, m pb.Message) error {
return n.stepWithWaitOption(ctx, m, true) }
// Step advances the state machine using msgs. The ctx.Err() will be returned, if any.
func (n *node) stepWithWaitOption(ctx context.Context, m pb.Message, wait bool) error {
if m.Type != pb.MsgProp {
// 这里不会走
select {
case n.recvc <- m: return nil
case <-ctx.Done(): return ctx.Err()
case <-n.done: return ErrStopped
}
}
ch := n.propc // 取出node结构体提供的propc通道
pm := msgWithResult{
m: m}
if wait {
pm.result = make(chan error, 1) }
select {
case ch <- pm: if !wait {
return nil } // 将带有msg的pm结构体放入propc通道
case <-ctx.Done(): return ctx.Err()
case <-n.done: return ErrStopped
}
select {
case err := <-pm.result:
if err != nil {
return err }
case <-ctx.Done(): return ctx.Err()
case <-n.done: return ErrStopped
}
return nil
}
从raft/node.go的run函数可以看出,当从propc通道中取出msgWithResult,在从其中取出消息msg,设置msg的来源节点为本节点id。然后调用raft模块的Step函数驱动状态机。
(2)etcd-raft模块将该Entry记录封装到前面介绍的Ready实例中,返回给上层模块进行持久化。
(3)当上层模块收到待持久化的Entry记录之后,会先将其记录到WAL日志文件中,然后进行持久化操作,最后通知etcd-raft模块进行处理。
(4)此时etcd-raft模块就会将该Entry记录从unstable移动到storage中保存。
(5)待该Entry记录被复制到集群中的半数以上节点时,该Entry记录会被Leader节点确认为已提交(committed),并封装进Ready实例返回给上层模块。
(6)此时上层模块即可将该Ready实例中携带的待应用Entry记录应用到状态机中。
边栏推荐
- 9 - class
- Advanced area of attack and defense world misc 3-11
- Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
- 特征缩放 标准化 归一化
- Gnawing down the big bone - sorting (II)
- Duplicate ADMAS part name
- Notepad++--编辑的技巧
- Hit the core in the advanced area of misc in the attack and defense world
- EditPlus--用法--快捷键/配置/背景色/字体大小
- Advanced area a of attack and defense world misc Masters_ good_ idea
猜你喜欢

【剑指Offer】6-10题

Install the gold warehouse database of NPC

The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal

Redis入门完整教程:事务与Lua

Advanced area a of attack and defense world misc Masters_ good_ idea
![P2181 diagonal and p1030 [noip2001 popularization group] arrange in order](/img/79/36c46421bce08284838f68f11cda29.png)
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order

A complete tutorial for getting started with redis: hyperloglog

One of the commonly used technical indicators, reading boll Bollinger line indicators

Detailed explanation of heap sort code

蓝队攻防演练中的三段作战
随机推荐
A complete tutorial for getting started with redis: getting to know redis for the first time
The overview and definition of clusters can be seen at a glance
[roommate learned to use Bi report data processing in the time of King glory in one game]
[graph theory] topological sorting
【剑指offer】1-5题
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
The difference between Max and greatest in SQL
Photoshop batch adds different numbers to different pictures
模拟摇杆控制舵机
EditPlus--用法--快捷键/配置/背景色/字体大小
攻防世界 MISC 进阶区 can_has_stdio?
微信公众号解决从自定义菜单进入的缓存问题
Common methods in string class
企业如何跨越数字化鸿沟?尽在云原生2.0
Three stage operations in the attack and defense drill of the blue team
Sword finger offer 67 Convert a string to an integer
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
Redis入门完整教程:Bitmaps
Redis入门完整教程:列表讲解
Gnawing down the big bone - sorting (II)