squito commented on a change in pull request #23260: [SPARK-26311][YARN] New feature: custom log URL for stdout/stderr URL: https://github.com/apache/spark/pull/23260#discussion_r248853445
########## File path: core/src/main/scala/org/apache/spark/deploy/history/HistoryAppStatusListener.scala ########## @@ -0,0 +1,93 @@ +/* + * 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.history + +import org.apache.spark.SparkConf +import org.apache.spark.internal.config.History._ +import org.apache.spark.scheduler.SparkListenerExecutorAdded +import org.apache.spark.scheduler.cluster.ExecutorInfo +import org.apache.spark.status.{AppStatusListener, AppStatusSource, ElementTrackingStore} + +private[spark] class HistoryAppStatusListener( + kvstore: ElementTrackingStore, + conf: SparkConf, + live: Boolean, + appStatusSource: Option[AppStatusSource] = None, + lastUpdateTime: Option[Long] = None) + extends AppStatusListener(kvstore, conf, live, appStatusSource, lastUpdateTime) { + + override def onExecutorAdded(event: SparkListenerExecutorAdded): Unit = { + val execInfo = event.executorInfo + val newExecInfo = new ExecutorInfo(execInfo.executorHost, execInfo.totalCores, + renewLogUrls(execInfo), execInfo.attributes) + + super.onExecutorAdded(event.copy(executorInfo = newExecInfo)) + } + + def renewLogUrls(execInfo: ExecutorInfo): Map[String, String] = { + val oldLogUrlMap = execInfo.logUrlMap + val attributes = execInfo.attributes + + conf.get(CUSTOM_EXECUTOR_LOG_URL) match { + case Some(logUrlPattern) => + val pattern = "\\{\\{([A-Za-z0-9_\\-]+)\\}\\}".r + + val allPatterns = pattern.findAllMatchIn(logUrlPattern).map(_.group(1)).toSet + val allPatternsExceptFileName = allPatterns.filter(_ != "FILE_NAME") + val allAttributeKeys = attributes.keys.toSet + val allAttributeKeysExceptLogFiles = allAttributeKeys.filter(_ != "LOG_FILES") + + if (allPatternsExceptFileName.diff(allAttributeKeysExceptLogFiles).nonEmpty) { + logFailToRenewLogUrls("some of required attributes are missing in app's event log.", + allPatternsExceptFileName, allAttributeKeys) + return oldLogUrlMap + } else if (allPatterns.contains("FILE_NAME") && !allAttributeKeys.contains("LOG_FILES")) { + logFailToRenewLogUrls("'FILE_NAME' parameter is provided, but file information is " + + "missing in app's event log.", allPatternsExceptFileName, allAttributeKeys) + return oldLogUrlMap + } + + var replacingUrl = logUrlPattern + + allPatternsExceptFileName.foreach { pattern => + // we already checked the existence of attribute when comparing keys + replacingUrl = replacingUrl.replace(s"{{$pattern}}", attributes(pattern)) + } Review comment: `replacingUrl` -> `updatedUrl` you could also make this more functional and avoid the mutable state, eg. ```scala val updatedUrl = allPatternsExceptionFileName.foldLeft(logUrlPattern) { case( orig, pattern) => orig.replace(...) } ``` ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org