HeartSaVioR commented on a change in pull request #23260: [SPARK-26311][CORE] New feature: apply custom log URL pattern for executor log URLs in SHS URL: https://github.com/apache/spark/pull/23260#discussion_r252068756
########## File path: resource-managers/yarn/src/main/scala/org/apache/spark/util/YarnContainerInfoHelper.scala ########## @@ -0,0 +1,103 @@ +/* + * 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.util + +import org.apache.hadoop.HadoopIllegalArgumentException +import org.apache.hadoop.conf.Configuration +import org.apache.hadoop.yarn.api.ApplicationConstants.Environment +import org.apache.hadoop.yarn.api.records.{Container, ContainerId} +import org.apache.hadoop.yarn.conf.YarnConfiguration +import org.apache.hadoop.yarn.util.ConverterUtils + +import org.apache.spark.deploy.yarn.YarnSparkHadoopUtil +import org.apache.spark.internal.Logging + +private[spark] object YarnContainerInfoHelper extends Logging { + def getLogUrls( + conf: Configuration, + container: Option[Container]): Option[Map[String, String]] = { + try { + val yarnConf = new YarnConfiguration(conf) + + val containerId = getContainerId(container) + val user = Utils.getCurrentUserName() + val httpScheme = getYarnHttpScheme(yarnConf) + val httpAddress = getNodeManagerHttpAddress(container) + + val baseUrl = s"$httpScheme$httpAddress/node/containerlogs/$containerId/$user" + logDebug(s"Base URL for logs: $baseUrl") + + Some(Map( + "stdout" -> s"$baseUrl/stdout?start=-4096", + "stderr" -> s"$baseUrl/stderr?start=-4096")) + } catch { + case e: Exception => + logInfo("Error while building executor logs - executor logs will not be available", e) + None + } + } + + def getAttributes( + conf: Configuration, + container: Option[Container]): Option[Map[String, String]] = { + try { + val yarnConf = new YarnConfiguration(conf) + Some(Map( + "HTTP_SCHEME" -> getYarnHttpScheme(yarnConf), + "NM_HTTP_ADDRESS" -> getNodeManagerHttpAddress(container), + "CLUSTER_ID" -> getClusterId(yarnConf).getOrElse(""), + "CONTAINER_ID" -> ConverterUtils.toString(getContainerId(container)), + "USER" -> Utils.getCurrentUserName(), + "LOG_FILES" -> "stderr,stdout" + )) + } catch { + case e: Exception => + logInfo("Error while retrieving executor attributes - executor logs will not be replaced " + + "with custom log pattern", e) + None + } + } + + def getContainerId(container: Option[Container]): ContainerId = container match { + case Some(c) => c.getId + case None => YarnSparkHadoopUtil.getContainerId + } + + def getClusterId(yarnConf: YarnConfiguration): Option[String] = { + try { + Some(YarnConfiguration.getClusterId(yarnConf)) + } catch { + case _: HadoopIllegalArgumentException => None + } + } + + def getYarnHttpScheme(yarnConf: YarnConfiguration): String = { + // lookup appropriate http scheme for container log urls + val yarnHttpPolicy = yarnConf.get( + YarnConfiguration.YARN_HTTP_POLICY_KEY, + YarnConfiguration.YARN_HTTP_POLICY_DEFAULT + ) + if (yarnHttpPolicy == "HTTPS_ONLY") "https://" else "http://" + } + + def getNodeManagerHttpAddress(container: Option[Container]): String = container match { + case Some(c) => c.getNodeHttpAddress + case None => System.getenv(Environment.NM_HOST.name()) + ":" + Review comment: > Seem like we have to figure out how to have the executor build and return this data. I'm working on the patch - I guess I need to clean up the code, but below commit works and passes the tests. https://github.com/HeartSaVioR/spark/commit/4ff6cb5096b0708892f93f05c953b2861ed7a281 I guess we continue review on the new change once I apply above commit to here - could we do it in new JIRA issue/PR instead? We already put lots of efforts (review, apply, new requirements) on this PR and I want to make it just be done soon. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
