def _update(self, data): """Updates the DataNode objects data values. If the values change via the Watcher object, update the DataNode cached settings so that we know what the current state of Zookeeper is. These are updated with the true values in Zookeeper (regardless of what the initial object was created with) so that when the set_data() method is called, we only make updates to Zookeeper if necessary.""" log.debug('[%s] Received updated data: %s' % (self._path, data)) # If the returned 'stat' is empty, then the node was deleted in # Zookeeper and we should update our local 'state'. if not data['stat']: log.debug("[%s] No stat supplied, setting local state " "to false." % self._path) self._state = False # Quickly check that if data['data'] is none, we just clear our # settings and jump out of this method. if not data['data']: log.error("[%s] No data supplied at all. Wiping out " "local data cache." % self._path) self._encoded_data = funcs.encode(None) self._decoded_data = None self._data = None return # First, store the directly supplied data as our _decoded_data # (string), and then decode that into a proper hash and store it # as our _encoded_data. self._encoded_data = funcs.encode(data['data']) self._decoded_data = dict(data['data']) # Strictly speaking, the self._data object should contain the user # supplied data object, without any of the additional data # automatically supplied by funcs.default_data(). If this DataNode # object receives an updated bunch of data from Zookeeper, it will # include these additional data parameters. We need to strip those # out first, to ensure that we're only storing the user-supplied # parameters. self._data = dict(data['data']) for k in funcs.default_data().keys(): self._data.pop(k, None) # If the only key left in the self._data object is 'string_value', # then the supplied user data was actually in string format -- so # thats actually what we want to save. if self._data.keys() == ['string_value']: self._data = self._data['string_value']
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 __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 test_encode_creates_dict_from_single_string(self): to_encode = "String" json_data = funcs.encode(to_encode) self.assertIn(b'"string_value":"String"', json_data)
def test_encode_adds_extra_properties(self): json_data = funcs.encode({"foo": "bar", "baz": "foo"}) self.assertIn(b'"pid":', json_data) self.assertIn(b'"created":"', json_data)