From eed62c913f327a4d6228e4ad1f3d68120d835952 Mon Sep 17 00:00:00 2001 From: shuqiang-zheng Date: Mon, 20 May 2024 11:02:39 +0800 Subject: [PATCH] enhance(log): Modify the threshold triggering log to logLeftSpaceLimitRatio of the total disk space @formatter:off Signed-off-by: shuqiang-zheng --- blockcache/cmd.go | 2 +- cli/cli.go | 2 +- client/fuse.go | 2 +- cmd/cmd.go | 32 ++++++++++++++++---------------- deploy/deploy_cli.go | 2 +- fsck/cmd/clean.go | 2 +- libsdk/libsdk.go | 2 +- master/api_service_test.go | 2 +- metanode/multi_ver_test.go | 2 +- metanode/transaction_test.go | 2 +- preload/sdk/preloadsdk.go | 2 +- snapshot/cmd/clean.go | 2 +- util/log/log.go | 9 ++------- util/log/log_test.go | 6 +++--- util/log/rotate.go | 4 ++-- 15 files changed, 34 insertions(+), 39 deletions(-) diff --git a/blockcache/cmd.go b/blockcache/cmd.go index c704a4384..a122b073c 100644 --- a/blockcache/cmd.go +++ b/blockcache/cmd.go @@ -166,7 +166,7 @@ func main() { level = log.ErrorLevel } - _, err = log.InitLog(logDir, module, level, nil, log.DefaultLogLeftSpaceLimit) + _, err = log.InitLog(logDir, module, level, nil, log.DefaultLogLeftSpaceLimitRatio) if err != nil { err = errors.NewErrorf("Fatal: failed to init log - %v", err) fmt.Println(err) diff --git a/cli/cli.go b/cli/cli.go index 4d5df38e4..d2591892b 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -71,7 +71,7 @@ func setupCommands(cfg *cmd.Config) *cobra.Command { func main() { var err error - _, err = log.InitLog("/tmp/cfs", "cli", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimit) + _, err = log.InitLog("/tmp/cfs", "cli", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimitRatio) if err != nil { fmt.Fprintln(os.Stderr, "Error:", err) os.Exit(1) diff --git a/client/fuse.go b/client/fuse.go index a3be78f35..1a9ba1dfc 100644 --- a/client/fuse.go +++ b/client/fuse.go @@ -308,7 +308,7 @@ func main() { // use uber automaxprocs: get real cpu number to k8s pod" level := parseLogLevel(opt.Loglvl) - _, err = log.InitLog(opt.Logpath, opt.Volname, level, nil, log.DefaultLogLeftSpaceLimit) + _, err = log.InitLog(opt.Logpath, opt.Volname, level, nil, log.DefaultLogLeftSpaceLimitRatio) if err != nil { err = errors.NewErrorf("Init log dir fail: %v\n", err) fmt.Println(err) diff --git a/cmd/cmd.go b/cmd/cmd.go index 6cec49a20..9197951ab 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -48,16 +48,16 @@ import ( ) const ( - ConfigKeyRole = "role" - ConfigKeyLogDir = "logDir" - ConfigKeyLogLevel = "logLevel" - ConfigKeyLogRotateSize = "logRotateSize" - ConfigKeyLogRotateHeadRoom = "logRotateHeadRoom" - ConfigKeyProfPort = "prof" - ConfigKeyWarnLogDir = "warnLogDir" - ConfigKeyBuffersTotalLimit = "buffersTotalLimit" - ConfigKeyLogLeftSpaceLimit = "logLeftSpaceLimit" - ConfigKeyEnableLogPanicHook = "enableLogPanicHook" + ConfigKeyRole = "role" + ConfigKeyLogDir = "logDir" + ConfigKeyLogLevel = "logLevel" + ConfigKeyLogRotateSize = "logRotateSize" + ConfigKeyLogRotateHeadRoom = "logRotateHeadRoom" + ConfigKeyProfPort = "prof" + ConfigKeyWarnLogDir = "warnLogDir" + ConfigKeyBuffersTotalLimit = "buffersTotalLimit" + ConfigKeyLogLeftSpaceLimitRatio = "logLeftSpaceLimitRatio" + ConfigKeyEnableLogPanicHook = "enableLogPanicHook" ) const ( @@ -165,12 +165,12 @@ func main() { profPort := cfg.GetString(ConfigKeyProfPort) umpDatadir := cfg.GetString(ConfigKeyWarnLogDir) buffersTotalLimit := cfg.GetInt64(ConfigKeyBuffersTotalLimit) - logLeftSpaceLimitStr := cfg.GetString(ConfigKeyLogLeftSpaceLimit) - logLeftSpaceLimit, err := strconv.ParseInt(logLeftSpaceLimitStr, 10, 64) + logLeftSpaceLimitRatioStr := cfg.GetString(ConfigKeyLogLeftSpaceLimitRatio) + logLeftSpaceLimitRatio, err := strconv.ParseFloat(logLeftSpaceLimitRatioStr, 64) enableLogPanicHook := cfg.GetBool(ConfigKeyEnableLogPanicHook) - if err != nil || logLeftSpaceLimit == 0 { - log.LogErrorf("logLeftSpaceLimit is not a legal int value: %v", err.Error()) - logLeftSpaceLimit = log.DefaultLogLeftSpaceLimit + if err != nil || logLeftSpaceLimitRatio <= 0 || logLeftSpaceLimitRatio > 1.0 { + log.LogErrorf("logLeftSpaceLimitRatio is not a legal float value: %v", err.Error()) + logLeftSpaceLimitRatio = log.DefaultLogLeftSpaceLimitRatio } // Init server instance with specified role configuration. var ( @@ -231,7 +231,7 @@ func main() { if logRotateHeadRoom > 0 { rotate.SetHeadRoomMb(logRotateHeadRoom) } - _, err = log.InitLog(logDir, module, level, rotate, logLeftSpaceLimit) + _, err = log.InitLog(logDir, module, level, rotate, logLeftSpaceLimitRatio) if err != nil { err = errors.NewErrorf("Fatal: failed to init log - %v", err) fmt.Println(err) diff --git a/deploy/deploy_cli.go b/deploy/deploy_cli.go index 90b515c82..fc127bcaa 100644 --- a/deploy/deploy_cli.go +++ b/deploy/deploy_cli.go @@ -17,7 +17,7 @@ func init() { } func main() { - _, err := log.InitLog("/tmp/cfs", "deploy", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimit) + _, err := log.InitLog("/tmp/cfs", "deploy", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimitRatio) if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) log.LogFlush() diff --git a/fsck/cmd/clean.go b/fsck/cmd/clean.go index 5674656f2..df252efb7 100644 --- a/fsck/cmd/clean.go +++ b/fsck/cmd/clean.go @@ -105,7 +105,7 @@ func Clean(opt string) error { ump.InitUmp("fsck", "") - _, err := log.InitLog("fscklog", "fsck", log.InfoLevel, nil, log.DefaultLogLeftSpaceLimit) + _, err := log.InitLog("fscklog", "fsck", log.InfoLevel, nil, log.DefaultLogLeftSpaceLimitRatio) if err != nil { return fmt.Errorf("Init log failed: %v", err) } diff --git a/libsdk/libsdk.go b/libsdk/libsdk.go index 1f630862d..ffcb60349 100644 --- a/libsdk/libsdk.go +++ b/libsdk/libsdk.go @@ -1158,7 +1158,7 @@ func (c *client) start() (err error) { c.logLevel = "WARN" } level := parseLogLevel(c.logLevel) - log.InitLog(c.logDir, "libcfs", level, nil, log.DefaultLogLeftSpaceLimit) + log.InitLog(c.logDir, "libcfs", level, nil, log.DefaultLogLeftSpaceLimitRatio) stat.NewStatistic(c.logDir, "libcfs", int64(stat.DefaultStatLogSize), stat.DefaultTimeOutUs, true) } proto.InitBufferPool(int64(32768)) diff --git a/master/api_service_test.go b/master/api_service_test.go index a3de940c6..0d3b00c93 100644 --- a/master/api_service_test.go +++ b/master/api_service_test.go @@ -238,7 +238,7 @@ func createMasterServer(cfgJSON string) (server *Server, err error) { level = log.ErrorLevel } if mocktest.LogOn { - if _, err = log.InitLog(logDir, "master", level, nil, log.DefaultLogLeftSpaceLimit); err != nil { + if _, err = log.InitLog(logDir, "master", level, nil, log.DefaultLogLeftSpaceLimitRatio); err != nil { fmt.Println("Fatal: failed to start the cubefs daemon - ", err) return } diff --git a/metanode/multi_ver_test.go b/metanode/multi_ver_test.go index 45efd6561..a7c4f59ed 100644 --- a/metanode/multi_ver_test.go +++ b/metanode/multi_ver_test.go @@ -93,7 +93,7 @@ func init() { logDir := cfg.GetString(ConfigKeyLogDir) os.RemoveAll(logDir) - if _, err := log.InitLog(logDir, "metanode", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimit); err != nil { + if _, err := log.InitLog(logDir, "metanode", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimitRatio); err != nil { fmt.Println("Fatal: failed to start the cubefs daemon - ", err) return } diff --git a/metanode/transaction_test.go b/metanode/transaction_test.go index ff9456c41..e6f70512d 100644 --- a/metanode/transaction_test.go +++ b/metanode/transaction_test.go @@ -45,7 +45,7 @@ const ( ) func init() { - log.InitLog("/tmp/cfs/logs/", "test", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimit) + log.InitLog("/tmp/cfs/logs/", "test", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimitRatio) } func newMetaPartition(PartitionId uint64, manager *metadataManager) (mp *metaPartition) { diff --git a/preload/sdk/preloadsdk.go b/preload/sdk/preloadsdk.go index 567bf9dc3..c163cf8dc 100644 --- a/preload/sdk/preloadsdk.go +++ b/preload/sdk/preloadsdk.go @@ -89,7 +89,7 @@ func NewClient(config PreloadConfig) *PreLoadClient { } if config.LogDir != "" { - log.InitLog(config.LogDir, "preload", convertLogLevel(config.LogLevel), nil, log.DefaultLogLeftSpaceLimit) + log.InitLog(config.LogDir, "preload", convertLogLevel(config.LogLevel), nil, log.DefaultLogLeftSpaceLimitRatio) stat.NewStatistic(config.LogDir, "preload", int64(stat.DefaultStatLogSize), stat.DefaultTimeOutUs, true) } diff --git a/snapshot/cmd/clean.go b/snapshot/cmd/clean.go index fd9f04e57..44879e9a1 100644 --- a/snapshot/cmd/clean.go +++ b/snapshot/cmd/clean.go @@ -125,7 +125,7 @@ func Clean(opt string, args []string) error { ump.InitUmp("snapshot", "") - _, err := log.InitLog("snapshotlog", "snapshot", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimit) + _, err := log.InitLog("snapshotlog", "snapshot", log.DebugLevel, nil, log.DefaultLogLeftSpaceLimitRatio) if err != nil { return fmt.Errorf("Init log failed: %v", err) } diff --git a/util/log/log.go b/util/log/log.go index a5b221a30..c02115766 100644 --- a/util/log/log.go +++ b/util/log/log.go @@ -302,7 +302,7 @@ func (l *Log) outputStderr(calldepth int, s string) { } // InitLog initializes the log. -func InitLog(dir, module string, level Level, rotate *LogRotate, logLeftSpaceLimit int64) (*Log, error) { +func InitLog(dir, module string, level Level, rotate *LogRotate, logLeftSpaceLimitRatio float64) (*Log, error) { l := new(Log) l.printStderr = 1 dir = path.Join(dir, module) @@ -329,13 +329,8 @@ func InitLog(dir, module string, level Level, rotate *LogRotate, logLeftSpaceLim if rotate.headRoom == 0 { var minLogLeftSpaceLimit float64 - if float64(fs.Bavail*uint64(fs.Bsize)) < float64(fs.Blocks*uint64(fs.Bsize))*DefaultHeadRatio { - minLogLeftSpaceLimit = float64(fs.Bavail*uint64(fs.Bsize)) * DefaultHeadRatio / 1024 / 1024 - } else { - minLogLeftSpaceLimit = float64(fs.Blocks*uint64(fs.Bsize)) * DefaultHeadRatio / 1024 / 1024 - } - minLogLeftSpaceLimit = math.Max(minLogLeftSpaceLimit, float64(logLeftSpaceLimit)) + minLogLeftSpaceLimit = float64(fs.Blocks*uint64(fs.Bsize)) * logLeftSpaceLimitRatio / 1024 / 1024 rotate.SetHeadRoomMb(int64(math.Min(minLogLeftSpaceLimit, DefaultHeadRoom))) } diff --git a/util/log/log_test.go b/util/log/log_test.go index dcf7c3998..15a1cf56c 100644 --- a/util/log/log_test.go +++ b/util/log/log_test.go @@ -53,7 +53,7 @@ func TestLog(t *testing.T) { return } - InitLog("/tmp/cfs", "cfs", DebugLevel, nil, DefaultLogLeftSpaceLimit) + InitLog("/tmp/cfs", "cfs", DebugLevel, nil, DefaultLogLeftSpaceLimitRatio) for i := 0; i < 10; i++ { LogDebugf("[debug] current time %v.", time.Now()) LogWarnf("[warn] current time %v.", time.Now()) @@ -107,7 +107,7 @@ func TestLogLeftSpaceLimit01(t *testing.T) { t.Errorf("create file[%v] err[%v]", logFilePath, err) return } - log, err := InitLog("/tmp/cfs", "cfs", DebugLevel, nil, DefaultLogLeftSpaceLimit) + log, err := InitLog("/tmp/cfs", "cfs", DebugLevel, nil, DefaultLogLeftSpaceLimitRatio) if err != nil { t.Errorf("init log err[%v]", err) return @@ -132,7 +132,7 @@ func TestLogLeftSpaceLimit02(t *testing.T) { return } - log, err := InitLog("/tmp/cfs", "cfs", DebugLevel, nil, DefaultLogLeftSpaceLimit) + log, err := InitLog("/tmp/cfs", "cfs", DebugLevel, nil, DefaultLogLeftSpaceLimitRatio) if err != nil { t.Errorf("init log err[%v]", err) return diff --git a/util/log/rotate.go b/util/log/rotate.go index 2ff693651..8a932e02c 100644 --- a/util/log/rotate.go +++ b/util/log/rotate.go @@ -22,8 +22,8 @@ const ( // DefaultHeadRoom The tolerance for the log space limit (in megabytes) DefaultHeadRoom = 50 * 1024 // DefaultHeadRatio The disk reserve space ratio - DefaultHeadRatio = 0.2 - DefaultLogLeftSpaceLimit = 5 * 1024 + DefaultHeadRatio = 0.2 + DefaultLogLeftSpaceLimitRatio = 0.05 ) // LogRotate A log can be rotated by the size or time.