diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 88556ca6ab..2aa2122945 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -502,9 +502,13 @@ def update_watcher(tail_watcher, pe, new_inode) new_target_info = TargetInfo.new(path, new_inode) if @follow_inodes - # When follow_inodes is true, it's not cleaned up by refresh_watcher. - # So it should be unwatched here explicitly. - tail_watcher.unwatched = true + # If the old inode still exists, we can't unwatch it. + # If the old inode disappears, we should unwatch it here because + # `refresh_watcher` can't detect that the inode disappears. + # (It is because the path of `@tails` will be overwritten here.) + # (It is not a big problem if we don't unwatch here, as long as we make + # sure to detach it. The position will be cleared on restart eventually.) + tail_watcher.unwatched = true unless expand_paths.key?(tail_watcher.ino) new_position_entry = @pf[new_target_info] # If `refresh_watcher` find the new file before, this will not be zero. diff --git a/test/plugin/test_in_tail.rb b/test/plugin/test_in_tail.rb index 81bf0dfa7d..789ba12f3a 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -2704,22 +2704,14 @@ def test_updateTW_before_refreshTW_and_detach_before_refreshTW assert_equal( { - # TODO: This is BUG!! We need to fix it and replace this with the next. - record_values: ["file1 log1", "file1 log1", "file1 log2", "file1 log2", "file2 log1", "file2 log2"], - # record_values: ["file1 log1", "file1 log2", "file2 log1", "file2 log2"], + record_values: ["file1 log1", "file1 log2", "file2 log1", "file2 log2"], tail_watcher_paths: ["#{@tmp_dir}/tail.txt", "#{@tmp_dir}/tail.txt", "#{@tmp_dir}/tail.txt1"], tail_watcher_inodes: [inode_0, inode_1, inode_0], tail_watcher_io_handler_opened_statuses: [false, false, false], - # TODO: This is BUG!! We need to fix it and replace this with the next. position_entries: [ - ["#{@tmp_dir}/tail.txt", "ffffffffffffffff", inode_0], + ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_0], ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_1], - ["#{@tmp_dir}/tail.txt1", "0000000000000016", inode_0], ], - # position_entries: [ - # ["#{@tmp_dir}/tail.txt", "ffffffffffffffff", inode_0], - # ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_1], - # ], }, { record_values: record_values, @@ -2802,7 +2794,7 @@ def test_updateTW_before_refreshTW_and_detach_after_refreshTW tail_watcher_inodes: [inode_0, inode_1, inode_0], tail_watcher_io_handler_opened_statuses: [false, false, false], position_entries: [ - ["#{@tmp_dir}/tail.txt", "ffffffffffffffff", inode_0], + ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_0], ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_1], ], }, @@ -2861,9 +2853,10 @@ def test_updateTW_after_refreshTW # This overwrites `@tails["tail.txt"]` d.instance.refresh_watchers - # `watch_timer` calls `TailWatcher::on_notify`, and then `update_watcher` updates the TailWatcher: + # `watch_timer` calls `TailWatcher::on_notify`, and then `update_watcher` trys to update the TailWatcher: # TailWatcher(path: "tail.txt", inode: inode_0) => TailWatcher(path: "tail.txt", inode: inode_1) - # The old TailWathcer is detached here since `rotate_wait` is just `1s`. + # However, it is already added in `refresh_watcher`, so `update_watcher` doesn't create the new TailWatcher. + # `update_watcher` only detachs the old TailWathcer here since `rotate_wait` is just `1s` sleep 3 # This adds a new TailWatcher: TailWatcher(path: "tail.txt1", inode: inode_0) @@ -2886,22 +2879,14 @@ def test_updateTW_after_refreshTW assert_equal( { - # TODO: This is BUG!! We need to fix it and replace this with the next. - record_values: ["file1 log1", "file1 log1", "file1 log2", "file1 log2", "file2 log1", "file2 log2"], - # record_values: ["file1 log1", "file1 log2", "file2 log1", "file2 log2"], + record_values: ["file1 log1", "file1 log2", "file2 log1", "file2 log2"], tail_watcher_paths: ["#{@tmp_dir}/tail.txt", "#{@tmp_dir}/tail.txt", "#{@tmp_dir}/tail.txt1"], tail_watcher_inodes: [inode_0, inode_1, inode_0], tail_watcher_io_handler_opened_statuses: [false, false, false], - # TODO: This is BUG!! We need to fix it and replace this with the next. position_entries: [ - ["#{@tmp_dir}/tail.txt", "ffffffffffffffff", inode_0], + ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_0], ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_1], - ["#{@tmp_dir}/tail.txt1", "0000000000000016", inode_0], ], - # position_entries: [ - # ["#{@tmp_dir}/tail.txt", "ffffffffffffffff", inode_0], - # ["#{@tmp_dir}/tail.txt", "0000000000000016", inode_1], - # ], }, { record_values: record_values,