waiting_pods_map.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 v1alpha1
  14. import (
  15. "fmt"
  16. "sync"
  17. "time"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. )
  21. // waitingPodsMap a thread-safe map used to maintain pods waiting in the permit phase.
  22. type waitingPodsMap struct {
  23. pods map[types.UID]*waitingPod
  24. mu sync.RWMutex
  25. }
  26. // newWaitingPodsMap returns a new waitingPodsMap.
  27. func newWaitingPodsMap() *waitingPodsMap {
  28. return &waitingPodsMap{
  29. pods: make(map[types.UID]*waitingPod),
  30. }
  31. }
  32. // add a new WaitingPod to the map.
  33. func (m *waitingPodsMap) add(wp *waitingPod) {
  34. m.mu.Lock()
  35. defer m.mu.Unlock()
  36. m.pods[wp.GetPod().UID] = wp
  37. }
  38. // remove a WaitingPod from the map.
  39. func (m *waitingPodsMap) remove(uid types.UID) {
  40. m.mu.Lock()
  41. defer m.mu.Unlock()
  42. delete(m.pods, uid)
  43. }
  44. // get a WaitingPod from the map.
  45. func (m *waitingPodsMap) get(uid types.UID) *waitingPod {
  46. m.mu.RLock()
  47. defer m.mu.RUnlock()
  48. return m.pods[uid]
  49. }
  50. // iterate acquires a read lock and iterates over the WaitingPods map.
  51. func (m *waitingPodsMap) iterate(callback func(WaitingPod)) {
  52. m.mu.RLock()
  53. defer m.mu.RUnlock()
  54. for _, v := range m.pods {
  55. callback(v)
  56. }
  57. }
  58. // waitingPod represents a pod waiting in the permit phase.
  59. type waitingPod struct {
  60. pod *v1.Pod
  61. pendingPlugins map[string]*time.Timer
  62. s chan *Status
  63. mu sync.RWMutex
  64. }
  65. var _ WaitingPod = &waitingPod{}
  66. // newWaitingPod returns a new waitingPod instance.
  67. func newWaitingPod(pod *v1.Pod, pluginsMaxWaitTime map[string]time.Duration) *waitingPod {
  68. wp := &waitingPod{
  69. pod: pod,
  70. // Allow() and Reject() calls are non-blocking. This property is guaranteed
  71. // by using non-blocking send to this channel. This channel has a buffer of size 1
  72. // to ensure that non-blocking send will not be ignored - possible situation when
  73. // receiving from this channel happens after non-blocking send.
  74. s: make(chan *Status, 1),
  75. }
  76. wp.pendingPlugins = make(map[string]*time.Timer, len(pluginsMaxWaitTime))
  77. // The time.AfterFunc calls wp.Reject which iterates through pendingPlugins map. Acquire the
  78. // lock here so that time.AfterFunc can only execute after newWaitingPod finishes.
  79. wp.mu.Lock()
  80. defer wp.mu.Unlock()
  81. for k, v := range pluginsMaxWaitTime {
  82. plugin, waitTime := k, v
  83. wp.pendingPlugins[plugin] = time.AfterFunc(waitTime, func() {
  84. msg := fmt.Sprintf("rejected due to timeout after waiting %v at plugin %v",
  85. waitTime, plugin)
  86. wp.Reject(msg)
  87. })
  88. }
  89. return wp
  90. }
  91. // GetPod returns a reference to the waiting pod.
  92. func (w *waitingPod) GetPod() *v1.Pod {
  93. return w.pod
  94. }
  95. // GetPendingPlugins returns a list of pending permit plugin's name.
  96. func (w *waitingPod) GetPendingPlugins() []string {
  97. w.mu.RLock()
  98. defer w.mu.RUnlock()
  99. plugins := make([]string, 0, len(w.pendingPlugins))
  100. for p := range w.pendingPlugins {
  101. plugins = append(plugins, p)
  102. }
  103. return plugins
  104. }
  105. // Allow declares the waiting pod is allowed to be scheduled by plugin pluginName.
  106. // If this is the last remaining plugin to allow, then a success signal is delivered
  107. // to unblock the pod.
  108. func (w *waitingPod) Allow(pluginName string) {
  109. w.mu.Lock()
  110. defer w.mu.Unlock()
  111. if timer, exist := w.pendingPlugins[pluginName]; exist {
  112. timer.Stop()
  113. delete(w.pendingPlugins, pluginName)
  114. }
  115. // Only signal success status after all plugins have allowed
  116. if len(w.pendingPlugins) != 0 {
  117. return
  118. }
  119. // The select clause works as a non-blocking send.
  120. // If there is no receiver, it's a no-op (default case).
  121. select {
  122. case w.s <- NewStatus(Success, ""):
  123. default:
  124. }
  125. }
  126. // Reject declares the waiting pod unschedulable.
  127. func (w *waitingPod) Reject(msg string) {
  128. w.mu.RLock()
  129. defer w.mu.RUnlock()
  130. for _, timer := range w.pendingPlugins {
  131. timer.Stop()
  132. }
  133. // The select clause works as a non-blocking send.
  134. // If there is no receiver, it's a no-op (default case).
  135. select {
  136. case w.s <- NewStatus(Unschedulable, msg):
  137. default:
  138. }
  139. }