generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 215
/
scheduler.go
288 lines (261 loc) · 9.1 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright 2022 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 scheduler
import (
"context"
"sort"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
kueue "sigs.k8s.io/kueue/api/v1alpha1"
"sigs.k8s.io/kueue/pkg/capacity"
"sigs.k8s.io/kueue/pkg/queue"
"sigs.k8s.io/kueue/pkg/workload"
)
type Scheduler struct {
queues *queue.Manager
capacityCache *capacity.Cache
client client.Client
}
func New(queues *queue.Manager, cache *capacity.Cache, cl client.Client) *Scheduler {
return &Scheduler{
queues: queues,
capacityCache: cache,
client: cl,
}
}
func (s *Scheduler) Start(ctx context.Context) {
log := ctrl.LoggerFrom(ctx).WithName("scheduler")
ctx = ctrl.LoggerInto(ctx, log)
wait.UntilWithContext(ctx, s.schedule, 0)
}
func (s *Scheduler) schedule(ctx context.Context) {
log, err := logr.FromContext(ctx)
if err != nil {
return
}
// 1. Get the heads from the queues, including their desired capacity.
// This operation blocks while the queues are empty.
headWorkloads := s.queues.Heads(ctx)
// No elements means the program is finishing.
if len(headWorkloads) == 0 {
return
}
// 2. Take a snapshot of the capacities and their usage.
snapshot := s.capacityCache.Snapshot()
// 3. Calculate requirements for assigning workloads to capacities
// (resource flavors, borrowing).
entries := calculateRequirementsForAssignments(log, headWorkloads, snapshot)
// 4. Sort entries based on borrowing and timestamps.
sort.Sort(entryOrdering(entries))
// 5. Assign to capacities, ensuring that no more than one workload gets
// assigned to a capacity or a cohort (if borrowing).
// This is because there can be other worloads deeper in a queue whose head
// got assigned that should be scheduled before the heads of other queues.
usedCapacity := sets.NewString()
usedCohorts := sets.NewString()
assignedWorkloads := sets.NewString()
for _, e := range entries {
if usedCapacity.Has(e.Capacity) {
continue
}
cap := snapshot.Capacities[e.Capacity]
if len(e.borrows) > 0 && cap.Cohort != nil && usedCohorts.Has(cap.Cohort.Name) {
continue
}
usedCapacity.Insert(e.Capacity)
s.assign(ctx, &e)
assignedWorkloads.Insert(workload.Key(e.Obj))
if cap.Cohort != nil {
usedCohorts.Insert(cap.Cohort.Name)
}
}
// 6. Requeue the heads that were not scheduled.
for _, w := range headWorkloads {
if assignedWorkloads.Has(workload.Key(w.Obj)) {
continue
}
if s.queues.RequeueWorkload(ctx, &w) {
log.V(2).Info("Workload requeued", "workload", klog.KObj(w.Obj), "queue", klog.KRef(w.Obj.Namespace, w.Obj.Spec.QueueName))
}
}
}
// entry holds requirements for a workload to be scheduled in a capacity.
type entry struct {
// workload.Info holds the workload from the API as well as resource usage
// and flavors assigned.
workload.Info
// borrows is the resouces that the workload would need to borrow from the
// cohort if it was scheduled in the capacity.
borrows capacity.Resources
}
// calculateRequirementsForAssignments returns the workloads with their
// requirements (resource flavors, borrowing) if they were assigned to the
// capacities in the snapshot.
func calculateRequirementsForAssignments(log logr.Logger, workloads []workload.Info, snap capacity.Snapshot) []entry {
entries := make([]entry, 0, len(workloads))
for _, w := range workloads {
log := log.WithValues("queuedWorkload", klog.KObj(w.Obj), "capacity", w.Capacity)
cap := snap.Capacities[w.Capacity]
if cap == nil {
log.V(3).Info("Capacity not found when calculating workload assingments")
continue
}
e := entry{Info: w}
if !e.assignFlavors(cap) {
log.V(2).Info("Workload didn't fit in remaining capacity even when borrowing")
continue
}
entries = append(entries, e)
}
return entries
}
// assignFlavors calculates the flavors that should be assigned to this entry
// if scheduled to this capacity, including details of how much it needs to
// borrow from the cohort.
// It returns whether the entry would fit. If it doesn't fit, the object is
// unmodified.
func (e *entry) assignFlavors(cap *capacity.Capacity) bool {
flavoredRequests := make(map[string]workload.Resources, len(e.TotalRequests))
wUsed := make(capacity.Resources)
wBorrows := make(capacity.Resources)
for psName, podSet := range e.TotalRequests {
flavors := make(map[corev1.ResourceName]string, len(podSet.Requests))
for resName, reqVal := range podSet.Requests {
rFlavor, borrow := findFlavorForResource(resName, reqVal, cap, wUsed[resName])
if rFlavor == "" {
return false
}
if borrow > 0 {
if wBorrows[resName] == nil {
wBorrows[resName] = make(map[string]int64)
}
// Don't accumulate borrowing. The returned `borrow` already considers
// usage from previous pod sets.
wBorrows[resName][rFlavor] = borrow
}
if wUsed[resName] == nil {
wUsed[resName] = make(map[string]int64)
}
wUsed[resName][rFlavor] += reqVal
flavors[resName] = rFlavor
}
flavoredRequests[psName] = workload.Resources{
Requests: podSet.Requests,
Flavors: flavors,
}
}
e.TotalRequests = flavoredRequests
if len(wBorrows) > 0 {
e.borrows = wBorrows
}
return true
}
// assign sets the assigned capacity and flavors into the workload of
// the entry, and asynchronously updates the object in the apiserver after
// assuming it in the cache.
func (s *Scheduler) assign(ctx context.Context, e *entry) {
log := ctrl.LoggerFrom(ctx).WithValues("queuedWorkload", klog.KObj(e.Obj), "capacity", e.Capacity)
newWorkload := e.Obj.DeepCopy()
for i := range newWorkload.Spec.Pods {
podSet := &newWorkload.Spec.Pods[i]
podSet.AssignedFlavors = e.TotalRequests[podSet.Name].Flavors
}
newWorkload.Spec.AssignedCapacity = kueue.CapacityReference(e.Capacity)
s.capacityCache.AssumeWorkload(newWorkload)
log.V(2).Info("Workload assumed in the cache")
go func() {
err := s.client.Update(ctx, newWorkload)
if err == nil {
log.V(2).Info("Successfully assigned capacity and resource flavors to workload")
return
}
// Ignore errors because the workload or capacity could have been deleted
// by an event.
_ = s.capacityCache.ForgetWorkload(newWorkload)
if errors.IsNotFound(err) {
log.V(2).Info("Workload not assigned because it was deleted")
return
}
log.Error(err, "Assigning capacity and resource flavors to workload")
log.V(2).Info("Requeueing")
s.queues.RequeueWorkload(ctx, &e.Info)
}()
}
// findFlavorForResources returns a flavor which can satisfy the resource request,
// given that wUsed is the usage of flavors by previous podsets.
// If it finds a flavor, also returns any borrowing required.
func findFlavorForResource(name corev1.ResourceName, val int64, cap *capacity.Capacity, wUsed map[string]int64) (string, int64) {
for _, flavor := range cap.RequestableResources[name] {
// Consider the usage assigned to previous pod sets.
ok, borrow := canAssignFlavor(name, val+wUsed[flavor.Name], cap, &flavor)
if ok {
return flavor.Name, borrow
}
}
return "", 0
}
// canAssignFlavor returns whether a requested resource fits in a specific flavor.
// If it fits, also returns any borrowing required.
func canAssignFlavor(name corev1.ResourceName, val int64, cap *capacity.Capacity, rFlavor *kueue.ResourceFlavor) (bool, int64) {
ceiling := workload.ResourceValue(name, rFlavor.Quota.Ceiling)
used := cap.UsedResources[name][rFlavor.Name]
if used+val > ceiling {
// Past borrowing limit.
return false, 0
}
guaranteed := workload.ResourceValue(name, rFlavor.Quota.Guaranteed)
cohortUsed := used
cohortTotal := guaranteed
if cap.Cohort != nil {
cohortUsed = cap.Cohort.UsedResources[name][rFlavor.Name]
cohortTotal = cap.Cohort.RequestableResources[name][rFlavor.Name]
}
borrow := used + val - guaranteed
if borrow < 0 {
borrow = 0
}
if cohortUsed+val > cohortTotal {
// Doesn't fit even with borrowing.
// TODO(PostMVP): preemption could help if borrow == 0
return false, 0
}
return true, borrow
}
type entryOrdering []entry
func (e entryOrdering) Len() int {
return len(e)
}
func (e entryOrdering) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
// Less is the ordering criteria:
// 1. guaranteed before borrowing.
// 2. FIFO on creation timestamp.
func (e entryOrdering) Less(i, j int) bool {
a := e[i]
b := e[j]
// 1. Prefer guaranteed (not borrowing)
aGuaranteed := len(a.borrows) == 0
bGuaranteed := len(b.borrows) == 0
if aGuaranteed != bGuaranteed {
return aGuaranteed
}
// 2. FIFO
return a.Obj.CreationTimestamp.Before(&b.Obj.CreationTimestamp)
}