Skip to content

Commit

Permalink
Fix memory leak around timestamp precision adjust
Browse files Browse the repository at this point in the history
* Global MAX_REASONABLE_TIMESTAMP adjusted in error. Due to arbitrary precision
arithmetic, over time memory can be seen leaking for lines processed with ms
precision timestamps.

* Perform timestamp precision check before converting timestamp to int to
save allocation of buffer.

* Replace space normalization looping construct with split/join.
  • Loading branch information
cheynearista authored and tsuna committed Oct 12, 2018
1 parent ae9014a commit 7b7ab7e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions tcollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ def process_line(self, col, line):
self.lines_collected += 1
# If the line contains more than a whitespace between
# parameters, it won't be interpeted.
while ' ' in line:
line = line.replace(' ', ' ')
line = ' '.join(line.split())

col.lines_received += 1
if len(line) >= 1024: # Limit in net.opentsdb.tsd.PipelineFactory
Expand All @@ -345,13 +344,14 @@ def process_line(self, col, line):
col.lines_invalid += 1
return
metric, timestamp, value, tags = parsed.groups()
timestamp = int(timestamp)

# If there are more than 11 digits we're dealing with a timestamp
# with millisecond precision
if len(str(timestamp)) > 11:
global MAX_REASONABLE_TIMESTAMP
MAX_REASONABLE_TIMESTAMP = MAX_REASONABLE_TIMESTAMP * 1000
max_timestamp = MAX_REASONABLE_TIMESTAMP
if len(timestamp) > 11:
max_timestamp = MAX_REASONABLE_TIMESTAMP * 1000

timestamp = int(timestamp)

# De-dupe detection... To reduce the number of points we send to the
# TSD, we suppress sending values of metrics that don't change to
Expand All @@ -373,7 +373,7 @@ def process_line(self, col, line):
col.values[key][3], timestamp, value, col.name)
col.lines_invalid += 1
return
elif timestamp >= MAX_REASONABLE_TIMESTAMP:
elif timestamp >= max_timestamp:
LOG.error("Timestamp is too far out in the future: metric=%s%s"
" old_ts=%d, new_ts=%d - ignoring data point"
" (value=%r, collector=%s)", metric, tags,
Expand Down

0 comments on commit 7b7ab7e

Please sign in to comment.