Files
seaweedfs/weed/storage/disk_location_minfree_test.go
T
e9fa5802e1 volume: keep a low-disk location read-only until free space is back by a margin (#13199)
* volume: keep a low-disk location read-only until free space is back by a margin

The volume server checks each disk location once a minute and sets the
location's low-disk flag from that reading alone. A disk whose free
space hovers at -minFreeSpace (writes use space, deletes and vacuum give
a little back) flips the flag on and off, and every volume on the
location with it. The master hears each flip in a heartbeat, so it keeps
assigning writes to volumes that are about to turn read-only, and the
write fails with a read-only error. Nothing smooths this: the volume
reads the location's flag directly, and the master's volume layout takes
the heartbeat's read-only bit as it comes (its recovery delay and size
band apply to full volumes, not to read-only ones).

A location that went low now stays low until its free space is 1% of the
disk above the limit, for a percentage limit and a byte limit alike
(MinFreeSpace.IsLowFrom). Going low is unchanged. The margin is a
constant, not a flag: what flips a disk at the limit within one check is
a vacuum, a volume copy or the writes of one minute, each about one
volume (30 GB by default) at most. 1% of a multi-TB disk is about that
much, so a swing of that size does not clear the state, while an
ordinary cleanup of a few volumes does. The disk check runs on one
goroutine per location and is the only writer of the flag, so the
previous verdict it reads cannot change under it; readers only load it.

Every consumer of the flag (heartbeat max volume counts, read-only
flags, volume allocation, EC shard placement) reads the same flag, so
they all see the held state. The live checks of volume copy and
preallocation, which read the disk instead of the flag, only ever refuse
more than the flag does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume: do not remember a low verdict from a reading without space values

When statfs fails or times out before any good reading is cached, the
disk status has All, Free and PercentFree at 0, and MinFreeSpace.IsLow
calls that low. With the hysteresis, that verdict was remembered, so a
later good reading between the limit and limit+1% kept the location
read-only, where it used to clear it: a disk whose first check timed out
stayed read-only until its free space cleared the margin.

A reading without space values (All is 0) is now judged by IsLow alone,
as before, and does not change the remembered verdict, which lives in
its own atomic field that only readings with space values update. A
failed reading between two good ones leaves the held verdict as it was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rs): keep a low-disk location read-only until free space is back by a margin

Mirror Go's MinFreeSpace.IsLowFrom: once a location goes low it stays low
until free space is 1% of the disk above the limit, for percentage and
byte limits alike. A failed statfs reading (total 0) already leaves the
flag untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume: trim the low-disk hysteresis comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rs): test the low-disk hysteresis with fixed readings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-01 00:17:29 -08:00

151 lines
4.8 KiB
Go

