timed_workers.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright 2015 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 scheduler
  14. import (
  15. "sync"
  16. "time"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/klog"
  19. )
  20. // WorkArgs keeps arguments that will be passed to the function executed by the worker.
  21. type WorkArgs struct {
  22. NamespacedName types.NamespacedName
  23. }
  24. // KeyFromWorkArgs creates a key for the given `WorkArgs`
  25. func (w *WorkArgs) KeyFromWorkArgs() string {
  26. return w.NamespacedName.String()
  27. }
  28. // NewWorkArgs is a helper function to create new `WorkArgs`
  29. func NewWorkArgs(name, namespace string) *WorkArgs {
  30. return &WorkArgs{types.NamespacedName{Namespace: namespace, Name: name}}
  31. }
  32. // TimedWorker is a responsible for executing a function no earlier than at FireAt time.
  33. type TimedWorker struct {
  34. WorkItem *WorkArgs
  35. CreatedAt time.Time
  36. FireAt time.Time
  37. Timer *time.Timer
  38. }
  39. // CreateWorker creates a TimedWorker that will execute `f` not earlier than `fireAt`.
  40. func CreateWorker(args *WorkArgs, createdAt time.Time, fireAt time.Time, f func(args *WorkArgs) error) *TimedWorker {
  41. delay := fireAt.Sub(createdAt)
  42. if delay <= 0 {
  43. go f(args)
  44. return nil
  45. }
  46. timer := time.AfterFunc(delay, func() { f(args) })
  47. return &TimedWorker{
  48. WorkItem: args,
  49. CreatedAt: createdAt,
  50. FireAt: fireAt,
  51. Timer: timer,
  52. }
  53. }
  54. // Cancel cancels the execution of function by the `TimedWorker`
  55. func (w *TimedWorker) Cancel() {
  56. if w != nil {
  57. w.Timer.Stop()
  58. }
  59. }
  60. // TimedWorkerQueue keeps a set of TimedWorkers that are still wait for execution.
  61. type TimedWorkerQueue struct {
  62. sync.Mutex
  63. // map of workers keyed by string returned by 'KeyFromWorkArgs' from the given worker.
  64. workers map[string]*TimedWorker
  65. workFunc func(args *WorkArgs) error
  66. }
  67. // CreateWorkerQueue creates a new TimedWorkerQueue for workers that will execute
  68. // given function `f`.
  69. func CreateWorkerQueue(f func(args *WorkArgs) error) *TimedWorkerQueue {
  70. return &TimedWorkerQueue{
  71. workers: make(map[string]*TimedWorker),
  72. workFunc: f,
  73. }
  74. }
  75. func (q *TimedWorkerQueue) getWrappedWorkerFunc(key string) func(args *WorkArgs) error {
  76. return func(args *WorkArgs) error {
  77. err := q.workFunc(args)
  78. q.Lock()
  79. defer q.Unlock()
  80. if err == nil {
  81. // To avoid duplicated calls we keep the key in the queue, to prevent
  82. // subsequent additions.
  83. q.workers[key] = nil
  84. } else {
  85. delete(q.workers, key)
  86. }
  87. return err
  88. }
  89. }
  90. // AddWork adds a work to the WorkerQueue which will be executed not earlier than `fireAt`.
  91. func (q *TimedWorkerQueue) AddWork(args *WorkArgs, createdAt time.Time, fireAt time.Time) {
  92. key := args.KeyFromWorkArgs()
  93. klog.V(4).Infof("Adding TimedWorkerQueue item %v at %v to be fired at %v", key, createdAt, fireAt)
  94. q.Lock()
  95. defer q.Unlock()
  96. if _, exists := q.workers[key]; exists {
  97. klog.Warningf("Trying to add already existing work for %+v. Skipping.", args)
  98. return
  99. }
  100. worker := CreateWorker(args, createdAt, fireAt, q.getWrappedWorkerFunc(key))
  101. q.workers[key] = worker
  102. }
  103. // CancelWork removes scheduled function execution from the queue. Returns true if work was cancelled.
  104. func (q *TimedWorkerQueue) CancelWork(key string) bool {
  105. q.Lock()
  106. defer q.Unlock()
  107. worker, found := q.workers[key]
  108. result := false
  109. if found {
  110. klog.V(4).Infof("Cancelling TimedWorkerQueue item %v at %v", key, time.Now())
  111. if worker != nil {
  112. result = true
  113. worker.Cancel()
  114. }
  115. delete(q.workers, key)
  116. }
  117. return result
  118. }
  119. // GetWorkerUnsafe returns a TimedWorker corresponding to the given key.
  120. // Unsafe method - workers have attached goroutines which can fire afater this function is called.
  121. func (q *TimedWorkerQueue) GetWorkerUnsafe(key string) *TimedWorker {
  122. q.Lock()
  123. defer q.Unlock()
  124. return q.workers[key]
  125. }