Skip to content

Commit 11c42f7

Browse files
author
k8s-merge-robot
committed
Merge pull request kubernetes#19907 from olegshaldybin/minor-scheduler-optimization
Auto commit by PR queue bot
2 parents 8ca6c92 + 3a36dfb commit 11c42f7

File tree

2 files changed

+56
-59
lines changed

2 files changed

+56
-59
lines changed

plugin/pkg/scheduler/generic_scheduler.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"fmt"
2222
"math/rand"
23-
"sort"
2423
"strings"
2524
"sync"
2625

@@ -101,20 +100,31 @@ func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeListe
101100
return g.selectHost(priorityList)
102101
}
103102

104-
// This method takes a prioritized list of nodes and sorts them in reverse order based on scores
105-
// and then picks one randomly from the nodes that had the highest score
103+
// selectHost takes a prioritized list of nodes and then picks one
104+
// randomly from the nodes that had the highest score.
106105
func (g *genericScheduler) selectHost(priorityList schedulerapi.HostPriorityList) (string, error) {
107106
if len(priorityList) == 0 {
108107
return "", fmt.Errorf("empty priorityList")
109108
}
110-
sort.Sort(sort.Reverse(priorityList))
111109

112-
hosts := getBestHosts(priorityList)
110+
maxScore := priorityList[0].Score
111+
// idx contains indices of elements with score == maxScore.
112+
idx := []int{}
113+
114+
for i, entry := range priorityList {
115+
if entry.Score > maxScore {
116+
maxScore = entry.Score
117+
idx = []int{i}
118+
} else if entry.Score == maxScore {
119+
idx = append(idx, i)
120+
}
121+
}
122+
113123
g.randomLock.Lock()
114-
defer g.randomLock.Unlock()
124+
ix := g.random.Int() % len(idx)
125+
g.randomLock.Unlock()
115126

116-
ix := g.random.Int() % len(hosts)
117-
return hosts[ix], nil
127+
return priorityList[idx[ix]].Host, nil
118128
}
119129

120130
// Filters the nodes to find the ones that fit based on the given predicate functions
@@ -258,18 +268,6 @@ func PrioritizeNodes(pod *api.Pod, machinesToPods map[string][]*api.Pod, podList
258268
return result, nil
259269
}
260270

261-
func getBestHosts(list schedulerapi.HostPriorityList) []string {
262-
result := []string{}
263-
for _, hostEntry := range list {
264-
if hostEntry.Score == list[0].Score {
265-
result = append(result, hostEntry.Host)
266-
} else {
267-
break
268-
}
269-
}
270-
return result
271-
}
272-
273271
// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes
274272
func EqualPriority(_ *api.Pod, machinesToPods map[string][]*api.Pod, podLister algorithm.PodLister, nodeLister algorithm.NodeLister) (schedulerapi.HostPriorityList, error) {
275273
nodes, err := nodeLister.List()

plugin/pkg/scheduler/generic_scheduler_test.go

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ func TestSelectHost(t *testing.T) {
166166

167167
func TestGenericScheduler(t *testing.T) {
168168
tests := []struct {
169-
name string
170-
predicates map[string]algorithm.FitPredicate
171-
prioritizers []algorithm.PriorityConfig
172-
nodes []string
173-
pod *api.Pod
174-
pods []*api.Pod
175-
expectedHost string
176-
expectsErr bool
169+
name string
170+
predicates map[string]algorithm.FitPredicate
171+
prioritizers []algorithm.PriorityConfig
172+
nodes []string
173+
pod *api.Pod
174+
pods []*api.Pod
175+
expectedHosts sets.String
176+
expectsErr bool
177177
}{
178178
{
179179
predicates: map[string]algorithm.FitPredicate{"false": falsePredicate},
@@ -183,44 +183,43 @@ func TestGenericScheduler(t *testing.T) {
183183
name: "test 1",
184184
},
185185
{
186-
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
187-
prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
188-
nodes: []string{"machine1", "machine2"},
189-
// Random choice between both, the rand seeded above with zero, chooses "machine1"
190-
expectedHost: "machine1",
191-
name: "test 2",
186+
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
187+
prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
188+
nodes: []string{"machine1", "machine2"},
189+
expectedHosts: sets.NewString("machine1", "machine2"),
190+
name: "test 2",
192191
},
193192
{
194193
// Fits on a machine where the pod ID matches the machine name
195-
predicates: map[string]algorithm.FitPredicate{"matches": matchesPredicate},
196-
prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
197-
nodes: []string{"machine1", "machine2"},
198-
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "machine2"}},
199-
expectedHost: "machine2",
200-
name: "test 3",
194+
predicates: map[string]algorithm.FitPredicate{"matches": matchesPredicate},
195+
prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
196+
nodes: []string{"machine1", "machine2"},
197+
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "machine2"}},
198+
expectedHosts: sets.NewString("machine2"),
199+
name: "test 3",
201200
},
202201
{
203-
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
204-
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
205-
nodes: []string{"3", "2", "1"},
206-
expectedHost: "3",
207-
name: "test 4",
202+
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
203+
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
204+
nodes: []string{"3", "2", "1"},
205+
expectedHosts: sets.NewString("3"),
206+
name: "test 4",
208207
},
209208
{
210-
predicates: map[string]algorithm.FitPredicate{"matches": matchesPredicate},
211-
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
212-
nodes: []string{"3", "2", "1"},
213-
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
214-
expectedHost: "2",
215-
name: "test 5",
209+
predicates: map[string]algorithm.FitPredicate{"matches": matchesPredicate},
210+
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
211+
nodes: []string{"3", "2", "1"},
212+
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
213+
expectedHosts: sets.NewString("2"),
214+
name: "test 5",
216215
},
217216
{
218-
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
219-
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}, {Function: reverseNumericPriority, Weight: 2}},
220-
nodes: []string{"3", "2", "1"},
221-
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
222-
expectedHost: "1",
223-
name: "test 6",
217+
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
218+
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}, {Function: reverseNumericPriority, Weight: 2}},
219+
nodes: []string{"3", "2", "1"},
220+
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
221+
expectedHosts: sets.NewString("1"),
222+
name: "test 6",
224223
},
225224
{
226225
predicates: map[string]algorithm.FitPredicate{"true": truePredicate, "false": falsePredicate},
@@ -266,8 +265,8 @@ func TestGenericScheduler(t *testing.T) {
266265
if err != nil {
267266
t.Errorf("Unexpected error: %v", err)
268267
}
269-
if test.expectedHost != machine {
270-
t.Errorf("Failed : %s, Expected: %s, Saw: %s", test.name, test.expectedHost, machine)
268+
if !test.expectedHosts.Has(machine) {
269+
t.Errorf("Failed : %s, Expected: %s, Saw: %s", test.name, test.expectedHosts, machine)
271270
}
272271
}
273272
}

0 commit comments

Comments
 (0)