def _f_(event): if event.state_name == "connected" and event.type_name == "changed": utils.log(utils.cur(), true_path, event, watcher.__name__ if callable(watcher) else watcher) self.watch_node(true_path, watcher) if callable(watcher): try: watcher(event, true_path) except Exception, err: utils.err(utils.cur(), err, err.__class__)
def unsafe_check(event, check_callback, my_id): ip_dict, ip_self = self.get_leaders(ZKConf.ZK_PATH_LEADER, specific_leader_path) me_path = self.ip + ":" + str(port) if port else self.ip me_path = os.path.join(specific_leader_path, me_path) utils.log(utils.cur(), ip_dict, ip_self) utils.log(utils.cur(), self.ip, me_path) if ip_self: if (not self.ip in ip_dict) and \ (ip_self in ip_dict) and \ (ip_dict[ip_self] > 1): leader_childs = self.get_children(specific_leader_path, None) for leader_child in leader_childs: self.delnode( os.path.join(specific_leader_path, leader_child)) utils.log(utils.cur(), me_path) self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) childs = sorted( self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith( me_path.split("/")[-1]): callback(True) else: callback(False) else: utils.log(utils.cur(), my_id, ip_self) childs = sorted( self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith( my_id.split(":")[0]): callback(True) else: callback(False) else: self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) utils.log(utils.cur(), me_path) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith( me_path.split("/")[-1]): callback(True) else: callback(False)
def try_lock(self, data, path=ZKConf.LEADER_LOCK): utils.log(utils.cur(), "trying '%s'" % data) if self.exists(path, None): try: olddata, meta = self.get(path, None) utils.log( utils.cur(), "'%s' gets lock %s %s" % (data, olddata, olddata == data)) return olddata == data except Exception, err: utils.err(utils.cur(), err) return False
def try_lock(self, data, path=ZKConf.LEADER_LOCK): utils.log(utils.cur(), "trying '%s'" % data) if self.exists(path, None): try: olddata, meta = self.get(path, None) utils.log(utils.cur(), "'%s' gets lock %s %s" % (data, olddata, olddata==data)) return olddata == data except Exception, err: utils.err(utils.cur(), err) return False
def unsafe_check(event, check_callback, my_id): ip_dict, ip_self = self.get_leaders(ZKConf.ZK_PATH_LEADER, specific_leader_path) me_path = self.ip + ":" + str(port) if port else self.ip me_path = os.path.join(specific_leader_path, me_path) utils.log(utils.cur(), ip_dict, ip_self) utils.log(utils.cur(), self.ip, me_path) if ip_self: if (not self.ip in ip_dict) and \ (ip_self in ip_dict) and \ (ip_dict[ip_self] > 1): leader_childs = self.get_children(specific_leader_path, None) for leader_child in leader_childs: self.delnode(os.path.join(specific_leader_path, leader_child)) utils.log(utils.cur(), me_path) self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith(me_path.split("/")[-1]): callback(True) else: callback(False) else: utils.log(utils.cur(), my_id, ip_self) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith(my_id.split(":")[0]): callback(True) else: callback(False) else: self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) utils.log(utils.cur(), me_path) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith(me_path.split("/")[-1]): callback(True) else: callback(False)
def check_childs(event, true_path): try: utils.log(utils.cur(), event.type_name, event.state_name) except: utils.log(utils.cur(), event) my_id = self.ip + ":" + str(port) if port else self.ip i = 1 while not self.try_lock(my_id) and i < 100: utils.log(utils.cur(), "'%s' tries lock %d times" % (my_id, i)) i += 1 time.sleep(1) utils.log(utils.cur(), "'%s' tries lock %d times and exit" % (my_id, i)) if i < 100: utils.log(utils.cur(), "'%s' got lock and unsafe_checking" % my_id) unsafe_check(event, self.unlock, my_id)
def send_msg(self, msg): try: self.pika.send_msg(msg) except Exception, err: utils.err(utils.cur(), err) self.is_server_down = True self.try_reconnect()
def unlock(self, data, path=ZKConf.LEADER_LOCK): if not self.exists(path, None): utils.log(utils.cur(), "somebody else releases lock not me '%s'" % data) return True try: value = self.get(path, None) # "'NoneType' object is not iterable" means "zookeeper.get is None" if value is None: return True if value[0] == data: self.delete(path) utils.log(utils.cur(), "'%s' releases lock" % data) return True except Exception, err: utils.err(utils.cur(), err)
def __getattr__(self, name): if not isinstance(name, str) or not hasattr(zookeeper, name): raise ZooKeeperError("Method %s() doesn't exist" % str(name)) if name in ("ASSOCIATING_STATE","AUTH_FAILED_STATE", "CONNECTED_STATE","CONNECTING_STATE", "EXPIRED_SESSION_STATE","NOTWATCHING_EVENT", "SESSION_EVENT","CREATED_EVENT", "DELETED_EVENT","CHANGED_EVENT","CHILD_EVENT"): return getattr(zookeeper, name) def safe_get(*args, **kwargs): func, result = getattr(zookeeper, name), None def real_func(): if name in ("get", "exists", "get_children"): path, watcher = args[0], args[1] if len(args) > 1 else None return func(self._handler, path, self._wrap_watcher(watcher)) else: return func(self._handler, *args, **kwargs) try: result = real_func() except zookeeper.SessionExpiredException, err: utils.err(utils.cur(), err, "session expired, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func() except zookeeper.ConnectionLossException, err: utils.err(utils.cur(), err, "connection loss, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func()
def try_reconnect(self): while self.running and self.is_server_down: try: self.pika.try_connect() self.is_server_down = False except Exception, err: utils.err(utils.cur(), time.time(), err) time.sleep(MQConf.RECONNECTION_TIME)
def register_service(self, service, port=0): path = "" if service == "master": path = ZKConf.ZK_PATH_TEMP_MASTER elif service == "stats": path = ZKConf.ZK_PATH_TEMP_STATS elif service == "proxy": path = ZKConf.ZK_PATH_TEMP_PROXY if not path: raise Exception("service type '%s' doesn't exist" % (service, )) node = zookeeper.exists(self._handler, path, None) if not node: if not zookeeper.exists(self._handler, ZKConf.ZK_PATH_ROOT, None): zookeeper.create(self._handler, ZKConf.ZK_PATH_ROOT, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) zookeeper.create(self._handler, path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) ip = utils.getip() mepath = "".join([path, "/", ip, ":", str(port), "-"]) if not port: mepath = "".join([path, "/", ip, "-"]) childs = zookeeper.get_children(self._handler, path, None) is_created = False pathme = mepath.split("/")[-1] for child in childs: if child.startswith(pathme): is_created = child.split("-")[-1] break if is_created: meflag = mepath + is_created utils.log(utils.cur(), "%s none-callback %s" % ("*" * 10, "*" * 10), meflag) else: utils.log(utils.cur(), "%s %s %s" % ("*" * 10, self.register_node_cb, "*" * 10), mepath) meflag = zookeeper.create(self._handler, mepath, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.SEQUENCE | zookeeper.EPHEMERAL) utils.log(utils.cur(), "-" * 10, meflag)
def watcher_conn(self, handler, mode, state, path): self._conn_cv.acquire() event = ClientEvent(mode, state, path) if state == zookeeper.EXPIRED_SESSION_STATE: utils.log(utils.cur(), self._name, event.state_name) try: zookeeper.close(self._handler) except: pass self._connected = False self.safe_connect() elif state == zookeeper.CONNECTED_STATE: utils.log(utils.cur(), self._name, event.state_name) self._connected = True else: utils.log(utils.cur(), self._name, event.state_name) self._conn_cv.notifyAll() self._conn_cv.release()
def run(self): while self.running: if callable(self.callback): try: self.pika.recv_msg(self.callback, self.queue) except Exception, err: utils.err(utils.cur(), err) self.is_server_down = True self.try_reconnect()
def register_service(self, service, port=0): path = "" if service == "master": path = ZKConf.ZK_PATH_TEMP_MASTER elif service == "stats": path = ZKConf.ZK_PATH_TEMP_STATS elif service == "proxy": path = ZKConf.ZK_PATH_TEMP_PROXY if not path: raise Exception("service type '%s' doesn't exist" % (service,)) node = zookeeper.exists(self._handler, path, None) if not node: if not zookeeper.exists(self._handler, ZKConf.ZK_PATH_ROOT, None): zookeeper.create(self._handler, ZKConf.ZK_PATH_ROOT, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) zookeeper.create(self._handler, path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) ip = utils.getip() mepath = "".join([path, "/", ip, ":", str(port), "-"]) if not port: mepath = "".join([path, "/", ip, "-"]) childs = zookeeper.get_children(self._handler, path, None) is_created = False pathme = mepath.split("/")[-1] for child in childs: if child.startswith(pathme): is_created = child.split("-")[-1] break if is_created: meflag = mepath + is_created utils.log(utils.cur(), "%s none-callback %s" % ("*"*10, "*"*10), meflag) else: utils.log(utils.cur(), "%s %s %s" % ("*"*10, self.register_node_cb, "*"*10), mepath) meflag = zookeeper.create(self._handler, mepath, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.SEQUENCE|zookeeper.EPHEMERAL) utils.log(utils.cur(), "-"*10, meflag)
def safe_get(*args, **kwargs): func, result = getattr(zookeeper, name), None def real_func(): if name in ("get", "exists", "get_children"): path, watcher = args[0], args[1] if len(args) > 1 else None return func(self._handler, path, self._wrap_watcher(watcher)) else: return func(self._handler, *args, **kwargs) try: result = real_func() except zookeeper.SessionExpiredException, err: utils.err(utils.cur(), err, "session expired, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func()
def safe_connect(self): self._conn_cv.acquire() self._handler = zookeeper.init( ZKConf.ZK_HOST, self.watcher_conn, ZKConf.ZOO_SESSION_TIMEOUT) # milliseconds self._conn_cv.wait(8.0) self._conn_cv.release() try_times = 10 while not self._connected and try_times > 0: self.connect() try_times -= 1 if not self._connected: raise ZooKeeperError("zookeeper [ %s ] safe_connect() failed" % str(ZKConf.ZK_HOST)) try: user_pass = "******" % (ZKConf.ZOO_USERNAME, ZKConf.ZOO_PASSWORD) utils.log(utils.cur(), self._name, "--add_auth--") zookeeper.add_auth(self._handler, "digest", user_pass, None) if self.register_node_cb: try: func = getattr(self, self.register_node_cb) if func and callable(func): utils.log(utils.cur(), self.register_node_cb, "%s callback %s" % ("*" * 10, "*" * 10)) def _do_register_node(): func(*self._args, **self._kwargs) _do_register_node() """ check whether temporary nodes are still existed when session timeout elapsed because there are old temporary nodes when register new temporary nodes """ threading.Timer(int(ZKConf.ZOO_SESSION_TIMEOUT / 1000), _do_register_node).start() except AttributeError, err: utils.err(utils.cur(), traceback.format_exc()) utils.log(utils.cur(), self._first, self.register_watch_cb) if not self._first: if self.register_watch_cb: try: utils.log(utils.cur(), "ready to callback") self.register_watch_cb() except Exception, err: utils.err(utils.cur(), traceback.format_exc())
def safe_get(*args, **kwargs): func, result = getattr(zookeeper, name), None def real_func(): if name in ("get", "exists", "get_children"): path, watcher = args[0], args[1] if len(args) > 1 else None return func(self._handler, path, self._wrap_watcher(watcher)) else: return func(self._handler, *args, **kwargs) try: result = real_func() except zookeeper.SessionExpiredException, err: utils.err( utils.cur(), err, "session expired, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func()
def safe_connect(self): self._conn_cv.acquire() self._handler = zookeeper.init(ZKConf.ZK_HOST, self.watcher_conn, ZKConf.ZOO_SESSION_TIMEOUT) # milliseconds self._conn_cv.wait(8.0) self._conn_cv.release() try_times = 10 while not self._connected and try_times > 0: self.connect() try_times -= 1 if not self._connected: raise ZooKeeperError("zookeeper [ %s ] safe_connect() failed" % str(ZKConf.ZK_HOST)) try: user_pass = "******" % (ZKConf.ZOO_USERNAME, ZKConf.ZOO_PASSWORD) utils.log(utils.cur(), self._name, "--add_auth--") zookeeper.add_auth(self._handler, "digest", user_pass, None) if self.register_node_cb: try: func = getattr(self, self.register_node_cb) if func and callable(func): utils.log(utils.cur(), self.register_node_cb, "%s callback %s" % ("*"*10, "*"*10)) def _do_register_node(): func(*self._args, **self._kwargs) _do_register_node() """ check whether temporary nodes are still existed when session timeout elapsed because there are old temporary nodes when register new temporary nodes """ threading.Timer(int(ZKConf.ZOO_SESSION_TIMEOUT/1000), _do_register_node).start() except AttributeError, err: utils.err(utils.cur(), traceback.format_exc()) utils.log(utils.cur(), self._first, self.register_watch_cb) if not self._first: if self.register_watch_cb: try: utils.log(utils.cur(), "ready to callback") self.register_watch_cb() except Exception, err: utils.err(utils.cur(), traceback.format_exc())
def __getattr__(self, name): if not isinstance(name, str) or not hasattr(zookeeper, name): raise ZooKeeperError("Method %s() doesn't exist" % str(name)) if name in ("ASSOCIATING_STATE", "AUTH_FAILED_STATE", "CONNECTED_STATE", "CONNECTING_STATE", "EXPIRED_SESSION_STATE", "NOTWATCHING_EVENT", "SESSION_EVENT", "CREATED_EVENT", "DELETED_EVENT", "CHANGED_EVENT", "CHILD_EVENT"): return getattr(zookeeper, name) def safe_get(*args, **kwargs): func, result = getattr(zookeeper, name), None def real_func(): if name in ("get", "exists", "get_children"): path, watcher = args[0], args[1] if len(args) > 1 else None return func(self._handler, path, self._wrap_watcher(watcher)) else: return func(self._handler, *args, **kwargs) try: result = real_func() except zookeeper.SessionExpiredException, err: utils.err( utils.cur(), err, "session expired, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func() except zookeeper.ConnectionLossException, err: utils.err( utils.cur(), err, "connection loss, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func()
threading.Timer(int(ZKConf.ZOO_SESSION_TIMEOUT/1000), _do_register_node).start() except AttributeError, err: utils.err(utils.cur(), traceback.format_exc()) utils.log(utils.cur(), self._first, self.register_watch_cb) if not self._first: if self.register_watch_cb: try: utils.log(utils.cur(), "ready to callback") self.register_watch_cb() except Exception, err: utils.err(utils.cur(), traceback.format_exc()) else: self._first = False except Exception, err: utils.err(utils.cur(), err) def watcher_conn(self, handler, mode, state, path): self._conn_cv.acquire() event = ClientEvent(mode, state, path) if state == zookeeper.EXPIRED_SESSION_STATE: utils.log(utils.cur(), self._name, event.state_name) try: zookeeper.close(self._handler) except: pass self._connected = False self.safe_connect() elif state == zookeeper.CONNECTED_STATE: utils.log(utils.cur(), self._name, event.state_name) self._connected = True
def elect_leader(self, service_path, port, callback, specific_leader_path, first=False): meflag = None if not self.exists(specific_leader_path, None): self.mknode(specific_leader_path) def unsafe_check(event, check_callback, my_id): ip_dict, ip_self = self.get_leaders(ZKConf.ZK_PATH_LEADER, specific_leader_path) me_path = self.ip + ":" + str(port) if port else self.ip me_path = os.path.join(specific_leader_path, me_path) utils.log(utils.cur(), ip_dict, ip_self) utils.log(utils.cur(), self.ip, me_path) if ip_self: if (not self.ip in ip_dict) and \ (ip_self in ip_dict) and \ (ip_dict[ip_self] > 1): leader_childs = self.get_children(specific_leader_path, None) for leader_child in leader_childs: self.delnode( os.path.join(specific_leader_path, leader_child)) utils.log(utils.cur(), me_path) self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) childs = sorted( self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith( me_path.split("/")[-1]): callback(True) else: callback(False) else: utils.log(utils.cur(), my_id, ip_self) childs = sorted( self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith( my_id.split(":")[0]): callback(True) else: callback(False) else: self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) utils.log(utils.cur(), me_path) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith( me_path.split("/")[-1]): callback(True) else: callback(False) def check_childs(event, true_path): try: utils.log(utils.cur(), event.type_name, event.state_name) except: utils.log(utils.cur(), event) my_id = self.ip + ":" + str(port) if port else self.ip i = 1 while not self.try_lock(my_id) and i < 100: utils.log(utils.cur(), "'%s' tries lock %d times" % (my_id, i)) i += 1 time.sleep(1) utils.log(utils.cur(), "'%s' tries lock %d times and exit" % (my_id, i)) if i < 100: utils.log(utils.cur(), "'%s' got lock and unsafe_checking" % my_id) unsafe_check(event, self.unlock, my_id) if not self.exists(service_path, None): self.create(service_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) mepath = "".join([service_path, "/", self.ip, "-"]) if port: mepath = "".join([service_path, "/", self.ip, ":", str(port), "-"]) childs = self.get_children(service_path, None) self.watch_child(service_path, check_childs) utils.log(utils.cur(), service_path, childs) created = False pathme = mepath.split("/")[-1] for child in childs: if child.startswith(pathme): created = child.split("-")[-1] break if created: meflag = mepath + created check_childs(None, None) else: meflag = self.create(mepath, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.SEQUENCE | zookeeper.EPHEMERAL)
def elect_leader(self, service_path, port, callback, specific_leader_path, first=False): meflag = None if not self.exists(specific_leader_path, None): self.mknode(specific_leader_path) def unsafe_check(event, check_callback, my_id): ip_dict, ip_self = self.get_leaders(ZKConf.ZK_PATH_LEADER, specific_leader_path) me_path = self.ip + ":" + str(port) if port else self.ip me_path = os.path.join(specific_leader_path, me_path) utils.log(utils.cur(), ip_dict, ip_self) utils.log(utils.cur(), self.ip, me_path) if ip_self: if (not self.ip in ip_dict) and \ (ip_self in ip_dict) and \ (ip_dict[ip_self] > 1): leader_childs = self.get_children(specific_leader_path, None) for leader_child in leader_childs: self.delnode(os.path.join(specific_leader_path, leader_child)) utils.log(utils.cur(), me_path) self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith(me_path.split("/")[-1]): callback(True) else: callback(False) else: utils.log(utils.cur(), my_id, ip_self) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith(my_id.split(":")[0]): callback(True) else: callback(False) else: self.create(me_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.EPHEMERAL) utils.log(utils.cur(), me_path) childs = sorted(self.get_children(specific_leader_path, None)) for child in childs[1:]: self.delnode(os.path.join(specific_leader_path, child)) check_callback(my_id) if len(childs) and childs[0].startswith(me_path.split("/")[-1]): callback(True) else: callback(False) def check_childs(event, true_path): try: utils.log(utils.cur(), event.type_name, event.state_name) except: utils.log(utils.cur(), event) my_id = self.ip + ":" + str(port) if port else self.ip i = 1 while not self.try_lock(my_id) and i < 100: utils.log(utils.cur(), "'%s' tries lock %d times" % (my_id, i)) i += 1 time.sleep(1) utils.log(utils.cur(), "'%s' tries lock %d times and exit" % (my_id, i)) if i < 100: utils.log(utils.cur(), "'%s' got lock and unsafe_checking" % my_id) unsafe_check(event, self.unlock, my_id) if not self.exists(service_path, None): self.create(service_path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) mepath = "".join([service_path, "/", self.ip, "-"]) if port: mepath = "".join([service_path, "/", self.ip, ":", str(port), "-"]) childs = self.get_children(service_path, None) self.watch_child(service_path, check_childs) utils.log(utils.cur(), service_path, childs) created = False pathme = mepath.split("/")[-1] for child in childs: if child.startswith(pathme): created = child.split("-")[-1] break if created: meflag = mepath + created check_childs(None, None) else: meflag = self.create(mepath, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.SEQUENCE|zookeeper.EPHEMERAL)
class ZooKeeper: def __init__(self, name, register_watch_cb=None, register_node_cb=None, *args, **kwargs): self._name = name self.register_watch_cb = register_watch_cb self.register_node_cb = register_node_cb self._args = args self._kwargs = kwargs self.ip = utils.getip() self._handler = None self.connect(True) self.i = 0 def connect(self, first=False): try: if self._handler: zookeeper.close(self._handler) except: pass self._handler = Connection(self._name, self.register_watch_cb, first, self.register_node_cb, *self._args, **self._kwargs).handler def __nonzero__(self): return int(self.client_id()[0]) def __str__(self): return str(self.client_id()) def _wrap_watcher(self, watcher): if watcher is None: return watcher def wrapper(handle, event_type, conn_state, path): event = ClientEvent(event_type, conn_state, path) return watcher(event) return wrapper def __getattr__(self, name): if not isinstance(name, str) or not hasattr(zookeeper, name): raise ZooKeeperError("Method %s() doesn't exist" % str(name)) if name in ("ASSOCIATING_STATE", "AUTH_FAILED_STATE", "CONNECTED_STATE", "CONNECTING_STATE", "EXPIRED_SESSION_STATE", "NOTWATCHING_EVENT", "SESSION_EVENT", "CREATED_EVENT", "DELETED_EVENT", "CHANGED_EVENT", "CHILD_EVENT"): return getattr(zookeeper, name) def safe_get(*args, **kwargs): func, result = getattr(zookeeper, name), None def real_func(): if name in ("get", "exists", "get_children"): path, watcher = args[0], args[1] if len(args) > 1 else None return func(self._handler, path, self._wrap_watcher(watcher)) else: return func(self._handler, *args, **kwargs) try: result = real_func() except zookeeper.SessionExpiredException, err: utils.err( utils.cur(), err, "session expired, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func() except zookeeper.ConnectionLossException, err: utils.err( utils.cur(), err, "connection loss, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func() except zookeeper.OperationTimeoutException, err: utils.err( utils.cur(), err, "operation timeout, retry %s(%s,%s)" % (name, args, kwargs)) self.connect() result = real_func()
_do_register_node).start() except AttributeError, err: utils.err(utils.cur(), traceback.format_exc()) utils.log(utils.cur(), self._first, self.register_watch_cb) if not self._first: if self.register_watch_cb: try: utils.log(utils.cur(), "ready to callback") self.register_watch_cb() except Exception, err: utils.err(utils.cur(), traceback.format_exc()) else: self._first = False except Exception, err: utils.err(utils.cur(), err) def watcher_conn(self, handler, mode, state, path): self._conn_cv.acquire() event = ClientEvent(mode, state, path) if state == zookeeper.EXPIRED_SESSION_STATE: utils.log(utils.cur(), self._name, event.state_name) try: zookeeper.close(self._handler) except: pass self._connected = False self.safe_connect() elif state == zookeeper.CONNECTED_STATE: utils.log(utils.cur(), self._name, event.state_name) self._connected = True