0
# Server Management
1
2
Core server lifecycle management for the Spark Hive Thrift Server, including startup, configuration, and shutdown operations.
3
4
## Capabilities
5
6
### Main Entry Point
7
8
Primary entry point for starting the Hive Thrift Server daemon process.
9
10
```scala { .api }
11
/**
12
* Main entry point for starting the Hive Thrift Server
13
* Processes command line arguments and initializes the server
14
* @param args Command line arguments compatible with HiveServer2
15
*/
16
def main(args: Array[String]): Unit
17
```
18
19
**Usage Example:**
20
21
```scala
22
// Start server from main method
23
object MyThriftServer extends App {
24
HiveThriftServer2.main(Array(
25
"--hiveconf", "hive.server2.thrift.port=10000",
26
"--hiveconf", "hive.server2.thrift.bind.host=0.0.0.0"
27
))
28
}
29
```
30
31
### Programmatic Server Startup
32
33
Developer API for starting the thrift server with an existing HiveContext, useful for embedding in custom applications.
34
35
```scala { .api }
36
/**
37
* Programmatic API to start thrift server with existing HiveContext
38
* Marked as DeveloperApi for advanced use cases
39
* @param sqlContext Pre-configured HiveContext instance
40
*/
41
@DeveloperApi
42
def startWithContext(sqlContext: HiveContext): Unit
43
```
44
45
**Usage Example:**
46
47
```scala
48
import org.apache.spark.sql.hive.HiveContext
49
import org.apache.spark.sql.hive.thriftserver.HiveThriftServer2
50
51
// Create custom HiveContext with specific configuration
52
val sparkConf = new SparkConf()
53
.setAppName("My Spark SQL Server")
54
.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
55
56
val sparkContext = new SparkContext(sparkConf)
57
val hiveContext = new HiveContext(sparkContext)
58
59
// Configure Hive-specific settings
60
hiveContext.setConf("hive.server2.thrift.port", "10001")
61
62
// Start thrift server with custom context
63
HiveThriftServer2.startWithContext(hiveContext)
64
```
65
66
### Server State Variables
67
68
Public variables for accessing server state and components.
69
70
```scala { .api }
71
/**
72
* Logger instance for server operations
73
*/
74
var LOG: Log
75
76
/**
77
* Optional web UI tab for monitoring server through Spark UI
78
*/
79
var uiTab: Option[ThriftServerTab]
80
81
/**
82
* Event listener for tracking sessions and query execution
83
*/
84
var listener: HiveThriftServer2Listener
85
```
86
87
### Internal Server Implementation
88
89
The private `HiveThriftServer2` class extends Hive's `HiveServer2` with Spark-specific implementations.
90
91
```scala { .api }
92
private[hive] class HiveThriftServer2(hiveContext: HiveContext)
93
extends HiveServer2 with ReflectedCompositeService {
94
95
override def init(hiveConf: HiveConf): Unit
96
override def start(): Unit
97
override def stop(): Unit
98
}
99
```
100
101
### Command Line Processing
102
103
Integration with Hive's command line argument processing for server options.
104
105
```scala { .api }
106
private[hive] class HiveServerServerOptionsProcessor(serverName: String) {
107
/**
108
* Process and validate command line arguments
109
* @param args Array of command line arguments
110
* @return true if arguments are valid and processing should continue
111
*/
112
def process(args: Array[String]): Boolean
113
}
114
```
115
116
**Supported Command Line Options:**
117
118
- `--hiveconf <property=value>` - Set Hive configuration property
119
- `--service <servicename>` - Specify service to run (default: hiveserver2)
120
- `--help` - Display help information
121
122
### Server Lifecycle Management
123
124
The server follows a standard lifecycle pattern:
125
126
1. **Initialization**: Command line processing and configuration setup
127
2. **Context Creation**: SparkSQLEnv initialization with optimized settings
128
3. **Service Setup**: CLI service and Thrift transport layer configuration
129
4. **Startup**: Server start with listener registration and UI integration
130
5. **Monitoring**: Event tracking and web UI updates
131
6. **Shutdown**: Clean resource cleanup with shutdown hooks
132
133
### Error Handling
134
135
The server includes comprehensive error handling:
136
137
```scala
138
// Automatic shutdown on SparkContext termination
139
if (SparkSQLEnv.sparkContext.stopped.get()) {
140
logError("SparkContext has stopped even if HiveServer2 has started, so exit")
141
System.exit(-1)
142
}
143
144
// Exception handling during startup
145
try {
146
val server = new HiveThriftServer2(SparkSQLEnv.hiveContext)
147
server.init(SparkSQLEnv.hiveContext.hiveconf)
148
server.start()
149
// ... setup monitoring
150
} catch {
151
case e: Exception =>
152
logError("Error starting HiveThriftServer2", e)
153
System.exit(-1)
154
}
155
```
156
157
### Transport Mode Configuration
158
159
The server supports both binary and HTTP transport modes based on Hive configuration:
160
161
```scala
162
private def isHTTPTransportMode(hiveConf: HiveConf): Boolean = {
163
val transportMode = hiveConf.getVar(ConfVars.HIVE_SERVER2_TRANSPORT_MODE)
164
transportMode.toLowerCase(Locale.ENGLISH).equals("http")
165
}
166
```
167
168
- **Binary Mode**: Direct TCP connections (default, better performance)
169
- **HTTP Mode**: HTTP-based transport (better firewall compatibility)
170
171
### Integration with Spark UI
172
173
When Spark UI is enabled, the server automatically registers a monitoring tab:
174
175
```scala
176
uiTab = if (sqlContext.sparkContext.getConf.getBoolean("spark.ui.enabled", true)) {
177
Some(new ThriftServerTab(sqlContext.sparkContext))
178
} else {
179
None
180
}
181
```
182
183
This provides real-time visibility into:
184
- Active sessions and their details
185
- Running and completed queries
186
- Query execution statistics
187
- Session duration and activity metrics