Skip to content

Commit d8f69cf

Browse files
ryan-williamspwendell
authored andcommitted
[SPARK-5778] throw if nonexistent metrics config file provided
previous behavior was to log an error; this is fine in the general case where no `spark.metrics.conf` parameter was specified, in which case a default `metrics.properties` is looked for, and the execption logged and suppressed if it doesn't exist. if the user has purposefully specified a metrics.conf file, however, it makes more sense to show them an error when said file doesn't exist. Author: Ryan Williams <ryan.blake.williams@gmail.com> Closes apache#4571 from ryan-williams/metrics and squashes the following commits: 5bccb14 [Ryan Williams] private-ize some MetricsConfig members 08ff998 [Ryan Williams] rename METRICS_CONF: DEFAULT_METRICS_CONF_FILENAME f4d7fab [Ryan Williams] fix tests ad24b0e [Ryan Williams] add "metrics.properties" to .rat-excludes 94e810b [Ryan Williams] throw if nonexistent Sink class is specified 31d2c30 [Ryan Williams] metrics code review feedback 56287db [Ryan Williams] throw if nonexistent metrics config file provided
1 parent d8adefe commit d8f69cf

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

.rat-excludes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fairscheduler.xml.template
1919
spark-defaults.conf.template
2020
log4j.properties
2121
log4j.properties.template
22+
metrics.properties
2223
metrics.properties.template
2324
slaves
2425
slaves.template

core/src/main/scala/org/apache/spark/metrics/MetricsConfig.scala

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ import org.apache.spark.util.Utils
2828

2929
private[spark] class MetricsConfig(val configFile: Option[String]) extends Logging {
3030

31-
val DEFAULT_PREFIX = "*"
32-
val INSTANCE_REGEX = "^(\\*|[a-zA-Z]+)\\.(.+)".r
33-
val METRICS_CONF = "metrics.properties"
31+
private val DEFAULT_PREFIX = "*"
32+
private val INSTANCE_REGEX = "^(\\*|[a-zA-Z]+)\\.(.+)".r
33+
private val DEFAULT_METRICS_CONF_FILENAME = "metrics.properties"
3434

35-
val properties = new Properties()
36-
var propertyCategories: mutable.HashMap[String, Properties] = null
35+
private[metrics] val properties = new Properties()
36+
private[metrics] var propertyCategories: mutable.HashMap[String, Properties] = null
3737

3838
private def setDefaultProperties(prop: Properties) {
3939
prop.setProperty("*.sink.servlet.class", "org.apache.spark.metrics.sink.MetricsServlet")
@@ -47,20 +47,22 @@ private[spark] class MetricsConfig(val configFile: Option[String]) extends Loggi
4747
setDefaultProperties(properties)
4848

4949
// If spark.metrics.conf is not set, try to get file in class path
50-
var is: InputStream = null
51-
try {
52-
is = configFile match {
53-
case Some(f) => new FileInputStream(f)
54-
case None => Utils.getSparkClassLoader.getResourceAsStream(METRICS_CONF)
50+
val isOpt: Option[InputStream] = configFile.map(new FileInputStream(_)).orElse {
51+
try {
52+
Option(Utils.getSparkClassLoader.getResourceAsStream(DEFAULT_METRICS_CONF_FILENAME))
53+
} catch {
54+
case e: Exception =>
55+
logError("Error loading default configuration file", e)
56+
None
5557
}
58+
}
5659

57-
if (is != null) {
60+
isOpt.foreach { is =>
61+
try {
5862
properties.load(is)
63+
} finally {
64+
is.close()
5965
}
60-
} catch {
61-
case e: Exception => logError("Error loading configure file", e)
62-
} finally {
63-
if (is != null) is.close()
6466
}
6567

6668
propertyCategories = subProperties(properties, INSTANCE_REGEX)

core/src/main/scala/org/apache/spark/metrics/MetricsSystem.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ private[spark] class MetricsSystem private (
191191
sinks += sink.asInstanceOf[Sink]
192192
}
193193
} catch {
194-
case e: Exception => logError("Sink class " + classPath + " cannot be instantialized", e)
194+
case e: Exception => {
195+
logError("Sink class " + classPath + " cannot be instantialized")
196+
throw e
197+
}
195198
}
196199
}
197200
}

core/src/test/resources/test_metrics_system.properties

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@
1818
*.sink.console.period = 10
1919
*.sink.console.unit = seconds
2020
test.sink.console.class = org.apache.spark.metrics.sink.ConsoleSink
21-
test.sink.dummy.class = org.apache.spark.metrics.sink.DummySink
22-
test.source.dummy.class = org.apache.spark.metrics.source.DummySource
2321
test.sink.console.period = 20
2422
test.sink.console.unit = minutes

core/src/test/scala/org/apache/spark/metrics/MetricsConfigSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MetricsConfigSuite extends FunSuite with BeforeAndAfter {
2727
}
2828

2929
test("MetricsConfig with default properties") {
30-
val conf = new MetricsConfig(Option("dummy-file"))
30+
val conf = new MetricsConfig(None)
3131
conf.initialize()
3232

3333
assert(conf.properties.size() === 4)

0 commit comments

Comments
 (0)