From 4be6a60df2d3e0bbbb36a3eb8feb1dcc81e4c077 Mon Sep 17 00:00:00 2001 From: Yongkun Anfernee Gui Date: Tue, 7 Nov 2017 11:34:20 -0800 Subject: [PATCH] Refactor Priority Reduce functions - Reducing the duplicated reduce function by introducing a reduce function generator that generates common reduce functions. - Remove logs from reduce function, so it's purely calculating scores. - Optimize the reduce functions by removing unnecessary conversion to float64. --- .../pkg/scheduler/algorithm/priorities/BUILD | 1 + .../algorithm/priorities/node_affinity.go | 28 +------- .../scheduler/algorithm/priorities/reduce.go | 64 +++++++++++++++++++ .../algorithm/priorities/taint_toleration.go | 26 +------- plugin/pkg/scheduler/algorithm/types.go | 1 + .../pkg/scheduler/core/generic_scheduler.go | 5 ++ plugin/pkg/scheduler/factory/plugins.go | 2 + 7 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 plugin/pkg/scheduler/algorithm/priorities/reduce.go diff --git a/plugin/pkg/scheduler/algorithm/priorities/BUILD b/plugin/pkg/scheduler/algorithm/priorities/BUILD index 9e78e1dca3..9555dd4e27 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/BUILD +++ b/plugin/pkg/scheduler/algorithm/priorities/BUILD @@ -18,6 +18,7 @@ go_library( "node_affinity.go", "node_label.go", "node_prefer_avoid_pods.go", + "reduce.go", "selector_spreading.go", "taint_toleration.go", "test_util.go", diff --git a/plugin/pkg/scheduler/algorithm/priorities/node_affinity.go b/plugin/pkg/scheduler/algorithm/priorities/node_affinity.go index 1f62091042..ac94e72689 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/node_affinity.go +++ b/plugin/pkg/scheduler/algorithm/priorities/node_affinity.go @@ -24,8 +24,6 @@ import ( v1helper "k8s.io/kubernetes/pkg/api/v1/helper" schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api" "k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache" - - "github.com/golang/glog" ) // CalculateNodeAffinityPriority prioritizes nodes according to node affinity scheduling preferences @@ -76,28 +74,4 @@ func CalculateNodeAffinityPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *s }, nil } -func CalculateNodeAffinityPriorityReduce(pod *v1.Pod, meta interface{}, nodeNameToInfo map[string]*schedulercache.NodeInfo, result schedulerapi.HostPriorityList) error { - var maxCount int - for i := range result { - if result[i].Score > maxCount { - maxCount = result[i].Score - } - } - maxCountFloat := float64(maxCount) - - var fScore float64 - for i := range result { - if maxCount > 0 { - fScore = float64(schedulerapi.MaxPriority) * (float64(result[i].Score) / maxCountFloat) - } else { - fScore = 0 - } - if glog.V(10) { - // We explicitly don't do glog.V(10).Infof() to avoid computing all the parameters if this is - // not logged. There is visible performance gain from it. - glog.Infof("%v -> %v: NodeAffinityPriority, Score: (%d)", pod.Name, result[i].Host, int(fScore)) - } - result[i].Score = int(fScore) - } - return nil -} +var CalculateNodeAffinityPriorityReduce = NormalizeReduce(schedulerapi.MaxPriority, false) diff --git a/plugin/pkg/scheduler/algorithm/priorities/reduce.go b/plugin/pkg/scheduler/algorithm/priorities/reduce.go new file mode 100644 index 0000000000..9ce84fd9f6 --- /dev/null +++ b/plugin/pkg/scheduler/algorithm/priorities/reduce.go @@ -0,0 +1,64 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package priorities + +import ( + "k8s.io/api/core/v1" + "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm" + schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api" + "k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache" +) + +// NormalizeReduce generates a PriorityReduceFunction that can normalize the result +// scores to [0, maxPriority]. If reverse is set to true, it reverses the scores by +// subtracting it from maxPriority. +func NormalizeReduce(maxPriority int, reverse bool) algorithm.PriorityReduceFunction { + return func( + _ *v1.Pod, + _ interface{}, + _ map[string]*schedulercache.NodeInfo, + result schedulerapi.HostPriorityList) error { + + var maxCount int + for i := range result { + if result[i].Score > maxCount { + maxCount = result[i].Score + } + } + + if maxCount == 0 { + if reverse { + for i := range result { + result[i].Score = maxPriority + } + } + return nil + } + + for i := range result { + score := result[i].Score + + score = maxPriority * score / maxCount + if reverse { + score = maxPriority - score + } + + result[i].Score = score + } + return nil + } +} diff --git a/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go b/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go index a017531f2f..da81a3efed 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go +++ b/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go @@ -23,8 +23,6 @@ import ( v1helper "k8s.io/kubernetes/pkg/api/v1/helper" schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api" "k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache" - - "github.com/golang/glog" ) // CountIntolerableTaintsPreferNoSchedule gives the count of intolerable taints of a pod with effect PreferNoSchedule @@ -75,26 +73,4 @@ func ComputeTaintTolerationPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo * } // ComputeTaintTolerationPriorityReduce calculates the source of each node based on the number of intolerable taints on the node -func ComputeTaintTolerationPriorityReduce(pod *v1.Pod, meta interface{}, nodeNameToInfo map[string]*schedulercache.NodeInfo, result schedulerapi.HostPriorityList) error { - var maxCount int - for i := range result { - if result[i].Score > maxCount { - maxCount = result[i].Score - } - } - maxCountFloat := float64(maxCount) - - for i := range result { - fScore := float64(schedulerapi.MaxPriority) - if maxCountFloat > 0 { - fScore = (1.0 - float64(result[i].Score)/maxCountFloat) * float64(schedulerapi.MaxPriority) - } - if glog.V(10) { - // We explicitly don't do glog.V(10).Infof() to avoid computing all the parameters if this is - // not logged. There is visible performance gain from it. - glog.Infof("%v -> %v: Taint Toleration Priority, Score: (%d)", pod.Name, result[i].Host, int(fScore)) - } - result[i].Score = int(fScore) - } - return nil -} +var ComputeTaintTolerationPriorityReduce = NormalizeReduce(schedulerapi.MaxPriority, true) diff --git a/plugin/pkg/scheduler/algorithm/types.go b/plugin/pkg/scheduler/algorithm/types.go index 8932b1f6bd..b3e34e0240 100644 --- a/plugin/pkg/scheduler/algorithm/types.go +++ b/plugin/pkg/scheduler/algorithm/types.go @@ -53,6 +53,7 @@ type MetadataProducer func(pod *v1.Pod, nodeNameToInfo map[string]*schedulercach type PriorityFunction func(pod *v1.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodes []*v1.Node) (schedulerapi.HostPriorityList, error) type PriorityConfig struct { + Name string Map PriorityMapFunction Reduce PriorityReduceFunction // TODO: Remove it after migrating all functions to diff --git a/plugin/pkg/scheduler/core/generic_scheduler.go b/plugin/pkg/scheduler/core/generic_scheduler.go index ee39aea1af..adf6ca7bf9 100644 --- a/plugin/pkg/scheduler/core/generic_scheduler.go +++ b/plugin/pkg/scheduler/core/generic_scheduler.go @@ -433,6 +433,11 @@ func PrioritizeNodes( if err := config.Reduce(pod, meta, nodeNameToInfo, results[index]); err != nil { appendError(err) } + if glog.V(10) { + for _, hostPriority := range results[index] { + glog.Infof("%v -> %v: %v, Score: (%d)", pod.Name, hostPriority.Host, config.Name, hostPriority.Score) + } + } }(i, priorityConfig) } // Wait for all computations to be finished. diff --git a/plugin/pkg/scheduler/factory/plugins.go b/plugin/pkg/scheduler/factory/plugins.go index 9f5398c54b..0d24d0ef30 100644 --- a/plugin/pkg/scheduler/factory/plugins.go +++ b/plugin/pkg/scheduler/factory/plugins.go @@ -429,12 +429,14 @@ func getPriorityFunctionConfigs(names sets.String, args PluginFactoryArgs) ([]al } if factory.Function != nil { configs = append(configs, algorithm.PriorityConfig{ + Name: name, Function: factory.Function(args), Weight: factory.Weight, }) } else { mapFunction, reduceFunction := factory.MapReduceFunction(args) configs = append(configs, algorithm.PriorityConfig{ + Name: name, Map: mapFunction, Reduce: reduceFunction, Weight: factory.Weight,