diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index ea483d87ef..9e94b6a39e 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -9,12 +9,12 @@ import ( "errors" "flag" "fmt" + "log" "math" "net" "os" "os/exec" "path/filepath" - "runtime/debug" "strconv" "strings" "time" @@ -163,6 +163,10 @@ type flags struct { // gating through the durable WriteObserver seam. replicationAck string version bool + + // memoryLimitFallback says why the default soft memory limit is in + // force instead of a cgroup-derived one (seam). + memoryLimitFallback string } // volumeLockDirTestOnlyFlag is hidden from --help (printVisibleUsage). @@ -592,13 +596,11 @@ func main() { fmt.Println(buildinfo.Version("blockvolume")) return } - if f.memoryLimitMiB > 0 { - debug.SetMemoryLimit(f.memoryLimitMiB << 20) - } if err := logging.Configure(f.logLevel); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(2) } + applyMemoryLimit(f, log.Printf) os.Exit(run(f)) } diff --git a/seaweed-block/cmd/blockvolume/memory_fallback_test.go b/seaweed-block/cmd/blockvolume/memory_fallback_test.go new file mode 100644 index 0000000000..7d9c924460 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/memory_fallback_test.go @@ -0,0 +1,110 @@ +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()) + } +} diff --git a/seaweed-block/cmd/blockvolume/memory_limit.go b/seaweed-block/cmd/blockvolume/memory_limit.go new file mode 100644 index 0000000000..8ce7f76eb1 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/memory_limit.go @@ -0,0 +1,11 @@ +package main + +import "runtime/debug" + +// applyMemoryLimit sets the process's Go soft memory limit from the parsed +// flags (seam: no fallback report yet). +func applyMemoryLimit(f flags, logf func(string, ...any)) { + if f.memoryLimitMiB > 0 { + debug.SetMemoryLimit(f.memoryLimitMiB << 20) + } +} diff --git a/seaweed-block/core/host/volume/memory_fallback.go b/seaweed-block/core/host/volume/memory_fallback.go new file mode 100644 index 0000000000..78adf6b52f --- /dev/null +++ b/seaweed-block/core/host/volume/memory_fallback.go @@ -0,0 +1,8 @@ +package volume + +// SetSoftLimitFallback records why the process runs on the default soft +// memory limit (seam: not reported yet). +func SetSoftLimitFallback(reason string) {} + +// SoftLimitFallback is the reason SetSoftLimitFallback recorded (seam). +func SoftLimitFallback() string { return "" } diff --git a/seaweed-block/core/host/volume/memory_fallback_test.go b/seaweed-block/core/host/volume/memory_fallback_test.go new file mode 100644 index 0000000000..d5c6cc4b31 --- /dev/null +++ b/seaweed-block/core/host/volume/memory_fallback_test.go @@ -0,0 +1,18 @@ +package volume + +import "testing" + +// The status projection reports why the process runs on the default soft +// memory limit, and reports nothing once there is no such reason. +func TestStatusProjectionReportsTheSoftLimitFallback(t *testing.T) { + t.Cleanup(func() { SetSoftLimitFallback("") }) + s := NewStatusServer(NewAdapterProjectionView(stubProjector{}, "v1", "r1", nil)) + SetSoftLimitFallback("no cgroup2 mount resolves membership \"/user.slice/x.scope\"") + if got := s.statusProjection().Memory.SoftLimitFallback; got != "no cgroup2 mount resolves membership \"/user.slice/x.scope\"" { + t.Fatalf("status reports the fallback as %q", got) + } + SetSoftLimitFallback("") + if got := s.statusProjection().Memory.SoftLimitFallback; got != "" { + t.Fatalf("status still reports a cleared fallback: %q", got) + } +} diff --git a/seaweed-block/core/host/volume/memory_status.go b/seaweed-block/core/host/volume/memory_status.go index 1ad9e5573e..1c952f7f75 100644 --- a/seaweed-block/core/host/volume/memory_status.go +++ b/seaweed-block/core/host/volume/memory_status.go @@ -16,6 +16,9 @@ type ProcessMemoryStatus struct { HeapSysBytes uint64 // Go heap bytes obtained from the OS (HeapSys) HeapReleasedBytes uint64 // Go heap bytes returned to the OS (HeapReleased) SoftLimitBytes int64 // the Go soft memory limit in force (flag, GOMEMLIMIT or cgroup-derived default) + // SoftLimitFallback says why SoftLimitBytes is the default instead of a + // cgroup-derived limit (seam). + SoftLimitFallback string `json:",omitempty"` RSSBytes uint64 // resident set size from /proc/self/status (VmRSS); 0 off Linux AnonymousRSSBytes uint64 // anonymous resident memory (RssAnon); 0 off Linux }