node节点的三种状态
一个node节点具有三种状态
- Follower state
- Condidate state
- Leader state
选举过程
- 领导人选举
- 所有节点开始时处于Follower state
All our nodes start in the follower state - 如果follower 没有听到过leader发来的消息,那么就可以成为condidate候选者.
If followers don't hear from a leader then they can become a candidate. - 候选者请求其他节点投票给自己。
The candidate then requests votes from other nodes. - 节点会以投票的方式回复。
Nodes will reply with their vote. - 如果候选者获得大多数节点的投票,他会成为领导者。
The candidate becomes the leader if it gets votes from a majority of nodes. - 领导人选举结束。
This process is called Leader Election.
- 数据一致性(日志复制)
- 到此系统的所有的变化都要经过领导者Leader;
- 每一次的变更节点都会记录节点日志;
- 如果节点日志没有提交,就不会更新其他节点的信息;
- 如果节点日志提交了,会将信息复制给其他follower;
- 然后节点等待大多数节点写入数据完成;
- 此时在将变更的数据提交到leader节点;
- 然后leader会通知follower信息已经提交;
- 到此为止,整个集群的数据达成了一致。
- 这个过程称为日志复制。
- 候选人选举
election timeout
选举超时,follower等待成为condidate的时间
超时时间被随机分配到150 ms到300毫秒之间
一般在150 ms到300毫秒这个随机时间范围内会选举出condidate
condidate 选举成功后,重新进入新的一路Leader选举。
- 健康检查
Leader和各个Follower之间维持一个心跳,当和某一个Follower之间的心跳停止,便重新开始新的一轮选举。