From aad86975243f248bfe83cc73b472eb92bfec1add Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Mon, 21 Feb 2022 20:33:54 -0300 Subject: [PATCH] [ws-daemon] Improve cache error handling --- components/ws-daemon/pkg/daemon/cache_reclaim.go | 12 ++++++++++++ .../ws-daemon/pkg/daemon/cache_reclaim_test.go | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/components/ws-daemon/pkg/daemon/cache_reclaim.go b/components/ws-daemon/pkg/daemon/cache_reclaim.go index d1e761eca86548..1555c1637e85cc 100644 --- a/components/ws-daemon/pkg/daemon/cache_reclaim.go +++ b/components/ws-daemon/pkg/daemon/cache_reclaim.go @@ -7,6 +7,8 @@ package daemon import ( "bufio" "context" + "errors" + "io/fs" "io/ioutil" "math" "os" @@ -116,6 +118,11 @@ func readLimit(memCgroupPath string) (uint64, error) { fn := filepath.Join(string(memCgroupPath), "memory.limit_in_bytes") fc, err := os.ReadFile(fn) if err != nil { + // TODO(toru): find out why the file does not exists + if errors.Is(err, fs.ErrNotExist) { + return 0, nil + } + return 0, xerrors.Errorf("cannot read memory.limit_in_bytes: %v", err) } @@ -134,6 +141,11 @@ func readLimit(memCgroupPath string) (uint64, error) { func readCache(memCgroupPath string) (uint64, error) { f, err := os.Open(filepath.Join(string(memCgroupPath), "memory.stat")) if err != nil { + // TODO(toru): find out why the file does not exists + if errors.Is(err, fs.ErrNotExist) { + return 0, nil + } + return 0, xerrors.Errorf("cannot read memory.stat: %w", err) } defer f.Close() diff --git a/components/ws-daemon/pkg/daemon/cache_reclaim_test.go b/components/ws-daemon/pkg/daemon/cache_reclaim_test.go index b66ac97634c162..a54786b482b737 100644 --- a/components/ws-daemon/pkg/daemon/cache_reclaim_test.go +++ b/components/ws-daemon/pkg/daemon/cache_reclaim_test.go @@ -54,8 +54,8 @@ func TestReadLimitBadValue(t *testing.T) { t.Fatal(err) } _, err = readCache(tempdir) - if err == nil { - t.Fatal("expected failure") + if err != nil { + t.Fatalf("unexpected error: is '%v' but expected no error", err) } } }