Skip to content

Commit

Permalink
in_tail: Simplify the error handling while contructing a watcher
Browse files Browse the repository at this point in the history
Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Aug 26, 2021
1 parent 9b9eced commit 5b3dd1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
29 changes: 11 additions & 18 deletions lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,35 +406,28 @@ def setup_watcher(target_info, pe)

def construct_watcher(target_info)
path = target_info.path

begin
ino = Fluent::FileWrapper.stat(path).ino
rescue Errno::ENOENT, Errno::EACCES
$log.warn "stat() for #{path} failed. Continuing without tailing it."
return
end

pe = nil
if @pf
pe = @pf[target_info]
if @read_from_head && pe.read_inode.zero?
begin
pe.update(Fluent::FileWrapper.stat(path).ino, 0)
rescue Errno::ENOENT, Errno::EACCES
$log.warn "stat() for #{path} failed. Continuing without tailing it."
end
end
pe.update(ino, 0) if @read_from_head && pe.read_inode.zero?
end

begin
tw = setup_watcher(target_info, pe)
@tails[path] = tw
tw.on_notify
rescue WatcherSetupError => e
log.warn "Skip #{path} because unexpected setup error happens: #{e}"
return
end

begin
target_info = TargetInfo.new(path, Fluent::FileWrapper.stat(path).ino)
@tails[path] = tw
tw.on_notify
rescue Errno::ENOENT, Errno::EACCES => e
$log.warn "stat() for #{path} failed with #{e.class.name}. Drop tail watcher for now."
# explicitly detach and unwatch watcher `tw`.
tw.unwatched = true
detach_watcher(tw, target_info.ino, false)
end
end

def start_watchers(targets_info)
Expand Down
18 changes: 10 additions & 8 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2171,14 +2171,15 @@ def test_ENOENT_error_after_setup_watcher
'format' => 'none',
})
d = create_driver(config)
mock.proxy(d.instance).setup_watcher(anything, anything) do |tw|
mock.proxy(d.instance).existence_path do |hash|
cleanup_file(path)
tw
end
hash
end.at_least(1)
assert_nothing_raised do
d.run(shutdown: false) {}
end
assert($log.out.logs.any?{|log| log.include?("stat() for #{path} failed with Errno::ENOENT. Drop tail watcher for now.\n") })
assert($log.out.logs.any?{|log| log.include?("stat() for #{path} failed. Continuing without tailing it.\n") },
$log.out.logs.join("\n"))
ensure
d.instance_shutdown if d && d.instance
end
Expand All @@ -2196,14 +2197,15 @@ def test_EACCES_error_after_setup_watcher
'format' => 'none',
})
d = create_driver(config, false)
mock.proxy(d.instance).setup_watcher(anything, anything) do |tw|
mock.proxy(d.instance).existence_path do |hash|
FileUtils.chmod(0000, "#{TMP_DIR}/noaccess")
tw
end
hash
end.at_least(1)
assert_nothing_raised do
d.run(shutdown: false) {}
end
assert($log.out.logs.any?{|log| log.include?("stat() for #{path} failed with Errno::EACCES. Drop tail watcher for now.\n") })
assert($log.out.logs.any?{|log| log.include?("stat() for #{path} failed. Continuing without tailing it.\n") },
$log.out.logs.join("\n"))
end
ensure
d.instance_shutdown if d && d.instance
Expand Down

0 comments on commit 5b3dd1c

Please sign in to comment.