Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scheduler: remove unused random generator #26021

Merged
merged 1 commit into from
May 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions plugin/pkg/scheduler/extender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package scheduler

import (
"fmt"
"math/rand"
"testing"

"k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -281,12 +280,11 @@ func TestGenericSchedulerWithExtenders(t *testing.T) {
}

for _, test := range tests {
random := rand.New(rand.NewSource(0))
extenders := []algorithm.SchedulerExtender{}
for ii := range test.extenders {
extenders = append(extenders, &test.extenders[ii])
}
scheduler := NewGenericScheduler(schedulertesting.PodsToCache(test.pods), test.predicates, test.prioritizers, extenders, random)
scheduler := NewGenericScheduler(schedulertesting.PodsToCache(test.pods), test.predicates, test.prioritizers, extenders)
machine, err := scheduler.Schedule(test.pod, algorithm.FakeNodeLister(makeNodeList(test.nodes)))
if test.expectsErr {
if err == nil {
Expand Down
5 changes: 1 addition & 4 deletions plugin/pkg/scheduler/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package factory

import (
"fmt"
"math/rand"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -317,9 +316,7 @@ func (f *ConfigFactory) CreateFromKeys(predicateKeys, priorityKeys sets.String,

f.Run()

r := rand.New(rand.NewSource(time.Now().UnixNano()))

algo := scheduler.NewGenericScheduler(f.schedulerCache, predicateFuncs, priorityConfigs, extenders, r)
algo := scheduler.NewGenericScheduler(f.schedulerCache, predicateFuncs, priorityConfigs, extenders)

podBackoff := podBackoff{
perPodBackoff: map[types.NamespacedName]*backoffEntry{},
Expand Down
25 changes: 11 additions & 14 deletions plugin/pkg/scheduler/generic_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package scheduler
import (
"bytes"
"fmt"
"math/rand"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -56,14 +55,13 @@ func (f *FitError) Error() string {
}

type genericScheduler struct {
cache schedulercache.Cache
predicates map[string]algorithm.FitPredicate
prioritizers []algorithm.PriorityConfig
extenders []algorithm.SchedulerExtender
pods algorithm.PodLister
random *rand.Rand
randomLock sync.Mutex
lastNodeIndex uint64
cache schedulercache.Cache
predicates map[string]algorithm.FitPredicate
prioritizers []algorithm.PriorityConfig
extenders []algorithm.SchedulerExtender
pods algorithm.PodLister
lastNodeIndexLock sync.Mutex
lastNodeIndex uint64
}

// Schedule tries to schedule the given pod to one of node in the node list.
Expand Down Expand Up @@ -116,7 +114,7 @@ func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeListe
}

// selectHost takes a prioritized list of nodes and then picks one
// randomly from the nodes that had the highest score.
// in a round-robin manner from the nodes that had the highest score.
func (g *genericScheduler) selectHost(priorityList schedulerapi.HostPriorityList) (string, error) {
if len(priorityList) == 0 {
return "", fmt.Errorf("empty priorityList")
Expand All @@ -126,10 +124,10 @@ func (g *genericScheduler) selectHost(priorityList schedulerapi.HostPriorityList
maxScore := priorityList[0].Score
firstAfterMaxScore := sort.Search(len(priorityList), func(i int) bool { return priorityList[i].Score < maxScore })

g.randomLock.Lock()
g.lastNodeIndexLock.Lock()
ix := int(g.lastNodeIndex % uint64(firstAfterMaxScore))
g.lastNodeIndex++
g.randomLock.Unlock()
g.lastNodeIndexLock.Unlock()

return priorityList[ix].Host, nil
}
Expand Down Expand Up @@ -324,12 +322,11 @@ func EqualPriority(_ *api.Pod, nodeNameToInfo map[string]*schedulercache.NodeInf
return result, nil
}

func NewGenericScheduler(cache schedulercache.Cache, predicates map[string]algorithm.FitPredicate, prioritizers []algorithm.PriorityConfig, extenders []algorithm.SchedulerExtender, random *rand.Rand) algorithm.ScheduleAlgorithm {
func NewGenericScheduler(cache schedulercache.Cache, predicates map[string]algorithm.FitPredicate, prioritizers []algorithm.PriorityConfig, extenders []algorithm.SchedulerExtender) algorithm.ScheduleAlgorithm {
return &genericScheduler{
cache: cache,
predicates: predicates,
prioritizers: prioritizers,
extenders: extenders,
random: random,
}
}
6 changes: 2 additions & 4 deletions plugin/pkg/scheduler/generic_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package scheduler
import (
"fmt"
"math"
"math/rand"
"reflect"
"strconv"
"testing"
Expand Down Expand Up @@ -114,7 +113,7 @@ func makeNodeList(nodeNames []string) api.NodeList {
}

func TestSelectHost(t *testing.T) {
scheduler := genericScheduler{random: rand.New(rand.NewSource(0))}
scheduler := genericScheduler{}
tests := []struct {
list schedulerapi.HostPriorityList
possibleHosts sets.String
Expand Down Expand Up @@ -277,15 +276,14 @@ func TestGenericScheduler(t *testing.T) {
},
}
for _, test := range tests {
random := rand.New(rand.NewSource(0))
cache := schedulercache.New(time.Duration(0), wait.NeverStop)
for _, pod := range test.pods {
cache.AddPod(pod)
}
for _, name := range test.nodes {
cache.AddNode(&api.Node{ObjectMeta: api.ObjectMeta{Name: name}})
}
scheduler := NewGenericScheduler(cache, test.predicates, test.prioritizers, []algorithm.SchedulerExtender{}, random)
scheduler := NewGenericScheduler(cache, test.predicates, test.prioritizers, []algorithm.SchedulerExtender{})
machine, err := scheduler.Schedule(test.pod, algorithm.FakeNodeLister(makeNodeList(test.nodes)))
if test.expectsErr {
if err == nil {
Expand Down
4 changes: 1 addition & 3 deletions plugin/pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package scheduler
import (
"errors"
"fmt"
"math/rand"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -218,8 +217,7 @@ func TestSchedulerForgetAssumedPodAfterDelete(t *testing.T) {
cache,
map[string]algorithm.FitPredicate{"PodFitsHostPorts": predicates.PodFitsHostPorts},
[]algorithm.PriorityConfig{},
[]algorithm.SchedulerExtender{},
rand.New(rand.NewSource(time.Now().UnixNano())))
[]algorithm.SchedulerExtender{})

var gotBinding *api.Binding
c := &Config{
Expand Down