【raft】学习二:etcd/raft raft数据结构学习

13 篇文章 0 订阅

Raft-etcd 源码学习

各位朋友大家好啊,还没有学习消息类型的盆友,可以去这里哦,本次主要学习一下raft协议层中所定义的数据结构。

数据结构学习

type raft struct {
	// id ID代表当前节点的ID
	id uint64
	// Term 代表当前任期
	Term uint64
	// Vote 当前节点投票给了谁,可以是自己
	Vote uint64

	// readStates 存储读请求到的结果数据
	readStates []ReadState

	// the log
	// raftLog 主要存储当前的raft的快照数据和日志打印
	raftLog *raftLog

	// maxMsgSize raftLog中保存 pb.Entry 的数目
	maxMsgSize         uint64
	maxUncommittedSize uint64
	// TODO(tbg): rename to trk.
	// prs 记录其他节点的状态和跟踪,一般对主节点生效
	prs tracker.ProgressTracker
	// state 当前节点的状态,leader 或者 follower 或者 定义的节点状态
	state StateType

	// isLearner 当前节点是否为主节点,true代表是,反之不是
	isLearner bool
	// msgs 协议层中的消息
	msgs []pb.Message

	// lead 当前时刻的主节点ID
	lead uint64
	// leadTransferee 是节点ID,当这个id不为0时,将主切为这个节点id
	leadTransferee uint64
	// 一次只能有一个 conf 更改处于挂起状态(在日志中,但尚未应用)。
	//这是通过 pendingConfIndex 强制执行的,
	//它设置为一个值 >= 最新挂起配置更改(如果有)的日志索引。
	//仅当领导者应用的索引大于此值时,才允许提出配置更改。
	pendingConfIndex uint64
	// 对 Raft 日志未提交尾部大小的估计。 用于防止无限制的日志增长。 仅由领导维护。 重置术语更改。
	uncommittedSize uint64
	
	// readOnly 是为了处理只读请求的而设计数据结构,一方面保存请求数据,一方面选择不同的读请求的处理方式
	readOnly *readOnly

	// number of ticks since it reached last electionTimeout when it is leader
	// or candidate.
	// number of ticks since it reached last electionTimeout or received a
	// valid message from current leader when it is a follower.
	electionElapsed int

	// number of ticks since it reached last heartbeatTimeout.
	// only leader keeps heartbeatElapsed.
	heartbeatElapsed int
	// 是从 leader 角度判断自己是否合法;
	// leader 每隔 election timeout 检查其他节点的活跃情况,若少于 majority 活跃,则自动 step down 为 follower。
	checkQuorum bool
	// 是否开启预选举
	preVote     bool

	heartbeatTimeout int
	electionTimeout  int
	// randomizedElectionTimeout is a random number between
	// [electiontimeout, 2 * electiontimeout - 1]. It gets reset
	// when raft changes its state to follower or candidate.
	randomizedElectionTimeout int
	disableProposalForwarding bool
	
	// tick函数,主要是不理不同超时函数,心跳和选举
	tick func()
	// step 节点在不同状态的消息消息处理函数
	step stepFunc

	logger Logger

	// 主要是面对当前主节点还没有提交过消息,以至于当前主节点的commit index 可能不是最新的,需要先把read消息存储起来,等到主节点提交日志消息后在处理这些读请求
	pendingReadIndexMessages []pb.Message
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值