carbon icon indicating copy to clipboard operation
carbon copied to clipboard

memory leak in relay and aggregator daemons

Open dkulikovsky opened this issue 13 years ago • 39 comments

I've got a leaking megacarbon instances. The load is low - 50-100k metrics per minute. Config is simple - i have a pipeline 2 relays with aggregated-consistent-hashing -> 1 aggregator with consistent hashing -> 2 writers. Instances that run relay or aggregation role consumed tons of memory:

top - 16:03:22 up 6 days,  1:09,  7 users,  load average: 143.34, 76.86, 48.83
Tasks: 414 total,   4 running, 410 sleeping,   0 stopped,   0 zombie
Cpu(s):  8.2%us,  0.5%sy,  0.0%ni, 90.0%id,  1.2%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:  65949272k total, 65443728k used,   505544k free,  1215012k buffers
Swap:        0k total,        0k used,        0k free,   269768k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  WCHAN     COMMAND                                                                                                                        
 2824 www-data  20   0 23.7g  23g 1816 R 99.4 37.5  34:38.02 -         /usr/bin/python /var/lib/graphite/bin/carbon-daemon.py carbon-relay-st01 start                                                  
 2897 www-data  20   0 11.9g  11g 1760 R 99.4 18.8   9:28.29 -         /usr/bin/python /var/lib/graphite/bin/carbon-daemon.py carbon-relay-st02 start                                                  
15538 www-data  20   0 23.7g  23g 1796 S 44.7 37.5  14:47.56 ep_poll   /usr/bin/python /var/lib/graphite/bin/carbon-daemon.py carbon-aggregator-st01 start                                             
 1125 www-data  20   0  299m 121m 1860 S  7.9  0.2  31:58.85 poll_sche /usr/bin/python /var/lib/graphite/bin/carbon-daemon.py carbon-writer-st02 start                                                 
 1120 www-data  20   0  310m 146m 1860 S  7.4  0.2  37:09.29 poll_sche /usr/bin/python /var/lib/graphite/bin/carbon-daemon.py carbon-writer-st01 start                                                 
 1118 www-data  20   0  310m 146m 1860 S  3.2  0.2  20:11.00 ep_poll   /usr/bin/python /var/lib/graphite/bin/carbon-daemon.py carbon-writer-st01 start                                                 
 1124 www-data  20   0  299m 121m 1860 S  3.2  0.2  17:23.44 ep_poll   /usr/bin/python /var/lib/graphite/bin/carbon-daemon.py carbon-writer-st02 start               

That happens really fast, just after restarting megacarbon daemons and starting perf test tool, in first minute relay and aggregator consume about 720Mb of memory each, in the second minute memory usage grows to 3Gb each. After 20-30 min procs consume 6-11Gb each, and so on. Top output above is taken after 10hours after restarting carbon daemons (perf test lasted for 30min and then all these daemons works only on data it generates by itself). If RELAY_METHOD is set to relay-rules for relay instances - everything is fine, memory usage is about 30-50Mb, but for aggregator this doesn't help at all.

I've tried to catch this memory leak with different tools, but i didn't succeed.

Current config for relay is

REPLICATION_FACTOR = 1
MAX_DATAPOINTS_PER_MESSAGE = 5000
MAX_QUEUE_SIZE = 1000
USE_FLOW_CONTROL = True
DESTINATIONS = 127.0.0.1:12301:st01
RELAY_METHOD = aggregated-consistent-hashing

for aggregator:

REPLICATION_FACTOR = 1
MAX_DATAPOINTS_PER_MESSAGE = 5000
MAX_QUEUE_SIZE = 1000000
USE_FLOW_CONTROL = True
DESTINATIONS = 127.0.0.1:2301:st01, 127.0.0.1:2302:st02
RELAY_METHOD = consistent-hashing

dkulikovsky avatar Nov 19 '13 12:11 dkulikovsky

Sounds interesting, what is the size of your relays destination queues. Is it growing while the memory usage raises? I see something similar with carbon-relay (master branch) not freeing memory after it was used for queueing.

