-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-16272][core] Allow config values to reference conf, env, system props. #14022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f4e772a
[SPARK-16272][core] Allow config values to reference conf, env, syste…
240ab73
Allow chained references.
392bddc
Use SQL-style prefixes for the references.
b928a55
Merge branch 'master' into SPARK-16272
24ccef0
Apply substitution to all configs.
cca8314
Use Regex.replaceAllIn.
6618bc4
Don't error out on unknown prefixes.
3afe221
Drop "spark." requirement.
33de81f
Revert "Drop "spark." requirement."
ed5c18b
Expand default values that are strings.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
|
|
||
| package org.apache.spark.internal.config | ||
|
|
||
| import java.util.{Map => JMap} | ||
|
|
||
| import org.apache.spark.SparkConf | ||
|
|
||
| /** | ||
|
|
@@ -40,19 +42,32 @@ private[spark] abstract class ConfigEntry[T] ( | |
| val valueConverter: String => T, | ||
| val stringConverter: T => String, | ||
| val doc: String, | ||
| val isPublic: Boolean) { | ||
| val isPublic: Boolean, | ||
| private val expandVars: Boolean) { | ||
|
|
||
| import ConfigEntry._ | ||
|
|
||
| registerEntry(this) | ||
|
|
||
| def defaultValueString: String | ||
|
|
||
| def readFrom(conf: SparkConf): T | ||
| def readFrom(conf: JMap[String, String], getenv: String => String): T | ||
|
|
||
| // This is used by SQLConf, since it doesn't use SparkConf to store settings and thus cannot | ||
| // use readFrom(). | ||
| def defaultValue: Option[T] = None | ||
|
|
||
| override def toString: String = { | ||
| s"ConfigEntry(key=$key, defaultValue=$defaultValueString, doc=$doc, public=$isPublic)" | ||
| } | ||
|
|
||
| protected def readAndExpand( | ||
| conf: JMap[String, String], | ||
| getenv: String => String, | ||
| usedRefs: Set[String] = Set()): Option[String] = { | ||
| Option(conf.get(key)).map { value => | ||
| if (expandVars) expand(value, conf, getenv, usedRefs) else value | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private class ConfigEntryWithDefault[T] ( | ||
|
|
@@ -61,15 +76,16 @@ private class ConfigEntryWithDefault[T] ( | |
| valueConverter: String => T, | ||
| stringConverter: T => String, | ||
| doc: String, | ||
| isPublic: Boolean) | ||
| extends ConfigEntry(key, valueConverter, stringConverter, doc, isPublic) { | ||
| isPublic: Boolean, | ||
| private val expandVars: Boolean) | ||
| extends ConfigEntry(key, valueConverter, stringConverter, doc, isPublic, expandVars) { | ||
|
|
||
| override def defaultValue: Option[T] = Some(_defaultValue) | ||
|
|
||
| override def defaultValueString: String = stringConverter(_defaultValue) | ||
|
|
||
| override def readFrom(conf: SparkConf): T = { | ||
| conf.getOption(key).map(valueConverter).getOrElse(_defaultValue) | ||
| def readFrom(conf: JMap[String, String], getenv: String => String): T = { | ||
| readAndExpand(conf, getenv).map(valueConverter).getOrElse(_defaultValue) | ||
| } | ||
|
|
||
| } | ||
|
|
@@ -82,13 +98,16 @@ private[spark] class OptionalConfigEntry[T]( | |
| val rawValueConverter: String => T, | ||
| val rawStringConverter: T => String, | ||
| doc: String, | ||
| isPublic: Boolean) | ||
| isPublic: Boolean, | ||
| private val expandVars: Boolean) | ||
| extends ConfigEntry[Option[T]](key, s => Some(rawValueConverter(s)), | ||
| v => v.map(rawStringConverter).orNull, doc, isPublic) { | ||
| v => v.map(rawStringConverter).orNull, doc, isPublic, expandVars) { | ||
|
|
||
| override def defaultValueString: String = "<undefined>" | ||
|
|
||
| override def readFrom(conf: SparkConf): Option[T] = conf.getOption(key).map(rawValueConverter) | ||
| override def readFrom(conf: JMap[String, String], getenv: String => String): Option[T] = { | ||
| readAndExpand(conf, getenv).map(rawValueConverter) | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
@@ -99,13 +118,83 @@ private class FallbackConfigEntry[T] ( | |
| key: String, | ||
| doc: String, | ||
| isPublic: Boolean, | ||
| private val fallback: ConfigEntry[T]) | ||
| extends ConfigEntry[T](key, fallback.valueConverter, fallback.stringConverter, doc, isPublic) { | ||
| private[config] val fallback: ConfigEntry[T], | ||
| private val expandVars: Boolean) | ||
| extends ConfigEntry[T]( | ||
| key, | ||
| fallback.valueConverter, | ||
| fallback.stringConverter, | ||
| doc, | ||
| isPublic, | ||
| expandVars) { | ||
|
|
||
| override def defaultValueString: String = s"<value of ${fallback.key}>" | ||
|
|
||
| override def readFrom(conf: SparkConf): T = { | ||
| conf.getOption(key).map(valueConverter).getOrElse(fallback.readFrom(conf)) | ||
| override def readFrom(conf: JMap[String, String], getenv: String => String): T = { | ||
| Option(conf.get(key)).map(valueConverter).getOrElse(fallback.readFrom(conf, getenv)) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private object ConfigEntry { | ||
|
|
||
| private val knownConfigs = new java.util.concurrent.ConcurrentHashMap[String, ConfigEntry[_]]() | ||
|
|
||
| private val REF_RE = "\\$\\{(?:(\\w+?):)?(\\S+?)\\}".r.pattern | ||
|
|
||
| def registerEntry(entry: ConfigEntry[_]): Unit = { | ||
| val existing = knownConfigs.putIfAbsent(entry.key, entry) | ||
| require(existing == null, s"Config entry ${entry.key} already registered!") | ||
| } | ||
|
|
||
| def findEntry(key: String): ConfigEntry[_] = knownConfigs.get(key) | ||
|
|
||
| /** | ||
| * Expand the `value` according to the rules explained in `ConfigBuilder.withVariableExpansion`. | ||
| */ | ||
| def expand( | ||
| value: String, | ||
| conf: JMap[String, String], | ||
| getenv: String => String, | ||
| usedRefs: Set[String]): String = { | ||
| val matcher = REF_RE.matcher(value) | ||
| val result = new StringBuilder() | ||
| var end = 0 | ||
|
|
||
| while (end < value.length() && matcher.find(end)) { | ||
| val prefix = matcher.group(1) | ||
| val name = matcher.group(2) | ||
|
|
||
| result.append(value.substring(end, matcher.start())) | ||
|
|
||
| val replacement = prefix match { | ||
| case null => | ||
| require(!usedRefs.contains(name), s"Circular reference in $value: $name") | ||
| Option(findEntry(name)) | ||
| .flatMap(_.readAndExpand(conf, getenv, usedRefs = usedRefs + name)) | ||
| .orElse(Option(conf.get(name))) | ||
| .orElse(defaultValueString(name)) | ||
| case "system" => sys.props.get(name) | ||
| case "env" => Option(getenv(name)) | ||
| case _ => throw new IllegalArgumentException(s"Invalid prefix: $prefix") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this throw?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I have to take a look at this. Throwing might actually break some existing code in SparkHadoopUtil. |
||
| } | ||
|
|
||
| result.append(replacement.getOrElse(matcher.group(0))) | ||
| end = matcher.end() | ||
| } | ||
|
|
||
| if (end < value.length()) { | ||
| result.append(value.substring(end, value.length())) | ||
| } | ||
| result.toString() | ||
| } | ||
|
|
||
| private def defaultValueString(key: String): Option[String] = { | ||
| findEntry(key) match { | ||
| case e: ConfigEntryWithDefault[_] => Some(e.defaultValueString) | ||
| case e: FallbackConfigEntry[_] => defaultValueString(e.fallback.key) | ||
| case _ => None | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use Regex#replaceAllIn here instead of a loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks interesting, let me take a look.