Skip to content

Commit

Permalink
Replace def #{type}_func with define_singleton_method
Browse files Browse the repository at this point in the history
When you run multiples pipeline and the code get evaluated the
class cache will be clear and the last evaluated code will be called
everything. The `filter_func` and the `output_func` need to be unique
for every instance of the pipeline.

This PR is based on #4254
  • Loading branch information
ph committed Dec 10, 2015
1 parent 39dbad8 commit 8c5393f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion logstash-core/lib/logstash/config/config_ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ def compile
["filter", "output"].each do |type|
# defines @filter_func and @output_func

definitions << "def #{type}_func(event)"
# This need to be defined as a singleton method
# so each instance of the pipeline has his own implementation
# of the output/filter function
definitions << "define_singleton_method :#{type}_func do |event|"
definitions << " targeted_outputs = []" if type == "output"
definitions << " events = [event]" if type == "filter"
definitions << " @logger.debug? && @logger.debug(\"#{type} received\", :event => event.to_hash)"
Expand Down
43 changes: 43 additions & 0 deletions logstash-core/spec/logstash/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ def close
end
end

class DummyInputGenerator < LogStash::Inputs::Base
config_name "dummyinputgenerator"
milestone 2

def register
end

def run(queue)
queue << Logstash::Event.new while !stop?
end

def close
end
end

class DummyCodec < LogStash::Codecs::Base
config_name "dummycodec"
milestone 2
Expand Down Expand Up @@ -47,6 +62,10 @@ def initialize(params={})

def register
end

def threadsafe?
false
end

def receive(event)
@events << event
Expand All @@ -57,6 +76,10 @@ def close
end
end

class DummyOutputMore < DummyOutput
config_name "dummyoutputmore"
end

class DummyFilter < LogStash::Filters::Base
config_name "dummyfilter"
milestone 2
Expand Down Expand Up @@ -362,4 +385,24 @@ class TestPipeline < LogStash::Pipeline
pipeline.shutdown
end
end

context "Multiples pipelines" do
before do
allow(LogStash::Plugin).to receive(:lookup).with("input", "dummyinputgenerator").and_return(DummyInputGenerator)
allow(LogStash::Plugin).to receive(:lookup).with("codec", "plain").and_return(DummyCodec)
allow(LogStash::Plugin).to receive(:lookup).with("filter", "dummyfilter").and_return(DummyFilter)
allow(LogStash::Plugin).to receive(:lookup).with("output", "dummyoutput").and_return(DummyOutput)
allow(LogStash::Plugin).to receive(:lookup).with("output", "dummyoutputmore").and_return(DummyOutputMore)
end

let(:pipeline1) { LogStash::Pipeline.new("input { dummyinputgenerator {} } filter { dummyfilter {} } output { dummyoutput {}}") }
let(:pipeline2) { LogStash::Pipeline.new("input { dummyinputgenerator {} } filter { dummyfilter {} } output { dummyoutputmore {}}") }

it "should handle evaluating different config" do
expect(pipeline1.output_func(LogStash::Event.new)).not_to include(nil)
expect(pipeline1.filter_func(LogStash::Event.new)).not_to include(nil)
expect(pipeline2.output_func(LogStash::Event.new)).not_to include(nil)
expect(pipeline1.filter_func(LogStash::Event.new)).not_to include(nil)
end
end
end

0 comments on commit 8c5393f

Please sign in to comment.