vector icon indicating copy to clipboard operation
vector copied to clipboard

Memory leak in 0.8.2

Open tarabanton opened this issue 6 years ago • 23 comments

Good time of the day. We are using vector 0.8.2 (v0.8.2 x86_64-unknown-linux-musl 2020-03-06)

Simplified flow: source.log -> transform.regex -> transform.log_to_metric -> sink.prometheus

There is some annoying memory leak somewhere. Over the time vector (week or two) can consume up to tens of gigabytes of RAM. We have 10 hosts with identical configuration files. Memory consumption increase correlates with amount of data in logs files.

Memory consumption graph

image

Configuration file
data_dir = "/var/lib/vector"

# Debug only
#[sources.stdin]
#  type = "stdin"
#[sinks.stdout]
#  inputs   = []
#  type     = "console"
#  encoding = "json"


# Input data
[sources.nginx_log]
  # General
  type = "file"
  include = ["/var/log/nginx/monitoring.log"]
  ignore_older = 3600 # optional, no default, seconds
  start_at_beginning = false # optional, default
  # Priority
  oldest_first = false # optional, default
  # Fingerprinting, how the file source should identify files and changes
  fingerprinting.strategy = "device_and_inode"
  # An approximate limit on the amount of data read from a single file at a given time
  max_read_bytes = 102400

[transforms.nginx_parsed_log]
  # General
  type = "regex_parser" # required
  inputs = ["nginx_log"] # required
  # Classic access_log
  #regex = '^(?P<client_ip>[\w+\.:]+) - [-\w]+ \[(?P<timestamp>.*)\] "(.*?)" (?P<code>\d+) (?P<bytes>\d+).*'
  # Monitoring format - remote_addr [time_local] status body_bytes_sent request_time request_length host
  regex =  '^(?P<client_ip>[\w+\.:]+) \[(?P<timestamp>.*)\] (?P<code>\d+) (?P<bytes>\d+) (?P<request_time>[\d\.]+) (?P<request_length>\d+) (?P<host>.*)'
  drop_field = true # optional, default
  field = "message" # optional, default
  # Types
  #types.client_ip = "string"
  #types.timestamp = "timestamp|%d/%b/%Y:%T %z"
  types.code = "int"
  #types.bytes = "int"
  types.request_time = "float"
  #types.request_length = "int"
  #types.host = "string"


[transforms.nginx_parsed_log_to_metric]
  inputs = ["nginx_parsed_log"]
  type = "log_to_metric"

  [[transforms.nginx_parsed_log_to_metric.metrics]]
    type = "counter"
    field = "code"
    name = "http_requests_total"
    tags.code = "{{ code }}"
    #tags.host = "{{ host }}"

  [[transforms.nginx_parsed_log_to_metric.metrics]]
    type = "histogram"
    field = "request_time"
    name = "http_request_duration_seconds"
    #tags.host = "{{ host }}"

# Output data
[sinks.prometheus]
  type = "prometheus" # required
  inputs = ["nginx_parsed_log_to_metric"] # required
  address = "0.0.0.0:9598" # required
  namespace = "" # required
  flush_period_secs = 15 # optional, default, seconds

tarabanton avatar Apr 17 '20 10:04 tarabanton

Hi @tarabanton, thanks for reporting. We'll prioritize this and take a look. Nothing jumps out in your configuration file that should cause this. Apologize for the bad experience, we'll get it resolved!

binarylogic avatar Apr 18 '20 14:04 binarylogic

Thanks for this report, especially the included config and grafana configs! :) I'll take a look into this in the next few days.

Hoverbear avatar Apr 19 '20 14:04 Hoverbear

So I haven't found anything immediately obvious related to processing memory leaks in small files yet. I'm going to build up a big log file and have a try. In the meantime you can use a tool like logrotate to keep these logfiles split up.

Hoverbear avatar Apr 27 '20 18:04 Hoverbear

In the meantime you can use a tool like logrotate to keep these logfiles split up.

Thanks for the heads up. We are rotating them on daily basis. Could I help with some profiling or verbose logging?

tarabanton avatar Apr 27 '20 19:04 tarabanton

@tarabanton I'm trying to collect logs using https://github.com/koute/memory-profiler or https://github.com/KDE/heaptrack.

So far I've tested up to 1.5gb log files and not seen major issues on Ubuntu 19.10 with EXT4 virtualized.

Hoverbear avatar Apr 27 '20 19:04 Hoverbear

Here are some examples of a heaptrack running inside an Ubuntu WSL2 VM with a virtualized EXT4 disk. The log file is 2.4gb at the end of the run, started at 1.5gb. I used 4 workers sending queries to sequential query strings:

