Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the election mechanism of Zookeeper?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What the election mechanism of Zookeeper is like, many novices are not very clear about it. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.

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? Are you curious? next, let's take you to find out.

The basic principles of human election

Before we explain the Zookeeper election process, let's introduce the human election.

Each of us has more or less gone through several elections, and we may encounter several situations in the process of voting:

Case 1: you are familiar with several candidates, and you will vote for people who you think are more capable.

Acquaintance election

Case 2: you are also a candidate and are not familiar with several other candidates. At this time, you must want to canvass votes, because you think you are the best person, and everyone should vote for me. But it is a pity that in the process of canvassing, you find that others are better than you, you begin to feel inferior, and finally vote for the person you think is the strongest.

Take part in the election yourself.

After all the votes have been cast, the votes are finally counted from the ballot box, and the person who gets the most votes is elected.

Mind map

Throughout the voting process, we can extract four core concepts:

Candidate ability: the basic principle of voting is to choose the strongest person.

Change your vote when forced: if you find a stronger person later, you can change your vote.

Ballot box: everyone's votes will be placed in the ballot box.

Leader: the person who gets the most votes is the leader.

From the principle of human election, let's briefly deduce the election principle of Zookeeper.

The basic principles of Zookeeper Election

Note that elections are not required if the Zookeeper is deployed standalone, but only in cluster mode.

The election principle of Zookeeper is similar to the logic of human election. Apply the four basic concepts of human election to explain Zookeeper in detail.

Individual ability

How to measure the individual capability of Zookeeper nodes? The answer depends on whether the data is new enough. If the newer the data of the node is, the stronger the personal ability of the node is. Is it strange that it is so decided?

In Zookeeper, transaction id (hereinafter referred to as zxid) is usually used to identify the new and old degree of data (version). The larger the latest zxid of the node, the newer the data of the node, the stronger the capability of the node.

The full name of zxid is ZooKeeper Transaction Id, that is, Zookeeper transaction id.

Change to invest when you are strong.

At the beginning of the cluster election, the node first thinks that it is the strongest (that is, the data is up-to-date), and then writes its name (including zxid and sid) on the ballot. Zxid is the transaction id,sid that uniquely identifies itself.

The ballots will then be passed to other nodes, and they will also receive ballots from other nodes. Each node will make a comparison after receiving the vote, whether this person is better than me (zxid is bigger than me), if it is stronger, then I need to change the ticket, obviously others are better than me, I can't be brazen, right?

Ballot box

Slightly different from human election ballot boxes, the Zookeeper cluster maintains a ballot box in the memory of each node. The node will put its own ballot paper and the ballots of other nodes in this ballot box. Because the ballots are passed on to each other, eventually the ballots in the ballot box of each node will be the same.

Leader

In the process of voting, we will count whether more than half of the votes are the same node as we have chosen, that is, we all think that a certain node is the strongest. Once more than half of the nodes in the cluster think that a node is the strongest, that node is the leader and the voting is over.

In what scenario does Zookeeper need an election?

When one of the following two situations occurs on a server in the Zookeeper cluster, you need to enter the Leader election.

(1) the server initializes and starts.

(2) Leader failure during server operation.

The Leader election in the start-up period

Suppose there are five servers in a Zookeeper cluster, with id numbered from 1 to 5, and they are all newly started and have no historical data.

The cluster has just started the election process.

Assuming that the server starts in turn, let's analyze the election process:

(1) Server 1 starts

Initiate an election, server 1 cast its own vote, at this time the server 1 vote, less than half (3 votes), the election can not be completed.

Voting result: server 1 is 1 vote.

The server 1 state remains LOOKING.

(2) Server 2 starts

When an election is initiated, server 1 and server 2 vote for themselves respectively, and server 1 finds that server 2 has a larger id than server 2, and changes the vote to server 2.

Voting result: server 1 has 0 votes and server 2 has 2 votes.

Server 1pc2 status remains LOOKING

(3) Server 3 starts

To initiate an election, servers 1, 2, and 3 vote for themselves first, and then because server 3 has the largest id, both change their votes to server 3.

Voting results: 0 votes for server 1, 0 votes for server 2, and 3 votes for server 3. At this time, the number of votes of server 3 has exceeded half (3 votes), and server 3 has been elected Leader.

Server 1, 2, changes state to FOLLOWING, server 3 changes state to LEADING.

(4) Server 4 starts

Initiate an election, at which time the server 1jie 2jin3 is no longer in LOOKING status and will not change the ballot information. The result of exchanging ballot information: server 3 has 3 votes and server 4 has 1 vote. At this time, server 4 obeys the majority and changes the ballot information to server 3.

Server 4 and change the state to FOLLOWING.

(5) Server 5 starts

Vote for 3 like server 4, where server 3 has a total of 5 votes and server 5 has 0 votes.

Server 5 and change the state to FOLLOWING.

The end result:

Server 3 is Leader, status is LEADING;, the rest of the servers are Follower, status is FOLLOWING.

Leader election during the run-time

Leader and non-Leader perform their respective duties during the Zookeeper operation. When a non-Leader server goes down or joins, the Leader will not be affected, but once the Leader server goes down, the whole Zookeeper cluster will suspend external service and trigger a new round of election.

In the initial state, server 3 is selected as Leader. Suppose server 3 is down now, and the zxid may be different on each server. The server1 is 99 Magi server2, 102 Magi server4, 100 Magi server5, 101.

Cluster Leader node failure

The run-time election is basically similar to the initial state voting process, which can be roughly divided into the following steps:

(1) status change. After a Leader failure, the remaining non-Observer servers will change their server status to LOOKING and begin the Leader election process.

(2) each Server will issue a vote.

(3) receive votes from each server, and will change the vote if the data of other servers is newer than their own.

(4) to process and count ballots. Ballots will be counted at the end of each round, and more than half of the ballots will be elected.

(5) change the status of the server and declare it elected.

Without saying more, let's start with a picture:

Election process after runner Leader failure

(1) when voting for the first time, each machine will vote for itself.

(2) each machine will then send its own vote to other machines, and if it is found that the zxid of other machines is larger than its own, then it will need to vote again. For example, server1 received three tickets and found that server2's xzid for 102 PK found that he had lost, and then decisively changed his vote to elect server2 as the boss.

Core concepts involved in the electoral mechanism

Knock on the blackboard, these concepts are required for the interview.

(1) Server id (or sid): server ID

For example, there are three servers, numbered as 1, 1, 2, 2, 3, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 and 4 respectively. The larger the number, the greater the weight in the selection algorithm, for example, when initializing the startup, it is compared according to the server ID.

(2) Zxid: transaction ID

The transaction ID of the data stored in the server, the higher the value, the newer the data, and the greater the weight in the election algorithm.

(3) Epoch: logical clock

Also known as the number of votes, the logical clock value in the same round of voting is the same, and this data increases after each vote.

(4) Server status: election status

LOOKING, campaign status.

FOLLOWING, attendant status, synchronize leader status, participate in voting.

OBSERVING, observe status, synchronize leader status, do not participate in voting.

LEADING, leadership status.

Summary

(1) the Zookeeper election will occur in the initial state and running state of the server.

(2) in the initial state, according to the number comparison of the server sid, the larger the number, the greater the weight, and the Leader can be selected by more than half of the votes.

(3) Leader failure will trigger a new round of election, and the newer the zxid representative data, the greater the weight.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report