marco-hoyer avatar Nov 19 '13 12:11 marco-hoyer

Number of queued datapoints is growing while memory usage raises. I did add some debug logging and i saw that daemons do add new points to queue. I set queue limit for daemons to 1000 points and this doesn't help. Daemon hits the limit, drops datapoints but still uses Gbs of memory after a while.

19.11.2013, 16:23, "Marco Hoyer" [email protected]:

Sounds interesting, what is the size of your relays destination queues. Is it growing while the memory usage raises? I see something similar with carbon-relay (master branch) not freeing memory after it was used for queueing.

— Reply to this email directly or view it on GitHub.

dkulikovsky avatar Nov 19 '13 12:11 dkulikovsky

I reduced the number of aggregation-rules to 500 (was 1000) and memory usage dropped twice, the bug is somewhere there.

dkulikovsky avatar Nov 19 '13 16:11 dkulikovsky

Only a wild guess: could it be a case of catastrophic backtracking in one of your regular expressions in aggregation-rules causing things to back up?

On Tue, Nov 19, 2013 at 10:30 AM, dkulikovsky [email protected]:

I reduced the number of aggregation-rules to 500 (was 1000) and memory usage dropped twice, the bug is somewhere there.

— Reply to this email directly or view it on GitHubhttps://github.com/graphite-project/carbon/issues/185#issuecomment-28804964 .

mleinart avatar Nov 19 '13 16:11 mleinart

rules are pretty simple

one_min.agg.test995 (60) = avg one_min.agg_test.test995.m*
one_min.agg.test996 (60) = avg one_min.agg_test.test996.m*
one_min.agg.test997 (60) = avg one_min.agg_test.test997.m*
one_min.agg.test998 (60) = avg one_min.agg_test.test998.m*
one_min.agg.test999 (60) = avg one_min.agg_test.test999.m*
one_min.agg.test1000 (60) = avg one_min.agg_test.test1000.m*

I think i have found a leak, cache in rules can grow unattended, disabling it fixes the problem:

--- lib/carbon/aggregator/rules.py      2013-11-19 23:05:48.632684971 +0400
+++ /var/lib/graphite/lib/carbon/aggregator/rules.py    2013-11-19 23:00:44.158481765 +0400
@@ -93,7 +93,7 @@
       except:
         log.err("Failed to interpolate template %s with fields %s" % (self.output_template, extracted_fields))

-    self.cache[metric_path] = result
+    #self.cache[metric_path] = result
     return result

   def build_regex(self):

dkulikovsky avatar Nov 19 '13 20:11 dkulikovsky

We have a similar and constant memory leak in the relay when using persistent hashing. On a relay getting about 200k metrics per minute and taking a bit of time to write to disk (network storage with a bit of delay on IOPs), we have to restart relays every 12h to free the memory, and when I am saying free, we are talking about 20G leaked in a day.

frlinux avatar Nov 25 '13 22:11 frlinux

We also have an issue when using consistent hashing like FRLinux. Our environment is sending ~150k metrics every 10 seconds, which is a relatively light load for the node.

The relay executes the hash without much queuing, but the memory consumption spikes through the roof (~5GB) within 1 hour after relay restart. We overrun memory and OOM well under the 12h that FRLinux is seeing.

sebito91 avatar Nov 26 '13 16:11 sebito91

@dkulikovsky, would the ideal fix for this be a configuration item to limit the size of the cache, and logic to purge LRU items from the cache?

jamesjuran avatar Dec 03 '13 19:12 jamesjuran

It seems like the rate of memory growth would depend on how often "new" metrics are seen by the aggregator. I'm getting millions of metrics every 5 minutes, and very slow (but non-zero) growth in relay and aggregator memory usage. @frlinux, are you often seeing "new" metrics?

jamesjuran avatar Dec 03 '13 20:12 jamesjuran

