leader election
election timeout:follower超过这个时间会变成candidates,term++,发送Request vote竞选。election timeout被随机分配在150毫秒至300毫秒之间。
heartbeat timeout:只有leader才有该配置和机制,第一个作用:超时了给follower心跳检测,candidates收到后会将election timeout计数器置零,以免发生不必要的竞选。第二个作用:以Append Entries的数据格式发送,携带日志写和日志提交操作信息,既是心跳信息,也是业务信息。(heartbeat timeout一般需要小于election timeout,不然导致频繁选举)
初次选举:
服务启动时所有节点为follower,此时没有leader,等待150-300ms的election timeout后,成为candidates,term++,发送Request vote竞选,收到Request vote的节点如果此时还没发送Request vote,那么投票给Request vote的节点;如果过程中发起Request vote的节点收到的选票多于一半,那么直接当选leader,然后此leader定期heartbeat timeout发送Append Entries给所有follower维持心跳
leader心跳丢失,触发follower的election timeout:
此时没有leader,等待150-300ms的election timeout后,成为candidates,term++,发送Request vote竞选,收到Request vote的节点如果此时还没发送Request vote,那么投票给Request vote的节点;如果过程中发起Request vote的节点收到的选票多于一半,那么直接当选leader,然后此leader定期heartbeat timeout发送Append Entries给所有follower维持心跳(heartbeat timeout超时):
没有leader,等待150-300ms的election timeout后,成为candidates,term++,发送Request vote竞选,收到Request vote的节点如果此时还没发送Request vote,那么投票给Request vote的节点;如果过程中发起Request vote的节点收到的选票多于一半,那么直接当选leader,然后此leader定期heartbeat timeout发送Append Entries给所有follower维持心跳
出现同票:
如果多个节点出现同票,那么会在election timeout到期了继续选举:term++,发送Request vote竞选。利用微小的物理时差,反复选举,直到选举出leader
log replication
通过heartbeat timeout周期发送Append Entries,先发送日志写操作(占用一个周期),回复客户端成功,在日志提交操作(占用一个周期)
参考
http://www.kailing.pub/raft/index.html
https://www.zhihu.com/question/263684969/answer/2235672505
https://blog.csdn.net/androidstarjack/article/details/119745571?app_version=5.11.0&code=app_1562916241&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22119745571%22%2C%22source%22%3A%22unlogin%22%7D&uLinkId=usr1mkqgl919blen&utm_source=app
https://blog.csdn.net/yaoxie1534/article/details/125657808?app_version=5.11.0&code=app_1562916241&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125657808%22%2C%22source%22%3A%22unlogin%22%7D&uLinkId=usr1mkqgl919blen&utm_source=app