The function and working principle of Zookeeper

1. What is ZooKeeper?

ZooKeeper is a distributed, open source distributed application coordination service. It is an open source implementation of Google's Chubby. It is the manager of the cluster and monitors the status of each node in the cluster. The next step is reasonable according to the feedback submitted by the nodes. operate. Finally, provide users with an easy-to-use interface and a system with high performance and stable functions

2. What does ZooKeeper provide?

1) File system

2) Notification mechanism

3. Zookeeper file system

Each subdirectory item such as NameService is called a znode. Like the file system, we can freely add and delete znodes, and add and delete sub-znodes under a znode. The only difference is that znodes can store data. 
There are four types of znodes: 
1. PERSISTENT-persistent directory node 
client is disconnected from zookeeper, the node still exists 
2. PERSISTENT_SEQUENTIAL-persistent sequence number directory node  After the
client is disconnected from zookeeper, the node still exists Exist, but Zookeeper sequentially numbered the node name 
3. After the EPHEMERAL-temporary directory node  client
is disconnected from zookeeper, the node is deleted 

The node is deleted, but Zookeeper sequentially numbers the node name
 


4. Zookeeper notification mechanism

The client registers and listens to the directory nodes it cares about. When the directory nodes change (data changes, deletions, subdirectory nodes are added and deleted), zookeeper will notify the client.

5. What does Zookeeper do?

1. Naming service 2. Configuration management 3. Cluster management 4. Distributed lock 5. Queue management

6. Zookeeper naming service

Create a directory in zookeeper's file system with a unique path. When we use tborg to be unable to determine the deployment machine of the upstream program, we can agree a path with the downstream program, and we can explore each other through the path.

7. Zookeeper configuration management

Programs always need to be configured, and if the program is spread across multiple machines, it becomes difficult to change the configuration one by one. Now put all these configurations on zookeeper, save them in a directory node of Zookeeper, and then all related applications monitor this directory node. Once the configuration information changes, each application will receive a notification from Zookeeper. Then get the new configuration information from Zookeeper and apply it to the system


8. Zookeeper cluster management

The so-called cluster management does not care about two points: whether there are machines exiting and joining, and electing the master. 
For the first point, all machines agree to create a temporary directory node under the parent directory GroupMembers, and then listen for child node change messages of the parent directory node. Once a machine dies, the machine is disconnected from zookeeper, the temporary directory node created by it is deleted, and all other machines are notified: a sibling directory is deleted, so everyone knows: it is on board .
The same is true for the addition of new machines. All machines are notified: the new sibling directory is added, and the highcount is again. For the second point, we make a slight change. All machines create temporary sequential numbered directory nodes, and each time the machine with the smallest number is selected as the master Just fine.


9. Zookeeper distributed lock

有了zookeeper的一致性文件系统,锁的问题变得容易。锁服务可以分为两类,一个是保持独占,另一个是控制时序。 
对于第一类,我们将zookeeper上的一个znode看作是一把锁,通过createznode的方式来实现。所有客户端都去创建 /distribute_lock 节点,最终成功创建的那个客户端也即拥有了这把锁。用完删除掉自己创建的distribute_lock 节点就释放出锁。 
对于第二类, /distribute_lock 已经预先存在,所有客户端在它下面创建临时顺序编号目录节点,和选master一样,编号最小的获得锁,用完删除,依次方便。


10.Zookeeper队列管理

两种类型的队列:
1、同步队列,当一个队列的成员都聚齐时,这个队列才可用,否则一直等待所有成员到达。 
2、队列按照 FIFO 方式进行入队和出队操作。 
第一类,在约定目录下创建临时目录节点,监听节点数目是否是我们要求的数目。 
第二类,和分布式锁服务中的控制时序场景基本原理一致,入列有编号,出列按编号。

11.分布式与数据复制 

Zookeeper作为一个集群提供一致的数据服务,自然,它要在所有机器间做数据复制。数据复制的好处: 
1、容错:一个节点出错,不致于让整个系统停止工作,别的节点可以接管它的工作; 
2、提高系统的扩展能力 :把负载分布到多个节点上,或者增加节点来提高系统的负载能力; 
3、提高性能:让客户端本地访问就近的节点,提高用户访问速度。 

从客户端读写访问的透明度来看,数据复制集群系统分下面两种: 
1、写主(WriteMaster) :对数据的修改提交给指定的节点。读无此限制,可以读取任何一个节点。这种情况下客户端需要对读与写进行区别,俗称读写分离; 
2、写任意(Write Any):对数据的修改可提交给任意的节点,跟读一样。这种情况下,客户端对集群节点的角色与变化透明。

对zookeeper来说,它采用的方式是写任意。通过增加机器,它的读吞吐能力和响应能力扩展性非常好,而写,随着机器的增多吞吐能力肯定下降(这也是它建立observer的原因),而响应能力则取决于具体实现方式,是延迟复制保持最终一致性,还是立即复制快速响应。

12.Zookeeper角色描述


13.Zookeeper与客户端


14.Zookeeper设计目的

1.最终一致性:client不论连接到哪个Server,展示给它都是同一个视图,这是zookeeper最重要的性能。 
2.可靠性:具有简单、健壮、良好的性能,如果消息被到一台服务器接受,那么它将被所有的服务器接受。 
3.实时性:Zookeeper保证客户端将在一个时间间隔范围内获得服务器的更新信息,或者服务器失效的信息。但由于网络延时等原因,Zookeeper不能保证两个客户端能同时得到刚更新的数据,如果需要最新数据,应该在读数据之前调用sync()接口。 
4.等待无关(wait-free):慢的或者失效的client不得干预快速的client的请求,使得每个client都能有效的等待。 
5.原子性:更新只能成功或者失败,没有中间状态。 
6.顺序性:包括全局有序和偏序两种:全局有序是指如果在一台服务器上消息a在消息b前发布,则在所有Server上消息a都将在消息b前被发布;偏序是指如果一个消息b在消息a后被同一个发送者发布,a必将排在b前面。 

