From 84111fd66fedaf80fb46cbb97e0e4862a03b2afa Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 22:55:32 -0800 Subject: [PATCH] feat: validate declared host timeout against WAL hold cap --- seaweed-block/cmd/blockvolume/main.go | 7 +++ .../cmd/blockvolume/wal_hold_config_test.go | 42 +++++++++++++++ .../core/frontend/wal_hold_config.go | 36 +++++++++++++ .../core/frontend/wal_hold_config_test.go | 54 +++++++++++++++++++ seaweed-block/docs/operator-guide.md | 6 +++ 5 files changed, 145 insertions(+) create mode 100644 seaweed-block/cmd/blockvolume/wal_hold_config_test.go create mode 100644 seaweed-block/core/frontend/wal_hold_config.go create mode 100644 seaweed-block/core/frontend/wal_hold_config_test.go diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index fb725ce54d..c47ba686dc 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -44,6 +44,7 @@ import ( ) type flags struct { + walHold frontend.WALHoldConfig logLevel string cpuProfileDir string memoryLimitMiB int64 @@ -193,6 +194,9 @@ func parseFlags(args []string) (flags, error) { func parseFlagsWithCgroup(args []string, readFile func(string) ([]byte, error)) (flags, error) { var f flags fs := flag.NewFlagSet("blockvolume", flag.ContinueOnError) + writePolicy := frontend.DefaultWALHoldConfig() + fs.DurationVar(&f.walHold.MaxHold, "wal-hold-timeout", writePolicy.MaxHold, "maximum per-command WAL-pressure hold; positive and at least 5s below --host-io-timeout") + fs.DurationVar(&f.walHold.HostIOTimeout, "host-io-timeout", writePolicy.HostIOTimeout, "operator-declared host I/O timeout (host_io_timeout); set to match your hosts when changing io_timeout") 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 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)") @@ -286,6 +290,9 @@ func parseFlagsWithCgroup(args []string, readFile func(string) ([]byte, error)) if err := fs.Parse(args); err != nil { return flags{}, err } + if err := f.walHold.Validate(); err != nil { + return flags{}, err + } if err := logging.ValidateLevel(f.logLevel); err != nil { return flags{}, err } diff --git a/seaweed-block/cmd/blockvolume/wal_hold_config_test.go b/seaweed-block/cmd/blockvolume/wal_hold_config_test.go new file mode 100644 index 0000000000..7e1f463dd2 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/wal_hold_config_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "errors" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend" +) + +func TestParseFlagsWALHoldHostTimeout(t *testing.T) { + for _, tc := range []struct { + name string + args []string + hold, host time.Duration + valid bool + }{ + {"defaults", nil, 25 * time.Second, 30 * time.Second, true}, + {"explicit equality", []string{"--wal-hold-timeout", "10s", "--host-io-timeout", "15s"}, 10 * time.Second, 15 * time.Second, true}, + {"host lowered without hold", []string{"--host-io-timeout", "29s"}, 0, 0, false}, + {"hold above margin", []string{"--wal-hold-timeout", "25.000000001s"}, 0, 0, false}, + {"zero hold", []string{"--wal-hold-timeout", "0"}, 0, 0, false}, + } { + t.Run(tc.name, func(t *testing.T) { + args := append(requiredBlockvolumeArgs(), tc.args...) + f, err := parseFlags(args) + if tc.valid { + if err != nil { + t.Fatal(err) + } + if f.walHold.MaxHold != tc.hold || f.walHold.HostIOTimeout != tc.host { + t.Fatalf("parsed policy: %+v", f.walHold) + } + } else { + var invalid *frontend.WALHoldConfigError + if !errors.As(err, &invalid) { + t.Fatalf("want typed startup refusal, got %v", err) + } + } + }) + } +} diff --git a/seaweed-block/core/frontend/wal_hold_config.go b/seaweed-block/core/frontend/wal_hold_config.go new file mode 100644 index 0000000000..59a3a29d48 --- /dev/null +++ b/seaweed-block/core/frontend/wal_hold_config.go @@ -0,0 +1,36 @@ +package frontend + +import ( + "fmt" + "time" +) + +const walHoldHostMargin = 5 * time.Second + +// WALHoldConfig describes the host timeout declared by the operator and the +// maximum time one command may wait for WAL capacity. A target cannot discover +// the initiator's timeout; HostIOTimeout must match the hosts using the volume. +type WALHoldConfig struct { + MaxHold time.Duration + HostIOTimeout time.Duration +} + +func DefaultWALHoldConfig() WALHoldConfig { + return WALHoldConfig{MaxHold: 25 * time.Second, HostIOTimeout: 30 * time.Second} +} + +// WALHoldConfigError is a startup refusal, not a runtime pressure outcome. +type WALHoldConfigError struct{ Config WALHoldConfig } + +func (e *WALHoldConfigError) Error() string { + return fmt.Sprintf("frontend: invalid WAL hold configuration: hold=%s host_io_timeout=%s; require positive hold and hold + 5s <= host_io_timeout", e.Config.MaxHold, e.Config.HostIOTimeout) +} + +func (c WALHoldConfig) Validate() error { + // Subtract only after checking the host value. Adding the margin to a + // user-supplied duration could overflow and accept an unsafe configuration. + if c.MaxHold <= 0 || c.HostIOTimeout <= walHoldHostMargin || c.MaxHold > c.HostIOTimeout-walHoldHostMargin { + return &WALHoldConfigError{Config: c} + } + return nil +} diff --git a/seaweed-block/core/frontend/wal_hold_config_test.go b/seaweed-block/core/frontend/wal_hold_config_test.go new file mode 100644 index 0000000000..0454b42ff0 --- /dev/null +++ b/seaweed-block/core/frontend/wal_hold_config_test.go @@ -0,0 +1,54 @@ +package frontend + +import ( + "errors" + "math" + "strings" + "testing" + "time" +) + +func TestWALHoldConfigHostTimeoutMargin(t *testing.T) { + cfg := DefaultWALHoldConfig() + if cfg.MaxHold != 25*time.Second || cfg.HostIOTimeout != 30*time.Second { + t.Fatalf("defaults: %+v", cfg) + } + for _, tc := range []struct { + name string + hold, host time.Duration + valid bool + }{ + {"default equality", 25 * time.Second, 30 * time.Second, true}, + {"raised host", 40 * time.Second, 45 * time.Second, true}, + {"lowered host", 10 * time.Second, 15 * time.Second, true}, + {"one nanosecond short", 25 * time.Second, 30*time.Second - time.Nanosecond, false}, + {"zero hold", 0, 30 * time.Second, false}, + {"negative hold", -time.Second, 30 * time.Second, false}, + {"zero host", 25 * time.Second, 0, false}, + {"negative host", time.Second, -time.Second, false}, + {"no room for hold", time.Nanosecond, 5 * time.Second, false}, + {"overflow must refuse", time.Duration(math.MaxInt64), 30 * time.Second, false}, + {"largest valid", time.Duration(math.MaxInt64) - 5*time.Second, time.Duration(math.MaxInt64), true}, + } { + t.Run(tc.name, func(t *testing.T) { + cfg := WALHoldConfig{MaxHold: tc.hold, HostIOTimeout: tc.host} + err := cfg.Validate() + if tc.valid { + if err != nil { + t.Fatal(err) + } + } else { + var invalid *WALHoldConfigError + if !errors.As(err, &invalid) { + t.Fatalf("want typed config refusal, got %v", err) + } + if invalid.Config != cfg { + t.Fatalf("refusal lost supplied configuration: %+v", invalid) + } + if !strings.Contains(err.Error(), "host_io_timeout") { + t.Fatalf("missing operator setting: %v", err) + } + } + }) + } +} diff --git a/seaweed-block/docs/operator-guide.md b/seaweed-block/docs/operator-guide.md index 18aa423eba..c46da25cff 100644 --- a/seaweed-block/docs/operator-guide.md +++ b/seaweed-block/docs/operator-guide.md @@ -674,3 +674,9 @@ Every command on the pages above, and the test that runs it. | Scheduled snapshots (`--chunk-snapshot-schedule`) on a running volume with a real clock | not run in 1.0 as written: the schedule is tested with a fake clock (`core/frontend/durable/chunk_snapshot_schedule_test.go`) | not run | | Periodic capture (`--chunk-capture-interval`) on a running volume | a lab run on the release candidate: automatic cuts while the guest wrote, every acknowledged write read back | lab | | SeaweedFS vacuum (`/vol/vacuum`, `volume.vacuum`) | not run in 1.0: a SeaweedFS command; the tests check that the cleaning deletes the chunks, not that SeaweedFS compacts its files | not run | + +## Host I/O timeout configuration + +The holder cannot discover an initiator's I/O timeout. Set `blockvolume --host-io-timeout` (`host_io_timeout`) to match the hosts using the volume; the default is `30s`. If you raise or lower the hosts' `io_timeout`, change this setting to match. For hosts with different timeouts, use the shortest timeout. + +The per-command WAL-pressure hold limit, `--wal-hold-timeout`, defaults to `25s`. Startup refuses a non-positive hold or a hold that leaves less than five seconds before `--host-io-timeout`: `hold + 5s <= host_io_timeout`. Thus the default `25s` hold and `30s` host timeout are valid. For a `15s` host timeout, set the hold to at most `10s`. The five-second margin allows for network and queueing delay; it does not configure the host.