Improve performance of JoinConfigCompatChecker (#30801)

This commit is contained in:
Dragisa Krsmanovic 2021-10-19 04:12:42 -07:00 committed by GitHub
parent 70120060b8
commit d7fe31f3a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,8 +4,6 @@
package akka.cluster package akka.cluster
import java.util
import scala.collection.{ immutable => im } import scala.collection.{ immutable => im }
import com.typesafe.config.{ Config, ConfigFactory, ConfigValue } import com.typesafe.config.{ Config, ConfigFactory, ConfigValue }
@ -73,18 +71,18 @@ object JoinConfigCompatChecker {
toCheck: Config, toCheck: Config,
actualConfig: Config): ConfigValidation = { actualConfig: Config): ConfigValidation = {
def checkCompat(entry: util.Map.Entry[String, ConfigValue]) = { def checkCompat(key: String, value: ConfigValue) = {
val key = entry.getKey actualConfig.hasPath(key) && actualConfig.getValue(key) == value
actualConfig.hasPath(key) && actualConfig.getValue(key) == entry.getValue
} }
// retrieve all incompatible keys // retrieve all incompatible keys
// NOTE: we only check the key if effectively required // NOTE: we only check the key if effectively required
// because config may contain more keys than required for this checker // because config may contain more keys than required for this checker
val incompatibleKeys =
toCheck.entrySet().asScala.collect { val incompatibleKeys = for {
case entry if keys.contains(entry.getKey) && !checkCompat(entry) => s"${entry.getKey} is incompatible" key <- keys if toCheck.hasPath(key)
} value = toCheck.getValue(key) if !checkCompat(key, value)
} yield s"$key is incompatible"
if (incompatibleKeys.isEmpty) Valid if (incompatibleKeys.isEmpty) Valid
else Invalid(incompatibleKeys.to(im.Seq)) else Invalid(incompatibleKeys.to(im.Seq))
@ -102,10 +100,9 @@ object JoinConfigCompatChecker {
@ccompatUsedUntil213 @ccompatUsedUntil213
private[cluster] def filterWithKeys(requiredKeys: im.Seq[String], config: Config): Config = { private[cluster] def filterWithKeys(requiredKeys: im.Seq[String], config: Config): Config = {
val filtered = val filtered = for {
config.entrySet().asScala.collect { key <- requiredKeys if config.hasPath(key)
case e if requiredKeys.contains(e.getKey) => (e.getKey, e.getValue) } yield (key, config.getValue(key))
}
ConfigFactory.parseMap(filtered.toMap.asJava) ConfigFactory.parseMap(filtered.toMap.asJava)
} }