-
Notifications
You must be signed in to change notification settings - Fork 700
/
job.go
412 lines (360 loc) · 14.4 KB
/
job.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
/*
Copyright 2023 The Kubeflow 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 common
import (
"fmt"
"reflect"
"time"
apiv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"github.com/kubeflow/training-operator/pkg/controller.v1/expectation"
"github.com/kubeflow/training-operator/pkg/core"
commonutil "github.com/kubeflow/training-operator/pkg/util"
"github.com/kubeflow/training-operator/pkg/util/k8sutil"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog/v2"
schedulerpluginsv1alpha1 "sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"
volcanov1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
)
func (jc *JobController) DeletePodsAndServices(runPolicy *apiv1.RunPolicy, job interface{}, pods []*corev1.Pod) error {
if len(pods) == 0 {
return nil
}
// Delete nothing when the cleanPodPolicy is None.
if *runPolicy.CleanPodPolicy == apiv1.CleanPodPolicyNone {
return nil
}
for _, pod := range pods {
// Note that pending pod will turn into running once schedulable,
// not cleaning it may leave orphan running pod in the future,
// we should treat it equivalent to running phase here.
if *runPolicy.CleanPodPolicy == apiv1.CleanPodPolicyRunning && pod.Status.Phase != corev1.PodRunning && pod.Status.Phase != corev1.PodPending {
continue
}
if err := jc.PodControl.DeletePod(pod.Namespace, pod.Name, job.(runtime.Object)); err != nil {
return err
}
// Pod and service have the same name, thus the service could be deleted using pod's name.
if err := jc.ServiceControl.DeleteService(pod.Namespace, pod.Name, job.(runtime.Object)); err != nil {
return err
}
}
return nil
}
// recordAbnormalPods records the active pod whose latest condition is not in True status.
func (jc *JobController) recordAbnormalPods(activePods []*corev1.Pod, object runtime.Object) {
core.RecordAbnormalPods(activePods, object, jc.Recorder)
}
// ReconcileJobs checks and updates replicas for each given ReplicaSpec.
// It will requeue the job in case of an error while creating/deleting pods/services.
func (jc *JobController) ReconcileJobs(
job interface{},
replicas map[apiv1.ReplicaType]*apiv1.ReplicaSpec,
jobStatus apiv1.JobStatus,
runPolicy *apiv1.RunPolicy) error {
metaObject, ok := job.(metav1.Object)
jobName := metaObject.GetName()
if !ok {
return fmt.Errorf("job is not of type metav1.Object")
}
runtimeObject, ok := job.(runtime.Object)
if !ok {
return fmt.Errorf("job is not of type runtime.Object")
}
jobKey, err := KeyFunc(job)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get key for job object %#v: %v", job, err))
return err
}
jobKind := jc.Controller.GetAPIGroupVersionKind().Kind
// Reset expectations
// 1. Since `ReconcileJobs` is called, we expect that previous expectations are all satisfied,
// and it's safe to reset the expectations
// 2. Reset expectations can avoid dirty data such as `expectedDeletion = -1`
// (pod or service was deleted unexpectedly)
if err = jc.ResetExpectations(jobKey, replicas); err != nil {
log.Warnf("Failed to reset expectations: %v", err)
}
log.Infof("Reconciling for job %s", metaObject.GetName())
pods, err := jc.Controller.GetPodsForJob(job)
if err != nil {
log.Warnf("GetPodsForJob error %v", err)
return err
}
services, err := jc.Controller.GetServicesForJob(job)
if err != nil {
log.Warnf("GetServicesForJob error %v", err)
return err
}
oldStatus := jobStatus.DeepCopy()
if commonutil.IsSucceeded(jobStatus) || commonutil.IsFailed(jobStatus) {
// If the Job is succeed or failed, delete all pods and services.
if err := jc.DeletePodsAndServices(runPolicy, job, pods); err != nil {
return err
}
if jc.Config.EnableGangScheduling() {
jc.Recorder.Event(runtimeObject, corev1.EventTypeNormal, "JobTerminated", "Job has been terminated. Deleting PodGroup")
if err := jc.DeletePodGroup(metaObject); err != nil {
jc.Recorder.Eventf(runtimeObject, corev1.EventTypeWarning, "FailedDeletePodGroup", "Error deleting: %v", err)
return err
} else {
jc.Recorder.Eventf(runtimeObject, corev1.EventTypeNormal, "SuccessfulDeletePodGroup", "Deleted PodGroup: %v", jobName)
}
}
if err := jc.CleanupJob(runPolicy, jobStatus, job); err != nil {
return err
}
// At this point the pods may have been deleted.
// 1) If the job succeeded, we manually set the replica status.
// 2) If any replicas are still active, set their status to succeeded.
if commonutil.IsSucceeded(jobStatus) {
for rtype := range jobStatus.ReplicaStatuses {
jobStatus.ReplicaStatuses[rtype].Succeeded += jobStatus.ReplicaStatuses[rtype].Active
jobStatus.ReplicaStatuses[rtype].Active = 0
}
}
// No need to update the job status if the status hasn't changed since last time.
if !reflect.DeepEqual(*oldStatus, jobStatus) {
return jc.Controller.UpdateJobStatusInApiServer(job, &jobStatus)
}
return nil
}
// retrieve the previous number of retry
previousRetry := jc.WorkQueue.NumRequeues(jobKey)
activePods := k8sutil.FilterActivePods(pods)
jc.recordAbnormalPods(activePods, runtimeObject)
active := int32(len(activePods))
failed := k8sutil.FilterPodCount(pods, corev1.PodFailed)
totalReplicas := k8sutil.GetTotalReplicas(replicas)
prevReplicasFailedNum := k8sutil.GetTotalFailedReplicas(jobStatus.ReplicaStatuses)
var failureMessage string
jobExceedsLimit := false
exceedsBackoffLimit := false
pastBackoffLimit := false
if runPolicy.BackoffLimit != nil {
jobHasNewFailure := failed > prevReplicasFailedNum
// new failures happen when status does not reflect the failures and active
// is different than parallelism, otherwise the previous controller loop
// failed updating status so even if we pick up failure it is not a new one
exceedsBackoffLimit = jobHasNewFailure && (active != totalReplicas) &&
(int32(previousRetry)+1 > *runPolicy.BackoffLimit)
pastBackoffLimit, err = jc.PastBackoffLimit(jobName, runPolicy, replicas, pods)
if err != nil {
return err
}
}
if exceedsBackoffLimit || pastBackoffLimit {
// check if the number of pod restart exceeds backoff (for restart OnFailure only)
// OR if the number of failed jobs increased since the last syncJob
jobExceedsLimit = true
failureMessage = fmt.Sprintf("Job %s has failed because it has reached the specified backoff limit", jobName)
} else if jc.PastActiveDeadline(runPolicy, jobStatus) {
failureMessage = fmt.Sprintf("Job %s has failed because it was active longer than specified deadline", jobName)
jobExceedsLimit = true
}
if jobExceedsLimit {
// Set job completion time before resource cleanup
if jobStatus.CompletionTime == nil {
now := metav1.Now()
jobStatus.CompletionTime = &now
}
// If the Job exceeds backoff limit or is past active deadline
// delete all pods and services, then set the status to failed
if err := jc.DeletePodsAndServices(runPolicy, job, pods); err != nil {
return err
}
if err := jc.CleanupJob(runPolicy, jobStatus, job); err != nil {
return err
}
if jc.Config.EnableGangScheduling() {
jc.Recorder.Event(runtimeObject, corev1.EventTypeNormal, "JobTerminated", "Job has been terminated. Deleting PodGroup")
if err := jc.DeletePodGroup(metaObject); err != nil {
jc.Recorder.Eventf(runtimeObject, corev1.EventTypeWarning, "FailedDeletePodGroup", "Error deleting: %v", err)
return err
} else {
jc.Recorder.Eventf(runtimeObject, corev1.EventTypeNormal, "SuccessfulDeletePodGroup", "Deleted PodGroup: %v", jobName)
}
}
jc.Recorder.Event(runtimeObject, corev1.EventTypeNormal, commonutil.NewReason(jobKind, commonutil.JobFailedReason), failureMessage)
if err = commonutil.UpdateJobConditions(&jobStatus, apiv1.JobFailed, commonutil.NewReason(jobKind, commonutil.JobFailedReason), failureMessage); err != nil {
log.Infof("Append job condition error: %v", err)
return err
}
return jc.Controller.UpdateJobStatusInApiServer(job, &jobStatus)
} else {
// General cases which need to reconcile
if jc.Config.EnableGangScheduling() {
minMember := totalReplicas
queue := ""
priorityClass := ""
var schedulerTimeout *int32
var minResources *v1.ResourceList
if runPolicy.SchedulingPolicy != nil {
if minAvailable := runPolicy.SchedulingPolicy.MinAvailable; minAvailable != nil {
minMember = *minAvailable
}
if q := runPolicy.SchedulingPolicy.Queue; len(q) != 0 {
queue = q
}
if pc := runPolicy.SchedulingPolicy.PriorityClass; len(pc) != 0 {
priorityClass = pc
}
if mr := runPolicy.SchedulingPolicy.MinResources; mr != nil {
minResources = (*v1.ResourceList)(mr)
}
if timeout := runPolicy.SchedulingPolicy.ScheduleTimeoutSeconds; timeout != nil {
schedulerTimeout = timeout
}
}
if minResources == nil {
minResources = jc.calcPGMinResources(minMember, replicas)
}
var pgSpecFill FillPodGroupSpecFunc
switch jc.Config.GangScheduling {
case GangSchedulerVolcano:
pgSpecFill = func(pg metav1.Object) error {
volcanoPodGroup, match := pg.(*volcanov1beta1.PodGroup)
if !match {
return fmt.Errorf("unable to recognize PodGroup: %v", klog.KObj(pg))
}
volcanoPodGroup.Spec = volcanov1beta1.PodGroupSpec{
MinMember: minMember,
Queue: queue,
PriorityClassName: priorityClass,
MinResources: minResources,
}
return nil
}
default:
pgSpecFill = func(pg metav1.Object) error {
schedulerPluginsPodGroup, match := pg.(*schedulerpluginsv1alpha1.PodGroup)
if !match {
return fmt.Errorf("unable to recognize PodGroup: %v", klog.KObj(pg))
}
schedulerPluginsPodGroup.Spec = schedulerpluginsv1alpha1.PodGroupSpec{
MinMember: minMember,
MinResources: *minResources,
ScheduleTimeoutSeconds: schedulerTimeout,
}
return nil
}
}
syncReplicas := true
pg, err := jc.SyncPodGroup(metaObject, pgSpecFill)
if err != nil {
log.Warnf("Sync PodGroup %v: %v", jobKey, err)
syncReplicas = false
}
// Delay pods creation until PodGroup status is Inqueue
if jc.PodGroupControl.DelayPodCreationDueToPodGroup(pg) {
log.Warnf("PodGroup %v unschedulable", jobKey)
syncReplicas = false
}
if !syncReplicas {
now := metav1.Now()
jobStatus.LastReconcileTime = &now
// Update job status here to trigger a new reconciliation
return jc.Controller.UpdateJobStatusInApiServer(job, &jobStatus)
}
}
// Diff current active pods/services with replicas.
for rtype, spec := range replicas {
err := jc.Controller.ReconcilePods(metaObject, &jobStatus, pods, rtype, spec, replicas)
if err != nil {
log.Warnf("ReconcilePods error %v", err)
return err
}
err = jc.Controller.ReconcileServices(metaObject, services, rtype, spec)
if err != nil {
log.Warnf("ReconcileServices error %v", err)
return err
}
}
}
err = jc.Controller.UpdateJobStatus(job, replicas, &jobStatus)
if err != nil {
log.Warnf("UpdateJobStatus error %v", err)
return err
}
// No need to update the job status if the status hasn't changed since last time.
if !reflect.DeepEqual(*oldStatus, jobStatus) {
return jc.Controller.UpdateJobStatusInApiServer(job, &jobStatus)
}
return nil
}
// ResetExpectations reset the expectation for creates and deletes of pod/service to zero.
func (jc *JobController) ResetExpectations(jobKey string, replicas map[apiv1.ReplicaType]*apiv1.ReplicaSpec) error {
var allErrs error
for rtype := range replicas {
expectationPodsKey := expectation.GenExpectationPodsKey(jobKey, string(rtype))
if err := jc.Expectations.SetExpectations(expectationPodsKey, 0, 0); err != nil {
allErrs = err
}
expectationServicesKey := expectation.GenExpectationServicesKey(jobKey, string(rtype))
if err := jc.Expectations.SetExpectations(expectationServicesKey, 0, 0); err != nil {
allErrs = fmt.Errorf("%s: %w", allErrs.Error(), err)
}
}
return allErrs
}
// PastActiveDeadline checks if job has ActiveDeadlineSeconds field set and if it is exceeded.
func (jc *JobController) PastActiveDeadline(runPolicy *apiv1.RunPolicy, jobStatus apiv1.JobStatus) bool {
return core.PastActiveDeadline(runPolicy, jobStatus)
}
// PastBackoffLimit checks if container restartCounts sum exceeds BackoffLimit
// this method applies only to pods when restartPolicy is one of OnFailure, Always or ExitCode
func (jc *JobController) PastBackoffLimit(jobName string, runPolicy *apiv1.RunPolicy,
replicas map[apiv1.ReplicaType]*apiv1.ReplicaSpec, pods []*corev1.Pod) (bool, error) {
return core.PastBackoffLimit(jobName, runPolicy, replicas, pods, jc.FilterPodsForReplicaType)
}
func (jc *JobController) CleanupJob(runPolicy *apiv1.RunPolicy, jobStatus apiv1.JobStatus, job interface{}) error {
currentTime := time.Now()
metaObject, _ := job.(metav1.Object)
ttl := runPolicy.TTLSecondsAfterFinished
if ttl == nil {
return nil
}
duration := time.Second * time.Duration(*ttl)
if jobStatus.CompletionTime == nil {
return fmt.Errorf("job completion time is nil, cannot cleanup")
}
finishTime := jobStatus.CompletionTime
expireTime := finishTime.Add(duration)
if currentTime.After(expireTime) {
err := jc.Controller.DeleteJob(job)
if err != nil {
commonutil.LoggerForJob(metaObject).Warnf("Cleanup Job error: %v.", err)
return err
}
return nil
} else {
if finishTime.After(currentTime) {
commonutil.LoggerForJob(metaObject).Warnf("Found Job finished in the future. This is likely due to time skew in the cluster. Job cleanup will be deferred.")
}
remaining := expireTime.Sub(currentTime)
key, err := KeyFunc(job)
if err != nil {
commonutil.LoggerForJob(metaObject).Warnf("Couldn't get key for job object: %v", err)
return err
}
jc.WorkQueue.AddAfter(key, remaining)
return nil
}
}
func (jc *JobController) calcPGMinResources(minMember int32, replicas map[apiv1.ReplicaType]*apiv1.ReplicaSpec) *v1.ResourceList {
return CalcPGMinResources(minMember, replicas, jc.PriorityClassLister.Get)
}