pod_backoff.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2019 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 queue
  14. import (
  15. "sync"
  16. "time"
  17. ktypes "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/kubernetes/pkg/scheduler/util"
  19. )
  20. // PodBackoffMap is a structure that stores backoff related information for pods
  21. type PodBackoffMap struct {
  22. // lock for performing actions on this PodBackoffMap
  23. lock sync.RWMutex
  24. clock util.Clock
  25. // initial backoff duration
  26. initialDuration time.Duration
  27. // maximal backoff duration
  28. maxDuration time.Duration
  29. // map for pod -> number of attempts for this pod
  30. podAttempts map[ktypes.NamespacedName]int
  31. // map for pod -> lastUpdateTime pod of this pod
  32. podLastUpdateTime map[ktypes.NamespacedName]time.Time
  33. }
  34. // NewPodBackoffMap creates a PodBackoffMap with initial duration and max duration.
  35. func NewPodBackoffMap(initialDuration, maxDuration time.Duration, clock util.Clock) *PodBackoffMap {
  36. return &PodBackoffMap{
  37. clock: clock,
  38. initialDuration: initialDuration,
  39. maxDuration: maxDuration,
  40. podAttempts: make(map[ktypes.NamespacedName]int),
  41. podLastUpdateTime: make(map[ktypes.NamespacedName]time.Time),
  42. }
  43. }
  44. // GetBackoffTime returns the time that nsPod completes backoff
  45. func (pbm *PodBackoffMap) GetBackoffTime(nsPod ktypes.NamespacedName) (time.Time, bool) {
  46. pbm.lock.RLock()
  47. defer pbm.lock.RUnlock()
  48. if _, found := pbm.podAttempts[nsPod]; found == false {
  49. return time.Time{}, false
  50. }
  51. lastUpdateTime := pbm.podLastUpdateTime[nsPod]
  52. backoffDuration := pbm.calculateBackoffDuration(nsPod)
  53. backoffTime := lastUpdateTime.Add(backoffDuration)
  54. return backoffTime, true
  55. }
  56. // calculateBackoffDuration is a helper function for calculating the backoffDuration
  57. // based on the number of attempts the pod has made.
  58. func (pbm *PodBackoffMap) calculateBackoffDuration(nsPod ktypes.NamespacedName) time.Duration {
  59. backoffDuration := pbm.initialDuration
  60. if _, found := pbm.podAttempts[nsPod]; found {
  61. for i := 1; i < pbm.podAttempts[nsPod]; i++ {
  62. backoffDuration = backoffDuration * 2
  63. if backoffDuration > pbm.maxDuration {
  64. return pbm.maxDuration
  65. }
  66. }
  67. }
  68. return backoffDuration
  69. }
  70. // clearPodBackoff removes all tracking information for nsPod.
  71. // Lock is supposed to be acquired by caller.
  72. func (pbm *PodBackoffMap) clearPodBackoff(nsPod ktypes.NamespacedName) {
  73. delete(pbm.podAttempts, nsPod)
  74. delete(pbm.podLastUpdateTime, nsPod)
  75. }
  76. // ClearPodBackoff is the thread safe version of clearPodBackoff
  77. func (pbm *PodBackoffMap) ClearPodBackoff(nsPod ktypes.NamespacedName) {
  78. pbm.lock.Lock()
  79. pbm.clearPodBackoff(nsPod)
  80. pbm.lock.Unlock()
  81. }
  82. // CleanupPodsCompletesBackingoff execute garbage collection on the pod backoff,
  83. // i.e, it will remove a pod from the PodBackoffMap if
  84. // lastUpdateTime + maxDuration >> timestamp
  85. // We should wait longer than the maxDuration so that the pod gets a chance to
  86. // (1) move to the active queue and (2) get an schedule attempt.
  87. func (pbm *PodBackoffMap) CleanupPodsCompletesBackingoff() {
  88. pbm.lock.Lock()
  89. defer pbm.lock.Unlock()
  90. for pod, value := range pbm.podLastUpdateTime {
  91. // Here we assume that maxDuration should be enough for a pod to move up the
  92. // active queue and get an schedule attempt.
  93. if value.Add(2 * pbm.maxDuration).Before(pbm.clock.Now()) {
  94. pbm.clearPodBackoff(pod)
  95. }
  96. }
  97. }
  98. // BackoffPod updates the lastUpdateTime for an nsPod,
  99. // and increases its numberOfAttempts by 1
  100. func (pbm *PodBackoffMap) BackoffPod(nsPod ktypes.NamespacedName) {
  101. pbm.lock.Lock()
  102. pbm.podLastUpdateTime[nsPod] = pbm.clock.Now()
  103. pbm.podAttempts[nsPod]++
  104. pbm.lock.Unlock()
  105. }