Files
slacker-bot/slackerbot/internal/balance/balance.go
T
2026-11-01 14:34:23 -05:00

138 lines
3.2 KiB
Go

package balance
import (
"errors"
"math"
"sort"
"slackerbot/internal/model"
"slackerbot/internal/node"
"slackerbot/internal/storage"
)
var ErrMaxedOut = errors.New("no node has room for this container")
type NodeView struct {
Name string
Version string
Usable model.Size
Used model.Size
Drives []storage.DriveReport
Running int
}
func (v NodeView) Storage() (usableMB, committedMB, bookedMB int64) {
for _, d := range v.Drives {
usableMB += d.UsableMB
committedMB += d.CommittedMB
bookedMB += d.BookedMB
}
return
}
func ratio(used, total float64) float64 {
if total <= 0 {
return math.Inf(1)
}
return used / total
}
func (v NodeView) Homes() int {
n := 0
for _, d := range v.Drives {
n += d.Homes
}
return n
}
func (v NodeView) Load() (cpu, ram, storage float64) {
us, cm, _ := v.Storage()
return ratio(v.Used.CPUs, v.Usable.CPUs),
ratio(float64(v.Used.MemoryMB), float64(v.Usable.MemoryMB)),
ratio(float64(cm), float64(us))
}
// The fullest of CPU, RAM and storage after adding the container. Placing
// where this stays lowest keeps all three level across nodes.
func (v NodeView) loadAfter(want model.Size, storageMB int64) float64 {
us, cm, _ := v.Storage()
return max(
ratio(v.Used.CPUs+want.CPUs, v.Usable.CPUs),
ratio(float64(v.Used.MemoryMB+want.MemoryMB), float64(v.Usable.MemoryMB)),
ratio(float64(cm+storageMB), float64(us)),
)
}
func ChoosePlacement(views []NodeView, want model.Size, storageMB int64) (node, drive string, err error) {
sorted := append([]NodeView(nil), views...)
sort.Slice(sorted, func(i, j int) bool { return sorted[i].Name < sorted[j].Name })
best := math.Inf(1)
roomForCompute := false
for _, v := range sorted {
if !v.Used.Add(want).Fits(v.Usable) {
continue
}
roomForCompute = true
d, err := storage.PickDrive(v.Drives, storageMB)
if err != nil {
continue
}
if score := v.loadAfter(want, storageMB); score < best {
best, node, drive = score, v.Name, d
}
}
switch {
case node != "":
return node, drive, nil
case roomForCompute:
return "", "", storage.ErrStorageFull
}
return "", "", ErrMaxedOut
}
func BuildView(info node.NodeInfo, owners []model.Owner, defaultSize model.Size) NodeView {
v := NodeView{Name: info.Name, Version: info.Version, Usable: info.Usable}
byMachine := map[string]*model.Owner{}
for i := range owners {
if owners[i].Node == info.Name {
byMachine[owners[i].Machine] = &owners[i]
}
}
v.Running = len(info.Running)
for _, r := range info.Running {
if u := byMachine[r.Name]; u != nil {
v.Used = v.Used.Add(u.Size)
} else {
v.Used = v.Used.Add(defaultSize)
}
}
for _, d := range info.Drives {
r := storage.DriveReport{Path: d.Path}
var homesUsed int64
for machine, used := range d.HomesMB {
homesUsed += used
if u := byMachine[machine]; u != nil && u.Drive == d.Path {
continue
}
r.CommittedMB += used
r.BookedMB += used
r.Homes++
}
for _, u := range byMachine {
if u.Drive != d.Path {
continue
}
used := d.HomesMB[u.Machine]
r.CommittedMB += storage.Committed(u.StorageMinMB, used)
r.BookedMB += u.StorageMaxMB
r.Homes++
}
r.UsableMB = d.FreeMB + homesUsed - d.ReserveMB
v.Drives = append(v.Drives, r)
}
return v
}