Skip to content

Commit 6d89279

Browse files
committed
Revert "scheduler: performance improvement on PodAffinity"
This reverts commit 2873091.
1 parent 46ba211 commit 6d89279

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

pkg/scheduler/algorithm/priorities/interpod_affinity.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package priorities
1919
import (
2020
"context"
2121
"sync"
22-
"sync/atomic"
2322

2423
"k8s.io/api/core/v1"
2524
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -64,15 +63,15 @@ type podAffinityPriorityMap struct {
6463
nodes []*v1.Node
6564
// counts store the mapping from node name to so-far computed score of
6665
// the node.
67-
counts map[string]*int64
66+
counts map[string]float64
6867
// The first error that we faced.
6968
firstError error
7069
}
7170

7271
func newPodAffinityPriorityMap(nodes []*v1.Node) *podAffinityPriorityMap {
7372
return &podAffinityPriorityMap{
7473
nodes: nodes,
75-
counts: make(map[string]*int64, len(nodes)),
74+
counts: make(map[string]float64, len(nodes)),
7675
}
7776
}
7877

@@ -84,7 +83,7 @@ func (p *podAffinityPriorityMap) setError(err error) {
8483
}
8584
}
8685

87-
func (p *podAffinityPriorityMap) processTerm(term *v1.PodAffinityTerm, podDefiningAffinityTerm, podToCheck *v1.Pod, fixedNode *v1.Node, weight int64) {
86+
func (p *podAffinityPriorityMap) processTerm(term *v1.PodAffinityTerm, podDefiningAffinityTerm, podToCheck *v1.Pod, fixedNode *v1.Node, weight float64) {
8887
namespaces := priorityutil.GetNamespacesFromPodAffinityTerm(podDefiningAffinityTerm, term)
8988
selector, err := metav1.LabelSelectorAsSelector(term.LabelSelector)
9089
if err != nil {
@@ -93,18 +92,22 @@ func (p *podAffinityPriorityMap) processTerm(term *v1.PodAffinityTerm, podDefini
9392
}
9493
match := priorityutil.PodMatchesTermsNamespaceAndSelector(podToCheck, namespaces, selector)
9594
if match {
96-
for _, node := range p.nodes {
97-
if priorityutil.NodesHaveSameTopologyKey(node, fixedNode, term.TopologyKey) {
98-
atomic.AddInt64(p.counts[node.Name], weight)
95+
func() {
96+
p.Lock()
97+
defer p.Unlock()
98+
for _, node := range p.nodes {
99+
if priorityutil.NodesHaveSameTopologyKey(node, fixedNode, term.TopologyKey) {
100+
p.counts[node.Name] += weight
101+
}
99102
}
100-
}
103+
}()
101104
}
102105
}
103106

104107
func (p *podAffinityPriorityMap) processTerms(terms []v1.WeightedPodAffinityTerm, podDefiningAffinityTerm, podToCheck *v1.Pod, fixedNode *v1.Node, multiplier int) {
105108
for i := range terms {
106109
term := &terms[i]
107-
p.processTerm(&term.PodAffinityTerm, podDefiningAffinityTerm, podToCheck, fixedNode, int64(term.Weight*int32(multiplier)))
110+
p.processTerm(&term.PodAffinityTerm, podDefiningAffinityTerm, podToCheck, fixedNode, float64(term.Weight*int32(multiplier)))
108111
}
109112
}
110113

@@ -118,17 +121,17 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
118121
hasAffinityConstraints := affinity != nil && affinity.PodAffinity != nil
119122
hasAntiAffinityConstraints := affinity != nil && affinity.PodAntiAffinity != nil
120123

121-
// priorityMap stores the mapping from node name to so-far computed score of
122-
// the node.
123-
pm := newPodAffinityPriorityMap(nodes)
124124
allNodeNames := make([]string, 0, len(nodeNameToInfo))
125125
for name := range nodeNameToInfo {
126126
allNodeNames = append(allNodeNames, name)
127-
pm.counts[name] = new(int64)
128127
}
129128

130129
// convert the topology key based weights to the node name based weights
131-
var maxCount, minCount int64
130+
var maxCount float64
131+
var minCount float64
132+
// priorityMap stores the mapping from node name to so-far computed score of
133+
// the node.
134+
pm := newPodAffinityPriorityMap(nodes)
132135

133136
processPod := func(existingPod *v1.Pod) error {
134137
existingPodNode, err := ipa.info.GetNodeInfo(existingPod.Spec.NodeName)
@@ -169,7 +172,7 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
169172
// terms = append(terms, existingPodAffinity.PodAffinity.RequiredDuringSchedulingRequiredDuringExecution...)
170173
//}
171174
for _, term := range terms {
172-
pm.processTerm(&term, existingPod, pod, existingPodNode, int64(ipa.hardPodAffinityWeight))
175+
pm.processTerm(&term, existingPod, pod, existingPodNode, float64(ipa.hardPodAffinityWeight))
173176
}
174177
}
175178
// For every soft pod affinity term of <existingPod>, if <pod> matches the term,
@@ -214,11 +217,11 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
214217
}
215218

216219
for _, node := range nodes {
217-
if *pm.counts[node.Name] > maxCount {
218-
maxCount = *pm.counts[node.Name]
220+
if pm.counts[node.Name] > maxCount {
221+
maxCount = pm.counts[node.Name]
219222
}
220-
if *pm.counts[node.Name] < minCount {
221-
minCount = *pm.counts[node.Name]
223+
if pm.counts[node.Name] < minCount {
224+
minCount = pm.counts[node.Name]
222225
}
223226
}
224227

@@ -227,7 +230,7 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
227230
for _, node := range nodes {
228231
fScore := float64(0)
229232
if (maxCount - minCount) > 0 {
230-
fScore = float64(schedulerapi.MaxPriority) * (float64(*pm.counts[node.Name]-minCount) / float64(maxCount-minCount))
233+
fScore = float64(schedulerapi.MaxPriority) * ((pm.counts[node.Name] - minCount) / (maxCount - minCount))
231234
}
232235
result = append(result, schedulerapi.HostPriority{Host: node.Name, Score: int(fScore)})
233236
if klog.V(10) {

0 commit comments

Comments
 (0)