Skip to content

move async finish_register to bottom of register to avoid race condition #1125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 20, 2023

Conversation

jsvd
Copy link
Member

@jsvd jsvd commented Apr 14, 2023

This PR fixes a race condition between the thread running register and the one running finish_register.

The issue can be observed by having 20-30 simple pipelines in pipelines.yml like:

input {
 java_generator { eps => 1.1 }
}
output {
  elasticsearch {
    user => elastic
    password => ".."
    data_stream => true
  }
- pipeline.id: test_0
  pipeline.workers: 1
  pipeline.batch.size: 1
  path.config: "/tmp/logstash-8.7.0/cfg"
ruby -ryaml -e '30.times.each {|i| puts "- pipeline.id: test_#{i}\n  pipeline.workers: 1\n  pipeline.batch.size: 1\n  path.config: \"/tmp/logstash-8.7.0/cfg\""}' > config/pipelines.yml

Run Logstash w/ pipeline reloading:

bin/logstash -r

Changing the pipeline config a few times and letting Logstash reload will eventually show one or more pipelines overwriting the index with "ecs-logstash":

[2023-04-20T12:08:33,181][WARN ][logstash.outputs.elasticsearch][test_0] Overwriting supplied index logs-generic-default with rollover alias ecs-logstash

Putting finish_register at the end of register ensures there are no concurrent instructions manipulating @index and other internal state variables.

fixes #1126

@jsvd jsvd marked this pull request as ready for review April 19, 2023 22:08
@jsvd jsvd force-pushed the reorder_register branch from f84f186 to b4a2c67 Compare April 20, 2023 10:06
@andsel andsel self-requested a review April 20, 2023 12:12
@jsvd jsvd requested a review from kaisecheng April 20, 2023 12:59
Copy link
Contributor

@kaisecheng kaisecheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@andsel andsel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

wasn't able to reproduce the original issue on local host, but the code changes seems wise.

@jsvd
Copy link
Member Author

jsvd commented Apr 20, 2023

For reliable reproduceability:

/tmp/test/logstash-8.7.0
> bin/logstash-plugin update logstash-output-elasticsearch
> vim ./vendor/bundle/jruby/2.6.0/gems/logstash-output-elasticsearch-11.15.0-java/lib/logstash/outputs/elasticsearch.rb
  1. Add sleep 1 before the if on line https://github.com/logstash-plugins/logstash-output-elasticsearch/blob/main/lib/logstash/outputs/elasticsearch.rb#L341
  2. Change line https://github.com/logstash-plugins/logstash-output-elasticsearch/blob/main/lib/logstash/outputs/elasticsearch.rb#L364 to (sleep 3; setup_ilm) if ilm_in_use?

@andsel
Copy link
Contributor

andsel commented Apr 20, 2023

@jsvd tested as you suggested and it was reproducible, confirming that the PR fixes it.

@jsvd
Copy link
Member Author

jsvd commented Apr 20, 2023

@logstashmachine bump patch

@jsvd jsvd merged commit 2bc99f6 into logstash-plugins:main Apr 20, 2023
@jsvd jsvd deleted the reorder_register branch April 20, 2023 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition between register and finish_register lands data in ecs_logstash instead of data stream
4 participants