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

140 lines
4.3 KiB
Go

package util
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
// MinFreeSpaceType is the type of MinFreeSpace.
type MinFreeSpaceType int
const (
// AsPercent set the MinFreeSpaceType to a percentage value from 0 to 100.
AsPercent MinFreeSpaceType = iota
// AsBytes set the MinFreeSpaceType to a absolute value bytes.
AsBytes
)
// MinFreeSpace is type that defines the limit for the minimum free space.
type MinFreeSpace struct {
Type MinFreeSpaceType
Bytes uint64
Percent float32
Raw string
}
// IsLow tells whether the free space is low or not.
func (s MinFreeSpace) IsLow(freeBytes uint64, freePercent float32) (yes bool, desc string) {
switch s.Type {
case AsPercent:
yes = freePercent < s.Percent
op := IfElse(yes, "<", ">=")
return yes, fmt.Sprintf("disk free %.2f%% %s required %.2f%%", freePercent, op, s.Percent)
case AsBytes:
yes = freeBytes < s.Bytes
op := IfElse(yes, "<", ">=")
return yes, fmt.Sprintf("disk free %s %s required %s",
BytesToHumanReadable(freeBytes), op, BytesToHumanReadable(s.Bytes))
}
return false, ""
}
// MinFreeSpaceReleaseMarginPercent is how far above the limit, in percent of
// the disk, free space must climb before a low location is writable again.
const MinFreeSpaceReleaseMarginPercent = 1
// IsLowFrom is IsLow with memory: a location that was low stays low until free
// space is MinFreeSpaceReleaseMarginPercent of the disk above the limit, so a
// disk hovering at the limit does not flip read-only on every check.
func (s MinFreeSpace) IsLowFrom(wasLow bool, freeBytes, totalBytes uint64, freePercent float32) (yes bool, desc string) {
yes, desc = s.IsLow(freeBytes, freePercent)
if !wasLow || yes {
return yes, desc
}
switch s.Type {
case AsPercent:
release := s.Percent + MinFreeSpaceReleaseMarginPercent
if freePercent < release {
return true, fmt.Sprintf("disk free %.2f%% >= required %.2f%% but still below %.2f%% needed to leave the low-disk state",
freePercent, s.Percent, release)
}
case AsBytes:
release := s.Bytes + totalBytes/100*MinFreeSpaceReleaseMarginPercent
if release < s.Bytes { // overflow
release = math.MaxUint64
}
if freeBytes < release {
return true, fmt.Sprintf("disk free %s >= required %s but still below %s needed to leave the low-disk state",
BytesToHumanReadable(freeBytes), BytesToHumanReadable(s.Bytes), BytesToHumanReadable(release))
}
}
return false, desc
}
func (s MinFreeSpace) AvailableSpace(freeBytes uint64, totalBytes uint64) uint64 {
var minFreeSpaceBytes uint64
switch s.Type {
case AsPercent:
minFreeSpaceBytes = uint64((float32(totalBytes) * s.Percent) / 100)
case AsBytes:
minFreeSpaceBytes = s.Bytes
}
if minFreeSpaceBytes > freeBytes {
return 0
}
return freeBytes - minFreeSpaceBytes
}
// String returns a string representation of MinFreeSpace.
func (s MinFreeSpace) String() string {
switch s.Type {
case AsPercent:
return fmt.Sprintf("%.2f%%", s.Percent)
default:
return s.Raw
}
}
// MustParseMinFreeSpace parses comma-separated argument for min free space setting.
// minFreeSpace has the high priority than minFreeSpacePercent if it is set.
func MustParseMinFreeSpace(minFreeSpace string, minFreeSpacePercent string) (spaces []MinFreeSpace) {
ss := strings.Split(EmptyTo(minFreeSpace, minFreeSpacePercent), ",")
for _, freeString := range ss {
if vv, e := ParseMinFreeSpace(freeString); e == nil {
spaces = append(spaces, *vv)
} else {
glog.Fatalf("The value specified in -minFreeSpace not a valid value %s", freeString)
}
}
return spaces
}
var ErrMinFreeSpaceBadValue = errors.New("minFreeSpace is invalid")
// ParseMinFreeSpace parses min free space expression s as percentage like 1,10 or human readable size like 10G
func ParseMinFreeSpace(s string) (*MinFreeSpace, error) {
s = strings.TrimSpace(s)
if percent, e := strconv.ParseFloat(s, 32); e == nil {
if math.IsNaN(percent) || percent < 0 || percent > 100 {
return nil, ErrMinFreeSpaceBadValue
}
return &MinFreeSpace{Type: AsPercent, Percent: float32(percent), Raw: s}, nil
}
if directSize, e := ParseBytes(s); e == nil {
if directSize <= 100 {
return nil, ErrMinFreeSpaceBadValue
}
return &MinFreeSpace{Type: AsBytes, Bytes: directSize, Raw: s}, nil
}
return nil, ErrMinFreeSpaceBadValue
}