package main import ( "fmt" "os" "runtime/debug" "strings" "testing" "github.com/seaweedfs/seaweed-block/core/host/volume" ) var fallbackBase = []string{"--master", "127.0.0.1:1", "--server-id", "h1", "--volume-id", "v1", "--replica-id", "r1", "--data-addr", "127.0.0.1:2", "--ctrl-addr", "127.0.0.1:3"} // cgroupFiles serves fixture /proc and cgroup files; anything else is absent. func cgroupFiles(files map[string]string) func(string) ([]byte, error) { return func(name string) ([]byte, error) { if s, ok := files[name]; ok { return []byte(s), nil } return nil, os.ErrNotExist } } // A holder whose cgroup v2 limit cannot be derived, with neither // --memory-limit-mib nor GOMEMLIMIT given, starts on the 256 MiB default and // says why: it is the no-limit case, never a refusal. The first case is a // mount namespace whose cgroup2 mount does not cover the process's // membership (what the process sees in /proc/self/mountinfo and cgroup). func TestMemoryLimitFallsBackWhenTheCgroupLimitCannotBeDerived(t *testing.T) { t.Setenv("GOMEMLIMIT", "") for _, tc := range []struct { name, reason string files map[string]string }{ {"membership outside the namespace's cgroup2 mount", `no cgroup2 mount resolves membership "/user.slice/user-1000.slice/session-7.scope"`, map[string]string{ "/proc/self/mountinfo": "29 23 0:26 /system.slice/runner.service /sys/fs/cgroup rw - cgroup2 cgroup rw\n", "/proc/self/cgroup": "0::/user.slice/user-1000.slice/session-7.scope\n", }}, {"unreadable memory.max", "invalid /sys/fs/cgroup/holder/memory.max value", map[string]string{ "/proc/self/mountinfo": "29 23 0:26 / /sys/fs/cgroup rw - cgroup2 cgroup rw\n", "/proc/self/cgroup": "0::/holder\n", "/sys/fs/cgroup/holder/memory.max": "garbage\n", }}, } { t.Run(tc.name, func(t *testing.T) { f, err := parseFlagsWithCgroup(fallbackBase, cgroupFiles(tc.files)) if err != nil { t.Fatalf("refused to start: %v", err) } if f.memoryLimitMiB != 256 { t.Fatalf("limit %d MiB, want the 256 MiB default", f.memoryLimitMiB) } if !strings.Contains(f.memoryLimitFallback, tc.reason) { t.Fatalf("fallback reason %q does not name %q", f.memoryLimitFallback, tc.reason) } }) } // An explicit but invalid limit still refuses. if _, err := parseFlagsWithCgroup(append(append([]string{}, fallbackBase...), "--memory-limit-mib", "-1"), cgroupFiles(nil)); err == nil { t.Fatal("an invalid --memory-limit-mib was accepted") } } // Applying a fallback limit logs exactly one WARN naming why and the two ways // to choose a limit, and status reports the reason; a derived limit logs // nothing and reports no reason. func TestMemoryLimitFallbackWarnsOnceAndShowsInStatus(t *testing.T) { t.Setenv("GOMEMLIMIT", "") old := debug.SetMemoryLimit(-1) t.Cleanup(func() { debug.SetMemoryLimit(old); volume.SetSoftLimitFallback("") }) f, err := parseFlagsWithCgroup(fallbackBase, cgroupFiles(map[string]string{ "/proc/self/mountinfo": "29 23 0:26 /system.slice/runner.service /sys/fs/cgroup rw - cgroup2 cgroup rw\n", "/proc/self/cgroup": "0::/user.slice/user-1000.slice/session-7.scope\n", })) if err != nil { t.Fatalf("refused to start: %v", err) } var lines []string logf := func(format string, args ...any) { lines = append(lines, fmt.Sprintf(format, args...)) } applyMemoryLimit(f, logf) if len(lines) != 1 { t.Fatalf("%d log lines, want exactly one WARN: %q", len(lines), lines) } for _, want := range []string{"level=WARN", "no cgroup2 mount resolves membership", "256 MiB", "--memory-limit-mib", "GOMEMLIMIT"} { if !strings.Contains(lines[0], want) { t.Fatalf("the WARN does not name %q: %s", want, lines[0]) } } if got := debug.SetMemoryLimit(-1); got != 256<<20 { t.Fatalf("soft limit in force %d, want 256 MiB", got) } if got := volume.SoftLimitFallback(); !strings.Contains(got, "no cgroup2 mount resolves membership") { t.Fatalf("status reports the fallback as %q", got) } lines = nil f, err = parseFlagsWithCgroup(fallbackBase, cgroupFiles(map[string]string{ "/proc/self/mountinfo": "29 23 0:26 / /sys/fs/cgroup rw - cgroup2 cgroup rw\n", "/proc/self/cgroup": "0::/system.slice/volume.service\n", "/sys/fs/cgroup/system.slice/volume.service/memory.max": "2147483648\n", })) if err != nil { t.Fatal(err) } applyMemoryLimit(f, logf) if len(lines) != 0 || volume.SoftLimitFallback() != "" { t.Fatalf("a derived limit logged %q and reports fallback %q", lines, volume.SoftLimitFallback()) } }