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

How does Zookeeper solve the problem of brain fissure?

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

Share

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

What is a brain fissure?

Split-brain is "brain split", that is, a "brain" is split into two or more "brains". We all know that if a person has more than one brain and is independent of each other, it will cause the human body to "dance" and "not listen".

Brain fissure usually occurs in cluster environments, such as ElasticSearch and Zookeeper clusters, and these cluster environments have a unified feature, that is, they have a brain, such as Master nodes in ElasticSearch clusters and Leader nodes in Zookeeper clusters.

This article focuses on the problem of brain fissure in Zookeeper and how to solve it.

Brain fissure scene in Zookeeper Cluster

For a cluster, multi-server room deployment is usually used to improve the availability of the cluster. For example, there is a cluster composed of 6 zkServer deployed in two data centers:

Under normal circumstances, there will only be one Leader in this cluster, so if the network between the two data centers is down, the zkServer in the two data centers can still communicate with each other. If more than half of the mechanism is not considered, a Leader will be selected in each data center.

This is equivalent to the original cluster, was divided into two clusters, the emergence of two "brains", this is the brain fissure.

In this case, we can also see that what was supposed to be a unified cluster to provide services to the outside world has now become two clusters providing services at the same time. If after a while, the disconnected network is suddenly connected, then there will be problems. The two clusters have just provided services, how to merge data, how to resolve data conflicts, and so on.

When explaining the brain fissure scenario just now, a prerequisite is that more than half of the mechanism is not considered, so in fact, the problem of brain fissure will not occur in the Zookeeper cluster, and the reason why it will not occur is related to the more than half mechanism.

More than half mechanism

In the process of leader election, if a zkServer gets more than half of the votes, then the zkServer can become a Leader.

The source code implementation of more than half of the mechanism is actually very simple:

Public class QuorumMaj implements QuorumVerifier {

Private static final Logger LOG = LoggerFactory.getLogger (QuorumMaj.class)

Int half

/ / n indicates the number of zkServer in the cluster (exactly the number of participants, which does not include observer nodes)

Public QuorumMaj (int n) {

This.half = nmax 2

}

/ / verify whether it conforms to the more than half mechanism

Public boolean containsQuorum (Set set) {

/ / half is assigned in the constructor

/ / set.size () indicates the number of votes obtained by a certain zkServer

Return (set.size () > half)

}

}

If you take a closer look at the comments in the above method, the core code is the following two lines:

This.half = nmax 2

Return (set.size () > half)

To take a simple example: if there are five zkServer in the cluster, then half=5/2=2, that is to say, in the process of leader election, at least three zkServer must vote in the same zkServer to meet the more than half mechanism and a Leader can be selected.

Well, there is a question: why must there be a more than half mechanism for verification in the process of election?

Because you don't have to wait for all zkServer to vote for the same zkServer before you can elect a Leader, it's faster, so it's called a fast leader election algorithm.

So let's think about another question, why is it greater than, not greater than or equal to, in the more than half of the mechanism?

This is more relevant to the problem of brain fissure, such as going back to the scene of brain fissure above:

When the network in the middle of server room is cut off, the three servers in server room 1 will elect leaders, but the condition of more than half of the mechanism is set.size () > 3, that is, at least 4 zkServer can be selected to select a Leader, so for computer room 1, it cannot select a Leader, nor can it select a Leader. In this case, when the network of the whole cluster crash room is cut off, there will be no Leader in the whole cluster.

If the condition of more than half of the mechanism is set.size () > = 3, then both computer room 1 and computer room 2 will choose a Leader, resulting in a brain fissure. So we know why more than half of the mechanism is greater than, not greater than or equal to. To prevent a brain fissure.

If we assume that we now have only five machines and are also deployed in two computer rooms:

At this time, the condition of more than half of the mechanism is that set.size () > 2, that is, at least 3 servers are needed to select a Leader. At this time, the network of server room components is disconnected, which has no effect on data center 1. Leader is still Leader. For server room 2, Leader cannot be selected. There is only one Leader in the whole cluster.

So, we can conclude that with more than half of the mechanism, for a Zookeeper cluster, there is either no Leader or only one Leader, which avoids the problem of brain fissure.

Recommended reading

Four seasons of gold, silver and gold, Ali has worked for more than 10 years and the "experience" of Java Daniel is dedicated to you in confusion.

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