From 7a70a7a1ca41a1fb72ecc2d74ab8051cf515ba62 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 15:46:02 -0800 Subject: [PATCH] blockvolume: an underivable cgroup memory limit starts on the default With neither --memory-limit-mib nor GOMEMLIMIT, a cgroup v2 limit that cannot be derived (a membership outside the cgroup2 mount of the holder's mount namespace, an unreadable or malformed memory.max) is the no-limit case: the holder keeps the 256 MiB default and starts, instead of exiting 2. applyMemoryLimit, run once logging is configured, logs one level=WARN line naming the derivation's error and the two ways to choose a limit, and status reports the reason in Memory.SoftLimitFallback (empty otherwise). Only an explicit but invalid limit still refuses. Docs: the flag's help, the operator guide's memory section, the status schema and the v1.1 notes. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/blockvolume/main.go | 13 +++++++---- .../cmd/blockvolume/memory_cgroup.go | 3 ++- seaweed-block/cmd/blockvolume/memory_limit.go | 14 +++++++++-- .../core/host/volume/memory_fallback.go | 23 +++++++++++++++---- .../core/host/volume/memory_status.go | 6 ++--- seaweed-block/docs/operator-guide.md | 10 +++++--- seaweed-block/docs/releases/v1.1.md | 4 ++++ seaweed-block/docs/status-schema.md | 1 + 8 files changed, 54 insertions(+), 20 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index 9e94b6a39e..dcf572881f 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -165,7 +165,8 @@ type flags struct { version bool // memoryLimitFallback says why the default soft memory limit is in - // force instead of a cgroup-derived one (seam). + // force instead of a cgroup-derived one: the derivation's error. Empty + // when a limit was derived, given, or not needed. memoryLimitFallback string } @@ -193,7 +194,7 @@ func parseFlagsWithCgroup(args []string, readFile func(string) ([]byte, error)) var f flags fs := flag.NewFlagSet("blockvolume", flag.ContinueOnError) fs.StringVar(&f.logLevel, "log-level", "info", "log verbosity: info or debug; timestamps are UTC with microseconds") - fs.Int64Var(&f.memoryLimitMiB, "memory-limit-mib", 4*storage.DefaultCutPayloadMemoryBudget/(1<<20), "process Go soft memory limit in MiB; unset: 90% of the effective cgroup v2 memory.max, or 256 MiB without a limit; 0 preserves GOMEMLIMIT; explicit GOMEMLIMIT is preserved unless this flag is set") + fs.Int64Var(&f.memoryLimitMiB, "memory-limit-mib", 4*storage.DefaultCutPayloadMemoryBudget/(1<<20), "process Go soft memory limit in MiB; unset: 90% of the effective cgroup v2 memory.max, or 256 MiB without a limit or when it cannot be derived (one WARN says why); 0 preserves GOMEMLIMIT; explicit GOMEMLIMIT is preserved unless this flag is set") fs.StringVar(&f.masterAddr, "master", "", "master gRPC address (required)") fs.StringVar(&f.serverID, "server-id", "", "this server identity (required)") fs.StringVar(&f.masterTLS.Cert, "master-tls-cert", "", "this node's certificate (PEM) for mutual TLS to blockmaster; its CN must equal --server-id") @@ -297,11 +298,13 @@ func parseFlagsWithCgroup(args []string, readFile func(string) ([]byte, error)) if !memoryFlagSet && os.Getenv("GOMEMLIMIT") != "" { f.memoryLimitMiB = 0 } else if !memoryFlagSet && !f.version { + // An underivable cgroup limit is the no-limit case: the default stays + // in force and applyMemoryLimit warns why. Only an explicit limit + // (the flag, or GOMEMLIMIT read by the runtime) may refuse. limit, err := cgroupMemoryLimitMiB(readFile) if err != nil { - return flags{}, fmt.Errorf("derive memory limit: %w", err) - } - if limit != 0 { + f.memoryLimitFallback = err.Error() + } else if limit != 0 { f.memoryLimitMiB = limit } } diff --git a/seaweed-block/cmd/blockvolume/memory_cgroup.go b/seaweed-block/cmd/blockvolume/memory_cgroup.go index 7bab4193f8..ca6854f88d 100644 --- a/seaweed-block/cmd/blockvolume/memory_cgroup.go +++ b/seaweed-block/cmd/blockvolume/memory_cgroup.go @@ -75,7 +75,8 @@ func cgroupMemoryLimitMiB(readFile func(string) ([]byte, error)) (int64, error) // mountinfo names both the filesystem subtree root and its visible mountpoint. // A membership path must resolve inside that view; an unresolved v2 hierarchy -// is a configuration error, not evidence of unlimited memory. +// is an error here, not evidence of unlimited memory: the caller keeps the +// default limit and warns why. func cgroupMemoryDirectory(group string, readFile func(string) ([]byte, error)) (string, string, error) { data, err := readFile("/proc/self/mountinfo") if err != nil { diff --git a/seaweed-block/cmd/blockvolume/memory_limit.go b/seaweed-block/cmd/blockvolume/memory_limit.go index 8ce7f76eb1..c4f2d17c99 100644 --- a/seaweed-block/cmd/blockvolume/memory_limit.go +++ b/seaweed-block/cmd/blockvolume/memory_limit.go @@ -1,11 +1,21 @@ package main -import "runtime/debug" +import ( + "runtime/debug" + + "github.com/seaweedfs/seaweed-block/core/host/volume" +) // applyMemoryLimit sets the process's Go soft memory limit from the parsed -// flags (seam: no fallback report yet). +// flags. When the default is in force because the cgroup v2 limit could not +// be derived, it logs one WARN naming why and how to choose a limit, and +// status reports the reason (Memory.SoftLimitFallback). func applyMemoryLimit(f flags, logf func(string, ...any)) { if f.memoryLimitMiB > 0 { debug.SetMemoryLimit(f.memoryLimitMiB << 20) } + volume.SetSoftLimitFallback(f.memoryLimitFallback) + if f.memoryLimitFallback != "" { + logf("blockvolume: level=WARN memory limit: the cgroup v2 limit could not be derived (%s); running with the %d MiB default soft limit; set --memory-limit-mib or GOMEMLIMIT to choose a limit", f.memoryLimitFallback, f.memoryLimitMiB) + } } diff --git a/seaweed-block/core/host/volume/memory_fallback.go b/seaweed-block/core/host/volume/memory_fallback.go index 78adf6b52f..adb2b1aeb5 100644 --- a/seaweed-block/core/host/volume/memory_fallback.go +++ b/seaweed-block/core/host/volume/memory_fallback.go @@ -1,8 +1,21 @@ package volume -// SetSoftLimitFallback records why the process runs on the default soft -// memory limit (seam: not reported yet). -func SetSoftLimitFallback(reason string) {} +import "sync/atomic" -// SoftLimitFallback is the reason SetSoftLimitFallback recorded (seam). -func SoftLimitFallback() string { return "" } +// softLimitFallback is why the process runs on the default soft memory limit +// instead of a cgroup-derived one; the limit itself is process-wide too. +var softLimitFallback atomic.Pointer[string] + +// SetSoftLimitFallback records why the process runs on the default soft +// memory limit; empty clears it. +func SetSoftLimitFallback(reason string) { + softLimitFallback.Store(&reason) +} + +// SoftLimitFallback is the reason SetSoftLimitFallback recorded, or empty. +func SoftLimitFallback() string { + if p := softLimitFallback.Load(); p != nil { + return *p + } + return "" +} diff --git a/seaweed-block/core/host/volume/memory_status.go b/seaweed-block/core/host/volume/memory_status.go index 1c952f7f75..b77a4fad11 100644 --- a/seaweed-block/core/host/volume/memory_status.go +++ b/seaweed-block/core/host/volume/memory_status.go @@ -16,9 +16,7 @@ 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"` + SoftLimitFallback string `json:",omitempty"` // why SoftLimitBytes is the 256 MiB default instead of a cgroup-derived limit (the derivation's error) RSSBytes uint64 // resident set size from /proc/self/status (VmRSS); 0 off Linux AnonymousRSSBytes uint64 // anonymous resident memory (RssAnon); 0 off Linux } @@ -26,7 +24,7 @@ type ProcessMemoryStatus struct { func processMemoryStatus() ProcessMemoryStatus { var m runtime.MemStats runtime.ReadMemStats(&m) - out := ProcessMemoryStatus{HeapAllocBytes: m.HeapAlloc, HeapInuseBytes: m.HeapInuse, HeapSysBytes: m.HeapSys, HeapReleasedBytes: m.HeapReleased, SoftLimitBytes: debug.SetMemoryLimit(-1)} + out := ProcessMemoryStatus{HeapAllocBytes: m.HeapAlloc, HeapInuseBytes: m.HeapInuse, HeapSysBytes: m.HeapSys, HeapReleasedBytes: m.HeapReleased, SoftLimitBytes: debug.SetMemoryLimit(-1), SoftLimitFallback: SoftLimitFallback()} b, err := os.ReadFile("/proc/self/status") if err != nil { return out diff --git a/seaweed-block/docs/operator-guide.md b/seaweed-block/docs/operator-guide.md index 711b03dc45..18aa423eba 100644 --- a/seaweed-block/docs/operator-guide.md +++ b/seaweed-block/docs/operator-guide.md @@ -271,9 +271,13 @@ are its only SeaweedFS flags. Run SeaweedFS on a trusted network; mTLS covers th Automatic sizing requires the cgroup v2 memory controller. On cgroup v1, or a hybrid hierarchy without v2 `memory.max`, set `--memory-limit-mib` explicitly for a large volume; otherwise it receives the 256 MiB fallback. - An unreadable or malformed cgroup limit refuses startup with a diagnostic; - an explicit memory flag or `GOMEMLIMIT` bypasses this detection. - Inspect `Memory.SoftLimitBytes` on the holder's status for the value in force. + A cgroup limit that cannot be derived (unreadable or malformed, or a + membership outside the cgroup2 mount visible in the holder's mount namespace) + does not stop the holder: it starts on the 256 MiB default and logs one + `level=WARN` line naming why. An explicit memory flag or `GOMEMLIMIT` bypasses + this detection; only an explicit but invalid one refuses startup. + Inspect `Memory.SoftLimitBytes` on the holder's status for the value in force, + and `Memory.SoftLimitFallback` for why the default is in force. - **Measured throughput on one host** (three runs; 16 cores, 23.2 GiB RAM, one consumer NVMe drive, fsync on every volume): diff --git a/seaweed-block/docs/releases/v1.1.md b/seaweed-block/docs/releases/v1.1.md index 13c0502601..2deb89237d 100644 --- a/seaweed-block/docs/releases/v1.1.md +++ b/seaweed-block/docs/releases/v1.1.md @@ -20,6 +20,10 @@ These notes collect v1.1's changes as they land; v1.1 is not released. - A normal cut that has held its host slot for `--host-limit-yield-after` (default 5 min) gives the slot to a waiting cut at its next staging batch, keeping what it staged; its next attempt queues behind the waiters. A lone cut never yields, and status counts yields apart from failures (`CutYields`). +- A `blockvolume` whose cgroup v2 memory limit cannot be derived (for example, its cgroup membership lies outside + the cgroup2 mount visible in its mount namespace) now starts on the 256 MiB default soft limit and logs one + `level=WARN` line naming why, instead of exiting 2; status reports the reason in `Memory.SoftLimitFallback`. An + explicit but invalid `--memory-limit-mib` or `GOMEMLIMIT` still refuses. ## Upgrade - With `--host-limit-dir` set, the limiter's `configuration.json` in that directory records the flags it was diff --git a/seaweed-block/docs/status-schema.md b/seaweed-block/docs/status-schema.md index 36c0315fe7..41533450f0 100644 --- a/seaweed-block/docs/status-schema.md +++ b/seaweed-block/docs/status-schema.md @@ -26,6 +26,7 @@ The volume's authority and health projection. Go type `volume.StatusProjection`. | `Memory.HeapSysBytes` | uint64 | Go heap bytes obtained from the OS (HeapSys) | | `Memory.HeapReleasedBytes` | uint64 | Go heap bytes returned to the OS (HeapReleased) | | `Memory.SoftLimitBytes` | int64 | the Go soft memory limit in force (flag, GOMEMLIMIT or cgroup-derived default) | +| `Memory.SoftLimitFallback` | string | why SoftLimitBytes is the 256 MiB default instead of a cgroup-derived limit (the derivation's error) (omitted when empty) | | `Memory.RSSBytes` | uint64 | resident set size from /proc/self/status (VmRSS); 0 off Linux | | `Memory.AnonymousRSSBytes` | uint64 | anonymous resident memory (RssAnon); 0 off Linux | | `FrontendPrimaryReady` | bool | FrontendPrimaryReady: this replica is the healthy, non-superseded primary, its local durable backend admits I/O, and it may serve guest I/O (INV-FE-PRIMARY-READY-OPERATIONAL-001). |