-
Notifications
You must be signed in to change notification settings - Fork 132
/
util.go
416 lines (384 loc) · 12.6 KB
/
util.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package api
import (
"fmt"
math "math"
"strings"
time "time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
v1 "k8s.io/api/core/v1"
"github.com/armadaproject/armada/internal/armada/configuration"
"github.com/armadaproject/armada/internal/common/armadaerrors"
"github.com/armadaproject/armada/internal/common/logging"
armadaresource "github.com/armadaproject/armada/internal/common/resource"
"github.com/armadaproject/armada/internal/scheduler/schedulerobjects"
)
// IsTerminal returns true if the JobState s corresponds to a state
// that indicates the job has been terminated.
func (s JobState) IsTerminal() bool {
switch s {
case JobState_SUCCEEDED:
return true
case JobState_FAILED:
return true
}
return false
}
func NewNodeFromNodeInfo(nodeInfo *NodeInfo, executor string, allowedPriorities []int32, lastSeen time.Time) (*schedulerobjects.Node, error) {
if executor == "" {
return nil, errors.WithStack(&armadaerrors.ErrInvalidArgument{
Name: "executor",
Value: executor,
Message: "executor is empty",
})
}
if nodeInfo.Name == "" {
return nil, errors.WithStack(&armadaerrors.ErrInvalidArgument{
Name: "nodeInfo.Name",
Value: nodeInfo.Name,
Message: "nodeInfo.Name is empty",
})
}
allocatableByPriorityAndResource := schedulerobjects.NewAllocatableByPriorityAndResourceType(
allowedPriorities,
schedulerobjects.ResourceList{
Resources: nodeInfo.TotalResources,
},
)
for p, rl := range nodeInfo.NonArmadaAllocatedResources {
allocatableByPriorityAndResource.MarkAllocated(p, schedulerobjects.ResourceList{Resources: rl.Resources})
}
nonArmadaAllocatedResources := make(map[int32]schedulerobjects.ResourceList)
for p, rl := range nodeInfo.NonArmadaAllocatedResources {
nonArmadaAllocatedResources[p] = schedulerobjects.ResourceList{Resources: rl.Resources}
}
resourceUsageByQueue := make(map[string]*schedulerobjects.ResourceList)
for queueName, resourceUsage := range nodeInfo.ResourceUsageByQueue {
resourceUsageByQueue[queueName] = &schedulerobjects.ResourceList{Resources: resourceUsage.Resources}
}
jobRunsByState := make(map[string]schedulerobjects.JobRunState)
for jobId, state := range nodeInfo.RunIdsByState {
jobRunsByState[jobId] = JobRunStateFromApiJobState(state)
}
return &schedulerobjects.Node{
Id: fmt.Sprintf("%s-%s", executor, nodeInfo.Name),
Name: nodeInfo.Name,
LastSeen: lastSeen,
Taints: nodeInfo.GetTaints(),
Labels: nodeInfo.GetLabels(),
TotalResources: schedulerobjects.ResourceList{Resources: nodeInfo.TotalResources},
AllocatableByPriorityAndResource: allocatableByPriorityAndResource,
NonArmadaAllocatedResources: nonArmadaAllocatedResources,
StateByJobRunId: jobRunsByState,
Unschedulable: nodeInfo.Unschedulable,
ResourceUsageByQueue: resourceUsageByQueue,
ReportingNodeType: nodeInfo.NodeType,
}, nil
}
func JobRunStateFromApiJobState(s JobState) schedulerobjects.JobRunState {
switch s {
case JobState_QUEUED:
return schedulerobjects.JobRunState_UNKNOWN
case JobState_PENDING:
return schedulerobjects.JobRunState_PENDING
case JobState_RUNNING:
return schedulerobjects.JobRunState_RUNNING
case JobState_SUCCEEDED:
return schedulerobjects.JobRunState_SUCCEEDED
case JobState_FAILED:
return schedulerobjects.JobRunState_FAILED
case JobState_UNKNOWN:
return schedulerobjects.JobRunState_UNKNOWN
}
return schedulerobjects.JobRunState_UNKNOWN
}
func NewNodeTypeFromNodeInfo(nodeInfo *NodeInfo, indexedTaints map[string]interface{}, indexedLabels map[string]interface{}) *schedulerobjects.NodeType {
return schedulerobjects.NewNodeType(nodeInfo.GetTaints(), nodeInfo.GetLabels(), indexedTaints, indexedLabels)
}
func (job *Job) GetRequirements(priorityClasses map[string]configuration.PriorityClass) *schedulerobjects.JobSchedulingInfo {
podSpec := job.GetMainPodSpec()
priority, ok := PriorityFromPodSpec(podSpec, priorityClasses)
if priorityClasses != nil && !ok {
// Ignore this error if priorityByPriorityClassName is explicitly set to nil.
// We assume that in this case the caller is sure the priority does not need to be set.
err := errors.Errorf("unknown priorityClassName %s", podSpec.PriorityClassName)
logging.WithStacktrace(logrus.NewEntry(logrus.New()), err).Error("failed to get priority from priorityClassName")
}
preemptionPolicy := string(v1.PreemptLowerPriority)
if podSpec.PreemptionPolicy != nil {
preemptionPolicy = string(*podSpec.PreemptionPolicy)
}
podRequirements := &schedulerobjects.PodRequirements{
NodeSelector: podSpec.NodeSelector,
Affinity: podSpec.Affinity,
Tolerations: podSpec.Tolerations,
Annotations: maps.Clone(job.Annotations),
Priority: priority,
PreemptionPolicy: preemptionPolicy,
ResourceRequirements: job.GetResourceRequirements(),
}
return &schedulerobjects.JobSchedulingInfo{
PriorityClassName: podSpec.PriorityClassName,
Priority: LogSubmitPriorityFromApiPriority(job.GetPriority()),
SubmitTime: job.GetCreated(),
ObjectRequirements: []*schedulerobjects.ObjectRequirements{
{
Requirements: &schedulerobjects.ObjectRequirements_PodRequirements{
PodRequirements: podRequirements,
},
},
},
}
}
// SchedulingResourceRequirementsFromPodSpec returns resource requests and limits necessary for scheduling a pod.
// The requests and limits are set to:
//
// max(
//
// sum across all containers,
// max over all init containers,
//
// )
//
// This is because containers run in parallel, whereas initContainers run serially.
func SchedulingResourceRequirementsFromPodSpec(podSpec *v1.PodSpec) v1.ResourceRequirements {
rv := v1.ResourceRequirements{
Requests: make(v1.ResourceList),
Limits: make(v1.ResourceList),
}
for _, c := range podSpec.Containers {
for t, request := range c.Resources.Requests {
q := rv.Requests[t]
q.Add(request)
rv.Requests[t] = q
}
for t, limit := range c.Resources.Limits {
q := rv.Limits[t]
q.Add(limit)
rv.Limits[t] = q
}
}
for _, c := range podSpec.InitContainers {
for t, request := range c.Resources.Requests {
if request.Cmp(rv.Requests[t]) == 1 {
rv.Requests[t] = request
}
}
for t, limit := range c.Resources.Limits {
if limit.Cmp(rv.Limits[t]) == 1 {
rv.Limits[t] = limit
}
}
}
return rv
}
// PriorityFromPodSpec returns the priority in a pod spec.
// If priority is set directly, that value is returned.
// Otherwise, it returns the value of the key podSpec.
// In both cases the value along with true boolean is returned.
// PriorityClassName in priorityByPriorityClassName map.
// If no priority is set for the pod spec, 0 along with a false boolean would be returned
func PriorityFromPodSpec(podSpec *v1.PodSpec, priorityClasses map[string]configuration.PriorityClass) (int32, bool) {
// If there's no podspec there's nothing we can do
if podSpec == nil {
return 0, false
}
// If a priority is directly specified, use that
if podSpec.Priority != nil {
return *podSpec.Priority, true
}
// If we find a priority class use that
priorityClass, ok := priorityClasses[podSpec.PriorityClassName]
if ok {
return priorityClass.Priority, true
}
// Couldn't find anything
return 0, false
}
func (job *Job) GetPriorityClassName() string {
podSpec := job.GetMainPodSpec()
return podSpec.PriorityClassName
}
func (job *Job) GetNodeSelector() map[string]string {
podSpec := job.GetMainPodSpec()
return podSpec.NodeSelector
}
func (job *Job) GetAffinity() *v1.Affinity {
podSpec := job.GetMainPodSpec()
return podSpec.Affinity
}
func (job *Job) GetTolerations() []v1.Toleration {
podSpec := job.GetMainPodSpec()
return podSpec.Tolerations
}
func (job *Job) GetResourceRequirements() v1.ResourceRequirements {
// Use pre-computed schedulingResourceRequirements if available.
// Otherwise compute it from the containers in podSpec.
podSpec := job.GetMainPodSpec()
if len(job.SchedulingResourceRequirements.Requests) > 0 || len(job.SchedulingResourceRequirements.Limits) > 0 {
return job.SchedulingResourceRequirements
} else {
return SchedulingResourceRequirementsFromPodSpec(podSpec)
}
}
func (job *Job) GetJobSet() string {
return job.JobSetId
}
// LogSubmitPriorityFromApiPriority returns the uint32 representation of the priority included with a submitted job,
// or an error if the conversion fails.
func LogSubmitPriorityFromApiPriority(priority float64) uint32 {
if priority < 0 {
priority = 0
}
if priority > math.MaxUint32 {
priority = math.MaxUint32
}
priority = math.Round(priority)
return uint32(priority)
}
func (job *Job) GetMainPodSpec() *v1.PodSpec {
if job.PodSpec != nil {
return job.PodSpec
}
for _, podSpec := range job.PodSpecs {
if podSpec != nil {
return podSpec
}
}
return nil
}
func (job *JobSubmitRequestItem) GetMainPodSpec() *v1.PodSpec {
if job.PodSpec != nil {
return job.PodSpec
} else if len(job.PodSpecs) > 0 {
return job.PodSpecs[0]
}
return nil
}
func (job *Job) TotalResourceRequest() armadaresource.ComputeResources {
podSpec := job.GetMainPodSpec()
return armadaresource.TotalPodResourceRequest(podSpec)
}
func ShortStringFromEventMessages(msgs []*EventMessage) string {
var sb strings.Builder
sb.WriteString("[")
for i, msg := range msgs {
sb.WriteString(msg.ShortString())
if i < len(msgs)-1 {
sb.WriteString(", ")
}
}
sb.WriteString("]")
return sb.String()
}
func (msg *EventMessage) ShortString() string {
return strings.ReplaceAll(fmt.Sprintf("%T", msg.Events), "*api.EventMessage_", "")
}
func (testSpec *TestSpec) ShortString() string {
var sb strings.Builder
sb.WriteString(
fmt.Sprintf(
"%s: {queue: %s, job set: %s, timeout: %s, expected: [",
testSpec.Name, testSpec.Queue, testSpec.JobSetId, testSpec.Timeout.String(),
),
)
for i, e := range testSpec.GetExpectedEvents() {
sb.WriteString(e.ShortString())
if i < len(testSpec.GetExpectedEvents())-1 {
sb.WriteString(", ")
}
}
sb.WriteString("]}")
return sb.String()
}
func JobIdFromApiEvent(msg *EventMessage) string {
switch e := msg.Events.(type) {
case *EventMessage_Submitted:
return e.Submitted.JobId
case *EventMessage_Queued:
return e.Queued.JobId
case *EventMessage_DuplicateFound:
return e.DuplicateFound.JobId
case *EventMessage_Leased:
return e.Leased.JobId
case *EventMessage_LeaseReturned:
return e.LeaseReturned.JobId
case *EventMessage_LeaseExpired:
return e.LeaseExpired.JobId
case *EventMessage_Pending:
return e.Pending.JobId
case *EventMessage_Running:
return e.Running.JobId
case *EventMessage_UnableToSchedule:
return e.UnableToSchedule.JobId
case *EventMessage_Failed:
return e.Failed.JobId
case *EventMessage_Succeeded:
return e.Succeeded.JobId
case *EventMessage_Reprioritized:
return e.Reprioritized.JobId
case *EventMessage_Cancelling:
return e.Cancelling.JobId
case *EventMessage_Cancelled:
return e.Cancelled.JobId
case *EventMessage_Terminated:
return e.Terminated.JobId
case *EventMessage_Utilisation:
return e.Utilisation.JobId
case *EventMessage_IngressInfo:
return e.IngressInfo.JobId
case *EventMessage_Reprioritizing:
return e.Reprioritizing.JobId
case *EventMessage_Updated:
return e.Updated.JobId
case *EventMessage_Preempted:
return e.Preempted.JobId
}
return ""
}
func JobSetIdFromApiEvent(msg *EventMessage) string {
switch e := msg.Events.(type) {
case *EventMessage_Submitted:
return e.Submitted.JobSetId
case *EventMessage_Queued:
return e.Queued.JobSetId
case *EventMessage_DuplicateFound:
return e.DuplicateFound.JobSetId
case *EventMessage_Leased:
return e.Leased.JobSetId
case *EventMessage_LeaseReturned:
return e.LeaseReturned.JobSetId
case *EventMessage_LeaseExpired:
return e.LeaseExpired.JobSetId
case *EventMessage_Pending:
return e.Pending.JobSetId
case *EventMessage_Running:
return e.Running.JobSetId
case *EventMessage_UnableToSchedule:
return e.UnableToSchedule.JobSetId
case *EventMessage_Failed:
return e.Failed.JobSetId
case *EventMessage_Succeeded:
return e.Succeeded.JobSetId
case *EventMessage_Reprioritized:
return e.Reprioritized.JobSetId
case *EventMessage_Cancelling:
return e.Cancelling.JobSetId
case *EventMessage_Cancelled:
return e.Cancelled.JobSetId
case *EventMessage_Terminated:
return e.Terminated.JobSetId
case *EventMessage_Utilisation:
return e.Utilisation.JobSetId
case *EventMessage_IngressInfo:
return e.IngressInfo.JobSetId
case *EventMessage_Reprioritizing:
return e.Reprioritizing.JobSetId
case *EventMessage_Updated:
return e.Updated.JobSetId
case *EventMessage_Preempted:
return e.Preempted.JobSetId
}
return ""
}