Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the Implementation of preferred leader #22

Merged

Conversation

RongtongJin
Copy link
Contributor

@RongtongJin RongtongJin commented May 28, 2019

Purpose(目标)

This PR is to polish preferedleader.
优化preferedleader

Changelog(修改记录)

  • The current situation is that REJECT_TAKING_LEADERSHIP may occur even if preferred leader is not set. So currTerm is initially set to 0 and termToTakeLeadership is initially set to -1, which ensures that they will not be the same under normal circumstances.
    在测试的时候出现即使没有设置preferred leader,日志中仍然出现了REJECT_TAKING_LEADERSHIP。这是因为当前currTerm初始时设置为-1,而termToTakeLeadership设置为0,当term增加,两者会相等。因此设置currterm初始为0,termToTakeLeadership初始为-1,确保两者正常情况下不会相等。

  • Role may be follower when term < TermToTakeLeadership in check function , it should not return directly. For example, The node may receive heartbeat before term increase as a candidate, then it will be follower and should return TAKE_LEADERSHIP_FAILED.
    在TakeLeadershipTask的check方法中,当term <TermToTakeLeadership时,直接返回。但有可能出现这样的情况:抢主的candidate节点在提升自己的term之前收到了心跳,从而变成了follower,此时满足term < TermToTakeLeadership,这是应该算是抢主失败,所以应该返回TAKE_LEADERSHIP_FAILED。(这个情况出现的概率比较小)

  • When a node takeleadership, it refuses to vote for other nodes only if the LedgerEndTerm of the two nodes is consistent and the LedgerEndIndex of the current node is greater than or equal to that of other node.
    当一个节点正在抢主,当前如果日志高度相等则拒绝投票。更加保险的情况下先比较最后一个日志条目的term是否相等,然后再比较日志高度是否相等,都相等再拒绝投票。

  • Add volatile for some variables to ensure visibility
    为一些变量加上volatile属性确保可见性。

@duhenglucky duhenglucky merged commit 79860bf into openmessaging:prefered_leader Aug 27, 2019
@RongtongJin RongtongJin changed the title Polish preferedleader Optimize the Implementation of preferred leader Jul 18, 2020
@RongtongJin RongtongJin added this to the 0.2.0 milestone Jul 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants