Leader election in ZooKeeper
|
NOTE
The concepts and the main components of ZooKeeper are described in the ZooKeeper architecture article. |
ZooKeeper is a centralized service used to store information about services configuration.
The atomic messaging system at the heart of ZooKeeper is based on the principles of the consensus algorithm.
Consensus algorithm — a set of principles and rules, due to which all nodes participating in the cluster automatically come to a consensus on the current state of the network.
ZooKeeper implements one of the consensus algorithm protocols — ZAB (ZooKeeper Atomic BroadCast).
Consensus algorithm protocol
The ZAB protocol ensures that ZooKeeper replication occurs in order, and is also responsible for selecting lead nodes and recovering any failed nodes.
The main provisions of ZAB are described below.
Message exchange
In the messaging part, ZAB uses the following concepts:
-
Packet is a sequence of bytes sent over the FIFO channel.
-
Message is a sequence of bytes that will be atomically broadcast to all ZooKeeper servers. The message is placed in the offer and agreed upon before it is delivered.
-
Proposal is a unit of agreement. Proposal are agreed by exchanging packets when there is a quorum of ZooKeeper servers.
The proposal approval messaging model used by ZooKeeper is shown below.
To communicate with replicas, proposal approval uses a model in which each new record requires at least three transactions: a proposal (propose), an acknowledgment (ack), and a commit.
Message delivery principles
The ZAB protocol involves the creation of FIFO channels (in each session, the server executes client requests one by one in the order in which they are received) of the point-to-point type between servers. The TCP protocol is used for communication, providing the following properties:
-
Reliable delivery — if message
mis delivered by one server, it will eventually be delivered by all servers. -
Common order — if message
ais delivered before messagebby one server, thenawill be delivered beforebby all servers. If messagesaandbare delivered, then eitherawill be delivered beforeborbwill be delivered beforea. -
Causal order — if message
bis sent after messageawas delivered by senderb, messageamust be ordered beforeb. If the sender sendscafter sendingb,cmust be ordered afterb. -
Ordered delivery — data is delivered in the same order as it was sent, and message
mis only delivered after all messages sent beforemhave been delivered. The implication of this is that if themmessage is lost, all messages aftermwill be lost. -
No message after close — once a FIFO channel is closed, no messages will be received from it.
The use of timeouts ensures that consensus is reached in the presence of failures.
Controller epoch
Every cluster has one leader node and the remaining nodes are followers.
Below is the general sequence for a leader election.
-
A proposal (propose) of a new epoch arrives from the server, and each proposal is assigned its own zxid.
-
Every node acknowledges (ack) the offer only if it does not know any other leader with a higher epoch number in zxid, or if the epoch is the same, with a higher transaction counter. Before a leader can be chosen, it must collect votes from a quorum of nodes. Quorums to determine the leader must be
(n/2+1), wherenis the number of servers that make up the ZooKeeper service. -
After the quorum is confirmed, the new leader commits the creation of a new epoch and sets the next zxid to use.
-
The follower will record the formation of a new epoch.
Each phase of the controller epoch is described below.
Each epoch consists of three phases, and each node can be in one of these three phases at any given time:
-
Leader election — leader election phase based on current quorum configurations. There can be at most one leader at any given time. The end of the phase comes after the confirmation of the new epoch by all followers.
-
Synchronization — synchronization phase, during which the new leader synchronizes the available replicas with the previous epoch and with all the followers as a leader. The end of the phase occurs after the quorum of followers has recognized that they are in sync with the leader.
-
Broadcast — translation phase, the normal mode of operation in which the leader continues to offer new client requests. The end of the phase occurs after the leader failure.