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


##########
extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.{ScheduledExecutorService, TimeUnit}
+
+import scala.collection.JavaConverters._
+
+import KyuubiJVMQuake._
+import com.sun.management.HotSpotDiagnosticMXBean
+import org.apache.spark.SparkConf
+import org.apache.spark.internal.Logging
+import org.apache.spark.kyuubi.jvm.quake.KyuubiConf._
+import org.apache.spark.util.ThreadUtils
+
+class KyuubiJVMQuake(conf: SparkConf, heapDumpEnabled: Boolean) extends 
Logging {
+  private val appId = conf.get("spark.app.id", 
"app-id").stripPrefix("application_")
+  private val dumpThreshold = 
TimeUnit.SECONDS.toNanos(conf.get(JVM_QUAKE_DUMP_THRESHOLD))
+  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[quake] var heapExist: Boolean = false
+
+  private val scheduler: ScheduledExecutorService =
+    ThreadUtils.newDaemonSingleThreadScheduledExecutor("jvm-quake")
+
+  private[quake] def run(): Unit = {
+    val (currentExitTime, currentGcTime) = getLastGCInfo

Review Comment:
   What do these two variables mean?



##########
extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.{ScheduledExecutorService, TimeUnit}
+
+import scala.collection.JavaConverters._
+
+import KyuubiJVMQuake._
+import com.sun.management.HotSpotDiagnosticMXBean
+import org.apache.spark.SparkConf
+import org.apache.spark.internal.Logging
+import org.apache.spark.kyuubi.jvm.quake.KyuubiConf._
+import org.apache.spark.util.ThreadUtils
+
+class KyuubiJVMQuake(conf: SparkConf, heapDumpEnabled: Boolean) extends 
Logging {
+  private val appId = conf.get("spark.app.id", 
"app-id").stripPrefix("application_")
+  private val dumpThreshold = 
TimeUnit.SECONDS.toNanos(conf.get(JVM_QUAKE_DUMP_THRESHOLD))
+  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[quake] var heapExist: Boolean = false
+
+  private val scheduler: ScheduledExecutorService =
+    ThreadUtils.newDaemonSingleThreadScheduledExecutor("jvm-quake")
+
+  private[quake] def run(): Unit = {
+    val (currentExitTime, currentGcTime) = getLastGCInfo
+    if (currentExitTime == lastExitTime) {
+      return
+    }
+    val gcTime = currentGcTime - lastGCTime
+    val runTime = currentExitTime - lastExitTime - gcTime
+
+    bucket = Math.max(0, bucket + gcTime - BigDecimal(runTime * 
runTimeWeight).toLong)
+
+    if (bucket > dumpThreshold) {
+      logError(s"JVM GC has reached the threshold!!!" +
+        s" (bucket: ${bucket / 1000000000}s, dumpThreshold: ${dumpThreshold / 
1000000000}s)")
+      if (shouldDumpHeap) {
+        saveHeap(heapPath)

Review Comment:
   If the heap is large, does that lead to anything unexpected?



##########
extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.{ScheduledExecutorService, TimeUnit}
+
+import scala.collection.JavaConverters._
+
+import KyuubiJVMQuake._
+import com.sun.management.HotSpotDiagnosticMXBean
+import org.apache.spark.SparkConf
+import org.apache.spark.internal.Logging
+import org.apache.spark.kyuubi.jvm.quake.KyuubiConf._
+import org.apache.spark.util.ThreadUtils
+
+class KyuubiJVMQuake(conf: SparkConf, heapDumpEnabled: Boolean) extends 
Logging {
+  private val appId = conf.get("spark.app.id", 
"app-id").stripPrefix("application_")
+  private val dumpThreshold = 
TimeUnit.SECONDS.toNanos(conf.get(JVM_QUAKE_DUMP_THRESHOLD))
+  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[quake] var heapExist: Boolean = false
+
+  private val scheduler: ScheduledExecutorService =
+    ThreadUtils.newDaemonSingleThreadScheduledExecutor("jvm-quake")
+
+  private[quake] def run(): Unit = {
+    val (currentExitTime, currentGcTime) = getLastGCInfo
+    if (currentExitTime == lastExitTime) {
+      return
+    }
+    val gcTime = currentGcTime - lastGCTime
+    val runTime = currentExitTime - lastExitTime - gcTime
+
+    bucket = Math.max(0, bucket + gcTime - BigDecimal(runTime * 
runTimeWeight).toLong)
+
+    if (bucket > dumpThreshold) {
+      logError(s"JVM GC has reached the threshold!!!" +
+        s" (bucket: ${bucket / 1000000000}s, dumpThreshold: ${dumpThreshold / 
1000000000}s)")
+      if (shouldDumpHeap) {
+        saveHeap(heapPath)
+        heapExist = true
+      } else if (bucket > killThreshold) {
+        System.exit(exitCode)
+      }
+    }
+
+    lastExitTime = currentExitTime
+    lastGCTime = currentGcTime
+  }
+
+  def start(): Unit = {
+    scheduler.scheduleAtFixedRate(
+      new Runnable() {
+        override def run(): Unit = {
+          KyuubiJVMQuake.this.run()
+        }
+      },
+      0,
+      checkInterval,
+      TimeUnit.MILLISECONDS)
+  }
+
+  def stop(): Unit = {
+    scheduler.shutdown()
+  }
+
+  private def shouldDumpHeap: Boolean = {
+    heapDumpEnabled && !heapExist
+  }
+
+  private def saveHeap(savePath: String): Unit = {
+    try {
+      val saveDir = new File(savePath)
+      if (!saveDir.exists()) {
+        saveDir.mkdirs()
+      }
+      val heapDumpFile = new File(saveDir, s"kyuubi-jvm-quake-heapdump.hprof")
+      if (heapDumpFile.exists()) {
+        logInfo(s"Heap exits $heapDumpFile")
+        heapExist = true
+        return
+      }
+      logInfo(s"Starting heap dump at $heapDumpFile")
+      val server = ManagementFactory.getPlatformMBeanServer
+      val mxBean = ManagementFactory.newPlatformMXBeanProxy(
+        server,
+        "com.sun.management:type=HotSpotDiagnostic",
+        classOf[HotSpotDiagnosticMXBean])
+      mxBean.dumpHeap(heapDumpFile.getAbsolutePath, false)

Review Comment:
   btw, are these apis currently stable between different jdk versions?



##########
extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiConf.scala:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.util.concurrent.TimeUnit
+
+import org.apache.spark.internal.config.ConfigBuilder
+
+object KyuubiConf {

Review Comment:
   emm, that's not the right name



-- 
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