You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by GitBox <gi...@apache.org> on 2022/04/08 04:31:18 UTC

[GitHub] [incubator-kyuubi] turboFei commented on a diff in pull request #2295: [KYUUBI #2250] Support to limit the spark engine max running time

turboFei commented on code in PR #2295:
URL: https://github.com/apache/incubator-kyuubi/pull/2295#discussion_r845723230


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/SparkSQLEngine.scala:
##########
@@ -64,11 +70,43 @@ case class SparkSQLEngine(spark: SparkSession) extends Serverable("SparkSQLEngin
       assert(currentEngine.isDefined)
       currentEngine.get.stop()
     })
+
+    startLifetimeChecker(() => {

Review Comment:
   maybe `startLifetimeTerminatingChecker` is better



##########
kyuubi-common/src/main/scala/org/apache/kyuubi/config/KyuubiConf.scala:
##########
@@ -622,6 +622,14 @@ object KyuubiConf {
       .stringConf
       .createOptional
 
+  val ENGINE_SPARK_MAX_LIFETIME: ConfigEntry[Long] =
+    buildConf("kyuubi.session.engine.spark.max.lifetime")
+      .doc("max lifetime for spark engine, the engine will self-terminate when it comes to the" +

Review Comment:
   nit: max -> Max



##########
kyuubi-common/src/main/scala/org/apache/kyuubi/config/KyuubiConf.scala:
##########
@@ -622,6 +622,14 @@ object KyuubiConf {
       .stringConf
       .createOptional
 
+  val ENGINE_SPARK_MAX_LIFETIME: ConfigEntry[Long] =
+    buildConf("kyuubi.session.engine.spark.max.lifetime")
+      .doc("max lifetime for spark engine, the engine will self-terminate when it comes to the" +
+        " end of life. 0 or negative means not to self-terminate.")
+      .version("1.6.0")
+      .timeConf
+      .createWithDefault(Duration.ofHours(6).toMillis)

Review Comment:
   by default it should be legacy `0` so that to align with original behavior



##########
kyuubi-common/src/main/scala/org/apache/kyuubi/util/ThreadUtils.scala:
##########
@@ -64,4 +64,21 @@ object ThreadUtils extends Logging {
         throw new KyuubiException("Exception thrown in awaitResult: ", e)
     }
   }
+
+  def shutdown(
+      executor: ExecutorService,
+      gracePeriod: Duration = FiniteDuration(30, TimeUnit.SECONDS)): Unit = {

Review Comment:
   ```
   gracePeriod: Option[Duration] = None
   ```
   
   So that:
   ```
   gracePeriod.map { shutdownTimeout =>
   executor.awaitTermination(shutdownTimeout, TimeUnit.MILLISECONDS)
   }.getOrElse(executor.shutdown())
   ```



##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/SparkSQLEngine.scala:
##########
@@ -64,11 +70,43 @@ case class SparkSQLEngine(spark: SparkSession) extends Serverable("SparkSQLEngin
       assert(currentEngine.isDefined)
       currentEngine.get.stop()
     })
+
+    startLifetimeChecker(() => {
+      assert(currentEngine.isDefined)
+      currentEngine.get.stop()
+    })
+
+  }
+
+  override def stop(): Unit = synchronized {
+    super.stop()
+
+    shutdown = true
+    val shutdownTimeout: Long = conf.get(ENGINE_EXEC_POOL_SHUTDOWN_TIMEOUT)
+    ThreadUtils.shutdown(lifetimeChecker, Duration(shutdownTimeout, TimeUnit.MILLISECONDS))
   }
 
   override protected def stopServer(): Unit = {
     countDownLatch.countDown()
   }
+
+  private[kyuubi] def startLifetimeChecker(stop: () => Unit): Unit = {
+    val interval = conf.get(ENGINE_CHECK_INTERVAL)
+    val maxLifetime = conf.get(ENGINE_SPARK_MAX_LIFETIME)
+    if (maxLifetime > 0) {
+      val checkTask = new Runnable {
+        override def run(): Unit = {
+          val lifetime: Long = System.currentTimeMillis() - getStartTime
+          val openSessionCount: Int = backendService.sessionManager.getOpenSessionCount
+          if (!shutdown && lifetime > maxLifetime && openSessionCount <= 0) {
+            info(s"Spark engine has been running for more than $maxLifetime ms, terminating")
+            stop()

Review Comment:
   We need deregister the engineServiceDiscovery at first.
   
   And then if the current open session count is zero, we can stop the engine, otherwise, we need wait.
   
   ```
   if (!shutdown && lifetime > maxLifetime) {
       
      if (unDeregistered) {
         info (".... deregistering ...")
         frontendServices.flatMap(_.discoveryService).map {
           case engineServiceDiscovery: EngineServiceDiscovery =>
             engineServiceDiscovery.stop()
         }
      }
   
     if (openSessionCount <= 0) {
               info(s"Spark engine has been running for more than $maxLifetime ms, terminating")
               stop()
     }
   }
   
   
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org