Zookeeper understanding and application

It is the manager of the cluster and monitors the status of each node in the cluster to perform the next reasonable operation according to the feedback submitted by the node.

1. Leader election during server startup

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 themselves as Leader servers. Each vote will contain the myid and ZXID of the recommended server, which is represented by (myid, ZXID). At this time, Server1's vote 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 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 their own votes. The PK rules are as follows

· Check ZXID first. The server with the larger ZXID is preferred as the leader.

· 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, the ZXID of the two are compared, and both are 0, and then the myid is compared. At this time, the myid of Server2 is the largest, 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.

2. Leader election during server operation

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

(1) Change status. After the leader is suspended, the remaining non-Observer servers will change their server status to LOOKING, and then begin the leader election process.

(2) Each server will issue a vote. During operation, the ZXID on each server may be different (the zxid after joining the cluster is small). At this time, assume 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. Generate votes (1, 123), (3, 122), and then send the votes to all machines in the cluster.

(3) Receive votes from various servers. The process is the same as at startup.

(4) Process voting. The process is the same as that at startup. At this time, Server1 will become the leader.

(5) Statistical voting. The process is the same as at startup.

(6) Change the status of the server. The process is the same as at startup.



 

Guess you like

Origin blog.csdn.net/skylibiao/article/details/86504231