trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zw...@apache.org
Subject [trafficserver] 03/07: Fixing rolled log cleanup parsing for <blah>.log.<blah> files.
Date Tue, 10 Dec 2019 23:02:29 GMT
This is an automated email from the ASF dual-hosted git repository.

zwoop pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 6ab28a55a8dd80e296135de0b34b5c80a0702ea3
Author: bneradt <bneradt@verizonmedia.com>
AuthorDate: Fri Nov 15 00:15:42 2019 +0000

    Fixing rolled log cleanup parsing for <blah>.log.<blah> files.
    
    (cherry picked from commit 84f0219058488b1634190b4a741720964806dfb3)
---
 proxy/logging/LogConfig.cc                 | 12 +++-------
 proxy/logging/LogUtils.cc                  | 35 ++++++++++++++++++++++++++++++
 proxy/logging/LogUtils.h                   | 17 +++++++++++++++
 proxy/logging/unit-tests/test_LogUtils2.cc | 23 ++++++++++++++++++++
 4 files changed, 78 insertions(+), 9 deletions(-)

diff --git a/proxy/logging/LogConfig.cc b/proxy/logging/LogConfig.cc
index 6500bc1..efc6011 100644
--- a/proxy/logging/LogConfig.cc
+++ b/proxy/logging/LogConfig.cc
@@ -616,15 +616,9 @@ LogConfig::update_space_used()
         //
         // then check if the candidate belongs to any given log type
         //
