logstash icon indicating copy to clipboard operation
logstash copied to clipboard

Duplicate events on service restart with file input

Open brianlow opened this issue 12 years ago • 11 comments

Symptom: logstash re-reads files when the logstash service is restarted. Ubuntu, logstash 1.4.2, installed with apt-get.

Cause: logstash does not trap SIGTERM sent by upstart so File Input does not get a chance to flush any sincedb changes to disk.

To reproduce:

  1. create 5 files with 1 message each
  2. sudo service logstash start
  3. wait for logstash to process the files
  4. sudo service logstash restart
  5. wait for logstash to process the files expected: after the restart logstash will not output any events actual: logstash outputs 4 duplicate events

Here is a vagrantfile to reproduce: https://github.com/brianlow/logstash-dupe

A mailing list discussion: https://groups.google.com/forum/#!topic/logstash-users/wrwx99ar2qo

brianlow avatar Aug 29 '14 21:08 brianlow

confirmed by running the test case. @brianlow awesome bug report! :+1:

ph avatar Jan 30 '15 14:01 ph

Correct me if I am wrong but this issue still exists, at least on the portable version of logstash 1.5.

Conditions I used:

  • Created 15 files with 50000 messages each
  • Using the following config:
input{
    file{
        path => "/$HOME/input*.txt"
        start_position => "beginning"
    }
}
output{
    file{
        path => "/$HOME/someoutput.txt"
    }
}
  • Start logstash and let it run for about 30 seconds, send a shutdown signal, wait for it to shutdown
  • Start logstash again, and let it run until it finishes reading the file/processing

You'll end up with more than 750000 events. You can use this config to verify:

input{
    file{
        path => "/$HOME/someoutput.txt"
        start_position => "beginning"
    }
}
filter{
    metrics{
        meter => ["something"]
        add_tag => "m"
        flush_interval => 10
        rates => []
    }
}
output{
    if "m" in [tags] {
        stdout{
            codec => "rubydebug"
        }
    }
}

There is also an issue where the first message after the restart gets cut off. My guess at the issue is because the offset in which the file input saves is by byte or by some other measure rather than message.

kelvinfann-zz avatar Jun 24 '15 21:06 kelvinfann-zz

Trying to replicate your issue but the first try worked correctly

joaoduarte@Joaos-MacBook-Pro-2 /tmp/logstash-1.5.1 % cat config
input{
    file{
        path => "/tmp/logs/input*.txt"
        start_position => "beginning"
    }
}
output{
    file{
        path => "/tmp/logs/someoutput.txt"
    }
}
joaoduarte@Joaos-MacBook-Pro-2 /tmp/logstash-1.5.1 % wc -l /tmp/logs/input* 
   50000 /tmp/logs/input1.txt
   50000 /tmp/logs/input10.txt
   50000 /tmp/logs/input2.txt
   50000 /tmp/logs/input3.txt
   50000 /tmp/logs/input4.txt
   50000 /tmp/logs/input5.txt
   50000 /tmp/logs/input6.txt
   50000 /tmp/logs/input7.txt
   50000 /tmp/logs/input8.txt
   50000 /tmp/logs/input9.txt
  500000 total
joaoduarte@Joaos-MacBook-Pro-2 /tmp/logstash-1.5.1 % bin/logstash -f config
Logstash startup completed
^CSIGINT received. Shutting down the pipeline. {:level=>:warn}
Logstash shutdown completed
joaoduarte@Joaos-MacBook-Pro-2 /tmp/logstash-1.5.1 % wc -l /tmp/logs/someoutput.txt
  346290 /tmp/logs/someoutput.txt
joaoduarte@Joaos-MacBook-Pro-2 /tmp/logstash-1.5.1 % bin/logstash -f config
Logstash startup completed
^CSIGINT received. Shutting down the pipeline. {:level=>:warn}
Logstash shutdown completed
joaoduarte@Joaos-MacBook-Pro-2 /tmp/logstash-1.5.1 % wc -l /tmp/logs/someoutput.txt
  500000 /tmp/logs/someoutput.txt

jsvd avatar Jun 25 '15 11:06 jsvd

@kelvinfann can you show me an example of the log files you generated?

jsvd avatar Jun 25 '15 11:06 jsvd

Hmmm, that is odd. Uhhh the logs I am using are just numbers separated by a '\n' character generated from a python script. Each file I made has a different random number repeated 50000 times. eg.

14887
14887
14887
14887
...

The output log looks like this

{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.955Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.956Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.956Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.956Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.957Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}

...

{"message":"26035","@version":"1","@timestamp":"2015-06-25T22:46:39.208Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/9.txt"}
{"message":"26035","@version":"1","@timestamp":"2015-06-25T22:46:39.208Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/9.txt"}
{"message":"26035","@version":"1","@timestamp":"2015-06-25T22:46:39.208Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/9.txt"}
{"message":"26035","@version":"1","@timestamp":"2015-06-25T22:46:39.208Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/9.txt"}
{"message":"26035","@version":"1","@timestamp":"2015-06-25T22:46:39.208Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/9.txt"}

