Skip to content

Commit 515708d

Browse files
steveloughranjerryshao
authored andcommitted
[SPARK-25183][SQL] Spark HiveServer2 to use Spark ShutdownHookManager
## What changes were proposed in this pull request? Switch `org.apache.hive.service.server.HiveServer2` to register its shutdown callback with Spark's `ShutdownHookManager`, rather than direct with the Java Runtime callback. This avoids race conditions in shutdown where the filesystem is shutdown before the flush/write/rename of the event log is completed, particularly on object stores where the write and rename can be slow. ## How was this patch tested? There's no explicit unit for test this, which is consistent with every other shutdown hook in the codebase. * There's an implicit test when the scalatest process is halted. * More manual/integration testing is needed. HADOOP-15679 has added the ability to explicitly execute the hadoop shutdown hook sequence which spark uses; that could be stabilized for testing if desired, after which all the spark hooks could be tested. Until then: external system tests only. Author: Steve Loughran <[email protected]> Closes apache#22186 from steveloughran/BUG/SPARK-25183-shutdown.
1 parent aa70a0a commit 515708d

File tree

1 file changed

+22
-8
lines changed
  • sql/hive-thriftserver/src/main/java/org/apache/hive/service/server

1 file changed

+22
-8
lines changed

sql/hive-thriftserver/src/main/java/org/apache/hive/service/server/HiveServer2.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
import java.util.Properties;
2222

23+
import scala.runtime.AbstractFunction0;
24+
import scala.runtime.BoxedUnit;
25+
2326
import org.apache.commons.cli.GnuParser;
2427
import org.apache.commons.cli.HelpFormatter;
2528
import org.apache.commons.cli.Option;
@@ -39,6 +42,8 @@
3942
import org.apache.hive.service.cli.thrift.ThriftCLIService;
4043
import org.apache.hive.service.cli.thrift.ThriftHttpCLIService;
4144

45+
import org.apache.spark.util.ShutdownHookManager;
46+
4247
/**
4348
* HiveServer2.
4449
*
@@ -67,13 +72,23 @@ public synchronized void init(HiveConf hiveConf) {
6772
super.init(hiveConf);
6873

6974
// Add a shutdown hook for catching SIGTERM & SIGINT
70-
final HiveServer2 hiveServer2 = this;
71-
Runtime.getRuntime().addShutdownHook(new Thread() {
72-
@Override
73-
public void run() {
74-
hiveServer2.stop();
75-
}
76-
});
75+
// this must be higher than the Hadoop Filesystem priority of 10,
76+
// which the default priority is.
77+
// The signature of the callback must match that of a scala () -> Unit
78+
// function
79+
ShutdownHookManager.addShutdownHook(
80+
new AbstractFunction0<BoxedUnit>() {
81+
public BoxedUnit apply() {
82+
try {
83+
LOG.info("Hive Server Shutdown hook invoked");
84+
stop();
85+
} catch (Throwable e) {
86+
LOG.warn("Ignoring Exception while stopping Hive Server from shutdown hook",
87+
e);
88+
}
89+
return BoxedUnit.UNIT;
90+
}
91+
});
7792
}
7893

7994
public static boolean isHTTPTransportMode(HiveConf hiveConf) {
@@ -95,7 +110,6 @@ public synchronized void start() {
95110
@Override
96111
public synchronized void stop() {
97112
LOG.info("Shutting down HiveServer2");
98-
HiveConf hiveConf = this.getHiveConf();
99113
super.stop();
100114
}
101115

0 commit comments

Comments
 (0)