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 to generate storage local path using usage #1438

Merged
merged 2 commits into from
Jan 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion lib/fluent/plugin/storage_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def configure(conf)
end
end
elsif root_dir = owner.plugin_root_dir
@path = File.join(root_dir, 'storage.json')
basename = (conf.arg && !conf.arg.empty?) ? "storage.#{conf.arg}.json" : "storage.json"
Copy link
Member

Choose a reason for hiding this comment

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

conf.arg can contain space like <storage foo bar>.
So need to validate or convert unexpected charactors.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I'll add validation for usage.

Copy link
Contributor

Choose a reason for hiding this comment

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

This conf.arg will be given by :usage in storage_create?
Or users should specify <storage args> by themselves?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I got it.
We should fill up usage when conf is Fluent::Config::Element's instance case: https://github.com/fluent/fluentd/blob/master/lib/fluent/plugin_helper/storage.rb#L56

Copy link
Contributor

Choose a reason for hiding this comment

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

This behavior is a limitations for now.

@path = File.join(root_dir, basename)
@multi_workers_available = true
else
if @persistent
Expand Down
8 changes: 7 additions & 1 deletion lib/fluent/plugin_helper/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def storage_create(usage: '', type: nil, conf: nil, default_type: nil)
if conf && !conf.arg.empty?
usage = conf.arg
end
if !usage.empty? && usage !~ /^[a-zA-Z][-_.a-zA-Z0-9]*$/
raise Fluent::ConfigError, "Argument in <storage ARG> uses invalid characters: '#{usage}'"
end

s = @_storages[usage]
if s && s.running
Expand All @@ -59,7 +62,7 @@ def storage_create(usage: '', type: nil, conf: nil, default_type: nil)
conf = Hash[conf.map{|k,v| [k.to_s, v]}]
Fluent::Config::Element.new('storage', usage, conf, [])
when nil
Fluent::Config::Element.new('storage', usage, {}, [])
Fluent::Config::Element.new('storage', usage, {'@type' => type}, [])
else
raise ArgumentError, "BUG: conf must be a Element, Hash (or unspecified), but '#{conf.class}'"
end
Expand Down Expand Up @@ -98,6 +101,9 @@ def configure(conf)
super

@storage_configs.each do |section|
if !section.usage.empty? && section.usage !~ /^[a-zA-Z][-_.a-zA-Z0-9]*$/
raise Fluent::ConfigError, "Argument in <storage ARG> uses invalid characters: '#{section.usage}'"
end
if @_storages[section.usage]
raise Fluent::ConfigError, "duplicated storages configured: #{section.usage}"
end
Expand Down
14 changes: 14 additions & 0 deletions test/plugin/test_storage_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ class MyInput < Fluent::Plugin::Input
assert_equal '2', @p.get('key1')
assert_equal 4, @p.get('key2')
end

test 'works with customized path by specified usage' do
root_dir = File.join(TMP_DIR, 'root')
expected_storage_path = File.join(root_dir, 'worker0', 'local_storage_test', 'storage.usage.json')
conf = config_element('ROOT', 'usage', {'@id' => 'local_storage_test'})
Fluent::SystemConfig.overwrite_system_config('root_dir' => root_dir) do
@d.configure(conf)
end
@d.start
@p = @d.storage_create(usage: 'usage', type: 'local')

assert_equal expected_storage_path, @p.path
assert @p.store.empty?
end
end

sub_test_case 'configured with root-dir and plugin id, and multi workers' do
Expand Down
11 changes: 11 additions & 0 deletions test/plugin_helper/test_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ class Dummy2 < Fluent::Plugin::TestBase
end
end

test 'raises config error if config argument has invalid characters' do
d = Dummy.new
assert_raise Fluent::ConfigError.new("Argument in <storage ARG> uses invalid characters: 'yaa y'") do
d.configure(config_element('root', '', {}, [config_element('storage', 'yaa y', {'@type' => 'local'})]))
end
d.configure(config_element())
assert_raise Fluent::ConfigError.new("Argument in <storage ARG> uses invalid characters: 'a,b'") do
d.storage_create(usage: 'a,b', type: 'local')
end
end

test 'can be configured without storage sections' do
d = Dummy.new
assert_nothing_raised do
Expand Down