17
17
18
18
package org .apache .kyuubi .server .api .v1
19
19
20
- import java .util .UUID
21
20
import javax .ws .rs .{Consumes , DELETE , GET , Path , PathParam , POST , Produces , _ }
22
21
import javax .ws .rs .core .{MediaType , Response }
23
22
@@ -30,10 +29,11 @@ import io.swagger.v3.oas.annotations.tags.Tag
30
29
import org .apache .hive .service .rpc .thrift .{TGetInfoType , TProtocolVersion }
31
30
32
31
import org .apache .kyuubi .Utils .error
33
- import org .apache .kyuubi .cli . HandleIdentifier
34
- import org .apache .kyuubi .operation .{ OperationHandle , OperationType }
32
+ import org .apache .kyuubi .operation . OperationHandle
33
+ import org .apache .kyuubi .operation .OperationHandle . parseOperationHandle
35
34
import org .apache .kyuubi .server .api .ApiRequestContext
36
35
import org .apache .kyuubi .session .SessionHandle
36
+ import org .apache .kyuubi .session .SessionHandle .parseSessionHandle
37
37
38
38
@ Tag (name = " Session" )
39
39
@ Produces (Array (MediaType .APPLICATION_JSON ))
@@ -61,8 +61,8 @@ private[v1] class SessionsResource extends ApiRequestContext {
61
61
@ GET
62
62
@ Path (" {sessionHandle}" )
63
63
def sessionInfo (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): SessionDetail = {
64
- val sessionHandle = parseSessionHandle(sessionHandleStr)
65
64
try {
65
+ val sessionHandle = parseSessionHandle(sessionHandleStr)
66
66
val session = backendService.sessionManager.getSession(sessionHandle)
67
67
SessionDetail (
68
68
session.user,
@@ -75,8 +75,8 @@ private[v1] class SessionsResource extends ApiRequestContext {
75
75
session.conf)
76
76
} catch {
77
77
case NonFatal (e) =>
78
- error(s " Invalid $sessionHandle " , e)
79
- throw new NotFoundException (s " Invalid $sessionHandle " )
78
+ error(s " Invalid $sessionHandleStr " , e)
79
+ throw new NotFoundException (s " Invalid $sessionHandleStr " )
80
80
}
81
81
}
82
82
@@ -91,11 +91,9 @@ private[v1] class SessionsResource extends ApiRequestContext {
91
91
def getInfo (
92
92
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
93
93
@ PathParam (" infoType" ) infoType : Int ): InfoDetail = {
94
- val sessionHandle = parseSessionHandle(sessionHandleStr)
95
- val info = TGetInfoType .findByValue(infoType)
96
-
97
94
try {
98
- val infoValue = backendService.getInfo(sessionHandle, info)
95
+ val info = TGetInfoType .findByValue(infoType)
96
+ val infoValue = backendService.getInfo(parseSessionHandle(sessionHandleStr), info)
99
97
InfoDetail (info.toString, infoValue.getStringValue)
100
98
} catch {
101
99
case NonFatal (e) =>
@@ -152,8 +150,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
152
150
@ DELETE
153
151
@ Path (" {sessionHandle}" )
154
152
def closeSession (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): Response = {
155
- val sessionHandle = parseSessionHandle(sessionHandleStr)
156
- backendService.closeSession(sessionHandle)
153
+ backendService.closeSession(parseSessionHandle(sessionHandleStr))
157
154
Response .ok().build()
158
155
}
159
156
@@ -167,10 +164,9 @@ private[v1] class SessionsResource extends ApiRequestContext {
167
164
def executeStatement (
168
165
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
169
166
request : StatementRequest ): OperationHandle = {
170
- val sessionHandle = parseSessionHandle(sessionHandleStr)
171
167
try {
172
168
backendService.executeStatement(
173
- sessionHandle ,
169
+ parseSessionHandle(sessionHandleStr) ,
174
170
request.statement,
175
171
request.runAsync,
176
172
request.queryTimeout)
@@ -188,9 +184,8 @@ private[v1] class SessionsResource extends ApiRequestContext {
188
184
@ POST
189
185
@ Path (" {sessionHandle}/operations/typeInfo" )
190
186
def getTypeInfo (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): OperationHandle = {
191
- val sessionHandle = parseSessionHandle(sessionHandleStr)
192
187
try {
193
- backendService.getTypeInfo(sessionHandle )
188
+ backendService.getTypeInfo(parseSessionHandle(sessionHandleStr) )
194
189
} catch {
195
190
case NonFatal (_) =>
196
191
throw new NotFoundException (s " Error getting type information " )
@@ -205,9 +200,8 @@ private[v1] class SessionsResource extends ApiRequestContext {
205
200
@ POST
206
201
@ Path (" {sessionHandle}/operations/catalogs" )
207
202
def getCatalogs (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): OperationHandle = {
208
- val sessionHandle = parseSessionHandle(sessionHandleStr)
209
203
try {
210
- backendService.getCatalogs(sessionHandle )
204
+ backendService.getCatalogs(parseSessionHandle(sessionHandleStr) )
211
205
} catch {
212
206
case NonFatal (_) =>
213
207
throw new NotFoundException (s " Error getting catalogs " )
@@ -224,9 +218,11 @@ private[v1] class SessionsResource extends ApiRequestContext {
224
218
def getSchemas (
225
219
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
226
220
request : GetSchemasRequest ): OperationHandle = {
227
- val sessionHandle = parseSessionHandle(sessionHandleStr)
228
221
try {
229
- backendService.getSchemas(sessionHandle, request.catalogName, request.schemaName)
222
+ backendService.getSchemas(
223
+ parseSessionHandle(sessionHandleStr),
224
+ request.catalogName,
225
+ request.schemaName)
230
226
} catch {
231
227
case NonFatal (_) =>
232
228
throw new NotFoundException (s " Error getting schemas " )
@@ -243,10 +239,9 @@ private[v1] class SessionsResource extends ApiRequestContext {
243
239
def getTables (
244
240
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
245
241
request : GetTablesRequest ): OperationHandle = {
246
- val sessionHandle = parseSessionHandle(sessionHandleStr)
247
242
try {
248
243
backendService.getTables(
249
- sessionHandle ,
244
+ parseSessionHandle(sessionHandleStr) ,
250
245
request.catalogName,
251
246
request.schemaName,
252
247
request.tableName,
@@ -265,9 +260,8 @@ private[v1] class SessionsResource extends ApiRequestContext {
265
260
@ POST
266
261
@ Path (" {sessionHandle}/operations/tableTypes" )
267
262
def getTableTypes (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): OperationHandle = {
268
- val sessionHandle = parseSessionHandle(sessionHandleStr)
269
263
try {
270
- backendService.getTableTypes(sessionHandle )
264
+ backendService.getTableTypes(parseSessionHandle(sessionHandleStr) )
271
265
} catch {
272
266
case NonFatal (_) =>
273
267
throw new NotFoundException (s " Error getting table types " )
@@ -284,10 +278,9 @@ private[v1] class SessionsResource extends ApiRequestContext {
284
278
def getColumns (
285
279
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
286
280
request : GetColumnsRequest ): OperationHandle = {
287
- val sessionHandle = parseSessionHandle(sessionHandleStr)
288
281
try {
289
282
backendService.getColumns(
290
- sessionHandle ,
283
+ parseSessionHandle(sessionHandleStr) ,
291
284
request.catalogName,
292
285
request.schemaName,
293
286
request.tableName,
@@ -308,10 +301,9 @@ private[v1] class SessionsResource extends ApiRequestContext {
308
301
def getFunctions (
309
302
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
310
303
request : GetFunctionsRequest ): OperationHandle = {
311
- val sessionHandle = parseSessionHandle(sessionHandleStr)
312
304
try {
313
305
backendService.getFunctions(
314
- sessionHandle ,
306
+ parseSessionHandle(sessionHandleStr) ,
315
307
request.catalogName,
316
308
request.schemaName,
317
309
request.functionName)
@@ -331,77 +323,15 @@ private[v1] class SessionsResource extends ApiRequestContext {
331
323
def closeOperation (
332
324
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
333
325
@ PathParam (" operationHandle" ) operationHandleStr : String ): OperationHandle = {
334
- val sessionHandle = parseSessionHandle(sessionHandleStr)
335
- val operationHandle = parseOperationHandle(operationHandleStr)
326
+
336
327
try {
337
- backendService.sessionManager.getSession(sessionHandle).closeOperation(operationHandle)
328
+ val operationHandle = parseOperationHandle(operationHandleStr)
329
+ backendService.sessionManager.getSession(parseSessionHandle(sessionHandleStr))
330
+ .closeOperation(operationHandle)
338
331
operationHandle
339
332
} catch {
340
333
case NonFatal (_) =>
341
334
throw new NotFoundException (s " Error closing an operation " )
342
335
}
343
336
}
344
-
345
- @ ApiResponse (
346
- responseCode = " 200" ,
347
- content = Array (new Content (
348
- mediaType = MediaType .APPLICATION_JSON )),
349
- description =
350
- " Get an operation detail with a given session identifier and operation identifier" )
351
- @ GET
352
- @ Path (" {sessionHandle}/operations/{operationHandle}" )
353
- def getOperationHandle (
354
- @ PathParam (" sessionHandle" ) sessionHandleStr : String ,
355
- @ PathParam (" operationHandle" ) operationHandleStr : String ): OperationDetail = {
356
- val operationHandle = parseOperationHandle(operationHandleStr)
357
- try {
358
- val operation = backendService.sessionManager.operationManager.getOperation(operationHandle)
359
- OperationDetail (operation.shouldRunAsync, operation.isTimedOut, operation.getStatus)
360
- } catch {
361
- case NonFatal (e) =>
362
- throw new NotFoundException (s " Error closing an operation " )
363
- }
364
- }
365
-
366
- def parseOperationHandle (operationHandleStr : String ): OperationHandle = {
367
- try {
368
- val operationHandleParts = operationHandleStr.split(" \\ |" )
369
- require(
370
- operationHandleParts.size == 4 ,
371
- s " Expected 4 parameters but found ${operationHandleParts.size}. " )
372
-
373
- val handleIdentifier = new HandleIdentifier (
374
- UUID .fromString(operationHandleParts(0 )),
375
- UUID .fromString(operationHandleParts(1 )))
376
-
377
- val protocolVersion = TProtocolVersion .findByValue(operationHandleParts(2 ).toInt)
378
- val operationType = OperationType .withName(operationHandleParts(3 ))
379
- val operationHandle = new OperationHandle (handleIdentifier, operationType, protocolVersion)
380
-
381
- operationHandle
382
- } catch {
383
- case NonFatal (e) =>
384
- error(s " Error getting operationHandle by $operationHandleStr. " , e)
385
- throw new NotFoundException (s " Error getting operationHandle by $operationHandleStr. " )
386
- }
387
- }
388
-
389
- def parseSessionHandle (sessionHandleStr : String ): SessionHandle = {
390
- try {
391
- val splitSessionHandle = sessionHandleStr.split(" \\ |" )
392
- val handleIdentifier = new HandleIdentifier (
393
- UUID .fromString(splitSessionHandle(0 )),
394
- UUID .fromString(splitSessionHandle(1 )))
395
- val protocolVersion = TProtocolVersion .findByValue(splitSessionHandle(2 ).toInt)
396
- val sessionHandle = new SessionHandle (handleIdentifier, protocolVersion)
397
-
398
- // if the sessionHandle is invalid, KyuubiSQLException will be thrown here.
399
- backendService.sessionManager.getSession(sessionHandle)
400
- sessionHandle
401
- } catch {
402
- case NonFatal (e) =>
403
- error(s " Error getting sessionHandle by $sessionHandleStr. " , e)
404
- throw new NotFoundException (s " Error getting sessionHandle by $sessionHandleStr. " )
405
- }
406
- }
407
337
}
0 commit comments