Module: Fluent::Plugin::TailInput::DirWatcher
- Included in:
- Fluent::Plugin::TailInput
- Defined in:
- lib/fluent/plugin/in_tail/dir_watcher.rb
Overview
Discover files matching the configured paths and refresh file watchers. Periodic refreshes always run; enable_dir_watcher adds directory events to discover new files sooner than the next refresh_interval check.
Coolio::StatWatcher uses libev's ev_stat, with inotify on Linux where available and stat polling as a fallback. Detection latency depends on the platform and file system.
ev_stat compares several stat fields, including second-resolution timestamps. Changes can be missed when those fields remain unchanged; delaying a refresh does not guarantee detection. Periodic refreshes remain the fallback for missed changes.
Constant Summary collapse
- DIR_WATCH_DEBOUNCE_INTERVAL =
Directory changes may arrive close together. Collect requests during this interval and refresh once, without extending the pending timer.
1- MAX_DIR_PATTERNS =
Brace alternatives can multiply the paths whose directories must be examined. Bound their expansion as well as the number of watchers.
100- WILDCARD_CHARS =
A component of a path which contains one of these characters may match a directory which is not named by the component itself, that is a directory which may be created later.
/[*?\[{]/- GLOB_STAR =
A component of `**' stands for no directory at all or for directories at any depth, so it is expanded recursively instead of as a wildcard of one directory name.
'**'
Instance Method Summary collapse
- #attach_dir_watcher(dir) ⇒ Object
-
#brace_group(pattern) ⇒ Object
Return the positions of the first unescaped 'and its closing '', or nil if there is no complete group.
- #detach_dir_watcher(dir) ⇒ Object
- #detach_dir_watchers ⇒ Object
- #detach_refresh_timer ⇒ Object
-
#dir_path_parts(dir_path) ⇒ Object
Split a directory path into the root directory it starts from and the components below it, so that the path can be walked from its topmost directory:
/base/logs' gives/' and [base',logs'], andC:/base' givesC:/' and [`base']. - #existence_path ⇒ Object
-
#existing_dirs(parent, component, glob) ⇒ Object
The existing directories matching one component of a path pattern inside one of the directories matched by the components above it.
-
#expand_brace_patterns(pattern, warning_key: pattern) ⇒ Object
Expand the brace groups of a path pattern into the paths they stand for, the same way Dir.glob does:
{a,b}' stands fora' and `b', the groups can be nested, and a group may contain a separator. - #expand_brace_patterns_into(pattern, patterns, limit: MAX_DIR_PATTERNS) ⇒ Object
- #expand_paths ⇒ Object
- #expand_paths_raw ⇒ Object
-
#expand_watch_dirs ⇒ Object
Derive watch directories from path patterns, not existing files, so files created later can also be discovered.
-
#expand_watch_dirs_of_pattern(path, expanded, glob) ⇒ Object
The directories to watch for one path pattern.
- #extended_glob_pattern(path) ⇒ Object
- #initialize_dir_watchers ⇒ Object
-
#interleave_watch_dirs(dirs_by_path) ⇒ Object
Interleave candidates so a large glob cannot monopolize the watcher limit and leave later paths entirely unwatched.
-
#limit_dir_watchers(dirs, rotate:) ⇒ Object
Take a circular window of the interleaved candidates.
-
#literal_glob_pattern(path) ⇒ Object
Escape glob characters in matched directory names so later components do not reinterpret those names as patterns.
- #on_dir_changed(dir) ⇒ Object
-
#path_components(path) ⇒ Object
Normalize Windows file system separators before splitting.
-
#real_path(dir) ⇒ Object
Resolve symbolic links and `..' through the file system and return a canonical path for deduplication, or nil if resolution fails.
-
#refresh_watchers ⇒ Object
Rotated files matching path can be discovered as new files and reread.
-
#refresh_watchers_on_timer ⇒ Object
Without directory events, keep the ordinary periodic refresh path.
- #refresh_watchers_raw ⇒ Object
-
#request_refresh_watchers(trigger, delay: nil) ⇒ Object
Merge directory changes and periodic requests into a pending refresh.
-
#split_brace_alternatives(group) ⇒ Object
Split the content of a brace group at the commas which are not inside another group.
-
#take_dir_watcher_rotation_request ⇒ Object
Consume the rotation request before reading files, so a request arriving during that read remains pending for the next refresh.
-
#time_dependent_component_index(path, expanded) ⇒ Object
The index of the first component of the directory part of a path whose value depends on the current time, or the number of the components when none of them does.
- #trigger_refresh_watchers ⇒ Object
-
#unwatch_removed_inodes(target_paths_hash) ⇒ Object
When using @follow_inodes, need this to unwatch the rotated old inode when it disappears.
-
#update_dir_watchers(rotate: false) ⇒ Object
Attach the stat watchers of the directories which are needed now and detach the ones which are not needed anymore.
-
#use_glob?(path) ⇒ Boolean
Brace alternatives conflict with the default ',' path delimiter; configure rejects that combination with glob_policy :always.
-
#watch_dirs_of_path(path, glob, time_dependent_from) ⇒ Object
Walk one path after brace expansion, using the parent-selection rules above.
Instance Method Details
#attach_dir_watcher(dir) ⇒ Object
679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 679 def attach_dir_watcher(dir) watcher = StatWatcher.new(dir, log) { on_dir_changed(dir) } @dir_watchers[dir] = watcher event_loop_attach(watcher) log.debug { "in_tail: watching directory #{dir} to detect new files" } rescue SystemCallError => e # Failing to watch a directory only means that the new files in it are # detected by refresh_interval, like before. @dir_watchers.delete(dir) log.debug { "in_tail: failed to watch directory #{dir}: #{e}" } end |
#brace_group(pattern) ⇒ Object
Return the positions of the first unescaped 'and its closing '', or nil if there is no complete group. In that case the caller leaves the pattern unchanged for subsequent path/glob processing.
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 546 def brace_group(pattern) start = nil depth = 0 index = 0 while index < pattern.length char = pattern[index] if char == '\\' index += 2 next end if char == '{' start ||= index depth += 1 elsif char == '}' && start depth -= 1 return [start, index] if depth == 0 end index += 1 end nil end |
#detach_dir_watcher(dir) ⇒ Object
691 692 693 694 695 696 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 691 def detach_dir_watcher(dir) watcher = @dir_watchers.delete(dir) return unless watcher event_loop_detach(watcher) log.debug { "in_tail: stop watching directory #{dir}" } end |
#detach_dir_watchers ⇒ Object
698 699 700 701 702 703 704 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 698 def detach_dir_watchers @dir_watchers_mutex.synchronize do @dir_watchers.keys.each do |dir| detach_dir_watcher(dir) end end end |
#detach_refresh_timer ⇒ Object
306 307 308 309 310 311 312 313 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 306 def detach_refresh_timer timer = nil @refresh_request_mutex.synchronize do timer = @refresh_timer @refresh_timer = nil end event_loop_detach(timer) if timer end |
#dir_path_parts(dir_path) ⇒ Object
Split a directory path into the root directory it starts from and the
components below it, so that the path can be walked from its topmost
directory: /base/logs' gives /' and [base', logs'], and C:/base' gives C:/' and [`base'].
Resolve relative paths from the working directory, like #expand_paths_raw. Preserve `..' until lookup so the platform decides its meaning. On Unix it refers to a symbolic link target's parent, unlike expand_path.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 455 def dir_path_parts(dir_path) components = path_components(dir_path) if File::ALT_SEPARATOR && components[0, 2] == ['', ''] && components.size >= 4 # A UNC share is the root; walking from '/' would use the current # drive and lose the server/share portion of the path. ["//#{components[2]}/#{components[3]}/", components.drop(4)] elsif components.empty? # The path is a root directory itself, e.g. the directory of # `/review.log', and there is no component to walk below it. [File.(dir_path), []] elsif components.first == '' components.shift [File.(File::SEPARATOR), components] elsif File::ALT_SEPARATOR && components.first.match?(/\A[a-zA-Z]:\z/) [components.shift + File::SEPARATOR, components] else dir_path_parts(File.join(Dir.pwd, dir_path)) end end |
#existence_path ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 157 def existence_path hash = {} @tails.each {|path, tw| if @follow_inodes hash[tw.ino] = TargetInfo.new(tw.path, tw.ino) else hash[tw.path] = TargetInfo.new(tw.path, tw.ino) end } hash end |
#existing_dirs(parent, component, glob) ⇒ Object
The existing directories matching one component of a path pattern inside one of the directories matched by the components above it. The component is expanded as a glob only when the path it comes from is expanded by Dir.glob in #expand_paths_raw: otherwise its glob characters are parts of the directory name. Even a component without wildcard characters must go through Dir.glob when glob is enabled, because backslashes escape characters there. Yield the parent of each matched symbolic link so that replacing the link can also be detected.
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 407 def existing_dirs(parent, component, glob) dirs = if !glob dir = File.join(parent, component) File.directory?(dir) ? [dir] : [] elsif component == GLOB_STAR # `**' stands for no directory at all or for directories at any # depth. Dir.glob takes it recursively only when it is followed # by a separator: `dir/**' gives the same as `dir/*' there. Dir.glob(File.join(literal_glob_pattern(parent), GLOB_STAR) + File::SEPARATOR) else Dir.glob(File.join(literal_glob_pattern(parent), component)) end dirs.filter_map { |dir| next unless File.directory?(dir) if block_given? && File.symlink?(dir.chomp(File::SEPARATOR)) yield File.dirname(dir.chomp(File::SEPARATOR)) end dir } end |
#expand_brace_patterns(pattern, warning_key: pattern) ⇒ Object
Expand the brace groups of a path pattern into the paths they stand for,
the same way Dir.glob does: {a,b}' stands for a' and b', the groups can be nested, and a group may contain a separator. The directories of /base/Fluent::Plugin::TailInput::DirWatcher.a/xa/x.log,b/xa/x.log,b/x.log' are /base/a' and /base/b', which
File.dirname can't give because it cuts the path inside the group.
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 504 def (pattern, warning_key: pattern) patterns = [] # One extra result distinguishes truncation from an exact fit. (pattern, patterns, limit: MAX_DIR_PATTERNS + 1) if patterns.size > MAX_DIR_PATTERNS patterns.pop if @truncated_dir_patterns[warning_key] log.debug { "Directory watch pattern expansion remains limited to #{MAX_DIR_PATTERNS}" + " paths for `#{pattern}'" } else log.warn( "Too many paths expanded from the braces of `#{pattern}'." + " Only the first #{MAX_DIR_PATTERNS} paths are considered for directory watching;" + " remaining paths rely on refresh_interval (#{@refresh_interval}s)" ) end @truncated_dir_patterns[warning_key] = true else @truncated_dir_patterns.delete(warning_key) end patterns end |
#expand_brace_patterns_into(pattern, patterns, limit: MAX_DIR_PATTERNS) ⇒ Object
529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 529 def (pattern, patterns, limit: MAX_DIR_PATTERNS) return if patterns.size >= limit start, stop = brace_group(pattern) unless start patterns << pattern return end head = pattern[0...start] tail = pattern[(stop + 1)..] split_brace_alternatives(pattern[(start + 1)...stop]).each { |alternative| (head + alternative + tail, patterns, limit: limit) } end |
#expand_paths ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 135 def # filter out non existing files, so in case pattern is without '*' we don't do unnecessary work hash = {} .select { |path| File.exist?(path) }.each { |path| # Even we just checked for existence, there is a race condition here as # of which stat() might fail with ENOENT. See #3224. begin target_info = TargetInfo.new(path, Fluent::FileWrapper.stat(path).ino) if @follow_inodes hash[target_info.ino] = target_info else hash[target_info.path] = target_info end rescue Errno::ENOENT, Errno::EACCES => e log.warn "expand_paths: stat() for #{path} failed with #{e.class.name}. Skip file." end } hash end |
#expand_paths_raw ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 86 def date = Fluent::EventTime.now paths = [] @paths.each { |path| path = if @path_timezone @path_formatters[path].call(date) else date.to_time.strftime(path) end if use_glob?(path) paths += Dir.glob(path).select { |p| begin is_file = !File.directory?(p) if (File.readable?(p) || have_read_capability?) && is_file if @limit_recently_modified && File.mtime(p) < (date.to_time - @limit_recently_modified) false else true end else if is_file unless @ignore_list.include?(p) log.warn "#{p} unreadable. It is excluded and would be examined next time." @ignore_list << p if @ignore_repeated_permission_error end end false end rescue Errno::ENOENT, Errno::EACCES log.debug { "#{p} is missing after refresh file list" } false end } else # When file is not created yet, Dir.glob returns an empty array. So just add when path is static. paths << path end } excluded = @exclude_path.map { |path| path = if @path_timezone @exclude_path_formatters[path].call(date) else date.to_time.strftime(path) end use_glob?(path) ? Dir.glob(path) : path }.flatten.uniq paths - excluded end |
#expand_watch_dirs ⇒ Object
Derive watch directories from path patterns, not existing files, so files created later can also be discovered.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 317 def date = Fluent::EventTime.now dirs_by_path = @paths.map { |path| = if @path_timezone @path_formatters[path].call(date) else date.to_time.strftime(path) end # Whether the path is expanded as a glob is decided on the whole path, # exactly as #expand_paths_raw does. Deciding it on the directory part # only would take `/base/[a]/*.log' as a literal directory name while # #expand_paths_raw matches it as a wildcard. (path, , use_glob?()) } interleave_watch_dirs(dirs_by_path) end |
#expand_watch_dirs_of_pattern(path, expanded, glob) ⇒ Object
The directories to watch for one path pattern. path' is the pattern as it is configured and expanded' is the pattern with the strftime
directives of the current time replaced.
Walk existing directories one component at a time, separately for each parent. Watch the final directories where files can be created, and parents where another matching directory can appear. For wildcard or time-dependent components, keep all parents; for literal components, keep only parents where that component is missing.
For example, /base/*/logs/*.log' with /base/one/logs' and
/base/two' existing gives /base', to notice a new directory matching
*', /base/one/logs', the directory of the files, and /base/two', to notice the creation of its own logs'.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 348 def (path, , glob) # Expand braces before File.dirname; see #expand_brace_patterns. patterns = glob ? (, warning_key: path) : [] configured_patterns = [] if glob (path, configured_patterns) else configured_patterns << path end patterns.each_with_index.flat_map { |pattern, index| # Brace alternatives can have different depths. Compare each # alternative with its own unformatted path, not the unsplit group. time_dependent_from = time_dependent_component_index(configured_patterns[index] || path, pattern) watch_dirs_of_path(pattern, glob, time_dependent_from) } end |
#extended_glob_pattern(path) ⇒ Object
70 71 72 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 70 def extended_glob_pattern(path) path.include?('*') || path.include?('?') || /\[.*\]/.match?(path) end |
#initialize_dir_watchers ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 53 def initialize_dir_watchers @dir_watchers = {} @dir_watcher_offset = 0 @dir_watcher_start_dir = nil @dir_watcher_limit_exceeded = false @truncated_dir_patterns = {} # The event loop schedules refreshes; shutdown detaches the pending # timer from another thread. Protect only this scheduling state. @refresh_request_mutex = Mutex.new @refresh_timer = nil @rotate_dir_watchers = false # Directory watcher updates and shutdown detachment share this lock. # Do not hold it while refreshing files: initial reads can take a long # time, and shutdown must be able to signal the IO handlers promptly. @dir_watchers_mutex = Mutex.new end |
#interleave_watch_dirs(dirs_by_path) ⇒ Object
Interleave candidates so a large glob cannot monopolize the watcher limit and leave later paths entirely unwatched.
603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 603 def interleave_watch_dirs(dirs_by_path) # Deduplicate across paths only after interleaving. Otherwise a static # path already included late in a large glob loses its own turn. groups = dirs_by_path.map(&:uniq) dirs = [] until groups.all?(&:empty?) groups.each { |group| dirs << group.shift unless group.empty? } end dirs.uniq end |
#limit_dir_watchers(dirs, rotate:) ⇒ Object
Take a circular window of the interleaved candidates. Advance only on periodic refreshes, not on directory events. Keep the window's starting directory when candidates change, or fall back to its previous index if that directory disappeared. Called under @dir_watchers_mutex.
621 622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 621 def limit_dir_watchers(dirs, rotate:) if dirs.size <= @dir_watcher_limit @dir_watcher_offset = 0 @dir_watcher_start_dir = dirs.first return dirs end offset = dirs.index(@dir_watcher_start_dir) || @dir_watcher_offset offset += @dir_watcher_limit if rotate @dir_watcher_offset = offset % dirs.size @dir_watcher_start_dir = dirs[@dir_watcher_offset] dirs.rotate(@dir_watcher_offset).first(@dir_watcher_limit) end |
#literal_glob_pattern(path) ⇒ Object
Escape glob characters in matched directory names so later components do not reinterpret those names as patterns.
430 431 432 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 430 def literal_glob_pattern(path) path.gsub(/[*?{}\[\]\\]/) { |char| "\\#{char}" } end |
#on_dir_changed(dir) ⇒ Object
706 707 708 709 710 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 706 def on_dir_changed(dir) # The directory may have been removed. #update_dir_watchers will drop it # from the watched list at the next refresh. request_refresh_watchers("change of #{dir}", delay: DIR_WATCH_DEBOUNCE_INTERVAL) end |
#path_components(path) ⇒ Object
Normalize Windows file system separators before splitting. This is not glob parsing: Dir.glob patterns use '/' and treat backslashes as escapes.
477 478 479 480 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 477 def path_components(path) path = path.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR path.split(File::SEPARATOR) end |
#real_path(dir) ⇒ Object
Resolve symbolic links and `..' through the file system and return a canonical path for deduplication, or nil if resolution fails. Callers check whether the path is a directory.
437 438 439 440 441 442 443 444 445 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 437 def real_path(dir) # Win32 normalizes '..' before following symbolic links. These are # already matched directory names, not unexpanded glob patterns. dir = File.(dir) if Fluent.windows? File.realpath(dir) rescue SystemCallError # A matched path may disappear or become inaccessible before resolution. nil end |
#refresh_watchers ⇒ Object
Rotated files matching path can be discovered as new files and reread. follow_inodes avoids duplicate watchers for the same inode after rename, but not for a copytruncate archive, which has a different inode. Exclude archives from path to avoid rediscovering them. Directory-triggered refreshes can discover them sooner than periodic refreshes.
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 174 def refresh_watchers @watcher_refresh_monitor.synchronize do return if before_shutdown? || @shutdown_start_time rotate = take_dir_watcher_rotation_request # Arm directory watchers before scanning so changes during initial # reads cannot become part of a new watcher's baseline. update_dir_watchers(rotate: rotate) refresh_watchers_raw end end |
#refresh_watchers_on_timer ⇒ Object
Without directory events, keep the ordinary periodic refresh path.
256 257 258 259 260 261 262 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 256 def refresh_watchers_on_timer if @enable_dir_watcher request_refresh_watchers(:refresh_interval) else refresh_watchers end end |
#refresh_watchers_raw ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 186 def refresh_watchers_raw target_paths_hash = existence_paths_hash = existence_path log.debug { target_paths_str = target_paths_hash.collect { |key, target_info| target_info.path }.join(",") existence_paths_str = existence_paths_hash.collect { |key, target_info| target_info.path }.join(",") "tailing paths: target = #{target_paths_str} | existing = #{existence_paths_str}" } removed_hash = existence_paths_hash.reject {|key, value| target_paths_hash.key?(key)} added_hash = target_paths_hash.reject {|key, value| existence_paths_hash.key?(key)} if @follow_inodes # A TailWatcher waiting for `rotate_wait` still reads its inode, so do not start a second one for it. @tails_rotate_wait.each_value do |v| target = added_hash.delete(v[:ino]) log.debug { "skip #{target.path} (inode: #{target.ino}) because a watcher waiting rotate_wait still reads it" } if target end end # If an existing TailWatcher already follows a target path with the different inode, # it means that the TailWatcher following the rotated file still exists. In this case, # `refresh_watcher` can't start the new TailWatcher for the new current file. So, we # should output a warning log in order to prevent silent collection stops. # (Such as https://github.com/fluent/fluentd/pull/4327) # (Usually, such a TailWatcher should be removed from `@tails` in `update_watcher`.) # (The similar warning may work for `@follow_inodes true` too. Just limiting the case # to suppress the impact to existing logics.) unless @follow_inodes target_paths_hash.each do |path, target| next unless @tails.key?(path) # We can't use `existence_paths_hash[path].ino` because it is from `TailWatcher.ino`, # which is very unstable parameter. (It can be `nil` or old). # So, we need to use `TailWatcher.pe.read_inode`. existing_watcher_inode = @tails[path].pe.read_inode if existing_watcher_inode != target.ino log.warn "Could not follow a file (inode: #{target.ino}) because an existing watcher for that filepath follows a different inode: #{existing_watcher_inode} (e.g. keeps watching a already rotated file). If you keep getting this message, please restart Fluentd.", filepath: target.path end end end stop_watchers(removed_hash, unwatched: !@follow_inodes) unless removed_hash.empty? unwatch_removed_inodes(target_paths_hash) if @follow_inodes start_watchers(added_hash) unless added_hash.empty? @metrics.tracked.set(@tails.size) @startup = false if @startup end |
#request_refresh_watchers(trigger, delay: nil) ⇒ Object
Merge directory changes and periodic requests into a pending refresh. With delay, schedule a one-shot timer without extending an existing one.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 278 def request_refresh_watchers(trigger, delay: nil) log.debug { "in_tail: #{trigger} requested to refresh watchers" } refresh_now = false @refresh_request_mutex.synchronize do # Detaching a watcher cannot cancel an already dispatched callback. return if before_shutdown? || @shutdown_start_time # Preserve periodic rotation when it joins a directory-triggered refresh. @rotate_dir_watchers = true if trigger == :refresh_interval if @refresh_timer log.debug { "in_tail: merge the request by #{trigger} into the scheduled refresh" } elsif delay @refresh_timer = TimerTrigger.new(delay, log, repeat: false) { trigger_refresh_watchers } event_loop_attach(@refresh_timer) else # Do not hold the scheduling lock while reading files. refresh_now = true end end trigger_refresh_watchers if refresh_now end |
#split_brace_alternatives(group) ⇒ Object
Split the content of a brace group at the commas which are not inside another group.
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 570 def split_brace_alternatives(group) alternatives = [] alternative = +'' depth = 0 index = 0 while index < group.length char = group[index] if char == '\\' && index + 1 < group.length alternative << char << group[index + 1] index += 2 next end case char when '{' depth += 1 when '}' depth -= 1 when ',' if depth == 0 alternatives << alternative alternative = +'' index += 1 next end end alternative << char index += 1 end alternatives << alternative end |
#take_dir_watcher_rotation_request ⇒ Object
Consume the rotation request before reading files, so a request arriving during that read remains pending for the next refresh.
266 267 268 269 270 271 272 273 274 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 266 def take_dir_watcher_rotation_request return false unless @enable_dir_watcher @refresh_request_mutex.synchronize do requested = @rotate_dir_watchers @rotate_dir_watchers = false requested end end |
#time_dependent_component_index(path, expanded) ⇒ Object
The index of the first component of the directory part of a path whose value depends on the current time, or the number of the components when none of them does. Such a component may name another directory later, e.g. the directory of the next month of a `%Y%m' path, and that directory may be created inside one of the directories above it. If it already exists, the time boundary itself generates no directory event; periodic refreshes select the newly current directory.
489 490 491 492 493 494 495 496 497 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 489 def time_dependent_component_index(path, ) configured = dir_path_parts(File.dirname(path))[1] current = dir_path_parts(File.dirname())[1] index = 0 while index < configured.size && index < current.size && configured[index] == current[index] index += 1 end index end |
#trigger_refresh_watchers ⇒ Object
300 301 302 303 304 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 300 def trigger_refresh_watchers # Detach the pending timer before refreshing, including immediate refreshes. detach_refresh_timer refresh_watchers end |
#unwatch_removed_inodes(target_paths_hash) ⇒ Object
When using @follow_inodes, need this to unwatch the rotated old inode when it disappears.
After update_watcher detaches an old TailWatcher, the inode is lost from the @tails.
So that inode can't be contained in removed_hash, and can't be unwatched by stop_watchers.
Keep inodes still read by watchers waiting for rotate_wait or worker completion. Compaction only updates entries remaining in the position file, so removing one early would let its watcher write into another file's line.
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 243 def unwatch_removed_inodes(target_paths_hash) return unless @pf draining_hash = @tails_rotate_wait.to_h { |tw, v| [v[:ino], TargetInfo.new(tw.path, v[:ino])] } @worker_detached_watchers&.each do |tw| draining_hash[tw.ino] = TargetInfo.new(tw.path, tw.ino) end @worker_deferred_unwatch&.each_value do |target| draining_hash[target.ino] = target end @pf.unwatch_removed_targets(draining_hash.merge(target_paths_hash)) end |
#update_dir_watchers(rotate: false) ⇒ Object
Attach the stat watchers of the directories which are needed now and detach the ones which are not needed anymore.
Recompute and watch directories before discovering files, so changes during discovery or synchronous initial reads can request another refresh.
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 640 def update_dir_watchers(rotate: false) return unless @enable_dir_watcher # Do not add directory watchers once shutdown has begun. return if before_shutdown? || @shutdown_start_time dirs = @dir_watchers_mutex.synchronize do # Shutdown may have detached the watchers during path expansion. return if before_shutdown? || @shutdown_start_time over_limit = dirs.size > @dir_watcher_limit if over_limit if @dir_watcher_limit_exceeded log.debug { "Directory watcher limit remains exceeded: #{dirs.size} candidates," + " watching #{@dir_watcher_limit}" } else log.warn( "Too many directories to watch: #{dirs.size}." + " Watching #{@dir_watcher_limit} of them, rotating at each periodic refresh," + " so new files may not be detected until refresh_interval (#{@refresh_interval}s) passes" ) end end @dir_watcher_limit_exceeded = over_limit dirs = limit_dir_watchers(dirs, rotate: rotate) # Release the old window before attaching the new one so rotation # does not temporarily exceed the configured watcher limit. (@dir_watchers.keys - dirs).each do |dir| detach_dir_watcher(dir) end dirs.each do |dir| attach_dir_watcher(dir) unless @dir_watchers.key?(dir) end end end |
#use_glob?(path) ⇒ Boolean
Brace alternatives conflict with the default ',' path delimiter; configure rejects that combination with glob_policy :always.
76 77 78 79 80 81 82 83 84 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 76 def use_glob?(path) if @glob_policy == :always extended_glob_pattern(path) || /\{.*,.*\}/.match?(path) elsif @glob_policy == :extended extended_glob_pattern(path) elsif @glob_policy == :backward_compatible path.include?('*') end end |
#watch_dirs_of_path(path, glob, time_dependent_from) ⇒ Object
Walk one path after brace expansion, using the parent-selection rules above.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/fluent/plugin/in_tail/dir_watcher.rb', line 366 def watch_dirs_of_path(path, glob, time_dependent_from) root, components = dir_path_parts(File.dirname(path)) dirs = [] # The existing directories which match the prefix walked so far. parents = [root] components.each_with_index do |component, i| matched = [] missing = [] parents.each { |parent| found = existing_dirs(parent, component, glob) { |link_parent| # realpath follows a link, but replacing the link changes its # parent, not the old target directory. dirs << link_parent } if found.empty? missing << parent else matched.concat(found) end } if i >= time_dependent_from || (glob && WILDCARD_CHARS.match?(component)) dirs.concat(parents) else dirs.concat(missing) end parents = matched break if parents.empty? end # Keep unresolved prefixes while walking: resolving a symbolic link # before a later '..' can change Windows path lookup semantics. dirs.concat(parents).filter_map { |dir| real_path(dir) } end |