Exemple #1
0
    def async_exists(self, path, watcher=None, callback=None):
        """Async call to check if zookeeper node  exists.
        
        Args:
            path: zookeeper node path
            watcher: watcher method to be invoked upon node creation
                or removal with Zookeeper.Event as its sole argument.
                watcher will be invoked in the context of the underlying
                zookeeper API threads.
            callback: callback method to be invoked upon operation completion with
                zookeeper api handle, return_code, and stat arguments. callback
                will be invoked in the context of the underlying zookeeper API
                thread.

        Returns:
            Zookeeper.AsyncResult if callback is None, otherwise None.
        """
        async_result = None

        if callback is None:
            async_result = self.AsyncResult()

            def async_callback(handle, return_code, stat):
                if return_code == zookeeper.OK:
                    async_result.set(stat)
                elif return_code == zookeeper.NONODE:
                    async_result.set(None)
                else:
                    async_result.set_exception(self.error_to_exception(return_code))

            callback = async_callback

        zookeeper.aexists(self.handle, path, self._watcher_proxy(watcher), callback)

        return async_result
Exemple #2
0
    def async_exists(self, path, watcher=None):
        """Async call to check if zookeeper node  exists.
        
        Args:
            path: zookeeper node path
            watcher: watcher method to be invoked upon node creation
                or removal with Zookeeper.Event as its sole argument.
                watcher will be invoked in the context of a newly
                spawned greenlet.
        Returns:
            Zookeeper.AsyncResult if callback is None, otherwise None.
        """
        async_result = self._async_result()

        def callback(handle, return_code, stat):
            if return_code == zookeeper.OK:
                async_result.set(stat)
            elif return_code == zookeeper.NONODE:
                async_result.set(None)
            else:
                async_result.set_exception(self.error_to_exception(return_code))

        watcher_callback, greenlet = self._spawn_watcher(watcher)

        zookeeper.aexists(self.handle, path, watcher_callback, callback)

        return async_result
Exemple #3
0
    def exists_async(self, path, watch=None):
        """Asynchronously check if a node exists

        @param path: path of node
        @param watch: optional watch callback to set for future changes to this path
        @return stat of the node if it exists, else None
        """
        async_result = self._sync.async_result()
        callback = partial(_exists_callback, async_result)
        watch_callback = self._wrap_watch_callback(watch) if watch else None

        zookeeper.aexists(self._handle, path, watch_callback, callback)
        return async_result
Exemple #4
0
    def exists_async(self, path, watch=None):
        """Asynchronously check if a node exists

        @param path: path of node
        @param watch: optional watch callback to set for future changes to this path
        @return stat of the node if it exists, else None
        """
        async_result = self._sync.async_result()
        callback = partial(_exists_callback, async_result)
        watch_callback = self._wrap_watch_callback(watch) if watch else None

        zookeeper.aexists(self._handle, path, watch_callback, callback)
        return async_result
Exemple #5
0
    def exists_async(self, path, watcher):
        async_result = self._new_async_result()
        watcher_callback, watcher_greenlet = self._setup_watcher(watcher)

        def callback(handle, code, stat):
            if code == zookeeper.NONODE:
                self._queue_result(async_result, False, (False, None),
                    watcher_greenlet)
            else:
                self._queue_result(async_result, code != zookeeper.OK,
                    err_to_exception(code) if code != zookeeper.OK
                       else (True, stat), watcher_greenlet)

        zookeeper.aexists(self._handle, path, watcher_callback, callback)
        return async_result
 def exists(self, path, watch=None):
     """checks the existence of a node in zookeeper.
 
     path: the name of the node. Expressed as a file name with slashes 
     separating ancestors of the node.
 
     (Subsequent parameters are optional)
 
     watch: if not None, a watch will be set at the server to notify the 
     client if the node changes. The watch will be set even if the node does not 
     exist. This allows clients to watch for nodes to appear.
     
     Return: stat if the node exists    
     """
     results = []
     pc = utils.PipeCondition()
     ok = zookeeper.aexists(self._zhandle, path, watch, functools.partial(generic_completion, pc, results))
     assert ok == zookeeper.OK
     pc.wait()
     # unpack result as stat_completion
     handle, rc, stat = results
     assert handle == self._zhandle
     if rc == zookeeper.OK:
         return stat
     self._raise_exception(rc)
 def exists(self, path, watch=None):
     """checks the existence of a node in zookeeper.
 
     path: the name of the node. Expressed as a file name with slashes 
     separating ancestors of the node.
 
     (Subsequent parameters are optional)
 
     watch: if not None, a watch will be set at the server to notify the 
     client if the node changes. The watch will be set even if the node does not 
     exist. This allows clients to watch for nodes to appear.
     
     Return: stat if the node exists    
     """
     results = []
     pc = utils.PipeCondition()
     ok = zookeeper.aexists(
         self._zhandle, path, watch,
         functools.partial(generic_completion, pc, results))
     assert ok == zookeeper.OK
     pc.wait()
     #unpack result as stat_completion
     handle, rc, stat = results
     assert handle == self._zhandle
     if rc == zookeeper.OK:
         return stat
     self._raise_exception(rc)
