def watcher(handle, type, state, path): self.cv.acquire() if state == zookeeper.CONNECTED_STATE: print "Connected!" self.connected = True else: print "Disconnected from ZooKeeper: ", print zookeeper.zerror(state) self.cv.notify() self.cv.release()
def watcher (handle, type, state, path): self.cv.acquire() if state == zookeeper.CONNECTED_STATE: print "Connected!" self.connected = True else: print "Disconnected from ZooKeeper: ", print zookeeper.zerror(state) self.cv.notify() self.cv.release()
def test_async_getchildren(self): self.ensureCreated("/zk-python-getchildrentest", flags=0) self.ensureCreated("/zk-python-getchildrentest/child") def gc_callback(handle, rc, children): self.cv.acquire() self.rc = rc self.children = children self.callback_flag = True self.cv.notify() self.cv.release() self.cv.acquire() self.callback_flag = False zookeeper.aget_children(self.handle, "/zk-python-getchildrentest", None, gc_callback) self.cv.wait(15) self.assertEqual(self.callback_flag, True, "aget_children timed out") self.assertEqual( self.rc, zookeeper.OK, "Return code for aget_children was not OK - %s" % zookeeper.zerror(self.rc)) self.assertEqual( len(self.children), 1, "Expected to find 1 child, got " + str(len(self.children)))
def on_auth_callback(state, result): with self.cv: if result == zookeeper.AUTHFAILED: self.logger.error(zookeeper.zerror(zookeeper.AUTHFAILED)) self.logger.info("on_auth: state %s, result %s", state, result) self.cv.notify()
def error_to_exception(self, return_code, message=None): """Convert zookeeper error code to the appropriate exception. Args: return_code: zookeeper return code. message: exception message. Returns: zookeeper exception object which should be raised. """ try: error_message = zookeeper.zerror(return_code) except: error_message = "Unknown error code - %s" % return_code if message: message = "%s: %s" % (message, error_message) else: message = error_message exception_class = ERROR_CODE_EXCEPTION_MAP.get( return_code) or Exception return exception_class(message)
def _check_result(self, result_code, deferred, extra_codes=(), path=None): """Check an API call or result for errors. :param result_code: The api result code. :param deferred: The deferred returned the client api consumer. :param extra_codes: Additional result codes accepted as valid/ok. If the result code is an error, an appropriate Exception class is constructed and the errback on the deferred is invoked with it. """ error = None if not result_code == zookeeper.OK and not result_code in extra_codes: error_msg = zookeeper.zerror(result_code) if path is not None: error_msg += " %s" % path error_class = ERROR_MAPPING.get( result_code, zookeeper.ZooKeeperException) error = error_class(error_msg) if is_connection_exception(error): # Route connection errors to a connection level error # handler if specified. if self._connection_error_callback: # The result of the connection error handler is returned # to the api invoker. d = defer.maybeDeferred( self._connection_error_callback, self, error) d.chainDeferred(deferred) return True deferred.errback(error) return True return None
def __init__(self,queuename): self.connected = False self.queuename = "/" + queuename self.cv = threading.Condition() zookeeper.set_log_stream(open("/dev/null")) def watcher(handle,type,state,path): print "Connected" self.cv.acquire() self.connected = True self.cv.notify() self.cv.release() self.cv.acquire() self.handle = zookeeper.init("localhost:2181", watcher, 10000, 0) self.cv.wait(10.0) if not self.connected: print "Connection to ZooKeeper cluster timed out - is a server running on localhost:2181?" sys.exit() self.cv.release() try: zookeeper.create(self.handle,self.queuename,"queue top level", [ZOO_OPEN_ACL_UNSAFE],0) except IOError, e: if e.message == zookeeper.zerror(zookeeper.NODEEXISTS): print "Queue already exists" else: raise e
def __init__(self, server, root_node, host='localhost', port=2181): self.root = root_node self.host = host self.port = port self.counter = random.randint(0,2**30) self.server = server self.zcv = Condition() def watcher(handle,type,state,path): print "Z connected (%s:%s)" % (self.host, self.port) self.zcv.acquire() self.connected = True self.zcv.notify() self.zcv.release() self.zcv.acquire() self.connected = False self.handle = z.init("%s:%s"%(self.host,self.port), watcher, 10000, 0) self.zcv.wait(10.0) if not self.connected: print "Connection to Z cluster timed out - is a server running on %s:%s?" % (self.host,self.port) self.connected = False self.zcv.release() return self.zcv.release() # make sure root node exists try: self.create(self.root,"RACS root node", [ZOO_OPEN_ACL_UNSAFE]) except IOError, e: if e.args[0] != z.zerror(z.NODEEXISTS): raise e
def __init__(self, queuename): self.connected = False self.queuename = "/" + queuename self.cv = threading.Condition() zookeeper.set_log_stream(open("/dev/null")) def watcher(handle, type, state, path): print "Connected" self.cv.acquire() self.connected = True self.cv.notify() self.cv.release() self.cv.acquire() self.handle = zookeeper.init("localhost:2181", watcher, 10000, 0) self.cv.wait(10.0) if not self.connected: print "Connection to ZooKeeper cluster timed out - is a server running on localhost:2181?" sys.exit() self.cv.release() try: zookeeper.create(self.handle, self.queuename, "queue top level", [ZOO_OPEN_ACL_UNSAFE], 0) except IOError, e: if e.message == zookeeper.zerror(zookeeper.NODEEXISTS): print "Queue already exists" else: raise e
def get_and_delete(self, node): try: (data, stat) = zookeeper.get(self.handle, node, None) zookeeper.delete(self.handle, node, stat["version"]) return data except IOError, e: if e.message == zookeeper.zerror(zookeeper.NONODE): return None raise e
def get_and_delete(self,node): try: (data,stat) = zookeeper.get(self.handle, node, None) zookeeper.delete(self.handle, node, stat["version"]) return data except IOError, e: if e.message == zookeeper.zerror(zookeeper.NONODE): return None raise e
def __init__(self, **config): logger_name = config.get("logger_name") self.logger = logging.getLogger(logger_name) if logger_name else Null() self.zkhandle = None self.auth = None self.cv = threading.Condition() try: auth_config = config.get("auth") if auth_config is not None: auth_scheme = auth_config["scheme"] auth_data = auth_config["data"] self.auth = (auth_scheme, auth_data) zklogfile_path, zklog_level = config.get("ZookeeperLog", ("/dev/stderr", "WARN")) self.connection_timeout = config["timeout"] self.zkhosts = ",".join(config["host"]) except KeyError as err: self.logger.exception("Missing configuration option: %s", err) raise except Exception as err: self.logger.exception("Unknown configuration error: %s", err) raise try: _f = open(zklogfile_path, "a") except IOError as err: self.logger.error("Unable to open logfile %s %s", zklogfile_path, err) else: zookeeper.set_log_stream(_f) zookeeper.set_debug_level(LOG_LEVELS.get(zklog_level.upper(), zookeeper.LOG_LEVEL_WARN)) self.connect() if zookeeper.state(self.zkhandle) == zookeeper.CONNECTED_STATE: self.logger.info("Connected to Zookeeper successfully") else: raise zookeeper.ZooKeeperException("Unable to connect " "to Zookeeper") def on_auth_callback(state, result): with self.cv: if result == zookeeper.AUTHFAILED: self.logger.error(zookeeper.zerror(zookeeper.AUTHFAILED)) self.logger.info("on_auth: state %s, result %s", state, result) self.cv.notify() if self.auth: self.logger.info("Auth using %s", self.auth[0]) with self.cv: res = zookeeper.add_auth(self.zkhandle, self.auth[0], self.auth[1], on_auth_callback) if res != zookeeper.OK: self.logger.error("Invalid status %d", zookeeper.zerror(res)) raise Exception("Invalid status") self.cv.wait(self.connection_timeout) if zookeeper.state(self.zkhandle) == zookeeper.AUTH_FAILED_STATE: raise zookeeper.ZooKeeperException("authentication failed")
def error_to_exception(self, return_code, message=None): """Convert zookeeper error code to exceptions.""" try: error_message = zookeeper.zerror(return_code) except: error_message = "Unknown error code - %s" % return_code if message: message = "%s: %s" % (message, error_message) else: message = error_message exception_class = ERROR_CODE_EXCEPTION_MAP.get(return_code) or Exception return exception_class(message)
def test_async_getchildren(self): self.ensureCreated("/zk-python-getchildrentest", flags=0) self.ensureCreated("/zk-python-getchildrentest/child") def gc_callback(handle, rc, children): self.cv.acquire() self.rc = rc self.children = children self.callback_flag = True self.cv.notify() self.cv.release() self.cv.acquire() self.callback_flag = False zookeeper.aget_children(self.handle, "/zk-python-getchildrentest", None, gc_callback) self.cv.wait(15) self.assertEqual(self.callback_flag, True, "aget_children timed out") self.assertEqual( self.rc, zookeeper.OK, "Return code for aget_children was not OK - %s" % zookeeper.zerror(self.rc) ) self.assertEqual(len(self.children), 1, "Expected to find 1 child, got " + str(len(self.children)))
def test_async_exists(self): self.cv = threading.Condition() def callback(handle, rc, stat): self.cv.acquire() self.callback_flag = True self.cv.notify() self.cv.release() self.rc = rc self.assertEqual(self.connected, True) self.cv.acquire() ret = zookeeper.aexists(self.handle, "/zk-python-aexiststest", None, callback ) self.assertEqual(ret, zookeeper.OK) while not self.callback_flag: self.cv.wait(15) self.cv.release() self.assertEqual(self.callback_flag, True, "aexists timed out") self.assertEqual(self.rc, zookeeper.OK, "Return code not ok:" + zookeeper.zerror(self.rc))
def err_to_exception(error_code, msg=None): """Return an exception object for a Zookeeper error code """ try: zkmsg = zookeeper.zerror(error_code) except Exception: zkmsg = "" if msg: if zkmsg: msg = "%s: %s" % (zkmsg, msg) else: msg = zkmsg exc = _ERR_TO_EXCEPTION.get(error_code) if exc is None: # double check that it isn't an ok resonse if error_code == zookeeper.OK: return None # otherwise generic exception exc = Exception return exc(msg)
def error_to_exception(self, return_code, message=None): """Convert zookeeper error code to the appropriate exception. Args: return_code: zookeeper return code. message: exception message. Returns: zookeeper exception object which should be raised. """ try: error_message = zookeeper.zerror(return_code) except: error_message = "Unknown error code - %s" % return_code if message: message = "%s: %s" % (message, error_message) else: message = error_message exception_class = ERROR_CODE_EXCEPTION_MAP.get(return_code) or Exception return exception_class(message)
def _raise_exception(self, rc): raise self._rc2exception[rc](zookeeper.zerror(rc))
def __init__(self, **config): logger_name = config.get('logger_name') self.logger = logging.getLogger(logger_name) if logger_name else Null() self.zkhandle = None self.auth = None self.cv = threading.Condition() try: auth_config = config.get("auth") if auth_config is not None: auth_scheme = auth_config["scheme"] auth_data = auth_config["data"] self.auth = (auth_scheme, auth_data) zklogfile_path, zklog_level = config.get("ZookeeperLog", ("/dev/stderr", "WARN")) self.connection_timeout = config['timeout'] self.zkhosts = ','.join(config['host']) except KeyError as err: self.logger.exception("Missing configuration option: %s", err) raise except Exception as err: self.logger.exception("Unknown configuration error: %s", err) raise try: _f = open(zklogfile_path, 'a') except IOError as err: self.logger.error("Unable to open logfile %s %s", zklogfile_path, err) else: zookeeper.set_log_stream(_f) zookeeper.set_debug_level(LOG_LEVELS.get(zklog_level.upper(), zookeeper.LOG_LEVEL_WARN)) self.connect() if zookeeper.state(self.zkhandle) == zookeeper.CONNECTED_STATE: self.logger.info('Connected to Zookeeper successfully') else: raise zookeeper.ZooKeeperException('Unable to connect ' 'to Zookeeper') def on_auth_callback(state, result): with self.cv: if result == zookeeper.AUTHFAILED: self.logger.error(zookeeper.zerror(zookeeper.AUTHFAILED)) self.logger.info("on_auth: state %s, result %s", state, result) self.cv.notify() if self.auth: self.logger.info("Auth using %s", self.auth[0]) with self.cv: res = zookeeper.add_auth(self.zkhandle, self.auth[0], self.auth[1], on_auth_callback) if res != zookeeper.OK: self.logger.error("Invalid status %d", zookeeper.zerror(res)) raise Exception("Invalid status") self.cv.wait(self.connection_timeout) if zookeeper.state(self.zkhandle) == zookeeper.AUTH_FAILED_STATE: raise zookeeper.ZooKeeperException('authentication failed')