18
18
package org .apache .kyuubi .server .api .v1
19
19
20
20
import java .util .UUID
21
- import javax .ws .rs ._
22
- import javax .ws .rs .{Consumes , DELETE , GET , Path , PathParam , POST , Produces }
21
+ import javax .ws .rs .{Consumes , DELETE , GET , Path , PathParam , POST , Produces , _ }
23
22
import javax .ws .rs .core .{MediaType , Response }
24
23
25
24
import scala .collection .JavaConverters ._
@@ -32,7 +31,7 @@ import org.apache.hive.service.rpc.thrift.{TGetInfoType, TProtocolVersion}
32
31
33
32
import org .apache .kyuubi .Utils .error
34
33
import org .apache .kyuubi .cli .HandleIdentifier
35
- import org .apache .kyuubi .operation .OperationHandle
34
+ import org .apache .kyuubi .operation .{ OperationHandle , OperationType }
36
35
import org .apache .kyuubi .server .api .ApiRequestContext
37
36
import org .apache .kyuubi .session .SessionHandle
38
37
@@ -62,7 +61,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
62
61
@ GET
63
62
@ Path (" {sessionHandle}" )
64
63
def sessionInfo (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): SessionDetail = {
65
- val sessionHandle = getSessionHandle (sessionHandleStr)
64
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
66
65
try {
67
66
val session = backendService.sessionManager.getSession(sessionHandle)
68
67
SessionDetail (
@@ -92,7 +91,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
92
91
def getInfo (
93
92
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
94
93
@ PathParam (" infoType" ) infoType : Int ): InfoDetail = {
95
- val sessionHandle = getSessionHandle (sessionHandleStr)
94
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
96
95
val info = TGetInfoType .findByValue(infoType)
97
96
98
97
try {
@@ -153,7 +152,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
153
152
@ DELETE
154
153
@ Path (" {sessionHandle}" )
155
154
def closeSession (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): Response = {
156
- val sessionHandle = getSessionHandle (sessionHandleStr)
155
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
157
156
backendService.closeSession(sessionHandle)
158
157
Response .ok().build()
159
158
}
@@ -168,7 +167,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
168
167
def executeStatement (
169
168
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
170
169
request : StatementRequest ): OperationHandle = {
171
- val sessionHandle = getSessionHandle (sessionHandleStr)
170
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
172
171
try {
173
172
backendService.executeStatement(
174
173
sessionHandle,
@@ -189,7 +188,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
189
188
@ POST
190
189
@ Path (" {sessionHandle}/operations/typeInfo" )
191
190
def getTypeInfo (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): OperationHandle = {
192
- val sessionHandle = getSessionHandle (sessionHandleStr)
191
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
193
192
try {
194
193
backendService.getTypeInfo(sessionHandle)
195
194
} catch {
@@ -206,7 +205,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
206
205
@ POST
207
206
@ Path (" {sessionHandle}/operations/catalogs" )
208
207
def getCatalogs (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): OperationHandle = {
209
- val sessionHandle = getSessionHandle (sessionHandleStr)
208
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
210
209
try {
211
210
backendService.getCatalogs(sessionHandle)
212
211
} catch {
@@ -225,7 +224,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
225
224
def getSchemas (
226
225
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
227
226
request : GetSchemasRequest ): OperationHandle = {
228
- val sessionHandle = getSessionHandle (sessionHandleStr)
227
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
229
228
try {
230
229
backendService.getSchemas(sessionHandle, request.catalogName, request.schemaName)
231
230
} catch {
@@ -244,7 +243,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
244
243
def getTables (
245
244
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
246
245
request : GetTablesRequest ): OperationHandle = {
247
- val sessionHandle = getSessionHandle (sessionHandleStr)
246
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
248
247
try {
249
248
backendService.getTables(
250
249
sessionHandle,
@@ -266,7 +265,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
266
265
@ POST
267
266
@ Path (" {sessionHandle}/operations/tableTypes" )
268
267
def getTableTypes (@ PathParam (" sessionHandle" ) sessionHandleStr : String ): OperationHandle = {
269
- val sessionHandle = getSessionHandle (sessionHandleStr)
268
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
270
269
try {
271
270
backendService.getTableTypes(sessionHandle)
272
271
} catch {
@@ -285,7 +284,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
285
284
def getColumns (
286
285
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
287
286
request : GetColumnsRequest ): OperationHandle = {
288
- val sessionHandle = getSessionHandle (sessionHandleStr)
287
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
289
288
try {
290
289
backendService.getColumns(
291
290
sessionHandle,
@@ -309,7 +308,7 @@ private[v1] class SessionsResource extends ApiRequestContext {
309
308
def getFunctions (
310
309
@ PathParam (" sessionHandle" ) sessionHandleStr : String ,
311
310
request : GetFunctionsRequest ): OperationHandle = {
312
- val sessionHandle = getSessionHandle (sessionHandleStr)
311
+ val sessionHandle = parseSessionHandle (sessionHandleStr)
313
312
try {
314
313
backendService.getFunctions(
315
314
sessionHandle,
@@ -322,7 +321,72 @@ private[v1] class SessionsResource extends ApiRequestContext {
322
321
}
323
322
}
324
323
325
- def getSessionHandle (sessionHandleStr : String ): SessionHandle = {
324
+ @ ApiResponse (
325
+ responseCode = " 200" ,
326
+ content = Array (new Content (
327
+ mediaType = MediaType .APPLICATION_JSON )),
328
+ description = " Close an operation" )
329
+ @ DELETE
330
+ @ Path (" {sessionHandle}/operations/{operationHandle}" )
331
+ def closeOperation (
332
+ @ PathParam (" sessionHandle" ) sessionHandleStr : String ,
333
+ @ PathParam (" operationHandle" ) operationHandleStr : String ): OperationHandle = {
334
+ val sessionHandle = parseSessionHandle(sessionHandleStr)
335
+ val operationHandle = parseOperationHandle(operationHandleStr)
336
+ try {
337
+ backendService.sessionManager.getSession(sessionHandle).closeOperation(operationHandle)
338
+ operationHandle
339
+ } catch {
340
+ case NonFatal (_) =>
341
+ throw new NotFoundException (s " Error closing an operation " )
342
+ }
343
+ }
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 = {
326
390
try {
327
391
val splitSessionHandle = sessionHandleStr.split(" \\ |" )
328
392
val handleIdentifier = new HandleIdentifier (
0 commit comments