@@ -21,9 +21,11 @@ import scala.util.control.NonFatal
2121
2222import java .io .{File , IOException }
2323import java .lang .reflect .InvocationTargetException
24- import java .net .{Socket , URL }
24+ import java .net .{NetworkInterface , Socket , URL }
2525import java .util .concurrent .atomic .AtomicReference
2626
27+ import scala .collection .JavaConverters ._
28+
2729import org .apache .hadoop .fs .{FileSystem , Path }
2830import org .apache .hadoop .yarn .api ._
2931import 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