Files
seaweedfs/weed/util/minfreespace_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

125 lines
4.1 KiB
Go

package util
import "testing"
func TestParseMinFreeSpace(t *testing.T) {
tests := []struct {
in string
ok bool
value *MinFreeSpace
}{
{in: "42", ok: true, value: &MinFreeSpace{Type: AsPercent, Percent: 42, Raw: "42"}},
{in: "-1", ok: false, value: nil},
{in: "101", ok: false, value: nil},
{in: "100B", ok: false, value: nil},
{in: "100Ki", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 100 * 1024, Raw: "100Ki"}},
{in: "100GiB", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 100 * 1024 * 1024 * 1024, Raw: "100GiB"}},
{in: "42M", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 42 * 1000 * 1000, Raw: "42M"}},
{in: " 42 ", ok: true, value: &MinFreeSpace{Type: AsPercent, Percent: 42, Raw: "42"}},
{in: " 42M ", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 42 * 1000 * 1000, Raw: "42M"}},
}
for _, p := range tests {
got, err := ParseMinFreeSpace(p.in)
if p.ok != (err == nil) {
t.Errorf("failed to test %v", p.in)
}
if p.ok && err == nil && *got != *p.value {
t.Errorf("failed to test %v", p.in)
}
}
}
// A location that went low must not flip back on every reading that touches
// the limit; it is released only once free space is the margin above it.
func TestMinFreeSpaceIsLowFromPercent(t *testing.T) {
const total = uint64(1000) << 30
s := MinFreeSpace{Type: AsPercent, Percent: 7}
free := func(percent float64) (uint64, uint64, float32) {
return uint64(float64(total) * percent / 100), total, float32(percent)
}
// readings, as free percent, and the verdict wanted after each check.
readings := []struct {
percent float64
wantLow bool
}{
{7.50, false}, // above the limit
{7.00, false}, // exactly at the limit is not low
{6.99, true}, // crosses the limit
{7.01, true}, // a delete gives a little back: still low
{6.98, true},
{7.40, true},
{7.99, true}, // not yet the margin above the limit
{8.00, false},
{7.60, false}, // released, and stays so until it crosses the limit again
{6.99, true},
}
low := false
for i, r := range readings {
freeBytes, totalBytes, pct := free(r.percent)
var desc string
low, desc = s.IsLowFrom(low, freeBytes, totalBytes, pct)
if low != r.wantLow {
t.Fatalf("reading %d (%.2f%% free): low = %v, want %v (%s)", i, r.percent, low, r.wantLow, desc)
}
}
}
func TestMinFreeSpaceIsLowFromBytes(t *testing.T) {
const total = uint64(10) << 40 // 10 TiB: the margin is 1% of it
const limit = uint64(100) << 30
margin := total / 100
s := MinFreeSpace{Type: AsBytes, Bytes: limit, Raw: "100GiB"}
steps := []struct {
free uint64
wasLow bool
wantLow bool
}{
{limit + 1, false, false},
{limit, false, false},
{limit - 1, false, true},
{limit + 1, true, true},
{limit + margin - 1, true, true},
{limit + margin, true, false},
{limit + margin + 1, true, false},
// Not low before: the margin plays no part in going low.
{limit + margin - 1, false, false},
}
for i, st := range steps {
got, desc := s.IsLowFrom(st.wasLow, st.free, total, float32(float64(st.free)/float64(total)*100))
if got != st.wantLow {
t.Errorf("step %d (free %d, wasLow %v): low = %v, want %v (%s)", i, st.free, st.wasLow, got, st.wantLow, desc)
}
}
}
// Without a previous low verdict IsLowFrom is IsLow.
func TestMinFreeSpaceIsLowFromMatchesIsLowWhenNotLow(t *testing.T) {
for _, s := range []MinFreeSpace{
{Type: AsPercent, Percent: 7},
{Type: AsBytes, Bytes: 10 << 30},
{},
} {
for _, free := range []uint64{0, 5 << 30, 10 << 30, 70 << 30, 71 << 30, 500 << 30} {
const total = uint64(1000) << 30
pct := float32(float64(free) / float64(total) * 100)
want, _ := s.IsLow(free, pct)
got, _ := s.IsLowFrom(false, free, total, pct)
if got != want {
t.Errorf("%v free %d: IsLowFrom(false) = %v, IsLow = %v", s, free, got, want)
}
}
}
}
// A byte limit larger than the disk's headroom to MaxUint64 must not wrap the
// release level around to a small value.
func TestMinFreeSpaceIsLowFromBytesOverflow(t *testing.T) {
s := MinFreeSpace{Type: AsBytes, Bytes: ^uint64(0) - 10}
if low, _ := s.IsLowFrom(true, ^uint64(0)-5, ^uint64(0), 100); !low {
t.Fatal("a wrapped release level released a low location")
}
}