Skip to content

Commit

Permalink
Merge pull request #831 from deniszh/backport/1.1.x/pr-829
Browse files Browse the repository at this point in the history
[1.1.x] Fix carbon cache locking
  • Loading branch information
deniszh authored Dec 16, 2018
2 parents d063d0d + bd7ce6d commit f08650b
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions lib/carbon/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,14 @@ def __init__(self, strategy=None):

@property
def counts(self):
with self.lock:
return [(metric, len(datapoints)) for (metric, datapoints)
in self.items()]
return [(metric, len(datapoints)) for (metric, datapoints)
in self.items()]

@property
def watermarks(self):
with self.lock:
return [(metric, min(datapoints.keys()), max(datapoints.keys()))
for (metric, datapoints) in self.items()
if datapoints]
return [(metric, min(datapoints.keys()), max(datapoints.keys()))
for (metric, datapoints) in self.items()
if datapoints]

@property
def is_full(self):
Expand All @@ -187,7 +185,8 @@ def drain_metric(self):
if not self:
return (None, [])
if self.strategy:
metric = self.strategy.choose_item()
with self.lock:
metric = self.strategy.choose_item()
else:
# Avoid .keys() as it dumps the whole list
metric = next(iter(self))
Expand All @@ -209,18 +208,18 @@ def pop(self, metric):

def store(self, metric, datapoint):
timestamp, value = datapoint
if timestamp not in self[metric]:
# Not a duplicate, hence process if cache is not full
if self.is_full:
log.msg("MetricCache is full: self.size=%d" % self.size)
events.cacheFull()
else:
with self.lock:
with self.lock:
if timestamp not in self[metric]:
# Not a duplicate, hence process if cache is not full
if self.is_full:
log.msg("MetricCache is full: self.size=%d" % self.size)
events.cacheFull()
else:
self.size += 1
self[metric][timestamp] = value
else:
# Updating a duplicate does not increase the cache size
self[metric][timestamp] = value
else:
# Updating a duplicate does not increase the cache size
self[metric][timestamp] = value


_Cache = None
Expand Down

0 comments on commit f08650b

Please sign in to comment.