pod_backoff.go 3.6 KB

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