cmccabe commented on code in PR #13461: URL: https://github.com/apache/kafka/pull/13461#discussion_r1171945830
########## core/src/main/scala/kafka/zk/migration/ZkConfigMigrationClient.scala: ########## @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka.zk.migration + +import kafka.server.{ConfigEntityName, ConfigType, DynamicBrokerConfig, DynamicConfig, ZkAdminManager} +import kafka.utils.{Logging, PasswordEncoder} +import kafka.zk.ZkMigrationClient.wrapZkException +import kafka.zk._ +import kafka.zookeeper.{CreateRequest, SetDataRequest} +import org.apache.kafka.common.config.{ConfigDef, ConfigResource} +import org.apache.kafka.common.errors.InvalidRequestException +import org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData +import org.apache.kafka.common.quota.ClientQuotaEntity +import org.apache.kafka.metadata.migration.{ConfigMigrationClient, MigrationClientException, ZkMigrationLeadershipState} +import org.apache.zookeeper.KeeperException.Code +import org.apache.zookeeper.{CreateMode, KeeperException} + +import java.{lang, util} +import java.util.Properties +import java.util.function.BiConsumer +import scala.collection.Seq +import scala.jdk.CollectionConverters._ + +class ZkConfigMigrationClient( + zkClient: KafkaZkClient, + passwordEncoder: PasswordEncoder +) extends ConfigMigrationClient with Logging { + + val adminZkClient = new AdminZkClient(zkClient) + + override def iterateClientQuotas( + quotaEntityConsumer: BiConsumer[util.List[EntityData], util.Map[String, lang.Double]] + ): Unit = { + def migrateEntityType(entityType: String): Unit = { + adminZkClient.fetchAllEntityConfigs(entityType).foreach { case (name, props) => + val entity = List(new EntityData().setEntityType(entityType).setEntityName(name)).asJava + val quotaMap = ZkAdminManager.clientQuotaPropsToDoubleMap(props.asScala).map { + case (key, value) => key -> lang.Double.valueOf(value) + }.toMap.asJava + quotaEntityConsumer.accept(entity, quotaMap) + } + } + + migrateEntityType(ConfigType.User) + migrateEntityType(ConfigType.Client) + + adminZkClient.fetchAllChildEntityConfigs(ConfigType.User, ConfigType.Client).foreach { case (name, props) => + // Taken from ZkAdminManager + val components = name.split("/") + if (components.size != 3 || components(1) != "clients") + throw new IllegalArgumentException(s"Unexpected config path: ${name}") + val entity = List( + new EntityData().setEntityType(ConfigType.User).setEntityName(components(0)), + new EntityData().setEntityType(ConfigType.Client).setEntityName(components(2)) + ) + val quotaMap = props.asScala.map { case (key, value) => + val doubleValue = try lang.Double.valueOf(value) catch { + case _: NumberFormatException => + throw new IllegalStateException(s"Unexpected client quota configuration value: $key -> $value") + } + key -> doubleValue + }.asJava + quotaEntityConsumer.accept(entity.asJava, quotaMap) + } + + migrateEntityType(ConfigType.Ip) + } + + override def iterateBrokerConfigs(configConsumer: BiConsumer[String, util.Map[String, String]]): Unit = { + val brokerEntities = zkClient.getAllEntitiesWithConfig(ConfigType.Broker) + zkClient.getEntitiesConfigs(ConfigType.Broker, brokerEntities.toSet).foreach { case (broker, props) => + val brokerResource = if (broker == ConfigEntityName.Default) { + "" + } else { + broker + } + val decodedProps = props.asScala.map { case (key, value) => + if (DynamicBrokerConfig.isPasswordConfig(key)) + key -> passwordEncoder.decode(value).value + else + key -> value + }.toMap.asJava + + configConsumer.accept(brokerResource, decodedProps) + } + } + + override def writeConfigs( + configResource: ConfigResource, + configMap: util.Map[String, String], + state: ZkMigrationLeadershipState + ): ZkMigrationLeadershipState = wrapZkException { + val configType = configResource.`type`() match { + case ConfigResource.Type.BROKER => Some(ConfigType.Broker) + case ConfigResource.Type.TOPIC => Some(ConfigType.Topic) + case _ => None + } + + val configName = configResource.name() + if (configType.isDefined) { + val props = new Properties() + configMap.forEach { case (key, value) => props.put(key, value) } + tryWriteEntityConfig(configType.get, configName, props, create = false, state) match { + case Some(newState) => + newState + case None => + val createPath = ConfigEntityTypeZNode.path(configType.get) + debug(s"Recursively creating ZNode $createPath and attempting to write $configResource configs a second time.") + zkClient.createRecursive(createPath, throwIfPathExists = false) + + tryWriteEntityConfig(configType.get, configName, props, create = true, state) match { + case Some(newStateSecondTry) => newStateSecondTry + case None => throw new MigrationClientException( + s"Could not write ${configType.get} configs on second attempt when using Create instead of SetData.") + } + } + } else { + debug(s"Not updating ZK for $configResource since it is not a Broker or Topic entity.") Review Comment: I think this should be higher than `debug`. `warn`, even. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org