package main import ( "math" "sort" ) type NodeView struct { Name string Usable Size Used Size Drives []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 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 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 := 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 "", "", ErrStorageFull } return "", "", ErrMaxedOut } func buildView(info NodeInfo, owners []Owner, defaultSize Size) NodeView { v := NodeView{Name: info.Name, Usable: info.Usable} byMachine := map[string]*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 := 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 += 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 }