balanced_resource_allocation.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package priorities
  14. import (
  15. "math"
  16. utilfeature "k8s.io/apiserver/pkg/util/feature"
  17. "k8s.io/kubernetes/pkg/features"
  18. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  19. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  20. )
  21. var (
  22. balancedResourcePriority = &ResourceAllocationPriority{"BalancedResourceAllocation", balancedResourceScorer}
  23. // BalancedResourceAllocationMap favors nodes with balanced resource usage rate.
  24. // BalancedResourceAllocationMap should **NOT** be used alone, and **MUST** be used together
  25. // with LeastRequestedPriority. It calculates the difference between the cpu and memory fraction
  26. // of capacity, and prioritizes the host based on how close the two metrics are to each other.
  27. // Detail: score = 10 - variance(cpuFraction,memoryFraction,volumeFraction)*10. The algorithm is partly inspired by:
  28. // "Wei Huang et al. An Energy Efficient Virtual Machine Placement Algorithm with Balanced
  29. // Resource Utilization"
  30. BalancedResourceAllocationMap = balancedResourcePriority.PriorityMap
  31. )
  32. func balancedResourceScorer(requested, allocable *schedulernodeinfo.Resource, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
  33. cpuFraction := fractionOfCapacity(requested.MilliCPU, allocable.MilliCPU)
  34. memoryFraction := fractionOfCapacity(requested.Memory, allocable.Memory)
  35. // This to find a node which has most balanced CPU, memory and volume usage.
  36. if includeVolumes && utilfeature.DefaultFeatureGate.Enabled(features.BalanceAttachedNodeVolumes) && allocatableVolumes > 0 {
  37. volumeFraction := float64(requestedVolumes) / float64(allocatableVolumes)
  38. if cpuFraction >= 1 || memoryFraction >= 1 || volumeFraction >= 1 {
  39. // if requested >= capacity, the corresponding host should never be preferred.
  40. return 0
  41. }
  42. // Compute variance for all the three fractions.
  43. mean := (cpuFraction + memoryFraction + volumeFraction) / float64(3)
  44. variance := float64((((cpuFraction - mean) * (cpuFraction - mean)) + ((memoryFraction - mean) * (memoryFraction - mean)) + ((volumeFraction - mean) * (volumeFraction - mean))) / float64(3))
  45. // Since the variance is between positive fractions, it will be positive fraction. 1-variance lets the
  46. // score to be higher for node which has least variance and multiplying it with 10 provides the scaling
  47. // factor needed.
  48. return int64((1 - variance) * float64(schedulerapi.MaxPriority))
  49. }
  50. if cpuFraction >= 1 || memoryFraction >= 1 {
  51. // if requested >= capacity, the corresponding host should never be preferred.
  52. return 0
  53. }
  54. // Upper and lower boundary of difference between cpuFraction and memoryFraction are -1 and 1
  55. // respectively. Multiplying the absolute value of the difference by 10 scales the value to
  56. // 0-10 with 0 representing well balanced allocation and 10 poorly balanced. Subtracting it from
  57. // 10 leads to the score which also scales from 0 to 10 while 10 representing well balanced.
  58. diff := math.Abs(cpuFraction - memoryFraction)
  59. return int64((1 - diff) * float64(schedulerapi.MaxPriority))
  60. }
  61. func fractionOfCapacity(requested, capacity int64) float64 {
  62. if capacity == 0 {
  63. return 1
  64. }
  65. return float64(requested) / float64(capacity)
  66. }