This article is reproduced from the WeChat public account "Third Prince Ao Bing". Please contact the Third Prince Ao Bing public account for reprinting this article. Preface Zab (Zookeeper Atomic Broadcast) is a crash recovery atomic broadcast protocol designed for ZooKeeper. It ensures the consistency of Zookeeper cluster data and the global order of commands.
Concept Introduction Before introducing the zab protocol, you must first know several concepts related to zookeeper in order to better understand the zab protocol.
It can be seen that Zookeeper distinguishes its own role through its own status and performs its own tasks.
Zxid is an extremely important concept. It is a long (64-bit) integer divided into two parts: the epoch part and the counter part. It is a globally ordered number. Epoch represents the leader of the current cluster. The election of a leader is similar to the change of dynasties. The sword of the previous dynasty cannot kill the officials of the current dynasty. Epoch represents the validity of the current command, and counter is an increasing number. election Now that we have introduced the basic concepts, let’s start by explaining how the Zab protocol supports leader election. There are three questions about the leader election: when will it be held? What are the election rules? What is the selection process? I will answer these three questions one by one below: 1. When the election occurs There are two times when the leader election occurs. One is when the service is started. When there is no leader node in the entire cluster, it will enter the election state. If the leader already exists, the node will be told the leader's information and connect to the leader itself, and the entire cluster does not need to enter the election state. Another thing is that various situations may occur during service operation. When the service crashes, power outages, or network latency is very high, the leader can no longer provide services to the outside world. So when other points detect that the leader is lost through heartbeats, the cluster will enter the election state. 2. Election rules Entering the voting process, how can you elect a leader? Or what rules should be followed to make other nodes elect you as the leader? 3. The zab protocol screens votes according to several comparison rules. If your vote is better than mine, it will modify its own voting information and vote for you as the leader. The following code is the zookeeper voting comparison rule:
When the epoch of other nodes is higher than its own, it will be voted. If the epochs are the same, compare the sizes of their own zxids and elect the node with the larger zxid. Here, zxid represents the largest id of the transaction submitted by the node. The larger the zxid, the more complete the data of the node. Finally, if epoch and zxid are equal, the serverId of the service is compared. This ID is configured by the zookeeper cluster, so when we configure the zookeeper cluster, we can configure the serverId of the cluster with higher service performance to be larger, so that the machine with good performance can play the leader role. Election Process Now that we have the timing and rules, here is the leader election process:
Example The above picture comes from "ZooKeeper: Detailed Explanation of Distributed Process Collaboration Technology". The overall process is relatively simple, so I will not analyze it in detail here. broadcast After the leader election, the cluster will have two more steps: connecting to the leader and synchronizing. We will not analyze the processes of these two steps in detail here, but will mainly introduce how the cluster ensures the consistency of data on each node when providing external services. Zab ensures the following characteristics in the broadcast state
Order is a very important property that the Zab protocol must guarantee, because Zookeeper stores data in a data structure similar to a directory structure, and naming order must be required. For example, if a is named as /test and then b is named as /test/123, if the order cannot be guaranteed and b is named before a, the b command will fail to create because the parent node does not exist. As shown in the figure above, the entire write request is similar to a two-phase commit. When a write request is received from a client, the following steps are performed:
From the above process, we can see that Zookeeper ensures the consistency of data in the cluster through two-phase commit. Because transactions can be committed only after receiving more than half of the ACKs, the data in Zookeeper is not strongly consistent. The orderliness of the zab protocol is ensured in several aspects. First, the TCP protocol is used for communication between services to ensure orderliness in network transmission. Second, a FIFO queue is maintained between nodes to ensure global orderliness. Third, causal orderliness is ensured through the globally incremented zxid. State Transition As mentioned above, there are four types of Zookeeper service states and four types of ZAB states. Here we will briefly introduce the state transitions between them, which can help you better understand the role of the ZAB protocol in the Zookeeper workflow.
It can be seen that the workflow of the entire Zookeeper service is similar to the transition of a state machine, and the Zab protocol is the key to driving the flow of service states. If you understand Zab, you will understand the key principles of Zookeeper's work. Summarize This article briefly introduces the zab protocol in the workflow of zookeeper, hoping to help everyone understand and learn zookeeper. I’m Ao Bing, a tool who makes a living on the Internet. |
<<: 5G and IoT: Compatible with each other
>>: 5G and IoT: The mobile broadband future of IoT
Internet speed is getting slower and slower? The ...
Part 01, ECMP ECMP is a hop-by-hop, flow-based lo...
After 14 years of "three-kingdom melee"...
Alibaba Cloud (aliyun) launched another Golden Au...
RAKsmart is an early-established foreign hosting ...
Mr. Dongguo and the wolf, Lu Dongbin and the dog,...
In addition to the rapid development and wide cov...
Launchvps is a foreign VPS service provider estab...
On August 2, the 2017 Xiaoyu Yilian E=mc² new pro...
On June 24, according to foreign media reports, S...
Smart cities aim to achieve greater efficiency, s...
IPV4 resources have been exhausted and there is n...
No, you read that correctly - 6G. Considering tha...
Ethernet technology, also known as IP technology,...
Power outages are happening all over the country,...