mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-11-03 23:40:03 +00:00 
			
		
		
		
	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.
This commit is contained in:
		@@ -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",
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										64
									
								
								plugin/pkg/scheduler/algorithm/priorities/reduce.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								plugin/pkg/scheduler/algorithm/priorities/reduce.go
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user