-
Notifications
You must be signed in to change notification settings - Fork 674
/
plugin.go
335 lines (298 loc) · 10.9 KB
/
plugin.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
package databricks
import (
"bytes"
"context"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
flyteIdlCore "github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/plugins"
pluginErrors "github.com/flyteorg/flyte/flyteplugins/go/tasks/errors"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core"
pluginsCore "github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core/template"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/utils"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/webapi"
"github.com/flyteorg/flyte/flytestdlib/errors"
"github.com/flyteorg/flyte/flytestdlib/logger"
"github.com/flyteorg/flyte/flytestdlib/promutils"
)
const (
create string = "create"
get string = "get"
cancel string = "cancel"
databricksAPI string = "/api/2.1/jobs/runs"
newCluster string = "new_cluster"
dockerImage string = "docker_image"
sparkConfig string = "spark_conf"
sparkPythonTask string = "spark_python_task"
pythonFile string = "python_file"
parameters string = "parameters"
url string = "url"
)
// HTTPClient for mocking/testing purposes, and we'll override this method
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type Plugin struct {
metricScope promutils.Scope
cfg *Config
client HTTPClient
}
type ResourceWrapper struct {
StatusCode int
LifeCycleState string
ResultState string
JobID string
Message string
}
type ResourceMetaWrapper struct {
RunID string
DatabricksInstance string
Token string
}
func (p Plugin) GetConfig() webapi.PluginConfig {
return GetConfig().WebAPI
}
func (p Plugin) ResourceRequirements(_ context.Context, _ webapi.TaskExecutionContextReader) (
namespace core.ResourceNamespace, constraints core.ResourceConstraintsSpec, err error) {
// Resource requirements are assumed to be the same.
return "default", p.cfg.ResourceConstraints, nil
}
func (p Plugin) Create(ctx context.Context, taskCtx webapi.TaskExecutionContextReader) (webapi.ResourceMeta,
webapi.Resource, error) {
taskTemplate, err := taskCtx.TaskReader().Read(ctx)
if err != nil {
return nil, nil, err
}
token, err := taskCtx.SecretManager().Get(ctx, p.cfg.TokenKey)
if err != nil {
return nil, nil, err
}
container := taskTemplate.GetContainer()
sparkJob := plugins.SparkJob{}
err = utils.UnmarshalStruct(taskTemplate.GetCustom(), &sparkJob)
if err != nil {
return nil, nil, errors.Wrapf(pluginErrors.BadTaskSpecification, err, "invalid TaskSpecification [%v], failed to unmarshal", taskTemplate.GetCustom())
}
// override the default token in propeller
if len(sparkJob.DatabricksToken) != 0 {
token = sparkJob.DatabricksToken
}
modifiedArgs, err := template.Render(ctx, container.GetArgs(), template.Parameters{
TaskExecMetadata: taskCtx.TaskExecutionMetadata(),
Inputs: taskCtx.InputReader(),
OutputPath: taskCtx.OutputWriter(),
Task: taskCtx.TaskReader(),
})
if err != nil {
return nil, nil, err
}
databricksJob := make(map[string]interface{})
err = utils.UnmarshalStructToObj(sparkJob.DatabricksConf, &databricksJob)
if err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal databricksJob: %v: %v", sparkJob.DatabricksConf, err)
}
// If "existing_cluster_id" is in databricks_job, then we don't need to set "new_cluster"
// Refer the docs here: https://docs.databricks.com/en/workflows/jobs/jobs-2.0-api.html#request-structure
if clusterConfig, ok := databricksJob[newCluster].(map[string]interface{}); ok {
if dockerConfig, ok := clusterConfig[dockerImage].(map[string]interface{}); !ok || dockerConfig[url] == nil {
clusterConfig[dockerImage] = map[string]string{url: container.Image}
}
if clusterConfig[sparkConfig] == nil && len(sparkJob.SparkConf) != 0 {
clusterConfig[sparkConfig] = sparkJob.SparkConf
}
}
databricksJob[sparkPythonTask] = map[string]interface{}{pythonFile: p.cfg.EntrypointFile, parameters: modifiedArgs}
data, err := p.sendRequest(create, databricksJob, token, "")
if err != nil {
return nil, nil, err
}
if _, ok := data["run_id"]; !ok {
return nil, nil, errors.Errorf("CorruptedPluginState", "can't get the run_id")
}
runID := fmt.Sprintf("%.0f", data["run_id"])
return ResourceMetaWrapper{runID, p.cfg.DatabricksInstance, token}, nil, nil
}
func (p Plugin) Get(ctx context.Context, taskCtx webapi.GetContext) (latest webapi.Resource, err error) {
exec := taskCtx.ResourceMeta().(ResourceMetaWrapper)
res, err := p.sendRequest(get, nil, exec.Token, exec.RunID)
if err != nil {
return nil, err
}
if _, ok := res["state"]; !ok {
return nil, errors.Errorf("CorruptedPluginState", "can't get the job state")
}
jobState := res["state"].(map[string]interface{})
jobID := fmt.Sprintf("%.0f", res["job_id"])
message := fmt.Sprintf("%s", jobState["state_message"])
lifeCycleState := fmt.Sprintf("%s", jobState["life_cycle_state"])
var resultState string
if _, ok := jobState["result_state"]; !ok {
// The result_state is not available until the job is finished.
// https://docs.databricks.com/en/workflows/jobs/jobs-2.0-api.html#runresultstate
resultState = ""
} else {
resultState = fmt.Sprintf("%s", jobState["result_state"])
}
return ResourceWrapper{
JobID: jobID,
LifeCycleState: lifeCycleState,
ResultState: resultState,
Message: message,
}, nil
}
func (p Plugin) Delete(ctx context.Context, taskCtx webapi.DeleteContext) error {
if taskCtx.ResourceMeta() == nil {
return nil
}
exec := taskCtx.ResourceMeta().(ResourceMetaWrapper)
_, err := p.sendRequest(cancel, nil, exec.Token, exec.RunID)
if err != nil {
return err
}
logger.Info(ctx, "Deleted Databricks job execution.")
return nil
}
func (p Plugin) sendRequest(method string, databricksJob map[string]interface{}, token string, runID string) (map[string]interface{}, error) {
var databricksURL string
// for mocking/testing purposes
if p.cfg.databricksEndpoint == "" {
databricksURL = fmt.Sprintf("https://%v%v", p.cfg.DatabricksInstance, databricksAPI)
} else {
databricksURL = fmt.Sprintf("%v%v", p.cfg.databricksEndpoint, databricksAPI)
}
// build the request spec
var body io.Reader
var httpMethod string
switch method {
case create:
databricksURL += "/submit"
mJSON, err := json.Marshal(databricksJob)
if err != nil {
return nil, fmt.Errorf("failed to marshal the job request: %v", err)
}
body = bytes.NewBuffer(mJSON)
httpMethod = http.MethodPost
case get:
databricksURL += "/get?run_id=" + runID
httpMethod = http.MethodGet
case cancel:
databricksURL += "/cancel"
body = bytes.NewBuffer([]byte(fmt.Sprintf("{ \"run_id\": %v }", runID)))
httpMethod = http.MethodPost
}
req, err := http.NewRequest(httpMethod, databricksURL, body)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/json")
// Send the request
resp, err := p.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request to Databricks platform with err: [%v]", err)
}
defer resp.Body.Close()
// Parse the response body
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var data map[string]interface{}
err = json.Unmarshal(responseBody, &data)
if err != nil {
return nil, fmt.Errorf("failed to parse response with err: [%v]", err)
}
if resp.StatusCode != http.StatusOK {
message := ""
if v, ok := data["message"]; ok {
message = v.(string)
}
return nil, fmt.Errorf("failed to %v Databricks job with error [%v]", method, message)
}
return data, nil
}
func (p Plugin) Status(ctx context.Context, taskCtx webapi.StatusContext) (phase core.PhaseInfo, err error) {
exec := taskCtx.ResourceMeta().(ResourceMetaWrapper)
resource := taskCtx.Resource().(ResourceWrapper)
message := resource.Message
jobID := resource.JobID
lifeCycleState := resource.LifeCycleState
resultState := resource.ResultState
taskInfo := createTaskInfo(exec.RunID, jobID, exec.DatabricksInstance)
switch lifeCycleState {
// Job response format. https://docs.databricks.com/en/workflows/jobs/jobs-2.0-api.html#runlifecyclestate
case "PENDING":
return core.PhaseInfoInitializing(time.Now(), core.DefaultPhaseVersion, message, taskInfo), nil
case "RUNNING":
fallthrough
case "TERMINATING":
return core.PhaseInfoRunning(core.DefaultPhaseVersion, taskInfo), nil
case "TERMINATED":
if resultState == "SUCCESS" {
// Result state details. https://docs.databricks.com/en/workflows/jobs/jobs-2.0-api.html#runresultstate
if err := writeOutput(ctx, taskCtx); err != nil {
return core.PhaseInfoFailure(string(rune(http.StatusInternalServerError)), "failed to write output", taskInfo), nil
}
return core.PhaseInfoSuccess(taskInfo), nil
}
return core.PhaseInfoFailure(pluginErrors.TaskFailedWithError, message, taskInfo), nil
case "SKIPPED":
return core.PhaseInfoFailure(string(rune(http.StatusConflict)), message, taskInfo), nil
case "INTERNAL_ERROR":
return core.PhaseInfoFailure(string(rune(http.StatusInternalServerError)), message, taskInfo), nil
}
return core.PhaseInfoUndefined, pluginErrors.Errorf(pluginsCore.SystemErrorCode, "unknown execution phase [%v].", lifeCycleState)
}
func writeOutput(ctx context.Context, taskCtx webapi.StatusContext) error {
taskTemplate, err := taskCtx.TaskReader().Read(ctx)
if err != nil {
return err
}
if taskTemplate.Interface == nil || taskTemplate.Interface.Outputs == nil || taskTemplate.Interface.Outputs.Variables == nil {
logger.Infof(ctx, "The task declares no outputs. Skipping writing the outputs.")
return nil
}
outputReader := ioutils.NewRemoteFileOutputReader(ctx, taskCtx.DataStore(), taskCtx.OutputWriter(), taskCtx.MaxDatasetSizeBytes())
return taskCtx.OutputWriter().Put(ctx, outputReader)
}
func createTaskInfo(runID, jobID, databricksInstance string) *core.TaskInfo {
timeNow := time.Now()
return &core.TaskInfo{
OccurredAt: &timeNow,
Logs: []*flyteIdlCore.TaskLog{
{
Uri: fmt.Sprintf("https://%s/#job/%s/run/%s",
databricksInstance,
jobID,
runID),
Name: "Databricks Console",
},
},
}
}
func newDatabricksJobTaskPlugin() webapi.PluginEntry {
return webapi.PluginEntry{
ID: "databricks",
SupportedTaskTypes: []core.TaskType{"spark"},
PluginLoader: func(ctx context.Context, iCtx webapi.PluginSetupContext) (webapi.AsyncPlugin, error) {
return &Plugin{
metricScope: iCtx.MetricsScope(),
cfg: GetConfig(),
client: &http.Client{},
}, nil
},
}
}
func init() {
gob.Register(ResourceMetaWrapper{})
gob.Register(ResourceWrapper{})
pluginmachinery.PluginRegistry().RegisterRemotePlugin(newDatabricksJobTaskPlugin())
}