问题导读:
1.如何利用zookeeper保证集群Master可用性和唯一性?
2.zookeeper竞选Master包含哪些过程?
3.zookeeper竞选Master机制利用了zk哪些特性?
在zookeeper应用场景中提出了对于Master节点管理的问题,如何保证集群中Master可用性和唯一性,下面就利用zookeeper来实现。
在确保zookeeper集群节点安装配置的前提下,假设zk已经对外提供了正常的服务,通过下面的步骤来实现Master竞选
- Client连接到zk上,判断znode /Roles/workers是否存在,不存在则建立,znode的类型是PERSISTENT类型,保证不会随着C1的session断开而消失。
- Client在/Roles/workers下面建立一个SEQUENCE|EPHEMERAL类型的znode,前缀可以是worker,由zk保证znode编号是递增而且是暂时的,EPHEMERAL在前文说了,一旦session断开创建的znode也会消失。
- Client通过getChildren获取所有的/Roles/workers下znode列表,并且设置一个Watcher等待通知,返回值有多少个znode数量就对应Client来竞选。
- 对于步骤4返回的节点列表进行排序,找到最小的worker编号,如果是和自己创建的一致(步骤2返回值),那么就代表自己的编号是最小的,自己就是Master。如果发现自己的编号不是最小,那么就等待通知,一旦Watcher触发,就在Watcher回到步骤3。
上面的机制主要利用了zk的几个特性
- 对于N个客户端同时请求create一个znode,zk能保证顺序的一致性,并且保证每个客户端创建的znode节点是递增并且唯一。
- 因为创建的znode是临时的,一旦session断开,那么znode就会从zk上消失,从而给每个设置Watcher的客户端发送通知,让每个客户端重新竞选Master,编号小的肯定是Master,保证了唯一性。
下图是上面的逻辑图
<ignore_js_op>
下面是实现的代码,默认是连接本地的zk服务,端口是2181,zkclient模块位于zookeeper python接口只需要运行多个下面的脚本就会能实现Master的竞选。
- 先后在三个终端上面运行下面的脚本,模拟为c1,c2,c3三个client,创建的节点依次是/Roles/workers/worker0000000000,/Roles/workers/worker0000000001,依次是/Roles/workers/worker0000000002
- 发现c1成功竞选了Master,然后c2和c3都是slave
- 把c1关了从而导致依次是/Roles/workers/worker0000000000消失,一段时间后c2和c3会重新竞选,c2会成为master,c3是slave
- 重新启动c1,发现c1立马加入集群,消息里面变更表示创建了新的znode依次是/Roles/workers/worker0000000003,重新竞选,c2还是master
PS:上面步骤3里面一个客户端关闭后经历了一段时间znode才会删除,原因是这段时间内zk的session还没有被清除,因为关闭是通过ctrl+c关闭的。但是加了一个客户端,znode里面创建,就会通知其余注册了watcher的客户端
#!/usr/bin/env python2.7 # -*- coding: UTF-8 -*- import logging from os.path import basename, join from zkclient import ZKClient, zookeeper, watchmethod logging.basicConfig( level = logging.DEBUG, format = "[%(asctime)s] %(levelname)-8s %(message)s" ) log = logging class GJZookeeper(object): ZK_HOST = "localhost:2181" ROOT = "/Roles" WORKERS_PATH = join(ROOT, "workers") MASTERS_NUM = 1 TIMEOUT = 10000 def __init__(self, verbose = True): self.VERBOSE = verbose self.masters = [] self.is_master = False self.path = None self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT) self.say("login ok!") # init self.__init_zk() # register self.register() def __init_zk(self): """ create the zookeeper node if not exist |-Roles |-workers """ nodes = (self.ROOT, self.WORKERS_PATH) for node in nodes: if not self.zk.exists(node): try: self.zk.create(node, "") except: pass @property def is_slave(self): return not self.is_master def register(self): """ register a node for this worker,znode type : EPHEMERAL | SEQUENCE |-Roles |-workers |-worker000000000x ==>>master |-worker000000000x+1 ==>>worker .... """ self.path = self.zk.create(self.WORKERS_PATH + "/worker", "1", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE) self.path = basename(self.path) self.say("register ok! I'm %s" % self.path) # check who is the master self.get_master() def get_master(self): """ get children, and check who is the smallest child """ @watchmethod def watcher(event): self.say("child changed, try to get master again.") self.get_master() try : children = self.zk.get_children(self.WORKERS_PATH, watcher) except zookeeper.ConnectionLossException: print "losing connection with zookeeper..." return False except : return False children.sort() self.say("%s's children: %s" % (self.WORKERS_PATH, children)) # check if I'm master self.masters = children[:self.MASTERS_NUM] if self.path in self.masters: self.is_master = True self.say("I've become master!") else: self.say("%s is masters, I'm slave" % self.masters) def say(self, msg): """ print messages to screen """ if self.VERBOSE: if self.path: log.info("[ %s(%s) ] %s" % (self.path, "master" if self.is_master else "slave", msg)) else: log.info(msg) def main(): gj_zookeeper = GJZookeeper() if __name__ == "__main__": main() import time time.sleep(1000)
文章转自:http://www.aboutyun.com/forum.php?mod=viewthread&tid=9277&ctid=16
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/9580.html