yikf commented on code in PR #6572: URL: https://github.com/apache/kyuubi/pull/6572#discussion_r1703958767
########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala: ########## @@ -0,0 +1,146 @@ +/* + * 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.KyuubiJVMQuakeConf._ +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) { + 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") Review Comment: how about ${PROCESS_ID}.hprof ? ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala: ########## @@ -0,0 +1,146 @@ +/* + * 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.KyuubiJVMQuakeConf._ +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) { + 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 + } else { + 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) + } + } catch { + case e: Exception => + logError(s"Link failed ", e) Review Comment: Failed to link and dump ${PROCESS_NAME/ID} heap to ${SAVE_PATH} ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala: ########## @@ -0,0 +1,146 @@ +/* + * 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.KyuubiJVMQuakeConf._ +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) { + 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) + } Review Comment: Is it better to combine `dumpThreshold` and `killThreshold` into one variable? Or at least we need to specify that `dumpThreshold` is smaller than `killThreshold`. ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala: ########## @@ -0,0 +1,146 @@ +/* + * 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.KyuubiJVMQuakeConf._ +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) { + val gcTime = currentGcTime - lastGCTime + val runTime = currentExitTime - lastExitTime - gcTime + + bucket = Math.max(0, bucket + gcTime - BigDecimal(runTime * runTimeWeight).toLong) Review Comment: I'm not sure if this calculation makes sense, because when the app starts running, the gc time will be less than the run time, in which case the bucket will get smaller and smaller. With the running of the app, the gc time is actually greater than the running time at some point, but due to the need to make up the previous bucket, there will be a long delay before it is detected ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuakePlugin.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.{Map => JMap} + +import scala.collection.JavaConverters._ + +import org.apache.spark.SparkContext +import org.apache.spark.api.plugin.{DriverPlugin, ExecutorPlugin, PluginContext, SparkPlugin} +import org.apache.spark.internal.Logging +import org.apache.spark.kyuubi.jvm.quake.KyuubiJVMQuakeConf._ + +class KyuubiJVMQuakePlugin extends SparkPlugin with Logging { + + override def driverPlugin(): DriverPlugin = { + new DriverPlugin() { + override def init(sc: SparkContext, pluginContext: PluginContext): JMap[String, String] = { + val jvmQuakeEnabled = sc.conf.get(DRIVER_JVM_QUAKE_ENABLED) + val jvmQuakeHeapDumpEnabled = sc.conf.get(DRIVER_JVM_QUAKE_HEAP_DUMP_ENABLED) + if (jvmQuakeEnabled) { + KyuubiJVMQuake.start(sc.conf, jvmQuakeHeapDumpEnabled) + } + Map.empty[String, String].asJava + } + + override def shutdown(): Unit = { + KyuubiJVMQuake.stop() + } + } + } + + override def executorPlugin(): ExecutorPlugin = { + new ExecutorPlugin { + override def init(context: PluginContext, extraConf: JMap[String, String]): Unit = { + val jvmQuakeEnabled = context.conf().get(EXECUTOR_JVM_QUAKE_ENABLED) + val jvmQuakeHeapDumpEnabled = context.conf().get(EXECUTOR_JVM_QUAKE_HEAP_DUMP_ENABLED) Review Comment: jvmQuakeHeapDumpEnabled can be extracted in KyuubiJVMQuake ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuakePlugin.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.{Map => JMap} + +import scala.collection.JavaConverters._ + +import org.apache.spark.SparkContext +import org.apache.spark.api.plugin.{DriverPlugin, ExecutorPlugin, PluginContext, SparkPlugin} +import org.apache.spark.internal.Logging +import org.apache.spark.kyuubi.jvm.quake.KyuubiJVMQuakeConf._ + +class KyuubiJVMQuakePlugin extends SparkPlugin with Logging { Review Comment: Location is the guardian of the spark process, how about SparkJVMQuakePlugin, we only need to reflect kyuubi in the module and package path ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuakePlugin.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.{Map => JMap} + +import scala.collection.JavaConverters._ + +import org.apache.spark.SparkContext +import org.apache.spark.api.plugin.{DriverPlugin, ExecutorPlugin, PluginContext, SparkPlugin} +import org.apache.spark.internal.Logging +import org.apache.spark.kyuubi.jvm.quake.KyuubiJVMQuakeConf._ + +class KyuubiJVMQuakePlugin extends SparkPlugin with Logging { + + override def driverPlugin(): DriverPlugin = { + new DriverPlugin() { + override def init(sc: SparkContext, pluginContext: PluginContext): JMap[String, String] = { + val jvmQuakeEnabled = sc.conf.get(DRIVER_JVM_QUAKE_ENABLED) + val jvmQuakeHeapDumpEnabled = sc.conf.get(DRIVER_JVM_QUAKE_HEAP_DUMP_ENABLED) + if (jvmQuakeEnabled) { + KyuubiJVMQuake.start(sc.conf, jvmQuakeHeapDumpEnabled) Review Comment: ditto ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuakePlugin.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.{Map => JMap} + +import scala.collection.JavaConverters._ + +import org.apache.spark.SparkContext +import org.apache.spark.api.plugin.{DriverPlugin, ExecutorPlugin, PluginContext, SparkPlugin} +import org.apache.spark.internal.Logging +import org.apache.spark.kyuubi.jvm.quake.KyuubiJVMQuakeConf._ + +class KyuubiJVMQuakePlugin extends SparkPlugin with Logging { + + override def driverPlugin(): DriverPlugin = { + new DriverPlugin() { + override def init(sc: SparkContext, pluginContext: PluginContext): JMap[String, String] = { + val jvmQuakeEnabled = sc.conf.get(DRIVER_JVM_QUAKE_ENABLED) + val jvmQuakeHeapDumpEnabled = sc.conf.get(DRIVER_JVM_QUAKE_HEAP_DUMP_ENABLED) Review Comment: jvmQuakeHeapDumpEnabled can be extracted in KyuubiJVMQuake ########## extensions/spark/kyuubi-spark-jvm-quake/src/main/scala/org/apache/spark/kyuubi/jvm/quake/KyuubiJVMQuake.scala: ########## @@ -0,0 +1,146 @@ +/* + * 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.KyuubiJVMQuakeConf._ +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) { + 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 Review Comment: If quake is positioned as a guardian, I don't think heapExist is needed, that is, there is no need to dump many times, as soon as the threshold is reached, we dump and kill, rather than dump many times. This feels a bit like analyzing heap memory, but its goal is not that -- 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