Exemple #8
0
  def _aexists(self, path):
    if add_missing(self.__misslck, self.__missing, path):
      debug('_aexists is hooking in a callback on existence')
      watcher = self._events
    else:
      debug('_aexists is NOT hooking in a callback on existence')
      watcher = None

    self._try_zoo(
        lambda: self._use_socket(
          lambda z: zookeeper.aexists(z, path, watcher, self._exist_cb(path))))
Exemple #9
0
    def enter(self):
        self.name = zookeeper.create(self.handle, self.barrier + "/" + self.name, '\x00', [ZOO_OPEN_ACL_UNSAFE], 3) 
	self.ready = False
        def ready_watcher(handle, rc, stat):
            self.cv.acquire()
            self.cv.notify()
	    self.ready = True
            self.cv.release()
	zookeeper.aexists(self.handle, self.barrier + "/ready", None, ready_watcher)
	children = zookeeper.get_children(self.handle, self.barrier , None)
	while(len(children) < self.workers):
	    self.cv.acquire()
	    if self.ready:
	        break
	    print "Waiting for others. Number of children %s" %(len(children))
	    self.cv.wait(15.0)
	    self.cv.release()
        
	if not self.ready:
	    zookeeper.create(self.handle, self.barrier + "/ready", '\x00', [ZOO_OPEN_ACL_UNSAFE], 1)

        return True
Exemple #10
0
    def async_exists(self, path, watcher=None, callback=None):
        """Async call to check if zookeeper node  exists.
        
        Args:
            path: zookeeper node path
            watcher: watcher method to be invoked upon node creation
                or removal with Zookeeper.Event as its sole argument.
                watcher will be invoked in the context of the underlying
                zookeeper API threads.
            callback: callback method to be invoked upon operation completion with
                zookeeper api handle, return_code, and stat arguments. callback
                will be invoked in the context of the underlying zookeeper API
                thread.

        Returns:
            Zookeeper.AsyncResult if callback is None, otherwise None.
        """
        async_result = None

        if callback is None:
            async_result = self.AsyncResult()

            def async_callback(handle, return_code, stat):
                if return_code == zookeeper.OK:
                    async_result.set(stat)
                elif return_code == zookeeper.NONODE:
                    async_result.set(None)
                else:
                    async_result.set_exception(
                        self.error_to_exception(return_code))

            callback = async_callback

        zookeeper.aexists(self.handle, path, self._watcher_proxy(watcher),
                          callback)

        return async_result
Exemple #11
0
    def _exists(self, path, watcher):
        d = defer.Deferred()
        if self._check_connected(d):
            return d

        def _cb_exists(result_code, stat):
            if self._check_result(
                result_code, d, extra_codes=(zookeeper.NONODE,), path=path):
                return
            d.callback(stat)

        callback = self._zk_thread_callback(_cb_exists)
        watcher = self._wrap_watcher(watcher, "exists", path)
        result = zookeeper.aexists(self.handle, path, watcher, callback)
        self._check_result(result, d, path=path)
        return d
    def test_dispatch_types(self):
        """
        Test all the various dispatch mechanisms internal to the module.
        """
        def dispatch_callback(*args, **kwargs):
            self.callback_flag = True

        self.ensureCreated("/zk-python-dispatch-test")
        self.callback_harness(
            lambda: zookeeper.adelete(
                self.handle, "/zk-python-dispatch-test", -1,
                self.create_callback(dispatch_callback)), lambda: self.
            assertEqual(True, self.callback_flag, "Void dispatch not fired"))

        self.ensureCreated("/zk-python-dispatch-test")
        self.callback_harness(
            lambda: zookeeper.aexists(
                self.handle, "/zk-python-dispatch-test", None,
                self.create_callback(dispatch_callback)), lambda: self.
            assertEqual(True, self.callback_flag, "Stat dispatch not fired"))

        self.callback_harness(
            lambda: zookeeper.aget(
                self.handle, "/zk-python-dispatch-test", None,
                self.create_callback(dispatch_callback)), lambda: self.
            assertEqual(True, self.callback_flag, "Data dispatch not fired"))

        self.callback_harness(
            lambda: zookeeper.aget_children(
                self.handle, "/", None, self.
                create_callback(dispatch_callback)), lambda: self.assertEqual(
                    True, self.callback_flag, "Strings dispatch not fired"))

        self.callback_harness(
            lambda: getattr(zookeeper, 'async')
            (self.handle, "/", self.create_callback(dispatch_callback)),
            lambda: self.assertEqual(True, self.callback_flag,
                                     "String dispatch not fired"))

        self.callback_harness(
            lambda: zookeeper.aget_acl(
                self.handle, "/", self.create_callback(dispatch_callback)),
            lambda: self.assertEqual(True, self.callback_flag,
                                     "ACL dispatch not fired"))
Exemple #13
0
    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))
Exemple #14
0
 def aexists(self, path, callback, watcher=None):
     return zookeeper.aexists(self.handle, path, watcher, callback)
Exemple #15
0
 def aexists(self, path, callback, watcher=None):
     return zookeeper.aexists(self.handle, path, watcher, callback)