This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
transformer.go
263 lines (221 loc) · 9.79 KB
/
transformer.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
package datacatalog
import (
"context"
"encoding/base64"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/datacatalog"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/catalog"
"github.com/flyteorg/flytepropeller/pkg/compiler/validators"
"github.com/flyteorg/flytestdlib/pbhash"
)
const cachedTaskTag = "flyte_cached"
const taskNamespace = "flyte_task"
const maxParamHashLength = 8
// Declare the definition of empty literal and variable maps. This is important because we hash against
// the literal and variable maps. So Nil and empty literals and variable maps should translate to these defintions
// in order to have a consistent hash.
var emptyLiteralMap = core.LiteralMap{Literals: map[string]*core.Literal{}}
var emptyVariableMap = core.VariableMap{Variables: map[string]*core.Variable{}}
func getDatasetNameFromTask(taskID core.Identifier) string {
return fmt.Sprintf("%s-%s", taskNamespace, taskID.Name)
}
// Transform the artifact Data into task execution outputs as a literal map
func GenerateTaskOutputsFromArtifact(id core.Identifier, taskInterface core.TypedInterface, artifact *datacatalog.Artifact) (*core.LiteralMap, error) {
// if there are no outputs in the task, return empty map
if taskInterface.Outputs == nil || len(taskInterface.Outputs.Variables) == 0 {
return &emptyLiteralMap, nil
}
outputVariables := taskInterface.Outputs.Variables
artifactDataList := artifact.Data
// verify the task outputs matches what is stored in ArtifactData
if len(outputVariables) != len(artifactDataList) {
return nil, fmt.Errorf("the task %s with %d outputs, should have %d artifactData for artifact %s", id.String(), len(outputVariables), len(artifactDataList), artifact.Id)
}
outputs := make(map[string]*core.Literal, len(artifactDataList))
for _, artifactData := range artifactDataList {
// verify that the name and type of artifactData matches what is expected from the interface
if _, ok := outputVariables[artifactData.Name]; !ok {
return nil, fmt.Errorf("unexpected artifactData with name [%v] does not match any task output variables %v", artifactData.Name, reflect.ValueOf(outputVariables).MapKeys())
}
expectedVarType := outputVariables[artifactData.Name].GetType()
inputType := validators.LiteralTypeForLiteral(artifactData.Value)
if !validators.AreTypesCastable(inputType, expectedVarType) {
return nil, fmt.Errorf("unexpected artifactData: [%v] type: [%v] does not match any task output type: [%v]", artifactData.Name, inputType, expectedVarType)
}
outputs[artifactData.Name] = artifactData.Value
}
return &core.LiteralMap{Literals: outputs}, nil
}
func generateDataSetVersionFromTask(ctx context.Context, taskInterface core.TypedInterface, cacheVersion string) (string, error) {
signatureHash, err := generateTaskSignatureHash(ctx, taskInterface)
if err != nil {
return "", err
}
cacheVersion = strings.Trim(cacheVersion, " ")
if len(cacheVersion) == 0 {
return "", fmt.Errorf("task cannot have an empty discoveryVersion %v", cacheVersion)
}
return fmt.Sprintf("%s-%s", cacheVersion, signatureHash), nil
}
func generateTaskSignatureHash(ctx context.Context, taskInterface core.TypedInterface) (string, error) {
taskInputs := &emptyVariableMap
taskOutputs := &emptyVariableMap
if taskInterface.Inputs != nil && len(taskInterface.Inputs.Variables) != 0 {
taskInputs = taskInterface.Inputs
}
if taskInterface.Outputs != nil && len(taskInterface.Outputs.Variables) != 0 {
taskOutputs = taskInterface.Outputs
}
inputHash, err := pbhash.ComputeHash(ctx, taskInputs)
if err != nil {
return "", err
}
outputHash, err := pbhash.ComputeHash(ctx, taskOutputs)
if err != nil {
return "", err
}
inputHashString := base64.RawURLEncoding.EncodeToString(inputHash)
if len(inputHashString) > maxParamHashLength {
inputHashString = inputHashString[0:maxParamHashLength]
}
outputHashString := base64.RawURLEncoding.EncodeToString(outputHash)
if len(outputHashString) > maxParamHashLength {
outputHashString = outputHashString[0:maxParamHashLength]
}
return fmt.Sprintf("%v-%v", inputHashString, outputHashString), nil
}
// Generate a tag by hashing the input values
func GenerateArtifactTagName(ctx context.Context, inputs *core.LiteralMap) (string, error) {
if inputs == nil || len(inputs.Literals) == 0 {
inputs = &emptyLiteralMap
}
inputsHash, err := pbhash.ComputeHash(ctx, inputs)
if err != nil {
return "", err
}
hashString := base64.RawURLEncoding.EncodeToString(inputsHash)
tag := fmt.Sprintf("%s-%s", cachedTaskTag, hashString)
return tag, nil
}
// Get the DataSetID for a task.
// NOTE: the version of the task is a combination of both the discoverable_version and the task signature.
// This is because the interface may of changed even if the discoverable_version hadn't.
func GenerateDatasetIDForTask(ctx context.Context, k catalog.Key) (*datacatalog.DatasetID, error) {
datasetVersion, err := generateDataSetVersionFromTask(ctx, k.TypedInterface, k.CacheVersion)
if err != nil {
return nil, err
}
datasetID := &datacatalog.DatasetID{
Project: k.Identifier.Project,
Domain: k.Identifier.Domain,
Name: getDatasetNameFromTask(k.Identifier),
Version: datasetVersion,
}
return datasetID, nil
}
func DatasetIDToIdentifier(id *datacatalog.DatasetID) *core.Identifier {
if id == nil {
return nil
}
return &core.Identifier{ResourceType: core.ResourceType_DATASET, Name: id.Name, Project: id.Project, Domain: id.Domain, Version: id.Version}
}
// With Node-Node relationship this is bound to change. So lets keep it extensible
const (
taskVersionKey = "task-version"
execNameKey = "execution-name"
execDomainKey = "exec-domain"
execProjectKey = "exec-project"
execNodeIDKey = "exec-node"
execTaskAttemptKey = "exec-attempt"
)
// Understanding Catalog Identifiers
// DatasetID represents the ID of the dataset. For Flyte this represents the ID of the generating task and the version calculated as the hash of the interface & cache version. refer to `GenerateDatasetIDForTask`
// TaskID is the same as the DatasetID + name: (DataSetID - namespace) + task version which is stored in the metadata
// ExecutionID is stored only in the metadata (project and domain available after Jul-2020)
// NodeExecID = Execution ID + Node ID (available after Jul-2020)
// TaskExecID is the same as the NodeExecutionID + attempt (attempt is available in Metadata) after Jul-2020
func GetDatasetMetadataForSource(taskExecutionID *core.TaskExecutionIdentifier) *datacatalog.Metadata {
if taskExecutionID == nil {
return &datacatalog.Metadata{}
}
return &datacatalog.Metadata{
KeyMap: map[string]string{
taskVersionKey: taskExecutionID.TaskId.Version,
},
}
}
func GetArtifactMetadataForSource(taskExecutionID *core.TaskExecutionIdentifier) *datacatalog.Metadata {
if taskExecutionID == nil {
return &datacatalog.Metadata{}
}
return &datacatalog.Metadata{
KeyMap: map[string]string{
execProjectKey: taskExecutionID.NodeExecutionId.GetExecutionId().GetProject(),
execDomainKey: taskExecutionID.NodeExecutionId.GetExecutionId().GetDomain(),
execNameKey: taskExecutionID.NodeExecutionId.GetExecutionId().GetName(),
execNodeIDKey: taskExecutionID.NodeExecutionId.GetNodeId(),
execTaskAttemptKey: strconv.Itoa(int(taskExecutionID.GetRetryAttempt())),
},
}
}
// Returns the Source TaskExecutionIdentifier from the catalog metadata
// For all the information not available it returns Unknown. This is because as of July-2020 Catalog does not have all
// the information. After the first deployment of this code, it will have this and the "unknown's" can be phased out
func GetSourceFromMetadata(datasetMd, artifactMd *datacatalog.Metadata, currentID core.Identifier) *core.TaskExecutionIdentifier {
if datasetMd == nil || datasetMd.KeyMap == nil {
datasetMd = &datacatalog.Metadata{KeyMap: map[string]string{}}
}
if artifactMd == nil || artifactMd.KeyMap == nil {
artifactMd = &datacatalog.Metadata{KeyMap: map[string]string{}}
}
// Jul-06-2020 DataCatalog stores only wfExecutionKey & taskVersionKey So we will default the project / domain to the current dataset's project domain
attempt, _ := strconv.Atoi(GetOrDefault(artifactMd.KeyMap, execTaskAttemptKey, "0"))
return &core.TaskExecutionIdentifier{
TaskId: &core.Identifier{
ResourceType: currentID.ResourceType,
Project: currentID.Project,
Domain: currentID.Domain,
Name: currentID.Name,
Version: GetOrDefault(datasetMd.KeyMap, taskVersionKey, "unknown"),
},
RetryAttempt: uint32(attempt),
NodeExecutionId: &core.NodeExecutionIdentifier{
NodeId: GetOrDefault(artifactMd.KeyMap, execNodeIDKey, "unknown"),
ExecutionId: &core.WorkflowExecutionIdentifier{
Project: GetOrDefault(artifactMd.KeyMap, execProjectKey, currentID.GetProject()),
Domain: GetOrDefault(artifactMd.KeyMap, execDomainKey, currentID.GetDomain()),
Name: GetOrDefault(artifactMd.KeyMap, execNameKey, "unknown"),
},
},
}
}
// Given the Catalog Information (returned from a Catalog call), returns the CatalogMetadata that is populated in the event.
func EventCatalogMetadata(datasetID *datacatalog.DatasetID, tag *datacatalog.Tag, sourceID *core.TaskExecutionIdentifier) *core.CatalogMetadata {
md := &core.CatalogMetadata{
DatasetId: DatasetIDToIdentifier(datasetID),
}
if tag != nil {
md.ArtifactTag = &core.CatalogArtifactTag{
ArtifactId: tag.ArtifactId,
Name: tag.Name,
}
}
if sourceID != nil {
md.SourceExecution = &core.CatalogMetadata_SourceTaskExecution{
SourceTaskExecution: sourceID,
}
}
return md
}
// Returns a default value, if the given key is not found in the map, else returns the value in the map
func GetOrDefault(m map[string]string, key, defaultValue string) string {
v, ok := m[key]
if !ok {
return defaultValue
}
return v
}