Skip to content

Commit

Permalink
enhance: excessive disk usage logs generated if the path does not exi…
Browse files Browse the repository at this point in the history
…st (#38822)

issue: #38820
pr: #38821

Signed-off-by: jaime <[email protected]>
  • Loading branch information
jaime0815 authored Jan 2, 2025
1 parent 1d55ad6 commit 5fb8313
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/util/hardware/hardware_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"runtime"
"sync"

"github.com/cockroachdb/errors"
"github.com/cockroachdb/errors/oserror"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
Expand Down Expand Up @@ -107,6 +109,10 @@ func GetFreeMemoryCount() uint64 {
func GetDiskUsage(path string) (float64, float64, error) {
diskStats, err := disk.Usage(path)
if err != nil {
// If the path does not exist, ignore the error and return 0.
if errors.Is(err, oserror.ErrNotExist) {
return 0, 0, nil
}
return 0, 0, err
}
usedGB := float64(diskStats.Used) / 1e9
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/hardware/hardware_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func TestGetDiskUsage(t *testing.T) {
assert.NoError(t, err)
assert.GreaterOrEqual(t, used, 0.0)
assert.GreaterOrEqual(t, total, 0.0)

used, total, err = GetDiskUsage("/dir_not_exist")
assert.NoError(t, err)
assert.Equal(t, 0.0, used)
assert.Equal(t, 0.0, total)
}

func TestGetIOWait(t *testing.T) {
Expand Down

0 comments on commit 5fb8313

Please sign in to comment.