| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package task |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"sync" |
| 22 |
"sync/atomic" |
| 23 |
"time" |
| 24 |
|
| 25 |
"golang.org/x/sync/semaphore" |
| 26 |
) |
| 27 |
|
| 28 |
// NewBackgroundTaskManager provides a task manager. You can specify the |
| 29 |
// concurrency of background tasks. When running a background task, this will be |
| 30 |
// forced to wait until no prioritized task is running for some period. You can |
| 31 |
// specify the period through the argument of this function, too. |
| 32 |
func NewBackgroundTaskManager(concurrency int64, period time.Duration) *BackgroundTaskManager { |
| 33 |
return &BackgroundTaskManager{ |
| 34 |
backgroundSem: semaphore.NewWeighted(concurrency), |
| 35 |
prioritizedTaskSilencePeriod: period, |
| 36 |
prioritizedTaskStartNotify: make(chan struct{}), |
| 37 |
prioritizedTaskDoneCond: sync.NewCond(&sync.Mutex{}), |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// BackgroundTaskManager is a task manager which manages prioritized tasks and |
| 42 |
// background tasks execution. Background tasks are less important than |
| 43 |
// prioritized tasks. You can let these background tasks not to use compute |
| 44 |
// resources (CPU, NW, etc...) during more important tasks(=prioritized tasks) |
| 45 |
// running. |
| 46 |
// |
| 47 |
// When you run a prioritised task and don't want background tasks to use |
| 48 |
// resources you can tell it this manager by calling DoPrioritizedTask method. |
| 49 |
// DonePrioritizedTask method must be called at the end of the prioritised task |
| 50 |
// execution. |
| 51 |
// |
| 52 |
// For running a background task, you can use InvokeBackgroundTask method. The |
| 53 |
// background task must be able to be cancelled via context.Context argument. |
| 54 |
// The task is forced to wait until no prioritized task is running for some |
| 55 |
// period. You can specify the period when making this manager instance. The |
| 56 |
// limited number of background tasks run simultaneously and you can specify the |
| 57 |
// concurrency when making this manager instance too. If a prioritized task |
| 58 |
// starts during the execution of background tasks, all background tasks running |
| 59 |
// will be cancelled via context. These cancelled tasks will be executed again |
| 60 |
// later, same as other background tasks (when no prioritized task is running |
| 61 |
// for some period). |
| 62 |
type BackgroundTaskManager struct { |
| 63 |
prioritizedTasks int64 |
| 64 |
backgroundSem *semaphore.Weighted |
| 65 |
prioritizedTaskSilencePeriod time.Duration |
| 66 |
prioritizedTaskStartNotify chan struct{} |
| 67 |
prioritizedTaskStartNotifyMu sync.Mutex |
| 68 |
prioritizedTaskDoneCond *sync.Cond |
| 69 |
} |
| 70 |
|
| 71 |
// DoPrioritizedTask tells the manager that we are running a prioritized task |
| 72 |
// and don't want background tasks to disturb resources(CPU, NW, etc...) |
| 73 |
func (ts *BackgroundTaskManager) DoPrioritizedTask() { |
| 74 |
// Notify the prioritized task execution to background tasks. |
| 75 |
ts.prioritizedTaskStartNotifyMu.Lock() |
| 76 |
atomic.AddInt64(&ts.prioritizedTasks, 1) |
| 77 |
close(ts.prioritizedTaskStartNotify) |
| 78 |
ts.prioritizedTaskStartNotify = make(chan struct{}) |
| 79 |
ts.prioritizedTaskStartNotifyMu.Unlock() |
| 80 |
} |
| 81 |
|
| 82 |
// DonePrioritizedTask tells the manager that we've done a prioritized task |
| 83 |
// and don't want background tasks to disturb resources(CPU, NW, etc...) |
| 84 |
func (ts *BackgroundTaskManager) DonePrioritizedTask() { |
| 85 |
go func() { |
| 86 |
// Notify the task completion after `ts.prioritizedTaskSilencePeriod` |
| 87 |
// so that background tasks aren't invoked immediately. |
| 88 |
time.Sleep(ts.prioritizedTaskSilencePeriod) |
| 89 |
atomic.AddInt64(&ts.prioritizedTasks, -1) |
| 90 |
ts.prioritizedTaskDoneCond.L.Lock() |
| 91 |
ts.prioritizedTaskDoneCond.Broadcast() |
| 92 |
ts.prioritizedTaskDoneCond.L.Unlock() |
| 93 |
}() |
| 94 |
} |
| 95 |
|
| 96 |
// InvokeBackgroundTask invokes a background task. The task is started only when |
| 97 |
// no prioritized tasks are running. Prioritized task's execution stops the |
| 98 |
// execution of all background tasks. Background task must be able to be |
| 99 |
// cancelled via context.Context argument and be able to be restarted again. |
| 100 |
func (ts *BackgroundTaskManager) InvokeBackgroundTask(do func(context.Context), timeout time.Duration) { |
| 101 |
for { |
| 102 |
// Wait until all prioritized tasks are done |
| 103 |
for { |
| 104 |
if atomic.LoadInt64(&ts.prioritizedTasks) <= 0 { |
| 105 |
break |
| 106 |
} |
| 107 |
|
| 108 |
// waits until a prioritized task is done |
| 109 |
ts.prioritizedTaskDoneCond.L.Lock() |
| 110 |
if atomic.LoadInt64(&ts.prioritizedTasks) > 0 { |
| 111 |
ts.prioritizedTaskDoneCond.Wait() |
| 112 |
} |
| 113 |
ts.prioritizedTaskDoneCond.L.Unlock() |
| 114 |
} |
| 115 |
|
| 116 |
// limited number of background tasks can run at once. |
| 117 |
// if prioritized tasks are running, cancel this task. |
| 118 |
if func() bool { |
| 119 |
ts.backgroundSem.Acquire(context.Background(), 1) |
| 120 |
defer ts.backgroundSem.Release(1) |
| 121 |
|
| 122 |
// Get notify the prioritized tasks execution. |
| 123 |
ts.prioritizedTaskStartNotifyMu.Lock() |
| 124 |
ch := ts.prioritizedTaskStartNotify |
| 125 |
tasks := atomic.LoadInt64(&ts.prioritizedTasks) |
| 126 |
ts.prioritizedTaskStartNotifyMu.Unlock() |
| 127 |
if tasks > 0 { |
| 128 |
return false |
| 129 |
} |
| 130 |
|
| 131 |
// Invoke the background task. if some prioritized tasks added during |
| 132 |
// execution, cancel it and try it later. |
| 133 |
var ( |
| 134 |
done = make(chan struct{}) |
| 135 |
ctx, cancel = context.WithTimeout(context.Background(), timeout) |
| 136 |
) |
| 137 |
defer cancel() |
| 138 |
go func() { |
| 139 |
do(ctx) |
| 140 |
close(done) |
| 141 |
}() |
| 142 |
|
| 143 |
// Wait until the background task is done or canceled. |
| 144 |
select { |
| 145 |
case <-ch: // some prioritized tasks started; retry it later |
| 146 |
cancel() |
| 147 |
return false |
| 148 |
case <-done: // All tasks completed |
| 149 |
} |
| 150 |
return true |
| 151 |
}() { |
| 152 |
break |
| 153 |
} |
| 154 |
} |
| 155 |
} |