From 1f82b449508d156c1841ae8fe78bb17c69c4803a Mon Sep 17 00:00:00 2001 From: cliffordsun Date: Fri, 10 May 2019 17:28:43 +0800 Subject: [PATCH 1/3] bug fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1,修复配置修改或重启后sincedb中记录的last_check_at被重置为当前时间的问题。 2,修复tail模式下配置修改或重启后导致当前采集的日志在rotate后重复采集的问题 --- lib/filewatch/sincedb_collection.rb | 9 ++++++++- lib/filewatch/sincedb_value.rb | 4 ++++ lib/filewatch/tail_mode/processor.rb | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/filewatch/sincedb_collection.rb b/lib/filewatch/sincedb_collection.rb index 78c4e122..d5e76038 100644 --- a/lib/filewatch/sincedb_collection.rb +++ b/lib/filewatch/sincedb_collection.rb @@ -191,7 +191,14 @@ def flush_at_interval def handle_association(sincedb_value, watched_file) watched_file.update_bytes_read(sincedb_value.position) - sincedb_value.set_watched_file(watched_file) + if watched_file.all_read? + # avoid the last_changed_at of sincedb_value which record the watched file has bean read over being changed when reload sincedb + sincedb_value.set_watched_file_without_touch(watched_file) + logger.trace("handle_association call set_watched_file_without_touch") + else + sincedb_value.set_watched_file(watched_file) + logger.trace("handle_association call set_watched_file") + end watched_file.initial_completed if watched_file.all_read? watched_file.ignore diff --git a/lib/filewatch/sincedb_value.rb b/lib/filewatch/sincedb_value.rb index 56ac4840..bd2f3992 100644 --- a/lib/filewatch/sincedb_value.rb +++ b/lib/filewatch/sincedb_value.rb @@ -47,6 +47,10 @@ def set_watched_file(watched_file) @watched_file = watched_file end + def set_watched_file_without_touch(watched_file) + @watched_file = watched_file + end + def touch @last_changed_at = Time.now.to_f end diff --git a/lib/filewatch/tail_mode/processor.rb b/lib/filewatch/tail_mode/processor.rb index d363806b..05803ed2 100644 --- a/lib/filewatch/tail_mode/processor.rb +++ b/lib/filewatch/tail_mode/processor.rb @@ -195,6 +195,9 @@ def process_rotation_in_progress(watched_files) potential_sdb_value.set_watched_file(watched_file) end end + # Clean the path_in_sincedb because a file with the same name appears, this file must have been renamed + # Fix the duplicating collection after reloading sincedb(logstash restart or change logstash.conf) + sdb_value.add_path_in_sincedb(nil) unless sdb_value.nil? logger.trace("---------- >>>> Rotation In Progress: after handling rotation", "this watched_file details" => watched_file.details, "sincedb_value" => (potential_sdb_value || sdb_value)) end end From 109c36935099357cc02db82a8b41dcf3fb72191e Mon Sep 17 00:00:00 2001 From: cliffordsun Date: Tue, 14 May 2019 16:45:38 +0800 Subject: [PATCH 2/3] Fixed issue where logs that are at the end of file were being lost when file rotated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复当文件发生滚动时部分日志会丢失的问题 --- lib/filewatch/watched_file.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/filewatch/watched_file.rb b/lib/filewatch/watched_file.rb index 13a69338..5c030fdd 100644 --- a/lib/filewatch/watched_file.rb +++ b/lib/filewatch/watched_file.rb @@ -105,6 +105,12 @@ def restat if rotation_detected? # switch to new state now rotation_in_progress + # update the original file's size, there may be new data written after last read + unless @file.nil? + original_stat = @file.stat + @size = original_stat.size + update_bytes_unread + end else @size = @stat.size update_bytes_unread From 39da3b6befed6686223a5526f304ce961aecfd28 Mon Sep 17 00:00:00 2001 From: cliffordsun Date: Mon, 20 May 2019 18:20:16 +0800 Subject: [PATCH 3/3] change the condition of stat the opened file --- lib/filewatch/watched_file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filewatch/watched_file.rb b/lib/filewatch/watched_file.rb index 5c030fdd..def646bd 100644 --- a/lib/filewatch/watched_file.rb +++ b/lib/filewatch/watched_file.rb @@ -106,7 +106,7 @@ def restat # switch to new state now rotation_in_progress # update the original file's size, there may be new data written after last read - unless @file.nil? + if file_open? original_stat = @file.stat @size = original_stat.size update_bytes_unread