/
AbstractSession.scala
222 lines (189 loc) · 7.69 KB
/
AbstractSession.scala
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kyuubi.session
import scala.collection.JavaConverters._
import org.apache.hive.service.rpc.thrift.{TGetInfoType, TGetInfoValue, TProtocolVersion, TRowSet, TTableSchema}
import org.apache.kyuubi.{KyuubiSQLException, Logging}
import org.apache.kyuubi.events.KyuubiEvent
import org.apache.kyuubi.operation.{Operation, OperationHandle}
import org.apache.kyuubi.operation.FetchOrientation.FetchOrientation
import org.apache.kyuubi.operation.log.OperationLog
abstract class AbstractSession(
val protocol: TProtocolVersion,
val user: String,
val password: String,
val ipAddress: String,
val conf: Map[String, String],
val sessionManager: SessionManager) extends Session with Logging {
protected def logSessionInfo(msg: String): Unit = info(s"[$user:$ipAddress] $handle - $msg")
final private val _createTime: Long = System.currentTimeMillis()
override def createTime: Long = _createTime
@volatile private var _lastAccessTime: Long = _createTime
override def lastAccessTime: Long = _lastAccessTime
@volatile private var _lastIdleTime: Long = _createTime
override def lastIdleTime: Long = _lastIdleTime
override def getNoOperationTime: Long = {
if (lastIdleTime > 0) System.currentTimeMillis() - _lastIdleTime else 0
}
val normalizedConf: Map[String, String] = sessionManager.validateAndNormalizeConf(conf)
final private val opHandleSet = new java.util.HashSet[OperationHandle]
private def acquire(userAccess: Boolean): Unit = synchronized {
if (userAccess) {
_lastAccessTime = System.currentTimeMillis
}
_lastIdleTime = 0
}
private def release(userAccess: Boolean): Unit = {
if (userAccess) {
_lastAccessTime = System.currentTimeMillis
}
if (opHandleSet.isEmpty) {
_lastIdleTime = System.currentTimeMillis
}
}
private def withAcquireRelease[T](userAccess: Boolean = true)(f: => T): T = {
acquire(userAccess)
try f
finally release(userAccess)
}
override def close(): Unit = withAcquireRelease() {
opHandleSet.forEach { opHandle =>
try {
sessionManager.operationManager.closeOperation(opHandle)
} catch {
case e: Exception =>
warn(s"Error closing operation $opHandle during closing $handle for", e)
}
}
}
protected def runOperation(operation: Operation): OperationHandle = {
try {
val opHandle = operation.getHandle
operation.run()
opHandleSet.add(opHandle)
opHandle
} catch {
case e: KyuubiSQLException =>
sessionManager.operationManager.closeOperation(operation.getHandle)
throw e
}
}
override def getInfo(infoType: TGetInfoType): TGetInfoValue = withAcquireRelease() {
infoType match {
case TGetInfoType.CLI_SERVER_NAME => TGetInfoValue.stringValue("Apache Kyuubi (Incubating)")
case TGetInfoType.CLI_DBMS_NAME => TGetInfoValue.stringValue("Apache Kyuubi (Incubating)")
case TGetInfoType.CLI_DBMS_VER => TGetInfoValue.stringValue(org.apache.kyuubi.KYUUBI_VERSION)
case TGetInfoType.CLI_MAX_COLUMN_NAME_LEN |
TGetInfoType.CLI_MAX_SCHEMA_NAME_LEN |
TGetInfoType.CLI_MAX_TABLE_NAME_LEN => TGetInfoValue.lenValue(128)
case _ => throw KyuubiSQLException(s"Unrecognized GetInfoType value: $infoType")
}
}
override def executeStatement(
statement: String,
confOverlay: Map[String, String],
runAsync: Boolean,
queryTimeout: Long): OperationHandle = withAcquireRelease() {
val operation = sessionManager.operationManager
.newExecuteStatementOperation(this, statement, confOverlay, runAsync, queryTimeout)
runOperation(operation)
}
override def getTableTypes: OperationHandle = withAcquireRelease() {
val operation = sessionManager.operationManager.newGetTableTypesOperation(this)
runOperation(operation)
}
override def getTypeInfo: OperationHandle = {
val operation = sessionManager.operationManager.newGetTypeInfoOperation(this)
runOperation(operation)
}
override def getCatalogs: OperationHandle = {
val operation = sessionManager.operationManager.newGetCatalogsOperation(this)
runOperation(operation)
}
override def getSchemas(catalogName: String, schemaName: String): OperationHandle = {
val operation = sessionManager.operationManager
.newGetSchemasOperation(this, catalogName, schemaName)
runOperation(operation)
}
override def getTables(
catalogName: String,
schemaName: String,
tableName: String,
tableTypes: java.util.List[String]): OperationHandle = {
val operation = sessionManager.operationManager
.newGetTablesOperation(this, catalogName, schemaName, tableName, tableTypes)
runOperation(operation)
}
override def getColumns(
catalogName: String,
schemaName: String,
tableName: String,
columnName: String): OperationHandle = {
val operation = sessionManager.operationManager
.newGetColumnsOperation(this, catalogName, schemaName, tableName, columnName)
runOperation(operation)
}
override def getFunctions(
catalogName: String,
schemaName: String,
functionName: String): OperationHandle = {
val operation = sessionManager.operationManager
.newGetFunctionsOperation(this, catalogName, schemaName, functionName)
runOperation(operation)
}
override def cancelOperation(operationHandle: OperationHandle): Unit = withAcquireRelease() {
sessionManager.operationManager.cancelOperation(operationHandle)
}
override def closeOperation(operationHandle: OperationHandle): Unit = withAcquireRelease() {
opHandleSet.remove(operationHandle)
sessionManager.operationManager.closeOperation(operationHandle)
}
override def getResultSetMetadata(
operationHandle: OperationHandle): TTableSchema = withAcquireRelease() {
sessionManager.operationManager.getOperationResultSetSchema(operationHandle)
}
override def fetchResults(
operationHandle: OperationHandle,
orientation: FetchOrientation,
maxRows: Int,
fetchLog: Boolean): TRowSet = {
if (fetchLog) {
sessionManager.operationManager.getOperationLogRowSet(operationHandle, orientation, maxRows)
} else {
sessionManager.operationManager.getOperationNextRowSet(operationHandle, orientation, maxRows)
}
}
override def closeExpiredOperations: Unit = {
val operations = sessionManager.operationManager
.removeExpiredOperations(opHandleSet.asScala.toSeq)
operations.foreach { op =>
// After the last expired Handle has been cleaned, the 'lastIdleTime' needs to be updated.
withAcquireRelease(false) {
opHandleSet.remove(op.getHandle)
try {
op.close()
} catch {
case e: Exception => warn(s"Error closing timed-out operation ${op.getHandle}", e)
}
}
}
}
override def open(): Unit = {
OperationLog.createOperationLogRootDirectory(this)
}
override def getSessionEvent: Option[KyuubiEvent] = None
}