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 60
/
taskexec_context.go
160 lines (129 loc) · 4.61 KB
/
taskexec_context.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
package task
import (
"bytes"
"context"
"strconv"
"github.com/lyft/flytepropeller/pkg/controller/nodes/task/resourcemanager"
"github.com/lyft/flytestdlib/logger"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
pluginCatalog "github.com/lyft/flyteplugins/go/tasks/pluginmachinery/catalog"
pluginCore "github.com/lyft/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/lyft/flytepropeller/pkg/controller/nodes/errors"
"github.com/lyft/flytepropeller/pkg/controller/nodes/handler"
"github.com/lyft/flytepropeller/pkg/utils"
)
var (
_ pluginCore.TaskExecutionContext = &taskExecutionContext{}
)
const IDMaxLength = 50
type taskExecutionID struct {
execName string
id *core.TaskExecutionIdentifier
}
func (te taskExecutionID) GetID() core.TaskExecutionIdentifier {
return *te.id
}
func (te taskExecutionID) GetGeneratedName() string {
return te.execName
}
type taskExecutionMetadata struct {
handler.NodeExecutionMetadata
taskExecID taskExecutionID
o pluginCore.TaskOverrides
}
func (t taskExecutionMetadata) GetTaskExecutionID() pluginCore.TaskExecutionID {
return t.taskExecID
}
func (t taskExecutionMetadata) GetOverrides() pluginCore.TaskOverrides {
return t.o
}
type taskExecutionContext struct {
handler.NodeExecutionContext
tm taskExecutionMetadata
rm pluginCore.ResourceManager
psm *pluginStateManager
tr handler.TaskReader
ow *ioutils.BufferedOutputWriter
ber *bufferedEventRecorder
sm pluginCore.SecretManager
c pluginCatalog.AsyncClient
}
func (t *taskExecutionContext) TaskRefreshIndicator() pluginCore.SignalAsync {
return func(ctx context.Context) {
err := t.NodeExecutionContext.EnqueueOwnerFunc()
if err != nil {
logger.Errorf(ctx, "Failed to enqueue owner for Task [%v] and Owner [%v]. Error: %v",
t.TaskExecutionMetadata().GetTaskExecutionID(),
t.TaskExecutionMetadata().GetOwnerID(),
err)
}
}
}
func (t *taskExecutionContext) Catalog() pluginCatalog.AsyncClient {
return t.c
}
func (t taskExecutionContext) EventsRecorder() pluginCore.EventsRecorder {
return t.ber
}
func (t taskExecutionContext) ResourceManager() pluginCore.ResourceManager {
return t.rm
}
func (t taskExecutionContext) PluginStateReader() pluginCore.PluginStateReader {
return t.psm
}
func (t *taskExecutionContext) TaskReader() pluginCore.TaskReader {
return t.tr
}
func (t *taskExecutionContext) TaskExecutionMetadata() pluginCore.TaskExecutionMetadata {
return t.tm
}
func (t *taskExecutionContext) OutputWriter() io.OutputWriter {
return t.ow
}
func (t *taskExecutionContext) PluginStateWriter() pluginCore.PluginStateWriter {
return t.psm
}
func (t taskExecutionContext) SecretManager() pluginCore.SecretManager {
return t.sm
}
func (t *Handler) newTaskExecutionContext(ctx context.Context, nCtx handler.NodeExecutionContext, pluginID string) (*taskExecutionContext, error) {
id := GetTaskExecutionIdentifier(nCtx)
uniqueID, err := utils.FixedLengthUniqueIDForParts(IDMaxLength, nCtx.NodeExecutionMetadata().GetOwnerID().Name, nCtx.NodeID(), strconv.Itoa(int(id.RetryAttempt)))
if err != nil {
// SHOULD never really happen
return nil, err
}
outputSandbox, err := ioutils.NewShardedRawOutputPath(ctx, nCtx.OutputShardSelector(), nCtx.RawOutputPrefix(), uniqueID, nCtx.DataStore())
if err != nil {
return nil, errors.Wrapf(errors.StorageError, nCtx.NodeID(), err, "failed to create output sandbox for node execution")
}
ow := ioutils.NewBufferedOutputWriter(ctx, ioutils.NewRemoteFileOutputPaths(ctx, nCtx.DataStore(), nCtx.NodeStatus().GetOutputDir(), outputSandbox))
ts := nCtx.NodeStateReader().GetTaskNodeState()
var b *bytes.Buffer
if ts.PluginState != nil {
b = bytes.NewBuffer(ts.PluginState)
}
psm, err := newPluginStateManager(ctx, GobCodecVersion, ts.PluginStateVersion, b)
if err != nil {
return nil, errors.Wrapf(errors.RuntimeExecutionError, nCtx.NodeID(), err, "unable to initialize plugin state manager")
}
resourceNamespacePrefix := pluginCore.ResourceNamespace(t.resourceManager.GetID()).CreateSubNamespace(pluginCore.ResourceNamespace(pluginID))
return &taskExecutionContext{
NodeExecutionContext: nCtx,
tm: taskExecutionMetadata{
NodeExecutionMetadata: nCtx.NodeExecutionMetadata(),
taskExecID: taskExecutionID{execName: uniqueID, id: id},
o: nCtx.Node(),
},
rm: resourcemanager.GetTaskResourceManager(
t.resourceManager, resourceNamespacePrefix, id),
psm: psm,
tr: nCtx.TaskReader(),
ow: ow,
ber: newBufferedEventRecorder(),
c: t.asyncCatalog,
sm: t.secretManager,
}, nil
}