设为首页 收藏本站
查看: 723|回复: 0

[经验分享] zookeeper python接口

[复制链接]

尚未签到

发表于 2019-1-8 08:45:53 | 显示全部楼层 |阅读模式
#!/usr/bin/env python2.7# -*- coding: UTF-8 -*-import zookeeper, time, threadingfrom collections import namedtuple  

  
DEFAULT_TIMEOUT = 30000VERBOSE = TrueZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"}# Mapping of connection state values to human strings.STATE_NAME_MAPPING = {
  
    zookeeper.ASSOCIATING_STATE: "associating",
  
    zookeeper.AUTH_FAILED_STATE: "auth-failed",
  
    zookeeper.CONNECTED_STATE: "connected",
  
    zookeeper.CONNECTING_STATE: "connecting",
  
    zookeeper.EXPIRED_SESSION_STATE: "expired",}# Mapping of event type to human string.TYPE_NAME_MAPPING = {
  
    zookeeper.NOTWATCHING_EVENT: "not-watching",
  
    zookeeper.SESSION_EVENT: "session",
  
    zookeeper.CREATED_EVENT: "created",
  
    zookeeper.DELETED_EVENT: "deleted",
  
    zookeeper.CHANGED_EVENT: "changed",
  
    zookeeper.CHILD_EVENT: "child", }class ZKClientError(Exception):
  
    def __init__(self, value):
  
        self.value = value    def __str__(self):
  
        return repr(self.value)class ClientEvent(namedtuple("ClientEvent", 'type, connection_state, path')):
  
    """
  
    A client event is returned when a watch deferred fires. It denotes
  
    some event on the zookeeper client that the watch was requested on.
  
    """
  

  
    @property
  
    def type_name(self):
  
        return TYPE_NAME_MAPPING[self.type]
  

  
    @property
  
    def state_name(self):
  
        return STATE_NAME_MAPPING[self.connection_state]
  

  
    def __repr__(self):
  
        return  "" % (
  
            self.type_name, self.path, self.state_name)def watchmethod(func):
  
    def decorated(handle, atype, state, path):
  
        event = ClientEvent(atype, state, path)
  
        return func(event)
  
    return decoratedclass ZKClient(object):
  
    def __init__(self, servers, timeout=DEFAULT_TIMEOUT):
  
        self.timeout = timeout        self.connected = False
  
        self.conn_cv = threading.Condition( )
  
        self.handle = -1
  

  
        self.conn_cv.acquire()
  
        if VERBOSE: print("Connecting to %s" % (servers))
  
        start = time.time()
  
        self.handle = zookeeper.init(servers, self.connection_watcher, timeout)
  
        self.conn_cv.wait(timeout/1000)
  
        self.conn_cv.release()
  

  
        if not self.connected:
  
            raise ZKClientError("Unable to connect to %s" % (servers))
  

  
        if VERBOSE:
  
            print("Connected in %d ms, handle is %d"
  
                  % (int((time.time() - start) * 1000), self.handle))
  

  
    def connection_watcher(self, h, type, state, path):
  
        self.handle = h        self.conn_cv.acquire()
  
        self.connected = True
  
        self.conn_cv.notifyAll()
  
        self.conn_cv.release()
  

  
    def close(self):
  
        return zookeeper.close(self.handle)
  

  
    def create(self, path, data="", flags=0, acl=[ZOO_OPEN_ACL_UNSAFE]):
  
        start = time.time()
  
        result = zookeeper.create(self.handle, path, data, acl, flags)
  
        if VERBOSE:
  
            print("Node %s created in %d ms"
  
                  % (path, int((time.time() - start) * 1000)))
  
        return result    def delete(self, path, version=-1):
  
        start = time.time()
  
        result = zookeeper.delete(self.handle, path, version)
  
        if VERBOSE:
  
            print("Node %s deleted in %d ms"
  
                  % (path, int((time.time() - start) * 1000)))
  
        return result    def get(self, path, watcher=None):
  
        return zookeeper.get(self.handle, path, watcher)
  

  
    def exists(self, path, watcher=None):
  
        return zookeeper.exists(self.handle, path, watcher)
  

  
    def set(self, path, data="", version=-1):
  
        return zookeeper.set(self.handle, path, data, version)
  

  
    def set2(self, path, data="", version=-1):
  
        return zookeeper.set2(self.handle, path, data, version)
  

  

  
    def get_children(self, path, watcher=None):
  
        return zookeeper.get_children(self.handle, path, watcher)
  

  
    def async(self, path = "/"):
  
        return zookeeper.async(self.handle, path)
  

  
    def acreate(self, path, callback, data="", flags=0, acl=[ZOO_OPEN_ACL_UNSAFE]):
  
        result = zookeeper.acreate(self.handle, path, data, acl, flags, callback)
  
        return result    def adelete(self, path, callback, version=-1):
  
        return zookeeper.adelete(self.handle, path, version, callback)
  

  
    def aget(self, path, callback, watcher=None):
  
        return zookeeper.aget(self.handle, path, watcher, callback)
  

  
    def aexists(self, path, callback, watcher=None):
  
        return zookeeper.aexists(self.handle, path, watcher, callback)
  

  
    def aset(self, path, callback, data="", version=-1):
  
        return zookeeper.aset(self.handle, path, data, version, callback)watch_count = 0"""Callable watcher that counts the number of notifications"""class CountingWatcher(object):
  
    def __init__(self):
  
        self.count = 0
  
        global watch_count        self.id = watch_count
  
        watch_count += 1
  

  
    def waitForExpected(self, count, maxwait):
  
        """Wait up to maxwait for the specified count,
  
        return the count whether or not maxwait reached.
  

  
        Arguments:
  
        - `count`: expected count
  
        - `maxwait`: max milliseconds to wait
  
        """
  
        waited = 0
  
        while (waited < maxwait):
  
            if self.count >= count:
  
                return self.count
  
            time.sleep(1.0);
  
            waited += 1000
  
        return self.count    def __call__(self, handle, typ, state, path):
  
        self.count += 1
  
        if VERBOSE:
  
            print("handle %d got watch for %s in watcher %d, count %d" %
  
                  (handle, path, self.id, self.count))"""Callable watcher that counts the number of notifications
  
and verifies that the paths are sequential"""class SequentialCountingWatcher(CountingWatcher):
  
    def __init__(self, child_path):
  
        CountingWatcher.__init__(self)
  
        self.child_path = child_path    def __call__(self, handle, typ, state, path):
  
        if not self.child_path(self.count) == path:
  
            raise ZKClientError("handle %d invalid path order %s" % (handle, path))
  
        CountingWatcher.__call__(self, handle, typ, state, path)class Callback(object):
  
    def __init__(self):
  
        self.cv = threading.Condition()
  
        self.callback_flag = False
  
        self.rc = -1
  

  
    def callback(self, handle, rc, handler):
  
        self.cv.acquire()
  
        self.callback_flag = True
  
        self.handle = handle        self.rc = rc
  
        handler()
  
        self.cv.notify()
  
        self.cv.release()
  

  
    def waitForSuccess(self):
  
        while not self.callback_flag:
  
            self.cv.wait()
  
        self.cv.release()
  

  
        if not self.callback_flag == True:
  
            raise ZKClientError("asynchronous operation timed out on handle %d" %
  
                             (self.handle))
  
        if not self.rc == zookeeper.OK:
  
            raise ZKClientError(
  
                "asynchronous operation failed on handle %d with rc %d" %
  
                (self.handle, self.rc))class GetCallback(Callback):
  
    def __init__(self):
  
        Callback.__init__(self)
  

  
    def __call__(self, handle, rc, value, stat):
  
        def handler():
  
            self.value = value            self.stat = stat        self.callback(handle, rc, handler)class SetCallback(Callback):
  
    def __init__(self):
  
        Callback.__init__(self)
  

  
    def __call__(self, handle, rc, stat):
  
        def handler():
  
            self.stat = stat        self.callback(handle, rc, handler)class ExistsCallback(SetCallback):
  
    passclass CreateCallback(Callback):
  
    def __init__(self):
  
        Callback.__init__(self)
  

  
    def __call__(self, handle, rc, path):
  
        def handler():
  
            self.path = path        self.callback(handle, rc, handler)class DeleteCallback(Callback):
  
    def __init__(self):
  
        Callback.__init__(self)
  

  
    def __call__(self, handle, rc):
  
        def handler():
  
            pass
  
        self.callback(handle, rc, handler)



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-660565-1-1.html 上篇帖子: Ubuntu16.04安装zookeeper集群 下篇帖子: Mesosphere Cluster on CentOS7 (zookeeper+mesos+marathon)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表