Skip to content
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

Fix value of system_config.workers at run_configure #4066

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/fluent/plugin/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ def fluentd_worker_id
end

def configure(conf)
if Fluent::Engine.supervisor_mode || (conf.respond_to?(:for_this_worker?) && conf.for_this_worker?)
workers = if conf.target_worker_ids && !conf.target_worker_ids.empty?
conf.target_worker_ids.size
else
1
end
system_config_override(workers: workers)
raise ArgumentError, "BUG: type of conf must be Fluent::Config::Element, but #{conf.class} is passed." unless conf.is_a?(Fluent::Config::Element)

if (Fluent::Engine.supervisor_mode && !conf.target_worker_ids.empty?) || conf.for_this_worker?
ashie marked this conversation as resolved.
Show resolved Hide resolved
system_config_override(workers: conf.target_worker_ids.size)
end

super(conf, system_config.strict_config_value)
@_state ||= State.new(false, false, false, false, false, false, false, false, false)
@_state.configure = true
Expand Down
130 changes: 130 additions & 0 deletions test/plugin/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,134 @@ class FluentPluginBaseTest::DummyPlugin2 < Fluent::Plugin::TestBase
end
end
end

test '`ArgumentError` when `conf` is not `Fluent::Config::Element`' do
assert_raise ArgumentError.new('BUG: type of conf must be Fluent::Config::Element, but Hash is passed.') do
@p.configure({})
end
end

sub_test_case 'system_config.workers value after configure' do
sub_test_case 'with <system> workers 3 </system>' do
setup do
system_config = Fluent::SystemConfig.new
system_config.workers = 3
stub(Fluent::Engine).system_config { system_config }
end

sub_test_case 'supervisor_mode is true' do
setup do
stub(Fluent::Engine).supervisor_mode { true }
stub(Fluent::Engine).worker_id { -1 }
end

test 'without <worker> directive' do
conf = config_element()
conf.set_target_worker_ids([])
@p.configure(conf)
assert{ @p.system_config.workers == 3 }
end
ashie marked this conversation as resolved.
Show resolved Hide resolved

test 'with <worker 0>' do
conf = config_element()
conf.set_target_worker_ids([0])
@p.configure(conf)
assert{ @p.system_config.workers == 1 }
end

test 'with <worker 0-1>' do
conf = config_element()
conf.set_target_worker_ids([0, 1])
@p.configure(conf)
assert{ @p.system_config.workers == 2 }
end

test 'with <worker 0-2>' do
conf = config_element()
conf.set_target_worker_ids([0, 1, 2])
@p.configure(conf)
assert{ @p.system_config.workers == 3 }
end
end

sub_test_case 'supervisor_mode is false' do
setup do
stub(Fluent::Engine).supervisor_mode { false }
stub(Fluent::Engine).worker_id { 0 }
end

test 'without <worker> directive' do
conf = config_element()
conf.set_target_worker_ids([])
@p.configure(conf)
assert{ @p.system_config.workers == 3 }
end

test 'with <worker 0>' do
conf = config_element()
conf.set_target_worker_ids([0])
@p.configure(conf)
assert{ @p.system_config.workers == 1 }
end

test 'with <worker 0-1>' do
conf = config_element()
conf.set_target_worker_ids([0, 1])
@p.configure(conf)
assert{ @p.system_config.workers == 2 }
end

test 'with <worker 0-2>' do
conf = config_element()
conf.set_target_worker_ids([0, 1, 2])
@p.configure(conf)
assert{ @p.system_config.workers == 3 }
end
end
end

sub_test_case 'without <system> directive' do
sub_test_case 'supervisor_mode is true' do
setup do
stub(Fluent::Engine).supervisor_mode { true }
stub(Fluent::Engine).worker_id { -1 }
end

test 'without <worker> directive' do
conf = config_element()
conf.set_target_worker_ids([])
@p.configure(conf)
assert{ @p.system_config.workers == 1 }
end

test 'with <worker 0>' do
conf = config_element()
conf.set_target_worker_ids([0])
@p.configure(conf)
assert{ @p.system_config.workers == 1 }
end
end

sub_test_case 'supervisor_mode is false' do
setup do
stub(Fluent::Engine).supervisor_mode { false }
stub(Fluent::Engine).worker_id { 0 }
end

test 'without <worker> directive' do
conf = config_element()
conf.set_target_worker_ids([])
@p.configure(conf)
assert{ @p.system_config.workers == 1 }
end

test 'with <worker 0>' do
conf = config_element()
conf.set_target_worker_ids([0])
@p.configure(conf)
assert{ @p.system_config.workers == 1 }
end
end
end
end
end