schedule.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2021 gorse Project Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package task
  15. import (
  16. "runtime"
  17. "sort"
  18. "sync"
  19. "github.com/samber/lo"
  20. "github.com/zhenghaoz/gorse/base/log"
  21. "go.uber.org/zap"
  22. "modernc.org/mathutil"
  23. )
  24. type JobsAllocator struct {
  25. numJobs int // the max number of jobs
  26. taskName string // its task name in scheduler
  27. scheduler *JobsScheduler
  28. }
  29. func NewConstantJobsAllocator(num int) *JobsAllocator {
  30. return &JobsAllocator{
  31. numJobs: num,
  32. }
  33. }
  34. func (allocator *JobsAllocator) MaxJobs() int {
  35. if allocator == nil || allocator.numJobs < 1 {
  36. // Return 1 for invalid allocator
  37. return 1
  38. }
  39. return allocator.numJobs
  40. }
  41. func (allocator *JobsAllocator) AvailableJobs() int {
  42. if allocator == nil || allocator.numJobs < 1 {
  43. // Return 1 for invalid allocator
  44. return 1
  45. } else if allocator.scheduler != nil {
  46. // Use jobs scheduler
  47. return allocator.scheduler.allocateJobsForTask(allocator.taskName, true)
  48. }
  49. return allocator.numJobs
  50. }
  51. // Init jobs allocation. This method is used to request allocation of jobs for the first time.
  52. func (allocator *JobsAllocator) Init() {
  53. if allocator.scheduler != nil {
  54. allocator.scheduler.allocateJobsForTask(allocator.taskName, true)
  55. }
  56. }
  57. // taskInfo represents a task in JobsScheduler.
  58. type taskInfo struct {
  59. name string // name of the task
  60. priority int // high priority tasks are allocated first
  61. privileged bool // privileged tasks are allocated first
  62. jobs int // number of jobs allocated to the task
  63. previous int // previous number of jobs allocated to the task
  64. }
  65. // JobsScheduler allocates jobs to multiple tasks.
  66. type JobsScheduler struct {
  67. *sync.Cond
  68. numJobs int // number of jobs
  69. freeJobs int // number of free jobs
  70. tasks map[string]*taskInfo
  71. }
  72. // NewJobsScheduler creates a JobsScheduler with num jobs.
  73. func NewJobsScheduler(num int) *JobsScheduler {
  74. if num <= 0 {
  75. // Use all cores if num is less than 1.
  76. num = runtime.NumCPU()
  77. }
  78. return &JobsScheduler{
  79. Cond: sync.NewCond(&sync.Mutex{}),
  80. numJobs: num,
  81. freeJobs: num,
  82. tasks: make(map[string]*taskInfo),
  83. }
  84. }
  85. // Register a task in the JobsScheduler. Registered tasks will be ignored and return false.
  86. func (s *JobsScheduler) Register(taskName string, priority int, privileged bool) bool {
  87. s.L.Lock()
  88. defer s.L.Unlock()
  89. if _, exits := s.tasks[taskName]; !exits {
  90. s.tasks[taskName] = &taskInfo{name: taskName, priority: priority, privileged: privileged}
  91. return true
  92. } else {
  93. return false
  94. }
  95. }
  96. // Unregister a task from the JobsScheduler.
  97. func (s *JobsScheduler) Unregister(taskName string) {
  98. s.L.Lock()
  99. defer s.L.Unlock()
  100. if task, exits := s.tasks[taskName]; exits {
  101. // Return allocated jobs.
  102. s.freeJobs += task.jobs
  103. delete(s.tasks, taskName)
  104. s.Broadcast()
  105. }
  106. }
  107. func (s *JobsScheduler) GetJobsAllocator(taskName string) *JobsAllocator {
  108. return &JobsAllocator{
  109. numJobs: s.numJobs,
  110. taskName: taskName,
  111. scheduler: s,
  112. }
  113. }
  114. func (s *JobsScheduler) allocateJobsForTask(taskName string, block bool) int {
  115. // Find current task and return the jobs temporarily.
  116. s.L.Lock()
  117. currentTask, exist := s.tasks[taskName]
  118. if !exist {
  119. panic("task not found")
  120. }
  121. s.freeJobs += currentTask.jobs
  122. currentTask.jobs = 0
  123. s.L.Unlock()
  124. s.L.Lock()
  125. defer s.L.Unlock()
  126. for {
  127. s.allocateJobsForAll()
  128. if currentTask.jobs == 0 && block {
  129. if currentTask.previous > 0 {
  130. log.Logger().Debug("suspend task", zap.String("task", currentTask.name))
  131. s.Broadcast()
  132. }
  133. s.Wait()
  134. } else {
  135. if currentTask.previous == 0 {
  136. log.Logger().Debug("resume task", zap.String("task", currentTask.name))
  137. }
  138. return currentTask.jobs
  139. }
  140. }
  141. }
  142. func (s *JobsScheduler) allocateJobsForAll() {
  143. // Separate privileged tasks and normal tasks
  144. privileged := make([]*taskInfo, 0)
  145. normal := make([]*taskInfo, 0)
  146. for _, task := range s.tasks {
  147. if task.privileged {
  148. privileged = append(privileged, task)
  149. } else {
  150. normal = append(normal, task)
  151. }
  152. }
  153. var tasks []*taskInfo
  154. if len(privileged) > 0 {
  155. tasks = privileged
  156. } else {
  157. tasks = normal
  158. }
  159. // allocate jobs
  160. sort.Slice(tasks, func(i, j int) bool {
  161. return tasks[i].priority > tasks[j].priority
  162. })
  163. for i, task := range tasks {
  164. if s.freeJobs == 0 {
  165. return
  166. }
  167. targetJobs := s.numJobs/len(tasks) + lo.If(i < s.numJobs%len(tasks), 1).Else(0)
  168. targetJobs = mathutil.Min(targetJobs, s.freeJobs)
  169. if task.jobs < targetJobs {
  170. if task.previous != targetJobs {
  171. log.Logger().Debug("reallocate jobs for task",
  172. zap.String("task", task.name),
  173. zap.Int("previous_jobs", task.previous),
  174. zap.Int("target_jobs", targetJobs))
  175. }
  176. s.freeJobs -= targetJobs - task.jobs
  177. task.jobs = targetJobs
  178. task.previous = task.jobs
  179. }
  180. }
  181. }