Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Huang-Wei
Created July 26, 2019 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Huang-Wei/7fa9eafb7074e3b4556211aa4723afe4 to your computer and use it in GitHub Desktop.
Save Huang-Wei/7fa9eafb7074e3b4556211aa4723afe4 to your computer and use it in GitHub Desktop.
Parallel iteration
diff --git a/pkg/scheduler/algorithm/priorities/even_pods_spread.go b/pkg/scheduler/algorithm/priorities/even_pods_spread.go
index 31d84c6727..0cbd1b908a 100644
--- a/pkg/scheduler/algorithm/priorities/even_pods_spread.go
+++ b/pkg/scheduler/algorithm/priorities/even_pods_spread.go
@@ -38,14 +38,14 @@ type topologyPair struct {
type topologySpreadConstraintsMap struct {
// nodeNameToPodCounts is keyed with node name, and valued with the number of matching pods.
- nodeNameToPodCounts map[string]int64
+ nodeNameToPodCounts map[string]*int64
// topologyPairToPodCounts is keyed with topologyPair, and valued with the number of matching pods.
topologyPairToPodCounts map[topologyPair]*int64
}
func newTopologySpreadConstraintsMap() *topologySpreadConstraintsMap {
return &topologySpreadConstraintsMap{
- nodeNameToPodCounts: make(map[string]int64),
+ nodeNameToPodCounts: make(map[string]*int64),
topologyPairToPodCounts: make(map[topologyPair]*int64),
}
}
@@ -67,7 +67,7 @@ func (t *topologySpreadConstraintsMap) initialize(pod *v1.Pod, nodes []*v1.Node)
t.topologyPairToPodCounts[pair] = new(int64)
}
}
- t.nodeNameToPodCounts[node.Name] = 0
+ t.nodeNameToPodCounts[node.Name] = new(int64)
// For those nodes which don't have all required topologyKeys present, it's intentional to keep
// those entries absent in nodeNameToPodCounts, so that we're able to score them to 0 afterwards.
}
@@ -144,9 +144,10 @@ func CalculateEvenPodsSpreadPriority(pod *v1.Pod, nodeNameToInfo map[string]*sch
var minCount int64 = math.MaxInt64
// <total> sums up the number of matching pods on each qualified topology pair
var total int64
- for _, node := range nodes {
- if _, ok := t.nodeNameToPodCounts[node.Name]; !ok {
- continue
+ processCandidateNode := func(i int) {
+ node := nodes[i]
+ if t.nodeNameToPodCounts[node.Name] == nil {
+ return
}
// For each present <pair>, current node gets a credit of <matchSum>.
@@ -155,14 +156,25 @@ func CalculateEvenPodsSpreadPriority(pod *v1.Pod, nodeNameToInfo map[string]*sch
if tpVal, ok := node.Labels[constraint.TopologyKey]; ok {
pair := topologyPair{key: constraint.TopologyKey, value: tpVal}
matchSum := atomic.LoadInt64(t.topologyPairToPodCounts[pair])
- t.nodeNameToPodCounts[node.Name] += matchSum
+ atomic.AddInt64(t.nodeNameToPodCounts[node.Name], matchSum)
atomic.AddInt64(&total, matchSum)
}
}
- if t.nodeNameToPodCounts[node.Name] < minCount {
- minCount = t.nodeNameToPodCounts[node.Name]
+ // if t.nodeNameToPodCounts[node.Name] < minCount {
+ // minCount = t.nodeNameToPodCounts[node.Name]
+ // }
+ for {
+ swapped := true
+ _minCount := atomic.LoadInt64(&minCount)
+ if *t.nodeNameToPodCounts[node.Name] < _minCount {
+ swapped = swapped && atomic.CompareAndSwapInt64(&minCount, _minCount, *t.nodeNameToPodCounts[node.Name])
+ }
+ if swapped {
+ return
+ }
}
}
+ workqueue.ParallelizeUntil(ctx, 16, len(nodes), processCandidateNode)
// calculate final priority score for each node
// TODO(Huang-Wei): in alpha version, we keep the formula as simple as possible.
@@ -189,7 +201,7 @@ func CalculateEvenPodsSpreadPriority(pod *v1.Pod, nodeNameToInfo map[string]*sch
result[i].Score = schedulerapi.MaxPriority
continue
}
- fScore := float64(schedulerapi.MaxPriority) * (float64(total-t.nodeNameToPodCounts[node.Name]) / float64(maxMinDiff))
+ fScore := float64(schedulerapi.MaxPriority) * (float64(total-*t.nodeNameToPodCounts[node.Name]) / float64(maxMinDiff))
result[i].Score = int(fScore)
}
diff --git a/pkg/scheduler/algorithm/priorities/even_pods_spread_test.go b/pkg/scheduler/algorithm/priorities/even_pods_spread_test.go
index b7da0dbe69..fdc2fdd625 100644
--- a/pkg/scheduler/algorithm/priorities/even_pods_spread_test.go
+++ b/pkg/scheduler/algorithm/priorities/even_pods_spread_test.go
@@ -32,7 +32,7 @@ func Test_topologySpreadConstraintsMap_initialize(t *testing.T) {
name string
pod *v1.Pod
nodes []*v1.Node
- wantNodeNameMap map[string]int64
+ wantNodeNameMap map[string]*int64
wantTopologyPairMap map[topologyPair]*int64
}{
{
@@ -46,10 +46,10 @@ func Test_topologySpreadConstraintsMap_initialize(t *testing.T) {
st.MakeNode().Name("node-b").Label("zone", "zone1").Label("node", "node-b").Obj(),
st.MakeNode().Name("node-x").Label("zone", "zone2").Label("node", "node-x").Obj(),
},
- wantNodeNameMap: map[string]int64{
- "node-a": 0,
- "node-b": 0,
- "node-x": 0,
+ wantNodeNameMap: map[string]*int64{
+ "node-a": new(int64),
+ "node-b": new(int64),
+ "node-x": new(int64),
},
wantTopologyPairMap: map[topologyPair]*int64{
{key: "zone", value: "zone1"}: new(int64),
@@ -70,9 +70,9 @@ func Test_topologySpreadConstraintsMap_initialize(t *testing.T) {
st.MakeNode().Name("node-b").Label("zone", "zone1").Label("node", "node-b").Obj(),
st.MakeNode().Name("node-x").Label("node", "node-x").Obj(),
},
- wantNodeNameMap: map[string]int64{
- "node-a": 0,
- "node-b": 0,
+ wantNodeNameMap: map[string]*int64{
+ "node-a": new(int64),
+ "node-b": new(int64),
},
wantTopologyPairMap: map[topologyPair]*int64{
{key: "zone", value: "zone1"}: new(int64),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment