diff --git a/weed/shell/command_volume_balance.go b/weed/shell/command_volume_balance.go index 77c086c07c..4c7d4294f3 100644 --- a/weed/shell/command_volume_balance.go +++ b/weed/shell/command_volume_balance.go @@ -573,6 +573,9 @@ func (c *commandVolumeBalance) planBalance(diskType types.DiskType, nodes []*Nod return nil, nil, 0, volumeSizeLimitMb, false } idealVolumeRatio = float64(selectedVolumeCount) / volumeCapacities + if c.byDiskUsage { + idealVolumeRatio = idealRatioOfReceivingServers(diskType, nodesWithCapacity, capacityFunc, selectedVolumeCount, volumeCapacities) + } if c.commandEnv != nil && c.commandEnv.verbose { fmt.Fprintf(os.Stdout, "selected nodes %d, volumes:%d, cap:%d, idealVolumeRatio %f\n", len(nodesWithCapacity), selectedVolumeCount, int64(volumeCapacities), idealVolumeRatio*100) @@ -580,6 +583,40 @@ func (c *commandVolumeBalance) planBalance(diskType types.DiskType, nodes []*Nod return nodesWithCapacity, capacityFunc, idealVolumeRatio, volumeSizeLimitMb, true } +// idealRatioOfReceivingServers is the ideal ratio of a -byDiskUsage run. +// planBalanceMove never picks a server without a free volume slot as a target, +// so such a server below the ideal stays there, and counting its share of the +// capacity holds the ideal under the level the other servers can reach: the +// targets' next ratio then exceeds it and no move is planned. These servers are +// left out until none is below the ideal of the rest; one above it still counts, +// as a source. +func idealRatioOfReceivingServers(diskType types.DiskType, nodes []*Node, capacityFunc DensityFunc, usedVolumes uint64, capacity float64) float64 { + ideal := float64(usedVolumes) / capacity + leftOut := make(map[*Node]bool) + for { + changed := false + for _, n := range nodes { + if leftOut[n] || n.hasFreeVolumeSlot(diskType) { + continue + } + // the ratio the ideal is made of: DensityRatio puts a floor under a + // server with no whole volume used, which could keep it in + nodeCapacity, nodeUsed := capacityFunc(n.info) + if nodeCapacity <= 0 || float64(nodeUsed)/nodeCapacity >= ideal { + continue + } + leftOut[n] = true + usedVolumes -= nodeUsed + capacity -= nodeCapacity + changed = true + } + if !changed || capacity <= 0 { + return ideal + } + ideal = float64(usedVolumes) / capacity + } +} + // planBalanceMove selects one volume move without mutating the topology. The // caller reserves the returned move before handing it to the shared executor. func (c *commandVolumeBalance) planBalanceMove(diskType types.DiskType, volumeReplicas map[uint32][]*VolumeReplica, nodesWithCapacity []*Node, sortCandidatesFn func(volumes []*master_pb.VolumeInformationMessage), capacityFunc DensityFunc, idealVolumeRatio float64, volumeSizeLimitMb uint64, failedTargets map[string]struct{}) *volumeBalanceMove { diff --git a/weed/shell/command_volume_balance_test.go b/weed/shell/command_volume_balance_test.go index 56ffe70f82..3eb4024efc 100644 --- a/weed/shell/command_volume_balance_test.go +++ b/weed/shell/command_volume_balance_test.go @@ -638,6 +638,109 @@ func TestBalanceByDiskUsageMixedReportingFallsBackToDataSize(t *testing.T) { } } +// slotNode is a 1000 GB disk with 10 GB volumes, so each volume is one percent +// of it, used percent used. Without free it has no free volume slot. +func slotNode(id string, firstVolumeId uint32, used uint32, free bool) *Node { + const gb = 1024 * 1024 * 1024 + maxVolumeCount := int64(used) + if free { + maxVolumeCount = 100 + } + return makeByteNode(id, maxVolumeCount, 1000*gb, uint64(100-used)*10*gb, mkByteVolumes(firstVolumeId, used, 10*1024)) +} + +func diskUsedPercent(n *Node) uint64 { + disk := n.info.DiskInfos[""] + return (disk.DiskTotalBytes - disk.DiskFreeBytes) * 100 / disk.DiskTotalBytes +} + +// -byDiskUsage never moves a volume onto a server without a free volume slot. +// When such a server is emptier than the rest, its capacity must not pull the +// ideal ratio down: here it held the ideal at 57%, below the 60%-used target, +// so the 90%-used server was never drained. +func TestBalanceByDiskUsageLeavesOutEmptierServersWithoutFreeSlots(t *testing.T) { + slotless := slotNode("slotless", 1, 20, false) + full := slotNode("full", 101, 90, true) + target := slotNode("target", 201, 60, true) + + c := &commandVolumeBalance{volumeSizeLimitMb: 10 * 1024, byDiskUsage: true, diskUsageHighWaterPercent: 90} + runBalance(t, c, []*Node{slotless, full, target}) + + if got := volCount(slotless); got != 20 { + t.Fatalf("server without free slots holds %d volumes, want its 20 untouched", got) + } + if got, want := diskUsedPercent(full), uint64(75); got != want { + t.Errorf("full server at %d%% used, want it drained to the %d%% the servers with free slots share", got, want) + } + if got, want := diskUsedPercent(target), uint64(75); got != want { + t.Errorf("target at %d%% used, want %d%%", got, want) + } +} + +// Leaving a server out raises the ideal, which can put another server without +// free slots below it: the 65%-used one is above the first ideal (62.5%) but +// below the next (80%), which the 80%-used target cannot go under. +func TestBalanceByDiskUsageLeavesOutServersBelowTheRaisedIdeal(t *testing.T) { + emptier := slotNode("slotless-10", 1, 10, false) + nearer := slotNode("slotless-65", 101, 65, false) + full := slotNode("full", 201, 95, true) + target := slotNode("target", 301, 80, true) + + c := &commandVolumeBalance{volumeSizeLimitMb: 10 * 1024, byDiskUsage: true, diskUsageHighWaterPercent: 90} + runBalance(t, c, []*Node{emptier, nearer, full, target}) + + if volCount(emptier) != 10 || volCount(nearer) != 65 { + t.Fatalf("servers without free slots hold %d and %d volumes, want 10 and 65", volCount(emptier), volCount(nearer)) + } + if got := diskUsedPercent(full); got >= 95 { + t.Errorf("full server still at %d%% used, want it drained toward the 87.5%% the others share", got) + } + if got := diskUsedPercent(target); got <= 80 || got >= 90 { + t.Errorf("target at %d%% used, want it filled above 80%% and kept under the 90%% gate", got) + } +} + +// A server without free slots whose usage is under one volume is left out by +// its raw ratio, zero, not by the floor DensityRatio gives an empty server: +// here that floor (0.5 of a one-volume disk) kept the ideal at 39.8%, under the +// 39%-used target's next ratio. +func TestBalanceByDiskUsageLeavesOutAServerWithoutFreeSlotsUsingUnderOneVolume(t *testing.T) { + const gb = 1024 * 1024 * 1024 + tiny := makeByteNode("tiny-slotless", 1, 10*gb, 9*gb, mkByteVolumes(1, 1, 1024)) + full := slotNode("full", 101, 41, true) + target := slotNode("target", 201, 39, true) + + c := &commandVolumeBalance{volumeSizeLimitMb: 10 * 1024, byDiskUsage: true, diskUsageHighWaterPercent: 90} + runBalance(t, c, []*Node{tiny, full, target}) + + if got := volCount(tiny); got != 1 { + t.Fatalf("server without free slots holds %d volumes, want its 1 untouched", got) + } + if got := diskUsedPercent(full); got != 40 { + t.Errorf("full server at %d%% used, want one volume moved to reach the 40%% ideal", got) + } + if got := diskUsedPercent(target); got != 40 { + t.Errorf("target at %d%% used, want 40%%", got) + } +} + +// A server without free slots that is fuller than the rest still counts in the +// ideal and is drained, as a source. +func TestBalanceByDiskUsageDrainsAFullServerWithoutFreeSlots(t *testing.T) { + full := slotNode("slotless-full", 1, 95, false) + target := slotNode("target", 101, 50, true) + + c := &commandVolumeBalance{volumeSizeLimitMb: 10 * 1024, byDiskUsage: true, diskUsageHighWaterPercent: 90} + runBalance(t, c, []*Node{full, target}) + + if got := diskUsedPercent(full); got > 73 { + t.Errorf("full server without free slots at %d%% used, want it drained to the 72.5%% ideal", got) + } + if got := diskUsedPercent(target); got < 72 { + t.Errorf("target at %d%% used, want it filled to the 72.5%% ideal", got) + } +} + // makeByteNode builds a single-disk Node carrying physical disk bytes, for the // disk-fullness gate tests. func makeByteNode(id string, maxVolumeCount int64, totalBytes, freeBytes uint64, volumes []*master_pb.VolumeInformationMessage) *Node {