From 2a5b2d15a4506458bf0b9fc5985a08a43dbc0688 Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Thu, 6 Jul 2023 23:03:10 +0900 Subject: [PATCH] in_tail: Add debug log for pos file compaction These logs would be useful to check the status of pos file on startup. [debug]: ... Compacted entries: [...] [debug]: ... Remove missing entries. existing_targets=[...] entries_after_removing=[...] Signed-off-by: Daijiro Fukuda --- lib/fluent/plugin/in_tail/position_file.rb | 26 +++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/fluent/plugin/in_tail/position_file.rb b/lib/fluent/plugin/in_tail/position_file.rb index 2d58da9e0e..bc87697431 100644 --- a/lib/fluent/plugin/in_tail/position_file.rb +++ b/lib/fluent/plugin/in_tail/position_file.rb @@ -96,6 +96,7 @@ def try_compact end entries = fetch_compacted_entries + @logger&.debug "Compacted entries: ", entries.keys @file_mutex.synchronize do if last_modified == @file.mtime && size == @file.size @@ -119,15 +120,23 @@ def try_compact def compact(existing_targets = nil) @file_mutex.synchronize do - entries = fetch_compacted_entries(existing_targets).values.map(&:to_entry_fmt) + entries = fetch_compacted_entries + @logger&.debug "Compacted entries: ", entries.keys + + if existing_targets + entries = remove_deleted_files_entries(entries, existing_targets) + @logger&.debug "Remove missing entries.", + existing_targets: existing_targets.keys, + entries_after_removing: entries.keys + end @file.pos = 0 @file.truncate(0) - @file.write(entries.join) + @file.write(entries.values.map(&:to_entry_fmt).join) end end - def fetch_compacted_entries(existing_targets = nil) + def fetch_compacted_entries entries = {} @file.pos = 0 @@ -156,18 +165,13 @@ def fetch_compacted_entries(existing_targets = nil) end end - entries = remove_deleted_files_entries(entries, existing_targets) entries end def remove_deleted_files_entries(existent_entries, existing_targets) - if existing_targets - existent_entries.select { |path_or_ino| - existing_targets.key?(path_or_ino) - } - else - existent_entries - end + existent_entries.select { |path_or_ino| + existing_targets.key?(path_or_ino) + } end end