master: make volume capacity reservation timeout configurable (#11426) (#11497)

* master: make volume capacity reservation timeout configurable (#11426)

* master: expire reservations on reads, fix int timeout units

- AvailableSpaceForReservation now expires reservations too: a node that
  is full of reservations is filtered out before TryReserveCapacity can
  clean them, which stranded expired capacity indefinitely.
- Drop TryReserveCapacityWithTimeout: a per-call timeout lets one caller
  expire another's live reservations, and the Node interface stays
  stable for implementations outside this tree.
- parseReservationTimeout no longer routes integer values through
  GetDuration, which read them as nanoseconds; bare numbers are
  seconds. The 5m fallback is now the shared DefaultReservationTimeout.

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
yi111andChris Lu authored and GitHub committed 2026-09-28 18:40:11 +08:00
1 parent 4fec65d949
commit 150a69fe11
6 files changed
+235 -14

No files matched your search

+15
View File
@@ -119,6 +119,7 @@ func NewMasterServer(r *mux.Router, option *MasterOption, peers map[string]pb.Se
v.SetDefault("master.volume_growth.copy_3", topology.VolumeGrowStrategy.Copy3Count)
v.SetDefault("master.volume_growth.copy_other", topology.VolumeGrowStrategy.CopyOtherCount)
v.SetDefault("master.volume_growth.threshold", topology.VolumeGrowStrategy.Threshold)
v.SetDefault("master.volume_growth.reservation_timeout", "5m")
v.SetDefault("master.volume_growth.disable", false)
option.VolumeGrowthDisabled = v.GetBool("master.volume_growth.disable")
@@ -127,6 +128,7 @@ func NewMasterServer(r *mux.Router, option *MasterOption, peers map[string]pb.Se
topology.VolumeGrowStrategy.Copy3Count = v.GetUint32("master.volume_growth.copy_3")
topology.VolumeGrowStrategy.CopyOtherCount = v.GetUint32("master.volume_growth.copy_other")
topology.VolumeGrowStrategy.Threshold = v.GetFloat64("master.volume_growth.threshold")
topology.VolumeGrowStrategy.ReservationTimeout = parseReservationTimeout(v)
whiteList := util.StringSplit(v.GetString("guard.white_list"), ",")
var preallocateSize int64
@@ -663,3 +665,16 @@ func (ms *MasterServer) Reload() {
v.GetInt("jwt.signing.read.expires_after_seconds"),
)
}
func parseReservationTimeout(v *util.ViperProxy) time.Duration {
if str := strings.TrimSpace(v.GetString("master.volume_growth.reservation_timeout")); str != "" {
if d, err := time.ParseDuration(str); err == nil && d > 0 {
return d
}
}
// A bare number in the config means seconds; GetDuration would read it as nanoseconds.
if sec := v.GetInt("master.volume_growth.reservation_timeout"); sec > 0 {
return time.Duration(sec) * time.Second
}
return topology.DefaultReservationTimeout
}
@@ -0,0 +1,85 @@
package weed_server
import (
"testing"
"time"
"github.com/spf13/viper"
"github.com/seaweedfs/seaweedfs/weed/util"
)
func TestParseReservationTimeout(t *testing.T) {
tests := []struct {
name string
setup func(v *util.ViperProxy)
expected time.Duration
}{
{
name: "unset defaults to 5 minutes",
setup: func(v *util.ViperProxy) {},
expected: 5 * time.Minute,
},
{
name: "duration string 10m",
setup: func(v *util.ViperProxy) {
v.Set("master.volume_growth.reservation_timeout", "10m")
},
expected: 10 * time.Minute,
},
{
name: "duration string 30s",
setup: func(v *util.ViperProxy) {
v.Set("master.volume_growth.reservation_timeout", "30s")
},
expected: 30 * time.Second,
},
{
name: "integer seconds 300",
setup: func(v *util.ViperProxy) {
v.Set("master.volume_growth.reservation_timeout", 300)
},
expected: 300 * time.Second,
},
{
name: "large integer stays seconds not nanoseconds",
setup: func(v *util.ViperProxy) {
v.Set("master.volume_growth.reservation_timeout", 1000000000)
},
expected: 1000000000 * time.Second,
},
{
name: "non-positive zero defaults to 5 minutes",
setup: func(v *util.ViperProxy) {
v.Set("master.volume_growth.reservation_timeout", 0)
},
expected: 5 * time.Minute,
},
{
name: "negative seconds defaults to 5 minutes",
setup: func(v *util.ViperProxy) {
v.Set("master.volume_growth.reservation_timeout", -10)
},
expected: 5 * time.Minute,
},
{
name: "invalid string defaults to 5 minutes",
setup: func(v *util.ViperProxy) {
v.Set("master.volume_growth.reservation_timeout", "invalid")
},
expected: 5 * time.Minute,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rawViper := viper.New()
vp := util.NewViperProxy(rawViper)
tt.setup(vp)
actual := parseReservationTimeout(vp)
if actual != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, actual)
}
})
}
}