def get_server_by_id(zk_hosts, server_id): # TODO refactor me to accept KazooClient object. if not isinstance(server_id, int): raise ValueError('server_id must be int, got: %s' % type(server_id)) for host in zk_hosts: zk = KazooClient(hosts=host, read_only=True) try: zk.start() except KazooTimeoutError as e: print('ZK Timeout host: [%s], %s' % (host, e)) continue properties_str = zk.command(cmd=b'conf') properties = properties_str.split('\n') for line in properties: if not line.strip().lower().startswith('serverid='): continue key, val = line.split('=') val = int(val) if val == server_id: return host continue zk.stop() raise ValueError( "no host available with that server id [%d], from connections given" % server_id)
def verify(self): host = self.option.host port = self.option.port thost = host + ":" + str(port) try: conn = KazooClient(hosts=thost) conn.start() sysinfo = conn.command('envi') except Exception, e: self.result.error = "连接发生错误: {error}".format(error=str(e)) return
def get_stat(host): """get status of a single node in zookeeper cluster """ cluster_info = dict() try: zoo_client = KazooClient(hosts=host, connection_retry={"max_tries": 1, "backoff": 1}) zoo_client.start(timeout=3) cluster_info[host] = zoo_client.command("mntr") except KazooTimeoutError as exc: log.error("Failed to connnect zookeeper, %s", str(exc)) cluster_info[host] = str(exc) return cluster_info
class MetaHandler(object): def __init__(self, hosts, nodeInfo, eventCallback): parsed = urlparse(hosts) print hosts if parsed.scheme != 'zk': raise ValueError("Meta URL must start with zk://") if parsed.path in ('/', ''): raise ValueError("Service root path not found.") self.rootpath = parsed.path.rstrip('/') + '/oceanworker' log.error("ROOT = %s" % self.rootpath) # NOTE: currently, auth_data is not supported servers = parsed.netloc.split('@')[-1] self.masterLock = None self.callback = eventCallback self.client = KazooClient(servers) self.client.add_listener(self.__connectionState) self.lastEventUpdated = 0 self.bRun = False self.nodeInfo = nodeInfo self.party = self.client.ShallowParty(self.rootpath + '/workers', identifier=settings.UUID.get_hex()) self.node = self.party.node self.state = self.client.state self.devTasks = {} self.client.start() def ping(self): try: self.client.command() except Exception, e: log.error(e)
def get_stat(host): """get status of a single node in zookeeper cluster """ cluster_info = dict() try: zoo_client = KazooClient( hosts=host, connection_retry={"max_tries": 1, "backoff": 1} ) zoo_client.start(timeout=3) cluster_info[host] = zoo_client.command("mntr") except KazooTimeoutError as exc: log.error('Failed to connnect zookeeper, %s', str(exc)) cluster_info[host] = str(exc) return cluster_info
class ZkOpers(object): zk = None DEFAULT_RETRY_POLICY = KazooRetry( max_tries=None, max_delay=10000, ) rootPath = "/letv/mysql/mcluster" ''' classdocs ''' def __init__(self): ''' Constructor ''' self.zkaddress, self.zkport = local_get_zk_address() if "" != self.zkaddress and "" != self.zkport: self.zk = KazooClient(hosts=self.zkaddress + ':' + str(self.zkport), connection_retry=self.DEFAULT_RETRY_POLICY, timeout=20) self.zk.add_listener(self.listener) self.zk.start() #self.zk = self.ensureinstance() logging.info("instance zk client (%s:%s)" % (self.zkaddress, self.zkport)) def command(self, cmd): return self.zk.command(cmd) def watch(self): clusterUUID = self.getClusterUUID() myConfPath = self.rootPath + "/" + clusterUUID + "/mycnf" @self.zk.DataWatch(myConfPath) def watch_my_conf(data, stat): keyList = ["wsrep_cluster_address"] dic = getDictFromText(data, keyList) confOpers.setValue(options.mysql_cnf_file_name, dic) def close(self): try: self.zk.stop() self.zk.close() except Exception, e: logging.error(e)
def get_stat(hosts): """get stat of zookeeper cluster of given idc """ cluster_info = dict() for host in hosts.split(","): try: zoo_client = KazooClient( hosts=host, connection_retry={"max_tries": 1, "backoff": 1} ) zoo_client.start(3) cluster_info[host] = zoo_client.command("mntr") except KazooTimeoutError as exc: log.error('Failed to connnect zookeeper, %s', str(exc)) cluster_info[host] = str(exc) return cluster_info
def get_stat(hosts): """get stat of zookeeper cluster of given idc """ cluster_info = dict() for host in hosts.split(","): try: zoo_client = KazooClient(hosts=host, connection_retry={ "max_tries": 1, "backoff": 1 }) zoo_client.start(3) cluster_info[host] = zoo_client.command("mntr") except KazooTimeoutError as exc: log.error('Failed to connnect zookeeper, %s', str(exc)) cluster_info[host] = str(exc) return cluster_info
def get_leader(zk_hosts): # TODO refactor me to accept KazooClient object. for host in zk_hosts: zk = KazooClient(hosts=host, read_only=True) try: zk.start() except KazooTimeoutError as e: print('ZK Timeout host: [%s], %s' % (host, e)) continue properties_str = zk.command(cmd=b'srvr') properties = properties_str.split('\n') for line in properties: if not line.strip().lower().startswith('mode:'): continue key, val = line.split(':') if val.strip().lower() == MODE_LEADER: return host zk.stop() raise RuntimeError('no leader available, from connections given')
#!--*--coding:utf-8 --*-- ''' Created on 2016年7月26日 @author: tianyongkang ''' from kazoo.client import KazooClient zk = KazooClient('host=10.136.3.214:2181') zk.start(timeout=20) r = zk.command(cmd="conf")
#!/usr/bin/env python # _*_coding:utf-8_*_ from kazoo.client import KazooClient from kazoo.client import KazooState from kazoo.client import KeeperState zk = KazooClient(hosts='172.28.250.43:2181') zk.start() print zk.command(cmd=b'stat') """ # 创建路径 zk.ensure_path("/my/zookeeper") # 创建节点 zk.create("/my/zookeeper/node1", b"172.28.250.242") #删除节点 zk.delete("/my/zookeeper/node1") # 判断节点是否存在 if zk.exists("/my/zookeeper/node1"): print '/my/zookeeper/node1' # Print the version of a node and its data data, state = zk.get("/my/zookeeper/node1") print ("Version: %s, data: %s" % (state.version, data.decode("utf-8")))
class Zookeeper(object): executor = ThreadPoolExecutor(20) def __init__(self, server): self.client = KazooClient(hosts=server, timeout=3, read_only=True, max_retries=1) self.client.start(timeout=3) def __enter__(self): return self def __exit__(self, type, value, traceback): self.client.stop() self.client.close() @run_on_executor def fetchCons(self): """ 返回活跃的客户端状态数据 """ name = ('ip', 'queued', 'port', 'recved', 'sent', 'sid', 'lop', 'est', 'to', 'lcxid', 'lzxid', 'lresp', 'llat', 'minlat', 'avglat', 'maxlat') pattern = re.compile( r'/(.+):(\d+).+queued=(\d+).+recved=(\d+).+sent=(\d+).+sid=(\w+).+lop=(\w+).+est=(\d+).+to=(\d+).+lcxid=(\w+).+lzxid=(\w+).+lresp=(\d+).+llat=(\d+).+minlat=(\d+).+avglat=(\d+).+maxlat=(\d+)' ) data = [] for row in self.client.command(cmd=b'cons').split('\n'): if ')' == row[-1]: metric = pattern.findall(row) if metric: data.append(dict(zip(name, metric[0]))) return data @run_on_executor def fetchMntr(self): """ 返回zk节点状态数据 """ data = dict( item.split('\t', 1) for item in self.client.command(b'mntr').split('\n')[:-1]) data.update({ 'alive': (True if self.client.command(cmd=b'ruok') == 'imok' else False) }) return data @run_on_executor def get(self, path): """ 返回指定znode的状态与数据 """ data, stat = self.client.get(path) return data, json.loads(json.dumps(stat._asdict())) @run_on_executor def getChildren(self, path): """ 返回指定zk目录中的资源列表 """ try: children = map(str, self.client.get_children(path)) except NoNodeError as e: return [] return children
pass try: zk.create("/topic/" + str(i) + "/history", value="0,0,0,0,0", acl=None, ephemeral=False, sequence=False, makepath=False) except NodeExistsError: break print "Create Nodes Finished!" #leader election isLeader = False out = zk.command(cmd='stat') if "leader" in out: print "I am the leader!!" zk.set("/leader", value=myip) isLeader = True print "Leader Election Finished!" #ZMQ part context = zmq.Context() reg_sock = context.socket(zmq.REP) connect_str = "tcp://*:5556" reg_sock.bind(connect_str) #publish events from evt srv to subers socket = context.socket(zmq.PUB) socket.bind("tcp://*:5557")