Skip to content

Commit

Permalink
use closure table instead of ivars
Browse files Browse the repository at this point in the history
  • Loading branch information
colinsurprenant authored and jsvd committed May 17, 2016
1 parent c8e8bc4 commit 58bc205
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
28 changes: 15 additions & 13 deletions logstash-core/lib/logstash/config/config_ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def compile
@outputs = []
@periodic_flushers = []
@shutdown_flushers = []
@generated_objects = {}
CODE

sections = recursive_select(LogStash::Config::AST::PluginSection)
Expand Down Expand Up @@ -150,31 +151,31 @@ def compile_initializer


code << <<-CODE
#{name} = #{plugin.compile_initializer}
@#{plugin.plugin_type}s << #{name}
@generated_objects[:#{name}] = #{plugin.compile_initializer}
@#{plugin.plugin_type}s << @generated_objects[:#{name}]
CODE

# The flush method for this filter.
if plugin.plugin_type == "filter"

code << <<-CODE
#{name}_flush = lambda do |options, &block|
@logger.debug? && @logger.debug(\"Flushing\", :plugin => #{name})
@generated_objects[:#{name}_flush] = lambda do |options, &block|
@logger.debug? && @logger.debug(\"Flushing\", :plugin => @generated_objects[:#{name}])
events = #{name}.flush(options)
events = @generated_objects[:#{name}].flush(options)
return if events.nil? || events.empty?
@logger.debug? && @logger.debug(\"Flushing\", :plugin => #{name}, :events => events)
@logger.debug? && @logger.debug(\"Flushing\", :plugin => @generated_objects[:#{name}], :events => events)
#{plugin.compile_starting_here.gsub(/^/, " ")}
events.each{|e| block.call(e)}
end
if #{name}.respond_to?(:flush)
@periodic_flushers << #{name}_flush if #{name}.periodic_flush
@shutdown_flushers << #{name}_flush
if @generated_objects[:#{name}].respond_to?(:flush)
@periodic_flushers << @generated_objects[:#{name}_flush] if @generated_objects[:#{name}].periodic_flush
@shutdown_flushers << @generated_objects[:#{name}_flush]
end
CODE

Expand All @@ -197,7 +198,8 @@ def generate_variables
# Unique number for every plugin.
@i += 1
# store things as ivars, like @filter_grok_3
var = "@#{plugin.plugin_type}_#{plugin.plugin_name}_#{@i}"
var = :"#{plugin.plugin_type}_#{plugin.plugin_name}_#{@i}"
# puts("var=#{var.inspect}")
@variables[plugin] = var
end
return @variables
Expand Down Expand Up @@ -239,13 +241,13 @@ def compile_initializer
def compile
case plugin_type
when "input"
return "start_input(#{variable_name})"
return "start_input(@generated_objects[:#{variable_name}])"
when "filter"
return <<-CODE
events = #{variable_name}.multi_filter(events)
events = @generated_objects[:#{variable_name}].multi_filter(events)
CODE
when "output"
return "targeted_outputs << #{variable_name}\n"
return "targeted_outputs << @generated_objects[:#{variable_name}]\n"
when "codec"
settings = attributes.recursive_select(Attribute).collect(&:compile).reject(&:empty?)
attributes_code = "LogStash::Util.hash_merge_many(#{settings.map { |c| "{ #{c} }" }.join(", ")})"
Expand Down
16 changes: 16 additions & 0 deletions logstash-core/spec/logstash/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,20 @@ class TestPipeline < LogStash::Pipeline
end
end
end

context "Pipeline object" do
before do
allow(LogStash::Plugin).to receive(:lookup).with("input", "generator").and_return(LogStash::Inputs::Generator)
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)
end

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

it "should not add ivars" do
expect(pipeline1.instance_variables).to eq(pipeline2.instance_variables)
end
end
end

0 comments on commit 58bc205

Please sign in to comment.