15.Zookeeper工作原理

Zookeeper 的核心是原子广播,这个机制保证了各个Server之间的同步。实现这个机制的协议叫做Zab协议。Zab协议有两种模式,它们分别是恢复模式(选主)和广播模式(同步)。当服务启动或者在领导者崩溃后,Zab就进入了恢复模式,当领导者被选举出来,且大多数Server完成了和 leader的状态同步以后,恢复模式就结束了。状态同步保证了leader和Server具有相同的系统状态。 
为了保证事务的顺序一致性,zookeeper采用了递增的事务id号(zxid)来标识事务。所有的提议(proposal)都在被提出的时候加上了zxid。实现中zxid是一个64位的数字,它高32位是epoch用来标识leader关系是否改变,每次一个leader被选出来,它都会有一个新的epoch,标识当前属于那个leader的统治时期。低32位用于递增计数。

16.Zookeeper 下 Server工作状态

每个Server在工作过程中有三种状态: 
LOOKING:当前Server不知道leader是谁,正在搜寻
LEADING:当前Server即为选举出来的leader
FOLLOWING:leader已经选举出来,当前Server与之同步

17.Zookeeper选主流程(basic paxos)

当leader崩溃或者leader失去大多数的follower,这时候zk进入恢复模式,恢复模式需要重新选举出一个新的leader,让所有的Server都恢复到一个正确的状态。Zk的选举算法有两种:一种是基于basic paxos实现的,另外一种是基于fast paxos算法实现的。系统默认的选举算法为fast paxos。

1.选举线程由当前Server发起选举的线程担任,其主要功能是对投票结果进行统计,并选出推荐的Server; 
2.选举线程首先向所有Server发起一次询问(包括自己); 
3.选举线程收到回复后,验证是否是自己发起的询问(验证zxid是否一致),然后获取对方的id(myid),并存储到当前询问对象列表中,最后获取对方提议的leader相关信息(id,zxid),并将这些信息存储到当次选举的投票记录表中; 
4.收到所有Server回复以后,就计算出zxid最大的那个Server,并将这个Server相关信息设置成下一次要投票的Server; 
5.线程将当前zxid最大的Server设置为当前Server要推荐的Leader,如果此时获胜的Server获得n/2 + 1的Server票数,设置当前推荐的leader为获胜的Server,将根据获胜的Server相关信息设置自己的状态,否则,继续这个过程,直到leader被选举出来。 通过流程分析我们可以得出:要使Leader获得多数Server的支持,则Server总数必须是奇数2n+1,且存活的Server的数目不得少于n+1. 每个Server启动后都会重复以上流程。在恢复模式下,如果是刚从崩溃状态恢复的或者刚启动的server还会从磁盘快照中恢复数据和会话信息,zk会记录事务日志并定期进行快照,方便在恢复时进行状态恢复。选主的具体流程图所示: 


18.Zookeeper选主流程(fast paxos)

fast paxos流程是在选举过程中,某Server首先向所有Server提议自己要成为leader,当其它Server收到提议以后,解决epoch和 zxid的冲突,并接受对方的提议,然后向对方发送接受提议完成的消息,重复这个流程,最后一定能选举出Leader。


19.Zookeeper同步流程

选完Leader以后,zk就进入状态同步过程。 
 1. Leader等待server连接; 
 2 .Follower连接leader,将最大的zxid发送给leader; 
 3 .Leader根据follower的zxid确定同步点; 
 4 .完成同步后通知follower 已经成为uptodate状态; 
 5 .Follower收到uptodate消息后,又可以重新接受client的请求进行服务了。


20.Zookeeper工作流程-Leader

1 .恢复数据; 
2 .维持与Learner的心跳,接收Learner请求并判断Learner的请求消息类型; 
3 .Learner的消息类型主要有PING消息、REQUEST消息、ACK消息、REVALIDATE消息,根据不同的消息类型,进行不同的处理。 
PING 消息是指Learner的心跳信息;
REQUEST消息是Follower发送的提议信息,包括写请求及同步请求;
ACK消息是 Follower的对提议的回复,超过半数的Follower通过,则commit该提议;
REVALIDATE消息是用来延长SESSION有效时间。


21.Zookeeper工作流程-Follower

Follower主要有四个功能: 
1.向Leader发送请求(PING消息、REQUEST消息、ACK消息、REVALIDATE消息); 
2.接收Leader消息并进行处理; 
3.接收Client的请求,如果为写请求,发送给Leader进行投票;
4.返回Client结果。 

Follower的消息循环处理如下几种来自Leader的消息: 
1 .PING消息: 心跳消息; 
2 .PROPOSAL消息:Leader发起的提案,要求Follower投票; 
3 .COMMIT消息:服务器端最新一次提案的信息; 
4 .UPTODATE消息:表明同步完成; 
5 .REVALIDATE消息:根据Leader的REVALIDATE结果,关闭待revalidate的session还是允许其接受消息; 
6 .SYNC消息:返回SYNC结果到客户端,这个消息最初由客户端发起,用来强制得到最新的更新。 


源于:https://www.cnblogs.com/felixzh/p/5869212.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325390613&siteId=291194637