2019-05-31 09:45:11 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package workqueue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-12-08 13:50:47 +00:00
|
|
|
"k8s.io/utils/clock"
|
2019-05-31 09:45:11 +00:00
|
|
|
)
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
// Deprecated: Interface is deprecated, use TypedInterface instead.
|
|
|
|
type Interface TypedInterface[any]
|
|
|
|
|
|
|
|
type TypedInterface[T comparable] interface {
|
|
|
|
Add(item T)
|
2019-05-31 09:45:11 +00:00
|
|
|
Len() int
|
2024-08-19 08:01:33 +00:00
|
|
|
Get() (item T, shutdown bool)
|
|
|
|
Done(item T)
|
2019-05-31 09:45:11 +00:00
|
|
|
ShutDown()
|
2021-12-08 13:50:47 +00:00
|
|
|
ShutDownWithDrain()
|
2019-05-31 09:45:11 +00:00
|
|
|
ShuttingDown() bool
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
// Queue is the underlying storage for items. The functions below are always
|
|
|
|
// called from the same goroutine.
|
|
|
|
type Queue[T comparable] interface {
|
|
|
|
// Touch can be hooked when an existing item is added again. This may be
|
|
|
|
// useful if the implementation allows priority change for the given item.
|
|
|
|
Touch(item T)
|
|
|
|
// Push adds a new item.
|
|
|
|
Push(item T)
|
|
|
|
// Len tells the total number of items.
|
|
|
|
Len() int
|
|
|
|
// Pop retrieves an item.
|
|
|
|
Pop() (item T)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultQueue is a slice based FIFO queue.
|
|
|
|
func DefaultQueue[T comparable]() Queue[T] {
|
|
|
|
return new(queue[T])
|
|
|
|
}
|
|
|
|
|
|
|
|
// queue is a slice which implements Queue.
|
|
|
|
type queue[T comparable] []T
|
|
|
|
|
|
|
|
func (q *queue[T]) Touch(item T) {}
|
|
|
|
|
|
|
|
func (q *queue[T]) Push(item T) {
|
|
|
|
*q = append(*q, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queue[T]) Len() int {
|
|
|
|
return len(*q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queue[T]) Pop() (item T) {
|
|
|
|
item = (*q)[0]
|
|
|
|
|
|
|
|
// The underlying array still exists and reference this object, so the object will not be garbage collected.
|
|
|
|
(*q)[0] = *new(T)
|
|
|
|
*q = (*q)[1:]
|
|
|
|
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:58:10 +00:00
|
|
|
// QueueConfig specifies optional configurations to customize an Interface.
|
2024-08-19 08:01:33 +00:00
|
|
|
// Deprecated: use TypedQueueConfig instead.
|
|
|
|
type QueueConfig = TypedQueueConfig[any]
|
|
|
|
|
|
|
|
type TypedQueueConfig[T comparable] struct {
|
2023-06-01 16:58:10 +00:00
|
|
|
// Name for the queue. If unnamed, the metrics will not be registered.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// MetricsProvider optionally allows specifying a metrics provider to use for the queue
|
|
|
|
// instead of the global provider.
|
|
|
|
MetricsProvider MetricsProvider
|
|
|
|
|
|
|
|
// Clock ability to inject real or fake clock for testing purposes.
|
|
|
|
Clock clock.WithTicker
|
2024-08-19 08:01:33 +00:00
|
|
|
|
|
|
|
// Queue provides the underlying queue to use. It is optional and defaults to slice based FIFO queue.
|
|
|
|
Queue Queue[T]
|
2023-06-01 16:58:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 09:45:11 +00:00
|
|
|
// New constructs a new work queue (see the package comment).
|
2024-08-19 08:01:33 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use NewTyped instead.
|
2019-05-31 09:45:11 +00:00
|
|
|
func New() *Type {
|
2023-06-01 16:58:10 +00:00
|
|
|
return NewWithConfig(QueueConfig{
|
|
|
|
Name: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
// NewTyped constructs a new work queue (see the package comment).
|
|
|
|
func NewTyped[T comparable]() *Typed[T] {
|
|
|
|
return NewTypedWithConfig(TypedQueueConfig[T]{
|
|
|
|
Name: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:58:10 +00:00
|
|
|
// NewWithConfig constructs a new workqueue with ability to
|
|
|
|
// customize different properties.
|
2024-08-19 08:01:33 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use NewTypedWithConfig instead.
|
2023-06-01 16:58:10 +00:00
|
|
|
func NewWithConfig(config QueueConfig) *Type {
|
2024-08-19 08:01:33 +00:00
|
|
|
return NewTypedWithConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTypedWithConfig constructs a new workqueue with ability to
|
|
|
|
// customize different properties.
|
|
|
|
func NewTypedWithConfig[T comparable](config TypedQueueConfig[T]) *Typed[T] {
|
2023-06-01 16:58:10 +00:00
|
|
|
return newQueueWithConfig(config, defaultUnfinishedWorkUpdatePeriod)
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 16:58:10 +00:00
|
|
|
// NewNamed creates a new named queue.
|
|
|
|
// Deprecated: Use NewWithConfig instead.
|
2019-05-31 09:45:11 +00:00
|
|
|
func NewNamed(name string) *Type {
|
2023-06-01 16:58:10 +00:00
|
|
|
return NewWithConfig(QueueConfig{
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// newQueueWithConfig constructs a new named workqueue
|
|
|
|
// with the ability to customize different properties for testing purposes
|
2024-08-19 08:01:33 +00:00
|
|
|
func newQueueWithConfig[T comparable](config TypedQueueConfig[T], updatePeriod time.Duration) *Typed[T] {
|
2023-06-01 16:58:10 +00:00
|
|
|
var metricsFactory *queueMetricsFactory
|
|
|
|
if config.MetricsProvider != nil {
|
|
|
|
metricsFactory = &queueMetricsFactory{
|
|
|
|
metricsProvider: config.MetricsProvider,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
metricsFactory = &globalMetricsFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Clock == nil {
|
|
|
|
config.Clock = clock.RealClock{}
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
if config.Queue == nil {
|
|
|
|
config.Queue = DefaultQueue[T]()
|
|
|
|
}
|
|
|
|
|
2019-05-31 09:45:11 +00:00
|
|
|
return newQueue(
|
2023-06-01 16:58:10 +00:00
|
|
|
config.Clock,
|
2024-08-19 08:01:33 +00:00
|
|
|
config.Queue,
|
2023-06-01 16:58:10 +00:00
|
|
|
metricsFactory.newQueueMetrics(config.Name, config.Clock),
|
|
|
|
updatePeriod,
|
2019-05-31 09:45:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func newQueue[T comparable](c clock.WithTicker, queue Queue[T], metrics queueMetrics, updatePeriod time.Duration) *Typed[T] {
|
|
|
|
t := &Typed[T]{
|
2019-05-31 09:45:11 +00:00
|
|
|
clock: c,
|
2024-08-19 08:01:33 +00:00
|
|
|
queue: queue,
|
|
|
|
dirty: set[T]{},
|
|
|
|
processing: set[T]{},
|
2019-05-31 09:45:11 +00:00
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
metrics: metrics,
|
|
|
|
unfinishedWorkUpdatePeriod: updatePeriod,
|
|
|
|
}
|
2021-06-25 04:59:51 +00:00
|
|
|
|
|
|
|
// Don't start the goroutine for a type of noMetrics so we don't consume
|
|
|
|
// resources unnecessarily
|
|
|
|
if _, ok := metrics.(noMetrics); !ok {
|
|
|
|
go t.updateUnfinishedWorkLoop()
|
|
|
|
}
|
|
|
|
|
2019-05-31 09:45:11 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultUnfinishedWorkUpdatePeriod = 500 * time.Millisecond
|
|
|
|
|
|
|
|
// Type is a work queue (see the package comment).
|
2024-08-19 08:01:33 +00:00
|
|
|
// Deprecated: Use Typed instead.
|
|
|
|
type Type = Typed[any]
|
|
|
|
|
|
|
|
type Typed[t comparable] struct {
|
2019-05-31 09:45:11 +00:00
|
|
|
// queue defines the order in which we will work on items. Every
|
|
|
|
// element of queue should be in the dirty set and not in the
|
|
|
|
// processing set.
|
2024-08-19 08:01:33 +00:00
|
|
|
queue Queue[t]
|
2019-05-31 09:45:11 +00:00
|
|
|
|
|
|
|
// dirty defines all of the items that need to be processed.
|
2024-08-19 08:01:33 +00:00
|
|
|
dirty set[t]
|
2019-05-31 09:45:11 +00:00
|
|
|
|
|
|
|
// Things that are currently being processed are in the processing set.
|
|
|
|
// These things may be simultaneously in the dirty set. When we finish
|
|
|
|
// processing something and remove it from this set, we'll check if
|
|
|
|
// it's in the dirty set, and if so, add it to the queue.
|
2024-08-19 08:01:33 +00:00
|
|
|
processing set[t]
|
2019-05-31 09:45:11 +00:00
|
|
|
|
|
|
|
cond *sync.Cond
|
|
|
|
|
|
|
|
shuttingDown bool
|
2021-12-08 13:50:47 +00:00
|
|
|
drain bool
|
2019-05-31 09:45:11 +00:00
|
|
|
|
|
|
|
metrics queueMetrics
|
|
|
|
|
|
|
|
unfinishedWorkUpdatePeriod time.Duration
|
2021-12-08 13:50:47 +00:00
|
|
|
clock clock.WithTicker
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type empty struct{}
|
|
|
|
type t interface{}
|
2024-08-19 08:01:33 +00:00
|
|
|
type set[t comparable] map[t]empty
|
2019-05-31 09:45:11 +00:00
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (s set[t]) has(item t) bool {
|
2019-05-31 09:45:11 +00:00
|
|
|
_, exists := s[item]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (s set[t]) insert(item t) {
|
2019-05-31 09:45:11 +00:00
|
|
|
s[item] = empty{}
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (s set[t]) delete(item t) {
|
2019-05-31 09:45:11 +00:00
|
|
|
delete(s, item)
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (s set[t]) len() int {
|
2021-12-08 13:50:47 +00:00
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
2019-05-31 09:45:11 +00:00
|
|
|
// Add marks item as needing processing.
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) Add(item T) {
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
|
|
|
if q.shuttingDown {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if q.dirty.has(item) {
|
2024-08-19 08:01:33 +00:00
|
|
|
// the same item is added again before it is processed, call the Touch
|
|
|
|
// function if the queue cares about it (for e.g, reset its priority)
|
|
|
|
if !q.processing.has(item) {
|
|
|
|
q.queue.Touch(item)
|
|
|
|
}
|
2019-05-31 09:45:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q.metrics.add(item)
|
|
|
|
|
|
|
|
q.dirty.insert(item)
|
|
|
|
if q.processing.has(item) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
q.queue.Push(item)
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.Signal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the current queue length, for informational purposes only. You
|
|
|
|
// shouldn't e.g. gate a call to Add() or Get() on Len() being a particular
|
|
|
|
// value, that can't be synchronized properly.
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) Len() int {
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
2024-08-19 08:01:33 +00:00
|
|
|
return q.queue.Len()
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get blocks until it can return an item to be processed. If shutdown = true,
|
|
|
|
// the caller should end their goroutine. You must call Done with item when you
|
|
|
|
// have finished processing it.
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) Get() (item T, shutdown bool) {
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
2024-08-19 08:01:33 +00:00
|
|
|
for q.queue.Len() == 0 && !q.shuttingDown {
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.Wait()
|
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
if q.queue.Len() == 0 {
|
2019-05-31 09:45:11 +00:00
|
|
|
// We must be shutting down.
|
2024-08-19 08:01:33 +00:00
|
|
|
return *new(T), true
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
item = q.queue.Pop()
|
2019-05-31 09:45:11 +00:00
|
|
|
|
|
|
|
q.metrics.get(item)
|
|
|
|
|
|
|
|
q.processing.insert(item)
|
|
|
|
q.dirty.delete(item)
|
|
|
|
|
|
|
|
return item, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done marks item as done processing, and if it has been marked as dirty again
|
|
|
|
// while it was being processed, it will be re-added to the queue for
|
|
|
|
// re-processing.
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) Done(item T) {
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
|
|
|
|
|
|
|
q.metrics.done(item)
|
|
|
|
|
|
|
|
q.processing.delete(item)
|
|
|
|
if q.dirty.has(item) {
|
2024-08-19 08:01:33 +00:00
|
|
|
q.queue.Push(item)
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.Signal()
|
2021-12-08 13:50:47 +00:00
|
|
|
} else if q.processing.len() == 0 {
|
|
|
|
q.cond.Signal()
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 13:50:47 +00:00
|
|
|
// ShutDown will cause q to ignore all new items added to it and
|
|
|
|
// immediately instruct the worker goroutines to exit.
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) ShutDown() {
|
2023-12-20 12:23:59 +00:00
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
|
|
|
|
|
|
|
q.drain = false
|
|
|
|
q.shuttingDown = true
|
|
|
|
q.cond.Broadcast()
|
2021-12-08 13:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShutDownWithDrain will cause q to ignore all new items added to it. As soon
|
|
|
|
// as the worker goroutines have "drained", i.e: finished processing and called
|
|
|
|
// Done on all existing items in the queue; they will be instructed to exit and
|
|
|
|
// ShutDownWithDrain will return. Hence: a strict requirement for using this is;
|
|
|
|
// your workers must ensure that Done is called on all items in the queue once
|
|
|
|
// the shut down has been initiated, if that is not the case: this will block
|
|
|
|
// indefinitely. It is, however, safe to call ShutDown after having called
|
|
|
|
// ShutDownWithDrain, as to force the queue shut down to terminate immediately
|
|
|
|
// without waiting for the drainage.
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) ShutDownWithDrain() {
|
2021-12-08 13:50:47 +00:00
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
|
|
|
|
2023-12-20 12:23:59 +00:00
|
|
|
q.drain = true
|
2019-05-31 09:45:11 +00:00
|
|
|
q.shuttingDown = true
|
|
|
|
q.cond.Broadcast()
|
2023-12-20 12:23:59 +00:00
|
|
|
|
|
|
|
for q.processing.len() != 0 && q.drain {
|
|
|
|
q.cond.Wait()
|
|
|
|
}
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) ShuttingDown() bool {
|
2019-05-31 09:45:11 +00:00
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
|
|
|
|
|
|
|
return q.shuttingDown
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (q *Typed[T]) updateUnfinishedWorkLoop() {
|
2019-05-31 09:45:11 +00:00
|
|
|
t := q.clock.NewTicker(q.unfinishedWorkUpdatePeriod)
|
|
|
|
defer t.Stop()
|
|
|
|
for range t.C() {
|
|
|
|
if !func() bool {
|
|
|
|
q.cond.L.Lock()
|
|
|
|
defer q.cond.L.Unlock()
|
|
|
|
if !q.shuttingDown {
|
|
|
|
q.metrics.updateUnfinishedWork()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
|
|
|
|
}() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|