diff --git a/cmake/glog.cmake b/cmake/glog.cmake index 5e9c12687e3..50b892fe42f 100644 --- a/cmake/glog.cmake +++ b/cmake/glog.cmake @@ -20,8 +20,8 @@ include_guard() include(cmake/utils.cmake) FetchContent_DeclareGitHubWithMirror(glog - google/glog v0.6.0 - MD5=1b246d4d0e8a011d33e0813b256198ef + google/glog v0.7.1 + MD5=fa30180d4284c454bdd324ad3baf7f5f ) FetchContent_MakeAvailableWithArgs(glog diff --git a/src/cli/main.cc b/src/cli/main.cc index 2ce5f013aae..9eeab518c25 100644 --- a/src/cli/main.cc +++ b/src/cli/main.cc @@ -31,6 +31,7 @@ #include #include "daemon_util.h" +#include "glog/log_severity.h" #include "io_util.h" #include "pid_util.h" #include "scope_exit.h" @@ -100,7 +101,7 @@ static void InitGoogleLog(const Config *config) { if (util::EqualICase(config->log_dir, "stdout")) { for (int level = google::INFO; level <= google::FATAL; level++) { - google::SetLogDestination(level, ""); + google::SetLogDestination(static_cast(level), ""); } FLAGS_stderrthreshold = google::ERROR; FLAGS_logtostdout = true; @@ -108,7 +109,7 @@ static void InitGoogleLog(const Config *config) { } else { FLAGS_log_dir = config->log_dir + "/"; if (config->log_retention_days != -1) { - google::EnableLogCleaner(config->log_retention_days); + google::EnableLogCleaner(std::chrono::hours(24) * config->log_retention_days); } } } diff --git a/src/config/config.cc b/src/config/config.cc index c2491844253..1d74a88706d 100644 --- a/src/config/config.cc +++ b/src/config/config.cc @@ -106,7 +106,7 @@ std::string TrimRocksDbPrefix(std::string s) { } Status SetRocksdbCompression(Server *srv, const rocksdb::CompressionType compression, - const int compression_start_level) { + const size_t compression_start_level) { if (!srv) return Status::OK(); std::string compression_option; for (auto &option : engine::CompressionOptions) { @@ -128,7 +128,7 @@ Status SetRocksdbCompression(Server *srv, const rocksdb::CompressionType compres for (int i = 0; i < compression_start_level; i++) { compression_per_level_builder.emplace_back("kNoCompression"); } - for (int i = compression_start_level; i < KVROCKS_MAX_LSM_LEVEL; i++) { + for (size_t i = compression_start_level; i < KVROCKS_MAX_LSM_LEVEL; i++) { compression_per_level_builder.emplace_back(compression_option); } const std::string compression_per_level = util::StringJoin( @@ -600,7 +600,7 @@ void Config::initFieldCallback() { } if (log_retention_days != -1) { - google::EnableLogCleaner(log_retention_days); + google::EnableLogCleaner(std::chrono::hours(24) * log_retention_days); } else { google::DisableLogCleaner(); } diff --git a/tests/gocase/unit/log/logclean_test.go b/tests/gocase/unit/log/logclean_test.go index b3d6ac80d52..e4f073638bf 100644 --- a/tests/gocase/unit/log/logclean_test.go +++ b/tests/gocase/unit/log/logclean_test.go @@ -21,47 +21,58 @@ package log import ( "os" + "strings" "testing" - "time" "github.com/apache/kvrocks/tests/gocase/util" "github.com/stretchr/testify/require" ) -func TestLogClean(t *testing.T) { +const infoLogFileNamePart = ".INFO." + +func TestInfoLogClean(t *testing.T) { logDir := "/tmp/kvrocks/logfile" require.NoError(t, os.RemoveAll(logDir)) require.NoError(t, os.MkdirAll(logDir, os.ModePerm)) srv := util.StartServer(t, map[string]string{ "log-dir": logDir, - "log-retention-days": "0", + "log-retention-days": "-1", + }) + srv.CloseWithoutCleanup() + + logInfoFiles := getInfoLogFilesInDir(t, logDir) + require.NotEmpty(t, logInfoFiles) + + srv = util.StartServer(t, map[string]string{ + "log-dir": logDir, + "log-retention-days": "0", // all previous INFO level logs should be immediately removed }) defer srv.Close() - files1, err := os.ReadDir(logDir) + logInfoFiles = getInfoLogFilesInDir(t, logDir) + require.Empty(t, logInfoFiles) // the log directory doesn't contain any INFO level log files +} + +func getInfoLogFilesInDir(t *testing.T, dir string) []os.DirEntry { + t.Helper() + + files, err := os.ReadDir(dir) require.NoError(t, err) - if len(files1) == 0 { - return - } - require.Eventually(t, func() bool { - srv.Restart() - - files2, err := os.ReadDir(logDir) - require.NoError(t, err) - for _, f1 := range files1 { - fileExists := false - for _, f2 := range files2 { - if f1.Name() == f2.Name() { - fileExists = true - break - } - } - // If the file does not exist, it means the file has been cleaned - if !fileExists { - return true - } + + infoLogFiles := make([]os.DirEntry, 0, len(files)) + + for _, f := range files { + if !f.Type().IsRegular() { + continue + } + + if !strings.Contains(f.Name(), infoLogFileNamePart) { + continue } - return false - }, 10*time.Second, 200*time.Millisecond) + + infoLogFiles = append(infoLogFiles, f) + } + + return infoLogFiles } diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index 88a6211b993..2b60100c73e 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -114,6 +114,10 @@ func (s *KvrocksServer) Close() { s.close(false) } +func (s *KvrocksServer) CloseWithoutCleanup() { + s.close(true) +} + func (s *KvrocksServer) close(keepDir bool) { require.NoError(s.t, s.cmd.Process.Signal(syscall.SIGTERM)) f := func(err error) { require.NoError(s.t, err) }