Leader election at server startup

When each node is started, the state is LOOKING, in a wait-and-see state, and then the main selection process begins 

For Leader election, at least two machines are required. Here, a server cluster composed of three machines is selected as an example. In the cluster initialization phase, when one server Server1 starts, it alone cannot conduct and complete Leader election. When the second server Server2 starts, the two machines can communicate with each other at this time, and each machine tries to find the Leader, so enter Leader election process. The election process is as follows

(1) Each server sends a vote. Because it is the initial situation, Server1 and Server2 will vote on themselves as Leader servers. Each vote will include the myid, ZXID, and epoch of the recommended server, using (myid, ZXID, epoch) to indicate that Server1's vote is now It is (1, 0), Server2's vote is (2, 0), and then each will send this vote to other machines in the cluster.

(2) Accept votes from various servers. After each server in the cluster receives a vote, it first judges the validity of the vote, such as checking whether it is the current round of voting (epoch) and whether it comes from a server in the LOOKING state.

(3) Process voting. For each vote, the server needs to PK other people’s votes with its own votes. The PK rules are as follows 

i. Compare epoch first

ii. Check ZXID next. The server with a larger ZXID is preferred as the leader 

iii. If the ZXID is the same, then compare myid. The server with the larger myid serves as the leader server.

For Server1, its vote is (1, 0), and the vote of receiving Server2 is (2, 0). First, it will compare the ZXIDs of both, which are both 0, and then compare myid. At this time, Server2 has the largest myid, so Update your vote to (2, 0), and then vote again. For Server2, it does not need to update its vote, just send the last vote information to all machines in the cluster again.

(4) Statistical voting. After each vote, the server will count the voting information to determine whether more than half of the machines have received the same voting information. For Server1 and Server2, it is calculated that two machines in the cluster have already accepted (2, 0) votes. Information, at this time it is considered that the Leader has been selected.

(5) Change the server status. Once the Leader is determined, each server will update its own status, if it is a follower, then it will be changed to FOLLOWING, if it is a leader, it will be changed to LEADING.

 

Guess you like

Origin blog.csdn.net/Leon_Jinhai_Sun/article/details/112971466