Reuse the "min*Nodes" slices to save the GC time.

This commit is contained in:
tossmilestone 2018-02-13 09:25:37 +08:00
parent 69324f90e6
commit 5a083f2038

View File

@ -677,13 +677,15 @@ func EqualPriorityMap(_ *v1.Pod, _ interface{}, nodeInfo *schedulercache.NodeInf
// 3. Ties are broken by sum of priorities of all victims. // 3. Ties are broken by sum of priorities of all victims.
// 4. If there are still ties, node with the minimum number of victims is picked. // 4. If there are still ties, node with the minimum number of victims is picked.
// 5. If there are still ties, the first such node is picked (sort of randomly). // 5. If there are still ties, the first such node is picked (sort of randomly).
//TODO(bsalamat): Try to reuse the "min*Nodes" slices in order to save GC time. // The 'minNodes1' and 'minNodes2' are being reused here to save the memory
// allocation and garbage collection time.
func pickOneNodeForPreemption(nodesToVictims map[*v1.Node]*Victims) *v1.Node { func pickOneNodeForPreemption(nodesToVictims map[*v1.Node]*Victims) *v1.Node {
if len(nodesToVictims) == 0 { if len(nodesToVictims) == 0 {
return nil return nil
} }
minNumPDBViolatingPods := math.MaxInt32 minNumPDBViolatingPods := math.MaxInt32
var minPDBViolatingNodes []*v1.Node var minNodes1 []*v1.Node
lenNodes1 := 0
for node, victims := range nodesToVictims { for node, victims := range nodesToVictims {
if len(victims.pods) == 0 { if len(victims.pods) == 0 {
// We found a node that doesn't need any preemption. Return it! // We found a node that doesn't need any preemption. Return it!
@ -695,42 +697,48 @@ func pickOneNodeForPreemption(nodesToVictims map[*v1.Node]*Victims) *v1.Node {
numPDBViolatingPods := victims.numPDBViolations numPDBViolatingPods := victims.numPDBViolations
if numPDBViolatingPods < minNumPDBViolatingPods { if numPDBViolatingPods < minNumPDBViolatingPods {
minNumPDBViolatingPods = numPDBViolatingPods minNumPDBViolatingPods = numPDBViolatingPods
minPDBViolatingNodes = nil minNodes1 = nil
lenNodes1 = 0
} }
if numPDBViolatingPods == minNumPDBViolatingPods { if numPDBViolatingPods == minNumPDBViolatingPods {
minPDBViolatingNodes = append(minPDBViolatingNodes, node) minNodes1 = append(minNodes1, node)
lenNodes1++
} }
} }
if len(minPDBViolatingNodes) == 1 { if lenNodes1 == 1 {
return minPDBViolatingNodes[0] return minNodes1[0]
} }
// There are more than one node with minimum number PDB violating pods. Find // There are more than one node with minimum number PDB violating pods. Find
// the one with minimum highest priority victim. // the one with minimum highest priority victim.
minHighestPriority := int32(math.MaxInt32) minHighestPriority := int32(math.MaxInt32)
var minPriorityNodes []*v1.Node var minNodes2 = make([]*v1.Node, lenNodes1)
for _, node := range minPDBViolatingNodes { lenNodes2 := 0
for i := 0; i < lenNodes1; i++ {
node := minNodes1[i]
victims := nodesToVictims[node] victims := nodesToVictims[node]
// highestPodPriority is the highest priority among the victims on this node. // highestPodPriority is the highest priority among the victims on this node.
highestPodPriority := util.GetPodPriority(victims.pods[0]) highestPodPriority := util.GetPodPriority(victims.pods[0])
if highestPodPriority < minHighestPriority { if highestPodPriority < minHighestPriority {
minHighestPriority = highestPodPriority minHighestPriority = highestPodPriority
minPriorityNodes = nil lenNodes2 = 0
} }
if highestPodPriority == minHighestPriority { if highestPodPriority == minHighestPriority {
minPriorityNodes = append(minPriorityNodes, node) minNodes2[lenNodes2] = node
lenNodes2++
} }
} }
if len(minPriorityNodes) == 1 { if lenNodes2 == 1 {
return minPriorityNodes[0] return minNodes2[0]
} }
// There are a few nodes with minimum highest priority victim. Find the // There are a few nodes with minimum highest priority victim. Find the
// smallest sum of priorities. // smallest sum of priorities.
minSumPriorities := int64(math.MaxInt64) minSumPriorities := int64(math.MaxInt64)
var minSumPriorityNodes []*v1.Node lenNodes1 = 0
for _, node := range minPriorityNodes { for i := 0; i < lenNodes2; i++ {
var sumPriorities int64 var sumPriorities int64
node := minNodes2[i]
for _, pod := range nodesToVictims[node].pods { for _, pod := range nodesToVictims[node].pods {
// We add MaxInt32+1 to all priorities to make all of them >= 0. This is // We add MaxInt32+1 to all priorities to make all of them >= 0. This is
// needed so that a node with a few pods with negative priority is not // needed so that a node with a few pods with negative priority is not
@ -740,34 +748,37 @@ func pickOneNodeForPreemption(nodesToVictims map[*v1.Node]*Victims) *v1.Node {
} }
if sumPriorities < minSumPriorities { if sumPriorities < minSumPriorities {
minSumPriorities = sumPriorities minSumPriorities = sumPriorities
minSumPriorityNodes = nil lenNodes1 = 0
} }
if sumPriorities == minSumPriorities { if sumPriorities == minSumPriorities {
minSumPriorityNodes = append(minSumPriorityNodes, node) minNodes1[lenNodes1] = node
lenNodes1++
} }
} }
if len(minSumPriorityNodes) == 1 { if lenNodes1 == 1 {
return minSumPriorityNodes[0] return minNodes1[0]
} }
// There are a few nodes with minimum highest priority victim and sum of priorities. // There are a few nodes with minimum highest priority victim and sum of priorities.
// Find one with the minimum number of pods. // Find one with the minimum number of pods.
minNumPods := math.MaxInt32 minNumPods := math.MaxInt32
var minNumPodNodes []*v1.Node lenNodes2 = 0
for _, node := range minSumPriorityNodes { for i := 0; i < lenNodes1; i++ {
node := minNodes1[i]
numPods := len(nodesToVictims[node].pods) numPods := len(nodesToVictims[node].pods)
if numPods < minNumPods { if numPods < minNumPods {
minNumPods = numPods minNumPods = numPods
minNumPodNodes = nil lenNodes2 = 0
} }
if numPods == minNumPods { if numPods == minNumPods {
minNumPodNodes = append(minNumPodNodes, node) minNodes2[lenNodes2] = node
lenNodes2++
} }
} }
// At this point, even if there are more than one node with the same score, // At this point, even if there are more than one node with the same score,
// return the first one. // return the first one.
if len(minNumPodNodes) > 0 { if lenNodes2 > 0 {
return minNumPodNodes[0] return minNodes2[0]
} }
glog.Errorf("Error in logic of node scoring for preemption. We should never reach here!") glog.Errorf("Error in logic of node scoring for preemption. We should never reach here!")
return nil return nil