-
Notifications
You must be signed in to change notification settings - Fork 41
/
commands.go
159 lines (147 loc) · 4.34 KB
/
commands.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
package compute
import (
"context"
"fmt"
"github.com/databricks/databricks-sdk-go/client"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/useragent"
)
type CommandExecutorV2 struct {
clustersAPI *ClustersAPI
executionAPI *CommandExecutionAPI
language Language
clusterID string
contextID string
}
// Start the command execution context on a cluster and ensure it transitions to a running state
func (a *CommandExecutionAPI) Start(ctx context.Context, clusterID string, language Language) (*CommandExecutorV2, error) {
executionImpl, ok := a.impl.(*commandExecutionImpl)
if !ok {
return nil, fmt.Errorf("unknown command execution implementation: %v", a.impl)
}
// re-initializing clusters API here, so that we have less IF's in the generator for WorkspaceClient
clustersAPI := NewClusters(executionImpl.client)
err := clustersAPI.EnsureClusterIsRunning(ctx, clusterID)
if err != nil {
return nil, err
}
wait, err := a.Create(ctx, CreateContext{
ClusterId: clusterID,
Language: language,
})
if err != nil {
return nil, err
}
commandContext, err := wait.Get()
if err != nil {
return nil, err
}
return &CommandExecutorV2{
clustersAPI: clustersAPI,
executionAPI: a,
language: language,
clusterID: clusterID,
contextID: commandContext.Id,
}, nil
}
func (c *CommandExecutorV2) Destroy(ctx context.Context) error {
return c.executionAPI.Destroy(ctx, DestroyContext{
ClusterId: c.clusterID,
ContextId: c.contextID,
})
}
// Execute runs given command in the running cluster and context and waits for the results
func (c *CommandExecutorV2) Execute(ctx context.Context, command string) (*Results, error) {
wait, err := c.executionAPI.Execute(ctx, Command{
Command: TrimLeadingWhitespace(command),
ClusterId: c.clusterID,
ContextId: c.contextID,
Language: c.language,
})
if err != nil {
return nil, err
}
status, err := wait.Get()
if err != nil {
return nil, err
}
return status.Results, nil
}
// CommandExecutor creates a spark context and executes a command and then closes context
type CommandExecutor interface {
Execute(ctx context.Context, clusterID, language, commandStr string) Results
}
// CommandMock mocks the execution of command
type CommandMock func(commandStr string) Results
func (m CommandMock) Execute(_ context.Context, _, _, commandStr string) Results {
return m(commandStr)
}
func NewCommandExecutor(client *client.DatabricksClient) CommandExecutor {
return &CommandsHighLevelAPI{
clusters: NewClusters(client),
execution: NewCommandExecution(client),
}
}
// CommandsHighLevelAPI exposes more friendly wrapper over command execution
type CommandsHighLevelAPI struct {
clusters *ClustersAPI
execution *CommandExecutionAPI
}
// Execute creates a spark context and executes a command and then closes context
// Any leading whitespace is trimmed
//
// Deprecated: please switch to CommandExecutorV2
func (a *CommandsHighLevelAPI) Execute(ctx context.Context, clusterID, language, commandStr string) Results {
ctx = useragent.InContext(ctx, "sdk-feature", "command-execution")
cluster, err := a.clusters.Get(ctx, GetClusterRequest{
ClusterId: clusterID,
})
if err != nil {
return Results{
ResultType: "error",
Summary: err.Error(),
}
}
if !cluster.IsRunningOrResizing() {
return Results{
ResultType: "error",
Summary: fmt.Sprintf("Cluster %s has to be running or resizing, but is %s", clusterID, cluster.State),
}
}
commandStr = TrimLeadingWhitespace(commandStr)
logger.Debugf(ctx, "Executing %s command on %s:\n%s", language, clusterID, commandStr)
context, err := a.execution.CreateAndWait(ctx, CreateContext{
ClusterId: clusterID,
Language: Language(language),
})
if err != nil {
return Results{
ResultType: "error",
Summary: err.Error(),
}
}
defer a.execution.Destroy(ctx, DestroyContext{
ClusterId: clusterID,
ContextId: context.Id,
})
command, err := a.execution.ExecuteAndWait(ctx, Command{
ClusterId: clusterID,
ContextId: context.Id,
Language: Language(language),
Command: commandStr,
})
if err != nil {
return Results{
ResultType: "error",
Summary: err.Error(),
}
}
if command.Results == nil {
logger.Warnf(ctx, "Command has no results: %#v", command)
return Results{
ResultType: "error",
Summary: "Command has no results",
}
}
return *command.Results
}