MetricsConfig的主要功能是如果某一个分类有特定配置,则使用,否则使用默认配置。首先系统先设置默认配置,如setDefaultProperties方法:
private def setDefaultProperties(prop: Properties) {
prop.setProperty("*.sink.servlet.class", "org.apache.spark.metrics.sink.MetricsServlet")
prop.setProperty("*.sink.servlet.path", "/metrics/json")
prop.setProperty("master.sink.servlet.path", "/metrics/master/json")
prop.setProperty("applications.sink.servlet.path", "/metrics/applications/json")
}
在初始化方法里,首先设置默认配置,然后从配置文件加载配置,加载后同一个properties里,然后从配置conf里加载配置。
然后用subProperties对所有的配置进行分类。
最后判断是否分类中有默认配置,如果有,则对每一个分类进行判断是否分类中没有默认配置的这一项,如果有,则设置成默认配置。
def initialize() {
// Add default properties in case there's no properties file
setDefaultProperties(properties)
loadPropertiesFromFile(conf.getOption("spark.metrics.conf"))
// Also look for the properties in provided Spark configuration
val prefix = "spark.metrics.conf."
conf.getAll.foreach {
case (k, v) if k.startsWith(prefix) =>
properties.setProperty(k.substring(prefix.length()), v)
case _ =>
}
propertyCategories = subProperties(properties, INSTANCE_REGEX)
if (propertyCategories.contains(DEFAULT_PREFIX)) {
val defaultProperty = propertyCategories(DEFAULT_PREFIX).asScala
for((inst, prop) <- propertyCategories if (inst != DEFAULT_PREFIX);
(k, v) <- defaultProperty if (prop.get(k) == null)) {
prop.put(k, v)
}
}
}
对配置进行分类的方法:
def subProperties(prop: Properties, regex: Regex): mutable.HashMap[String, Properties] = {
val subProperties = new mutable.HashMap[String, Properties]
prop.asScala.foreach { kv =>
if (regex.findPrefixOf(kv._1.toString).isDefined) {
val regex(prefix, suffix) = kv._1.toString
subProperties.getOrElseUpdate(prefix, new Properties).setProperty(suffix, kv._2.toString)
}
}
subProperties
}