-        ts::TextView type_name(entry->d_name, strlen(entry->d_name));
-        // A rolled log will look something like:
-        // squid.log_some.hostname.com.20191029.18h15m02s-20191029.18h30m02s.old
-        //
-        // The following logic cuts things back to original unrolled file which
-        // is the key into the deleting_info map (squid.log in the above example).
-        auto suffix = type_name;
-        type_name.remove_suffix(suffix.remove_prefix(suffix.find('.') + 1).remove_prefix(suffix.find('_')).size());
-        auto iter = deleting_info.find(type_name);
+        ts::TextView rolled_filename(entry->d_name, strlen(entry->d_name));
+        auto type_name = LogUtils::get_unrolled_filename(rolled_filename);
+        auto iter      = deleting_info.find(type_name);
         if (iter == deleting_info.end()) {
           // We won't delete the log if its name doesn't match any give type.
           continue;
diff --git a/proxy/logging/LogUtils.cc b/proxy/logging/LogUtils.cc
index 95e3852..2a314d7 100644
--- a/proxy/logging/LogUtils.cc
+++ b/proxy/logging/LogUtils.cc
@@ -478,6 +478,41 @@ LogUtils::seconds_to_next_roll(time_t time_now, int rolling_offset, int
rolling_
   return ((tr >= sidl ? (tr - sidl) % rolling_interval : (86400 - (sidl - tr)) % rolling_interval));
 }
 
+ts::TextView
+LogUtils::get_unrolled_filename(ts::TextView rolled_filename)
+{
+  auto unrolled_name = rolled_filename;
+
+  // A rolled log will look something like:
+  //   squid.log_some.hostname.com.20191029.18h15m02s-20191029.18h30m02s.old
+  auto suffix = rolled_filename;
+
+  suffix.remove_prefix_at('.');
+  // Using the above squid.log example, suffix now looks like:
+  //   log_some.hostname.com.20191029.18h15m02s-20191029.18h30m02s.ol
+
+  if (suffix.find('_') != std::string::npos) {
+    suffix.remove_prefix_at('_');
+    // suffix now looks like:
+    //   some.hostname.com.20191029.18h15m02s-20191029.18h30m02s.ol
+  } else if (suffix.find('.') != std::string::npos) {
+    // There was no underscore in the file after the first '.'.
+    // Some logs have the following format (without the hostname):
+    // diags.log.20191114.21h43m16s-20191114.21h43m17s.old
+
+    suffix.remove_prefix_at('.');
+    // Using the diags.log from our above example, suffix now looks like:
+    //   log.20191114.21h43m16s-20191114.21h43m17s.old
+  } else {
+    // If there isn't a '.' or an '_' after the first '.', then this
+    // doesn't look like a rolled file.
+    return rolled_filename;
+  }
+
+  // ' + 1' to remove the '_' or second '.':
+  return unrolled_name.remove_suffix(suffix.size() + 1);
+}
+
 // Checks if the file pointed to by full_filename either is a regular
 // file or a pipe and has write permission, or, if the file does not
 // exist, if the path prefix of full_filename names a directory that
diff --git a/proxy/logging/LogUtils.h b/proxy/logging/LogUtils.h
index 2860db1..ade7418 100644
--- a/proxy/logging/LogUtils.h
+++ b/proxy/logging/LogUtils.h
@@ -26,6 +26,7 @@
 
 #include "tscore/ink_platform.h"
 #include "tscore/Arena.h"
+#include <tscpp/util/TextView.h>
 
 class MIMEHdr;
 
@@ -62,6 +63,22 @@ int seconds_to_next_roll(time_t time_now, int rolling_offset, int rolling_interv
 int file_is_writeable(const char *full_filename, off_t *size_bytes = nullptr, bool *has_size_limit
= nullptr,
                       uint64_t *current_size_limit_bytes = nullptr);
 
+/** Given a rolled file, determine the unrolled filename.
+ *
+ * For example, given this:
+ *   diags.log.20191114.21h43m16s-20191114.21h43m17s.old
+ *
+ * Return this:
+ *   diags.log
+ *
+ * @param[in] rolled_filename The rolled filename from which to derive the
+ * unrolled filename.
+ *
+ * @return The unrolled filename if it looked like a rolled log file or the
+ * input filename if it didn't.
+ */
+ts::TextView get_unrolled_filename(ts::TextView rolled_filename);
+
 // Marshals header tags and values together, with a single terminating nul character.  Returns
buffer space required.  'buf' points
 // to where to put the marshaled data.  If 'buf' is null, no data is marshaled, but the function
returns the amount of space that
 // would have been used.
diff --git a/proxy/logging/unit-tests/test_LogUtils2.cc b/proxy/logging/unit-tests/test_LogUtils2.cc
index 31376b3..70ccf6f 100644
--- a/proxy/logging/unit-tests/test_LogUtils2.cc
+++ b/proxy/logging/unit-tests/test_LogUtils2.cc
@@ -130,3 +130,26 @@ void
 RecSignalManager(int, char const *, std::size_t)
 {
 }
+
+TEST_CASE("LogUtilsUnrolled", "get_unrolled_filename")
+{
+  // Rolled log inputs.
+  constexpr ts::TextView with_underscore = "squid.log_some.hostname.com.20191029.18h15m02s-20191029.18h30m02s.old";
+  REQUIRE(get_unrolled_filename(with_underscore) == "squid.log");
+
+  constexpr ts::TextView without_underscore = "diags.log.20191114.21h43m16s-20191114.21h43m17s.old";
+  REQUIRE(get_unrolled_filename(without_underscore) == "diags.log");
+
+  constexpr ts::TextView dot_file = ".log.20191114.21h43m16s-20191114.21h43m17s.old";
+  // Maybe strange, but why not?
+  REQUIRE(get_unrolled_filename(dot_file) == ".log");
+
+  // Non-rolled log inputs.
+  REQUIRE(get_unrolled_filename("") == "");
+
+  constexpr ts::TextView not_a_log = "logging.yaml";
+  REQUIRE(get_unrolled_filename(not_a_log) == not_a_log);
+
+  constexpr ts::TextView no_dot = "logging_yaml";
+  REQUIRE(get_unrolled_filename(no_dot) == no_dot);
+}


Mime
View raw message