In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you an example analysis of the Zookeeper selection mechanism, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Zookeeper is a distributed service framework, which is mainly used to solve some data management problems encountered in distributed applications, such as unified naming service, state synchronization service, cluster management, distributed application configuration item management and so on. We can simply think of Zookeeper as the butler of a distributed family, so how did the butler team choose Leader?
4 states of the zookeeper node:
LEADING: this indicates that this node is already a leader node and is in a leading position, which is almost the master in a general cluster. But in zookeeper, only leader has write permission, and other nodes (FOLLOWING) do not have write permission and can read
LOOKING: during the election, we are looking for leader and will soon enter the leader election process.
FOLLOWING: follower, indicating that the leader in the current cluster has been elected and mainly has the following function points
Send a request to leader (PING message, REQUEST message, ACK message, REVALIDATE message)
Receive leader messages and process them
To receive the request sent by client, if it is a write request, it will be sent to Leader for voting processing, and then the client result will be returned.
OBSERVING:OBSERVING is similar to FOLLOWING, but does not participate in voting and elections, and accepts the results after the leader election.
Election process:
If you have the following five machines, server1, server2, server3, server4 and server5, the pictures are picked online.
Img
Each server has its own vote. When the initialization or the number of server crashes exceeds half, each server has its own myid (zookeeper configuration file), which is calculated as 1, 2, 3, 4, 5.
In the election process, the rotation training of server is mainly based on zxid and myid, and then statistical voting is compared.
Zxid (ZooKeeper Transaction Id, each request corresponds to a unique zxid, if zxid a
The election is divided into two situations. When initialization and leader hang up, at least two machines are needed for leader election, and the number of cluster machines is basically odd.
Initialization
When initializing the cluster, the myid of server1 is 1, server2 zxid is 0, myid is 2, and so on. In this case, the zxid is always 0. First compare zxid, then compare myid
Server 1 starts, votes for itself, and then sends a vote message. Because other machines have not been started, it can not receive feedback. The status of server 1 has always been Looking (election status). Server 2 starts, votes for itself, and exchanges results with the previously started server 1. Server 2 wins because of its large myid, but the number of votes is not more than half at this time, so the status of the two servers is still LOOKING. Server 3 starts, votes for itself, and exchanges information with the previously started server 1Magin2. Because server 3 has the largest myid, server 3 wins, and the number of votes happens to be more than half, so server 3 becomes the leader, and server 1Jing 2 becomes the younger brother. Server 4 starts to vote for itself, while exchanging information with the previously started server 1Magazine 3. Although server 4 has a large myid, server 3 has won before, so server 4 can only become a younger brother. Server 5 starts, and the logic behind it is the same as server 4.
When the election machine is more than halfway through, the leader has been elected, followed by the elected leader, so 4 and 5 follow the server3 of leader.
Therefore, during initialization, when the number of machines reaches more than half, whose myid is generally the largest is leader.
During operation
According to the above initialization, server3 becomes leader, and server3 in leader dies during operation, then non-Observer servers server1, server2, server4, server5 will change their node state to LOOKING state.
1. Start the leader election. Now elections are also conducted on the basis of myid and zxid.
2. First of all, each server will vote for leader. Suppose the zxid of server1 is 123, the zxid of server2 is 124, the zxid of server4 is 169, and the zxid of server5 is 188.
3. First, compare zxid and then compare server1, server2 and server4. Compare server4 to leader according to the priority conditions. Then server5 followed server4, even though server5 had the largest zxid, but by the time server4 was elected, the number of machines had exceeded half. No more elections, follow the leader that has been elected
In order to ensure the consistency of the data, all the operations of the zookeeper cluster are done by leader, and then synchronized to follower by leader. That's the point. Zookeeper does not ensure that all nodes have synchronized data, as long as most of the nodes (that is, name2 synchronization 1) synchronize successfully.
Let's assume that there is a successful write operation so that the data now exists only on the node leader, and then the leader is synchronized to other follower. At this time, three machines were down, more than half of the machines could not vote, and less than half of the remaining two were unable to provide any services. Start another machine recovery service. So do not crash more than half of the machine, more than half will lead to failure of normal service.
There will be a 30s-120s process during the leader election, and services will not be available during this period. If using zookeeper as a service discovery is a drawback, it is basically unbearable. Zookeeper itself is a CP system, which ensures data consistency and provides services at the time of recovery. There is no highly available solution. If the leader fails to provide services during the election, it can be fatal for a large application. It can provide other services in the same distributed system: unified naming service, configuration management, distributed lock service, cluster management, etc.) is a great open source project, very mature
The above is all the content of this article "sample Analysis of Zookeeper selection Mechanism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.