Hey @jamesjuran, we don't have any real new metrics if by new you mean that did not exist before and had to be created. The growth in carbon-relay in persistent hashing mode is consistent and grows linearly. For instance, on about 280.000 metrics per minute, carbon-relay in hashing mode will grow up to 10 gigs in memory in 6 hours. Restarting the relay purges the memory back. We have that behavior on all relays using hashing. Our setup is all 0.9.10 though so going to test that theory with 0.9.12 that i grabbed today.

frlinux avatar Dec 03 '13 20:12 frlinux

I'm using 0.9.12 + a few patches (none of which should have anything to do with memory consumption). Hopefully your upgrade helps.

jamesjuran avatar Dec 03 '13 20:12 jamesjuran

Thanks, will report whence I am done. Hoping to test this tomorrow. I just need to look at the new way to configure the different carbons.

frlinux avatar Dec 03 '13 20:12 frlinux

graphite_0912_memoryleak

Including a graph of my latest test, pushing 500k metrics per minute. First graph to the left is carbon cache memusage, middle one is the relay memusage, as you can see consistently growing and last is the number of metrics pushed.

Over an hour, carbon-relay is consistently hogging memory in hashing mode. Here is my configuration for a given cache and the relay that talks to all caches:

[cache:1] USER = apache LINE_RECEIVER_INTERFACE = 0.0.0.0 LINE_RECEIVER_PORT = 2003 PICKLE_RECEIVER_INTERFACE = 0.0.0.0 PICKLE_RECEIVER_PORT = 2004 CACHE_QUERY_INTERFACE = 0.0.0.0 CACHE_QUERY_PORT = 7201 MAX_CACHE_SIZE = inf MAX_UPDATES_PER_SECOND = inf WHISPER_AUTOFLUSH = True LOG_UPDATES = False

[relay:1] RELAY_METHOD = consistent-hashing LINE_RECEIVER_INTERFACE = 0.0.0.0 LINE_RECEIVER_PORT = 2013 PICKLE_RECEIVER_INTERFACE = 0.0.0.0 PICKLE_RECEIVER_PORT = 2014 MAX_QUEUE_SIZE = 10000 MAX_DATAPOINTS_PER_MESSAGE = 500 USE_FLOW_CONTROL = True DESTINATIONS = localhost:2004:1,localhost:2104:2,localhost:2204:3,localhost:2304:4,localhost:2404:5,localhost:2504:6,localhost:2604:7,localhost:2704:8

frlinux avatar Dec 04 '13 17:12 frlinux

And if you switch to rule based relay and just configure the single cache what do your graphs look like?

drawks avatar Dec 04 '13 17:12 drawks

@drawks we have that in production, rule based does not leak, only relay in hashing mode. It has been there for a while, 0.9.10 and today tested 0.9.12 latest pull from github to see if that fixed it. Both code base have that issue.

frlinux avatar Dec 04 '13 17:12 frlinux

are your tests running against a system without any metrics on it yet? If you are blocking on file creates it'll balloon up a bit. I'm not seeing any unbounded growth using CH or aggregation aware CH here. FWIW

drawks avatar Dec 04 '13 17:12 drawks

So yes, all metrics were created on the spot but this is on RAID SSD, took only a few minutes to create them all. From thereon, each minute sees an update to the 500k metrics, not a new metric creation. As you can see on my config above, there is no throttling of any sort.

frlinux avatar Dec 04 '13 17:12 frlinux

What version of python are you on? I'm similarly on fast SSD without any throttling configured. I haven't seen evidence of a memory leak, but I have seen problems with a large number of connections does seem to prevent the relay/aggregator from draining. What does the

carbon.relays.*.destinations.*.sent

graph look like?

drawks avatar Dec 04 '13 17:12 drawks

Here is a graph of the different relays_destinations_

carbon_relay_destinations

python2.7 CentOS 6.4 across the board.

frlinux avatar Dec 04 '13 17:12 frlinux

So does the sum of sent track the number received? I'm confused as to what your 4 graphs are. 0.9.x should only have "sent", "queueUntilReady", "fullQueueDrops" and "attemptedRelays".

drawks avatar Dec 04 '13 18:12 drawks