hoverbear@OBSIDIAN:~/CLionProjects/vector$ ls -lah /var/log/nginx/
total 2.4G
drwxr-xr-x  2 root     adm    4.0K Apr 27 10:33 .
drwxrwxr-x 15 root     syslog 4.0K Apr 27 10:33 ..
-rw-r-----  1 www-data adm    2.4G Apr 27 12:07 access.log
-rw-r-----  1 www-data adm       0 Apr 27 10:33 error.log
hoverbear@OBSIDIAN:~/CLionProjects/vector$ tail -n 5 /var/log/nginx/*
==> /var/log/nginx/access.log <==
127.0.0.1 - - [27/Apr/2020:12:09:19 -0700] "GET /?2974590 HTTP/1.1" 200 612 "-" "curl/7.65.3"
127.0.0.1 - - [27/Apr/2020:12:09:19 -0700] "GET /?2637373 HTTP/1.1" 200 612 "-" "curl/7.65.3"
127.0.0.1 - - [27/Apr/2020:12:09:19 -0700] "GET /?9955419 HTTP/1.1" 200 612 "-" "curl/7.65.3"
127.0.0.1 - - [27/Apr/2020:12:09:19 -0700] "GET /?2673453 HTTP/1.1" 200 612 "-" "curl/7.65.3"
127.0.0.1 - - [27/Apr/2020:12:09:19 -0700] "GET /?2660382 HTTP/1.1" 200 612 "-" "curl/7.65.3"

==> /var/log/nginx/error.log <==
hoverbear@OBSIDIAN:~/CLionProjects/vector$ 

This shows only 72 kb of tracked leaks, which is unavoidable stuff for us like C++ and openssl.

image

I do see a slowly increasing amount of memory consumed though:

image

Hoverbear avatar Apr 27 '20 19:04 Hoverbear

@tarabanton Can you confirm that restarting Vector at least temporarily resolves the issue? Did rotating the logs help mitigate the issue?

Hoverbear avatar Apr 27 '20 19:04 Hoverbear

@tarabanton Can you confirm that restarting Vector at least temporarily resolves the issue? Did rotating the logs help mitigate the issue?

We always used logs rotation. Restarting helps as anticipated, so memory leak is somewhere in the process heap.

Will try updating to 0.9.0 on some hosts.

How can i attach https://github.com/koute/memory-profiler to vector? It seems like LD_PRELOAD is ignored

Example of "leaking" process below

# cat /proc/19629/maps
00200000-0066e000 r--p 00000000 08:03 760878                             /usr/bin/vector
0066e000-01c81000 r-xp 0046e000 08:03 760878                             /usr/bin/vector
01c81000-01d9a000 rw-p 01a81000 08:03 760878                             /usr/bin/vector
01d9a000-01df8000 rw-p 00000000 00:00 0
02d05000-02d06000 rw-p 00000000 00:00 0                                  [heap]
7ff0fe000000-7ff1eec00000 rw-p 00000000 00:00 0
7ff1eec80000-7ff265400000 rw-p 00000000 00:00 0
7ff265580000-7ff276a00000 rw-p 00000000 00:00 0
7ff276bf2000-7ff276bf4000 ---p 00000000 00:00 0
7ff276bf4000-7ff276df9000 rw-p 00000000 00:00 0
7ff276df9000-7ff276dfb000 ---p 00000000 00:00 0
7ff276dfb000-7ff277000000 rw-p 00000000 00:00 0
7ff277000000-7ff277400000 rw-p 00000000 00:00 0
7ff2775fa000-7ff2779fa000 rw-p 00000000 00:00 0
7ff277a00000-7ff277c00000 rw-p 00000000 00:00 0
7ff277df9000-7ff277dfb000 ---p 00000000 00:00 0
7ff277dfb000-7ff278000000 rw-p 00000000 00:00 0
7ff278000000-7ff278200000 rw-p 00000000 00:00 0
7ff2783f2000-7ff2783f4000 ---p 00000000 00:00 0
7ff2783f4000-7ff2785f9000 rw-p 00000000 00:00 0
7ff2785f9000-7ff2785fb000 ---p 00000000 00:00 0
7ff2785fb000-7ff278800000 rw-p 00000000 00:00 0
7ff278800000-7ff278c00000 rw-p 00000000 00:00 0
7ff278cf9000-7ff278cfb000 ---p 00000000 00:00 0
7ff278cfb000-7ff278f00000 rw-p 00000000 00:00 0
7ff278f00000-7ff279c00000 rw-p 00000000 00:00 0
7ff279df9000-7ff279dfb000 ---p 00000000 00:00 0
7ff279dfb000-7ff27a000000 rw-p 00000000 00:00 0
7ff27a000000-7ff27a400000 rw-p 00000000 00:00 0
7ff27a40e000-7ff27a41a000 rw-p 00000000 00:00 0
7ff27a41a000-7ff27a41b000 r--s 00000000 08:03 100709908                  /usr/share/zoneinfo/Europe/Moscow
7ff27a41b000-7ff27a424000 rw-p 00000000 00:00 0
7ffd206b2000-7ffd206d3000 rw-p 00000000 00:00 0                          [stack]
7ffd2073c000-7ffd2073e000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

tarabanton avatar Apr 28 '20 17:04 tarabanton

Can you give me an idea of the size and number of the log files we're talking about? Is vector dealing with gigabytes? Terabytes?

I'm not sure how well the profiler will work in release binaries, I can cook you a dev binary if you'd like?

Hoverbear avatar Apr 29 '20 14:04 Hoverbear

(Sort of some personal notes to avoid extra investigation later)

So one suspicion I had was that the file source isn't letting go of checkpoints/fingerprint data for rotated log files that have been deleted.

I noticed we track things like renames and opens, but not if any watched files are deleted:

https://github.com/timberio/vector/blob/90af1003d3844f86ff756b46161d75e456559330/lib/file-source/src/file_server.rs#L148-L219

But, that's a-okay because later we do this:

https://github.com/timberio/vector/blob/90af1003d3844f86ff756b46161d75e456559330/lib/file-source/src/file_server.rs#L264-L266

Here's where fp_map is built up:

https://github.com/timberio/vector/blob/90af1003d3844f86ff756b46161d75e456559330/lib/file-source/src/file_server.rs#L111-L119

Hoverbear avatar Apr 29 '20 16:04 Hoverbear

@tarabanton Here's the Vector release binary of 0.8.2 with debug info on so the profiler should work: https://hoverbear.ams3.cdn.digitaloceanspaces.com/vector-0.8.2-release-with-debug

That should work with either that memory profiler or heaptrack. If you get dumps from either with evidence of leaking I'd really love it.

Otherwise I'm setting up a reproduction scenario to let it run for a few days. Any info you can give me about the frequency of your log rotation or their sizes would be great. :)

Hoverbear avatar Apr 29 '20 17:04 Hoverbear

Okay so I have one set up: image

Machine memory so far: image

Hoverbear avatar Apr 29 '20 18:04 Hoverbear

So far:

image

image

Memory looks reasonably stable:

image

Hoverbear avatar Apr 30 '20 04:04 Hoverbear

I've built release with debug enabled for my environment.

Data was collected for 8 hours. I don't see anything suspicious in memory profiling, but memory consumption for process (RES in top) rose from 24 megabytes to 135. Heaptrack Raw memory-profiler dat

Will try to gather info with 1 thread only, with "-t 1" added to arguments

# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
# uname -a
Linux 3.10.0-1062.18.1.el7.x86_64 #1 SMP Tue Mar 17 23:49:17 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# ldd vector-0.8.2-debug
        linux-vdso.so.1 =>  (0x00007ffde9dca000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fe098f9a000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fe098c91000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fe098a8d000)
        librt.so.1 => /lib64/librt.so.1 (0x00007fe098885000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fe09866f000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fe0982a1000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fe09b845000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fe097f9f000)
# ldd --version
ldd (GNU libc) 2.17

tarabanton avatar Apr 30 '20 06:04 tarabanton

Can you give me an idea of the size and number of the log files we're talking about? Is vector dealing with gigabytes? Terabytes?

Hoverbear avatar Apr 30 '20 06:04 Hoverbear

Can you give me an idea of the size and number of the log files we're talking about? Is vector dealing with gigabytes? Terabytes?

# ls -lha /var/log/nginx/monitoring.log*
559M Apr 30 11:15 /var/log/nginx/monitoring.log
2.7G Apr 30 03:06 /var/log/nginx/monitoring.log.1
2.8G Apr 29 03:16 /var/log/nginx/monitoring.log.2
2.8G Apr 28 03:14 /var/log/nginx/monitoring.log.3
2.8G Apr 27 03:50 /var/log/nginx/monitoring.log.4
3.1G Apr 26 03:45 /var/log/nginx/monitoring.log.5
3.0G Apr 25 03:22 /var/log/nginx/monitoring.log.6
2.7G Apr 24 03:45 /var/log/nginx/monitoring.log.7
2.8G Apr 23 03:15 /var/log/nginx/monitoring.log.8
2.8G Apr 22 03:44 /var/log/nginx/monitoring.log.9
2.7G Apr 21 03:37 /var/log/nginx/monitoring.log.10

Around 40 million lines each.

tarabanton avatar Apr 30 '20 08:04 tarabanton

So I left my box running overnight

image

image

image

image

image

image

Hoverbear avatar Apr 30 '20 21:04 Hoverbear

Based on this, and what you've told me about your system, I can't seem to replicate this memory leak?

Hoverbear avatar Apr 30 '20 21:04 Hoverbear

image heaptrack.zip It seems that i CAN replicate it, but don't have any evidence in heaptrack data.

We're at dead end. Seems like vector will be a big NO for our production infrastructure. Any memory limits doesn't work, it just stops working.

tarabanton avatar May 02 '20 14:05 tarabanton

Hi @tarabanton, thanks for your effort in helping with this. I'm bummed we couldn't resolve it. @Hoverbear is going to run the same test on the environment specs you posted. It's likely something is going on there, and we'll update when we have more info this week.

binarylogic avatar May 04 '20 00:05 binarylogic