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

Make out_file append option work without compression #3579

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/fluent/env.rb
Expand Up @@ -33,4 +33,8 @@ def self.windows?
def self.linux?
/linux/ === RUBY_PLATFORM
end

def self.macos?
/darwin/ =~ RUBY_PLATFORM
end
end
14 changes: 13 additions & 1 deletion lib/fluent/plugin/out_file.rb
Expand Up @@ -181,6 +181,13 @@ def configure(conf)
@dir_perm = system_config.dir_permission || Fluent::DEFAULT_DIR_PERMISSION
@file_perm = system_config.file_permission || Fluent::DEFAULT_FILE_PERMISSION
@need_lock = system_config.workers > 1

# https://github.com/fluent/fluentd/issues/3569
@need_ruby_on_macos_workaround = false
if @append && Fluent.macos?
condition = Gem::Dependency.new('', [">= 2.7.0", "< 3.1.0"])
@need_ruby_on_macos_workaround = true if condition.match?('', RUBY_VERSION)
end
end

def multi_workers_ready?
Expand Down Expand Up @@ -223,7 +230,12 @@ def write(chunk)

def write_without_compression(path, chunk)
File.open(path, "ab", @file_perm) do |f|
chunk.write_to(f)
if @need_ruby_on_macos_workaround
content = chunk.read()
f.puts content
else
chunk.write_to(f)
end
end
end

Expand Down
42 changes: 29 additions & 13 deletions test/plugin/test_out_file.rb
Expand Up @@ -394,6 +394,11 @@ def check_gzipped_result(path, expect)
assert_equal expect, result
end

def check_result(path, expect)
result = File.read(path, mode: "rb")
assert_equal expect, result
end

sub_test_case 'write' do
test 'basic case' do
d = create_driver
Expand Down Expand Up @@ -535,38 +540,49 @@ def parse_system(text)
assert_equal 3, Dir.glob("#{TMP_DIR}/out_file_test.*").size
end

test 'append' do
data(
"with compression" => true,
"without compression" => false,
)
test 'append' do |compression|
time = event_time("2011-01-02 13:14:15 UTC")
formatted_lines = %[2011-01-02T13:14:15Z\ttest\t{"a":1}#{@default_newline}] + %[2011-01-02T13:14:15Z\ttest\t{"a":2}#{@default_newline}]

write_once = ->(){
d = create_driver %[
config = %[
path #{TMP_DIR}/out_file_test
compress gz
utc
append true
<buffer>
timekey_use_utc true
</buffer>
]
if compression
config << " compress gz"
end
d = create_driver(config)
d.run(default_tag: 'test'){
d.feed(time, {"a"=>1})
d.feed(time, {"a"=>2})
}
d.instance.last_written_path
}

path = write_once.call
assert_equal "#{TMP_DIR}/out_file_test.20110102.log.gz", path
check_gzipped_result(path, formatted_lines)

path = write_once.call
assert_equal "#{TMP_DIR}/out_file_test.20110102.log.gz", path
check_gzipped_result(path, formatted_lines * 2)
log_file_name = "out_file_test.20110102.log"
if compression
log_file_name << ".gz"
end

path = write_once.call
assert_equal "#{TMP_DIR}/out_file_test.20110102.log.gz", path
check_gzipped_result(path, formatted_lines * 3)
1.upto(3) do |i|
path = write_once.call
assert_equal "#{TMP_DIR}/#{log_file_name}", path
expect = formatted_lines * i
if compression
check_gzipped_result(path, expect)
else
check_result(path, expect)
end
end
end

test 'append when JST' do
Expand Down