soarez commented on code in PR #16704: URL: https://github.com/apache/kafka/pull/16704#discussion_r1705629822
########## core/src/test/scala/unit/kafka/metrics/MetricsTest.scala: ########## @@ -120,8 +120,15 @@ class MetricsTest extends KafkaServerTestHarness with Logging { @ValueSource(strings = Array("zk", "kraft")) def testJMXFilter(quorum: String): Unit = { // Check if cluster id metrics is not exposed in JMX - assertTrue(ManagementFactory.getPlatformMBeanServer - .isRegistered(new ObjectName("kafka.controller:type=KafkaController,name=ActiveControllerCount"))) + if (quorum == "zk") { + assertTrue(ManagementFactory.getPlatformMBeanServer + .isRegistered(new ObjectName("kafka.controller:type=KafkaController,name=ActiveControllerCount,brokerId=0"))) + assertTrue(ManagementFactory.getPlatformMBeanServer + .isRegistered(new ObjectName("kafka.controller:type=KafkaController,name=ActiveControllerCount,brokerId=1"))) + } else { + assertTrue(ManagementFactory.getPlatformMBeanServer + .isRegistered(new ObjectName("kafka.controller:type=KafkaController,name=ActiveControllerCount"))) + } Review Comment: Noting that all the changes in this file are unrelated to the bug, just collateral from introducing controller metric tags via the KafkaServer in TestUtils. ########## core/src/test/scala/unit/kafka/controller/KafkaControllerTest.scala: ########## @@ -51,19 +54,20 @@ class KafkaControllerTest { initialBrokerEpoch = 0, tokenManager = mock(classOf[DelegationTokenManager]), brokerFeatures = mock(classOf[BrokerFeatures]), - featureCache = mock(classOf[ZkMetadataCache]) + featureCache = mock(classOf[ZkMetadataCache]), + metricTags = metricTags ) // shutdown kafkaController so that metrics are removed kafkaController.shutdown() val mockMetricsGroup = mockMetricsGroupCtor.constructed.get(0) val numMetricsRegistered = KafkaController.MetricNames.size - verify(mockMetricsGroup, times(numMetricsRegistered)).newGauge(anyString(), any()) - KafkaController.MetricNames.foreach(metricName => verify(mockMetricsGroup).newGauge(ArgumentMatchers.eq(metricName), any())) + verify(mockMetricsGroup, times(numMetricsRegistered)).newGauge(anyString(), any(), ArgumentMatchers.eq(metricTags.asJava)) + KafkaController.MetricNames.foreach(metricName => verify(mockMetricsGroup).newGauge(ArgumentMatchers.eq(metricName), any(), ArgumentMatchers.eq(metricTags.asJava))) // verify that each metric is removed - verify(mockMetricsGroup, times(numMetricsRegistered)).removeMetric(anyString()) - KafkaController.MetricNames.foreach(verify(mockMetricsGroup).removeMetric(_)) + verify(mockMetricsGroup, times(numMetricsRegistered)).removeMetric(anyString(), ArgumentMatchers.eq(metricTags.asJava)) + KafkaController.MetricNames.foreach { name => verify(mockMetricsGroup).removeMetric(ArgumentMatchers.eq(name), ArgumentMatchers.eq(metricTags.asJava)) } Review Comment: Noting the same here, these changes simply deal with the implications of introducing metric tags to the KafkaController. ########## core/src/main/scala/kafka/controller/KafkaController.scala: ########## @@ -539,7 +543,7 @@ class KafkaController(val config: KafkaConfig, } private def removeMetrics(): Unit = { - KafkaController.MetricNames.foreach(metricsGroup.removeMetric) + KafkaController.MetricNames.foreach(metricsGroup.removeMetric(_, metricTags.asJava)) Review Comment: Same here, tags default to an empty map in `metricsGroup.removeMetric(String)`. ########## core/src/main/scala/kafka/controller/KafkaController.scala: ########## @@ -175,20 +179,20 @@ class KafkaController(val config: KafkaConfig, /* single-thread scheduler to clean expired tokens */ private val tokenCleanScheduler = new KafkaScheduler(1, true, "delegation-token-cleaner") - metricsGroup.newGauge(ZkMigrationStateMetricName, () => ZkMigrationState.ZK.value().intValue()) - metricsGroup.newGauge(ActiveControllerCountMetricName, () => if (isActive) 1 else 0) - metricsGroup.newGauge(OfflinePartitionsCountMetricName, () => offlinePartitionCount) - metricsGroup.newGauge(PreferredReplicaImbalanceCountMetricName, () => preferredReplicaImbalanceCount) - metricsGroup.newGauge(ControllerStateMetricName, () => state.value) - metricsGroup.newGauge(GlobalTopicCountMetricName, () => globalTopicCount) - metricsGroup.newGauge(GlobalPartitionCountMetricName, () => globalPartitionCount) - metricsGroup.newGauge(TopicsToDeleteCountMetricName, () => topicsToDeleteCount) - metricsGroup.newGauge(ReplicasToDeleteCountMetricName, () => replicasToDeleteCount) - metricsGroup.newGauge(TopicsIneligibleToDeleteCountMetricName, () => ineligibleTopicsToDeleteCount) - metricsGroup.newGauge(ReplicasIneligibleToDeleteCountMetricName, () => ineligibleReplicasToDeleteCount) - metricsGroup.newGauge(ActiveBrokerCountMetricName, () => activeBrokerCount) + metricsGroup.newGauge(ZkMigrationStateMetricName, () => ZkMigrationState.ZK.value().intValue(), metricTags.asJava) + metricsGroup.newGauge(ActiveControllerCountMetricName, () => if (isActive) 1 else 0, metricTags.asJava) + metricsGroup.newGauge(OfflinePartitionsCountMetricName, () => offlinePartitionCount, metricTags.asJava) + metricsGroup.newGauge(PreferredReplicaImbalanceCountMetricName, () => preferredReplicaImbalanceCount, metricTags.asJava) + metricsGroup.newGauge(ControllerStateMetricName, () => state.value, metricTags.asJava) + metricsGroup.newGauge(GlobalTopicCountMetricName, () => globalTopicCount, metricTags.asJava) + metricsGroup.newGauge(GlobalPartitionCountMetricName, () => globalPartitionCount, metricTags.asJava) + metricsGroup.newGauge(TopicsToDeleteCountMetricName, () => topicsToDeleteCount, metricTags.asJava) + metricsGroup.newGauge(ReplicasToDeleteCountMetricName, () => replicasToDeleteCount, metricTags.asJava) + metricsGroup.newGauge(TopicsIneligibleToDeleteCountMetricName, () => ineligibleTopicsToDeleteCount, metricTags.asJava) + metricsGroup.newGauge(ReplicasIneligibleToDeleteCountMetricName, () => ineligibleReplicasToDeleteCount, metricTags.asJava) + metricsGroup.newGauge(ActiveBrokerCountMetricName, () => activeBrokerCount, metricTags.asJava) // FencedBrokerCount metric is always 0 in the ZK controller. - metricsGroup.newGauge(FencedBrokerCountMetricName, () => 0) + metricsGroup.newGauge(FencedBrokerCountMetricName, () => 0, metricTags.asJava) Review Comment: This new `metricsTags.asJava` argument is always an empty map in non-test paths, which is also the default used by the method overload without the new argument. So this causes no effective changes the controller metrics. ########## core/src/main/scala/kafka/controller/TopicDeletionManager.scala: ########## @@ -97,6 +97,9 @@ class TopicDeletionManager(config: KafkaConfig, if (isDeleteTopicEnabled) { controllerContext.queueTopicDeletion(initialTopicsToBeDeleted) + // We must populate topicsWithDeletionStarted with topics ineligible for deletion because that Set is used + // to check if OfflinePartitionCount metric must be changed or not. Review Comment: ```suggestion // in updatePartitionStateMetrics to gate changes to OfflinePartitionCount via isTopicDeletionInProgress. ``` ########## core/src/test/scala/integration/kafka/controller/OfflinePartitionsFromDeletedTopicTest.scala: ########## @@ -0,0 +1,123 @@ +/* + * 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 integration.kafka.controller + +import com.yammer.metrics.core.Gauge +import kafka.api.IntegrationTestHarness +import kafka.server.IntegrationTestUtils +import kafka.utils.TestUtils +import kafka.zk.KafkaZkClient +import org.apache.kafka.clients.admin.Admin +import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord} +import org.apache.kafka.common.serialization.StringSerializer +import org.apache.kafka.server.metrics.KafkaYammerMetrics +import org.junit.jupiter.api.Assertions.{assertEquals, assertNotEquals} +import org.junit.jupiter.api.{BeforeEach, Test, TestInfo} +import org.slf4j.{Logger, LoggerFactory} + +import java.time.Duration +import java.util.concurrent.TimeUnit +import scala.jdk.CollectionConverters._ + + +class OfflinePartitionsFromDeletedTopicTest extends IntegrationTestHarness { Review Comment: There's a new preferred approach to integration testing. See #16500. cc @chia7712 ########## core/src/main/scala/kafka/controller/KafkaController.scala: ########## @@ -111,7 +111,11 @@ class KafkaController(val config: KafkaConfig, tokenManager: DelegationTokenManager, brokerFeatures: BrokerFeatures, featureCache: ZkFinalizedFeatureCache, - threadNamePrefix: Option[String] = None) + threadNamePrefix: Option[String] = None, + // KafkaYammerMetrics uses a static singleton that is shared across the JVM + // Therefore, these are populated in tests with brokerId to disambiguate between metrics for + // different nodes Review Comment: There may be some missing part of the explanation here. At any point there should only be a single active controller, so the need to disambiguate metrics isn't clear from this comment. -- 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