Merge pull request #17343 from davidopp/sort

Auto commit by PR queue bot
pull/6/head
k8s-merge-robot 2015-11-16 17:46:32 -08:00
commit 5cf9d105a4
1 changed files with 20 additions and 19 deletions

View File

@ -19,6 +19,7 @@ package scheduler
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"sort"
"strings" "strings"
"sync" "sync"
@ -87,32 +88,20 @@ func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeListe
return g.selectHost(priorityList) return g.selectHost(priorityList)
} }
// This method takes a prioritized list of nodes, shuffles those with the highest scores to the // This method takes a prioritized list of nodes and sorts them in reverse order based on scores
// front of the list, and then picks one randomly from the nodes that had the highest score. // and then picks one randomly from the nodes that had the highest score
func (g *genericScheduler) selectHost(priorityList algorithm.HostPriorityList) (string, error) { func (g *genericScheduler) selectHost(priorityList algorithm.HostPriorityList) (string, error) {
if len(priorityList) == 0 { if len(priorityList) == 0 {
return "", fmt.Errorf("empty priorityList") return "", fmt.Errorf("empty priorityList")
} }
max := priorityList[0].Score sort.Sort(sort.Reverse(priorityList))
n := 0
for i, hostEntry := range priorityList {
if hostEntry.Score < max {
continue
}
if hostEntry.Score > max {
max = hostEntry.Score
n = 0
}
priorityList[i], priorityList[n] = priorityList[n], priorityList[i]
n++
}
if n == 1 {
return priorityList[0].Host, nil
}
hosts := getBestHosts(priorityList)
g.randomLock.Lock() g.randomLock.Lock()
defer g.randomLock.Unlock() defer g.randomLock.Unlock()
return priorityList[g.random.Intn(n)].Host, nil
ix := g.random.Int() % len(hosts)
return hosts[ix], nil
} }
// Filters the nodes to find the ones that fit based on the given predicate functions // Filters the nodes to find the ones that fit based on the given predicate functions
@ -190,6 +179,18 @@ func PrioritizeNodes(pod *api.Pod, podLister algorithm.PodLister, priorityConfig
return result, nil return result, nil
} }
func getBestHosts(list algorithm.HostPriorityList) []string {
result := []string{}
for _, hostEntry := range list {
if hostEntry.Score == list[0].Score {
result = append(result, hostEntry.Host)
} else {
break
}
}
return result
}
// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes // EqualPriority is a prioritizer function that gives an equal weight of one to all nodes
func EqualPriority(_ *api.Pod, podLister algorithm.PodLister, nodeLister algorithm.NodeLister) (algorithm.HostPriorityList, error) { func EqualPriority(_ *api.Pod, podLister algorithm.PodLister, nodeLister algorithm.NodeLister) (algorithm.HostPriorityList, error) {
nodes, err := nodeLister.List() nodes, err := nodeLister.List()