Graphic zookeeper Fastleader election algorithm "turn"

Source: Internet
Author: User

Transferred from: http://codemacro.com/2014/10/19/zk-fastleaderelection/

When zookeeper is configured as a cluster mode, an instance is elected as leader when it is started or in an abnormal situation. Its default election algorithm is FastLeaderElection .

I don't know zookeeper. It is possible to consider a problem where a service can be configured as multiple instances to form a cluster for external service. Each instance has redundant data locally, and each instance can provide read and write services directly to the outside. In order to ensure the consistency of data in this cluster, a leader is needed to coordinate some transactions. So here's the question: How do you determine which instance is leader?

The difficulty of the problem lies in:

    • There is not an arbitrator to select leader
    • Data may already exist on each instance, and it is not certain that the data on which instance is up to date

The distributed election algorithm is used to solve this problem.

This article is based on the Zookeeper 3.4.6 source code analysis. The source code of the fastleaderelection algorithm is all located FastLeaderElection.java in the file, its external interface FastLeaderElection.lookForLeader is, the interface is a synchronous interface, until the end of the election will not return. Also because there are similar articles on the Internet, so I will explain from the perspective of the diagram. Read some other articles to help you get the initial impression:

    • In layman's zookeeper five leader election, code guide
    • zookeeper3.3.3 Source Code Analysis (ii) Fastleader election algorithm, text description finer
Main process

Reading the code and the above recommended articles can make the whole process clear. Implementation, including a message processing main loop, is also the main logic of the election, as well as a message sending queue processing thread and message decoding thread. The main processes can be summarized as follows:

Recommended in comparison with the recommended articles and code to understand, do not repeat.

We understand this algorithm from the perceptual.

Each node, the equivalent of an elector, they all have their own referees, and at the very beginning they all recommend themselves. Who is better suited to be a leader has a simple rule, such as a SID big enough (config), enough data to hold the new (Zxid big enough). Each voter tells the other voters who they are recommending, similar to going out and courting other voters. Every voter finds someone better suited than himself to recommend this more suitable person. Finally, when most people agree, they can end the election.

It's so simple. In general, there is a sense of constant evolutionary approximation of results.

Of course, there will be some special cases to deal with. For example, a total of 3 voters, 1 and 2 have determined that 3 is leader, but 3 is not informed, at this point in LEADING/FOLLOWING the branch, the voter 3 is only to receive results.

Not all of the logic in the code is done in this big process. In the receiving message thread, it is also possible to respond to a node individually ( WorkerReceiver.run ):

As can be seen here, when a node has determined that the election results are no longer in the LOOKING state, it LOOKING will respond directly to the final outcome of the election when it receives the message. In combination with the above analogy, the equivalent of an election is over, and at this time the voter 4 has launched a new election, and other voters have directly told it the current leader situation. The equivalent, when the cluster master and slave is ready to open an instance, this instance will directly use the current election results.

State transitions

There are some key data structures on each node:

    • Current referrer, initial recommendation yourself, each time you receive other better referrals on the update
    • A collection of other people's votes to determine when the election is over

Each time a referral is updated, it broadcasts, and it is this constant broadcast that drives the entire algorithm toward results. Assuming that there are 3 nodes a/b/c, which have no data, according to the SID relationship is C>b>a, then according to the rule, C is more likely to become leader, the state of its various nodes is converted to:

Figure, V (A) indicates that the current recommendation is a;r[] represents the collection of votes received. It is important to note that the initial voting set contains its own vote, and the code will recommend it to itself, and the network module ( QuorumCnxManager ) puts the message directly into the receiving queue.

You can see when other nodes have determined the result of the poll, that is no longer LOOKING the state:

There is a special voting set in the code outofelection , which I understand to vote for the end of the election, which is used only to characterize the election results.

When a newly-launched node joins the cluster, it makes a poll request to the other nodes in the cluster, and the other node is no longer in LOOKING state, and the other node responds to the election result, which collects these results into the outofelection middle, and eventually receives the legitimate leader message and these ballot papers also form an election-closing condition, The node will end its own election behavior. Notice that the number of logicalclock = n.electionEpoch; election rounds is updated in the code

Finish

Graphic zookeeper Fastleader election algorithm "turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.