From f1cd730b0a52276992802ab652820b1dc93ba6c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=87=E7=BF=94?= Date: Thu, 13 May 2021 13:31:08 +0800 Subject: [PATCH] [KYUUBI #632] solve the timeout problem when stop the kyuubiServer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### _Why are the changes needed?_ When you stop the KyuubiServiceDiscovery, you need to close node in zk and delete localNodePath at first. But when EmbeddedZookeeper is closed before KyuubiServiceDiscovery, the operation that stop kyuubiServer will timeout. Now use 'org.apache.hadoop.util.ShutdownHookManager' to close each service by different priority. ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [ ] [Run test](https://kyuubi.readthedocs.io/en/latest/tools/testing.html#running-tests) locally before make a pull request Closes #632 from zhang1002/dev. Closes #632 ee21e68 [张宇翔] Change the relevant format f68fc7a [张宇翔] Merge branch 'master' into dev 128f244 [张宇翔] solve the timeout problem when stop the kyuubiServer 79b3d5a [张宇翔] ## solve the folder naming problem For starting kyuubiServer, the script need to get 'SPARK_HOME' from the file: RELEASE. The rule is: spark-${SPARK_VERSION}-bin-hadoop${HADOOP_VERSION}. But when we package project from source, the script named dist will create the directory about 'SPARK_HOME' by the rule: spark-${SPARK_VERSION}-bin-hadoop${SPARK_HADOOP_VERSION}. So it's the differences between two rules. This change let ${HADOOP_VERSION} as same as ${SPARK_HADOOP_VERSION}. Lead-authored-by: 张宇翔 Co-authored-by: 张宇翔 Signed-off-by: Kent Yao --- .../src/main/scala/org/apache/kyuubi/Utils.scala | 10 ++++++++++ .../scala/org/apache/kyuubi/server/KyuubiServer.scala | 4 +++- .../apache/kyuubi/zookeeper/EmbeddedZookeeper.scala | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala index a78166120..45924aef6 100644 --- a/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala +++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala @@ -26,6 +26,7 @@ import scala.collection.JavaConverters._ import org.apache.commons.lang3.SystemUtils import org.apache.hadoop.security.UserGroupInformation +import org.apache.hadoop.util.ShutdownHookManager import org.apache.kyuubi.config.internal.Tests.IS_TESTING @@ -165,4 +166,13 @@ private[kyuubi] object Utils extends Logging { def isTesting: Boolean = { System.getProperty(IS_TESTING.key) != null } + + /** + * Add some operations that you want into ShutdownHook + * @param hook + * @param priority: 0~100 + */ + def addShutdownHook(hook: Runnable, priority: Int): Unit = { + ShutdownHookManager.get().addShutdownHook(hook, priority) + } } diff --git a/kyuubi-main/src/main/scala/org/apache/kyuubi/server/KyuubiServer.scala b/kyuubi-main/src/main/scala/org/apache/kyuubi/server/KyuubiServer.scala index eb9bc0503..380df1674 100644 --- a/kyuubi-main/src/main/scala/org/apache/kyuubi/server/KyuubiServer.scala +++ b/kyuubi-main/src/main/scala/org/apache/kyuubi/server/KyuubiServer.scala @@ -44,7 +44,9 @@ object KyuubiServer extends Logging { val server = new KyuubiServer() server.initialize(conf) server.start() - sys.addShutdownHook(server.stop()) + Utils.addShutdownHook(new Runnable { + override def run(): Unit = server.stop() + }, 100) server } diff --git a/kyuubi-zookeeper/src/main/scala/org/apache/kyuubi/zookeeper/EmbeddedZookeeper.scala b/kyuubi-zookeeper/src/main/scala/org/apache/kyuubi/zookeeper/EmbeddedZookeeper.scala index 6c7d2af4f..475afbd6d 100644 --- a/kyuubi-zookeeper/src/main/scala/org/apache/kyuubi/zookeeper/EmbeddedZookeeper.scala +++ b/kyuubi-zookeeper/src/main/scala/org/apache/kyuubi/zookeeper/EmbeddedZookeeper.scala @@ -23,6 +23,7 @@ import scala.collection.JavaConverters._ import org.apache.curator.test.{InstanceSpec, TestingServer} +import org.apache.kyuubi.Utils import org.apache.kyuubi.config.KyuubiConf import org.apache.kyuubi.service.{AbstractService, ServiceState} import org.apache.kyuubi.zookeeper.ZookeeperConf._ @@ -71,7 +72,9 @@ class EmbeddedZookeeper extends AbstractService("EmbeddedZookeeper") { override def start(): Unit = synchronized { server.start() info(s"$getName is started at $getConnectString") - sys.addShutdownHook(server.close()) + Utils.addShutdownHook(new Runnable { + override def run(): Unit = server.close() + }, 50) super.start() }