Skip to content

Commit ab81c16

Browse files
committed
in_tail: Manage tail watchers under rorate_wait state
After a tail watcher transitions to `rotate_wait` state, the `rotate_wait` timer is no longer managed by in_tail, it might cause unexpected behaviour. e.g.) * It's never unwatched when shutdown occurs before `rotate_wait` passed. * Needless `rotate_wait` timers are executed when it detects more rotations. This patch fixes such unexpected behaviour. Signed-off-by: Takuro Ashie <[email protected]>
1 parent 0893c39 commit ab81c16

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Diff for: lib/fluent/plugin/in_tail.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def initialize
5252
super
5353
@paths = []
5454
@tails = {}
55+
@tails_rotate_wait = {}
5556
@pf_file = nil
5657
@pf = nil
5758
@ignore_list = []
@@ -267,6 +268,9 @@ def shutdown
267268
@shutdown_start_time = Fluent::Clock.now
268269
# during shutdown phase, don't close io. It should be done in close after all threads are stopped. See close.
269270
stop_watchers(existence_path, immediate: true, remove_watcher: false)
271+
@tails_rotate_wait.keys.each do |tw|
272+
detach_watcher(tw, @tails_rotate_wait[tw][:ino])
273+
end
270274
@pf_file.close if @pf_file
271275

272276
super
@@ -590,6 +594,8 @@ def detach_watcher(tw, ino, close_io = true)
590594
target_info = TargetInfo.new(tw.path, ino)
591595
@pf.unwatch(target_info)
592596
end
597+
598+
@tails_rotate_wait.delete(tw)
593599
end
594600

595601
def throttling_is_enabled?(tw)
@@ -604,7 +610,11 @@ def detach_watcher_after_rotate_wait(tw, ino)
604610
if @open_on_every_update
605611
# Detach now because it's already closed, waiting it doesn't make sense.
606612
detach_watcher(tw, ino)
607-
elsif throttling_is_enabled?(tw)
613+
end
614+
615+
return if @tails_rotate_wait[tw]
616+
617+
if throttling_is_enabled?(tw)
608618
# When the throttling feature is enabled, it might not reach EOF yet.
609619
# Should ensure to read all contents before closing it, with keeping throttling.
610620
start_time_to_wait = Fluent::Clock.now
@@ -615,11 +625,13 @@ def detach_watcher_after_rotate_wait(tw, ino)
615625
detach_watcher(tw, ino)
616626
end
617627
end
628+
@tails_rotate_wait[tw] = { ino: ino, timer: timer }
618629
else
619630
# when the throttling feature isn't enabled, just wait @rotate_wait
620-
timer_execute(:in_tail_close_watcher, @rotate_wait, repeat: false) do
631+
timer = timer_execute(:in_tail_close_watcher, @rotate_wait, repeat: false) do
621632
detach_watcher(tw, ino)
622633
end
634+
@tails_rotate_wait[tw] = { ino: ino, timer: timer }
623635
end
624636
end
625637

Diff for: test/plugin/test_in_tail.rb

-3
Original file line numberDiff line numberDiff line change
@@ -3084,9 +3084,6 @@ def test_refreshTW_during_rotation
30843084
sleep 3
30853085

30863086
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt0", "ab") {|f| f.puts "file3 log2"}
3087-
3088-
# Wait `rotate_wait` for file2 to make sure to close all IO handlers
3089-
sleep 3
30903087
end
30913088

30923089
inode_0 = tail_watchers[0]&.ino

0 commit comments

Comments
 (0)