-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
356 lines (282 loc) · 7.61 KB
/
session.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package livy
import (
"fmt"
"io"
"net/http"
)
const (
SessionKind_Spark = SessionKind("spark")
SessionKind_PySpark = SessionKind("pyspark")
SessionKind_SparkR = SessionKind("sparkr")
SessionKind_Sql = SessionKind("sql")
)
var (
SessionKindList = []SessionKind{
SessionKind_Spark,
SessionKind_PySpark,
SessionKind_SparkR,
SessionKind_Sql,
}
)
const (
SessionState_NotStarted = SessionState("not_started")
SessionState_Starting = SessionState("starting")
SessionState_Idle = SessionState("idle")
SessionState_Busy = SessionState("busy")
SessionState_ShuttingDown = SessionState("shutting_down")
SessionState_Error = SessionState("error")
SessionState_Dead = SessionState("dead")
SessionState_Killed = SessionState("killed")
SessionState_Success = SessionState("success")
)
type SessionKind string
type SessionState string
type SessionsService struct {
s *Service
}
type Sessions struct {
From int `json:"from"`
Size int `json:"size"`
Items []*Session `json:"sessions"`
}
type SessionsListCall struct {
s *Service
}
type Session struct {
ID int `json:"id"`
AppID string `json:"appId"`
Owner string `json:"owner"`
ProxyUser string `json:"proxyUser"`
Kind SessionKind `json:"kind"`
Log []string `json:"log"`
State SessionState `json:"state"`
AppInfo map[string]string `json:"appInfo"`
// Not officially https://livy.incubator.apache.org/docs/latest/rest-api.html#session
Name string `json:"name"`
// error message
Message string `json:"msg"`
}
func NewSessionsService(s *Service) *SessionsService {
rs := &SessionsService{s: s}
return rs
}
func (r *SessionsService) List() *SessionsListCall {
c := &SessionsListCall{s: r.s}
return c
}
func (c *SessionsListCall) Do() (*Sessions, error) {
res, err := c.doRequest()
if err != nil {
return nil, err
}
sessions := &Sessions{}
err = DecodeResponse(sessions, res)
if err != nil {
return nil, err
}
return sessions, nil
}
func (c *SessionsListCall) doRequest() (*http.Response, error) {
url := c.s.BasePath + "/sessions"
var body io.Reader = nil
req, err := http.NewRequest("GET", url, body)
if err != nil {
return nil, err
}
return SendRequest(c.s.client, req)
}
type SessionsGetCall struct {
s *Service
sessionID int
}
func (r *SessionsService) Get(sessionID int) *SessionsGetCall {
c := &SessionsGetCall{s: r.s}
c.sessionID = sessionID
return c
}
func (c *SessionsGetCall) Do() (*Session, error) {
res, err := c.doRequest()
if err != nil {
return nil, err
}
defer res.Body.Close()
session := &Session{}
err = DecodeResponse(session, res)
if err != nil {
return nil, err
}
return session, nil
}
func (c *SessionsGetCall) doRequest() (*http.Response, error) {
url := c.s.BasePath + fmt.Sprintf("/sessions/%v", c.sessionID)
var body io.Reader = nil
req, err := http.NewRequest("GET", url, body)
if err != nil {
return nil, err
}
return SendRequest(c.s.client, req)
}
type InsertSessionRequest struct {
// The name of this session
Name string `json:"name,omitempty"`
// The session kind
Kind SessionKind `json:"kind"`
// User to impersonate when starting the session
ProxyUser string `json:"proxyUser,omitempty"`
// jars to be used in this session
Jars []string `json:"jars,omitempty"`
// Python files to be used in this session
PyFiles []string `json:"pyFiles,omitempty"`
// files to be used in this session
Files []string `json:"files,omitempty"`
// Amount of memory to use for the driver process
DriverMemory string `json:"driverMemory,omitempty"`
// Number of cores to use for the driver process
DriverCores int `json:"driverCores,omitempty"`
// Amount of memory to use per executor process
ExecutorMemory string `json:"executorMemory,omitempty"`
// Number of cores to use for each executor
ExecutorCores int `json:"executorCores,omitempty"`
// Number of executors to launch for this session
NumExecutors int `json:"numExecutors,omitempty"`
// Archives to be used in this session
Archives []string `json:"archives,omitempty"`
// The name of the YARN queue to which submitted
Queue string `json:"queue,omitempty"`
// Spark configuration properties
Conf map[string]string `json:"conf,omitempty"`
// Timeout in second to which session be orphaned
HeartbeatTimeoutInSecond int `json:"heartbeatTimeoutInSecond,omitempty"`
}
type SessionsInsertCall struct {
s *Service
insertSessionRequest *InsertSessionRequest
}
// Insert: Creates a new session.
func (r *SessionsService) Insert(insertSessionRequest *InsertSessionRequest) *SessionsInsertCall {
c := &SessionsInsertCall{s: r.s}
c.insertSessionRequest = insertSessionRequest
return c
}
func (c *SessionsInsertCall) Do() (*Session, error) {
res, err := c.doRequest()
if err != nil {
return nil, err
}
session := &Session{}
err = DecodeResponse(session, res)
if err != nil {
return nil, err
}
return session, nil
}
func (c *SessionsInsertCall) doRequest() (*http.Response, error) {
url := c.s.BasePath + "/sessions"
body, err := JSONReader(c.insertSessionRequest)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
return SendRequest(c.s.client, req)
}
type SessionsDeleteCall struct {
s *Service
sessionID int
}
func (r *SessionsService) Delete(sessionID int) *SessionsDeleteCall {
c := &SessionsDeleteCall{s: r.s}
c.sessionID = sessionID
return c
}
func (c *SessionsDeleteCall) Do() error {
res, err := c.doRequest()
if err != nil {
return err
}
defer res.Body.Close()
return nil
}
func (c *SessionsDeleteCall) doRequest() (*http.Response, error) {
url := c.s.BasePath + fmt.Sprintf("/sessions/%v", c.sessionID)
var body io.Reader = nil
req, err := http.NewRequest("DELETE", url, body)
if err != nil {
return nil, err
}
return SendRequest(c.s.client, req)
}
type SessionStateResponse struct {
ID int `json:"id"`
State SessionState `json:"state"`
}
type SessionsGetStateCall struct {
s *Service
sessionID int
}
func (r *SessionsService) State(sessionID int) *SessionsGetStateCall {
c := &SessionsGetStateCall{s: r.s}
c.sessionID = sessionID
return c
}
func (c *SessionsGetStateCall) Do() (*SessionStateResponse, error) {
res, err := c.doRequest()
if err != nil {
return nil, err
}
defer res.Body.Close()
sessionState := &SessionStateResponse{}
err = DecodeResponse(sessionState, res)
if err != nil {
return nil, err
}
return sessionState, nil
}
func (c *SessionsGetStateCall) doRequest() (*http.Response, error) {
url := c.s.BasePath + fmt.Sprintf("/sessions/%v/state", c.sessionID)
var body io.Reader = nil
req, err := http.NewRequest("GET", url, body)
if err != nil {
return nil, err
}
return SendRequest(c.s.client, req)
}
type SessionLogResponse struct {
ID int
From int
Size int
Log []string
}
type SessionsLogCall struct {
s *Service
sessionID int
}
func (r *SessionsService) Log(sessionID int) *SessionsLogCall {
c := &SessionsLogCall{s: r.s}
c.sessionID = sessionID
return c
}
func (c *SessionsLogCall) Do() (*SessionLogResponse, error) {
res, err := c.doRequest()
if err != nil {
return nil, err
}
defer res.Body.Close()
sessionLog := &SessionLogResponse{}
err = DecodeResponse(sessionLog, res)
if err != nil {
return nil, err
}
return sessionLog, nil
}
func (c *SessionsLogCall) doRequest() (*http.Response, error) {
url := c.s.BasePath + fmt.Sprintf("/sessions/%v/log", c.sessionID)
var body io.Reader = nil
req, err := http.NewRequest("GET", url, body)
if err != nil {
return nil, err
}
return SendRequest(c.s.client, req)
}