generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 215
/
scheduler.go
372 lines (340 loc) · 12 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
corev1helpers "k8s.io/component-helpers/scheduling/corev1"
"k8s.io/component-helpers/scheduling/corev1/nodeaffinity"
"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
recorder record.EventRecorder
}
func New(queues *queue.Manager, cache *capacity.Cache, cl client.Client, recorder record.EventRecorder) *Scheduler {
return &Scheduler{
queues: queues,
capacityCache: cache,
client: cl,
recorder: recorder,
}
}
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 workloads 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
}
c := snapshot.Capacities[e.Capacity]
if len(e.borrows) > 0 && c.Cohort != nil && usedCohorts.Has(c.Cohort.Name) {
continue
}
usedCapacity.Insert(e.Capacity)
log := log.WithValues("queuedWorkload", klog.KObj(e.Obj), "capacity", e.Capacity)
if err := s.assign(ctrl.LoggerInto(ctx, log), &e); err != nil {
log.Error(err, "Failed assigning workload to capacity")
} else {
assignedWorkloads.Insert(workload.Key(e.Obj))
}
// Even if there was a failure, we shouldn't assign other workloads to this
// cohort.
if c.Cohort != nil {
usedCohorts.Insert(c.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", "queuedWorkload", 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 resources 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)
c := snap.Capacities[w.Capacity]
if c == nil {
log.V(3).Info("Capacity not found when calculating workload assignments")
continue
}
e := entry{Info: w}
if !e.assignFlavors(log, c) {
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(log logr.Logger, cap *capacity.Capacity) bool {
flavoredRequests := make([]workload.PodSetResources, 0, len(e.TotalRequests))
wUsed := make(capacity.Resources)
wBorrows := make(capacity.Resources)
for i, podSet := range e.TotalRequests {
flavors := make(map[corev1.ResourceName]string, len(podSet.Requests))
for resName, reqVal := range podSet.Requests {
rFlavor, borrow := findFlavorForResource(log, resName, reqVal, wUsed[resName], cap, &e.Obj.Spec.Pods[i].Spec)
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 = append(flavoredRequests, workload.PodSetResources{
Name: podSet.Name,
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) error {
log := ctrl.LoggerFrom(ctx)
newWorkload := e.Obj.DeepCopy()
for i := range newWorkload.Spec.Pods {
podSet := &newWorkload.Spec.Pods[i]
podSet.AssignedFlavors = e.TotalRequests[i].Flavors
}
newWorkload.Spec.AssignedCapacity = kueue.CapacityReference(e.Capacity)
if err := s.capacityCache.AssumeWorkload(newWorkload); err != nil {
return err
}
log.V(2).Info("Workload assumed in the cache")
go func() {
err := s.client.Update(ctx, newWorkload)
if err == nil {
s.recorder.Eventf(newWorkload, corev1.EventTypeNormal, "Assigned", "Assigned to capacity %v", newWorkload.Spec.AssignedCapacity)
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)
}()
return nil
}
// 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(
log logr.Logger,
name corev1.ResourceName,
val int64,
wUsed map[string]int64,
cap *capacity.Capacity,
spec *corev1.PodSpec) (string, int64) {
// We will only check against the flavors' labels for the resource.
selector := flavorSelector(spec, cap.LabelKeys[name])
for _, flavor := range cap.RequestableResources[name] {
_, untolerated := corev1helpers.FindMatchingUntoleratedTaint(flavor.Taints, spec.Tolerations, func(t *corev1.Taint) bool {
return t.Effect == corev1.TaintEffectNoSchedule || t.Effect == corev1.TaintEffectNoExecute
})
if untolerated {
continue
}
if match, err := selector.Match(&corev1.Node{ObjectMeta: metav1.ObjectMeta{Labels: flavor.Labels}}); !match || err != nil {
if err != nil {
log.Error(err, "Matching workload affinity against flavor; no flavor assigned")
return "", 0
}
continue
}
// Consider the usage assigned to previous pod sets.
ok, borrow := fitsFlavorLimits(name, val+wUsed[flavor.Name], cap, &flavor)
if ok {
return flavor.Name, borrow
}
}
return "", 0
}
func flavorSelector(spec *corev1.PodSpec, allowedKeys sets.String) nodeaffinity.RequiredNodeAffinity {
// This function generally replicates the implementation of kube-scheduler's NodeAffintiy
// Filter plugin as of v1.24.
var specCopy corev1.PodSpec
// Remove affinity constraints with irrelevant keys.
if len(spec.NodeSelector) != 0 {
specCopy.NodeSelector = map[string]string{}
for k, v := range spec.NodeSelector {
if allowedKeys.Has(k) {
specCopy.NodeSelector[k] = v
}
}
}
affinity := spec.Affinity
if affinity != nil && affinity.NodeAffinity != nil && affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil {
var termsCopy []corev1.NodeSelectorTerm
for _, t := range affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms {
var expCopy []corev1.NodeSelectorRequirement
for _, e := range t.MatchExpressions {
if allowedKeys.Has(e.Key) {
expCopy = append(expCopy, e)
}
}
// If a term becomes empty, it means node affinity matches any flavor since those terms are ORed,
// and so matching gets reduced to spec.NodeSelector
if len(expCopy) == 0 {
termsCopy = nil
break
}
termsCopy = append(termsCopy, corev1.NodeSelectorTerm{MatchExpressions: expCopy})
}
if len(termsCopy) != 0 {
specCopy.Affinity = &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: termsCopy,
},
},
}
}
}
return nodeaffinity.GetRequiredNodeAffinity(&corev1.Pod{Spec: specCopy})
}
// fitsFlavorLimits returns whether a requested resource fits in a specific flavor's quota limits.
// If it fits, also returns any borrowing required.
func fitsFlavorLimits(name corev1.ResourceName, val int64, cap *capacity.Capacity, flavor *capacity.FlavorInfo) (bool, int64) {
used := cap.UsedResources[name][flavor.Name]
if used+val > flavor.Ceiling {
// Past borrowing limit.
return false, 0
}
cohortUsed := used
cohortTotal := flavor.Guaranteed
if cap.Cohort != nil {
cohortUsed = cap.Cohort.UsedResources[name][flavor.Name]
cohortTotal = cap.Cohort.RequestableResources[name][flavor.Name]
}
borrow := used + val - flavor.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)
}