Files
seaweedfs/weed/topology/free_volume_count_test.go
T
5681be241e topology: never report negative free volume slots (#12698)
* topology: never report negative free volume slots

Free volume slots are computed as max + remote - volume - ecSlots. When phantom or over-registered volumes push the count past the configured max, the result is negative and rolls up into the cluster total, causing volume grow and assign to fail with a negative 'volumes left' count even while healthy servers have capacity. Clamp every free-volume count at zero so an over-committed server contributes no slots instead of subtracting from the cluster.

* topology: drop the issue reference from the free-slot test comment

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

* topology: test free slots next to an over-committed server

A server with more volumes than slots must not hide the free slots of
the other servers in its rack, data center or topology.

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

* topology: sum free slots per data node in racks, data centers and topology

Clamping the rolled-up counters only clamped their sum, so one server with
more volumes than slots still hid the free slots of every other server in
its rack, data center and topology. Interior nodes now add up their
children's free slots, so each data node counts as zero at worst. A data
node in maintenance mode no longer adds slots to its parents either.

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

* topology: test that disk info keeps an over-committed deficit

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

* topology: keep the signed free volume count in disk info

EC encode retries and the balance planners add refunds and planned moves
to the published FreeVolumeCount. Clamping it at zero dropped an
over-committed disk's deficit, so a refund could make a full disk look
free. Volume growth already reads the clamped AvailableSpaceFor.

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

* topology: test placement next to an over-committed server

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

* topology: accept either data center order in the placement spread check

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

---------

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

107 lines
4.1 KiB
Go

package topology
import (
"testing"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
)
// An over-committed node (more volumes registered than it has slots for) must
// report zero free slots, not a negative count that drags the cluster total
// below the slots healthy servers can still offer.
func TestFreeVolumeCountNeverNegative(t *testing.T) {
topo := NewTopology("free-slots", nil, 32*1024*1024*1024, 5, false)
dn := topo.GetOrCreateDataCenter("dc1").GetOrCreateRack("rack1").
GetOrCreateDataNode("10.0.0.1", 8080, 18080, "", "", map[string]uint32{"": 5})
// 8 volumes registered on a server configured for 5: 5 + 0 - 8 = -3.
dn.getOrCreateDisk("").UpAdjustDiskUsageDelta("", &DiskUsageCounts{volumeCount: 8})
option := &VolumeGrowOption{DiskType: types.HardDriveType}
if got := dn.AvailableSpaceFor(option); got != 0 {
t.Errorf("data node AvailableSpaceFor = %d, want 0 (over-committed slots must not go negative)", got)
}
if got := topo.AvailableSpaceFor(option); got != 0 {
t.Errorf("topology AvailableSpaceFor = %d, want 0", got)
}
if got := dn.diskUsages.FreeSpace(); got != 0 {
t.Errorf("diskUsages.FreeSpace = %d, want 0", got)
}
// Planners add refunds and moves to the published count, so it keeps the deficit.
if got := dn.diskUsages.ToDiskInfo()[""].FreeVolumeCount; got != -3 {
t.Errorf("disk info FreeVolumeCount = %d, want -3", got)
}
}
func TestFreeVolumeCountPositiveCaseUnchanged(t *testing.T) {
topo := NewTopology("free-slots", nil, 32*1024*1024*1024, 5, false)
dn := topo.GetOrCreateDataCenter("dc1").GetOrCreateRack("rack1").
GetOrCreateDataNode("10.0.0.1", 8080, 18080, "", "", map[string]uint32{"": 5})
dn.getOrCreateDisk("").UpAdjustDiskUsageDelta("", &DiskUsageCounts{volumeCount: 2})
option := &VolumeGrowOption{DiskType: types.HardDriveType}
if got := dn.AvailableSpaceFor(option); got != 3 {
t.Errorf("AvailableSpaceFor = %d, want 3", got)
}
if got := dn.diskUsages.ToDiskInfo()[""].FreeVolumeCount; got != 3 {
t.Errorf("disk info FreeVolumeCount = %d, want 3", got)
}
}
func TestFreeVolumeCountSkipsOverCommittedServer(t *testing.T) {
topo := NewTopology("free-slots", nil, 32*1024*1024*1024, 5, false)
dc1 := topo.GetOrCreateDataCenter("dc1")
rack1 := dc1.GetOrCreateRack("rack1")
over := rack1.GetOrCreateDataNode("10.0.0.1", 8080, 18080, "", "", map[string]uint32{"": 5})
over.getOrCreateDisk("").UpAdjustDiskUsageDelta("", &DiskUsageCounts{volumeCount: 20})
healthy := rack1.GetOrCreateDataNode("10.0.0.2", 8080, 18080, "", "", map[string]uint32{"": 10})
dc2 := topo.GetOrCreateDataCenter("dc2")
other := dc2.GetOrCreateRack("rack1").GetOrCreateDataNode("10.0.0.3", 8080, 18080, "", "", map[string]uint32{"": 4})
other.getOrCreateDisk("").UpAdjustDiskUsageDelta("", &DiskUsageCounts{volumeCount: 1})
option := &VolumeGrowOption{DiskType: types.HardDriveType}
for _, c := range []struct {
name string
node Node
want int64
}{
{"rack1", rack1, 10},
{"dc1", dc1, 10},
{"dc2", dc2, 3},
{"topology", topo, 13},
} {
if got := c.node.AvailableSpaceFor(option); got != c.want {
t.Errorf("%s AvailableSpaceFor = %d, want %d", c.name, got, c.want)
}
}
for i := 0; i < 20; i++ {
first, rest, err := topo.PickNodesByWeight(2, option, func(Node) error { return nil })
if err != nil {
t.Fatalf("topology PickNodesByWeight: %v", err)
}
if picked := map[Node]bool{first: true, rest[0]: true}; !picked[dc1] || !picked[dc2] {
t.Fatalf("topology picked %s and %s, want dc1 and dc2", first.Id(), rest[0].Id())
}
first, _, err = rack1.PickNodesByWeight(1, option, func(Node) error { return nil })
if err != nil {
t.Fatalf("rack1 PickNodesByWeight: %v", err)
}
if first != Node(healthy) {
t.Fatalf("rack1 picked %s, want %s", first.Id(), healthy.Id())
}
}
assigned := map[*DataNode]int{}
for r := int64(0); r < 13; r++ {
dn, err := topo.ReserveOneVolume(r, option)
if err != nil {
t.Fatalf("ReserveOneVolume(%d): %v", r, err)
}
assigned[dn]++
}
if assigned[over] != 0 || assigned[healthy] == 0 {
t.Errorf("ReserveOneVolume spread over=%d healthy=%d other=%d, want the healthy servers only",
assigned[over], assigned[healthy], assigned[other])
}
}