From 0431f2430df3d38f4a4000e79cdcb57db3cebec7 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Mon, 13 Oct 2014 14:46:31 -0700 Subject: [PATCH] Use cache for minion lookups, don't hammer apiserver --- cmd/integration/integration.go | 5 +---- plugin/cmd/scheduler/scheduler.go | 5 +---- plugin/pkg/scheduler/factory/factory.go | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/integration/integration.go b/cmd/integration/integration.go index d71f53d3c00..5ddafc3cdee 100644 --- a/cmd/integration/integration.go +++ b/cmd/integration/integration.go @@ -137,10 +137,7 @@ func startComponents(manifestURL string) (apiServerURL string) { // Scheduler schedulerConfigFactory := &factory.ConfigFactory{cl} - schedulerConfig, err := schedulerConfigFactory.Create() - if err != nil { - glog.Fatalf("Unable to construct scheduler config: %v", err) - } + schedulerConfig := schedulerConfigFactory.Create() scheduler.New(schedulerConfig).Run() endpoints := service.NewEndpointController(cl) diff --git a/plugin/cmd/scheduler/scheduler.go b/plugin/cmd/scheduler/scheduler.go index 93850e59c96..ca872ef0437 100644 --- a/plugin/cmd/scheduler/scheduler.go +++ b/plugin/cmd/scheduler/scheduler.go @@ -58,10 +58,7 @@ func main() { go http.ListenAndServe(net.JoinHostPort(address.String(), strconv.Itoa(*port)), nil) configFactory := &factory.ConfigFactory{Client: kubeClient} - config, err := configFactory.Create() - if err != nil { - glog.Fatalf("Can't create scheduler config: %v", err) - } + config := configFactory.Create() s := scheduler.New(config) s.Run() diff --git a/plugin/pkg/scheduler/factory/factory.go b/plugin/pkg/scheduler/factory/factory.go index 9b5e72076ec..91948c6c785 100644 --- a/plugin/pkg/scheduler/factory/factory.go +++ b/plugin/pkg/scheduler/factory/factory.go @@ -19,6 +19,7 @@ limitations under the License. package factory import ( + "fmt" "math/rand" "sync" "time" @@ -42,7 +43,7 @@ type ConfigFactory struct { } // Create creates a scheduler and all support functions. -func (factory *ConfigFactory) Create() (*scheduler.Config, error) { +func (factory *ConfigFactory) Create() *scheduler.Config { // Watch and queue pods that need scheduling. podQueue := cache.NewFIFO() cache.NewReflector(factory.createUnassignedPodLW(), &api.Pod{}, podQueue).Run() @@ -63,13 +64,14 @@ func (factory *ConfigFactory) Create() (*scheduler.Config, error) { } r := rand.New(rand.NewSource(time.Now().UnixNano())) + minionLister := &storeToMinionLister{minionCache} algo := algorithm.NewGenericScheduler( []algorithm.FitPredicate{ // Fit is defined based on the absence of port conflicts. algorithm.PodFitsPorts, // Fit is determined by resource availability - algorithm.NewResourceFitPredicate(algorithm.ClientNodeInfo{factory.Client}), + algorithm.NewResourceFitPredicate(minionLister), }, // Prioritize nodes by least requested utilization. algorithm.LeastRequestedPriority, @@ -81,7 +83,7 @@ func (factory *ConfigFactory) Create() (*scheduler.Config, error) { } return &scheduler.Config{ - MinionLister: &storeToMinionLister{minionCache}, + MinionLister: minionLister, Algorithm: algo, Binder: &binder{factory.Client}, NextPod: func() *api.Pod { @@ -93,7 +95,7 @@ func (factory *ConfigFactory) Create() (*scheduler.Config, error) { return pod }, Error: factory.makeDefaultErrorFunc(&podBackoff, podQueue), - }, nil + } } type listWatch struct { @@ -204,6 +206,14 @@ func (s *storeToMinionLister) List() (machines api.MinionList, err error) { return machines, nil } +// GetNodeInfo returns cached data for the minion 'id'. +func (s *storeToMinionLister) GetNodeInfo(id string) (*api.Minion, error) { + if minion, ok := s.Get(id); ok { + return minion.(*api.Minion), nil + } + return nil, fmt.Errorf("minion '%v' is not in cache") +} + // storeToPodLister turns a store into a pod lister. The store must contain (only) pods. type storeToPodLister struct { cache.Store