示例#1
0
    def _update(self, data, stat):
        """Function executed by Kazoo upon data/stat changes for a path.

        This function is registered during the __init__ process of this
        Object with Kazoo. Upon the initial registration, and any subsequent
        changes to the 'data' or 'stat' of a path, this function is called
        again.

        This function is responsible for updating the local Watcher object
        state (its path data, stat data, etc) and triggering any callbacks
        that have been registered with this Watcher.

        args:
            data: The 'data' stored in Zookeeper for this path
            stat: The 'stat' data for this path
        """
        self._data = funcs.decode(data)
        self._stat = stat
        log.debug('[%s] Data change detected: %s, Stat: %s' %
                  (self._path, self._data, self._stat))

        # ChildrenWatches can only be applied to already existing paths
        # (Kazoo throws a NoNodeError otherwise). To prevent this NoNodeError
        # from being thrown, we only register an additional ChildrenWatch
        # in the event that 'stat' was not None.
        if self._watch_children and stat and not self._current_children_watch:
            log.debug('[%s] Registering ChildrenWatch' % self._path)
            self._current_children_watch = watchers.ChildrenWatch(
                client=self._zk, path=self._path, func=self._update_children)

        # Lastly, execute our callbacks
        self._execute_callbacks()
示例#2
0
    def connect(self, timeout=10.0):
        def try_clean():
            # Attempt to do the needed cleanup if post-connection setup does
            # not succeed (maybe the connection is lost right after it is
            # obtained).
            try:
                self.close()
            except k_exceptions.KazooException:
                LOG.exception("Failed cleaning-up after post-connection"
                              " initialization failed")

        try:
            if timeout is not None:
                timeout = float(timeout)
            self._client.start(timeout=timeout)
        except (self._client.handler.timeout_exception,
                k_exceptions.KazooException) as e:
            raise excp.ConnectionFailure("Failed to connect to"
                                         " zookeeper due to: %s" % (e))
        try:
            kazoo_utils.check_compatible(self._client, MIN_ZK_VERSION)
            self._client.ensure_path(self.path)
            self._job_watcher = watchers.ChildrenWatch(
                self._client,
                self.path,
                func=self._on_job_posting,
                allow_session_lost=False)
        except excp.IncompatibleVersion:
            with excutils.save_and_reraise_exception():
                try_clean()
        except (self._client.handler.timeout_exception,
                k_exceptions.KazooException) as e:
            try_clean()
            raise excp.ConnectionFailure("Failed to do post-connection"
                                         " initialization due to: %s" % (e))
示例#3
0
    def connect(self, timeout=10.0):
        def try_clean():
            # Attempt to do the needed cleanup if post-connection setup does
            # not succeed (maybe the connection is lost right after it is
            # obtained).
            try:
                self.close()
            except k_exceptions.KazooException:
                LOG.exception("Failed cleaning-up after post-connection"
                              " initialization failed")

        try:
            if timeout is not None:
                timeout = float(timeout)
            self._client.start(timeout=timeout)
            self._closing = False
        except (self._client.handler.timeout_exception,
                k_exceptions.KazooException):
            excp.raise_with_cause(excp.JobFailure,
                                  "Failed to connect to zookeeper")
        try:
            if self._conf.get('check_compatible', True):
                kazoo_utils.check_compatible(self._client, self.MIN_ZK_VERSION)
            if self._worker is None and self._emit_notifications:
                self._worker = futurist.ThreadPoolExecutor(max_workers=1)
            self._client.ensure_path(self.path)
            self._client.ensure_path(self.trash_path)
            if self._job_watcher is None:
                self._job_watcher = watchers.ChildrenWatch(
                    self._client,
                    self.path,
                    func=self._on_job_posting,
                    allow_session_lost=True)
            self._connected = True
        except excp.IncompatibleVersion:
            with excutils.save_and_reraise_exception():
                try_clean()
        except (self._client.handler.timeout_exception,
                k_exceptions.KazooException):
            exc_type, exc, exc_tb = sys.exc_info()
            try:
                try_clean()
                excp.raise_with_cause(excp.JobFailure,
                                      "Failed to do post-connection"
                                      " initialization",
                                      cause=exc)
            finally:
                del (exc_type, exc, exc_tb)
示例#4
0
    def test_child_watch(self):
        updates = collections.deque()
        ev = Event()

        def one_time_collector_func(children):
            updates.extend(children)
            if children:
                ev.set()
                return False

        with start_close(self.client) as c:
            k_watchers.ChildrenWatch(self.client,
                                     "/",
                                     func=one_time_collector_func)
            c.ensure_path("/b")
            self.assertTrue(ev.wait(WAIT_TIME))

        self.assertEqual(['b'], list(updates))
示例#5
0
    def __init__(self, zookeeperHosts='localhost:2181'):
        # might need to handle exceptions here
        # and try a list of zookeeper hosts
        self.zookeeper = KazooClient(zookeeperHosts)
        self.zookeeper.start()

        # use a hashtable to store all circuit states fetched from zookeeper
        self.circuitStates = {}
        # update the circuit state dictionary
        self._getCircuitState()

        # add to serval service table
        for auth in self.circuitStates.keys():
            next_ip = self.circuitStates[auth]['next_ip']
            next_auth = self.circuitStates[auth]['next_auth']
            register_service(auth, next_ip, next_auth)

        # set watcher on root node
        watchers.ChildrenWatch(self.zookeeper, '/circuit', self._circuitNodeWatcher)