-
Notifications
You must be signed in to change notification settings - Fork 963
/
job_info.go
865 lines (730 loc) · 23.1 KB
/
job_info.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/*
Copyright 2017 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 api
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
"volcano.sh/apis/pkg/apis/scheduling"
"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
volumescheduling "volcano.sh/volcano/pkg/scheduler/capabilities/volumebinding"
)
// DisruptionBudget define job min pod available and max pod unvailable value
type DisruptionBudget struct {
MinAvailable string
MaxUnavilable string
}
// NewDisruptionBudget create disruption budget for job
func NewDisruptionBudget(minAvailable, maxUnavilable string) *DisruptionBudget {
disruptionBudget := &DisruptionBudget{
MinAvailable: minAvailable,
MaxUnavilable: maxUnavilable,
}
return disruptionBudget
}
// Clone return a clone of DisruptionBudget
func (db *DisruptionBudget) Clone() *DisruptionBudget {
return &DisruptionBudget{
MinAvailable: db.MinAvailable,
MaxUnavilable: db.MaxUnavilable,
}
}
// JobWaitingTime is maximum waiting time that a job could stay Pending in service level agreement
// when job waits longer than waiting time, it should enqueue at once, and cluster should reserve resources for it
const JobWaitingTime = "sla-waiting-time"
// TaskID is UID type for Task
type TaskID types.UID
// TransactionContext holds all the fields that needed by scheduling transaction
type TransactionContext struct {
NodeName string
Status TaskStatus
}
// Clone returns a clone of TransactionContext
func (ctx *TransactionContext) Clone() *TransactionContext {
if ctx == nil {
return nil
}
clone := *ctx
return &clone
}
type TopologyInfo struct {
Policy string
ResMap map[int]v1.ResourceList // key: numa ID
}
func (info *TopologyInfo) Clone() *TopologyInfo {
copyInfo := &TopologyInfo{
Policy: info.Policy,
ResMap: make(map[int]v1.ResourceList),
}
for numaID, resList := range info.ResMap {
copyInfo.ResMap[numaID] = resList.DeepCopy()
}
return copyInfo
}
// TaskInfo will have all infos about the task
type TaskInfo struct {
UID TaskID
Job JobID
Name string
Namespace string
// Resreq is the resource that used when task running.
Resreq *Resource
// InitResreq is the resource that used to launch a task.
InitResreq *Resource
TransactionContext
// LastTransaction holds the context of last scheduling transaction
LastTransaction *TransactionContext
Priority int32
VolumeReady bool
Preemptable bool
BestEffort bool
// RevocableZone supports setting volcano.sh/revocable-zone annotation or label for pod/podgroup
// we only support empty value or * value for this version and we will support specify revocable zone name for future releases
// empty value means workload can not use revocable node
// * value means workload can use all the revocable node for during node active revocable time.
RevocableZone string
NumaInfo *TopologyInfo
PodVolumes *volumescheduling.PodVolumes
Pod *v1.Pod
}
func getJobID(pod *v1.Pod) JobID {
if gn, found := pod.Annotations[v1beta1.KubeGroupNameAnnotationKey]; found && len(gn) != 0 {
// Make sure Pod and PodGroup belong to the same namespace.
jobID := fmt.Sprintf("%s/%s", pod.Namespace, gn)
return JobID(jobID)
}
return ""
}
func getTaskID(pod *v1.Pod) TaskID {
if ts, found := pod.Annotations[batch.TaskSpecKey]; found && len(ts) != 0 {
return TaskID(ts)
}
return ""
}
const TaskPriorityAnnotation = "volcano.sh/task-priority"
// NewTaskInfo creates new taskInfo object for a Pod
func NewTaskInfo(pod *v1.Pod) *TaskInfo {
initResReq := GetPodResourceRequest(pod)
resReq := initResReq
bestEffort := initResReq.IsEmpty()
preemptable := GetPodPreemptable(pod)
revocableZone := GetPodRevocableZone(pod)
topologyInfo := GetPodTopologyInfo(pod)
jobID := getJobID(pod)
ti := &TaskInfo{
UID: TaskID(pod.UID),
Job: jobID,
Name: pod.Name,
Namespace: pod.Namespace,
Priority: 1,
Pod: pod,
Resreq: resReq,
InitResreq: initResReq,
Preemptable: preemptable,
BestEffort: bestEffort,
RevocableZone: revocableZone,
NumaInfo: topologyInfo,
TransactionContext: TransactionContext{
NodeName: pod.Spec.NodeName,
Status: getTaskStatus(pod),
},
}
if pod.Spec.Priority != nil {
ti.Priority = *pod.Spec.Priority
}
if taskPriority, ok := pod.Annotations[TaskPriorityAnnotation]; ok {
if priority, err := strconv.ParseInt(taskPriority, 10, 32); err == nil {
ti.Priority = int32(priority)
}
}
return ti
}
// GetTransactionContext get transaction context of a task
func (ti *TaskInfo) GetTransactionContext() TransactionContext {
return ti.TransactionContext
}
// GenerateLastTxContext generate and set context of last transaction for a task
func (ti *TaskInfo) GenerateLastTxContext() {
ctx := ti.GetTransactionContext()
ti.LastTransaction = &ctx
}
// ClearLastTxContext clear context of last transaction for a task
func (ti *TaskInfo) ClearLastTxContext() {
ti.LastTransaction = nil
}
func (ti *TaskInfo) SetPodResourceDecision() error {
if ti.NumaInfo == nil || len(ti.NumaInfo.ResMap) == 0 {
return nil
}
klog.V(4).Infof("%v/%v resource decision: %v", ti.Namespace, ti.Name, ti.NumaInfo.ResMap)
decision := PodResourceDecision{
NUMAResources: ti.NumaInfo.ResMap,
}
layout, err := json.Marshal(&decision)
if err != nil {
return err
}
metav1.SetMetaDataAnnotation(&ti.Pod.ObjectMeta, topologyDecisionAnnotation, string(layout[:]))
return nil
}
func (ti *TaskInfo) UnsetPodResourceDecision() {
delete(ti.Pod.Annotations, topologyDecisionAnnotation)
}
// Clone is used for cloning a task
func (ti *TaskInfo) Clone() *TaskInfo {
return &TaskInfo{
UID: ti.UID,
Job: ti.Job,
Name: ti.Name,
Namespace: ti.Namespace,
Priority: ti.Priority,
PodVolumes: ti.PodVolumes,
Pod: ti.Pod,
Resreq: ti.Resreq.Clone(),
InitResreq: ti.InitResreq.Clone(),
VolumeReady: ti.VolumeReady,
Preemptable: ti.Preemptable,
BestEffort: ti.BestEffort,
RevocableZone: ti.RevocableZone,
NumaInfo: ti.NumaInfo.Clone(),
TransactionContext: TransactionContext{
NodeName: ti.NodeName,
Status: ti.Status,
},
LastTransaction: ti.LastTransaction.Clone(),
}
}
func (ti *TaskInfo) GetTaskSpecKey() TaskID {
if ti.Pod == nil {
return ""
}
return getTaskID(ti.Pod)
}
// String returns the taskInfo details in a string
func (ti TaskInfo) String() string {
res := fmt.Sprintf("Task (%v:%v/%v): job %v, status %v, pri %v, "+
"resreq %v, preemptable %v, revocableZone %v",
ti.UID, ti.Namespace, ti.Name, ti.Job, ti.Status, ti.Priority,
ti.Resreq, ti.Preemptable, ti.RevocableZone)
if ti.NumaInfo != nil {
res += fmt.Sprintf(", numaInfo %v", *ti.NumaInfo)
}
return res
}
// JobID is the type of JobInfo's ID.
type JobID types.UID
type tasksMap map[TaskID]*TaskInfo
// NodeResourceMap stores resource in a node
type NodeResourceMap map[string]*Resource
// JobInfo will have all info of a Job
type JobInfo struct {
UID JobID
Name string
Namespace string
Queue QueueID
Priority int32
MinAvailable int32
WaitingTime *time.Duration
JobFitErrors string
NodesFitErrors map[TaskID]*FitErrors
// All tasks of the Job.
TaskStatusIndex map[TaskStatus]tasksMap
Tasks tasksMap
TaskMinAvailable map[TaskID]int32
TaskMinAvailableTotal int32
Allocated *Resource
TotalRequest *Resource
CreationTimestamp metav1.Time
PodGroup *PodGroup
ScheduleStartTimestamp metav1.Time
Preemptable bool
// RevocableZone support set volcano.sh/revocable-zone annotaion or label for pod/podgroup
// we only support empty value or * value for this version and we will support specify revocable zone name for futrue release
// empty value means workload can not use revocable node
// * value means workload can use all the revocable node for during node active revocable time.
RevocableZone string
Budget *DisruptionBudget
}
// NewJobInfo creates a new jobInfo for set of tasks
func NewJobInfo(uid JobID, tasks ...*TaskInfo) *JobInfo {
job := &JobInfo{
UID: uid,
MinAvailable: 0,
NodesFitErrors: make(map[TaskID]*FitErrors),
Allocated: EmptyResource(),
TotalRequest: EmptyResource(),
TaskStatusIndex: map[TaskStatus]tasksMap{},
Tasks: tasksMap{},
TaskMinAvailable: map[TaskID]int32{},
}
for _, task := range tasks {
job.AddTaskInfo(task)
}
return job
}
// UnsetPodGroup removes podGroup details from a job
func (ji *JobInfo) UnsetPodGroup() {
ji.PodGroup = nil
}
// SetPodGroup sets podGroup details to a job
func (ji *JobInfo) SetPodGroup(pg *PodGroup) {
ji.Name = pg.Name
ji.Namespace = pg.Namespace
ji.MinAvailable = pg.Spec.MinMember
ji.Queue = QueueID(pg.Spec.Queue)
ji.CreationTimestamp = pg.GetCreationTimestamp()
var err error
ji.WaitingTime, err = ji.extractWaitingTime(pg, v1beta1.JobWaitingTime)
if err != nil {
klog.Warningf("Error occurs in parsing waiting time for job <%s/%s>, err: %s.",
pg.Namespace, pg.Name, err.Error())
ji.WaitingTime = nil
}
if ji.WaitingTime == nil {
ji.WaitingTime, err = ji.extractWaitingTime(pg, JobWaitingTime)
if err != nil {
klog.Warningf("Error occurs in parsing waiting time for job <%s/%s>, err: %s.",
pg.Namespace, pg.Name, err.Error())
ji.WaitingTime = nil
}
}
ji.Preemptable = ji.extractPreemptable(pg)
ji.RevocableZone = ji.extractRevocableZone(pg)
ji.Budget = ji.extractBudget(pg)
taskMinAvailableTotal := int32(0)
for task, member := range pg.Spec.MinTaskMember {
ji.TaskMinAvailable[TaskID(task)] = member
taskMinAvailableTotal += member
}
ji.TaskMinAvailableTotal = taskMinAvailableTotal
ji.PodGroup = pg
}
// extractWaitingTime reads sla waiting time for job from podgroup annotations
// TODO: should also read from given field in volcano job spec
func (ji *JobInfo) extractWaitingTime(pg *PodGroup, waitingTimeKey string) (*time.Duration, error) {
if _, exist := pg.Annotations[waitingTimeKey]; !exist {
return nil, nil
}
jobWaitingTime, err := time.ParseDuration(pg.Annotations[waitingTimeKey])
if err != nil {
return nil, err
}
if jobWaitingTime <= 0 {
return nil, errors.New("invalid sla waiting time")
}
return &jobWaitingTime, nil
}
// extractPreemptable return volcano.sh/preemptable value for job
func (ji *JobInfo) extractPreemptable(pg *PodGroup) bool {
// check annotaion first
if len(pg.Annotations) > 0 {
if value, found := pg.Annotations[v1beta1.PodPreemptable]; found {
b, err := strconv.ParseBool(value)
if err != nil {
klog.Warningf("invalid %s=%s", v1beta1.PodPreemptable, value)
return false
}
return b
}
}
// it annotation does not exit, check label
if len(pg.Labels) > 0 {
if value, found := pg.Labels[v1beta1.PodPreemptable]; found {
b, err := strconv.ParseBool(value)
if err != nil {
klog.Warningf("invalid %s=%s", v1beta1.PodPreemptable, value)
return false
}
return b
}
}
return false
}
// extractRevocableZone return volcano.sh/revocable-zone value for pod/podgroup
func (ji *JobInfo) extractRevocableZone(pg *PodGroup) string {
// check annotation first
if len(pg.Annotations) > 0 {
if value, found := pg.Annotations[v1beta1.RevocableZone]; found {
if value != "*" {
return ""
}
return value
}
if value, found := pg.Annotations[v1beta1.PodPreemptable]; found {
if b, err := strconv.ParseBool(value); err == nil && b {
return "*"
}
}
}
return ""
}
// extractBudget return budget value for job
func (ji *JobInfo) extractBudget(pg *PodGroup) *DisruptionBudget {
if len(pg.Annotations) > 0 {
if value, found := pg.Annotations[v1beta1.JDBMinAvailable]; found {
return NewDisruptionBudget(value, "")
} else if value, found := pg.Annotations[v1beta1.JDBMaxUnavailable]; found {
return NewDisruptionBudget("", value)
}
}
return NewDisruptionBudget("", "")
}
// GetMinResources return the min resources of podgroup.
func (ji *JobInfo) GetMinResources() *Resource {
if ji.PodGroup.Spec.MinResources == nil {
return EmptyResource()
}
return NewResource(*ji.PodGroup.Spec.MinResources)
}
func (ji *JobInfo) GetElasticResources() *Resource {
if ji.Allocated.LessEqualPartly(ji.GetMinResources(), Zero) {
return EmptyResource()
}
return ji.Allocated.Clone().Sub(ji.GetMinResources())
}
func (ji *JobInfo) addTaskIndex(ti *TaskInfo) {
if _, found := ji.TaskStatusIndex[ti.Status]; !found {
ji.TaskStatusIndex[ti.Status] = tasksMap{}
}
ji.TaskStatusIndex[ti.Status][ti.UID] = ti
}
// AddTaskInfo is used to add a task to a job
func (ji *JobInfo) AddTaskInfo(ti *TaskInfo) {
ji.Tasks[ti.UID] = ti
ji.addTaskIndex(ti)
ji.TotalRequest.Add(ti.Resreq)
if AllocatedStatus(ti.Status) {
ji.Allocated.Add(ti.Resreq)
}
}
// UpdateTaskStatus is used to update task's status in a job.
// If error occurs both task and job are guaranteed to be in the original state.
func (ji *JobInfo) UpdateTaskStatus(task *TaskInfo, status TaskStatus) error {
if err := validateStatusUpdate(task.Status, status); err != nil {
return err
}
// First remove the task (if exist) from the task list.
if _, found := ji.Tasks[task.UID]; found {
if err := ji.DeleteTaskInfo(task); err != nil {
return err
}
}
// Update task's status to the target status once task addition is guaranteed to succeed.
task.Status = status
ji.AddTaskInfo(task)
return nil
}
func (ji *JobInfo) deleteTaskIndex(ti *TaskInfo) {
if tasks, found := ji.TaskStatusIndex[ti.Status]; found {
delete(tasks, ti.UID)
if len(tasks) == 0 {
delete(ji.TaskStatusIndex, ti.Status)
}
}
}
// DeleteTaskInfo is used to delete a task from a job
func (ji *JobInfo) DeleteTaskInfo(ti *TaskInfo) error {
if task, found := ji.Tasks[ti.UID]; found {
ji.TotalRequest.Sub(task.Resreq)
if AllocatedStatus(task.Status) {
ji.Allocated.Sub(task.Resreq)
}
delete(ji.Tasks, task.UID)
ji.deleteTaskIndex(task)
return nil
}
return fmt.Errorf("failed to find task <%v/%v> in job <%v/%v>",
ti.Namespace, ti.Name, ji.Namespace, ji.Name)
}
// Clone is used to clone a jobInfo object
func (ji *JobInfo) Clone() *JobInfo {
info := &JobInfo{
UID: ji.UID,
Name: ji.Name,
Namespace: ji.Namespace,
Queue: ji.Queue,
Priority: ji.Priority,
MinAvailable: ji.MinAvailable,
WaitingTime: ji.WaitingTime,
JobFitErrors: ji.JobFitErrors,
NodesFitErrors: make(map[TaskID]*FitErrors),
Allocated: EmptyResource(),
TotalRequest: EmptyResource(),
PodGroup: ji.PodGroup.Clone(),
TaskStatusIndex: map[TaskStatus]tasksMap{},
TaskMinAvailable: make(map[TaskID]int32, len(ji.TaskMinAvailable)),
TaskMinAvailableTotal: ji.TaskMinAvailableTotal,
Tasks: tasksMap{},
Preemptable: ji.Preemptable,
RevocableZone: ji.RevocableZone,
Budget: ji.Budget.Clone(),
}
ji.CreationTimestamp.DeepCopyInto(&info.CreationTimestamp)
for task, minAvailable := range ji.TaskMinAvailable {
info.TaskMinAvailable[task] = minAvailable
}
for _, task := range ji.Tasks {
info.AddTaskInfo(task.Clone())
}
return info
}
// String returns a jobInfo object in string format
func (ji JobInfo) String() string {
res := ""
i := 0
for _, task := range ji.Tasks {
res += fmt.Sprintf("\n\t %d: %v", i, task)
i++
}
return fmt.Sprintf("Job (%v): namespace %v (%v), name %v, minAvailable %d, podGroup %+v, preemptable %+v, revocableZone %+v, minAvailable %+v, maxAvailable %+v",
ji.UID, ji.Namespace, ji.Queue, ji.Name, ji.MinAvailable, ji.PodGroup, ji.Preemptable, ji.RevocableZone, ji.Budget.MinAvailable, ji.Budget.MaxUnavilable) + res
}
// FitError returns detailed information on why a job's task failed to fit on
// each available node
func (ji *JobInfo) FitError() string {
sortReasonsHistogram := func(reasons map[string]int) []string {
reasonStrings := []string{}
for k, v := range reasons {
reasonStrings = append(reasonStrings, fmt.Sprintf("%v %v", v, k))
}
sort.Strings(reasonStrings)
return reasonStrings
}
// Stat histogram for all tasks of the job
reasons := make(map[string]int)
for status, taskMap := range ji.TaskStatusIndex {
reasons[status.String()] += len(taskMap)
}
reasons["minAvailable"] = int(ji.MinAvailable)
reasonMsg := fmt.Sprintf("%v, %v", scheduling.PodGroupNotReady, strings.Join(sortReasonsHistogram(reasons), ", "))
// Stat histogram for pending tasks only
reasons = make(map[string]int)
for uid := range ji.TaskStatusIndex[Pending] {
reason, _ := ji.TaskSchedulingReason(uid)
reasons[reason]++
}
if len(reasons) > 0 {
reasonMsg += "; " + fmt.Sprintf("%s: %s", Pending.String(), strings.Join(sortReasonsHistogram(reasons), ", "))
}
return reasonMsg
}
// TaskSchedulingReason get detailed reason and message of the given task
// It returns detailed reason and message for tasks based on last scheduling transaction.
func (ji *JobInfo) TaskSchedulingReason(tid TaskID) (reason string, msg string) {
taskInfo, exists := ji.Tasks[tid]
if !exists {
return "", ""
}
// Get detailed scheduling reason based on LastTransaction
ctx := taskInfo.GetTransactionContext()
if taskInfo.LastTransaction != nil {
ctx = *taskInfo.LastTransaction
}
msg = ji.JobFitErrors
switch status := ctx.Status; status {
case Allocated:
// Pod is schedulable
msg = fmt.Sprintf("Pod %s/%s can possibly be assigned to %s", taskInfo.Namespace, taskInfo.Name, ctx.NodeName)
return PodReasonSchedulable, msg
case Pipelined:
msg = fmt.Sprintf("Pod %s/%s can possibly be assigned to %s, once resource is released", taskInfo.Namespace, taskInfo.Name, ctx.NodeName)
return PodReasonUnschedulable, msg
case Pending:
if fe := ji.NodesFitErrors[tid]; fe != nil {
// Pod is not schedulable
return PodReasonUnschedulable, fe.Error()
}
// Pod is not scheduled yet, keep UNSCHEDULABLE as the reason to support cluster autoscaler
return PodReasonUnschedulable, msg
default:
return status.String(), msg
}
}
// ReadyTaskNum returns the number of tasks that are ready or that is best-effort.
func (ji *JobInfo) ReadyTaskNum() int32 {
occupied := 0
occupied += len(ji.TaskStatusIndex[Bound])
occupied += len(ji.TaskStatusIndex[Binding])
occupied += len(ji.TaskStatusIndex[Running])
occupied += len(ji.TaskStatusIndex[Allocated])
occupied += len(ji.TaskStatusIndex[Succeeded])
if tasks, found := ji.TaskStatusIndex[Pending]; found {
for _, task := range tasks {
if task.BestEffort {
occupied++
}
}
}
return int32(occupied)
}
// WaitingTaskNum returns the number of tasks that are pipelined.
func (ji *JobInfo) WaitingTaskNum() int32 {
return int32(len(ji.TaskStatusIndex[Pipelined]))
}
// CheckTaskValid returns whether each task of job is valid.
func (ji *JobInfo) CheckTaskValid() bool {
// if job minAvailable is less than sumof(task minAvailable), skip this check
if ji.MinAvailable < ji.TaskMinAvailableTotal {
return true
}
actual := map[TaskID]int32{}
for status, tasks := range ji.TaskStatusIndex {
if AllocatedStatus(status) ||
status == Succeeded ||
status == Pipelined ||
status == Pending {
for _, task := range tasks {
actual[getTaskID(task.Pod)]++
}
}
}
klog.V(4).Infof("job %s/%s actual: %+v, ji.TaskMinAvailable: %+v", ji.Name, ji.Namespace, actual, ji.TaskMinAvailable)
for task, minAvailable := range ji.TaskMinAvailable {
if minAvailable == 0 {
continue
}
if act, ok := actual[task]; !ok || act < minAvailable {
return false
}
}
return true
}
// CheckTaskReady return whether each task of job is ready.
func (ji *JobInfo) CheckTaskReady() bool {
if ji.MinAvailable < ji.TaskMinAvailableTotal {
return true
}
occupiedMap := map[TaskID]int32{}
for status, tasks := range ji.TaskStatusIndex {
if AllocatedStatus(status) ||
status == Succeeded {
for _, task := range tasks {
occupiedMap[getTaskID(task.Pod)]++
}
continue
}
if status == Pending {
for _, task := range tasks {
if task.InitResreq.IsEmpty() {
occupiedMap[getTaskID(task.Pod)]++
}
}
}
}
for taskID, minNum := range ji.TaskMinAvailable {
if occupiedMap[taskID] < minNum {
klog.V(4).Infof("Job %s/%s Task %s occupied %v less than task min avaliable", ji.Namespace, ji.Name, taskID, occupiedMap[taskID])
return false
}
}
return true
}
// CheckTaskPipelined return whether each task of job is pipelined.
func (ji *JobInfo) CheckTaskPipelined() bool {
if ji.MinAvailable < ji.TaskMinAvailableTotal {
return true
}
occupiedMap := map[TaskID]int32{}
for status, tasks := range ji.TaskStatusIndex {
if AllocatedStatus(status) ||
status == Succeeded ||
status == Pipelined {
for _, task := range tasks {
occupiedMap[getTaskID(task.Pod)]++
}
continue
}
if status == Pending {
for _, task := range tasks {
if task.InitResreq.IsEmpty() {
occupiedMap[getTaskID(task.Pod)]++
}
}
}
}
for taskID, minNum := range ji.TaskMinAvailable {
if occupiedMap[taskID] < minNum {
klog.V(4).Infof("Job %s/%s Task %s occupied %v less than task min avaliable", ji.Namespace, ji.Name, taskID, occupiedMap[taskID])
return false
}
}
return true
}
// CheckTaskStarving return whether job has at least one task which is starving.
func (ji *JobInfo) CheckTaskStarving() bool {
if ji.MinAvailable < ji.TaskMinAvailableTotal {
return true
}
occupiedMap := map[TaskID]int32{}
for status, tasks := range ji.TaskStatusIndex {
if AllocatedStatus(status) ||
status == Succeeded ||
status == Pipelined {
for _, task := range tasks {
occupiedMap[getTaskID(task.Pod)]++
}
continue
}
if status == Pending {
for _, task := range tasks {
if task.InitResreq.IsEmpty() {
occupiedMap[getTaskID(task.Pod)]++
}
}
}
}
for taskID, minNum := range ji.TaskMinAvailable {
if occupiedMap[taskID] < minNum {
klog.V(4).Infof("Job %s/%s Task %s occupied %v less than task min available", ji.Namespace, ji.Name, taskID, occupiedMap[taskID])
return true
}
}
return false
}
// ValidTaskNum returns the number of tasks that are valid.
func (ji *JobInfo) ValidTaskNum() int32 {
occupied := 0
for status, tasks := range ji.TaskStatusIndex {
if AllocatedStatus(status) ||
status == Succeeded ||
status == Pipelined ||
status == Pending {
occupied += len(tasks)
}
}
return int32(occupied)
}
// Ready returns whether job is ready for run
func (ji *JobInfo) Ready() bool {
occupied := ji.ReadyTaskNum()
return occupied >= ji.MinAvailable
}
// IsPending returns whether job is in pending status
func (ji *JobInfo) IsPending() bool {
return ji.PodGroup == nil ||
ji.PodGroup.Status.Phase == scheduling.PodGroupPending ||
ji.PodGroup.Status.Phase == ""
}
// HasPendingTasks return whether job has pending tasks
func (ji *JobInfo) HasPendingTasks() bool {
return len(ji.TaskStatusIndex[Pending]) != 0
}