This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathcatalog.go
More file actions
514 lines (420 loc) · 18.4 KB
/
catalog.go
File metadata and controls
514 lines (420 loc) · 18.4 KB
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
package array
import (
"context"
"fmt"
"math"
"strconv"
arrayCore "github.com/flyteorg/flyteplugins/go/tasks/plugins/array/core"
"github.com/flyteorg/flytestdlib/bitarray"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/storage"
"github.com/flyteorg/flyteplugins/go/tasks/errors"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/catalog"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/ioutils"
idlCore "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
)
// DetermineDiscoverability checks if there are any previously cached tasks. If there are we will only submit an
// ArrayJob for the non-cached tasks. The ArrayJob is now a different size, and each task will get a new index location
// which is different than their original location. To find the original index we construct an indexLookup array.
// The subtask can find it's original index value in indexLookup[JOB_ARRAY_INDEX] where JOB_ARRAY_INDEX is an
// environment variable in the pod
func DetermineDiscoverability(ctx context.Context, tCtx core.TaskExecutionContext, state *arrayCore.State) (
*arrayCore.State, error) {
// Check that the taskTemplate is valid
taskTemplate, err := tCtx.TaskReader().Read(ctx)
if err != nil {
return state, err
} else if taskTemplate == nil {
return state, errors.Errorf(errors.BadTaskSpecification, "Required value not set, taskTemplate is nil")
}
// Extract the custom plugin pb
arrayJob, err := arrayCore.ToArrayJob(taskTemplate, taskTemplate.TaskTypeVersion)
if err != nil {
return state, err
}
var arrayJobSize int64
var inputReaders []io.InputReader
// Save this in the state
if taskTemplate.TaskTypeVersion == 0 {
state = state.SetOriginalArraySize(arrayJob.Size)
arrayJobSize = arrayJob.Size
state = state.SetOriginalMinSuccesses(arrayJob.GetMinSuccesses())
// build input readers
inputReaders, err = ConstructRemoteFileInputReaders(ctx, tCtx.DataStore(), tCtx.InputReader().GetInputPrefixPath(), int(arrayJobSize))
if err != nil {
return state, err
}
} else {
inputs, err := tCtx.InputReader().Get(ctx)
if err != nil {
return state, errors.Errorf(errors.MetadataAccessFailed, "Could not read inputs and therefore failed to determine array job size")
}
size := 0
var literalCollection *idlCore.LiteralCollection
var discoveredInputName string
for inputName, literal := range inputs.Literals {
if literalCollection = literal.GetCollection(); literalCollection != nil {
size = len(literal.GetCollection().Literals)
discoveredInputName = inputName
break
}
}
if size == 0 {
// Something is wrong, we should have inferred the array size when it is not specified by the size of the
// input collection (for any input value). Non-collection type inputs are not currently supported for
// taskTypeVersion > 0.
return state, errors.Errorf(errors.BadTaskSpecification, "Unable to determine array size from inputs")
}
minSuccesses := math.Ceil(arrayJob.GetMinSuccessRatio() * float64(size))
logger.Debugf(ctx, "Computed state: size [%d] and minSuccesses [%d]", int64(size), int64(minSuccesses))
state = state.SetOriginalArraySize(int64(size))
// We can cast the min successes because we already computed the ceiling value from the ratio
state = state.SetOriginalMinSuccesses(int64(minSuccesses))
arrayJobSize = int64(size)
// build input readers
inputReaders = ConstructStaticInputReaders(tCtx.InputReader(), literalCollection.Literals, discoveredInputName)
}
// If the task is not discoverable, then skip data catalog work and move directly to launch
if taskTemplate.Metadata == nil || !taskTemplate.Metadata.Discoverable {
logger.Infof(ctx, "Task is not discoverable, moving to launch phase...")
// Set an all set indexes to cache. This task won't try to write to catalog anyway.
state = state.SetIndexesToCache(arrayCore.InvertBitSet(bitarray.NewBitSet(uint(arrayJobSize)), uint(arrayJobSize)))
state = state.SetPhase(arrayCore.PhasePreLaunch, core.DefaultPhaseVersion).SetReason("Task is not discoverable.")
state.SetExecutionArraySize(int(arrayJobSize))
return state, nil
}
// Otherwise, run the data catalog steps - create and submit work items to the catalog processor,
// build output writers
outputWriters, err := ConstructOutputWriters(ctx, tCtx.DataStore(), tCtx.OutputWriter().GetOutputPrefixPath(), tCtx.OutputWriter().GetRawOutputPrefix(), int(arrayJobSize))
if err != nil {
return state, err
}
// build work items from inputs and outputs
workItems, err := ConstructCatalogReaderWorkItems(ctx, tCtx.TaskReader(), inputReaders, outputWriters)
if err != nil {
return state, err
}
// Check catalog, and if we have responses from catalog for everything, then move to writing the mapping file.
future, err := tCtx.Catalog().Download(ctx, workItems...)
if err != nil {
return state, err
}
switch future.GetResponseStatus() {
case catalog.ResponseStatusReady:
if err = future.GetResponseError(); err != nil {
// TODO: maybe add a config option to decide the behavior on catalog failure.
logger.Warnf(ctx, "Failing to lookup catalog. Will move on to launching the task. Error: %v", err)
state = state.SetIndexesToCache(arrayCore.InvertBitSet(bitarray.NewBitSet(uint(arrayJobSize)), uint(arrayJobSize)))
state = state.SetExecutionArraySize(int(arrayJobSize))
state = state.SetPhase(arrayCore.PhasePreLaunch, core.DefaultPhaseVersion).SetReason(fmt.Sprintf("Skipping cache check due to err [%v]", err))
return state, nil
}
logger.Debug(ctx, "Catalog download response is ready.")
resp, err := future.GetResponse()
if err != nil {
return state, err
}
cachedResults := resp.GetCachedResults()
state = state.SetIndexesToCache(arrayCore.InvertBitSet(cachedResults, uint(arrayJobSize)))
state = state.SetExecutionArraySize(int(arrayJobSize) - resp.GetCachedCount())
// If all the sub-tasks are actually done, then we can just move on.
if resp.GetCachedCount() == int(arrayJobSize) {
state.SetPhase(arrayCore.PhaseAssembleFinalOutput, core.DefaultPhaseVersion).SetReason("All subtasks are cached. assembling final outputs.")
return state, nil
}
indexLookup := CatalogBitsetToLiteralCollection(cachedResults, resp.GetResultsSize())
// TODO: Is the right thing to use? Haytham please take a look
indexLookupPath, err := ioutils.GetIndexLookupPath(ctx, tCtx.DataStore(), tCtx.InputReader().GetInputPrefixPath())
if err != nil {
return state, err
}
logger.Infof(ctx, "Writing indexlookup file to [%s], cached count [%d/%d], ",
indexLookupPath, resp.GetCachedCount(), arrayJobSize)
err = tCtx.DataStore().WriteProtobuf(ctx, indexLookupPath, storage.Options{}, indexLookup)
if err != nil {
return state, err
}
state = state.SetPhase(arrayCore.PhasePreLaunch, core.DefaultPhaseVersion).SetReason("Finished cache lookup.")
case catalog.ResponseStatusNotReady:
ownerSignal := tCtx.TaskRefreshIndicator()
future.OnReady(func(ctx context.Context, _ catalog.Future) {
ownerSignal(ctx)
})
}
return state, nil
}
func WriteToDiscovery(ctx context.Context, tCtx core.TaskExecutionContext, state *arrayCore.State, phaseOnSuccess arrayCore.Phase) (*arrayCore.State, error) {
// Check that the taskTemplate is valid
taskTemplate, err := tCtx.TaskReader().Read(ctx)
if err != nil {
return state, err
} else if taskTemplate == nil {
return state, errors.Errorf(errors.BadTaskSpecification, "Required value not set, taskTemplate is nil")
}
if tMeta := taskTemplate.Metadata; tMeta == nil || !tMeta.Discoverable {
logger.Debugf(ctx, "Task is not marked as discoverable. Moving to [%v] phase.", phaseOnSuccess)
return state.SetPhase(phaseOnSuccess, core.DefaultPhaseVersion).SetReason("Task is not discoverable."), nil
}
var inputReaders []io.InputReader
arrayJobSize := int(state.GetOriginalArraySize())
if taskTemplate.TaskTypeVersion == 0 {
// input readers
inputReaders, err = ConstructRemoteFileInputReaders(ctx, tCtx.DataStore(), tCtx.InputReader().GetInputPrefixPath(), arrayJobSize)
if err != nil {
return nil, err
}
} else {
inputs, err := tCtx.InputReader().Get(ctx)
if err != nil {
return state, errors.Errorf(errors.MetadataAccessFailed, "Could not read inputs and therefore failed to determine array job size")
}
var literalCollection *idlCore.LiteralCollection
var discoveredInputName string
for inputName, literal := range inputs.Literals {
if literalCollection = literal.GetCollection(); literalCollection != nil {
discoveredInputName = inputName
break
}
}
// build input readers
inputReaders = ConstructStaticInputReaders(tCtx.InputReader(), literalCollection.Literals, discoveredInputName)
}
// output reader
outputReaders, err := ConstructOutputReaders(ctx, tCtx.DataStore(), tCtx.OutputWriter().GetOutputPrefixPath(), tCtx.OutputWriter().GetRawOutputPrefix(), arrayJobSize)
if err != nil {
return nil, err
}
iface := *taskTemplate.Interface
iface.Outputs = makeSingularTaskInterface(iface.Outputs)
// Do not cache failed tasks. Retrieve the final phase from array status and unset the non-successful ones.
tasksToCache := state.GetIndexesToCache().DeepCopy()
for idx, phaseIdx := range state.ArrayStatus.Detailed.GetItems() {
phase := core.Phases[phaseIdx]
if !phase.IsSuccess() {
tasksToCache.Clear(uint(idx))
}
}
// Create catalog put items, but only put the ones that were not originally cached (as read from the catalog results bitset)
catalogWriterItems, err := ConstructCatalogUploadRequests(*tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetID().TaskId,
tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetID(), taskTemplate.Metadata.DiscoveryVersion,
iface, &tasksToCache, inputReaders, outputReaders)
if err != nil {
return nil, err
}
if len(catalogWriterItems) == 0 {
state.SetPhase(phaseOnSuccess, core.DefaultPhaseVersion).SetReason("No outputs need to be cached.")
return state, nil
}
allWritten, err := WriteToCatalog(ctx, tCtx.TaskRefreshIndicator(), tCtx.Catalog(), catalogWriterItems)
if err != nil {
return nil, err
}
if allWritten {
state.SetPhase(phaseOnSuccess, core.DefaultPhaseVersion).SetReason("Finished writing catalog cache.")
}
return state, nil
}
func WriteToCatalog(ctx context.Context, ownerSignal core.SignalAsync, catalogClient catalog.AsyncClient,
workItems []catalog.UploadRequest) (bool, error) {
// Enqueue work items
future, err := catalogClient.Upload(ctx, workItems...)
if err != nil {
return false, errors.Wrapf(arrayCore.ErrorWorkQueue, err,
"Error enqueuing work items")
}
// Immediately read back from the work queue, and see if it's done.
if future.GetResponseStatus() == catalog.ResponseStatusReady {
if err = future.GetResponseError(); err != nil {
// TODO: Add a config option to determine the behavior of catalog write failure.
logger.Warnf(ctx, "Catalog write failed. Will be ignored. Error: %v", err)
}
return true, nil
}
future.OnReady(func(ctx context.Context, _ catalog.Future) {
ownerSignal(ctx)
})
return false, nil
}
func ConstructCatalogUploadRequests(keyID idlCore.Identifier, taskExecID idlCore.TaskExecutionIdentifier,
cacheVersion string, taskInterface idlCore.TypedInterface, whichTasksToCache *bitarray.BitSet,
inputReaders []io.InputReader, outputReaders []io.OutputReader) ([]catalog.UploadRequest, error) {
writerWorkItems := make([]catalog.UploadRequest, 0, len(inputReaders))
if len(inputReaders) != len(outputReaders) {
return nil, errors.Errorf(arrayCore.ErrorInternalMismatch, "Length different building catalog writer items %d %d",
len(inputReaders), len(outputReaders))
}
for idx, input := range inputReaders {
if !whichTasksToCache.IsSet(uint(idx)) {
continue
}
wi := catalog.UploadRequest{
Key: catalog.Key{
Identifier: keyID,
InputReader: input,
CacheVersion: cacheVersion,
TypedInterface: taskInterface,
},
ArtifactData: outputReaders[idx],
ArtifactMetadata: catalog.Metadata{
TaskExecutionIdentifier: &taskExecID,
},
}
writerWorkItems = append(writerWorkItems, wi)
}
return writerWorkItems, nil
}
func NewLiteralScalarOfInteger(number int64) *idlCore.Literal {
return &idlCore.Literal{
Value: &idlCore.Literal_Scalar{
Scalar: &idlCore.Scalar{
Value: &idlCore.Scalar_Primitive{
Primitive: &idlCore.Primitive{
Value: &idlCore.Primitive_Integer{
Integer: number,
},
},
},
},
},
}
}
// When an AWS Batch array job kicks off, it is given the index of the array job in an environment variable.
// The SDK will use this index to look up the real index of the job using the output of this function. That is,
// if there are five subtasks originally, but 0-2 are cached in Catalog, then an array job with two jobs will kick off.
// The first job will have an AWS supplied index of 0, which will resolve to 3 from this function, and the second
// will have an index of 1, which will resolve to 4.
// The size argument to this function is needed because the BitSet may create more bits (has a capacity) higher than
// the original requested amount. If you make a BitSet with 10 bits, it may create 64 in the background, so you need
// to keep track of how many were actually requested.
func CatalogBitsetToLiteralCollection(catalogResults *bitarray.BitSet, size int) *idlCore.LiteralCollection {
literals := make([]*idlCore.Literal, 0, size)
for i := 0; i < size; i++ {
if !catalogResults.IsSet(uint(i)) {
literals = append(literals, NewLiteralScalarOfInteger(int64(i)))
}
}
return &idlCore.LiteralCollection{
Literals: literals,
}
}
func makeSingularTaskInterface(varMap *idlCore.VariableMap) *idlCore.VariableMap {
if varMap == nil || len(varMap.Variables) == 0 {
return varMap
}
res := &idlCore.VariableMap{
Variables: make(map[string]*idlCore.Variable, len(varMap.Variables)),
}
for key, val := range varMap.Variables {
if val.GetType().GetCollectionType() != nil {
res.Variables[key] = &idlCore.Variable{Type: val.GetType().GetCollectionType()}
} else {
res.Variables[key] = val
}
}
return res
}
func ConstructCatalogReaderWorkItems(ctx context.Context, taskReader core.TaskReader, inputs []io.InputReader,
outputs []io.OutputWriter) ([]catalog.DownloadRequest, error) {
t, err := taskReader.Read(ctx)
if err != nil {
return nil, err
}
workItems := make([]catalog.DownloadRequest, 0, len(inputs))
iface := *t.Interface
iface.Outputs = makeSingularTaskInterface(iface.Outputs)
for idx, inputReader := range inputs {
// TODO: Check if Identifier or Interface are empty and return err
item := catalog.DownloadRequest{
Key: catalog.Key{
Identifier: *t.Id,
CacheVersion: t.GetMetadata().DiscoveryVersion,
InputReader: inputReader,
TypedInterface: iface,
},
Target: outputs[idx],
}
workItems = append(workItems, item)
}
return workItems, nil
}
// ConstructStaticInputReaders constructs input readers that comply with the io.InputReader interface but have their
// inputs already populated.
func ConstructStaticInputReaders(inputPaths io.InputFilePaths, inputs []*idlCore.Literal, inputName string) []io.InputReader {
inputReaders := make([]io.InputReader, 0, len(inputs))
for i := 0; i < len(inputs); i++ {
inputReaders = append(inputReaders, NewStaticInputReader(inputPaths, &idlCore.LiteralMap{
Literals: map[string]*idlCore.Literal{
inputName: inputs[i],
},
}))
}
return inputReaders
}
func ConstructRemoteFileInputReaders(ctx context.Context, dataStore *storage.DataStore, inputPrefix storage.DataReference,
size int) ([]io.InputReader, error) {
inputReaders := make([]io.InputReader, 0, size)
for i := 0; i < size; i++ {
indexedInputLocation, err := dataStore.ConstructReference(ctx, inputPrefix, strconv.Itoa(i))
if err != nil {
return inputReaders, err
}
inputReader := ioutils.NewRemoteFileInputReader(ctx, dataStore, ioutils.NewInputFilePaths(ctx, dataStore, indexedInputLocation))
inputReaders = append(inputReaders, inputReader)
}
return inputReaders, nil
}
func ConstructOutputWriters(ctx context.Context, dataStore *storage.DataStore, outputPrefix, baseOutputSandbox storage.DataReference,
size int) ([]io.OutputWriter, error) {
outputWriters := make([]io.OutputWriter, 0, size)
for i := 0; i < size; i++ {
outputSandbox, err := dataStore.ConstructReference(ctx, baseOutputSandbox, strconv.Itoa(i))
if err != nil {
return nil, err
}
ow, err := ConstructOutputWriter(ctx, dataStore, outputPrefix, outputSandbox, i)
if err != nil {
return outputWriters, err
}
outputWriters = append(outputWriters, ow)
}
return outputWriters, nil
}
func ConstructOutputWriter(ctx context.Context, dataStore *storage.DataStore, outputPrefix, outputSandbox storage.DataReference,
index int) (io.OutputWriter, error) {
dataReference, err := dataStore.ConstructReference(ctx, outputPrefix, strconv.Itoa(index))
if err != nil {
return nil, err
}
// TODO when we fix https://github.com/flyteorg/flyte/issues/1276 we should make sure that the checkpoint paths are computed correctly
p := ioutils.NewCheckpointRemoteFilePaths(ctx, dataStore, dataReference, ioutils.NewRawOutputPaths(ctx, outputSandbox), "")
return ioutils.NewRemoteFileOutputWriter(ctx, dataStore, p), nil
}
func ConstructOutputReaders(ctx context.Context, dataStore *storage.DataStore, outputPrefix, baseOutputSandbox storage.DataReference,
size int) ([]io.OutputReader, error) {
outputReaders := make([]io.OutputReader, 0, size)
for i := 0; i < size; i++ {
reader, err := ConstructOutputReader(ctx, dataStore, outputPrefix, baseOutputSandbox, i)
if err != nil {
return nil, err
}
outputReaders = append(outputReaders, reader)
}
return outputReaders, nil
}
func ConstructOutputReader(ctx context.Context, dataStore *storage.DataStore, outputPrefix, baseOutputSandbox storage.DataReference,
index int) (io.OutputReader, error) {
strIndex := strconv.Itoa(index)
dataReference, err := dataStore.ConstructReference(ctx, outputPrefix, strIndex)
if err != nil {
return nil, err
}
outputSandbox, err := dataStore.ConstructReference(ctx, baseOutputSandbox, strIndex)
if err != nil {
return nil, err
}
// TODO when we fix https://github.com/flyteorg/flyte/issues/1276 we should make so that the checkpoint paths are computed correctly
outputPath := ioutils.NewCheckpointRemoteFilePaths(ctx, dataStore, dataReference, ioutils.NewRawOutputPaths(ctx, outputSandbox), "")
return ioutils.NewRemoteFileOutputReader(ctx, dataStore, outputPath, int64(999999999)), nil
}