yoock commented on code in PR #6572:
URL: https://github.com/apache/kyuubi/pull/6572#discussion_r1706927475


##########
extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/SparkJVMQuake.scala:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.kyuubi.jvm.quake
+
+import java.io.File
+import java.lang.management.ManagementFactory
+import java.util.concurrent.TimeUnit
+
+import scala.collection.JavaConverters._
+
+import SparkJVMQuake._
+import com.sun.management.HotSpotDiagnosticMXBean
+import org.apache.spark.SparkConf
+import org.apache.spark.internal.Logging
+import org.apache.spark.kyuubi.jvm.quake.SparkJVMQuakeConf._
+import org.apache.spark.util.ThreadUtils
+
+class SparkJVMQuake(conf: SparkConf, heapDumpEnabled: Boolean) extends Logging 
{
+  private val appId = conf.get("spark.app.id", 
"app-id").stripPrefix("application_")
+  private val killThreshold = 
TimeUnit.SECONDS.toNanos(conf.get(JVM_QUAKE_KILL_THRESHOLD))
+  private val exitCode = conf.get(JVM_QUAKE_EXIT_CODE)
+  private val checkInterval = 
TimeUnit.SECONDS.toMillis(conf.get(JVM_QUAKE_CHECK_INTERVAL))
+  private val runTimeWeight = conf.get(JVM_QUAKE_RUN_TIME_WEIGHT)
+  private[quake] val heapPath = s"${conf.get(JVM_QUAKE_HEAP_DUMP_PATH)}/$appId"
+
+  private[quake] var (lastExitTime, lastGCTime) = getLastGCInfo
+  private[quake] var bucket: Long = 0L
+  private var heapDumping: Boolean = false
+
+  private[quake] val heapDumpFileName = s"${getPid()}.hprof"
+
+  private val scheduler =
+    ThreadUtils.newDaemonSingleThreadScheduledExecutor("jvm-quake")
+
+  private[quake] def run(): Unit = {
+    val (currentExitTime, currentGcTime) = getLastGCInfo
+    if (currentExitTime != lastExitTime && !heapDumping) {
+      val gcTime = currentGcTime - lastGCTime
+      val runTime = currentExitTime - lastExitTime - gcTime
+
+      bucket = Math.max(0, bucket + gcTime - BigDecimal(runTime * 
runTimeWeight).toLong)
+
+      if (bucket > killThreshold) {
+        logError(s"JVM GC has reached the threshold!!!" +
+          s" (bucket: ${bucket / 1000000000}s, killThreshold: ${killThreshold 
/ 1000000000}s)")
+        if (heapDumpEnabled) {
+          heapDumping = true
+          saveHeap()
+        }
+        System.exit(exitCode)
+      }
+
+      lastExitTime = currentExitTime
+      lastGCTime = currentGcTime
+    }
+  }
+
+  def start(): Unit = {
+    scheduler.scheduleAtFixedRate(
+      new Runnable() {
+        override def run(): Unit = {
+          SparkJVMQuake.this.run()
+        }
+      },

Review Comment:
   Good idea, revised



-- 
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-unsubscr...@kyuubi.apache.org

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


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

Reply via email to