-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-14866: Remove controller module metrics when broker is shutting…
… down (#13473) Reviewers: Mickael Maison <mickael.maison@gmail.com>, Divij Vaidya <diviv@amazon.com>
- Loading branch information
Showing
2 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
core/src/test/scala/unit/kafka/controller/KafkaControllerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* 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.controller | ||
|
||
import kafka.server.metadata.ZkMetadataCache | ||
import kafka.server.{BrokerFeatures, DelegationTokenManager, KafkaConfig} | ||
import kafka.utils.{MockTime, TestUtils} | ||
import kafka.zk.{BrokerInfo, KafkaZkClient} | ||
import org.apache.kafka.common.metrics.Metrics | ||
import org.apache.kafka.server.metrics.KafkaMetricsGroup | ||
import org.junit.jupiter.api.{BeforeEach, Test} | ||
import org.mockito.ArgumentMatchers | ||
import org.mockito.ArgumentMatchers.{any, anyString} | ||
import org.mockito.Mockito.{mock, mockConstruction, times, verify, verifyNoMoreInteractions} | ||
|
||
class KafkaControllerTest { | ||
var config: KafkaConfig = _ | ||
|
||
@BeforeEach | ||
def setUp(): Unit = { | ||
val props = TestUtils.createBrokerConfig(1, TestUtils.MockZkConnect) | ||
config = KafkaConfig.fromProps(props) | ||
} | ||
|
||
@Test | ||
def testRemoveMetricsOnClose(): Unit = { | ||
val mockMetricsGroupCtor = mockConstruction(classOf[KafkaMetricsGroup]) | ||
try { | ||
val kafkaController = new KafkaController( | ||
config = config, | ||
zkClient = mock(classOf[KafkaZkClient]), | ||
time = new MockTime(), | ||
metrics = mock(classOf[Metrics]), | ||
initialBrokerInfo = mock(classOf[BrokerInfo]), | ||
initialBrokerEpoch = 0, | ||
tokenManager = mock(classOf[DelegationTokenManager]), | ||
brokerFeatures = mock(classOf[BrokerFeatures]), | ||
featureCache = mock(classOf[ZkMetadataCache]) | ||
) | ||
|
||
// 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 that each metric is removed | ||
verify(mockMetricsGroup, times(numMetricsRegistered)).removeMetric(anyString()) | ||
KafkaController.MetricNames.foreach(verify(mockMetricsGroup).removeMetric(_)) | ||
|
||
// assert that we have verified all invocations on | ||
verifyNoMoreInteractions(mockMetricsGroup) | ||
} finally { | ||
mockMetricsGroupCtor.close() | ||
} | ||
} | ||
|
||
} |