Skip to content

Commit

Permalink
Cached should not be used when creating gnocchi resources
Browse files Browse the repository at this point in the history
Once I delete resource manually by gnocchi client,
the resource won't be created by the ceilometer until 600s passed,
because the resource has cached in the memcached.

Change-Id: I601e39b4c4782276daba3f9d1b7be92f09efabfc
Closes-Bug: #1718570
  • Loading branch information
xuqiankun authored and dchavoll committed Aug 15, 2018
1 parent e58fe24 commit 7838fe0
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions ceilometer/publisher/gnocchi.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ def publish_samples(self, data):
if not resource_extra:
continue
try:
self._if_not_cached("update", resource_type, resource,
self._update_resource, resource_extra)
self._if_not_cached(resource_type, resource, resource_extra)
except gnocchi_exc.ClientException as e:
LOG.error(six.text_type(e))
except Exception as e:
Expand Down Expand Up @@ -377,8 +376,7 @@ def batch_measures(self, measures, resource_infos):
for resource_type, resource, resource_extra in resources:
try:
resource.update(resource_extra)
self._if_not_cached("create", resource_type, resource,
self._create_resource)
self._create_resource(resource_type, resource)
except gnocchi_exc.ResourceAlreadyExists:
# NOTE(sileht): resource created in the meantime
pass
Expand All @@ -389,6 +387,10 @@ def batch_measures(self, measures, resource_infos):
# and we can't patch it later
del measures[resource['id']]
del resource_infos[resource['id']]
else:
if self.cache:
self.cache.set(resource['id'],
self._hash_resource(resource))

# NOTE(sileht): we have created missing resources/metrics,
# now retry to post measures
Expand All @@ -410,12 +412,10 @@ def _update_resource(self, resource_type, resource, resource_extra):
resource_extra)
LOG.debug('Resource %s updated', resource["id"])

def _if_not_cached(self, operation, resource_type, resource, method,
*args, **kwargs):
def _if_not_cached(self, resource_type, resource, resource_extra):
if self.cache:
cache_key = resource['id']
attribute_hash = self._check_resource_cache(cache_key, resource)
hit = False
if attribute_hash:
with self._gnocchi_resource_lock[cache_key]:
# NOTE(luogangyi): there is a possibility that the
Expand All @@ -424,25 +424,25 @@ def _if_not_cached(self, operation, resource_type, resource, method,
attribute_hash = self._check_resource_cache(cache_key,
resource)
if attribute_hash:
method(resource_type, resource, *args, **kwargs)
self._update_resource(resource_type, resource,
resource_extra)
self.cache.set(cache_key, attribute_hash)
else:
hit = True
LOG.debug('resource cache recheck hit for '
'%s %s', operation, cache_key)
'%s', cache_key)
self._gnocchi_resource_lock.pop(cache_key, None)
else:
hit = True
LOG.debug('Resource cache hit for %s %s', operation, cache_key)
if hit and operation == "create":
raise gnocchi_exc.ResourceAlreadyExists()
LOG.debug('Resource cache hit for %s', cache_key)
else:
method(resource_type, resource, *args, **kwargs)
self._update_resource(resource_type, resource, resource_extra)

@staticmethod
def _hash_resource(resource):
return hash(tuple(i for i in resource.items() if i[0] != 'metrics'))

def _check_resource_cache(self, key, resource_data):
cached_hash = self.cache.get(key)
attribute_hash = hash(tuple(i for i in resource_data.items()
if i[0] != 'metrics'))
attribute_hash = self._hash_resource(resource_data)
if not cached_hash or cached_hash != attribute_hash:
return attribute_hash
else:
Expand Down

0 comments on commit 7838fe0

Please sign in to comment.