-
Notifications
You must be signed in to change notification settings - Fork 353
/
commands.go
208 lines (196 loc) · 6.28 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
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
package service
import (
"encoding/json"
"errors"
"fmt"
"github.com/databrickslabs/databricks-terraform/client/model"
"log"
"net/http"
"time"
)
// CommandsAPI exposes the Context & Commands API
type CommandsAPI struct {
Client DBApiClient
}
// Execute creates a spark context and executes a command and then closes context
func (a CommandsAPI) Execute(clusterID, langauge, commandStr string) (model.Command, error) {
var resp model.Command
context, err := a.createContext(langauge, clusterID)
if err != nil {
return resp, err
}
err = a.waitForContextReady(context, clusterID, 1, 10)
if err != nil {
return resp, err
}
commandID, err := a.createCommand(context, clusterID, langauge, commandStr)
if err != nil {
return resp, err
}
err = a.waitForCommandFinished(commandID, context, clusterID, 5, 10)
if err != nil {
return resp, err
}
command, err := a.getCommand(commandID, context, clusterID)
if err != nil {
return resp, err
}
err = a.deleteContext(context, clusterID)
return command, err
}
func (a CommandsAPI) createCommand(contextID, clusterID, language, commandStr string) (string, error) {
var command struct {
ID string `json:"id,omitempty"`
}
commandRequest := struct {
Language string `json:"language,omitempty"`
ClusterID string `json:"clusterId,omitempty"`
ContextID string `json:"contextId,omitempty"`
Command string `json:"command,omitempty"`
}{
Language: language,
ClusterID: clusterID,
ContextID: contextID,
Command: commandStr,
}
resp, err := a.Client.performQuery(http.MethodPost, "/commands/execute", "1.2", nil, commandRequest, nil)
if err != nil {
return command.ID, err
}
err = json.Unmarshal(resp, &command)
return command.ID, err
}
func (a CommandsAPI) getCommand(commandID, contextID, clusterID string) (model.Command, error) {
var commandResp model.Command
contextGetRequest := struct {
CommandID string `json:"commandId,omitempty" url:"commandId,omitempty"`
ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"`
ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"`
}{
CommandID: commandID,
ContextID: contextID,
ClusterID: clusterID,
}
resp, err := a.Client.performQuery(http.MethodGet, "/commands/status", "1.2", nil, contextGetRequest, nil)
if err != nil {
return commandResp, err
}
err = json.Unmarshal(resp, &commandResp)
return commandResp, err
}
func (a CommandsAPI) waitForCommandFinished(commandID, contextID, clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error {
errChan := make(chan error, 1)
go func() {
for {
commandInfo, err := a.getCommand(commandID, contextID, clusterID)
if err != nil {
errChan <- err
return
}
if commandInfo.Status == "Finished" {
errChan <- nil
return
} else if commandInfo.Status == "Cancelling" || commandInfo.Status == "Cancelled" || commandInfo.Status == "Error" {
errChan <- fmt.Errorf("context is in a failure state: %s", commandInfo.Status)
return
}
log.Println(fmt.Sprintf("Waiting for command to finish, current state is: %s.", commandInfo.Status))
time.Sleep(sleepDurationSeconds * time.Second)
}
}()
select {
case err := <-errChan:
return err
case <-time.After(timeoutDurationMinutes * time.Minute):
return errors.New("Timed out context has not reached running state")
}
}
// Commented till this will be used
//func (a CommandsAPI) deleteCommand(commandID, contextID, clusterID string) error {
// contextDeleteRequest := struct {
// CommandID string `json:"commandId,omitempty" url:"commandId,omitempty"`
// ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"`
// ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"`
// }{
// CommandID: commandID,
// ContextID: contextID,
// ClusterID: clusterID,
// }
// _, err := a.Client.performQuery(http.MethodPost, "/commands/cancel", "1.2", nil, contextDeleteRequest, nil)
// return err
//}
func (a CommandsAPI) deleteContext(contextID, clusterID string) error {
contextDeleteRequest := struct {
ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"`
ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"`
}{
ContextID: contextID,
ClusterID: clusterID,
}
_, err := a.Client.performQuery(http.MethodPost, "/contexts/destroy", "1.2", nil, contextDeleteRequest, nil)
return err
}
func (a CommandsAPI) waitForContextReady(contextID, clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error {
errChan := make(chan error, 1)
go func() {
for {
status, err := a.getContext(contextID, clusterID)
if err != nil {
errChan <- err
return
}
if status == "Running" {
errChan <- nil
return
} else if status == "Error" {
errChan <- errors.New("context is in a errored state")
return
}
log.Println("Waiting for context to go to running, current state is pending.")
time.Sleep(sleepDurationSeconds * time.Second)
}
}()
select {
case err := <-errChan:
return err
case <-time.After(timeoutDurationMinutes * time.Minute):
return errors.New("Timed out context has not reached running state")
}
}
func (a CommandsAPI) getContext(contextID, clusterID string) (string, error) {
var contextStatus struct {
ID string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
}
contextGetRequest := struct {
ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"`
ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"`
}{
ContextID: contextID,
ClusterID: clusterID,
}
resp, err := a.Client.performQuery(http.MethodGet, "/contexts/status", "1.2", nil, contextGetRequest, nil)
if err != nil {
return contextStatus.Status, err
}
err = json.Unmarshal(resp, &contextStatus)
return contextStatus.Status, err
}
func (a CommandsAPI) createContext(language, clusterID string) (string, error) {
var context struct {
ID string `json:"id,omitempty"`
}
contextRequest := struct {
Language string `json:"language,omitempty"`
ClusterID string `json:"clusterId,omitempty"`
}{
Language: language,
ClusterID: clusterID,
}
resp, err := a.Client.performQuery(http.MethodPost, "/contexts/create", "1.2", nil, contextRequest, nil)
if err != nil {
return context.ID, err
}
err = json.Unmarshal(resp, &context)
return context.ID, err
}