package storage
import (
"sync"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
"github.com/seaweedfs/seaweedfs/weed/util"
)
const minFreeTestDiskBytes = uint64(1000) << 30
// readingAt is a disk reading with the given percentage of free space.
func readingAt(percent float64) *volume_server_pb.DiskStatus {
return &volume_server_pb.DiskStatus{
All: minFreeTestDiskBytes,
Free: uint64(float64(minFreeTestDiskBytes) * percent / 100),
PercentFree: float32(percent),
}
}
func newMinFreeTestLocation() *DiskLocation {
return &DiskLocation{
DiskType: types.HardDriveType,
MinFreeSpace: util.MinFreeSpace{Type: util.AsPercent, Percent: 7},
}
}
// A disk whose free space hovers at -minFreeSpacePercent must not turn its
// volumes read-only and writable again on every check.
func TestDiskSpaceLowStaysUntilFreeSpaceIsClearlyBack(t *testing.T) {
location := newMinFreeTestLocation()
steps := []struct {
percent float64
wantLow bool
}{
{7.50, false},
{6.99, true}, // writes take it under the limit
{7.02, true}, // a delete gives a little back
{6.98, true}, // more writes
{7.05, true}, // vacuum gives back more
{6.99, true},
{7.60, true}, // still inside the margin
{8.00, false}, // clearly back
{7.50, false}, // writes after the release do not trigger it above the limit
{6.99, true}, // crossing the limit again does
}
flips := 0
prev := false
for i, step := range steps {
isLow, desc := location.updateDiskSpaceLow(readingAt(step.percent))
if isLow != step.wantLow || location.isDiskSpaceLow.Load() != step.wantLow {
t.Fatalf("check %d (%.2f%% free): low = %v, flag = %v, want %v (%s)",
i, step.percent, isLow, location.isDiskSpaceLow.Load(), step.wantLow, desc)
}
if isLow != prev {
flips++
prev = isLow
}
}
if flips != 3 {
t.Fatalf("state flipped %d times over the sequence, want 3 (enter, leave, enter)", flips)
}
}
// A reading without space values (statfs failed before any good reading) is
// low, as on a disk with no space, but that verdict is not remembered: the
// next good reading above the limit releases the location at once.
func TestDiskSpaceLowFromFailedReadingIsNotHeld(t *testing.T) {
location := newMinFreeTestLocation()
if isLow, _ := location.updateDiskSpaceLow(&volume_server_pb.DiskStatus{Error: "statfs timeout"}); !isLow {
t.Fatal("a reading without space values must still be low")
}
if !location.isDiskSpaceLow.Load() {
t.Fatal("flag not set by a reading without space values")
}
// 7.5% is above the 7% limit but inside the 1% margin.
if isLow, desc := location.updateDiskSpaceLow(readingAt(7.5)); isLow {
t.Fatalf("a good reading above the limit stayed low after a failed one: %s", desc)
}
if location.isDiskSpaceLow.Load() {
t.Fatal("flag still set after a good reading above the limit")
}
// A held verdict survives a failed reading in between.
location.updateDiskSpaceLow(readingAt(6.9))
location.updateDiskSpaceLow(&volume_server_pb.DiskStatus{Error: "statfs timeout"})
if isLow, desc := location.updateDiskSpaceLow(readingAt(7.5)); !isLow {
t.Fatalf("a failed reading between two good ones dropped the held verdict: %s", desc)
}
}
// A byte limit is held the same way.
func TestDiskSpaceLowStaysUntilFreeSpaceIsClearlyBackForByteLimit(t *testing.T) {
const limit = uint64(70) << 30
location := &DiskLocation{
DiskType: types.HardDriveType,
MinFreeSpace: util.MinFreeSpace{Type: util.AsBytes, Bytes: limit, Raw: "70GiB"},
}
at := func(free uint64) *volume_server_pb.DiskStatus {
return &volume_server_pb.DiskStatus{All: minFreeTestDiskBytes, Free: free, PercentFree: float32(float64(free) / float64(minFreeTestDiskBytes) * 100)}
}
margin := minFreeTestDiskBytes / 100
for i, step := range []struct {
free uint64
wantLow bool
}{
{limit + 1, false},
{limit - 1, true},
{limit + 1, true},
{limit + margin - 1, true},
{limit + margin, false},
{limit + 1, false},
} {
if isLow, desc := location.updateDiskSpaceLow(at(step.free)); isLow != step.wantLow {
t.Fatalf("check %d (free %d): low = %v, want %v (%s)", i, step.free, isLow, step.wantLow, desc)
}
}
}
// The flag is read by the heartbeat, the write path and the volumes' read-only
// reports while the disk check updates it.
func TestDiskSpaceLowIsSafeToReadWhileItIsUpdated(t *testing.T) {
location := newMinFreeTestLocation()
done := make(chan struct{})
var readers sync.WaitGroup
for i := 0; i < 4; i++ {
readers.Add(1)
go func() {
defer readers.Done()
for {
select {
case <-done:
return
default:
_ = location.isDiskSpaceLow.Load()
}
}
}()
}
for i := 0; i < 1000; i++ {
location.updateDiskSpaceLow(readingAt([]float64{6.9, 7.1, 7.5, 8.2, 7.1}[i%5]))
}
close(done)
readers.Wait()
}