Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.deploy

import java.util.{Map => JMap}
import java.util.concurrent.{ScheduledExecutorService, TimeUnit}

import scala.jdk.CollectionConverters._

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.internal.LogKeys._
import org.apache.spark.internal.config._
import org.apache.spark.util.{SparkExitCode, ThreadUtils}

/**
* A built-in plugin to check liveness of Spark essential components, e.g., SparkContext.
*/
class SparkLivenessPlugin extends SparkPlugin {
override def driverPlugin(): DriverPlugin = new SparkLivenessDriverPlugin()

// No-op
override def executorPlugin(): ExecutorPlugin = null
}

class SparkLivenessDriverPlugin extends DriverPlugin with Logging {

private val timer: ScheduledExecutorService =
ThreadUtils.newDaemonSingleThreadScheduledExecutor("driver-liveness")

override def init(sc: SparkContext, ctx: PluginContext): JMap[String, String] = {
val checkInterval = sc.conf.get(DRIVER_SPARK_CONTEXT_LIVENESS_CHECK_INTERVAL)
val terminateDelay = sc.conf.get(DRIVER_SPARK_CONTEXT_LIVENESS_TERMINATE_DELAY)
if (checkInterval == 0) {
logWarning("SparkContext liveness check is disabled.")
} else {
val task: Runnable = () => {
if (sc.isStopped) {
Copy link
Member

@dongjoon-hyun dongjoon-hyun Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although there is a delay, terminateDelay, this approach looks like a kind of race condition because this checks only the starting point of all stop logic of SparkContext like the following. SparkContext is supposed to do many things after setting this flag.

if (!stopped.compareAndSet(false, true)) {
logInfo("SparkContext already stopped.")
return
}
if (_shutdownHookRef != null) {
ShutdownHookManager.removeShutdownHook(_shutdownHookRef)
}
if (listenerBus != null) {
Utils.tryLogNonFatalError {
postApplicationEnd(exitCode)
}
}
Utils.tryLogNonFatalError {
_driverLogger.foreach(_.stop())
}
Utils.tryLogNonFatalError {
_ui.foreach(_.stop())
}
Utils.tryLogNonFatalError {
_cleaner.foreach(_.stop())
}
Utils.tryLogNonFatalError {
_executorAllocationManager.foreach(_.stop())
}

If this is really needed, it's more easier to trigger System.exit thread inside SparkContext.stop instead of SparkLivenessPlugin. That would be much cheaper.

logWarning(log"SparkContext is stopped, will terminate Driver JVM " +
log"after ${MDC(TIME_UNITS, terminateDelay)} seconds.")
Thread.sleep(terminateDelay * 1000L)
System.exit(SparkExitCode.SPARK_CONTEXT_STOPPED)
}
}
timer.scheduleWithFixedDelay(task, checkInterval, checkInterval, TimeUnit.SECONDS)
}
Map.empty[String, String].asJava
}
}
21 changes: 21 additions & 0 deletions core/src/main/scala/org/apache/spark/internal/config/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,27 @@ package object config {
.checkValue(v => v >= 0, "The value should be a non-negative time value.")
.createWithDefaultString("0min")

private[spark] val DRIVER_SPARK_CONTEXT_LIVENESS_CHECK_INTERVAL =
ConfigBuilder("spark.driver.liveness.sparkContext.checkInterval")
.doc("If set a positive time value, spark driver periodically checks whether " +
"the SparkContext is live. Once the SparkContext is detected to be stopped, " +
"terminate the driver with the exit code 69 after a delay. " +
"To use, set `spark.plugins=org.apache.spark.deploy.SparkLivenessPlugin`.")
.version("4.1.0")
.timeConf(TimeUnit.SECONDS)
.checkValue(v => v >= 0, "The value should be a non-negative time value.")
.createWithDefaultString("10s")

private[spark] val DRIVER_SPARK_CONTEXT_LIVENESS_TERMINATE_DELAY =
ConfigBuilder("spark.driver.liveness.sparkContext.terminateDelay")
.doc("Self-terminate waiting duration after detecting SparkContext is stopped. " +
s"This config takes effect only when " +
s"${DRIVER_SPARK_CONTEXT_LIVENESS_CHECK_INTERVAL.key} effective.")
.version("4.1.0")
.timeConf(TimeUnit.SECONDS)
.checkValue(v => v >= 0, "The value should be a non-negative time value.")
.createWithDefaultString("120s")

private[spark] val DRIVER_BIND_ADDRESS = ConfigBuilder("spark.driver.bindAddress")
.doc("Address where to bind network listen sockets on the driver.")
.version("2.1.0")
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/org/apache/spark/util/SparkExitCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private[spark] object SparkExitCode {
OutOfMemoryError. */
val OOM = 52

/** Exit because the SparkContext is stopped. */
val SPARK_CONTEXT_STOPPED = 69
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SparkContext is an essential component/service of the Spark application, according to [1][2], 69 is a widely-used exit code with the typical meaning - Service unavailable: A service required to complete the task is unavailable.

[1] https://www.ditig.com/linux-exit-status-codes
[2] https://www.man7.org/linux/man-pages/man3/sysexits.h.3head.html


/** Exit due to ClassNotFoundException or NoClassDefFoundError. */
val CLASS_NOT_FOUND = 101

Expand Down