Zookeeper's leader election process

Leader election is the key to ensuring distributed data consistency. When a server in the Zookeeper cluster has one of the following two conditions, it needs to enter the leader election.

  (1) The server is initialized and started.

  (2) The connection to the Leader cannot be maintained while the server is running.

  The following two situations are analyzed and explained.

  1. Leader election during server startup

  For leader election, at least two machines are required. Here, a server cluster consisting of three machines is taken as an example. In the cluster initialization stage, when one server Server1 starts, it cannot perform and complete the leader election alone. 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 issues a vote. Since it is the initial situation, Server1 and Server2 will vote as Leader servers. Each vote will contain the myid and ZXID of the recommended server, which is represented by (myid, ZXID). At this time, the vote of Server1 is (1, 0), Server2's vote is (2, 0), and then each sends this vote to other machines in the cluster.

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

  (3) Processing votes. For each vote, the server needs to PK the votes of others and its own. The PK rules are as follows

    · Check ZXID first. A server with a larger ZXID takes priority as the leader.

    · If the ZXIDs are the same, then compare the myid. The server with larger myid is used as the leader server.

  For Server1, its vote is (1, 0), and the vote received by Server2 is (2, 0). First, the ZXID of the two will be compared, both of which are 0, and then the myid will be compared. At this time, the myid of Server2 is the largest, so Update its own vote to (2, 0), and then vote again. For Server2, it does not need to update its own vote, but just sends the last vote information to all machines in the cluster again.

  (4) Counting votes. 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 counted that two machines in the cluster have accepted the vote of (2, 0). information, at this time it is considered that the Leader has been selected.

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

  2. Leader election during server running time

  During the operation of Zookeeper, the leader and the non-leader server perform their own duties. Even when a non-leader server is down or newly added, the leader will not be affected at this time, but once the leader server is down, the entire cluster will suspend external services. Entering a new round of leader election, the process is basically the same as the leader election process in the startup period. Assuming that there are three servers running, Server1, Server2, and Server3, and the current leader is Server2. If the leader hangs at a certain moment, the leader election will start at this time. The election process is as follows

  (1) Change status. After the Leader hangs, the remaining non-Observer servers will change their server status to LOOKING, and then start the Leader election process.

  (2) Each server will issue a vote. During operation, the ZXID on each server may be different. At this time, it is assumed that the ZXID of Server1 is 123 and the ZXID of Server3 is 122; in the first round of voting, both Server1 and Server3 will vote for themselves, resulting in a vote (1, 123), (3, 122), and then each sends their votes to all machines in the cluster.

  (3) Receive votes from each server. Same process as at startup.

  (4) Processing of votes. The process is the same as at startup, at this time, Server1 will become the Leader.

  (5) Counting votes. Same process as at startup.

  (6) Change the state of the server. Same process as at startup.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253112&siteId=291194637