heap.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. Copyright 2017 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. // This file implements a heap data structure.
  14. package cache
  15. import (
  16. "container/heap"
  17. "fmt"
  18. "sync"
  19. )
  20. const (
  21. closedMsg = "heap is closed"
  22. )
  23. type LessFunc func(interface{}, interface{}) bool
  24. type heapItem struct {
  25. obj interface{} // The object which is stored in the heap.
  26. index int // The index of the object's key in the Heap.queue.
  27. }
  28. type itemKeyValue struct {
  29. key string
  30. obj interface{}
  31. }
  32. // heapData is an internal struct that implements the standard heap interface
  33. // and keeps the data stored in the heap.
  34. type heapData struct {
  35. // items is a map from key of the objects to the objects and their index.
  36. // We depend on the property that items in the map are in the queue and vice versa.
  37. items map[string]*heapItem
  38. // queue implements a heap data structure and keeps the order of elements
  39. // according to the heap invariant. The queue keeps the keys of objects stored
  40. // in "items".
  41. queue []string
  42. // keyFunc is used to make the key used for queued item insertion and retrieval, and
  43. // should be deterministic.
  44. keyFunc KeyFunc
  45. // lessFunc is used to compare two objects in the heap.
  46. lessFunc LessFunc
  47. }
  48. var (
  49. _ = heap.Interface(&heapData{}) // heapData is a standard heap
  50. )
  51. // Less compares two objects and returns true if the first one should go
  52. // in front of the second one in the heap.
  53. func (h *heapData) Less(i, j int) bool {
  54. if i > len(h.queue) || j > len(h.queue) {
  55. return false
  56. }
  57. itemi, ok := h.items[h.queue[i]]
  58. if !ok {
  59. return false
  60. }
  61. itemj, ok := h.items[h.queue[j]]
  62. if !ok {
  63. return false
  64. }
  65. return h.lessFunc(itemi.obj, itemj.obj)
  66. }
  67. // Len returns the number of items in the Heap.
  68. func (h *heapData) Len() int { return len(h.queue) }
  69. // Swap implements swapping of two elements in the heap. This is a part of standard
  70. // heap interface and should never be called directly.
  71. func (h *heapData) Swap(i, j int) {
  72. h.queue[i], h.queue[j] = h.queue[j], h.queue[i]
  73. item := h.items[h.queue[i]]
  74. item.index = i
  75. item = h.items[h.queue[j]]
  76. item.index = j
  77. }
  78. // Push is supposed to be called by heap.Push only.
  79. func (h *heapData) Push(kv interface{}) {
  80. keyValue := kv.(*itemKeyValue)
  81. n := len(h.queue)
  82. h.items[keyValue.key] = &heapItem{keyValue.obj, n}
  83. h.queue = append(h.queue, keyValue.key)
  84. }
  85. // Pop is supposed to be called by heap.Pop only.
  86. func (h *heapData) Pop() interface{} {
  87. key := h.queue[len(h.queue)-1]
  88. h.queue = h.queue[0 : len(h.queue)-1]
  89. item, ok := h.items[key]
  90. if !ok {
  91. // This is an error
  92. return nil
  93. }
  94. delete(h.items, key)
  95. return item.obj
  96. }
  97. // Heap is a thread-safe producer/consumer queue that implements a heap data structure.
  98. // It can be used to implement priority queues and similar data structures.
  99. type Heap struct {
  100. lock sync.RWMutex
  101. cond sync.Cond
  102. // data stores objects and has a queue that keeps their ordering according
  103. // to the heap invariant.
  104. data *heapData
  105. // closed indicates that the queue is closed.
  106. // It is mainly used to let Pop() exit its control loop while waiting for an item.
  107. closed bool
  108. }
  109. // Close the Heap and signals condition variables that may be waiting to pop
  110. // items from the heap.
  111. func (h *Heap) Close() {
  112. h.lock.Lock()
  113. defer h.lock.Unlock()
  114. h.closed = true
  115. h.cond.Broadcast()
  116. }
  117. // Add inserts an item, and puts it in the queue. The item is updated if it
  118. // already exists.
  119. func (h *Heap) Add(obj interface{}) error {
  120. key, err := h.data.keyFunc(obj)
  121. if err != nil {
  122. return KeyError{obj, err}
  123. }
  124. h.lock.Lock()
  125. defer h.lock.Unlock()
  126. if h.closed {
  127. return fmt.Errorf(closedMsg)
  128. }
  129. if _, exists := h.data.items[key]; exists {
  130. h.data.items[key].obj = obj
  131. heap.Fix(h.data, h.data.items[key].index)
  132. } else {
  133. h.addIfNotPresentLocked(key, obj)
  134. }
  135. h.cond.Broadcast()
  136. return nil
  137. }
  138. // Adds all the items in the list to the queue and then signals the condition
  139. // variable. It is useful when the caller would like to add all of the items
  140. // to the queue before consumer starts processing them.
  141. func (h *Heap) BulkAdd(list []interface{}) error {
  142. h.lock.Lock()
  143. defer h.lock.Unlock()
  144. if h.closed {
  145. return fmt.Errorf(closedMsg)
  146. }
  147. for _, obj := range list {
  148. key, err := h.data.keyFunc(obj)
  149. if err != nil {
  150. return KeyError{obj, err}
  151. }
  152. if _, exists := h.data.items[key]; exists {
  153. h.data.items[key].obj = obj
  154. heap.Fix(h.data, h.data.items[key].index)
  155. } else {
  156. h.addIfNotPresentLocked(key, obj)
  157. }
  158. }
  159. h.cond.Broadcast()
  160. return nil
  161. }
  162. // AddIfNotPresent inserts an item, and puts it in the queue. If an item with
  163. // the key is present in the map, no changes is made to the item.
  164. //
  165. // This is useful in a single producer/consumer scenario so that the consumer can
  166. // safely retry items without contending with the producer and potentially enqueueing
  167. // stale items.
  168. func (h *Heap) AddIfNotPresent(obj interface{}) error {
  169. id, err := h.data.keyFunc(obj)
  170. if err != nil {
  171. return KeyError{obj, err}
  172. }
  173. h.lock.Lock()
  174. defer h.lock.Unlock()
  175. if h.closed {
  176. return fmt.Errorf(closedMsg)
  177. }
  178. h.addIfNotPresentLocked(id, obj)
  179. h.cond.Broadcast()
  180. return nil
  181. }
  182. // addIfNotPresentLocked assumes the lock is already held and adds the provided
  183. // item to the queue if it does not already exist.
  184. func (h *Heap) addIfNotPresentLocked(key string, obj interface{}) {
  185. if _, exists := h.data.items[key]; exists {
  186. return
  187. }
  188. heap.Push(h.data, &itemKeyValue{key, obj})
  189. }
  190. // Update is the same as Add in this implementation. When the item does not
  191. // exist, it is added.
  192. func (h *Heap) Update(obj interface{}) error {
  193. return h.Add(obj)
  194. }
  195. // Delete removes an item.
  196. func (h *Heap) Delete(obj interface{}) error {
  197. key, err := h.data.keyFunc(obj)
  198. if err != nil {
  199. return KeyError{obj, err}
  200. }
  201. h.lock.Lock()
  202. defer h.lock.Unlock()
  203. if item, ok := h.data.items[key]; ok {
  204. heap.Remove(h.data, item.index)
  205. return nil
  206. }
  207. return fmt.Errorf("object not found")
  208. }
  209. // Pop waits until an item is ready. If multiple items are
  210. // ready, they are returned in the order given by Heap.data.lessFunc.
  211. func (h *Heap) Pop() (interface{}, error) {
  212. h.lock.Lock()
  213. defer h.lock.Unlock()
  214. for len(h.data.queue) == 0 {
  215. // When the queue is empty, invocation of Pop() is blocked until new item is enqueued.
  216. // When Close() is called, the h.closed is set and the condition is broadcast,
  217. // which causes this loop to continue and return from the Pop().
  218. if h.closed {
  219. return nil, fmt.Errorf("heap is closed")
  220. }
  221. h.cond.Wait()
  222. }
  223. obj := heap.Pop(h.data)
  224. if obj != nil {
  225. return obj, nil
  226. } else {
  227. return nil, fmt.Errorf("object was removed from heap data")
  228. }
  229. }
  230. // List returns a list of all the items.
  231. func (h *Heap) List() []interface{} {
  232. h.lock.RLock()
  233. defer h.lock.RUnlock()
  234. list := make([]interface{}, 0, len(h.data.items))
  235. for _, item := range h.data.items {
  236. list = append(list, item.obj)
  237. }
  238. return list
  239. }
  240. // ListKeys returns a list of all the keys of the objects currently in the Heap.
  241. func (h *Heap) ListKeys() []string {
  242. h.lock.RLock()
  243. defer h.lock.RUnlock()
  244. list := make([]string, 0, len(h.data.items))
  245. for key := range h.data.items {
  246. list = append(list, key)
  247. }
  248. return list
  249. }
  250. // Get returns the requested item, or sets exists=false.
  251. func (h *Heap) Get(obj interface{}) (interface{}, bool, error) {
  252. key, err := h.data.keyFunc(obj)
  253. if err != nil {
  254. return nil, false, KeyError{obj, err}
  255. }
  256. return h.GetByKey(key)
  257. }
  258. // GetByKey returns the requested item, or sets exists=false.
  259. func (h *Heap) GetByKey(key string) (interface{}, bool, error) {
  260. h.lock.RLock()
  261. defer h.lock.RUnlock()
  262. item, exists := h.data.items[key]
  263. if !exists {
  264. return nil, false, nil
  265. }
  266. return item.obj, true, nil
  267. }
  268. // IsClosed returns true if the queue is closed.
  269. func (h *Heap) IsClosed() bool {
  270. h.lock.RLock()
  271. defer h.lock.RUnlock()
  272. if h.closed {
  273. return true
  274. }
  275. return false
  276. }
  277. // NewHeap returns a Heap which can be used to queue up items to process.
  278. func NewHeap(keyFn KeyFunc, lessFn LessFunc) *Heap {
  279. h := &Heap{
  280. data: &heapData{
  281. items: map[string]*heapItem{},
  282. queue: []string{},
  283. keyFunc: keyFn,
  284. lessFunc: lessFn,
  285. },
  286. }
  287. h.cond.L = &h.lock
  288. return h
  289. }