From d7fe31f3a8d4b1e6ab4fb904926116c5f3f45b38 Mon Sep 17 00:00:00 2001 From: Dragisa Krsmanovic Date: Tue, 19 Oct 2021 04:12:42 -0700 Subject: [PATCH] Improve performance of JoinConfigCompatChecker (#30801) --- .../cluster/JoinConfigCompatChecker.scala | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/akka-cluster/src/main/scala/akka/cluster/JoinConfigCompatChecker.scala b/akka-cluster/src/main/scala/akka/cluster/JoinConfigCompatChecker.scala index 6c18295ad1..a239ef3632 100644 --- a/akka-cluster/src/main/scala/akka/cluster/JoinConfigCompatChecker.scala +++ b/akka-cluster/src/main/scala/akka/cluster/JoinConfigCompatChecker.scala @@ -4,8 +4,6 @@ package akka.cluster -import java.util - import scala.collection.{ immutable => im } import com.typesafe.config.{ Config, ConfigFactory, ConfigValue } @@ -73,18 +71,18 @@ object JoinConfigCompatChecker { toCheck: Config, actualConfig: Config): ConfigValidation = { - def checkCompat(entry: util.Map.Entry[String, ConfigValue]) = { - val key = entry.getKey - actualConfig.hasPath(key) && actualConfig.getValue(key) == entry.getValue + def checkCompat(key: String, value: ConfigValue) = { + actualConfig.hasPath(key) && actualConfig.getValue(key) == value } // retrieve all incompatible keys // NOTE: we only check the key if effectively required // because config may contain more keys than required for this checker - val incompatibleKeys = - toCheck.entrySet().asScala.collect { - case entry if keys.contains(entry.getKey) && !checkCompat(entry) => s"${entry.getKey} is incompatible" - } + + val incompatibleKeys = for { + key <- keys if toCheck.hasPath(key) + value = toCheck.getValue(key) if !checkCompat(key, value) + } yield s"$key is incompatible" if (incompatibleKeys.isEmpty) Valid else Invalid(incompatibleKeys.to(im.Seq)) @@ -102,10 +100,9 @@ object JoinConfigCompatChecker { @ccompatUsedUntil213 private[cluster] def filterWithKeys(requiredKeys: im.Seq[String], config: Config): Config = { - val filtered = - config.entrySet().asScala.collect { - case e if requiredKeys.contains(e.getKey) => (e.getKey, e.getValue) - } + val filtered = for { + key <- requiredKeys if config.hasPath(key) + } yield (key, config.getValue(key)) ConfigFactory.parseMap(filtered.toMap.asJava) }