Such a hot ZooKeeper, in the end how to choose master?

  Foreword 

Previous article we have given you on the core principles of ZooKeeper, this one we focus on the analysis of ZooKeeper Leader election algorithms. Leader election is one of the most important technology ZooKeeper, is key to ensuring a consistent system of distributed data.

  Leader Election Process Overview 

Our purpose of this article is a detailed analysis of the electoral process Leader, but the process is more complicated Leader elections, we speak directly up on the details of the process, we easily Mongolian laps, so we will briefly describe this short Leader election probably processes.

As FIG. Therefore, we assume there are three nodes ZooKeeper a cluster configuration, the number of MyID are 0,1,2, and because this is an empty cluster of the cluster, so ZXID initial each node are zero , the Leader cluster startup time of the election process is as follows:

(1) We first started as a service myid 0, but currently only one ZooKeeper service, it is unable to complete the election of Leader, Leader ZooKeeper cluster requires a ballot provided that at least two service job otherwise be no law voting.

(2) to start the service myid 1, after the start of the second stage, these two ZooKeeper can communicate with each other, and then you can vote elected.

Time (3) 2 ZooKeeper a ballot for the first time are recommended themselves as Leader, voting information is included: the server itself myid and ZXID. For example, the first vote in their own words, it will be sent to the second voting machine is (0, 0), 0 represents the first machine myid, 0 represents the second is the ZXID. Therefore, voting machines received two of the following:

The first: (1,0)

Second stage: (0,0)

(4) two servers after receiving the vote, the people's vote and their vote PK. PK is the rules are:

 (A) Priority contrast ZXID, ZXID large priority as Leader (ZXID large data representing multiple)

 (B) If ZXID the same, then it is more myid, let myid large as Leader server.

That according to this rule, then the first server, the vote is received (1,0), with their vote (0,0) ratio, ZXID is the same, but smaller than the received myid vote, so the first one originally recommended themselves to vote for (0,0), now the PK after the vote modified to (1,0). The second server, the vote is received (0,0), with their vote (1,0) ratio, ZXID is the same, but myid is larger than the vote received, so stick to their vote (1 , 0). Two servers to vote again.

After (5) After each vote, the server will count all votes, just more than half of the machine cast the same machine, then Leader electoral success, above a second vote two servers, two servers will receive the same vote (1,0). So this time myid server 1 is the Leader.

Time as the Leader election is actually in the process of cluster start in just a few milliseconds to complete, so if you have to build ZooKeeper cluster experience students will find, if we start order service, then start the second machine, Leader on I have been elected, so we will see the second stage is generally Leader. The third stage when he started as a Follower.

We describe above is a cluster Leader of the electoral process in the initialization process, if the process is running in a cluster node is down Follower, Leader of the nodes are not affected, if the process cluster running Leader node goes down, and on It will be re-election and re-election process consistent with the above.

  Details of the election process Leader 

Pictured above is the core algorithm logic diagram ZooKeeper elections, then we look at the details of their election processes.

0x01 voting rounds increments

Used to identify the current voting round, ZooKeeper vote with the provisions of the second round are valid. So if you want a new round of voting, the voting rounds need to be incremented.

0x02 initialization votes

Before the vote each server will initialize their votes, the votes inside the two most important value is the value of a myid of the machine, it is a value ZXID of the machine.

Send initialization votes 0x03

After completion of the initialization of the vote, the server will launch the first ballot, voting for the first time are recommended themselves as Leader, which is cast themselves.

0x04 determine service status

ZooKeeper的服务有四个状态:LOOKING, FOLLOWING, LEADING, OBSERVING

LOOKING:寻找Leader的状态。

FOLLOWING:跟随者状态。

LEADING:领导者状态。

OBSERVING:观察者状态。

如果当前服务的状态是LOOKING,那么说明还没选举出来Leader,继续下面的步骤,正常情况下一开始都是LOOKING的状态。

0x05 接受外部选票

每台服务器会不断的从某个队列里面获取外部的选票。如果发现服务器无法获取外部的任何选票,那么就会立即确认自己是否和集群中的其他服务器保持着有效连接。如果发现没有建立连接,那么立马建立连接。如果已经建立连接了,那么再次发送自己的投票。

0x06 判断选举轮次

发送完初始化选票以后,接下来就要处理外部的选票了,处理选票的时候会根据不同轮次的选票进行不同的处理。

  6.1 外部投票的选举轮次大于自己的轮次

如果发现自己的投票轮次小于外部的轮次,那么立即更新自己的轮次,然后清空已经接收到的选票。然后使用初始化的选票来PK刚刚的外部投票以确定是否变更自己的投票(PK的规则跟概述里描述一致)。

  6.2 外部投票的选举轮次小于自己的轮次

如果接收到的选票的选举轮次落后与服务器本身的轮次,那么直接忽略该外部选票,不做任何处理。

  6.3 外部投票的选举轮次等于自己的轮次

大多数属于这个情况,如果外部的选票的轮次跟自己的选票轮次一致的话,就进行选票PK。

0x07 选票PK

(a)优先对比ZXID,ZXID大的优先作为Leader。

(b)如果ZXID一样的话,那么就比较选票里的myid,myid大的选为Leader。

0x08 变更选票

通过选票PK以后,如果确定了别的服务的选票优于自己的选票,那么就进行选票变更,把自己的选票信息变更为更适合作为Leader的服务器的选票信息。然后再把这些选票发送出去。不过无论是否发生了选票变更,当前的服务都会将刚刚接收到的选票进行归档并按照服务器的myid来区分,如:{(0,vote1),(1,vote2),....}

0x09 统计投票

完成了选票归档之后,就可以统计选票了,说白了就是看是否有过半的服务器认为某台服务器适合做Leader,如果确定了则终止投票。否者返回步骤(4)。

0x0A 更新服务状态

统计投票后,如果已经确定出来Leader就终止投票,接下来就变更服务的状态,首先看一下自己是不是Leader,如果自己是Leader那么就更新自己的状态为Leader,如果不是Leader,那么就根据配置文件,要么切换为Follower,要么切换为Observer。

以上10个步骤就是ZooKeeper选举的核心算法,其中4-9会经过几轮循环直到Leader产生。

最后

ZooKeeper是我们学习架构的过程中必不可少的一个技术,今天主要跟大家讲解了ZooKeeper的Leader选举算法,后面会陆续剖析ZooKeeper的ZAB协议算法,数据快照机制,数据清理机制,会话机制,长连接机制等。欢迎大家持续关注。

发布了50 篇原创文章 · 获赞 1628 · 访问量 203万+

Guess you like

Origin blog.csdn.net/zl1zl2zl3/article/details/104610772