Skip to content

Commit 94e867b

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 52e46f0 commit 94e867b

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
@@ -568,6 +572,8 @@ def detach_watcher(tw, ino, close_io = true)
568572
target_info = TargetInfo.new(tw.path, ino)
569573
@pf.unwatch(target_info)
570574
end
575+
576+
@tails_rotate_wait.delete(tw)
571577
end
572578

573579
def throttling_is_enabled?(tw)
@@ -582,7 +588,11 @@ def detach_watcher_after_rotate_wait(tw, ino)
582588
if @open_on_every_update
583589
# Detach now because it's already closed, waiting it doesn't make sense.
584590
detach_watcher(tw, ino)
585-
elsif throttling_is_enabled?(tw)
591+
end
592+
593+
return if @tails_rotate_wait[tw]
594+
595+
if throttling_is_enabled?(tw)
586596
# When the throttling feature is enabled, it might not reach EOF yet.
587597
# Should ensure to read all contents before closing it, with keeping throttling.
588598
start_time_to_wait = Fluent::Clock.now
@@ -593,11 +603,13 @@ def detach_watcher_after_rotate_wait(tw, ino)
593603
detach_watcher(tw, ino)
594604
end
595605
end
606+
@tails_rotate_wait[tw] = { ino: ino, timer: timer }
596607
else
597608
# when the throttling feature isn't enabled, just wait @rotate_wait
598-
timer_execute(:in_tail_close_watcher, @rotate_wait, repeat: false) do
609+
timer = timer_execute(:in_tail_close_watcher, @rotate_wait, repeat: false) do
599610
detach_watcher(tw, ino)
600611
end
612+
@tails_rotate_wait[tw] = { ino: ino, timer: timer }
601613
end
602614
end
603615

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)