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()
def test_decode_returns_string_value_dict_on_malformed_json_string_input( self): json.loads = mock.Mock() exception_message = "This should be a more specific exception which \ gets caught by the decode function" json.loads.side_effect = Exception(exception_message) self.assertEquals({"string_value": '{"foo":"bar}'}, funcs.decode('{"foo":"bar}'))
def test_decode_converts_json_to_dict(self): result_dict = funcs.decode( '{"pid":1,"string_value":"String","created":"2013-11-18 19:37:04"}' ) six.assertCountEqual(self, [u"pid", u"string_value", u"created"], result_dict.keys()) six.assertCountEqual(self, [1, u"String", u"2013-11-18 19:37:04"], result_dict.values())
def test_decode_converts_json_to_dict(self): result_dict = funcs.decode( '{"pid":1,"string_value":"String","created":"2013-11-18 19:37:04"}' ) self.assertEqual([u"pid", u"string_value", u"created"], result_dict.keys()) self.assertEqual([1, u"String", u"2013-11-18 19:37:04"], result_dict.values())
def _update_child_list(data): log.debug('[%s] New children: %s' % (self._path, sorted(data))) children = {} for child in data: fullpath = '%s/%s' % (self._path, child) data, stat = self._zk.retry(self._zk.get, fullpath) children[child] = funcs.decode(data) self._children = children self._execute_callbacks()
def set_data(self, data): """Sets self._data. Args: data: String or Dict of data to register with this object.""" if not data == self._data: self._data = data self._encoded_data = funcs.encode(data) self._decoded_data = funcs.decode(self._encoded_data) self._update_data()
def __init__(self, zk, path, data=None, state=False, ephemeral=False): # Set our local variables self._ephemeral = ephemeral self._zk = zk self._path = path self._state = state # Store both encdoed-string and decoded-dict versions of our 'data' # for comparison purposes later. self._data = data self._encoded_data = funcs.encode(data) self._decoded_data = funcs.decode(self._encoded_data) # Set a default watcher without a callback. self._watcher = Watcher(self._zk, path=self._path, watch_children=False)
def __init__(self, zk, path, data=None, state=True, ephemeral=False): # Set our local variables self._ephemeral = ephemeral self._zk = zk self._path = path self._state = state # Store both encdoed-string and decoded-dict versions of our 'data' # for comparison purposes later. self._data = data self._encoded_data = funcs.encode(data) self._decoded_data = funcs.decode(self._encoded_data) # Make sure that we have a watcher on the path we care about self._watcher = Watcher(self._zk, path=self._path, watch_children=False, callback=self._update)
def _update_root_data(data, stat): self.log.debug("Data change detected") # Since we set allow_missing_node to True, the 'data' passed back # is ALWAYS 'None'. This means that we need to actually go out and # explicitly get the data whenever this function is called. As # long as 'stat' is not None, we know the node exists so this will # succeed. if stat: self.log.debug("Node is registered.") data, self._stat = self._zk.retry(self._zk.get, self._path) else: # Just a bit of logging self.log.debug("Node is not registered.") self._data = funcs.decode(data) self._stat = stat self.log.debug("Data: %s, Stat: %s" % (self._data, self._stat)) self._execute_callbacks()
def __init__(self, zk, path, data=None, state=True): # Create our logger self.log = logging.getLogger('%s.Registration.%s' % (__name__, path)) # Set our local variables self._zk = zk self._path = path self._state = state # Store both encdoed-string and decoded-dict versions of our 'data' # for comparison purposes later. self._data = data self._encoded_data = funcs.encode(data) self._decoded_data = funcs.decode(self._encoded_data) # Make sure that we have a watcher on the path we care about self._watcher = Watcher(self._zk, path=self._path, watch_children=False, callback=self._update)
def _update_root_data(data, stat): log.debug('[%s] Data change detected' % self._path) # NOTE: Applies to Kazoo <= 0.9, fixed in >= 1.0 # # Since we set allow_missing_node to True, the 'data' passed back # is ALWAYS 'None'. This means that we need to actually go out and # explicitly get the data whenever this function is called. Try to # get the data with zk.get(). If a NoNodeError is thrown, then # we know the host is not registered. try: data, self._stat = self._zk.retry(self._zk.get, self._path) log.debug('[%s] Node is registered.' % self._path) except kazoo.exceptions.NoNodeError: log.debug('[%s] Node is not registered.' % self._path) self._data = funcs.decode(data) self._stat = stat log.debug('[%s] Data: %s, Stat: %s' % (self._path, self._data, self._stat)) self._execute_callbacks()
def test_decode_returns_none_on_empty_input(self): self.assertEqual(None, funcs.decode(''))
def test_decode_returns_dict_when_on_non_json_string_input(self): self.assertEquals({"string_value": "foo"}, funcs.decode("foo"))