-
Notifications
You must be signed in to change notification settings - Fork 79
/
status.go
141 lines (127 loc) · 5.24 KB
/
status.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
/*
Copyright 2019 The Alibaba 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 pytorch
import (
"errors"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
training "github.com/alibaba/kubedl/apis/training/v1alpha1"
"github.com/alibaba/kubedl/pkg/job_controller"
v1 "github.com/alibaba/kubedl/pkg/job_controller/api/v1"
commonutil "github.com/alibaba/kubedl/pkg/util"
)
// updateGeneralJobStatus updates the status of job with given replica specs and job status.
func (r *PytorchJobReconciler) updateGeneralJobStatus(pytorchJob *training.PyTorchJob,
replicaSpecs map[v1.ReplicaType]*v1.ReplicaSpec, jobStatus *v1.JobStatus, restart bool) error {
log.Info("Updating status", "PytorchJob name", pytorchJob.Name, "restart", restart)
// Set job status start time since this job has acknowledged by controller.
if jobStatus.StartTime == nil {
now := metav1.Now()
jobStatus.StartTime = &now
}
previousRestarting := commonutil.IsRestarting(*jobStatus)
previousFailed := commonutil.IsFailed(*jobStatus)
allWorkersSucceed := false
workerRep, workerFound := replicaSpecs[training.PyTorchReplicaTypeWorker]
if workerFound {
succeed := int32(0)
if jobStatus.ReplicaStatuses[training.PyTorchReplicaTypeWorker] != nil {
succeed = jobStatus.ReplicaStatuses[training.PyTorchReplicaTypeWorker].Succeeded
}
allWorkersSucceed = *workerRep.Replicas == succeed
}
for rtype, spec := range replicaSpecs {
replicas := *spec.Replicas
// If rtype in replica status not found, there must be a mistyped/invalid rtype in job spec,
// and it has not been reconciled in previous processes, discard it.
status, ok := jobStatus.ReplicaStatuses[rtype]
if !ok {
log.Info("skipping invalid replica type", "rtype", rtype)
continue
}
expected := replicas - status.Succeeded
running := status.Active
failed := status.Failed
log.Info("Update pytorch job status", "PyTorchJob", pytorchJob.Name,
"ReplicaType", rtype, "expected", expected, "running", running, "failed", failed)
if job_controller.ContainsReplicaType(replicaSpecs, training.PyTorchReplicaTypeMaster, v1.JobReplicaTypeAIMaster) {
if rtype == training.PyTorchReplicaTypeMaster || rtype == v1.JobReplicaTypeAIMaster {
if running > 0 {
msg := fmt.Sprintf("PyTorchJob %s is running.", pytorchJob.Name)
err := commonutil.UpdateJobConditions(jobStatus, v1.JobRunning, commonutil.JobRunningReason, msg)
if err != nil {
log.Info("Append job condition", " error:", err)
return err
}
}
// Conditions for marking job as succeeded:
// 1. master exit successfully with success policy is none.
// 2. if success policy is AllWorkers, then wait util all workers succeed.
// 3. aimaster is enabled and it exits successfully.
succeed := replicas > 0 && expected == 0
if rtype != v1.JobReplicaTypeAIMaster && workerFound {
succeed = succeed && allWorkersSucceed
}
if succeed {
msg := fmt.Sprintf("PyTorchJob %s is successfully completed.", pytorchJob.Name)
r.recorder.Event(pytorchJob, corev1.EventTypeNormal, commonutil.JobSucceededReason, msg)
if jobStatus.CompletionTime == nil {
now := metav1.Now()
jobStatus.CompletionTime = &now
}
err := commonutil.UpdateJobConditions(jobStatus, v1.JobSucceeded, commonutil.JobSucceededReason, msg)
if err != nil {
log.Info("Append job condition", "error:", err)
return err
}
r.ctrl.Metrics.SuccessInc()
}
}
} else {
log.Info("Invalid config: Job must contain master replica spec")
return errors.New("invalid config: Job must contain master replica spec")
}
if failed > 0 {
if restart && rtype != v1.JobReplicaTypeAIMaster {
msg := fmt.Sprintf("PyTorchJob %s is restarting because %d %s replica(s) failed.", pytorchJob.Name, failed, rtype)
r.recorder.Event(pytorchJob, corev1.EventTypeWarning, commonutil.JobRestartingReason, msg)
err := commonutil.UpdateJobConditions(jobStatus, v1.JobRestarting, commonutil.JobRestartingReason, msg)
if err != nil {
log.Info("Append job condition", "error:", err)
return err
}
if !previousRestarting {
r.ctrl.Metrics.FailureInc()
r.ctrl.Metrics.RestartInc()
}
} else {
msg := fmt.Sprintf("PyTorchJob %s is failed because %d %s replica(s) failed.", pytorchJob.Name, failed, rtype)
r.recorder.Event(pytorchJob, corev1.EventTypeNormal, commonutil.JobFailedReason, msg)
if jobStatus.CompletionTime == nil {
now := metav1.Now()
jobStatus.CompletionTime = &now
}
err := commonutil.UpdateJobConditions(jobStatus, v1.JobFailed, commonutil.JobFailedReason, msg)
if err != nil {
log.Info("Append job condition", "error: ", err)
return err
}
if !previousFailed {
r.ctrl.Metrics.FailureInc()
}
}
}
}
return nil
}