waiting_pods_map.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // newWaitingPod returns a new waitingPod instance.
  66. func newWaitingPod(pod *v1.Pod, pluginsMaxWaitTime map[string]time.Duration) *waitingPod {
  67. wp := &waitingPod{
  68. pod: pod,
  69. s: make(chan *Status),
  70. }
  71. wp.pendingPlugins = make(map[string]*time.Timer, len(pluginsMaxWaitTime))
  72. // The time.AfterFunc calls wp.Reject which iterates through pendingPlugins map. Acquire the
  73. // lock here so that time.AfterFunc can only execute after newWaitingPod finishes.
  74. wp.mu.Lock()
  75. defer wp.mu.Unlock()
  76. for k, v := range pluginsMaxWaitTime {
  77. plugin, waitTime := k, v
  78. wp.pendingPlugins[plugin] = time.AfterFunc(waitTime, func() {
  79. msg := fmt.Sprintf("rejected due to timeout after waiting %v at plugin %v",
  80. waitTime, plugin)
  81. wp.Reject(msg)
  82. })
  83. }
  84. return wp
  85. }
  86. // GetPod returns a reference to the waiting pod.
  87. func (w *waitingPod) GetPod() *v1.Pod {
  88. return w.pod
  89. }
  90. // GetPendingPlugins returns a list of pending permit plugin's name.
  91. func (w *waitingPod) GetPendingPlugins() []string {
  92. w.mu.RLock()
  93. defer w.mu.RUnlock()
  94. plugins := make([]string, 0, len(w.pendingPlugins))
  95. for p := range w.pendingPlugins {
  96. plugins = append(plugins, p)
  97. }
  98. return plugins
  99. }
  100. // Allow declares the waiting pod is allowed to be scheduled by plugin pluginName.
  101. // If this is the last remaining plugin to allow, then a success signal is delivered
  102. // to unblock the pod.
  103. func (w *waitingPod) Allow(pluginName string) {
  104. w.mu.Lock()
  105. defer w.mu.Unlock()
  106. if timer, exist := w.pendingPlugins[pluginName]; exist {
  107. timer.Stop()
  108. delete(w.pendingPlugins, pluginName)
  109. }
  110. // Only signal success status after all plugins have allowed
  111. if len(w.pendingPlugins) != 0 {
  112. return
  113. }
  114. // The select clause works as a non-blocking send.
  115. // If there is no receiver, it's a no-op (default case).
  116. select {
  117. case w.s <- NewStatus(Success, ""):
  118. default:
  119. }
  120. }
  121. // Reject declares the waiting pod unschedulable.
  122. func (w *waitingPod) Reject(msg string) {
  123. w.mu.RLock()
  124. defer w.mu.RUnlock()
  125. for _, timer := range w.pendingPlugins {
  126. timer.Stop()
  127. }
  128. // The select clause works as a non-blocking send.
  129. // If there is no receiver, it's a no-op (default case).
  130. select {
  131. case w.s <- NewStatus(Unschedulable, msg):
  132. default:
  133. }
  134. }