Fixed ceilometer arithmetic transformer bug

For the arithmetic transformer, when some thread use method _update_cache()
to update self.cache, and the same time the flush() method may be invoke
self.cache.pop(res_id) and raise "RuntimeError: dictionary changed size
during iteration"

Change-Id: I3ea89c7d3bfbdb661770b5d478188f8723c17716
Closes-bug: #1700875
(cherry picked from commit 91d11e7c79)
This commit is contained in:
xianbin 2017-06-28 11:13:02 +08:00 committed by xianbin xie
parent 73bd95d4c5
commit 40b36e0e2e
1 changed files with 8 additions and 5 deletions

View File

@ -14,6 +14,7 @@
# under the License.
import collections
import copy
import keyword
import math
import re
@ -101,14 +102,16 @@ class ArithmeticTransformer(transformer.TransformerBase):
def flush(self):
new_samples = []
cache_clean_list = []
if not self.misconfigured:
for resource_id in self.cache:
# When loop self.cache, the dict could not be change by others.
# If changed, will raise "RuntimeError: dictionary changed size
# during iteration". so we make a tmp copy and just loop it.
tmp_cache = copy.copy(self.cache)
for resource_id in tmp_cache:
if self._check_requirements(resource_id):
new_samples.append(self._calculate(resource_id))
cache_clean_list.append(resource_id)
for res_id in cache_clean_list:
self.cache.pop(res_id)
if resource_id in self.cache:
self.cache.pop(resource_id)
return new_samples
@classmethod