ZooKeeper Technology Insider | What is the process of Leader election?

A few questions for thought:

  1. When is the leader election?

  2. The election process?

  3. Are services available during the election process?

  4. Election results, will data be lost?

server role

2 small questions:

  1. How many roles does a server node have?

  2. What does each role do?

Role

In the ZK cluster, the server node has 3 roles:

  1. Leader: The core of the ZK cluster working mechanism, the main tasks are:

    • Scheduler: the scheduler of each service node in the cluster

    • Transaction request: the only scheduler and processor of transaction requests, ensuring the sequence of cluster transaction processing

  2. Follower: Main Responsibilities:

    • Non-transactional requests: Follower directly processes non-transactional requests, and forwards transactional requests to Leader

    • Proposal voting: When the leader executes the transaction, the follower vote is required, and the leader will actually execute it

    • Leader Election Vote

  3. Observer: The ZK  3.3.0+ version has been introduced to improve the non-transactional processing capabilities of the ZK cluster. The main responsibilities are:

    • Non-transactional requests: Follower directly processes non-transactional requests, and forwards transactional requests to Leader

Special Note: The only difference between Observer and Follower:

  1. Follower participates in voting: Leader election, Proposal proposal voting (transaction execution confirmation)

  2. Observer does not participate in voting: it is only used to provide processing of non-transactional requests

Question: Is it set in the configuration file whether a node becomes a Follower or an Observer?

Leader election

2 small questions:

  1. When will the Leader election be held?

  2. What is the specific process of Leader election?

opportunity

Any of the following situations will trigger Leader election:

  1. At startup, the cluster servers just started

  2. Leader crashes when running

The state flow of the server:

process

The essence of the Leader election process is 优先级消息the process of broadcasting. The service node with the latest data is selected, and the service node with the highest priority is selected . The basic steps are:

  1. Each server node broadcasts its own priority identification (sid,zxid)

  2. After the server node receives other broadcast messages, it compares with its own priority and if its own priority is low, it changes the priority of the current node’s voting (sid,zxid) and broadcasts the changed result

  3. When the number of votes received by any server node exceeds 法定数量(quorum), it will be upgraded to Leader and broadcast the result.

Question: 法定数量(quorum), generally set to more than half of the cluster size, where is quorum configured?

Special Note:

  1. Priority identification of server nodes:(sid,zxid)

  2. Compare  zxid (transaction ID) first, compare sid(server ID) second

  3. sid (server ID) is set in the node configuration file

  4. When is the current server  zxid set? Is it synchronized to the current server during the Leader's execution of the transaction? How live on the current server  zxid?

Specific election process:

Supplementary note:

  1. Due to network delay, when nodes do not get enough broadcast information, they will make wrong voting judgments, and the correction process is more time-consuming

  2. During the election process, the server node will wait for a certain period of time before broadcasting the voting information. The time interval is generally set as 200 ms

  3. In the above Leader election, an event is used to trigger a  Push 方式 broadcast message, which is called  快速 Leader 选举because the previous Leader election adopts  Pull 方式and pulls every other  1s time.

doubt:

Server node, what is the starting point of waiting  200ms ? Did you receive a new round of voting news and start counting?

RE:

  1. Under normal circumstances, voting information is 事件触发broadcast;

  2. When a server node judges that the number of votes supporting the current node is >=  法定数量(quorum), it still waits  200msto confirm whether there is a better vote.

Real voting information:

Attributes illustrate
id The sid of the elected Leader
zxid The transaction ID of the elected Leader
electionEpoch The number of rounds of voting, constraints: the same round of voting, the count is valid
peerEpoch The epoch of the elected Leader
state current server status

A Leader election process belongs to the same one  electionEpoch. At the end, a new Leader will be elected; the server node  (sid,zxid) will compare the election rounds before comparison  electionEpoch, and only the Leader voting information of the same round is valid:

  1. External voting round > internal voting round, update the internal voting, and trigger the rebroadcast of the current node voting information

  2. External voting round < internal voting round, directly ignore the current external voting

  3. Outer Voting Rounds = Inner Voting Rounds, for further comparison (sid,zxid)

Question: Leader is responsible for performing all transaction operations, one transaction operation,

  1. How does Leader synchronize transaction operations to Follower and Observer? Synchronous Asynchronous?

  2. How to ensure that the transaction must be executed successfully during the synchronization process? Impact of transaction failure?

The transaction status executed on the Leader is  Zab updated to Follower and Observer through the status update broadcast protocol.

appendix

Distributed System Leader Election: Split Brain

Split brain: Two subsets of the server cluster can independently elect a leader at the same time and run normally, forming two clusters.

Solution: Leader election 法定数量(quorum) exceeds half of the normal cluster.

The necessary condition for Leader election: the number of nodes >  法定数量.

Guess you like

Origin blog.csdn.net/z_ssyy/article/details/128721879