Categories
Uncategorized

Zookeeper’s Leader election process

Leader is a very important role in the cluster, is responsible for the processing and scheduling of the entire transaction to ensure consistency of distributed data key. Since the Leader in ZooKeeper cluster is so important and so make sure the cluster at all times and only the presence of a Leader.

concept

Zookeeper Server three roles: Leader, Follower, Observer.

Leader Zookeeper is the core mechanism of the cluster, the main work:

    a dispatcher: internal nodes of the cluster each service dispatcher

    b transaction request: scheduling and unique transaction request handlers, assure an orderly cluster transaction

Follower main responsibilities:

    . A non-transactional request: Follower directly with non-transaction requests for transaction request, forwarded to Leader

    b.Proposal vote: when the transaction is executed on the Leader, Follower need to vote, Leader really executed

    c.Leader elections

Observer main functions:

    . A non-transactional request: Follower directly with non-transaction requests for transaction request, forwarded to Leader

Observer with Follower difference:

    a.Follower vote: Leader election, Proposal proposed vote (transaction execution confirmation)

    Only it provides treatment for non-transactional requests: b.Observer not vote

The state Zookeeper Server

    LOOKING: looking Leader

    LEADING: Leader status, the corresponding node Leader.

    FOLLOWING: Follower state, the corresponding node Follower.

    OBSERVING: Observer status for the corresponding node Observer, the node does not participate in Leader election.

Other concepts

    ZXID (zookeeper transaction id): Zookeeper each change state operations will form a corresponding zxid, and recorded in the transaction log. The larger this value is, the more new update

    myid: server SID, a digital, through the configuration file to configure, the only

    The unique identity of the server: SID

    Leader becomes necessary conditions: Leader to have the highest zxid; when the cluster size is n, cluster Most machines (at least n / 2 + 1) and obtained in response to the selected Follow Leader.

    Heartbeat mechanism: Leader and Follower use PING to perceive the other’s is alive, not when the corresponding PING Leader, will re-initiate Leader election.

Elections there are two cases, one is server-initiated vote, and second vote during operation.

Leader election server startup period

1. Each server sends a vote (SID, ZXID)

Where sid is their myid, the initial phase will vote for the Leader himself.

2. receiving votes from other servers.

After each vote received server cluster, first determine the validity of the vote, such as checking whether the current round of voting, whether from the state LOOKING server.

3. Processing Vote

For each of the following voting rules are voting with their PK, PK after case basis whether to update the vote, and then sent to other machines.

    a. Priority Check ZXID, ZXID greater priority to Leader

    b. If ZXID same check SID, SID priority is greater Leader

5. Voting Statistics

After each vote, the vote of all server statistics to determine whether there is more than half of the voting machines receive the same, if a vote to reach half of the requirements is considered to be the author of the vote to become Leader.

6. Change Server Status

Once the Leader, each server can update their status, Leader changed to Leading, Follower changed Following
    Under normal circumstances, once it has been selected a Leader will remain, unless the Leader server dawdle off, then re-re-election.

Leader election runtime server

1. Change status

When the Leader is down, the rest of the non-Observer own server will change the status Looking, then open a new Leader of the election process.

2. Each server issues a vote.

Generation (SID, ZXID) information, attention ZXID during operation may be different, but at the time of the vote will be cast as the Leader himself, and then sent to other servers.

3. receives votes from each server

When you start the process with the same

4. The voting process

When you start the process with the same

5. Voting Statistics

When you start the process with the same

6. Change Server Status

When you start the process with the same

data

    Java collection problems

    ZooKeeper Inside: Leader election

Leave a Reply