I downloaded log stash with curl from the following

curl -O https://download.elastic.co/logstash/logstash/logstash-1.5.0.tar.gz

I then populate 15 sample logs with the following python script

import random as r
def generate_random_file(filename, rep):
    f = open(filename, 'a')
    try:
        for _ in xrange(rep):
            f.write(str(int(r.random()*10000)) + '\n')
    finally:
        f.close()

I don't think the problem is with my log files though, since if I do not shutdown the log stash midrun it will get the correct output of 750000 messages

kelvinfann-zz avatar Jun 25 '15 22:06 kelvinfann-zz

@jsvd Here is a transcript of what I did

localhost:logstash_test_1906 kelvin.fann$ wc input_files/*
   50000   50000  300000 input_files/1.txt
   50000   50000  250000 input_files/10.txt
   50000   50000  300000 input_files/11.txt
   50000   50000  250000 input_files/12.txt
   50000   50000  300000 input_files/13.txt
   50000   50000  250000 input_files/14.txt
   50000   50000  300000 input_files/15.txt
   50000   50000  300000 input_files/2.txt
   50000   50000  300000 input_files/3.txt
   50000   50000  300000 input_files/4.txt
   50000   50000  250000 input_files/5.txt
   50000   50000  300000 input_files/6.txt
   50000   50000  300000 input_files/7.txt
   50000   50000  250000 input_files/8.txt
   50000   50000  300000 input_files/9.txt
  750000  750000 4250000 total
localhost:logstash_test_1906 kelvin.fann$ wc output_files/out.log
       0       0       0 output_files/out.log
localhost:logstash_test_1906 kelvin.fann$ cat logstash/config
input{ file{ path => "/Users/kelvin.fann/Git/logstash_test_1906/input_files/*" start_position => "beginning" } } output{ file{ path => "/Users/kelvin.fann/Git/logstash_test_1906/output_files/out.log" } }
localhost:logstash_test_1906 kelvin.fann$ logstash/bin/logstash -f logstash/config
fatal: Not a git repository (or any of the parent directories): .git
fatal: Not a git repository (or any of the parent directories): .git
Logstash startup completed
^CSIGINT received. Shutting down the pipeline. {:level=>:warn}
Logstash shutdown completed
localhost:logstash_test_1906 kelvin.fann$ wc output_files/out.log
  339303  339303 55445692 output_files/out.log
localhost:logstash_test_1906 kelvin.fann$ logstash/bin/logstash -f logstash/config
fatal: Not a git repository (or any of the parent directories): .git
fatal: Not a git repository (or any of the parent directories): .git
Logstash startup completed
^CSIGINT received. Shutting down the pipeline. {:level=>:warn}
Logstash shutdown completed
localhost:logstash_test_1906 kelvin.fann$ wc output_files/out.log
  751074  751074 122476134 output_files/out.log
localhost:logstash_test_1906 kelvin.fann$ head -5 output_files/out.log
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.955Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.956Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.956Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.956Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
{"message":"14887","@version":"1","@timestamp":"2015-06-25T22:45:25.957Z","host":"localhost","path":"/Users/kelvin.fann/Git/logstash_test_1906/input_files/1.txt"}
localhost:logstash_test_1906 kelvin.fann$ head -5 input_files/1.txt
14887
14887
14887
14887
14887

kelvinfann-zz avatar Jun 25 '15 22:06 kelvinfann-zz

Here I wrote a script to show this issue. It uses TERM rather than INT but I am pretty sure that log stash handles them the same. https://github.com/kelvinfann/scripts/blob/master/file_bug.sh

kelvinfann-zz avatar Jun 26 '15 17:06 kelvinfann-zz

any update?

johnkors avatar Nov 01 '16 12:11 johnkors

This shouldn't even be in this repository any more, as the file input plugin now has its own repository. I do not think this issue remains, as that plugin has gone through several iterations since the date of the last entry, let alone the first. If it does remain, it is because of uncontrollable circumstances surrounding inode tracking. In such a case, it would be wiser to use filebeat (even locally), as it uses a different mechanism to track files. In fact, we have been considering deprecating the file input plugin at some point, in favor of using filebeat for this reason.

untergeek avatar Nov 01 '16 12:11 untergeek

Thx, @untergeek . We're having loads of (randomly occuring) duplicates using the file input of logstash & ElasticSearch output - so we will give filebeat a go instead.

johnkors avatar Nov 03 '16 10:11 johnkors

In such a case, it would be wiser to use filebeat (even locally), as it uses a different mechanism to track files.

We are using a small Ruby filter in our Shipper logstash configurations, which send the IP addresses of the machines, rather than Hostname. I think that Filebeat looses here, as we cannot automate some things in there.

ruby { init => 'require "socket"' code => 'event["host"] = IPSocket.getaddress(Socket.gethostname)' }

AnkurThakur avatar May 17 '17 11:05 AnkurThakur