In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to analyze ZooKeeper clusters. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
First, why do you need clusters
1.1 Ma Guoguo is sick
Old rules of ZKr~ ~
Ma Guoguo was old after all, and there were more and more things in this office. Finally, one day he couldn't bear it. He got sick and was hospitalized. He heard the doctor say that he would rest for several days. In the absence of the head of the office, we will not be able to provide services to the villagers. Everyone should pay attention to their health at ordinary times.
Kun Kun, who had not been notified for several days, was worried to death, and other villagers who were very dependent on the office went to the village committee to complain. The village committee was also very helpless, and finally discussed it. Ma Xiaoyun, a famous entrepreneur with high prestige in the village, Tai Chi enthusiast and the richest man in the village, Ma Xiaoyun, set up a second office, which was also very close to the original office. Also responsible for handling the affairs of the previous Ma Guoguo office. Ma Xiaoyun was also a regular customer of the office before, and he was very clear about the process. He, who always wanted to do practical things for the people, gladly agreed to it.
And you already have the previous successful experience, so just copy the previous process. But the careful villagers soon discovered the problem. Many affairs that had been recorded in Ma Guoguo's office have now disappeared, and it is very inconvenient to re-register here in the new office, but Ma Xiaoyun said there is nothing he can do. Ma Guoguo is so sick that he has not been handed over yet, so it can only show that everyone can endure it.
In this way, after two weeks, Ma Guoguo recovered and was discharged from the hospital. during his stay in hospital, he also learned that Ma Xiaoyun had helped the villagers solve the big and small affairs on his behalf in the past two weeks. He was very grateful to Ma Xiaoyun for what he had done, and he, who loved his work, immediately returned to work, reopened the door of the office, and told the villagers that he could handle affairs here again. I hope everyone can continue to come over. After all, Ma Xiaoyun's business capacity is slightly worse, and the processing speed is not so fast, which leads to more serious queues in the second office.
The villagers who heard that the first office had opened again were very happy. After all, no one wanted to wait in a long line to waste time, so they all came to the first office.
However, Ma Guoguo took a two-week rest, and during these two weeks, he did not have any registration matters for the villagers. The villagers said that this would not work: "No matter how many offices you have, you have to make sure that the data are consistent!" The village committee also agreed with the villagers' demands and ordered the two offices to rectify and reform, and this problem needs to be solved! And because Ma Guoguo is older and more experienced, let Ma Xiaoyun listen to Ma Guoguo's command, and the plan is also put forward by Ma Guoguo.
1.2 New regulations on Ma Guo Guo
Ma Guoguo deserved that the ginger was still old and spicy, and he soon came up with a good idea and issued a series of rules:
The data must be based on horse fruit.
The two offices need to get in touch with each other and keep communication at all times.
In the past, it was a very right decision to distinguish between reading and writing for the registration of villagers. After that, the write operation must be done through Ma Guoguo, and the read operation Ma Xiaoyun can solve it on his own.
But the introduction of rules is not enough, but also need a series of operations that can be landed on the ground, so Ma Guoguo applied to the village committee, the office needs to expand recruitment, the village committee decided to let Ma Guoguo let go and agreed to his application.
Ma Guoguo readjusted the layout of his office like this:
A brief introduction to the new colleagues:
Little PS is responsible for distinguishing whether the villager's request needs to initiate a proposal, and forwards the request to Xiao C and Xiao S again.
Xiao C is responsible for managing the submission of small PS proposals, and this position is very important, so Ma Guoguo selfishly hired a girl to take up the position.
Now Xiao S no longer deals with Xiao F but deals with Xiao A. He will inform Xiao An after he has finished filing.
Since there are now two offices, we need to hire an operator to communicate with Ma Xiaoyun's office next door.
It is not enough to arrange yourself. Ma Guoguo has also designed a new office plan for Ma Xiaoyun:
Unlike Ma Guoguo, here is a brief introduction:
Use the small FR to replace the original small P as the first receptionist of the office.
Xiao S no longer deals with Xiao F, but directly deals with Xiao SA. He will inform Xiao SA when he has finished filing.
Like Ma Guoguo, he also hired an operator to contact Ma Guoguo.
It turned out that only Ma Guoguo was responsible for an office, but with Ma Guoguo's illness, the business of the villagers could not continue, which is a single point of failure, so an office was added on the original basis. it can increase the throughput of the entire office while at the same time, when an office is unable to provide services, it will not lead to the villagers being unable to use it, which is high availability. This is the most important reason for the need for cluster deployment!
Second, the first office
Before the introduction of the cluster, the internal operation and management of the data of a node was very convenient, but after the introduction of the cluster, how to communicate between the nodes in the cluster has become a problem. Let's take a look at how the new employees of Ma Guoguo do it.
Different from the previous stand-alone process, now the process is much more complicated, and a lot of characters have been added. In order to let you remember quickly, I will give you a spoiler of the origin of the name in advance:
The PrepRequestProcessor in the corresponding code of Xiao P is responsible for preprocessing.
The small PS corresponds to the proposal that ProposalRequestProcessor in the code is responsible for writing the transaction.
The CommitProcessor in the corresponding code of Xiao C is responsible for submitting the transaction request.
The SyncRequestProcessor in the corresponding code of Xiao S is responsible for archiving the data.
AckRequestProcessor in the corresponding code of Xiao An is responsible for telling Ma Guoguo the ACK information of the current transaction.
The FinalRequestProcessor in the corresponding code of Xiao F is responsible for the operation of the memory model.
2.1 small PS in charge
Previously, after Xiao P asked the villagers immediately and marked the request, he forwarded the request to Xiao PS, and what Xiao PS did was also very simple:
The main thing is to see whether to write a request, if so, to initiate a proposal and notify Xiao S locally for filing.
2.2 busy Little C
Besides Xiao F, Xiao C is the busiest person. After receiving the request from her last colleague, she will:
Didn't you say that Xiao C is the busiest? That's it?
Don't worry, the processing process of Xiao C is indeed quite tedious, but I will first give a simple process, the most important submission operation, I will not expand for the time being, I will talk about it later.
2.3 Xiao S and Xiao A
The process of Xiao S's processing has changed, and his colleague in front of him is no longer Xiao P, and after he has finished the filing, he no longer gives the request to Xiao F but to Xiao A, while Xiao A does more simply, just telling Ma Guoguo Office that the request is successful, which is actually ACK.
2.4 operator
In order to communicate more smoothly with Ma Xiaoyun's office next door, Ma Guoguo set several codes, while the operator was responsible for notifying Ma Xiaoyun with the code.
REQUEST
PROPOSAL
ACK
COMMIT
Of course, the code is more than that, and we'll talk about it later when we encounter it.
Before we expand the details of the process, I think we should give a brief introduction to Ma Xiaoyun's process. After the introduction of both sides, we will explain it together.
III. Second Office
Similarly, because of the relationship between the two offices, Ma Xiaoyun cannot simply use the previous process, and there are obviously different small FR and SA from Ma Guoguo among the new employees, which are also introduced here:
The FollowerRequestProcessor in the corresponding code of Xiao FR is responsible for the preprocessing of Ma Xiaoyun's side.
The small SA corresponds to the SendAckRequestProcessor in the code similar to Ma Guoguo's small A, notifying Ma Guoguo of the ACK of the current transaction through the operator.
3.1Little FR and SA who are equally careful
Similar to small PS, it also needs to distinguish between read and write, but the difference is that writing requests need to notify Ma Guoguo.
The logic of small SA is that after receiving the archived information from Xiao S, inform Ma Guoguo of ACK. It is too simple to draw a picture.
IV. Actual combat
We have just introduced the logic of the two offices, but it is too fragmented and simple, so let's move on to the actual combat session, which will assume different business scenarios and complexity, from simple to complex. Sort out the logic from the villagers to the office registration to the completion of the office processing in chronological order.
Front row reminder, multi-map early warning
4.1 one read request (Ma Guoguo)
Suppose our Kun Kun comes to Ma Guoguo's office and wants to inquire about Chicken Taimei's latest dance video / Chicken Taimei / Dance.
Xiao P first knew that Kun Kun was a legitimate villager, and then asked that the purpose of coming to the office was to inquire, so he would mark the registration as a read request, and handed Kun Kun's request to the little PS at the next counter.
After the little PS got the request, he first gave the request to Xiao C intact, and then knew through the mark of Xiao P that it was a read request, so he did not do any other processing.
After getting this request, Xiao C also found that it was a read request, so he gave it directly to Xiao F and did not need any other processing.
Xiao F took out the little red book and looked at it, assuming that / the chicken was too beautiful / dancing existed, and returned the corresponding data to Kunkun.
Kun Kun got the result and went back with satisfaction and set the 17:00 alarm clock in front of the computer waiting for the beautiful chicken to start broadcasting.
You can see that the processing flow of a read request is very simple. Don't worry, it will be a little more difficult.
4.2 one read request (Ma Xiaoyun)
It is also our Kun Kun, but when I come to Ma Xiaoyun's office this time, I also want to inquire about the latest dance video / Chicken too Beautiful / Dance.
Unlike Ma Guoguo, the first person to deal with Kunkun's request is Xiao FR. He will first send the request to Xiao C, and then he will not do any other processing when he learns that the purpose is to query. You may ask, doesn't little FR need to verify Kun Kun's identity? I think it may be because the current read request does not cause damage to the data, so there is no verification.
There is no difference between the later versions of Xiao C and Xiao F and Ma Guo Guo, so I won't repeat them. Let's move on to the next difficulty.
4.3 one write request (Ma Guoguo)
The write request is different from the read request, because according to Ma Guoguo's rules, the data of the two offices have to be consistent, so it will involve how to notify each other, let's take a look.
Suppose our Kun Kun comes to Ma Guoguo's office and wants to create a transaction registration / Kun Kun / diary for himself.
Xiao P knew that Kunkun was a legitimate villager and that Kunkun's purpose was to write data this time, so he marked Kunkun's request as a transaction and gave it to Xiao PS.
Little PS still gives the request to Xiao C to deal with first.
When Xiao C saw that this was a request, he took out his own small book and wrote it down.
Little PS has learned that this is a write request. Be careful! It's different here from the beginning. Little PS will ask the operator to call Ma Xiaoyun's office.
The operator told them about the request with the code of PROPOSAL.
The transaction number must be mentioned here. In order to strictly ensure the order in which the villagers come to register, Ma Guoguo also stipulates that each write transaction must be assigned a unique incremental number, starting from 0.
And while notifying Ma Xiaoyun, Ma Guoguo will also record the current proposal:
At this time, we switch our perspective to Ma Xiaoyun's side. After Ma Xiaoyun's operator receives Ma Guoguo's PROPOSAL code, he will directly ask his own side of Xiao S to file, and Ma Xiaoyun will record it in the memo:
After Xiao S has finished filing, he will give Kun Kun's request to Xiao SA.
Little SA, the simple thing is to ask the operator to inform Ma Guoguo that the filing is complete.
Then the operator on Ma Xiaoyun's side will call Ma Guoguo's office to inform them that the filing is complete.
Once again, the perspective went back to Ma Guoguo. When Xiao PS asked the operator to notify Ma Xiaoyun, Xiao S was not idle and carried out the archiving operation.
After the filing is completed, Xiao S will give the request to Xiao A, and what Xiao A does is simply inform Ma Guoguo that the filing is completed.
We assume here that the small S on Ma Guoguo's side has been archived. After receiving the archiving completion message, Ma Guoguo will take out the notebook to find the corresponding proposal record, and write down what has been archived:
Because Ma Guoguo knows that there are two offices, he still needs to wait for Ma Xiaoyun's notice of completion of the filing.
After a while, Ma Xiaoyun's filing notice also came, and he wrote it down again in the notebook.
At this point, the two offices have completed the filing of the current proposal, Ma Guoguo will ask the operator to inform Ma Xiaoyun that he can submit it, and will delete the record of transaction 0 on the notebook (the picture will not be drawn).
After the notification, Ma Guoguo let Xiao C submit it.
Xiao C will take out the memo just now and find the first item of Kunkun's waiting transaction (there is only one item in the current scene): create / Kunkun / diary. The matter was immediately handed over to Xiao F to handle.
Xiao F will record the current transaction in the little red book:
After giving it to Xiao F, Xiao C found that all Kunkun's affairs had been dealt with and deleted him from the memo:
Let's cut the perspective back to Ma Xiaoyun. When dealing with Xiao C on Ma Guoguo's side, Ma Xiaoyun's operator received a COMMIT message from Ma Guoguo and told Ma Xiaoyun. Ma Xiaoyun will find the earliest request from the memo: Kun Kun, create, / Kunkun / diary, and then give the request to Xiao C
As for the processing of Xiao C and leaving it to Xiao F after processing, the logic is the same as that of Ma Guoguo, so I won't repeat it.
At this point, the basic process of writing a request (Ma Guoguo) is complete.
4.4 A write request (Ma Xiaoyun)
Suppose our Kunkun comes to Ma Xiaoyun's office this time and also creates a transaction registration / Kunkun / diary for himself.
Little FR first gave the request to Xiao C, and then found that Kunkun was writing the request this time, and would ask the operator to inform Ma Guoguo.
The operator will call Ma Guoguo's office and tell them about the request and bring the REQUEST code.
And Xiao C on Ma Xiaoyun's side will be recorded in the memo, just like the previous example.
Now let's cut the perspective to Ma Guoguo. After accepting REQUEST's request, the operator told Ma Guoguo, and Ma Guoguo will directly hand the request to Xiao P to deal with.
As if Kun Kun came directly to his office to handle business, the process since childhood P can be said to be exactly the same as the previous example, so I don't want to repeat it.
I now list four non-concurrency scenarios, all of which are very simple except for writing requests. Here, I will redraw the write request Ma Guoguo.
In order to omit the operator in the story as well as Ma Guoguo and Ma Xiaoyun in the simplified picture, the same color represents processing at the same time, and the time order is from small to large.
All right, let's continue to increase the difficulty to enter the concurrent actual combat, as a public office is impossible to deal with only one transaction at the same time!
More than 4.5 villagers have multiple requests.
I will not start drawing from scratch in this chapter. I will only draw the important parts. Let's take a look at how to remember those small books if there are many villagers.
Summary of the characteristics of Xiao C's memo:
Villagers are used as key, and subsequent requests are queued in the order of requests.
The first request in the queue must be a write request, and if it is a read request, it will not be recorded at all
When the request team corresponding to the villager is listed as empty, the whole record is deleted
Let's take Kun Kun in the picture as an example, assuming that the waiting request is like this (I omitted the path, here I only care about the type of transaction)
When the first created request is queued, the subsequent query request cannot be executed, and it needs to wait until the creation request is completed before it can continue, so when the first created request is submitted, the subsequent query 1, query 2, and query 3 will be removed from the queue and executed immediately, while query 4 will not be executed until the previous deletion and creation are all submitted.
This logic ensures that the requests of the same client are executed in chronological order, and the late read requests will not be executed before the previous write requests, resulting in dirty readings. However, it should be noted that the order of different clients cannot be guaranteed. It is possible that the query operation of things can be returned before Kun Kun's creation request is submitted.
Ma Guoguo's notebook is like this if there are multiple records.
You may ask: the record of the beauty of the chicken has not been filed, why is it still in the notebook? Because ZK must guarantee the order in which transactions are executed! Therefore, as long as there are other transactions that are still uncommitted than the current transaction number, this transaction cannot be committed. In the picture, Ji Taimei has to wait until Kun Kun and things have been submitted.
Ma Xiaoyun also has a memo here, which would be like this if there are multiple records:
This memo is actually a first-in-first-out queue, and each submission by Ma Xiaoyun removes the first record from the queue to operate.
That's almost the end of the story, and with some details in the programmer's language, I actually omitted two processors:
ToBeAppliedRequestProcessor is the processor that follows Xiao C on Ma Guo's side, but I personally feel that it is useless, so I won't talk about it. If you are interested, you can learn about it.
LeaderRequestProcessor is the first processor of Ma Guoguo. His logic is related to session and ACL, but nothing else is useful. I'll talk about it later.
Let's take a look at this picture first:
The objects marked with red boxes are all thread objects, and the main logic is in the run method. We have talked about Xiao P and Xiao S before, and here we have more small C and small FR. I have to mention here that whenever you see a thread object in ZK, it basically uses the producer-consumer model and maintains a blocking queue inside the object, so I won't draw this time, because the important logic has been talked about before.
After drawing so many pictures, let's make a summary here:
Ma Guoguo is the Leader node we usually hear in ZK, which is responsible for initiating a proposal for a write transaction request and finally deciding to submit it.
Ma Xiaoyun corresponds to the Follower in ZK. He can only handle read requests alone, and write requests need to be forwarded to Leader for processing.
Read request No matter which server node the client requests to, the processing flow is relatively simple and may require little communication between nodes and can be processed by itself (provided there are no outstanding write requests)
Write request if the client requests Leader, the Leader will initiate a proposal for the transaction request in the cluster, each Follower that receives the proposal will be archived, and the successful ACK information of Leader will be returned. Leader counts the ACK. More than half of the clusters initiate COMMIT requests in the cluster, and the Follower will modify the data in their memory only after they receive the submission request.
Write request if the client requests Follower,Follower to make a simple record locally, it will forward the request to Leader for processing, and then it is the same as the previous one.
In the theory of distributed transaction, there is a rollback phase. When the nodes in the cluster fail to commit locally, the Leader will be notified of the failure information. After counting the ACK, Leader will send a rollback request to each node when it finds that the transaction cannot be committed. But! It's a pity that I didn't find the logic related to transaction rollback in the source code of ZK (of course, it's possible that I missed it, if you know where the logic is, please let me know). I now think that Xiao S is not allowed to fail when handling archives. if the failure is wrong, the entire service node will report an error and exit.
5. Spoiler
At the end of this article, let's take a look at what happened in the animal village.
After all, Ma Guoguo is very old, and if he needs to go to the hospital regularly for a medical examination, it will delay the work here in the office, and gradually with the passage of time, Ma Xiaoyun's business ability is getting stronger and stronger. I have this in mind: why should I listen to this old man? So he took the initiative to propose to the village committee that the candidate of Leader should hold an election. Ma Guoguo could not hold this seat all the time. After listening to this, the village committee also felt that it was very reasonable. So Ma Guoguo was invited to discuss it together. Ma Guoguo certainly could not agree, but Ma Xiaoyun was after all the richest man, and soon the village committee unanimously approved this proposal by managing it up and down.
But now there are only two offices, and if the two sides hold their own opinions, the votes will be tied, so the village committee once again decided to introduce a third office after discussion. The person in charge of this new office chose Ma Xiaoteng, a famous entrepreneur in the village. In this way, there will be no leveling of votes. The specific election rules are as follows:
You have to vote for a Leader before opening three offices every day.
The Leader must be the one with the highest transaction number
More than half of the consent of each office is required.
Everything is based on Leader, and read and write requests follow the process set by Ma Guoguo before.
When the Leader is unable to handle affairs, a new Leader needs to be selected immediately, and the previous office cannot provide services.
So now there are three offices like this.
On how to analyze the ZooKeeper cluster to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.