From 37eb013eae22df11c4dc1a58d08f73cf57e33190 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Wed, 9 Dec 2020 18:32:38 +0900 Subject: [PATCH 1/9] Use target_info instead of path_with_inode and path and ino separated arguments Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail.rb | 54 ++++++++--------- lib/fluent/plugin/in_tail/position_file.rb | 20 +++---- test/plugin/in_tail/test_position_file.rb | 40 ++++++++----- test/plugin/test_in_tail.rb | 70 +++++++++++----------- 4 files changed, 96 insertions(+), 88 deletions(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index d57218e3e9..51d12ca78a 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -353,9 +353,9 @@ def refresh_watchers start_watchers(added_hash) unless added_hash.empty? end - def setup_watcher(path_with_inode, pe) + def setup_watcher(target_info, pe) line_buffer_timer_flusher = @multiline_mode ? TailWatcher::LineBufferTimerFlusher.new(log, @multiline_flush_interval, &method(:flush_buffer)) : nil - tw = TailWatcher.new(path_with_inode, pe, log, @read_from_head, @follow_inodes, method(:update_watcher), line_buffer_timer_flusher, method(:io_handler)) + tw = TailWatcher.new(target_info, pe, log, @read_from_head, @follow_inodes, method(:update_watcher), line_buffer_timer_flusher, method(:io_handler)) if @enable_watch_timer tt = TimerTrigger.new(1, log) { tw.on_notify } @@ -386,16 +386,14 @@ def setup_watcher(path_with_inode, pe) raise e end - def start_watchers(paths_with_inodes) - paths_with_inodes.each_value { |path_with_inode| - path = path_with_inode.path - ino = path_with_inode.ino + def start_watchers(targets_info) + targets_info.each_value { |target_info| pe = nil if @pf - pe = @pf[path, ino] + pe = @pf[target_info] if @read_from_head && pe.read_inode.zero? begin - pe.update(Fluent::FileWrapper.stat(path).ino, 0) + pe.update(Fluent::FileWrapper.stat(target_info.path).ino, 0) rescue Errno::ENOENT $log.warn "#{path} not found. Continuing without tailing it." end @@ -403,37 +401,37 @@ def start_watchers(paths_with_inodes) end begin - tw = setup_watcher(path_with_inode, pe) + tw = setup_watcher(target_info, pe) rescue WatcherSetupError => e log.warn "Skip #{path} because unexpected setup error happens: #{e}" next end - target_info = TargetInfo.new(path, Fluent::FileWrapper.stat(path).ino) + target_info = TargetInfo.new(target_info.path, Fluent::FileWrapper.stat(target_info.path).ino) @tails[target_info] = tw } end - def stop_watchers(paths_with_inodes, immediate: false, unwatched: false, remove_watcher: true) - paths_with_inodes.each_value { |path_with_inode| + def stop_watchers(targets_info, immediate: false, unwatched: false, remove_watcher: true) + targets_info.each_value { |target_info| if remove_watcher - tw = @tails.delete(path_with_inode) + tw = @tails.delete(target_info) else - tw = @tails[path_with_inode] + tw = @tails[target_info] end if tw tw.unwatched = unwatched if immediate - detach_watcher(tw, path_with_inode.ino, false) + detach_watcher(tw, target_info.ino, false) else - detach_watcher_after_rotate_wait(tw, path_with_inode.ino) + detach_watcher_after_rotate_wait(tw, target_info.ino) end end } end def close_watcher_handles - @tails.keys.each do |path_with_inode| - tw = @tails.delete(path_with_inode) + @tails.keys.each do |target_info| + tw = @tails.delete(target_info) if tw tw.close end @@ -441,23 +439,22 @@ def close_watcher_handles end # refresh_watchers calls @tails.keys so we don't use stop_watcher -> start_watcher sequence for safety. - def update_watcher(path_with_inode, pe) + def update_watcher(target_info, pe) log.info("detected rotation of #{path}; waiting #{@rotate_wait} seconds") if @pf - unless pe.read_inode == @pf[path_with_inode.path, pe.read_inode].read_inode + target_info_from_position_entry = TargetInfo.new(target_info.path, pe.read_inode) + unless pe.read_inode == @pf[target_info_from_position_entry].read_inode log.debug "Skip update_watcher because watcher has been already updated by other inotify event" return end end - target_info = TargetInfo.new(path_with_inode.path, pe.read_inode) rotated_tw = @tails[target_info] - - new_target_info = TargetInfo.new(path_with_inode.path, path_with_inode.ino) + new_target_info = TargetInfo.new(target_info.path, target_info.ino) if @follow_inodes - new_position_entry = @pf[path_with_inode.path, path_with_inode.ino] + new_position_entry = @pf[target_info] if new_position_entry.read_inode == 0 @tails[new_target_info] = setup_watcher(new_target_info, new_position_entry) @@ -481,7 +478,8 @@ def detach_watcher(tw, ino, close_io = true) tw.close if close_io if tw.unwatched && @pf - @pf.unwatch(tw.path, ino) + target_info = TargetInfo.new(tw.path, ino) + @pf.unwatch(target_info) end end @@ -657,9 +655,9 @@ def on_timer end class TailWatcher - def initialize(path_with_inode, pe, log, read_from_head, follow_inodes, update_watcher, line_buffer_timer_flusher, io_handler_build) - @path = path_with_inode.path - @ino = path_with_inode.ino + def initialize(target_info, pe, log, read_from_head, follow_inodes, update_watcher, line_buffer_timer_flusher, io_handler_build) + @path = target_info.path + @ino = target_info.ino @pe = pe || MemoryPositionEntry.new @read_from_head = read_from_head @follow_inodes = follow_inodes diff --git a/lib/fluent/plugin/in_tail/position_file.rb b/lib/fluent/plugin/in_tail/position_file.rb index 702584698c..5c3e7eb2e2 100644 --- a/lib/fluent/plugin/in_tail/position_file.rb +++ b/lib/fluent/plugin/in_tail/position_file.rb @@ -37,32 +37,32 @@ def initialize(file, follow_inodes, existing_paths, logger: nil) @existing_paths = existing_paths end - def [](path, inode) - if @follow_inodes && m = @map[inode] + def [](target_info) + if @follow_inodes && m = @map[target_info.ino] return m - elsif !@follow_inodes && m = @map[path] + elsif !@follow_inodes && m = @map[target_info.path] return m end @file_mutex.synchronize { @file.seek(0, IO::SEEK_END) - seek = @file.pos + path.bytesize + 1 - @file.write "#{path}\t0000000000000000\t0000000000000000\n" + seek = @file.pos + target_info.path.bytesize + 1 + @file.write "#{target_info.path}\t0000000000000000\t0000000000000000\n" if @follow_inodes - @map[inode] = FilePositionEntry.new(@file, @file_mutex, seek, 0, 0) + @map[target_info.ino] = FilePositionEntry.new(@file, @file_mutex, seek, 0, 0) else - @map[path] = FilePositionEntry.new(@file, @file_mutex, seek, 0, 0) + @map[target_info.path] = FilePositionEntry.new(@file, @file_mutex, seek, 0, 0) end } end - def unwatch(path, inode) + def unwatch(target_info) if @follow_inodes - if (entry = @map.delete(inode)) + if (entry = @map.delete(target_info.ino)) entry.update_pos(UNWATCHED_POSITION) end else - if (entry = @map.delete(path)) + if (entry = @map.delete(target_info.path)) entry.update_pos(UNWATCHED_POSITION) end end diff --git a/test/plugin/in_tail/test_position_file.rb b/test/plugin/in_tail/test_position_file.rb index fc79c24ee3..8363d41716 100644 --- a/test/plugin/in_tail/test_position_file.rb +++ b/test/plugin/in_tail/test_position_file.rb @@ -87,11 +87,15 @@ def follow_inodes_block test 'update seek postion of remained position entry' do pf = Fluent::Plugin::TailInput::PositionFile.new(@file, false, {}, **{logger: $log}) - pf['path1', -1] - pf['path2', -1] - pf['path3', -1] + target_info1 = Fluent::Plugin::TailInput::TargetInfo.new('path1', -1) + target_info2 = Fluent::Plugin::TailInput::TargetInfo.new('path2', -1) + target_info3 = Fluent::Plugin::TailInput::TargetInfo.new('path3', -1) + pf[target_info1] + pf[target_info2] + pf[target_info3] - pf.unwatch('path1', 1234) + target_info1_2 = Fluent::Plugin::TailInput::TargetInfo.new('path1', 1234) + pf.unwatch(target_info1_2) pf.try_compact @@ -101,8 +105,10 @@ def follow_inodes_block assert_equal "path3\t0000000000000000\t0000000000000000\n", lines[1] assert_equal 2, lines.size - pf.unwatch('path2', 1235) - pf.unwatch('path3', 1236) + target_info2_2 = Fluent::Plugin::TailInput::TargetInfo.new('path2', 1235) + target_info3_2 = Fluent::Plugin::TailInput::TargetInfo.new('path3', 1236) + pf.unwatch(target_info2_2) + pf.unwatch(target_info3_2) @file.seek(0) lines = @file.readlines assert_equal "path2\t#{UNWATCHED_STR}\t0000000000000000\n", lines[0] @@ -141,7 +147,8 @@ def follow_inodes_block write_data(@file, TEST_CONTENT) pf = Fluent::Plugin::TailInput::PositionFile.load(@file, false, {}, **{logger: $log}) - f = pf['valid_path', Fluent::FileWrapper.stat(@file).ino] + valid_target_info = Fluent::Plugin::TailInput::TargetInfo.new('valid_path', Fluent::FileWrapper.stat(@file).ino) + f = pf[valid_target_info] assert_equal Fluent::Plugin::TailInput::FilePositionEntry, f.class assert_equal 2, f.read_pos assert_equal 1, f.read_inode @@ -150,7 +157,8 @@ def follow_inodes_block lines = @file.readlines assert_equal 2, lines.size - f = pf['nonexist_path', -1] + nonexistent_target_info = Fluent::Plugin::TailInput::TargetInfo.new('nonexist_path', -1) + f = pf[nonexistent_target_info] assert_equal Fluent::Plugin::TailInput::FilePositionEntry, f.class assert_equal 0, f.read_pos assert_equal 0, f.read_inode @@ -165,17 +173,17 @@ def follow_inodes_block write_data(@file, TEST_CONTENT) pf = Fluent::Plugin::TailInput::PositionFile.load(@file, false, {}, logger: $log) - f = pf['nonexist_path', -1] + f = pf[Fluent::Plugin::TailInput::TargetInfo.new('nonexist_path', -1)] assert_equal 0, f.read_inode assert_equal 0, f.read_pos - pf['valid_path', Fluent::FileWrapper.stat(@file).ino].update(1, 2) + pf[Fluent::Plugin::TailInput::TargetInfo.new('valid_path', Fluent::FileWrapper.stat(@file).ino)].update(1, 2) - f = pf['nonexist_path', -1] + f = pf[Fluent::Plugin::TailInput::TargetInfo.new('nonexist_path', -1)] assert_equal 0, f.read_inode assert_equal 0, f.read_pos - pf['nonexist_path', -1].update(1, 2) + pf[Fluent::Plugin::TailInput::TargetInfo.new('nonexist_path', -1)].update(1, 2) assert_equal 1, f.read_inode assert_equal 2, f.read_pos end @@ -186,14 +194,16 @@ def follow_inodes_block write_data(@file, TEST_CONTENT) pf = Fluent::Plugin::TailInput::PositionFile.load(@file, false, {}, logger: $log) inode1 = Fluent::FileWrapper.stat(@file).ino - p1 = pf['valid_path', inode1] + target_info1 = Fluent::Plugin::TailInput::TargetInfo.new('valid_path', inode1) + p1 = pf[target_info1] assert_equal Fluent::Plugin::TailInput::FilePositionEntry, p1.class - pf.unwatch('valid_path', inode1) + pf.unwatch(target_info1) assert_equal p1.read_pos, Fluent::Plugin::TailInput::PositionFile::UNWATCHED_POSITION inode2 = Fluent::FileWrapper.stat(@file).ino - p2 = pf['valid_path', inode2] + target_info2 = Fluent::Plugin::TailInput::TargetInfo.new('valid_path', inode2) + p2 = pf[target_info2] assert_equal Fluent::Plugin::TailInput::FilePositionEntry, p2.class assert_not_equal p1, p2 diff --git a/test/plugin/test_in_tail.rb b/test/plugin/test_in_tail.rb index a20997b1ab..b84e26e5a7 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -47,7 +47,7 @@ def cleanup_file(path) end end - def path_to_tuple(path) + def create_target_info(path) Fluent::Plugin::TailInput::TargetInfo.new(path, Fluent::FileWrapper.stat(path).ino) end @@ -1055,9 +1055,9 @@ def test_multiline_without_firstline(data) }) def test_expand_paths ex_paths = [ - path_to_tuple('test/plugin/data/2010/01/20100102-030405.log'), - path_to_tuple('test/plugin/data/log/foo/bar.log'), - path_to_tuple('test/plugin/data/log/test.log') + create_target_info('test/plugin/data/2010/01/20100102-030405.log'), + create_target_info('test/plugin/data/log/foo/bar.log'), + create_target_info('test/plugin/data/log/test.log') ] plugin = create_driver(EX_CONFIG, false).instance flexstub(Time) do |timeclass| @@ -1073,8 +1073,8 @@ def test_expand_paths def test_expand_paths_with_duplicate_configuration expanded_paths = [ - path_to_tuple('test/plugin/data/log/foo/bar.log'), - path_to_tuple('test/plugin/data/log/test.log') + create_target_info('test/plugin/data/log/foo/bar.log'), + create_target_info('test/plugin/data/log/test.log') ] duplicate_config = EX_CONFIG.dup duplicate_config["path"]="test/plugin/data/log/**/*.log, test/plugin/data/log/**/*.log" @@ -1084,9 +1084,9 @@ def test_expand_paths_with_duplicate_configuration def test_expand_paths_with_timezone ex_paths = [ - path_to_tuple('test/plugin/data/2010/01/20100102-030405.log'), - path_to_tuple('test/plugin/data/log/foo/bar.log'), - path_to_tuple('test/plugin/data/log/test.log') + create_target_info('test/plugin/data/2010/01/20100102-030405.log'), + create_target_info('test/plugin/data/log/foo/bar.log'), + create_target_info('test/plugin/data/log/test.log') ] ['Asia/Taipei', '+08'].each do |tz_type| taipei_config = EX_CONFIG + config_element("", "", {"path_timezone" => tz_type}) @@ -1110,10 +1110,10 @@ def test_expand_paths_with_timezone def test_log_file_without_extension expected_files = [ - path_to_tuple('test/plugin/data/log/bar'), - path_to_tuple('test/plugin/data/log/foo/bar.log'), - path_to_tuple('test/plugin/data/log/foo/bar2'), - path_to_tuple('test/plugin/data/log/test.log') + create_target_info('test/plugin/data/log/bar'), + create_target_info('test/plugin/data/log/foo/bar.log'), + create_target_info('test/plugin/data/log/foo/bar2'), + create_target_info('test/plugin/data/log/test.log') ] config = config_element("", "", { @@ -1251,9 +1251,9 @@ def test_pos_file_dir_creation_with_system_dir_permission def test_z_refresh_watchers ex_paths = [ - path_to_tuple('test/plugin/data/2010/01/20100102-030405.log'), - path_to_tuple('test/plugin/data/log/foo/bar.log'), - path_to_tuple('test/plugin/data/log/test.log'), + create_target_info('test/plugin/data/2010/01/20100102-030405.log'), + create_target_info('test/plugin/data/log/foo/bar.log'), + create_target_info('test/plugin/data/log/test.log'), ] plugin = create_driver(EX_CONFIG, false).instance sio = StringIO.new @@ -1263,8 +1263,8 @@ def test_z_refresh_watchers end Timecop.freeze(2010, 1, 2, 3, 4, 5) do - ex_paths.each do |path_with_inode| - mock.proxy(Fluent::Plugin::TailInput::TailWatcher).new(path_with_inode , anything, anything, true, false, anything, nil, anything).once + ex_paths.each do |target_info| + mock.proxy(Fluent::Plugin::TailInput::TailWatcher).new(target_info, anything, anything, true, false, anything, nil, anything).once end plugin.refresh_watchers @@ -1503,7 +1503,7 @@ def test_should_keep_and_update_existing_file_pos_entry_for_deleted_file_when_ne Timecop.travel(Time.now + 10) do sleep 5 pos_file.pos = 0 - tuple = path_to_tuple("#{TMP_DIR}/tail.txt") + tuple = create_target_info("#{TMP_DIR}/tail.txt") path_pos_ino = /^([^\t]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)/.match(pos_file.readline) assert_equal(tuple.path, path_pos_ino[1]) assert_equal(12, path_pos_ino[2].to_i(16)) @@ -1531,7 +1531,7 @@ def test_should_mark_file_unwatched_after_limit_recently_modified_and_rotate_wai f.puts "test1" f.puts "test2" } - path_ino = path_to_tuple("#{TMP_DIR}/tail.txt") + target_info = create_target_info("#{TMP_DIR}/tail.txt") d.run(expect_emits: 1, shutdown: false) do File.open("#{TMP_DIR}/tail.txt", "ab") {|f| f.puts "test3\n"} @@ -1541,11 +1541,11 @@ def test_should_mark_file_unwatched_after_limit_recently_modified_and_rotate_wai Timecop.travel(Time.now + 10) do waiting(5) { # @pos will be reset as 0 when UNWATCHED_POSITION is specified. - sleep 0.1 until d.instance.instance_variable_get(:@pf)[path_ino.path, path_ino.ino].read_pos == 0 + sleep 0.1 until d.instance.instance_variable_get(:@pf)[target_info].read_pos == 0 } end - assert_equal(0, d.instance.instance_variable_get(:@pf)[path_ino.path, path_ino.ino].read_pos) + assert_equal(0, d.instance.instance_variable_get(:@pf)[target_info].read_pos) d.instance_shutdown end @@ -1613,7 +1613,7 @@ def test_should_mark_file_unwatched_if_same_name_file_created_with_different_ino f.puts "test1" f.puts "test2" } - path_ino = path_to_tuple("#{TMP_DIR}/tail.txt") + target_info = create_target_info("#{TMP_DIR}/tail.txt") d.run(expect_emits: 2, shutdown: false) do File.open("#{TMP_DIR}/tail.txt", "ab") {|f| f.puts "test3\n"} @@ -1621,15 +1621,15 @@ def test_should_mark_file_unwatched_if_same_name_file_created_with_different_ino File.open("#{TMP_DIR}/tail.txt", "wb") {|f| f.puts "test4\n"} end - new_path_ino = path_to_tuple("#{TMP_DIR}/tail.txt") + new_target_info = create_target_info("#{TMP_DIR}/tail.txt") pos_file = d.instance.instance_variable_get(:@pf) waiting(10) { # @pos will be reset as 0 when UNWATCHED_POSITION is specified. - sleep 0.1 until pos_file[path_ino.path, path_ino.ino].read_pos == 0 + sleep 0.1 until pos_file[target_info].read_pos == 0 } - new_position = pos_file[new_path_ino.path, new_path_ino.ino].read_pos + new_position = pos_file[new_target_info].read_pos assert_equal(6, new_position) d.instance_shutdown @@ -1645,16 +1645,16 @@ def test_should_close_watcher_after_rotate_wait f.puts "test1" f.puts "test2" } - path_ino = path_to_tuple("#{TMP_DIR}/tail.txt") - mock.proxy(Fluent::Plugin::TailInput::TailWatcher).new(path_ino, anything, anything, true, true, anything, nil, anything).once + target_info = create_target_info("#{TMP_DIR}/tail.txt") + mock.proxy(Fluent::Plugin::TailInput::TailWatcher).new(target_info, anything, anything, true, true, anything, nil, anything).once d.run(shutdown: false) - assert d.instance.instance_variable_get(:@tails)[path_ino] + assert d.instance.instance_variable_get(:@tails)[target_info] Timecop.travel(now + 10) do d.instance.instance_eval do - sleep 0.1 until @tails[path_ino] == nil + sleep 0.1 until @tails[target_info] == nil end - assert_nil d.instance.instance_variable_get(:@tails)[path_ino] + assert_nil d.instance.instance_variable_get(:@tails)[target_info] end d.instance_shutdown end @@ -1669,7 +1669,7 @@ def test_should_create_new_watcher_for_new_file_with_same_name f.puts "test1" f.puts "test2" } - path_ino = path_to_tuple("#{TMP_DIR}/tail.txt") + path_ino = create_target_info("#{TMP_DIR}/tail.txt") d.run(expect_emits: 1, shutdown: false) do File.open("#{TMP_DIR}/tail.txt", "ab") {|f| f.puts "test3\n"} @@ -1680,7 +1680,7 @@ def test_should_create_new_watcher_for_new_file_with_same_name f.puts "test3" f.puts "test4" } - new_path_ino = path_to_tuple("#{TMP_DIR}/tail.txt") + new_path_ino = create_target_info("#{TMP_DIR}/tail.txt") Timecop.travel(now + 10) do sleep 3 @@ -1852,8 +1852,8 @@ def test_limit_recently_modified }) expected_files = [ - path_to_tuple("#{TMP_DIR}/tail_watch1.txt"), - path_to_tuple("#{TMP_DIR}/tail_watch2.txt") + create_target_info("#{TMP_DIR}/tail_watch1.txt"), + create_target_info("#{TMP_DIR}/tail_watch2.txt") ] Timecop.freeze(now) do From 1eed5ec6cfdba3c2ae1d7be4ae8894c1b5c61f01 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 10 Dec 2020 11:34:28 +0900 Subject: [PATCH 2/9] position_file: Use more general name for key name This is because `@map` contains path/inode key and its value. Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail/position_file.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/fluent/plugin/in_tail/position_file.rb b/lib/fluent/plugin/in_tail/position_file.rb index 5c3e7eb2e2..d364061e56 100644 --- a/lib/fluent/plugin/in_tail/position_file.rb +++ b/lib/fluent/plugin/in_tail/position_file.rb @@ -112,8 +112,9 @@ def try_compact @file.truncate(0) @file.write(entries.values.map(&:to_entry_fmt).join) - entries.each do |path, val| - if (m = @map[path]) + # entry contains path/ino key and value. + entries.each do |key, val| + if (m = @map[key]) m.seek = val.seek end end From e4abf5847f2f0a29dc71319f3be1fb22b989ee9b Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 10 Dec 2020 16:53:26 +0900 Subject: [PATCH 3/9] in_tail: Create a variable to prevent calling twice Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 51d12ca78a..44e7658dbf 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -443,8 +443,9 @@ def update_watcher(target_info, pe) log.info("detected rotation of #{path}; waiting #{@rotate_wait} seconds") if @pf - target_info_from_position_entry = TargetInfo.new(target_info.path, pe.read_inode) - unless pe.read_inode == @pf[target_info_from_position_entry].read_inode + pe_inode = pe.read_inode + target_info_from_position_entry = TargetInfo.new(target_info.path, pe_inode) + unless pe_inode == @pf[target_info_from_position_entry].read_inode log.debug "Skip update_watcher because watcher has been already updated by other inotify event" return end From 828f91496763e8069a6e3a1c082a1f8d0b492d04 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 10 Dec 2020 16:57:14 +0900 Subject: [PATCH 4/9] Use #dup insetad of freshly created struct Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 44e7658dbf..eab855b2ed 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -452,7 +452,7 @@ def update_watcher(target_info, pe) end rotated_tw = @tails[target_info] - new_target_info = TargetInfo.new(target_info.path, target_info.ino) + new_target_info = target_info.dup if @follow_inodes new_position_entry = @pf[target_info] From bc0602657817f3e20d34fe5678ff1650bfc153d8 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 10 Dec 2020 16:58:44 +0900 Subject: [PATCH 5/9] position_file: Use ternary operator to simplify clause Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail/position_file.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/fluent/plugin/in_tail/position_file.rb b/lib/fluent/plugin/in_tail/position_file.rb index d364061e56..b7b0301084 100644 --- a/lib/fluent/plugin/in_tail/position_file.rb +++ b/lib/fluent/plugin/in_tail/position_file.rb @@ -57,14 +57,8 @@ def [](target_info) end def unwatch(target_info) - if @follow_inodes - if (entry = @map.delete(target_info.ino)) - entry.update_pos(UNWATCHED_POSITION) - end - else - if (entry = @map.delete(target_info.path)) - entry.update_pos(UNWATCHED_POSITION) - end + if (entry = @map.delete(@follow_inodes ? target_info.ino : target_info.path)) + entry.update_pos(UNWATCHED_POSITION) end end From 9c188481287e877229ddae751d12ebd3de9f789d Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 11 Dec 2020 11:04:39 +0900 Subject: [PATCH 6/9] in_tail: Use target_info.path instead of path on string interpolation Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index eab855b2ed..6c3665f848 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -395,7 +395,7 @@ def start_watchers(targets_info) begin pe.update(Fluent::FileWrapper.stat(target_info.path).ino, 0) rescue Errno::ENOENT - $log.warn "#{path} not found. Continuing without tailing it." + $log.warn "#{target_info.path} not found. Continuing without tailing it." end end end @@ -403,7 +403,7 @@ def start_watchers(targets_info) begin tw = setup_watcher(target_info, pe) rescue WatcherSetupError => e - log.warn "Skip #{path} because unexpected setup error happens: #{e}" + log.warn "Skip #{target_info.path} because unexpected setup error happens: #{e}" next end target_info = TargetInfo.new(target_info.path, Fluent::FileWrapper.stat(target_info.path).ino) @@ -440,7 +440,7 @@ def close_watcher_handles # refresh_watchers calls @tails.keys so we don't use stop_watcher -> start_watcher sequence for safety. def update_watcher(target_info, pe) - log.info("detected rotation of #{path}; waiting #{@rotate_wait} seconds") + log.info("detected rotation of #{target_info.path}; waiting #{@rotate_wait} seconds") if @pf pe_inode = pe.read_inode From 17b5d82a98bc8354fb60aae2f4470a838bc63a14 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 11 Dec 2020 11:11:48 +0900 Subject: [PATCH 7/9] in_tail: Use target_info instead of path_ino name Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 6c3665f848..c098504444 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -325,11 +325,11 @@ def expand_paths def existence_path hash = {} - @tails.each_key {|path_ino| + @tails.each_key {|target_info| if @follow_inodes - hash[path_ino.ino] = path_ino + hash[target_info.ino] = target_info else - hash[path_ino.path] = path_ino + hash[target_info.path] = target_info end } hash From a106a0317111e50f705fd861dcaf51d7da03acd7 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 11 Dec 2020 14:35:32 +0900 Subject: [PATCH 8/9] in_tail: Restore rotated target info line Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index c098504444..f1d6696ade 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -451,7 +451,8 @@ def update_watcher(target_info, pe) end end - rotated_tw = @tails[target_info] + rotated_target_info = TargetInfo.new(target_info.path, pe.read_inode) + rotated_tw = @tails[rotated_target_info] new_target_info = target_info.dup if @follow_inodes From 978551efca87bb0b89d4cf1ad15a1f5953c94de5 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 11 Dec 2020 15:03:11 +0900 Subject: [PATCH 9/9] position_file: Use ternary operator in early return on #[] Signed-off-by: Hiroshi Hatake --- lib/fluent/plugin/in_tail/position_file.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/fluent/plugin/in_tail/position_file.rb b/lib/fluent/plugin/in_tail/position_file.rb index b7b0301084..88614d2802 100644 --- a/lib/fluent/plugin/in_tail/position_file.rb +++ b/lib/fluent/plugin/in_tail/position_file.rb @@ -38,9 +38,7 @@ def initialize(file, follow_inodes, existing_paths, logger: nil) end def [](target_info) - if @follow_inodes && m = @map[target_info.ino] - return m - elsif !@follow_inodes && m = @map[target_info.path] + if m = @map[@follow_inodes ? target_info.ino : target_info.path] return m end