Interviewer: Tell me about the leader election mechanism in Zookeeper

hello! Hello everyone, I'm Xiaoqi, an unreliable programmer.
Xiaoqi intends to share some techniques in a relaxed and humorous way. If you think you have learned something through Xiaoqi's article, then give Xiaoqi a like.
The article is continuously updated, you can search for [Xiaoqi JAVA Interview] on WeChat to read it as soon as possible, and reply to [Information] and there are more benefits I have prepared for you!

I. Introduction

Today is another sunny day, and I set foot on the road of the interview with a heavy heart. Sometimes I wonder if I am not suitable for the profession of programmer, and do I really want to be a joker. . .

Unconsciously, I walked downstairs to the company where I was interviewing. I called HR excitedly and looking forward to it. I hoped that she would say: Come on, I will leave the door for you. . .

insert image description here

It's a pity that the door was not reserved for me, but it doesn't matter, I have memorized the house number, and I can climb your window in the middle of the night. . .

After I came in, Miss HR asked me to wait in the conference room for a while. After waiting for a while, a burly, 1.85-meter strong man came in. I thought that this should be the interviewer. Ah, I'll be more restrained this time, but when I saw him wearing black sunglasses, I wondered if he was. . .

2. Interview

Interviewer: Xiaoqi, right? Let me introduce myself first.

Me: Are you blind?

Interviewer: No, why do you ask that?

Me: You are not blind, why are you still asking me to introduce you with my resume in your hand?

Interviewer: No, this is the process. I can take a good look at your resume during the process of your introduction.

Me: (I stood up and grabbed the resume from the interviewer's hand and threw it on the ground) You don't need to read my resume, you just need to memorize two words and master it.

insert image description here

Interviewer: (The interviewer shivered) Proficient in everything?

Me: That must be.

3. Leader election mechanism in Zookeeper

Interviewer: Then tell me about the leader election mechanism in zookeeper

Me: Give me three young ladies. . .

Interviewer: Brother, you are here for an interview. This is not a foot bath shop. . .

insert image description here

Me: I know, I let three of them imitate the zookeeper node to show you how the zookeeper is elected vividly.

Interviewer: Well, this is indeed a good reason, I can do whatever I want in the future. . .

At this time, three little sisters came in. The eldest sister is beautiful and dignified, let's call her Da Dignified for the time being, the second sister Shuyueshaohua, let's call her Ershayhua for the time being, and the third sister Guosetianxiang, let's call her Santianxiang for the time being.

Me: I will now give each of you a ballot. You can write two numbers on it. The first number represents yourselves. Everyone’s numbers are different. You can use 1, 2, and 3 for the first number. You can fill in the second number as you like.

At this time, Da Dengzhuang wrote (1,0), Erxihua wrote (2,0), and Santianxiang wrote (3,0).

At this time, if we start two nodes first, one is the eldest sister and the other is the second sister.

At this time, the eldest sister sends a copy of the ticket in her hand to the second sister, and the second sister also sends a copy of the ticket in her hand to the eldest sister.

insert image description here

At this time, the eldest sister compares the ticket issued by the second sister with the ticket in her hand, and compares the latter first. If the other party is bigger than her own, then discard her own ticket and keep the other party's ticket. If it is small, discard the opponent's vote and keep its own. If it is equal, compare the first digit. If both digits are equal, keep both votes.

The eldest sister is (1,0), the second sister is (2,0), and the last digit is equal, then the first one is compared. The first second sister is 2 greater than the eldest sister's 1, so the eldest sister discards her original bill, and Save the ticket sent by the second sister.

insert image description here

Since the second sister's ticket is larger than the eldest sister's, the second sister's own ticket does not need to be discarded, just discard the copy given by the eldest sister.

At this time, the two parties send bills to each other again.

insert image description here

At this time, since both parties have the same bill size, they not only save their own bills, but also save the duplicate bills sent by the other party. At this time, the bills of the two people are as follows.

insert image description here

At this time, since the number of votes held by each person is greater than half of the total nodes (there are only two nodes, the eldest sister and the second sister, so the total node is 2). Here, it is determined that the leader should be the node corresponding to (2,0), that is The second sister is the leader.

Interviewer: What about the little three, the third sister Tianxiang hasn't started yet?

Me: Don't worry, now that the third sister Tianxiang has already elected the leader after starting up, so the third sister Tianxiang directly changes herself into the role of a follower.

Interviewer: Well, I understand what you mean, then can you finally summarize how the comparison is done there, I am a little dizzy

Me: Well, there are only three situations in comparison, one is that one is greater than the other, the second is that the other is greater than oneself, and the third is that the two are equal.

You are bigger than the other party: throw away the ticket sent by the other party and save your own ticket.
The other party is greater than you: throw away your own ticket and save the other party's ticket.
The two are equal: save both the tickets sent by yourself and the other party.

Interviewer: Well, if I want to be the leader, I can just make the ticket bigger. How did the data in the ticket come from?

Me: One of the two numbers in the ticket is myid and the other is zxid. Myid is the unique identifier of the node. It needs to be configured before starting the node, and the zxid will increase after the data is modified. The larger the zxid, the more the data in the node. new.

insert image description here

Interviewer: Oh, that means the newer the node data, the more qualified to be the leader.

Me: That's it. . .

Interviewer: This guy is amazing. I have nothing to ask here. Do you have any questions to ask me?

me: uh. . . Do you have the WeChat accounts of the three young ladies?

Interviewer: Cough, as long as you come, don't say three, even three hundred will not be a problem

Me: Then let's go to the job application now. . .

Interviewer: Don't worry, you have to go to the medical examination first to prove that you can. . .

Me: Then I'm sure there is no problem.

Interviewer: Do you have any other classmates? Recommend me a few

Me: Yes, there are quite a few people who have studied with me in the Little Tricks Learning Programming series, let me introduce them to you.

Interviewer: Okay, I'm short of people here. . .

Me: Then I'll go to the physical examination first, remember my 300. . .

4. Summary

The zookeeper here has not been sorted out, and the article will continue to be updated later. It is recommended to collect it.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

If you think my article is not bad, please like it. In addition, you can search [Xiaoqi JAVA Interview] on WeChat to read more good articles and get the information I have prepared for you.

Guess you like

Origin blog.csdn.net/weixin_44096133/article/details/123997316