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
10 changes: 9 additions & 1 deletion yarn/src/main/scala/org/apache/spark/deploy/yarn/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.apache.hadoop.yarn.api.protocolrecords._
import org.apache.hadoop.yarn.api.records._
import org.apache.hadoop.yarn.client.api.{YarnClient, YarnClientApplication}
import org.apache.hadoop.yarn.conf.YarnConfiguration
import org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException
import org.apache.hadoop.yarn.util.Records

import org.apache.spark.{Logging, SecurityManager, SparkConf, SparkContext, SparkException}
Expand Down Expand Up @@ -559,7 +560,14 @@ private[spark] class Client(
var lastState: YarnApplicationState = null
while (true) {
Thread.sleep(interval)
val report = getApplicationReport(appId)
val report: ApplicationReport =
try {
getApplicationReport(appId)
} catch {
case e: ApplicationNotFoundException =>
logError(s"Application $appId not found.")
return (YarnApplicationState.KILLED, FinalApplicationStatus.KILLED)
}
val state = report.getYarnApplicationState

if (logApplicationReport) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private[spark] class YarnClientSchedulerBackend(

private var client: Client = null
private var appId: ApplicationId = null
@volatile private var stopping: Boolean = false
private var monitorThread: Thread = null

/**
* Create a Yarn client to submit an application to the ResourceManager.
Expand All @@ -57,7 +57,8 @@ private[spark] class YarnClientSchedulerBackend(
client = new Client(args, conf)
appId = client.submitApplication()
waitForApplication()
asyncMonitorApplication()
monitorThread = asyncMonitorApplication()
monitorThread.start()
}

/**
Expand Down Expand Up @@ -123,42 +124,27 @@ private[spark] class YarnClientSchedulerBackend(
* If the application has exited for any reason, stop the SparkContext.
* This assumes both `client` and `appId` have already been set.
*/
private def asyncMonitorApplication(): Unit = {
private def asyncMonitorApplication(): Thread = {
assert(client != null && appId != null, "Application has not been submitted yet!")
val t = new Thread {
override def run() {
while (!stopping) {
var state: YarnApplicationState = null
try {
val report = client.getApplicationReport(appId)
state = report.getYarnApplicationState()
} catch {
case e: ApplicationNotFoundException =>
state = YarnApplicationState.KILLED
}
if (state == YarnApplicationState.FINISHED ||
state == YarnApplicationState.KILLED ||
state == YarnApplicationState.FAILED) {
logError(s"Yarn application has already exited with state $state!")
sc.stop()
stopping = true
}
Thread.sleep(1000L)
}
val (state, _) = client.monitorApplication(appId, logApplicationReport = false)
Copy link
Contributor

Choose a reason for hiding this comment

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

The process doesn't look the same. ApplicationNotFoundException is not handled.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we can just add it in Client#monitorApplication as well

logError(s"Yarn application has already exited with state $state!")
sc.stop()
Thread.currentThread().interrupt()
}
}
t.setName("Yarn application state monitor")
t.setDaemon(true)
t.start()
t
}

/**
* Stop the scheduler. This assumes `start()` has already been called.
*/
override def stop() {
assert(client != null, "Attempted to stop this scheduler before starting it!")
stopping = true
monitorThread.interrupt()
super.stop()
client.stop()
logInfo("Stopped")
Expand Down