From 98d87777aae5fa4e05b5dabfcf1327d164c7043f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 23 Nov 2026 01:41:58 -0800 Subject: [PATCH] fix(durable): headroom guard before any sidecar; the open warning counts data growth apart from the WAL; M1 trace row Review: a refused chunk create left its fresh marker; a fresh 256 MiB store (WAL only allocated) with 256 MiB free gave no warning. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/headroom.go | 3 +- .../core/frontend/durable/headroom_test.go | 88 ++++++++++++------- .../core/frontend/durable/provider.go | 6 +- seaweed-block/docs/design/invariants.md | 1 + 4 files changed, 65 insertions(+), 33 deletions(-) diff --git a/seaweed-block/core/frontend/durable/headroom.go b/seaweed-block/core/frontend/durable/headroom.go index ba0d13865d..fcfd199003 100644 --- a/seaweed-block/core/frontend/durable/headroom.go +++ b/seaweed-block/core/frontend/durable/headroom.go @@ -35,7 +35,8 @@ func (p *DurableProvider) warnLowHeadroom(volumeID, path string, size int64) { return } have, err := free() - growth := max(size-allocatedBytes(path), 0) + // The store's allocation includes its WAL; only the rest is data. + growth := max(size-max(allocatedBytes(path)-storage.DefaultWALBytes, 0), 0) if err == nil && have < growth+storage.DefaultWALBytes { log.Printf("durable: WARNING volume %s: %s has %d bytes free, less than the volume's remaining growth %d + WAL %d; when the disk fills, guest writes fail: free space on it", volumeID, p.cfg.StorageRoot, have, growth, storage.DefaultWALBytes) diff --git a/seaweed-block/core/frontend/durable/headroom_test.go b/seaweed-block/core/frontend/durable/headroom_test.go index 5463a8804a..ff352c4eea 100644 --- a/seaweed-block/core/frontend/durable/headroom_test.go +++ b/seaweed-block/core/frontend/durable/headroom_test.go @@ -2,6 +2,7 @@ package durable_test import ( "bytes" + "io/fs" "log" "os" "path/filepath" @@ -9,33 +10,52 @@ import ( "testing" "github.com/seaweedfs/seaweed-block/core/frontend/durable" + "github.com/seaweedfs/seaweed-block/core/storage" ) // A volume is not created on a durable root without room for its full size -// and its WAL (operator guide, Size a host; issue 484): a full local disk -// fails guest writes, so the cure comes before the first write. +// and its WAL (operator guide, Size a host; issue 484), and a refused create +// leaves nothing behind: no store, no chunk fresh marker or other sidecar. func TestCreateRefusesWithoutLocalHeadroom(t *testing.T) { defer durable.SetRootFreeForTest(8 << 20)() - root := t.TempDir() - p, err := durable.NewDurableProvider(durable.ProviderConfig{Impl: durable.ImplWALStore, StorageRoot: root, BlockSize: 4096, NumBlocks: 65536}, v1View()) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { p.Close() }) - _, err = p.EnsureStorage("v1") - if err == nil || !strings.Contains(err.Error(), "8388608 bytes free") || !strings.Contains(err.Error(), "needs 335544320") { - t.Fatalf("create on a root with 8 MiB free: %v", err) - } - if left, _ := filepath.Glob(filepath.Join(root, "*", "*.bin")); len(left) != 0 { - t.Fatalf("a refused create left %v", left) + for _, impl := range []durable.ImplName{durable.ImplWALStore, durable.ImplWALStoreChunk} { + t.Run(string(impl), func(t *testing.T) { + root := t.TempDir() + cfg := durable.ProviderConfig{Impl: impl, StorageRoot: root, BlockSize: 4096, NumBlocks: 65536} + if impl == durable.ImplWALStoreChunk { + remote := storage.NewMemChunkRemote() + cfg.ChunkRemoteFor = func(string) (storage.ChunkRemote, error) { return remote, nil } + } + p, err := durable.NewDurableProvider(cfg, v1View()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { p.Close() }) + _, err = p.EnsureStorage("v1") + if err == nil || !strings.Contains(err.Error(), "8388608 bytes free") || !strings.Contains(err.Error(), "needs 335544320") { + t.Fatalf("create on a root with 8 MiB free: %v", err) + } + var left []string + _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err == nil && !d.IsDir() { + left = append(left, path) + } + return nil + }) + if len(left) != 0 { + t.Fatalf("a refused create left %v", left) + } + }) } } -// An existing volume still opens on a root that is short of headroom, with a -// loud warning: refusing it would leave the acknowledged data unserved. +// An existing volume still opens on a root short of headroom, with a loud +// warning when its remaining data growth plus the WAL exceed the free space. +// A fresh 256 MiB volume holds only its WAL: 256 MiB free is short of the +// 256 MiB of data still to grow plus the 64 MiB WAL; 330 MiB free is not. func TestOpenWarnsWithoutLocalHeadroom(t *testing.T) { root := t.TempDir() - cfg := durable.ProviderConfig{Impl: durable.ImplWALStore, StorageRoot: root, BlockSize: 4096, NumBlocks: 64} + cfg := durable.ProviderConfig{Impl: durable.ImplWALStore, StorageRoot: root, BlockSize: 4096, NumBlocks: 65536} p, err := durable.NewDurableProvider(cfg, v1View()) if err != nil { t.Fatal(err) @@ -44,19 +64,25 @@ func TestOpenWarnsWithoutLocalHeadroom(t *testing.T) { t.Fatal(err) } p.Close() - var out bytes.Buffer - log.SetOutput(&out) - t.Cleanup(func() { log.SetOutput(os.Stderr) }) - defer durable.SetRootFreeForTest(4096)() - p, err = durable.NewDurableProvider(cfg, v1View()) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { p.Close() }) - if _, err := p.EnsureStorage("v1"); err != nil { - t.Fatalf("an existing volume did not open on a short root: %v", err) - } - if !strings.Contains(out.String(), "WARNING") || !strings.Contains(out.String(), "4096 bytes free") { - t.Fatalf("no headroom warning:\n%s", out.String()) + for _, c := range []struct { + free int64 + warn bool + }{{256 << 20, true}, {330 << 20, false}} { + var out bytes.Buffer + log.SetOutput(&out) + restore := durable.SetRootFreeForTest(c.free) + p, err := durable.NewDurableProvider(cfg, v1View()) + if err != nil { + t.Fatal(err) + } + if _, err := p.EnsureStorage("v1"); err != nil { + t.Fatalf("an existing volume did not open with %d bytes free: %v", c.free, err) + } + p.Close() + restore() + log.SetOutput(os.Stderr) + if got := strings.Contains(out.String(), "WARNING volume v1"); got != c.warn { + t.Fatalf("free %d: warning=%v, want %v:\n%s", c.free, got, c.warn, out.String()) + } } } diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index c18becd6b4..a6af566c84 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -845,6 +845,10 @@ func (p *DurableProvider) openOrCreate(volumeID string) (*volHandle, error) { if err := geometry.ValidateBlockGeometry(uint64(p.cfg.NumBlocks), uint64(p.cfg.BlockSize)); err != nil { return nil, err } + // Headroom before any sidecar: a refused create leaves nothing behind. + if err := p.refuseCreateWithoutHeadroom(volumeID); err != nil { + return nil, err + } if p.cfg.Impl == ImplWALStoreChunk { // Durable before the volume file exists: a crash anywhere between // here and the first successful attach leaves a fresh volume. @@ -989,7 +993,7 @@ func (p *DurableProvider) createFresh(volumeID, path string, impl ImplName) (*vo var err error if p.testWALStoreFactory != nil { ws, err = p.testWALStoreFactory(path, true) - } else if err = p.refuseCreateWithoutHeadroom(volumeID); err == nil { + } else { ws, err = storage.CreateWALStoreWithOptions(path, p.cfg.NumBlocks, p.cfg.BlockSize, storage.WALStoreOptions{ChunkBlocks: p.cfg.ChunkBlocks}) } if err != nil { diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 020ad8ec8f..cb11815e9f 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -60,6 +60,7 @@ stateDiagram-v2 | From | To | Guard | Enforced in | Protects | |---|---|---|---|---| | — | Opened | v3 A/B descriptors and owned-map dependencies valid, exclusive file ownership acquired; legacy formats refused; backend starts not operational | `core/storage/walstore.go` `OpenWALStoreWithOptions`; `core/frontend/durable/provider.go` `openOrCreate` | `INV-WAL-OPGATE-001`, `INV-WAL-V3-ACTIVATION-001`, `INV-WAL-V3-DESCRIPTOR-001`, `INV-WAL-V3-MAP-PUBLISH-001` | +| — | (create refused) | a new volume needs the durable root's free space >= its size + WAL, checked before any sidecar or marker; an existing volume opens and warns when its remaining data growth + WAL exceed the free space | `core/frontend/durable/headroom.go`; `core/frontend/durable/provider.go` `openOrCreate` | `INV-LOCAL-HEADROOM-001` | | Opened | Recovering | the volume is open in the provider | `core/frontend/durable/provider.go` `RecoverVolume` | `INV-WAL-OPGATE-001` | | Recovering | Serving | v3 nonce/cursor-bound physical prefix satisfies its W/BASE gate; rollback fence and rebuilt bounds publish before reuse; `Recover` aborts/drains an open COW window before lower locks, holds the capture barrier and invalidates proofs at start/end; the frontend targets start, and `FrontendPrimaryReady` is reported, only once the backend admits I/O (a chunk volume: after its storage claim; attach-pending or fenced reports not ready with the reason); a workspace primary claims HEAD, listens and reports ready only while its lease guard permits now (`WorkspaceLeaseGuard.Held`; without a grant: `NotReadyReason=lease_not_held`, exit 75 after the start-time wait) | `core/storage/walstore_v3.go` `recoverV3`; `core/storage/walstore.go` `WALStore.Recover`; `cmd/blockvolume/frontend_gate.go` `awaitFrontendStart`; `core/frontend/durable/storage_adapter.go` `FirstServing`; `core/host/volume/status_server.go` `statusProjection`; `core/host/volume/workspace_lease.go` `Held` | `INV-WAL-SYNCED-RECOVERED-001`, `INV-WAL-REPLAY-LWW-001`, `INV-WAL-FRONTIER-GAP-001`, `INV-CUT-RECOVER-BARRIER-001`, `INV-WAL-V3-W-RECOVERY-001`, `INV-WAL-V3-FENCE-001`, `INV-WAL-V3-MAP-PUBLISH-001`, `INV-FE-PRIMARY-READY-OPERATIONAL-001`, `INV-WS-SERVE-LEASE-HELD-001` | | Recovering | RecoveryFailed | a WAL integrity failure or an I/O error; the backend stays not operational and the host blocks local readiness with `wal_corrupt` or `io_error` | `cmd/blockvolume/durable_startup.go` `openAndRecoverDurable`; `core/host/volume/recovery_watchdog.go` `LocalRecoveryHealthReason` | `INV-WAL-TYPED-FAILCLOSED-001`, `INV-WAL-OPGATE-001`, `INV-WAL-V3-W-RECOVERY-001` |