-
Notifications
You must be signed in to change notification settings - Fork 28k
/
SparkSQLSessionManager.scala
133 lines (122 loc) · 5.43 KB
/
SparkSQLSessionManager.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
/*
* 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.spark.sql.hive.thriftserver
import scala.collection.JavaConverters._
import org.apache.hadoop.hive.conf.HiveConf
import org.apache.hadoop.security.{Credentials, UserGroupInformation}
import org.apache.hive.service.cli.{HiveSQLException, SessionHandle}
import org.apache.hive.service.cli.session.{HiveSession, HiveSessionImpl, HiveSessionImplwithUGI, HiveSessionProxy, SessionManager}
import org.apache.hive.service.server.HiveServer2
import org.apache.spark.SparkContext
import org.apache.spark.internal.Logging
import org.apache.spark.sql.{SQLContext, SparkSession}
import org.apache.spark.sql.hive.HiveUtils
import org.apache.spark.sql.hive.thriftserver.ReflectionUtils._
import org.apache.spark.sql.hive.thriftserver.server.SparkSQLOperationManager
import org.apache.spark.sql.hive.thriftserver.util.{ThriftServerHDFSDelegationTokenProvider, ThriftServerHadoopUtils}
private[hive] class SparkSQLSessionManager(hiveServer: HiveServer2, sqlContext: SQLContext)
extends SessionManager(hiveServer)
with ReflectedCompositeService
with Logging {
private lazy val sparkSqlOperationManager = new SparkSQLOperationManager()
private lazy val sparkSessionManager = new SparkSessionManager()
private lazy val hadoopTokenProvider =
new ThriftServerHDFSDelegationTokenProvider(
SparkSQLEnv.sparkContext.conf,
SparkSQLEnv.sparkContext.hadoopConfiguration)
override def init(hiveConf: HiveConf) {
setSuperField(this, "operationManager", sparkSqlOperationManager)
super.init(hiveConf)
}
override def openSession(
protocol: ThriftserverShimUtils.TProtocolVersion,
username: String,
passwd: String,
ipAddress: String,
sessionConf: java.util.Map[String, String],
withImpersonation: Boolean,
delegationToken: String): SessionHandle = {
var session: HiveSession = null
var sparkSession: SparkSession = null
if (withImpersonation) {
val sessionWithUGI =
new HiveSessionImplwithUGI(
protocol,
username,
passwd,
hiveConf,
ipAddress,
delegationToken)
if (UserGroupInformation.isSecurityEnabled) {
val ugi = sessionWithUGI.getSessionUgi
val originalCreds = ugi.getCredentials
val creds = new Credentials()
ThriftServerHadoopUtils.doAs(ugi)(() => hadoopTokenProvider
.obtainDelegationTokens(creds, username))
val tokens: String = creds.getAllTokens.asScala.map(token => {
token.encodeToUrlString()
}).mkString(SparkContext.SPARK_JOB_TOKEN_DELIMiTER)
ugi.addCredentials(creds)
val existing = ugi.getCredentials()
existing.mergeAll(originalCreds)
ugi.addCredentials(existing)
sparkSqlOperationManager.sessionToTokens.put(session.getSessionHandle, tokens)
}
session = HiveSessionProxy.getProxy(sessionWithUGI, sessionWithUGI.getSessionUgi)
sessionWithUGI.setProxySession(session)
sparkSession = sparkSessionManager.getOrCreteSparkSession(sessionWithUGI, true)
} else {
session = new HiveSessionImpl(protocol, username, passwd, hiveConf, ipAddress)
sparkSession = sparkSessionManager.getOrCreteSparkSession(session, false)
}
session.setSessionManager(this)
session.setOperationManager(operationManager)
try
session.open(sessionConf)
catch {
case e: Exception =>
try
session.close()
catch {
case t: Throwable =>
logWarning("Error closing session", t)
}
session = null
throw new HiveSQLException("Failed to open new session: " + e, e)
}
if (isOperationLogEnabled) {
session.setOperationLogSessionDir(operationLogRootDir)
}
handleToSession.put(session.getSessionHandle, session)
val sessionHandle = session.getSessionHandle
HiveThriftServer2.listener.onSessionCreated(
session.getIpAddress, sessionHandle.getSessionId.toString, session.getUsername)
sparkSession.sqlContext.setConf(HiveUtils.FAKE_HIVE_VERSION.key, HiveUtils.builtinHiveVersion)
if (sessionConf != null && sessionConf.containsKey("use:database")) {
sparkSession.sql(s"use ${sessionConf.get("use:database")}")
}
sparkSqlOperationManager.sessionToContexts.put(sessionHandle, sparkSession.sqlContext)
sessionHandle
}
override def closeSession(sessionHandle: SessionHandle) {
HiveThriftServer2.listener.onSessionClosed(sessionHandle.getSessionId.toString)
super.closeSession(sessionHandle)
sparkSqlOperationManager.sessionToActivePool.remove(sessionHandle)
sparkSqlOperationManager.sessionToContexts.remove(sessionHandle)
sparkSqlOperationManager.sessionToTokens.remove(sessionHandle)
}
}