Skip to content

Commit 402e8e4

Browse files
Use NodeReport to get the URL for the logs. Also, make the environment variables generic so other cluster managers can use them as well.
1 parent 1cf338f commit 402e8e4

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

core/src/main/scala/org/apache/spark/SparkContext.scala

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,21 +2432,13 @@ object SparkContext extends Logging {
24322432
}
24332433
}
24342434
scheduler.initialize(backend)
2435-
val logUrl = System.getProperty("spark.yarn.driver.log.url")
2436-
if (logUrl != null) {
2437-
// lookup appropriate http scheme for container log urls
2438-
val yarnConf: YarnConfiguration = new YarnConfiguration(sc.hadoopConfiguration)
2439-
val yarnHttpPolicy = yarnConf.get(
2440-
YarnConfiguration.YARN_HTTP_POLICY_KEY,
2441-
YarnConfiguration.YARN_HTTP_POLICY_DEFAULT
2442-
)
2443-
val httpScheme = if (yarnHttpPolicy == "HTTPS_ONLY") "https://" else "http://"
2444-
val baseUrl = s"$httpScheme$logUrl"
2445-
sc.logUrls =
2446-
Some(Predef.Map(
2447-
"stderr" -> s"$baseUrl/stderr?start=0",
2448-
"stdout" -> s"$baseUrl/stdout?start=0"))
2449-
}
2435+
val logPrefix = "spark.driver.log."
2436+
sc.logUrls = Option(
2437+
System.getProperties.stringPropertyNames()
2438+
.filter(_.startsWith(logPrefix))
2439+
.map (key => (key.substring(logPrefix.length), System.getProperty(key)))
2440+
.toMap
2441+
)
24502442
(backend, scheduler)
24512443

24522444
case "yarn-client" =>

yarn/src/main/scala/org/apache/spark/deploy/yarn/ApplicationMaster.scala

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import scala.util.control.NonFatal
2121

2222
import java.io.{File, IOException}
2323
import java.lang.reflect.InvocationTargetException
24-
import java.net.{Socket, URL}
24+
import java.net.{NetworkInterface, Socket, URL}
2525
import java.util.concurrent.atomic.AtomicReference
2626

27+
import scala.collection.JavaConverters._
28+
2729
import org.apache.hadoop.fs.{FileSystem, Path}
2830
import org.apache.hadoop.yarn.api._
2931
import org.apache.hadoop.yarn.api.records._
@@ -270,9 +272,43 @@ private[spark] class ApplicationMaster(
270272
val yarnClient = YarnClient.createYarnClient()
271273
yarnClient.init(yarnConf)
272274
yarnClient.start()
273-
val logUrl = yarnClient.getContainerReport(containerId).getLogUrl
274-
System.setProperty("spark.yarn.driver.log.url", logUrl)
275-
logInfo(s"Driver logs are at $logUrl")
275+
val addresses =
276+
NetworkInterface.getNetworkInterfaces.asScala
277+
.flatMap(_.getInetAddresses.asScala)
278+
.toSeq
279+
try {
280+
val nodeReports = yarnClient.getNodeReports(NodeState.RUNNING).asScala
281+
val nodeReport =
282+
nodeReports.find { x =>
283+
val host = x.getNodeId.getHost
284+
addresses.exists { address =>
285+
address.getHostAddress == host ||
286+
address.getHostName == host ||
287+
address.getCanonicalHostName == host
288+
}
289+
}
290+
291+
nodeReport.foreach { report =>
292+
val httpAddress = report.getHttpAddress
293+
// lookup appropriate http scheme for container log urls
294+
val yarnHttpPolicy = yarnConf.get(
295+
YarnConfiguration.YARN_HTTP_POLICY_KEY,
296+
YarnConfiguration.YARN_HTTP_POLICY_DEFAULT
297+
)
298+
val user = Utils.getCurrentUserName()
299+
val httpScheme = if (yarnHttpPolicy == "HTTPS_ONLY") "https://" else "http://"
300+
val baseUrl = s"$httpScheme$httpAddress/node/containerlogs/$containerId/$user"
301+
logInfo(s"Base URL for logs: $baseUrl")
302+
System.setProperty("spark.driver.log.stderr", s"$baseUrl/stderr?start=0")
303+
System.setProperty("spark.driver.log.stdout", s"$baseUrl/stdout?start=0")
304+
}
305+
} catch {
306+
case e: Exception =>
307+
logInfo("Container Report API is not available in the version of YARN being used, so AM" +
308+
" logs link will not appear in application UI")
309+
} finally {
310+
yarnClient.close()
311+
}
276312
}
277313
userClassThread = startUserApplication()
278314

0 commit comments

Comments
 (0)