-
Notifications
You must be signed in to change notification settings - Fork 28.8k
[SPARK-53198][CORE] Support terminating driver JVM after SparkContext is stopped #51929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ private[spark] object SparkExitCode { | |
OutOfMemoryError. */ | ||
val OOM = 52 | ||
|
||
/** Exit because the SparkContext is stopped. */ | ||
val SPARK_CONTEXT_STOPPED = 69 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[1] https://www.ditig.com/linux-exit-status-codes |
||
|
||
/** Exit due to ClassNotFoundException or NoClassDefFoundError. */ | ||
val CLASS_NOT_FOUND = 101 | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 allstop
logic ofSparkContext
like the following.SparkContext
is supposed to do many things after setting this flag.spark/core/src/main/scala/org/apache/spark/SparkContext.scala
Lines 2322 to 2346 in 5c52a00
If this is really needed, it's more easier to trigger
System.exit
thread insideSparkContext.stop
instead ofSparkLivenessPlugin
. That would be much cheaper.