simplify sorting comparator of numa nodes

This commit is contained in:
kikimo 2021-05-19 09:57:42 +08:00
parent 893ebf3a1c
commit 7d30bfecd5

View File

@ -120,6 +120,14 @@ type sourcesReadyStub struct{}
// PodReusableDevices is a map by pod name of devices to reuse. // PodReusableDevices is a map by pod name of devices to reuse.
type PodReusableDevices map[string]map[string]sets.String type PodReusableDevices map[string]map[string]sets.String
type nodePriority int
const (
priorityFromAffinity nodePriority = iota
priorityNotFromAffinity
priorityWithoutTopology
)
func (s *sourcesReadyStub) AddSource(source string) {} func (s *sourcesReadyStub) AddSource(source string) {}
func (s *sourcesReadyStub) AllReady() bool { return true } func (s *sourcesReadyStub) AllReady() bool { return true }
@ -791,23 +799,27 @@ func (m *ManagerImpl) filterByAffinity(podUID, contName, resource string, availa
nodes = append(nodes, node) nodes = append(nodes, node)
} }
getNodePriority := func(n int) nodePriority {
if hint.NUMANodeAffinity.IsSet(n) {
return priorityFromAffinity
}
if n != nodeWithoutTopology {
return priorityNotFromAffinity
}
return priorityWithoutTopology
}
// Sort the list of nodes by nodes from affinity set, nodes from none-affinity // Sort the list of nodes by nodes from affinity set, nodes from none-affinity
// set, nodes without topology set and how many devices they contain. // set, nodes without topology set and how many devices they contain.
sort.Slice(nodes, func(i, j int) bool { sort.Slice(nodes, func(i, j int) bool {
ni, nj := nodes[i], nodes[j] pi, pj := getNodePriority(nodes[i]), getNodePriority(nodes[j])
// 1. node[i] from to affninity set if pi != pj {
if hint.NUMANodeAffinity.IsSet(ni) { return pi < pj
return !hint.NUMANodeAffinity.IsSet(nj) || perNodeDevices[ni].Len() < perNodeDevices[nj].Len()
} }
// 2. node[i] from none-affinity set return perNodeDevices[nodes[i]].Len() < perNodeDevices[nodes[j]].Len()
if ni != nodeWithoutTopology {
return !hint.NUMANodeAffinity.IsSet(nj) &&
(nj == nodeWithoutTopology || perNodeDevices[ni].Len() < perNodeDevices[nj].Len())
}
// 3. node[i] from node without topology set
return nj == nodeWithoutTopology && perNodeDevices[ni].Len() < perNodeDevices[nj].Len()
}) })
// Generate three sorted lists of devices. Devices in the first list come // Generate three sorted lists of devices. Devices in the first list come