diff --git a/core/src/main/scala/org/apache/spark/metrics/MetricsConfig.scala b/core/src/main/scala/org/apache/spark/metrics/MetricsConfig.scala index a4056508c181..e459e196b522 100644 --- a/core/src/main/scala/org/apache/spark/metrics/MetricsConfig.scala +++ b/core/src/main/scala/org/apache/spark/metrics/MetricsConfig.scala @@ -58,7 +58,7 @@ private[spark] class MetricsConfig(conf: SparkConf) extends Logging { val prefix = "spark.metrics.conf." conf.getAll.foreach { case (k, v) if k.startsWith(prefix) => - properties.setProperty(k.substring(prefix.length()), v) + properties.setProperty(k.substring(prefix.length()).trim(), v.trim()) case _ => } @@ -78,7 +78,7 @@ private[spark] class MetricsConfig(conf: SparkConf) extends Logging { val defaultSubProperties = perInstanceSubProperties(DEFAULT_PREFIX).asScala for ((instance, prop) <- perInstanceSubProperties if (instance != DEFAULT_PREFIX); (k, v) <- defaultSubProperties if (prop.get(k) == null)) { - prop.put(k, v) + prop.put(k.trim(), v.trim()) } } } diff --git a/core/src/test/scala/org/apache/spark/metrics/MetricsConfigSuite.scala b/core/src/test/scala/org/apache/spark/metrics/MetricsConfigSuite.scala index a85011b42bbc..cff58d89ea1b 100644 --- a/core/src/test/scala/org/apache/spark/metrics/MetricsConfigSuite.scala +++ b/core/src/test/scala/org/apache/spark/metrics/MetricsConfigSuite.scala @@ -159,6 +159,20 @@ class MetricsConfigSuite extends SparkFunSuite with BeforeAndAfter { assert(servletProps.size() === 2) } + test("MetricsConfig with properties with spaces") { + val sparkConf = new SparkConf(loadDefaults = false) + setMetricsProperty(sparkConf, "*.sink.console.class", + " org.apache.spark.metrics.sink.ConsoleSink ") + setMetricsProperty(sparkConf, "*.sink.console.period", "10 ") + val conf = new MetricsConfig(sparkConf) + conf.initialize() + + val property = conf.getInstance("random") + assert(property.getProperty("sink.console.class") === + "org.apache.spark.metrics.sink.ConsoleSink") + assert(property.getProperty("sink.console.period") === "10") + } + private def setMetricsProperty(conf: SparkConf, name: String, value: String): Unit = { conf.set(s"spark.metrics.conf.$name", value) }