-
Notifications
You must be signed in to change notification settings - Fork 607
/
state.go
461 lines (370 loc) · 11.7 KB
/
state.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
/*
Copyright 2022 Cortex Labs, Inc.
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 job
import (
"path"
"path/filepath"
"time"
"github.com/cortexlabs/cortex/pkg/config"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/pointer"
"github.com/cortexlabs/cortex/pkg/lib/strings"
"github.com/cortexlabs/cortex/pkg/types/spec"
"github.com/cortexlabs/cortex/pkg/types/status"
"github.com/cortexlabs/cortex/pkg/types/userconfig"
)
const (
_averageFilesPerJobState = 10
)
type State struct {
spec.JobKey
Status status.JobCode
LastUpdatedMap map[string]time.Time
EndTime *time.Time
}
func (j State) GetLastUpdated() time.Time {
lastUpdated := time.Time{}
for _, fileLastUpdated := range j.LastUpdatedMap {
if lastUpdated.After(fileLastUpdated) {
lastUpdated = fileLastUpdated
}
}
return lastUpdated
}
func (j State) GetFirstCreated() time.Time {
firstCreated := time.Unix(1<<63-62135596801, 999999999) // Max time
for _, fileLastUpdated := range j.LastUpdatedMap {
if firstCreated.After(fileLastUpdated) {
firstCreated = fileLastUpdated
}
}
return firstCreated
}
// Doesn't assume only status files are present. The order below matters.
func GetTaskStatusCode(lastUpdatedMap map[string]time.Time) status.JobCode {
if _, ok := lastUpdatedMap[status.JobStopped.String()]; ok {
return status.JobStopped
}
if _, ok := lastUpdatedMap[status.JobTimedOut.String()]; ok {
return status.JobTimedOut
}
if _, ok := lastUpdatedMap[status.JobWorkerOOM.String()]; ok {
return status.JobWorkerOOM
}
if _, ok := lastUpdatedMap[status.JobWorkerError.String()]; ok {
return status.JobWorkerError
}
if _, ok := lastUpdatedMap[status.JobEnqueueFailed.String()]; ok {
return status.JobEnqueueFailed
}
if _, ok := lastUpdatedMap[status.JobUnexpectedError.String()]; ok {
return status.JobUnexpectedError
}
if _, ok := lastUpdatedMap[status.JobCompletedWithFailures.String()]; ok {
return status.JobCompletedWithFailures
}
if _, ok := lastUpdatedMap[status.JobSucceeded.String()]; ok {
return status.JobSucceeded
}
if _, ok := lastUpdatedMap[status.JobRunning.String()]; ok {
return status.JobRunning
}
if _, ok := lastUpdatedMap[status.JobEnqueuing.String()]; ok {
return status.JobEnqueuing
}
if _, ok := lastUpdatedMap[status.JobPending.String()]; ok {
return status.JobPending
}
return status.JobUnknown
}
func GetBatchStatusCode(lastUpdatedMap map[string]time.Time) status.JobCode {
if _, ok := lastUpdatedMap[status.JobTimedOut.String()]; ok {
return status.JobTimedOut
}
if _, ok := lastUpdatedMap[status.JobWorkerOOM.String()]; ok {
return status.JobWorkerOOM
}
if _, ok := lastUpdatedMap[status.JobWorkerError.String()]; ok {
return status.JobWorkerError
}
if _, ok := lastUpdatedMap[status.JobEnqueueFailed.String()]; ok {
return status.JobEnqueueFailed
}
if _, ok := lastUpdatedMap[status.JobUnexpectedError.String()]; ok {
return status.JobUnexpectedError
}
if _, ok := lastUpdatedMap[status.JobCompletedWithFailures.String()]; ok {
return status.JobCompletedWithFailures
}
if _, ok := lastUpdatedMap[status.JobSucceeded.String()]; ok {
return status.JobSucceeded
}
if _, ok := lastUpdatedMap[status.JobStopped.String()]; ok {
return status.JobStopped
}
if _, ok := lastUpdatedMap[status.JobRunning.String()]; ok {
return status.JobRunning
}
if _, ok := lastUpdatedMap[status.JobEnqueuing.String()]; ok {
return status.JobEnqueuing
}
if _, ok := lastUpdatedMap[status.JobPending.String()]; ok {
return status.JobPending
}
return status.JobUnknown
}
func GetJobState(jobKey spec.JobKey) (*State, error) {
s3Objects, err := config.AWS.ListS3Prefix(config.ClusterConfig.Bucket, jobKey.Prefix(config.ClusterConfig.ClusterUID), false, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to get job state", jobKey.UserString())
}
if len(s3Objects) == 0 {
return nil, errors.Wrap(ErrorJobNotFound(jobKey), "failed to get job state")
}
lastUpdatedMap := map[string]time.Time{}
for _, object := range s3Objects {
lastUpdatedMap[filepath.Base(*object.Key)] = *object.LastModified
}
jobState := getJobStateFromFiles(jobKey, lastUpdatedMap)
return &jobState, nil
}
func getJobStateFromFiles(jobKey spec.JobKey, lastUpdatedFileMap map[string]time.Time) State {
var statusCode status.JobCode
switch jobKey.Kind {
case userconfig.BatchAPIKind:
statusCode = GetBatchStatusCode(lastUpdatedFileMap)
case userconfig.TaskAPIKind:
statusCode = GetTaskStatusCode(lastUpdatedFileMap)
}
var jobEndTime *time.Time
if statusCode.IsCompleted() {
if endTime, ok := lastUpdatedFileMap[statusCode.String()]; ok {
jobEndTime = &endTime
}
}
return State{
JobKey: jobKey,
LastUpdatedMap: lastUpdatedFileMap,
Status: statusCode,
EndTime: jobEndTime,
}
}
func GetMostRecentlySubmittedJobStates(apiName string, count int, kind userconfig.Kind) ([]*State, error) {
// a single job state may include 5 files on average, overshoot the number of files needed
apiPrefix := strings.EnsureSuffix(spec.JobAPIPrefix(config.ClusterConfig.ClusterUID, kind, apiName), "/")
s3Objects, err := config.AWS.ListS3Prefix(
config.ClusterConfig.Bucket,
apiPrefix,
false,
pointer.Int64(int64(count*_averageFilesPerJobState)),
nil,
)
if err != nil {
return nil, err
}
// job id -> file name -> last update timestamp
lastUpdatedMaps := map[string]map[string]time.Time{}
var jobIDOrder []string
for _, object := range s3Objects {
if object == nil {
continue
}
fileName := filepath.Base(*object.Key)
jobID := filepath.Base(filepath.Dir(*object.Key))
if _, ok := lastUpdatedMaps[jobID]; !ok {
jobIDOrder = append(jobIDOrder, jobID)
lastUpdatedMaps[jobID] = map[string]time.Time{fileName: *object.LastModified}
} else {
lastUpdatedMaps[jobID][fileName] = *object.LastModified
}
}
jobStates := make([]*State, 0, count)
jobStateCount := 0
for _, jobID := range jobIDOrder {
// it is possible to have fragmented deletes, spec.json should always be there
_, found := lastUpdatedMaps[jobID]["spec.json"]
if !found {
go config.AWS.DeleteS3Dir(config.ClusterConfig.Bucket, path.Join(apiPrefix, jobID), true)
continue
}
jobState := getJobStateFromFiles(spec.JobKey{
APIName: apiName,
ID: jobID,
Kind: kind,
}, lastUpdatedMaps[jobID])
jobStates = append(jobStates, &jobState)
jobStateCount++
if jobStateCount == count {
break
}
}
return jobStates, nil
}
func SetStatusForJob(jobKey spec.JobKey, jobStatus status.JobCode) error {
switch jobStatus {
case status.JobEnqueuing:
return SetEnqueuingStatus(jobKey)
case status.JobRunning:
return SetRunningStatus(jobKey)
case status.JobEnqueueFailed:
return SetEnqueueFailedStatus(jobKey)
case status.JobCompletedWithFailures:
return SetCompletedWithFailuresStatus(jobKey)
case status.JobSucceeded:
return SetSucceededStatus(jobKey)
case status.JobUnexpectedError:
return SetUnexpectedErrorStatus(jobKey)
case status.JobWorkerError:
return SetWorkerErrorStatus(jobKey)
case status.JobWorkerOOM:
return SetWorkerOOMStatus(jobKey)
case status.JobTimedOut:
return SetTimedOutStatus(jobKey)
case status.JobStopped:
return SetStoppedStatus(jobKey)
}
return nil
}
func UpdateLiveness(jobKey spec.JobKey) error {
s3Key := path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), _enqueuingLivenessFile)
err := config.AWS.UploadJSONToS3(time.Now(), config.ClusterConfig.Bucket, s3Key)
if err != nil {
return errors.Wrap(err, "failed to update liveness", jobKey.UserString())
}
return nil
}
func SetEnqueuingStatus(jobKey spec.JobKey) error {
err := UpdateLiveness(jobKey)
if err != nil {
return err
}
err = config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobEnqueuing.String()))
if err != nil {
return err
}
err = uploadInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetFailedStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobEnqueueFailed.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetRunningStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobRunning.String()))
if err != nil {
return err
}
err = uploadInProgressFile(jobKey) // in progress file should already be there but just in case
if err != nil {
return err
}
return nil
}
func SetStoppedStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobStopped.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetSucceededStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobSucceeded.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetCompletedWithFailuresStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobCompletedWithFailures.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetWorkerErrorStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobWorkerError.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetWorkerOOMStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobWorkerOOM.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetEnqueueFailedStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobEnqueueFailed.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetUnexpectedErrorStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobUnexpectedError.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}
func SetTimedOutStatus(jobKey spec.JobKey) error {
err := config.AWS.UploadStringToS3("", config.ClusterConfig.Bucket, path.Join(jobKey.Prefix(config.ClusterConfig.ClusterUID), status.JobTimedOut.String()))
if err != nil {
return err
}
err = DeleteInProgressFile(jobKey)
if err != nil {
return err
}
return nil
}