diff --git a/seaweed-block/core/frontend/durable/headroom.go b/seaweed-block/core/frontend/durable/headroom.go new file mode 100644 index 0000000000..ba0d13865d --- /dev/null +++ b/seaweed-block/core/frontend/durable/headroom.go @@ -0,0 +1,43 @@ +package durable + +import ( + "fmt" + "log" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// rootFree probes the free bytes of the durable root's file system (nil where +// it cannot tell); tests replace it. +var rootFree = spillFree + +// Operator guide, Size a host: a full local disk fails guest writes, so a +// volume is created only with room for its full size and its WAL, and an +// existing volume opens with a loud warning when its remaining sparse growth +// and the WAL no longer fit. Neither adds state. +func (p *DurableProvider) refuseCreateWithoutHeadroom(volumeID string) error { + free := rootFree(p.cfg.StorageRoot) + if free == nil { + return nil + } + have, err := free() + size := int64(p.cfg.NumBlocks) * int64(p.cfg.BlockSize) + if need := size + storage.DefaultWALBytes; err == nil && have < need { + return fmt.Errorf("durable: %s has %d bytes free; volume %s needs %d (size %d + WAL %d): free space on it or use a larger file system", + p.cfg.StorageRoot, have, volumeID, need, size, storage.DefaultWALBytes) + } + return nil +} + +func (p *DurableProvider) warnLowHeadroom(volumeID, path string, size int64) { + free := rootFree(p.cfg.StorageRoot) + if free == nil { + return + } + have, err := free() + growth := max(size-allocatedBytes(path), 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_export_test.go b/seaweed-block/core/frontend/durable/headroom_export_test.go new file mode 100644 index 0000000000..9be29f63d7 --- /dev/null +++ b/seaweed-block/core/frontend/durable/headroom_export_test.go @@ -0,0 +1,9 @@ +package durable + +// SetRootFreeForTest replaces the durable root's free-space probe and returns +// the restore function. +func SetRootFreeForTest(free int64) func() { + old := rootFree + rootFree = func(string) func() (int64, error) { return func() (int64, error) { return free, nil } } + return func() { rootFree = old } +} diff --git a/seaweed-block/core/frontend/durable/headroom_test.go b/seaweed-block/core/frontend/durable/headroom_test.go new file mode 100644 index 0000000000..5463a8804a --- /dev/null +++ b/seaweed-block/core/frontend/durable/headroom_test.go @@ -0,0 +1,62 @@ +package durable_test + +import ( + "bytes" + "log" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/durable" +) + +// 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. +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) + } +} + +// 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. +func TestOpenWarnsWithoutLocalHeadroom(t *testing.T) { + root := t.TempDir() + cfg := durable.ProviderConfig{Impl: durable.ImplWALStore, StorageRoot: root, BlockSize: 4096, NumBlocks: 64} + p, err := durable.NewDurableProvider(cfg, v1View()) + if err != nil { + t.Fatal(err) + } + if _, err := p.EnsureStorage("v1"); err != nil { + 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()) + } +} diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index 7dfc67abfc..c18becd6b4 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -938,6 +938,7 @@ func (p *DurableProvider) openExisting(volumeID, path string, impl ImplName) (*v if err != nil { return nil, fmt.Errorf("durable: OpenWALStore %s: %w", path, err) } + p.warnLowHeadroom(volumeID, path, int64(ws.NumBlocks())*int64(ws.BlockSize())) // G6 §1.A α: plumb operator-tunable retention into walstore. // Zero (pre-G6 default) preserves strict checkpoint-driven // recycle. @@ -988,7 +989,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 { + } else if err = p.refuseCreateWithoutHeadroom(volumeID); err == nil { 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/core/frontend/durable/spill_free_linux.go b/seaweed-block/core/frontend/durable/spill_free_linux.go index 92843c70ff..d3a3516804 100644 --- a/seaweed-block/core/frontend/durable/spill_free_linux.go +++ b/seaweed-block/core/frontend/durable/spill_free_linux.go @@ -15,3 +15,12 @@ func spillFree(dir string) func() (int64, error) { return int64(st.Bavail) * int64(st.Bsize), nil } } + +// allocatedBytes is the space path's file holds on disk (0 if unknown). +func allocatedBytes(path string) int64 { + var st syscall.Stat_t + if syscall.Stat(path, &st) != nil { + return 0 + } + return st.Blocks * 512 +} diff --git a/seaweed-block/core/frontend/durable/spill_free_other.go b/seaweed-block/core/frontend/durable/spill_free_other.go index 7ba44e278c..7c3a16135f 100644 --- a/seaweed-block/core/frontend/durable/spill_free_other.go +++ b/seaweed-block/core/frontend/durable/spill_free_other.go @@ -4,3 +4,5 @@ package durable // spillFree is nil off Linux: the COW spill cap is the configured one. func spillFree(string) func() (int64, error) { return nil } + +func allocatedBytes(string) int64 { return 0 } diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index 250a6bfa45..afccc9023f 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -171,6 +171,9 @@ type WALStore struct { // numBlocks defines the addressable block range; blockSize is the IO // unit (default 4096 if zero). The on-disk file is preallocated to // hold the superblock + WAL region + a fully addressable extent. +// DefaultWALBytes is the WAL region a created store reserves. +const DefaultWALBytes = 64 << 20 + func CreateWALStore(path string, numBlocks uint32, blockSize int) (*WALStore, error) { return CreateWALStoreWithOptions(path, numBlocks, blockSize, WALStoreOptions{}) } @@ -192,7 +195,7 @@ func CreateWALStoreWithOptions(path string, numBlocks uint32, blockSize int, opt if cb == 0 { cb = 1024 } - return createWALStoreV3Configured(path, numBlocks, uint32(blockSize), 64*1024*1024, opts.WMode, 0, uint64(cb), opts) + return createWALStoreV3Configured(path, numBlocks, uint32(blockSize), DefaultWALBytes, opts.WMode, 0, uint64(cb), opts) } // OpenWALStore opens an existing store file WITHOUT recovering it.