You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by cm...@apache.org on 2024/08/28 21:19:29 UTC

(kafka) branch 3.9 updated (e5f47ba3501 -> 7356328f530)

This is an automated email from the ASF dual-hosted git repository.

cmccabe pushed a change to branch 3.9
in repository https://gitbox.apache.org/repos/asf/kafka.git


    from e5f47ba3501 KAFKA-15406: Add the ForwardingManager metrics from KIP-938 (#16904)
     new a4728f566f9 KAFKA-15746: KRaft support in ControllerMutationQuotaTest (#16620)
     new 7356328f530 KAFKA-12670: Support configuring unclean leader election in KRaft (#16866)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../main/scala/kafka/server/ControllerServer.scala |   2 +-
 core/src/main/scala/kafka/server/KafkaConfig.scala |   1 +
 .../admin/AdminFenceProducersIntegrationTest.scala |   2 +-
 .../kafka/admin/RemoteTopicCrudTest.scala          |   2 +-
 .../api/AbstractAuthorizerIntegrationTest.scala    |   4 +-
 .../AdminClientWithPoliciesIntegrationTest.scala   |   2 +-
 .../kafka/api/BaseAdminIntegrationTest.scala       |   2 +-
 .../kafka/api/GroupAuthorizerIntegrationTest.scala |   4 +-
 .../kafka/api/IntegrationTestHarness.scala         |   2 +-
 .../integration/kafka/api/TransactionsTest.scala   |   2 +-
 .../kafka/server/QuorumTestHarness.scala           |   4 +-
 .../integration/UncleanLeaderElectionTest.scala    |  98 +++++++++++-----
 ...rScramCredentialsRequestNotAuthorizedTest.scala |   5 +-
 .../AlterUserScramCredentialsRequestTest.scala     |   4 +-
 .../kafka/server/ControllerMutationQuotaTest.scala |  89 +++++++++-----
 .../server/CreateTopicsRequestWithPolicyTest.scala |   3 +-
 ...leteTopicsRequestWithDeletionDisabledTest.scala |   5 +-
 .../ListOffsetsRequestWithRemoteStoreTest.scala    |   3 +-
 .../scala/unit/kafka/server/RequestQuotaTest.scala |   2 +-
 .../controller/ConfigurationControlManager.java    |  50 +++++---
 .../apache/kafka/controller/QuorumController.java  |  92 +++++++++++++--
 .../controller/ReplicationControlManager.java      |  87 +++++++++-----
 .../metrics/ControllerMetadataMetrics.java         |  15 ++-
 .../metrics/ControllerMetricsChanges.java          |  10 ++
 .../apache/kafka/metadata/KafkaConfigSchema.java   |  28 ++++-
 .../ConfigurationControlManagerTest.java           |   4 +-
 .../kafka/controller/QuorumControllerTest.java     |  40 -------
 .../kafka/controller/QuorumControllerTestEnv.java  |   2 +
 .../controller/ReplicationControlManagerTest.java  | 130 ++++++++++++++++++---
 .../metrics/ControllerMetadataMetricsTest.java     |  23 +++-
 .../kafka/metadata/FakeKafkaConfigSchema.java      |  60 ++++++++++
 .../kafka/server/config/ReplicationConfigs.java    |   7 ++
 .../tiered/storage/TieredStorageTestHarness.java   |   2 +-
 33 files changed, 589 insertions(+), 197 deletions(-)
 create mode 100644 metadata/src/test/java/org/apache/kafka/metadata/FakeKafkaConfigSchema.java


(kafka) 02/02: KAFKA-12670: Support configuring unclean leader election in KRaft (#16866)

Posted by cm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cmccabe pushed a commit to branch 3.9
in repository https://gitbox.apache.org/repos/asf/kafka.git

commit 7356328f53088e1eaac93c6722033dd6efb9a2c3
Author: Colin Patrick McCabe <cm...@apache.org>
AuthorDate: Wed Aug 28 14:13:20 2024 -0700

    KAFKA-12670: Support configuring unclean leader election in KRaft (#16866)
    
    Previously in KRaft mode, we could request an unclean leader election for a specific topic using
    the electLeaders API. This PR adds an additional way to trigger unclean leader election when in
    KRaft mode via the static controller configuration and various dynamic configurations.
    
    In order to support all possible configuration methods, we have to do a multi-step configuration
    lookup process:
    
    1. check the dynamic topic configuration for the topic.
    2. check the dynamic node configuration.
    3. check the dynamic cluster configuration.
    4. check the controller's static configuration.
    
    Fortunately, we already have the logic to do this multi-step lookup in KafkaConfigSchema.java.
    This PR reuses that logic. It also makes setting a configuration schema in
    ConfigurationControlManager mandatory. Previously, it was optional for unit tests.
    
    Of course, the dynamic configuration can change over time, or the active controller can change
    to a different one with a different configuration. These changes can make unclean leader
    elections possible for partitions that they were not previously possible for. In order to address
    this, I added a periodic background task which scans leaderless partitions to check if they are
    eligible for an unclean leader election.
    
    Finally, this PR adds the UncleanLeaderElectionsPerSec metric.
    
    Co-authored-by: Luke Chen showuon@gmail.com
    
    Reviewers: Igor Soarez <so...@apple.com>, Luke Chen <sh...@gmail.com>
---
 .../main/scala/kafka/server/ControllerServer.scala |   2 +-
 core/src/main/scala/kafka/server/KafkaConfig.scala |   1 +
 .../admin/AdminFenceProducersIntegrationTest.scala |   2 +-
 .../kafka/admin/RemoteTopicCrudTest.scala          |   2 +-
 .../api/AbstractAuthorizerIntegrationTest.scala    |   4 +-
 .../AdminClientWithPoliciesIntegrationTest.scala   |   2 +-
 .../kafka/api/BaseAdminIntegrationTest.scala       |   2 +-
 .../kafka/api/GroupAuthorizerIntegrationTest.scala |   4 +-
 .../kafka/api/IntegrationTestHarness.scala         |   2 +-
 .../integration/kafka/api/TransactionsTest.scala   |   2 +-
 .../kafka/server/QuorumTestHarness.scala           |   4 +-
 .../integration/UncleanLeaderElectionTest.scala    |  98 +++++++++++-----
 ...rScramCredentialsRequestNotAuthorizedTest.scala |   5 +-
 .../AlterUserScramCredentialsRequestTest.scala     |   4 +-
 .../kafka/server/ControllerMutationQuotaTest.scala |   4 +-
 .../server/CreateTopicsRequestWithPolicyTest.scala |   3 +-
 ...leteTopicsRequestWithDeletionDisabledTest.scala |   5 +-
 .../ListOffsetsRequestWithRemoteStoreTest.scala    |   3 +-
 .../scala/unit/kafka/server/RequestQuotaTest.scala |   2 +-
 .../controller/ConfigurationControlManager.java    |  50 +++++---
 .../apache/kafka/controller/QuorumController.java  |  92 +++++++++++++--
 .../controller/ReplicationControlManager.java      |  87 +++++++++-----
 .../metrics/ControllerMetadataMetrics.java         |  15 ++-
 .../metrics/ControllerMetricsChanges.java          |  10 ++
 .../apache/kafka/metadata/KafkaConfigSchema.java   |  28 ++++-
 .../ConfigurationControlManagerTest.java           |   4 +-
 .../kafka/controller/QuorumControllerTest.java     |  40 -------
 .../kafka/controller/QuorumControllerTestEnv.java  |   2 +
 .../controller/ReplicationControlManagerTest.java  | 130 ++++++++++++++++++---
 .../metrics/ControllerMetadataMetricsTest.java     |  23 +++-
 .../kafka/metadata/FakeKafkaConfigSchema.java      |  60 ++++++++++
 .../kafka/server/config/ReplicationConfigs.java    |   7 ++
 .../tiered/storage/TieredStorageTestHarness.java   |   2 +-
 33 files changed, 530 insertions(+), 171 deletions(-)

diff --git a/core/src/main/scala/kafka/server/ControllerServer.scala b/core/src/main/scala/kafka/server/ControllerServer.scala
index 1008fe2af72..0e4321cffb3 100644
--- a/core/src/main/scala/kafka/server/ControllerServer.scala
+++ b/core/src/main/scala/kafka/server/ControllerServer.scala
@@ -247,7 +247,6 @@ class ControllerServer(
           setQuorumFeatures(quorumFeatures).
           setDefaultReplicationFactor(config.defaultReplicationFactor.toShort).
           setDefaultNumPartitions(config.numPartitions.intValue()).
-          setDefaultMinIsr(config.minInSyncReplicas.intValue()).
           setSessionTimeoutNs(TimeUnit.NANOSECONDS.convert(config.brokerSessionTimeoutMs.longValue(),
             TimeUnit.MILLISECONDS)).
           setLeaderImbalanceCheckIntervalNs(leaderImbalanceCheckIntervalNs).
@@ -266,6 +265,7 @@ class ControllerServer(
           setDelegationTokenMaxLifeMs(config.delegationTokenMaxLifeMs).
           setDelegationTokenExpiryTimeMs(config.delegationTokenExpiryTimeMs).
           setDelegationTokenExpiryCheckIntervalMs(config.delegationTokenExpiryCheckIntervalMs).
+          setUncleanLeaderElectionCheckIntervalMs(config.uncleanLeaderElectionCheckIntervalMs).
           setEligibleLeaderReplicasEnabled(config.elrEnabled)
       }
       controller = controllerBuilder.build()
diff --git a/core/src/main/scala/kafka/server/KafkaConfig.scala b/core/src/main/scala/kafka/server/KafkaConfig.scala
index fb71ad84c00..1edb3100167 100755
--- a/core/src/main/scala/kafka/server/KafkaConfig.scala
+++ b/core/src/main/scala/kafka/server/KafkaConfig.scala
@@ -532,6 +532,7 @@ class KafkaConfig private(doLog: Boolean, val props: util.Map[_, _])
   val autoLeaderRebalanceEnable = getBoolean(ReplicationConfigs.AUTO_LEADER_REBALANCE_ENABLE_CONFIG)
   val leaderImbalancePerBrokerPercentage = getInt(ReplicationConfigs.LEADER_IMBALANCE_PER_BROKER_PERCENTAGE_CONFIG)
   val leaderImbalanceCheckIntervalSeconds: Long = getLong(ReplicationConfigs.LEADER_IMBALANCE_CHECK_INTERVAL_SECONDS_CONFIG)
+  val uncleanLeaderElectionCheckIntervalMs: Long = getLong(ReplicationConfigs.UNCLEAN_LEADER_ELECTION_INTERVAL_MS_CONFIG)
   def uncleanLeaderElectionEnable: java.lang.Boolean = getBoolean(ReplicationConfigs.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG)
 
   // We keep the user-provided String as `MetadataVersion.fromVersionString` can choose a slightly different version (eg if `0.10.0`
diff --git a/core/src/test/scala/integration/kafka/admin/AdminFenceProducersIntegrationTest.scala b/core/src/test/scala/integration/kafka/admin/AdminFenceProducersIntegrationTest.scala
index cbfeee50981..8a611c592fa 100644
--- a/core/src/test/scala/integration/kafka/admin/AdminFenceProducersIntegrationTest.scala
+++ b/core/src/test/scala/integration/kafka/admin/AdminFenceProducersIntegrationTest.scala
@@ -71,7 +71,7 @@ class AdminFenceProducersIntegrationTest extends IntegrationTestHarness {
     props.foreach(p => p.putAll(overridingProps()))
   }
 
-  override protected def kraftControllerConfigs(): Seq[Properties] = {
+  override protected def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     Seq(overridingProps())
   }
 
diff --git a/core/src/test/scala/integration/kafka/admin/RemoteTopicCrudTest.scala b/core/src/test/scala/integration/kafka/admin/RemoteTopicCrudTest.scala
index 2983ba0e307..1880024ac89 100644
--- a/core/src/test/scala/integration/kafka/admin/RemoteTopicCrudTest.scala
+++ b/core/src/test/scala/integration/kafka/admin/RemoteTopicCrudTest.scala
@@ -56,7 +56,7 @@ class RemoteTopicCrudTest extends IntegrationTestHarness {
     props.foreach(p => p.putAll(overrideProps()))
   }
 
-  override protected def kraftControllerConfigs(): Seq[Properties] = {
+  override protected def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     Seq(overrideProps())
   }
 
diff --git a/core/src/test/scala/integration/kafka/api/AbstractAuthorizerIntegrationTest.scala b/core/src/test/scala/integration/kafka/api/AbstractAuthorizerIntegrationTest.scala
index e45689b67f0..5e181b9e823 100644
--- a/core/src/test/scala/integration/kafka/api/AbstractAuthorizerIntegrationTest.scala
+++ b/core/src/test/scala/integration/kafka/api/AbstractAuthorizerIntegrationTest.scala
@@ -98,8 +98,8 @@ class AbstractAuthorizerIntegrationTest extends BaseRequestTest {
     addNodeProperties(properties)
   }
 
-  override def kraftControllerConfigs(): collection.Seq[Properties] = {
-    val controllerConfigs = super.kraftControllerConfigs()
+  override def kraftControllerConfigs(testInfo: TestInfo): collection.Seq[Properties] = {
+    val controllerConfigs = super.kraftControllerConfigs(testInfo)
     controllerConfigs.foreach(addNodeProperties)
     controllerConfigs
   }
diff --git a/core/src/test/scala/integration/kafka/api/AdminClientWithPoliciesIntegrationTest.scala b/core/src/test/scala/integration/kafka/api/AdminClientWithPoliciesIntegrationTest.scala
index 3e99a0316d2..3b2c8d9739c 100644
--- a/core/src/test/scala/integration/kafka/api/AdminClientWithPoliciesIntegrationTest.scala
+++ b/core/src/test/scala/integration/kafka/api/AdminClientWithPoliciesIntegrationTest.scala
@@ -70,7 +70,7 @@ class AdminClientWithPoliciesIntegrationTest extends KafkaServerTestHarness with
     configs.map(KafkaConfig.fromProps)
   }
 
-  override def kraftControllerConfigs(): Seq[Properties] = {
+  override def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     val props = new Properties()
     overrideNodeConfigs(props)
     Seq(props)
diff --git a/core/src/test/scala/integration/kafka/api/BaseAdminIntegrationTest.scala b/core/src/test/scala/integration/kafka/api/BaseAdminIntegrationTest.scala
index 436673806a5..ab47edc8cd5 100644
--- a/core/src/test/scala/integration/kafka/api/BaseAdminIntegrationTest.scala
+++ b/core/src/test/scala/integration/kafka/api/BaseAdminIntegrationTest.scala
@@ -218,7 +218,7 @@ abstract class BaseAdminIntegrationTest extends IntegrationTestHarness with Logg
     }
   }
 
-  override def kraftControllerConfigs(): Seq[Properties] = {
+  override def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     val controllerConfig = new Properties()
     val controllerConfigs = Seq(controllerConfig)
     modifyConfigs(controllerConfigs)
diff --git a/core/src/test/scala/integration/kafka/api/GroupAuthorizerIntegrationTest.scala b/core/src/test/scala/integration/kafka/api/GroupAuthorizerIntegrationTest.scala
index 02c8399fa08..e053966c079 100644
--- a/core/src/test/scala/integration/kafka/api/GroupAuthorizerIntegrationTest.scala
+++ b/core/src/test/scala/integration/kafka/api/GroupAuthorizerIntegrationTest.scala
@@ -70,8 +70,8 @@ class GroupAuthorizerIntegrationTest extends BaseRequestTest {
   def brokerPrincipal: KafkaPrincipal = BrokerPrincipal
   def clientPrincipal: KafkaPrincipal = ClientPrincipal
 
-  override def kraftControllerConfigs(): collection.Seq[Properties] = {
-    val controllerConfigs = super.kraftControllerConfigs()
+  override def kraftControllerConfigs(testInfo: TestInfo): collection.Seq[Properties] = {
+    val controllerConfigs = super.kraftControllerConfigs(testInfo)
     controllerConfigs.foreach(addNodeProperties)
     controllerConfigs
   }
diff --git a/core/src/test/scala/integration/kafka/api/IntegrationTestHarness.scala b/core/src/test/scala/integration/kafka/api/IntegrationTestHarness.scala
index bbdc01e72aa..1c41cbc4fdb 100644
--- a/core/src/test/scala/integration/kafka/api/IntegrationTestHarness.scala
+++ b/core/src/test/scala/integration/kafka/api/IntegrationTestHarness.scala
@@ -82,7 +82,7 @@ abstract class IntegrationTestHarness extends KafkaServerTestHarness {
     cfgs.map(KafkaConfig.fromProps)
   }
 
-  override protected def kraftControllerConfigs(): Seq[Properties] = {
+  override protected def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     Seq(controllerConfig)
   }
 
diff --git a/core/src/test/scala/integration/kafka/api/TransactionsTest.scala b/core/src/test/scala/integration/kafka/api/TransactionsTest.scala
index 09462d361ff..9c8b93012a4 100644
--- a/core/src/test/scala/integration/kafka/api/TransactionsTest.scala
+++ b/core/src/test/scala/integration/kafka/api/TransactionsTest.scala
@@ -78,7 +78,7 @@ class TransactionsTest extends IntegrationTestHarness {
     props.foreach(p => p.putAll(overridingProps()))
   }
 
-  override protected def kraftControllerConfigs(): Seq[Properties] = {
+  override protected def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     Seq(overridingProps())
 
   }
diff --git a/core/src/test/scala/integration/kafka/server/QuorumTestHarness.scala b/core/src/test/scala/integration/kafka/server/QuorumTestHarness.scala
index 188b684c51f..e1f7415ca31 100755
--- a/core/src/test/scala/integration/kafka/server/QuorumTestHarness.scala
+++ b/core/src/test/scala/integration/kafka/server/QuorumTestHarness.scala
@@ -174,7 +174,7 @@ abstract class QuorumTestHarness extends Logging {
    */
   protected val controllerListenerSecurityProtocol: SecurityProtocol = SecurityProtocol.PLAINTEXT
 
-  protected def kraftControllerConfigs(): Seq[Properties] = {
+  protected def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     Seq(new Properties())
   }
 
@@ -315,7 +315,7 @@ abstract class QuorumTestHarness extends Logging {
   }
 
   protected def newKRaftQuorum(overridingProps: Properties): KRaftQuorumImplementation = {
-    val propsList = kraftControllerConfigs()
+    val propsList = kraftControllerConfigs(testInfo)
     if (propsList.size != 1) {
       throw new RuntimeException("Only one KRaft controller is supported for now.")
     }
diff --git a/core/src/test/scala/unit/kafka/integration/UncleanLeaderElectionTest.scala b/core/src/test/scala/unit/kafka/integration/UncleanLeaderElectionTest.scala
index 828f6eb111c..2a23023c898 100755
--- a/core/src/test/scala/unit/kafka/integration/UncleanLeaderElectionTest.scala
+++ b/core/src/test/scala/unit/kafka/integration/UncleanLeaderElectionTest.scala
@@ -22,7 +22,7 @@ import java.util.concurrent.ExecutionException
 import scala.util.Random
 import scala.jdk.CollectionConverters._
 import scala.collection.{Map, Seq}
-import kafka.server.{KafkaBroker, KafkaConfig, QuorumTestHarness}
+import kafka.server.{KafkaBroker, KafkaConfig, MetadataCache, QuorumTestHarness}
 import kafka.utils.{CoreUtils, TestUtils}
 import kafka.utils.TestUtils._
 import org.apache.kafka.common.TopicPartition
@@ -40,6 +40,7 @@ import org.junit.jupiter.api.Assertions._
 import org.junit.jupiter.params.ParameterizedTest
 import org.junit.jupiter.params.provider.ValueSource
 import com.yammer.metrics.core.Meter
+import org.apache.kafka.metadata.LeaderConstants
 
 class UncleanLeaderElectionTest extends QuorumTestHarness {
   val brokerId1 = 0
@@ -97,6 +98,15 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
     super.tearDown()
   }
 
+  override def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
+    val properties = new Properties()
+    if (testInfo.getTestMethod.get().getName.contains("testUncleanLeaderElectionEnabled")) {
+      properties.setProperty("unclean.leader.election.enable", "true")
+    }
+    properties.setProperty("unclean.leader.election.interval.ms", "10")
+    Seq(properties)
+  }
+
   private def startBrokers(cluster: Seq[Properties]): Unit = {
     for (props <- cluster) {
       val config = KafkaConfig.fromProps(props)
@@ -110,7 +120,7 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
   }
 
   @ParameterizedTest
-  @ValueSource(strings = Array("zk"))
+  @ValueSource(strings = Array("zk", "kraft"))
   def testUncleanLeaderElectionEnabled(quorum: String): Unit = {
     // enable unclean leader election
     configProps1.put("unclean.leader.election.enable", "true")
@@ -123,8 +133,8 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
   }
 
   @ParameterizedTest
-  @ValueSource(strings = Array("zk"))
-  def testUncleanLeaderElectionDisabled(): Unit = {
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testUncleanLeaderElectionDisabled(quorum: String): Unit = {
     // unclean leader election is disabled by default
     startBrokers(Seq(configProps1, configProps2))
 
@@ -135,8 +145,8 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
   }
 
   @ParameterizedTest
-  @ValueSource(strings = Array("zk"))
-  def testUncleanLeaderElectionEnabledByTopicOverride(): Unit = {
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testUncleanLeaderElectionEnabledByTopicOverride(quorum: String): Unit = {
     // disable unclean leader election globally, but enable for our specific test topic
     configProps1.put("unclean.leader.election.enable", "false")
     configProps2.put("unclean.leader.election.enable", "false")
@@ -151,8 +161,8 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
   }
 
   @ParameterizedTest
-  @ValueSource(strings = Array("zk"))
-  def testUncleanLeaderElectionDisabledByTopicOverride(): Unit = {
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testUncleanLeaderElectionDisabledByTopicOverride(quorum: String): Unit = {
     // enable unclean leader election globally, but disable for our specific test topic
     configProps1.put("unclean.leader.election.enable", "true")
     configProps2.put("unclean.leader.election.enable", "true")
@@ -167,8 +177,8 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
   }
 
   @ParameterizedTest
-  @ValueSource(strings = Array("zk"))
-  def testUncleanLeaderElectionInvalidTopicOverride(): Unit = {
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testUncleanLeaderElectionInvalidTopicOverride(quorum: String): Unit = {
     startBrokers(Seq(configProps1))
 
     // create topic with an invalid value for unclean leader election
@@ -205,7 +215,7 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
     //verify that unclean election metric count is 0
     val uncleanLeaderElectionsPerSecGauge = getGauge("UncleanLeaderElectionsPerSec")
     @volatile var uncleanLeaderElectionsPerSec = uncleanLeaderElectionsPerSecGauge.count()
-    assert(uncleanLeaderElectionsPerSec == 0)
+    assertEquals(0, uncleanLeaderElectionsPerSec)
 
     // shutdown leader and then restart follower
     brokers.filter(_.config.brokerId == leaderId).map(shutdownBroker)
@@ -215,7 +225,7 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
     // wait until new leader is (uncleanly) elected
     awaitLeaderChange(brokers, topicPartition, expectedLeaderOpt = Some(followerId), timeout = 30000)
     uncleanLeaderElectionsPerSec = uncleanLeaderElectionsPerSecGauge.count()
-    assert(uncleanLeaderElectionsPerSec == 1)
+    assertEquals(1, uncleanLeaderElectionsPerSec)
 
     produceMessage(brokers, topic, "third")
 
@@ -247,17 +257,18 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
     //remove any previous unclean election metric
     val uncleanLeaderElectionsPerSecGauge = getGauge("UncleanLeaderElectionsPerSec")
     @volatile var uncleanLeaderElectionsPerSec = uncleanLeaderElectionsPerSecGauge.count()
-    assert(uncleanLeaderElectionsPerSec == 0)
+    assertEquals(0, uncleanLeaderElectionsPerSec)
 
     // shutdown leader and then restart follower
     brokers.filter(_.config.brokerId == leaderId).map(shutdownBroker)
     val followerServer = brokers.find(_.config.brokerId == followerId).get
     followerServer.startup()
 
-    // verify that unclean election to non-ISR follower does not occur
-    awaitLeaderChange(brokers, topicPartition, expectedLeaderOpt = Some(leaderId))
+    // verify that unclean election to non-ISR follower does not occur.
+    // That is, leader should be NO_LEADER(-1) and the ISR should has only old leaderId.
+    waitForNoLeaderAndIsrHasOldLeaderId(followerServer.replicaManager.metadataCache, leaderId)
     uncleanLeaderElectionsPerSec = uncleanLeaderElectionsPerSecGauge.count()
-    assert(uncleanLeaderElectionsPerSec == 0)
+    assertEquals(0, uncleanLeaderElectionsPerSec)
 
     // message production and consumption should both fail while leader is down
     val e = assertThrows(classOf[ExecutionException], () => produceMessage(brokers, topic, "third", deliveryTimeoutMs = 1000, requestTimeoutMs = 1000))
@@ -312,8 +323,8 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
   }
 
   @ParameterizedTest
-  @ValueSource(strings = Array("zk"))
-  def testTopicUncleanLeaderElectionEnableWithAlterTopicConfigs(): Unit = {
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testTopicUncleanLeaderElectionEnableWithAlterTopicConfigs(quorum: String): Unit = {
     // unclean leader election is disabled by default
     startBrokers(Seq(configProps1, configProps2))
 
@@ -330,26 +341,45 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
     waitForPartitionMetadata(brokers, topic, partitionId)
     assertEquals(List("first"), consumeAllMessages(topic, 1))
 
+    // Verify the "unclean.leader.election.enable" won't be triggered even if it is enabled/disabled dynamically,
+    // because the leader is still alive
+    val adminClient = createAdminClient()
+    try {
+      val newProps = new Properties
+      newProps.put(ReplicationConfigs.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "true")
+      alterTopicConfigs(adminClient, topic, newProps).all.get
+      // leader should not change to followerId
+      awaitLeaderChange(brokers, topicPartition, expectedLeaderOpt = Some(leaderId), timeout = 10000)
+
+      newProps.put(ReplicationConfigs.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "false")
+      alterTopicConfigs(adminClient, topic, newProps).all.get
+      // leader should not change to followerId
+      awaitLeaderChange(brokers, topicPartition, expectedLeaderOpt = Some(leaderId), timeout = 10000)
+    } finally {
+      adminClient.close()
+    }
+
     // shutdown follower server
     brokers.filter(broker => broker.config.brokerId == followerId).map(broker => shutdownBroker(broker))
 
     produceMessage(brokers, topic, "second")
     assertEquals(List("first", "second"), consumeAllMessages(topic, 2))
 
-    //verify that unclean election metric count is 0
+    // verify that unclean election metric count is 0
     val uncleanLeaderElectionsPerSecGauge = getGauge("UncleanLeaderElectionsPerSec")
     @volatile var uncleanLeaderElectionsPerSec = uncleanLeaderElectionsPerSecGauge.count()
-    assert(uncleanLeaderElectionsPerSec == 0)
+    assertEquals(0, uncleanLeaderElectionsPerSec)
 
     // shutdown leader and then restart follower
     brokers.filter(_.config.brokerId == leaderId).map(shutdownBroker)
     val followerBroker = brokers.find(_.config.brokerId == followerId).get
     followerBroker.startup()
 
-    // leader should not change
-    awaitLeaderChange(brokers, topicPartition, expectedLeaderOpt = Some(leaderId), timeout = 30000)
+    // verify that unclean election to non-ISR follower does not occur.
+    // That is, leader should be NO_LEADER(-1) and the ISR should has only old leaderId.
+    waitForNoLeaderAndIsrHasOldLeaderId(followerBroker.replicaManager.metadataCache, leaderId)
     uncleanLeaderElectionsPerSec = uncleanLeaderElectionsPerSecGauge.count()
-    assert(uncleanLeaderElectionsPerSec == 0)
+    assertEquals(0, uncleanLeaderElectionsPerSec)
 
     // message production and consumption should both fail while leader is down
     val e = assertThrows(classOf[ExecutionException], () => produceMessage(brokers, topic, "third", deliveryTimeoutMs = 1000, requestTimeoutMs = 1000))
@@ -358,16 +388,19 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
     assertEquals(List.empty[String], consumeAllMessages(topic, 0))
 
     // Enable unclean leader election for topic
-    val adminClient = createAdminClient()
-    val newProps = new Properties
-    newProps.put(ReplicationConfigs.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "true")
-    alterTopicConfigs(adminClient, topic, newProps).all.get
-    adminClient.close()
+    val adminClient2 = createAdminClient()
+    try {
+      val newProps = new Properties
+      newProps.put(ReplicationConfigs.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "true")
+      alterTopicConfigs(adminClient2, topic, newProps).all.get
+    } finally {
+      adminClient2.close()
+    }
 
     // wait until new leader is (uncleanly) elected
     awaitLeaderChange(brokers, topicPartition, expectedLeaderOpt = Some(followerId), timeout = 30000)
     uncleanLeaderElectionsPerSec = uncleanLeaderElectionsPerSecGauge.count()
-    assert(uncleanLeaderElectionsPerSec == 1)
+    assertEquals(1, uncleanLeaderElectionsPerSec)
 
     produceMessage(brokers, topic, "third")
 
@@ -389,4 +422,11 @@ class UncleanLeaderElectionTest extends QuorumTestHarness {
     config.put(AdminClientConfig.METADATA_MAX_AGE_CONFIG, "10")
     Admin.create(config)
   }
+
+  private def waitForNoLeaderAndIsrHasOldLeaderId(metadataCache: MetadataCache, leaderId: Int): Unit = {
+    waitUntilTrue(() => metadataCache.getPartitionInfo(topic, partitionId).isDefined &&
+      metadataCache.getPartitionInfo(topic, partitionId).get.leader() == LeaderConstants.NO_LEADER &&
+      java.util.Arrays.asList(leaderId).equals(metadataCache.getPartitionInfo(topic, partitionId).get.isr()),
+      "Timed out waiting for broker metadata cache updates the info for topic partition:" + topicPartition)
+  }
 }
diff --git a/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestNotAuthorizedTest.scala b/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestNotAuthorizedTest.scala
index 9469961d2dd..220770c37e2 100644
--- a/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestNotAuthorizedTest.scala
+++ b/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestNotAuthorizedTest.scala
@@ -25,6 +25,7 @@ import org.apache.kafka.common.protocol.Errors
 import org.apache.kafka.common.requests.{AlterUserScramCredentialsRequest, AlterUserScramCredentialsResponse}
 import org.apache.kafka.server.config.ServerConfigs
 import org.junit.jupiter.api.Assertions._
+import org.junit.jupiter.api.TestInfo
 import org.junit.jupiter.params.ParameterizedTest
 import org.junit.jupiter.params.provider.ValueSource
 
@@ -47,8 +48,8 @@ class AlterUserScramCredentialsRequestNotAuthorizedTest extends BaseRequestTest
     properties.put(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG, classOf[AlterCredentialsTest.TestPrincipalBuilderReturningUnauthorized].getName)
   }
 
-  override def kraftControllerConfigs(): collection.Seq[Properties] = {
-    val controllerConfigs = super.kraftControllerConfigs()
+  override def kraftControllerConfigs(testInfo: TestInfo): collection.Seq[Properties] = {
+    val controllerConfigs = super.kraftControllerConfigs(testInfo)
     controllerConfigs.head.put(ServerConfigs.AUTHORIZER_CLASS_NAME_CONFIG, classOf[AlterCredentialsTest.TestStandardAuthorizer].getName)
     controllerConfigs.head.put(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG, classOf[AlterCredentialsTest.TestPrincipalBuilderReturningUnauthorized].getName)
     controllerConfigs
diff --git a/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestTest.scala b/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestTest.scala
index c55c83ccfb9..feb914a2528 100644
--- a/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestTest.scala
+++ b/core/src/test/scala/unit/kafka/server/AlterUserScramCredentialsRequestTest.scala
@@ -71,8 +71,8 @@ class AlterUserScramCredentialsRequestTest extends BaseRequestTest {
     super.setUp(testInfo)
   }
 
-  override def kraftControllerConfigs(): collection.Seq[Properties] = {
-    val controllerConfigs = super.kraftControllerConfigs()
+  override def kraftControllerConfigs(testInfo: TestInfo): collection.Seq[Properties] = {
+    val controllerConfigs = super.kraftControllerConfigs(testInfo)
     controllerConfigs.head.setProperty(ServerConfigs.AUTHORIZER_CLASS_NAME_CONFIG, classOf[AlterCredentialsTest.TestStandardAuthorizer].getName)
     controllerConfigs.head.setProperty(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG, classOf[AlterCredentialsTest.TestPrincipalBuilderReturningAuthorized].getName)
     controllerConfigs
diff --git a/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala b/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala
index 3c06b33640e..7886b851b97 100644
--- a/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala
+++ b/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala
@@ -110,8 +110,8 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     properties.put(QuotaConfigs.CONTROLLER_QUOTA_WINDOW_SIZE_SECONDS_CONFIG, ControllerQuotaWindowSizeSeconds.toString)
   }
 
-  override def kraftControllerConfigs(): Seq[Properties] = {
-    val props = super.kraftControllerConfigs()
+  override def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
+    val props = super.kraftControllerConfigs(testInfo)
     props.head.setProperty(QuotaConfigs.NUM_CONTROLLER_QUOTA_SAMPLES_CONFIG, ControllerQuotaSamples.toString)
     props
   }
diff --git a/core/src/test/scala/unit/kafka/server/CreateTopicsRequestWithPolicyTest.scala b/core/src/test/scala/unit/kafka/server/CreateTopicsRequestWithPolicyTest.scala
index 0f03b4069b1..9e82d5d3ec6 100644
--- a/core/src/test/scala/unit/kafka/server/CreateTopicsRequestWithPolicyTest.scala
+++ b/core/src/test/scala/unit/kafka/server/CreateTopicsRequestWithPolicyTest.scala
@@ -26,6 +26,7 @@ import org.apache.kafka.common.protocol.Errors
 import org.apache.kafka.server.config.ServerLogConfigs.CREATE_TOPIC_POLICY_CLASS_NAME_CONFIG
 import org.apache.kafka.server.policy.CreateTopicPolicy
 import org.apache.kafka.server.policy.CreateTopicPolicy.RequestMetadata
+import org.junit.jupiter.api.TestInfo
 import org.junit.jupiter.params.ParameterizedTest
 import org.junit.jupiter.params.provider.ValueSource
 
@@ -39,7 +40,7 @@ class CreateTopicsRequestWithPolicyTest extends AbstractCreateTopicsRequestTest
     properties.put(CREATE_TOPIC_POLICY_CLASS_NAME_CONFIG, classOf[Policy].getName)
   }
 
-  override def kraftControllerConfigs(): Seq[Properties] = {
+  override def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     val properties = new Properties()
     properties.put(CREATE_TOPIC_POLICY_CLASS_NAME_CONFIG, classOf[Policy].getName)
     Seq(properties)
diff --git a/core/src/test/scala/unit/kafka/server/DeleteTopicsRequestWithDeletionDisabledTest.scala b/core/src/test/scala/unit/kafka/server/DeleteTopicsRequestWithDeletionDisabledTest.scala
index 91ea2e8840e..5d0ec2ec60b 100644
--- a/core/src/test/scala/unit/kafka/server/DeleteTopicsRequestWithDeletionDisabledTest.scala
+++ b/core/src/test/scala/unit/kafka/server/DeleteTopicsRequestWithDeletionDisabledTest.scala
@@ -25,6 +25,7 @@ import org.apache.kafka.common.protocol.Errors
 import org.apache.kafka.common.requests.{DeleteTopicsRequest, DeleteTopicsResponse}
 import org.apache.kafka.server.config.ServerConfigs
 import org.junit.jupiter.api.Assertions._
+import org.junit.jupiter.api.TestInfo
 import org.junit.jupiter.params.ParameterizedTest
 import org.junit.jupiter.params.provider.ValueSource
 
@@ -32,8 +33,8 @@ class DeleteTopicsRequestWithDeletionDisabledTest extends BaseRequestTest {
 
   override def brokerCount: Int = 1
 
-  override def kraftControllerConfigs() = {
-    val props = super.kraftControllerConfigs()
+  override def kraftControllerConfigs(testInfo: TestInfo) = {
+    val props = super.kraftControllerConfigs(testInfo)
     props.head.setProperty(ServerConfigs.DELETE_TOPIC_ENABLE_CONFIG, "false")
     props
   }
diff --git a/core/src/test/scala/unit/kafka/server/ListOffsetsRequestWithRemoteStoreTest.scala b/core/src/test/scala/unit/kafka/server/ListOffsetsRequestWithRemoteStoreTest.scala
index 2f477486c68..970f3f7489b 100644
--- a/core/src/test/scala/unit/kafka/server/ListOffsetsRequestWithRemoteStoreTest.scala
+++ b/core/src/test/scala/unit/kafka/server/ListOffsetsRequestWithRemoteStoreTest.scala
@@ -18,13 +18,14 @@ package kafka.server
 
 import org.apache.kafka.common.config.TopicConfig
 import org.apache.kafka.server.log.remote.storage.{NoOpRemoteLogMetadataManager, NoOpRemoteStorageManager, RemoteLogManagerConfig}
+import org.junit.jupiter.api.TestInfo
 
 import java.util.Properties
 import scala.collection.Seq
 
 class ListOffsetsRequestWithRemoteStoreTest extends ListOffsetsRequestTest {
 
-  override def kraftControllerConfigs(): Seq[Properties] = {
+  override def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     val props = new Properties
     brokerPropertyOverrides(props)
     Seq(props)
diff --git a/core/src/test/scala/unit/kafka/server/RequestQuotaTest.scala b/core/src/test/scala/unit/kafka/server/RequestQuotaTest.scala
index 218c0cf5fbc..c7526e808be 100644
--- a/core/src/test/scala/unit/kafka/server/RequestQuotaTest.scala
+++ b/core/src/test/scala/unit/kafka/server/RequestQuotaTest.scala
@@ -93,7 +93,7 @@ class RequestQuotaTest extends BaseRequestTest {
     }
   }
 
-  override def kraftControllerConfigs(): Seq[Properties] = {
+  override def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
     val properties = new Properties()
     properties.put(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG, classOf[RequestQuotaTest.TestPrincipalBuilder].getName)
     Seq(properties)
diff --git a/metadata/src/main/java/org/apache/kafka/controller/ConfigurationControlManager.java b/metadata/src/main/java/org/apache/kafka/controller/ConfigurationControlManager.java
index b10614cc2e8..29b334c9770 100644
--- a/metadata/src/main/java/org/apache/kafka/controller/ConfigurationControlManager.java
+++ b/metadata/src/main/java/org/apache/kafka/controller/ConfigurationControlManager.java
@@ -50,6 +50,7 @@ import java.util.Optional;
 import java.util.function.Consumer;
 
 import static org.apache.kafka.clients.admin.AlterConfigOp.OpType.APPEND;
+import static org.apache.kafka.common.config.TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG;
 import static org.apache.kafka.common.protocol.Errors.INVALID_CONFIG;
 import static org.apache.kafka.controller.QuorumController.MAX_RECORDS_PER_USER_OP;
 
@@ -70,7 +71,7 @@ public class ConfigurationControlManager {
     static class Builder {
         private LogContext logContext = null;
         private SnapshotRegistry snapshotRegistry = null;
-        private KafkaConfigSchema configSchema = KafkaConfigSchema.EMPTY;
+        private KafkaConfigSchema configSchema = null;
         private Consumer<ConfigResource> existenceChecker = __ -> { };
         private Optional<AlterConfigPolicy> alterConfigPolicy = Optional.empty();
         private ConfigurationValidator validator = ConfigurationValidator.NO_OP;
@@ -120,6 +121,9 @@ public class ConfigurationControlManager {
         ConfigurationControlManager build() {
             if (logContext == null) logContext = new LogContext();
             if (snapshotRegistry == null) snapshotRegistry = new SnapshotRegistry(logContext);
+            if (configSchema == null) {
+                throw new RuntimeException("You must set the configSchema.");
+            }
             return new ConfigurationControlManager(
                 logContext,
                 snapshotRegistry,
@@ -440,22 +444,24 @@ public class ConfigurationControlManager {
     }
 
     /**
-     * Get the config value for the give topic and give config key.
+     * Get the config value for the given topic and given config key.
+     * The check order is:
+     *   1. dynamic topic overridden configs
+     *   2. dynamic node overridden configs
+     *   3. dynamic cluster overridden configs
+     *   4. static configs
      * If the config value is not found, return null.
      *
      * @param topicName            The topic name for the config.
      * @param configKey            The key for the config.
+     * @return the config value for the provided config key in the topic
      */
-    String getTopicConfig(String topicName, String configKey) throws NoSuchElementException {
-        Map<String, String> map = configData.get(new ConfigResource(Type.TOPIC, topicName));
-        if (map == null || !map.containsKey(configKey)) {
-            Map<String, ConfigEntry> effectiveConfigMap = computeEffectiveTopicConfigs(Collections.emptyMap());
-            if (!effectiveConfigMap.containsKey(configKey)) {
-                return null;
-            }
-            return effectiveConfigMap.get(configKey).value();
-        }
-        return map.get(configKey);
+    ConfigEntry getTopicConfig(String topicName, String configKey) throws NoSuchElementException {
+        ConfigEntry result = configSchema.resolveEffectiveTopicConfig(configKey,
+            staticConfig,
+            clusterConfig(),
+            currentControllerConfig(), currentTopicConfig(topicName));
+        return result;
     }
 
     public Map<ConfigResource, ResultOrError<Map<String, String>>> describeConfigs(
@@ -496,8 +502,19 @@ public class ConfigurationControlManager {
         configData.remove(new ConfigResource(Type.TOPIC, name));
     }
 
-    boolean uncleanLeaderElectionEnabledForTopic(String name) {
-        return false; // TODO: support configuring unclean leader election.
+    /**
+     * Check if this topic has "unclean.leader.election.enable" set to true.
+     *
+     * @param topicName            The topic name for the config.
+     * @return true if this topic has uncleanLeaderElection enabled
+     */
+    boolean uncleanLeaderElectionEnabledForTopic(String topicName) {
+        String uncleanLeaderElection = getTopicConfig(topicName, UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG).value();
+        if (!uncleanLeaderElection.isEmpty()) {
+            return Boolean.parseBoolean(uncleanLeaderElection);
+        }
+
+        return false;
     }
 
     Map<String, ConfigEntry> computeEffectiveTopicConfigs(Map<String, String> creationConfigs) {
@@ -514,4 +531,9 @@ public class ConfigurationControlManager {
         Map<String, String> result = configData.get(currentController);
         return (result == null) ? Collections.emptyMap() : result;
     }
+
+    Map<String, String> currentTopicConfig(String topicName) {
+        Map<String, String> result = configData.get(new ConfigResource(Type.TOPIC, topicName));
+        return (result == null) ? Collections.emptyMap() : result;
+    }
 }
diff --git a/metadata/src/main/java/org/apache/kafka/controller/QuorumController.java b/metadata/src/main/java/org/apache/kafka/controller/QuorumController.java
index 3f7bb4df74c..64723b86c0d 100644
--- a/metadata/src/main/java/org/apache/kafka/controller/QuorumController.java
+++ b/metadata/src/main/java/org/apache/kafka/controller/QuorumController.java
@@ -206,7 +206,6 @@ public final class QuorumController implements Controller {
         private QuorumFeatures quorumFeatures = null;
         private short defaultReplicationFactor = 3;
         private int defaultNumPartitions = 1;
-        private int defaultMinIsr = 1;
         private ReplicaPlacer replicaPlacer = new StripedReplicaPlacer(new Random());
         private OptionalLong leaderImbalanceCheckIntervalNs = OptionalLong.empty();
         private OptionalLong maxIdleIntervalNs = OptionalLong.empty();
@@ -225,6 +224,7 @@ public final class QuorumController implements Controller {
         private long delegationTokenMaxLifeMs;
         private long delegationTokenExpiryTimeMs;
         private long delegationTokenExpiryCheckIntervalMs;
+        private long uncleanLeaderElectionCheckIntervalMs = TimeUnit.MINUTES.toMillis(5);
 
         public Builder(int nodeId, String clusterId) {
             this.nodeId = nodeId;
@@ -285,11 +285,6 @@ public final class QuorumController implements Controller {
             return this;
         }
 
-        public Builder setDefaultMinIsr(int defaultMinIsr) {
-            this.defaultMinIsr = defaultMinIsr;
-            return this;
-        }
-
         public Builder setReplicaPlacer(ReplicaPlacer replicaPlacer) {
             this.replicaPlacer = replicaPlacer;
             return this;
@@ -380,6 +375,11 @@ public final class QuorumController implements Controller {
             return this;
         }
 
+        public Builder setUncleanLeaderElectionCheckIntervalMs(long uncleanLeaderElectionCheckIntervalMs) {
+            this.uncleanLeaderElectionCheckIntervalMs = uncleanLeaderElectionCheckIntervalMs;
+            return this;
+        }
+
         public QuorumController build() throws Exception {
             if (raftClient == null) {
                 throw new IllegalStateException("You must set a raft client.");
@@ -419,7 +419,6 @@ public final class QuorumController implements Controller {
                     quorumFeatures,
                     defaultReplicationFactor,
                     defaultNumPartitions,
-                    defaultMinIsr,
                     replicaPlacer,
                     leaderImbalanceCheckIntervalNs,
                     maxIdleIntervalNs,
@@ -437,7 +436,8 @@ public final class QuorumController implements Controller {
                     delegationTokenMaxLifeMs,
                     delegationTokenExpiryTimeMs,
                     delegationTokenExpiryCheckIntervalMs,
-                    eligibleLeaderReplicasEnabled
+                    eligibleLeaderReplicasEnabled,
+                    uncleanLeaderElectionCheckIntervalMs
                 );
             } catch (Exception e) {
                 Utils.closeQuietly(queue, "event queue");
@@ -850,6 +850,9 @@ public final class QuorumController implements Controller {
             // with leader that is not the preferred leader.
             maybeScheduleNextBalancePartitionLeaders();
 
+            // Schedule a new unclean leader election if there are partitions that do not have a leader.
+            maybeScheduleNextElectUncleanLeaders();
+
             // Remember the latest offset and future if it is not already completed
             if (!future.isDone()) {
                 if (featureControl.inPreMigrationMode() && flags.contains(RUNS_IN_PREMIGRATION)) {
@@ -1251,6 +1254,7 @@ public final class QuorumController implements Controller {
             // metadata.version and other in-memory state.
             maybeScheduleNextExpiredDelegationTokenSweep();
             maybeScheduleNextBalancePartitionLeaders();
+            maybeScheduleNextElectUncleanLeaders();
             maybeScheduleNextWriteNoOpRecord();
         }
     }
@@ -1271,6 +1275,7 @@ public final class QuorumController implements Controller {
             clusterControl.deactivate();
             cancelMaybeFenceReplicas();
             cancelMaybeBalancePartitionLeaders();
+            cancelMaybeNextElectUncleanLeaders();
             cancelNextWriteNoOpRecord();
         } catch (Throwable e) {
             fatalFaultHandler.handleFault("exception while renouncing leadership", e);
@@ -1345,7 +1350,13 @@ public final class QuorumController implements Controller {
             );
 
             ControllerWriteEvent<Boolean> event = new ControllerWriteEvent<>(MAYBE_BALANCE_PARTITION_LEADERS, () -> {
+                long startTimeNs = time.nanoseconds();
                 ControllerResult<Boolean> result = replicationControl.maybeBalancePartitionLeaders();
+                long endTimeNs = time.nanoseconds();
+                long durationNs = endTimeNs - startTimeNs;
+                log.info("maybeBalancePartitionLeaders: generated {} records in {} microseconds.{}",
+                    result.records().size(), NANOSECONDS.toMicros(durationNs),
+                        result.response() ? " Rescheduling immediately." : "");
 
                 // reschedule the operation after the leaderImbalanceCheckIntervalNs interval.
                 // Mark the imbalance event as completed and reschedule if necessary
@@ -1383,6 +1394,54 @@ public final class QuorumController implements Controller {
         queue.cancelDeferred(MAYBE_BALANCE_PARTITION_LEADERS);
     }
 
+    private static final String MAYBE_ELECT_UNCLEAN_LEADERS = "maybeElectUncleanLeaders";
+
+    private void maybeScheduleNextElectUncleanLeaders() {
+        if (uncleanScheduled != ImbalanceSchedule.SCHEDULED &&
+                replicationControl.areSomePartitionsLeaderless()) {
+            log.debug(
+                "Scheduling write event for {} because scheduled ({}), and areSomePartitionsLeaderless ({})",
+                MAYBE_ELECT_UNCLEAN_LEADERS,
+                uncleanScheduled,
+                replicationControl.areSomePartitionsLeaderless()
+            );
+
+            ControllerWriteEvent<Boolean> event = new ControllerWriteEvent<>(MAYBE_ELECT_UNCLEAN_LEADERS, () -> {
+                long startTimeNs = time.nanoseconds();
+                ControllerResult<Boolean> result = replicationControl.maybeElectUncleanLeaders();
+                long endTimeNs = time.nanoseconds();
+                long durationNs = endTimeNs - startTimeNs;
+                log.info("maybeElectUncleanLeaders: generated {} records in {} microseconds.{}",
+                        result.records().size(), NANOSECONDS.toMicros(durationNs),
+                        result.response() ? " Rescheduling immediately." : "");
+                if (result.response()) {
+                    uncleanScheduled = ImbalanceSchedule.IMMEDIATELY;
+                } else {
+                    uncleanScheduled = ImbalanceSchedule.DEFERRED;
+                }
+                return result;
+            }, EnumSet.of(DOES_NOT_UPDATE_QUEUE_TIME));
+
+            long delayNs = time.nanoseconds();
+            if (uncleanScheduled == ImbalanceSchedule.DEFERRED) {
+                delayNs += uncleanLeaderElectionCheckIntervalNs;
+            } else {
+                // The current implementation of KafkaEventQueue always picks from the deferred collection of operations
+                // before picking from the non-deferred collection of operations. This can result in some unfairness if
+                // deferred operation are scheduled for immediate execution. This delays them by a small amount of time.
+                delayNs += NANOSECONDS.convert(10, TimeUnit.MILLISECONDS);
+            }
+            queue.scheduleDeferred(MAYBE_ELECT_UNCLEAN_LEADERS, new EarliestDeadlineFunction(delayNs), event);
+            uncleanScheduled = ImbalanceSchedule.SCHEDULED;
+        }
+    }
+
+
+    private void cancelMaybeNextElectUncleanLeaders() {
+        uncleanScheduled = ImbalanceSchedule.DEFERRED;
+        queue.cancelDeferred(MAYBE_ELECT_UNCLEAN_LEADERS);
+    }
+
     private static final String WRITE_NO_OP_RECORD = "writeNoOpRecord";
 
     private void maybeScheduleNextWriteNoOpRecord() {
@@ -1742,6 +1801,11 @@ public final class QuorumController implements Controller {
      */
     private ImbalanceSchedule imbalancedScheduled = ImbalanceSchedule.DEFERRED;
 
+    /**
+     * Tracks the scheduling state for unclean leader election operations.
+     */
+    private ImbalanceSchedule uncleanScheduled = ImbalanceSchedule.DEFERRED;
+
     /**
      * Tracks if the write of the NoOpRecord has been scheduled.
      */
@@ -1758,6 +1822,11 @@ public final class QuorumController implements Controller {
 
     private final boolean eligibleLeaderReplicasEnabled;
 
+    /**
+     * The number of nanoseconds between unclean leader election checks.
+     */
+    private final long uncleanLeaderElectionCheckIntervalNs;
+
     /**
      * The maximum number of records per batch to allow.
      */
@@ -1781,7 +1850,6 @@ public final class QuorumController implements Controller {
         QuorumFeatures quorumFeatures,
         short defaultReplicationFactor,
         int defaultNumPartitions,
-        int defaultMinIsr,
         ReplicaPlacer replicaPlacer,
         OptionalLong leaderImbalanceCheckIntervalNs,
         OptionalLong maxIdleIntervalNs,
@@ -1799,7 +1867,8 @@ public final class QuorumController implements Controller {
         long delegationTokenMaxLifeMs,
         long delegationTokenExpiryTimeMs,
         long delegationTokenExpiryCheckIntervalMs,
-        boolean eligibleLeaderReplicasEnabled
+        boolean eligibleLeaderReplicasEnabled,
+        long uncleanLeaderElectionCheckIntervalMs
     ) {
         this.nonFatalFaultHandler = nonFatalFaultHandler;
         this.fatalFaultHandler = fatalFaultHandler;
@@ -1869,7 +1938,6 @@ public final class QuorumController implements Controller {
             setLogContext(logContext).
             setDefaultReplicationFactor(defaultReplicationFactor).
             setDefaultNumPartitions(defaultNumPartitions).
-            setDefaultMinIsr(defaultMinIsr).
             setEligibleLeaderReplicasEnabled(eligibleLeaderReplicasEnabled).
             setMaxElectionsPerImbalance(ReplicationControlManager.MAX_ELECTIONS_PER_IMBALANCE).
             setConfigurationControl(configurationControl).
@@ -1905,6 +1973,8 @@ public final class QuorumController implements Controller {
         this.zkMigrationEnabled = zkMigrationEnabled;
         this.recordRedactor = new RecordRedactor(configSchema);
         this.eligibleLeaderReplicasEnabled = eligibleLeaderReplicasEnabled;
+        this.uncleanLeaderElectionCheckIntervalNs =
+            TimeUnit.MILLISECONDS.toNanos(uncleanLeaderElectionCheckIntervalMs);
 
         log.info("Creating new QuorumController with clusterId {}.{}{}",
             clusterId, zkMigrationEnabled ? " ZK migration mode is enabled." : "",
diff --git a/metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java b/metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java
index 81abfbeb537..e7a4359fb54 100644
--- a/metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java
+++ b/metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java
@@ -159,7 +159,6 @@ public class ReplicationControlManager {
         private short defaultReplicationFactor = (short) 3;
         private int defaultNumPartitions = 1;
 
-        private int defaultMinIsr = 1;
         private int maxElectionsPerImbalance = MAX_ELECTIONS_PER_IMBALANCE;
         private ConfigurationControlManager configurationControl = null;
         private ClusterControlManager clusterControl = null;
@@ -187,11 +186,6 @@ public class ReplicationControlManager {
             return this;
         }
 
-        Builder setDefaultMinIsr(int defaultMinIsr) {
-            this.defaultMinIsr = defaultMinIsr;
-            return this;
-        }
-
         Builder setEligibleLeaderReplicasEnabled(boolean eligibleLeaderReplicasEnabled) {
             this.eligibleLeaderReplicasEnabled = eligibleLeaderReplicasEnabled;
             return this;
@@ -237,7 +231,6 @@ public class ReplicationControlManager {
                 logContext,
                 defaultReplicationFactor,
                 defaultNumPartitions,
-                defaultMinIsr,
                 maxElectionsPerImbalance,
                 eligibleLeaderReplicasEnabled,
                 configurationControl,
@@ -311,11 +304,6 @@ public class ReplicationControlManager {
      */
     private final int defaultNumPartitions;
 
-    /**
-     * The default min ISR that is used if a CreateTopics request does not specify one.
-     */
-    private final int defaultMinIsr;
-
     /**
      * True if eligible leader replicas is enabled.
      */
@@ -409,7 +397,6 @@ public class ReplicationControlManager {
         LogContext logContext,
         short defaultReplicationFactor,
         int defaultNumPartitions,
-        int defaultMinIsr,
         int maxElectionsPerImbalance,
         boolean eligibleLeaderReplicasEnabled,
         ConfigurationControlManager configurationControl,
@@ -421,7 +408,6 @@ public class ReplicationControlManager {
         this.log = logContext.logger(ReplicationControlManager.class);
         this.defaultReplicationFactor = defaultReplicationFactor;
         this.defaultNumPartitions = defaultNumPartitions;
-        this.defaultMinIsr = defaultMinIsr;
         this.maxElectionsPerImbalance = maxElectionsPerImbalance;
         this.eligibleLeaderReplicasEnabled = eligibleLeaderReplicasEnabled;
         this.configurationControl = configurationControl;
@@ -1631,6 +1617,10 @@ public class ReplicationControlManager {
         return !imbalancedPartitions.isEmpty();
     }
 
+    boolean areSomePartitionsLeaderless() {
+        return brokersToIsrs.partitionsWithNoLeader().hasNext();
+    }
+
     /**
      * Attempt to elect a preferred leader for all topic partitions which have a leader that is not the preferred replica.
      *
@@ -1641,12 +1631,17 @@ public class ReplicationControlManager {
      */
     ControllerResult<Boolean> maybeBalancePartitionLeaders() {
         List<ApiMessageAndVersion> records = new ArrayList<>();
+        maybeTriggerLeaderChangeForPartitionsWithoutPreferredLeader(records, maxElectionsPerImbalance);
+        return ControllerResult.of(records, records.size() >= maxElectionsPerImbalance);
+    }
 
-        boolean rescheduleImmediately = false;
+    void maybeTriggerLeaderChangeForPartitionsWithoutPreferredLeader(
+        List<ApiMessageAndVersion> records,
+        int maxElections
+    ) {
         for (TopicIdPartition topicPartition : imbalancedPartitions) {
-            if (records.size() >= maxElectionsPerImbalance) {
-                rescheduleImmediately = true;
-                break;
+            if (records.size() >= maxElections) {
+                return;
             }
 
             TopicControlInfo topic = topics.get(topicPartition.topicId());
@@ -1676,8 +1671,52 @@ public class ReplicationControlManager {
                 .setDefaultDirProvider(clusterDescriber)
                 .build().ifPresent(records::add);
         }
+    }
+
+    /**
+     * Check if we can do an unclean election for partitions with no leader.
+     *
+     * The response() method in the return object is true if this method returned without electing all possible preferred replicas.
+     * The quorum controller should reschedule this operation immediately if it is true.
+     *
+     * @return All of the election records and true if there may be more elections to be done.
+     */
+    ControllerResult<Boolean> maybeElectUncleanLeaders() {
+        List<ApiMessageAndVersion> records = new ArrayList<>();
+        maybeTriggerUncleanLeaderElectionForLeaderlessPartitions(records, maxElectionsPerImbalance);
+        return ControllerResult.of(records, records.size() >= maxElectionsPerImbalance);
+    }
 
-        return ControllerResult.of(records, rescheduleImmediately);
+    /**
+     * Trigger unclean leader election for partitions without leader (visiable for testing)
+     *
+     * @param records       The record list to append to.
+     * @param maxElections  The maximum number of elections to perform.
+     */
+    void maybeTriggerUncleanLeaderElectionForLeaderlessPartitions(
+            List<ApiMessageAndVersion> records,
+            int maxElections
+    ) {
+        Iterator<TopicIdPartition> iterator = brokersToIsrs.partitionsWithNoLeader();
+        while (iterator.hasNext() && records.size() < maxElections) {
+            TopicIdPartition topicIdPartition = iterator.next();
+            TopicControlInfo topic = topics.get(topicIdPartition.topicId());
+            if (configurationControl.uncleanLeaderElectionEnabledForTopic(topic.name)) {
+                ApiError result = electLeader(topic.name, topicIdPartition.partitionId(),
+                        ElectionType.UNCLEAN, records);
+                if (result.error().equals(Errors.NONE)) {
+                    log.error("Triggering unclean leader election for offline partition {}-{}.",
+                            topic.name, topicIdPartition.partitionId());
+                } else {
+                    log.warn("Cannot trigger unclean leader election for offline partition {}-{}: {}",
+                            topic.name, topicIdPartition.partitionId(), result.error());
+                }
+            } else if (log.isDebugEnabled()) {
+                log.debug("Cannot trigger unclean leader election for offline partition {}-{} " +
+                                "because unclean leader election is disabled for this topic.",
+                        topic.name, topicIdPartition.partitionId());
+            }
+        }
     }
 
     ControllerResult<List<CreatePartitionsTopicResult>> createPartitions(
@@ -2222,14 +2261,8 @@ public class ReplicationControlManager {
 
     // Visible to test.
     int getTopicEffectiveMinIsr(String topicName) {
-        int currentMinIsr = defaultMinIsr;
-        String minIsrConfig = configurationControl.getTopicConfig(topicName, MIN_IN_SYNC_REPLICAS_CONFIG);
-        if (minIsrConfig != null) {
-            currentMinIsr = Integer.parseInt(minIsrConfig);
-        } else {
-            log.debug("Can't find the min isr config for topic: " + topicName + ". Use default value " + defaultMinIsr);
-        }
-        
+        String minIsrConfig = configurationControl.getTopicConfig(topicName, MIN_IN_SYNC_REPLICAS_CONFIG).value();
+        int currentMinIsr = Integer.parseInt(minIsrConfig);
         Uuid topicId = topicsByName.get(topicName);
         int replicationFactor = topics.get(topicId).parts.get(0).replicas.length;
         return Math.min(currentMinIsr, replicationFactor);
diff --git a/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetadataMetrics.java b/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetadataMetrics.java
index bbe26345475..884bc709cf6 100644
--- a/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetadataMetrics.java
+++ b/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetadataMetrics.java
@@ -20,11 +20,13 @@ package org.apache.kafka.controller.metrics;
 import org.apache.kafka.server.metrics.KafkaYammerMetrics;
 
 import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.Meter;
 import com.yammer.metrics.core.MetricName;
 import com.yammer.metrics.core.MetricsRegistry;
 
 import java.util.Arrays;
 import java.util.Optional;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -54,6 +56,8 @@ public final class ControllerMetadataMetrics implements AutoCloseable {
         "KafkaController", "MetadataErrorCount");
     private static final MetricName ZK_MIGRATION_STATE = getMetricName(
         "KafkaController", "ZkMigrationState");
+    private static final MetricName UNCLEAN_LEADER_ELECTIONS_PER_SEC = getMetricName(
+        "ControllerStats", "UncleanLeaderElectionsPerSec");
 
     private final Optional<MetricsRegistry> registry;
     private final AtomicInteger fencedBrokerCount = new AtomicInteger(0);
@@ -65,6 +69,8 @@ public final class ControllerMetadataMetrics implements AutoCloseable {
     private final AtomicInteger preferredReplicaImbalanceCount = new AtomicInteger(0);
     private final AtomicInteger metadataErrorCount = new AtomicInteger(0);
     private final AtomicInteger zkMigrationState = new AtomicInteger(-1);
+    private Optional<Meter> uncleanLeaderElectionMeter = Optional.empty();
+
 
     /**
      * Create a new ControllerMetadataMetrics object.
@@ -129,6 +135,8 @@ public final class ControllerMetadataMetrics implements AutoCloseable {
             }
         }));
 
+        registry.ifPresent(r -> uncleanLeaderElectionMeter =
+                Optional.of(registry.get().newMeter(UNCLEAN_LEADER_ELECTIONS_PER_SEC, "elections", TimeUnit.SECONDS)));
     }
 
     public void setFencedBrokerCount(int brokerCount) {
@@ -231,6 +239,10 @@ public final class ControllerMetadataMetrics implements AutoCloseable {
         return zkMigrationState.byteValue();
     }
 
+    public void updateUncleanLeaderElection(int count) {
+        this.uncleanLeaderElectionMeter.ifPresent(m -> m.mark(count));
+    }
+
     @Override
     public void close() {
         registry.ifPresent(r -> Arrays.asList(
@@ -242,7 +254,8 @@ public final class ControllerMetadataMetrics implements AutoCloseable {
             OFFLINE_PARTITION_COUNT,
             PREFERRED_REPLICA_IMBALANCE_COUNT,
             METADATA_ERROR_COUNT,
-            ZK_MIGRATION_STATE
+            ZK_MIGRATION_STATE,
+            UNCLEAN_LEADER_ELECTIONS_PER_SEC
         ).forEach(r::removeMetric));
     }
 
diff --git a/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetricsChanges.java b/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetricsChanges.java
index 12956b3d610..a4b30a9f9f3 100644
--- a/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetricsChanges.java
+++ b/metadata/src/main/java/org/apache/kafka/controller/metrics/ControllerMetricsChanges.java
@@ -48,6 +48,7 @@ class ControllerMetricsChanges {
     private int globalPartitionsChange = 0;
     private int offlinePartitionsChange = 0;
     private int partitionsWithoutPreferredLeaderChange = 0;
+    private int uncleanLeaderElection = 0;
 
     public int fencedBrokersChange() {
         return fencedBrokersChange;
@@ -135,6 +136,11 @@ class ControllerMetricsChanges {
             isPresent = true;
             isOffline = !next.hasLeader();
             isWithoutPreferredLeader = !next.hasPreferredLeader();
+            // take current all replicas as ISR if prev is null (new created partition), so we won't treat it as unclean election.
+            int[] prevIsr = prev != null ? prev.isr : next.replicas;
+            if (!PartitionRegistration.electionWasClean(next.leader, prevIsr)) {
+                uncleanLeaderElection++;
+            }
         }
         globalPartitionsChange += delta(wasPresent, isPresent);
         offlinePartitionsChange += delta(wasOffline, isOffline);
@@ -166,5 +172,9 @@ class ControllerMetricsChanges {
         if (partitionsWithoutPreferredLeaderChange != 0) {
             metrics.addToPreferredReplicaImbalanceCount(partitionsWithoutPreferredLeaderChange);
         }
+        if (uncleanLeaderElection > 0) {
+            metrics.updateUncleanLeaderElection(uncleanLeaderElection);
+            uncleanLeaderElection = 0;
+        }
     }
 }
diff --git a/metadata/src/main/java/org/apache/kafka/metadata/KafkaConfigSchema.java b/metadata/src/main/java/org/apache/kafka/metadata/KafkaConfigSchema.java
index 642c697b8ba..d2d2521c128 100644
--- a/metadata/src/main/java/org/apache/kafka/metadata/KafkaConfigSchema.java
+++ b/metadata/src/main/java/org/apache/kafka/metadata/KafkaConfigSchema.java
@@ -173,11 +173,29 @@ public class KafkaConfigSchema {
         return effectiveConfigs;
     }
 
-    private ConfigEntry resolveEffectiveTopicConfig(ConfigDef.ConfigKey configKey,
-            Map<String, ?> staticNodeConfig,
-            Map<String, ?> dynamicClusterConfigs,
-            Map<String, ?> dynamicNodeConfigs,
-            Map<String, ?> dynamicTopicConfigs) {
+    public ConfigEntry resolveEffectiveTopicConfig(
+        String keyName,
+        Map<String, ?> staticNodeConfig,
+        Map<String, ?> dynamicClusterConfigs,
+        Map<String, ?> dynamicNodeConfigs,
+        Map<String, ?> dynamicTopicConfigs
+    ) {
+        ConfigDef configDef = configDefs.getOrDefault(ConfigResource.Type.TOPIC, EMPTY_CONFIG_DEF);
+        ConfigDef.ConfigKey configKey = configDef.configKeys().get(keyName);
+        return resolveEffectiveTopicConfig(configKey,
+            staticNodeConfig,
+            dynamicClusterConfigs,
+            dynamicNodeConfigs,
+            dynamicTopicConfigs);
+    }
+
+    public ConfigEntry resolveEffectiveTopicConfig(
+        ConfigDef.ConfigKey configKey,
+        Map<String, ?> staticNodeConfig,
+        Map<String, ?> dynamicClusterConfigs,
+        Map<String, ?> dynamicNodeConfigs,
+        Map<String, ?> dynamicTopicConfigs
+    ) {
         if (dynamicTopicConfigs.containsKey(configKey.name)) {
             return toConfigEntry(configKey,
                 dynamicTopicConfigs.get(configKey.name),
diff --git a/metadata/src/test/java/org/apache/kafka/controller/ConfigurationControlManagerTest.java b/metadata/src/test/java/org/apache/kafka/controller/ConfigurationControlManagerTest.java
index 0efdd13b112..2ce417f63aa 100644
--- a/metadata/src/test/java/org/apache/kafka/controller/ConfigurationControlManagerTest.java
+++ b/metadata/src/test/java/org/apache/kafka/controller/ConfigurationControlManagerTest.java
@@ -56,7 +56,6 @@ import static org.apache.kafka.common.config.ConfigResource.Type.TOPIC;
 import static org.apache.kafka.common.metadata.MetadataRecordType.CONFIG_RECORD;
 import static org.apache.kafka.server.config.ConfigSynonym.HOURS_TO_MILLISECONDS;
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
 
 
 @Timeout(value = 40)
@@ -136,8 +135,7 @@ public class ConfigurationControlManagerTest {
             setName("def").setValue("blah"));
         assertEquals(toMap(entry("abc", "x,y,z"), entry("def", "blah")),
             manager.getConfigs(MYTOPIC));
-        assertEquals("x,y,z", manager.getTopicConfig(MYTOPIC.name(), "abc"));
-        assertNull(manager.getTopicConfig(MYTOPIC.name(), "none-exists"));
+        assertEquals("x,y,z", manager.getTopicConfig(MYTOPIC.name(), "abc").value());
     }
 
     @Test
diff --git a/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java b/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java
index 08213dfdfee..0b754ec124f 100644
--- a/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java
+++ b/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java
@@ -158,7 +158,6 @@ import static org.apache.kafka.clients.admin.AlterConfigOp.OpType.SET;
 import static org.apache.kafka.common.config.ConfigResource.Type.BROKER;
 import static org.apache.kafka.common.config.ConfigResource.Type.TOPIC;
 import static org.apache.kafka.controller.ConfigurationControlManagerTest.BROKER0;
-import static org.apache.kafka.controller.ConfigurationControlManagerTest.SCHEMA;
 import static org.apache.kafka.controller.ConfigurationControlManagerTest.entry;
 import static org.apache.kafka.controller.ControllerRequestContextUtil.ANONYMOUS_CONTEXT;
 import static org.apache.kafka.controller.ControllerRequestContextUtil.anonymousContextFor;
@@ -191,9 +190,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 build()
         ) {
             controlEnv.activeController().registerBroker(ANONYMOUS_CONTEXT,
@@ -235,9 +231,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 build()
         ) {
             controlEnv.activeController().registerBroker(ANONYMOUS_CONTEXT,
@@ -282,9 +275,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 setSessionTimeoutMillis(OptionalLong.of(sessionTimeoutMillis)).
                 setBootstrapMetadata(SIMPLE_BOOTSTRAP).
                 build()
@@ -374,9 +364,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 setSessionTimeoutMillis(OptionalLong.of(sessionTimeoutMillis)).
 
                 setBootstrapMetadata(BootstrapMetadata.fromVersion(MetadataVersion.IBP_4_0_IV0, "test-provided bootstrap ELR enabled")).
@@ -509,9 +496,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 setSessionTimeoutMillis(OptionalLong.of(sessionTimeoutMillis)).
                 setLeaderImbalanceCheckIntervalNs(OptionalLong.of(leaderImbalanceCheckIntervalNs)).
                 setBootstrapMetadata(SIMPLE_BOOTSTRAP).
@@ -651,7 +635,6 @@ public class QuorumControllerTest {
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
                 setControllerBuilderInitializer(controllerBuilder -> {
-                    controllerBuilder.setConfigSchema(SCHEMA);
                     controllerBuilder.setMaxIdleIntervalNs(OptionalLong.of(maxIdleIntervalNs));
                 }).
                 build()
@@ -697,8 +680,6 @@ public class QuorumControllerTest {
                 setLastKRaftVersion(KRaftVersion.fromFeatureLevel(finalizedKraftVersion)).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)).
                 setBootstrapMetadata(SIMPLE_BOOTSTRAP).
                 build()
         ) {
@@ -750,9 +731,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 build()
         ) {
             ListenerCollection listeners = new ListenerCollection();
@@ -832,9 +810,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(3).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 setBootstrapMetadata(SIMPLE_BOOTSTRAP).
                 build()
         ) {
@@ -1042,9 +1017,6 @@ public class QuorumControllerTest {
         try (
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 build()
         ) {
             QuorumController controller = controlEnv.activeController();
@@ -1107,9 +1079,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(1).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 build()
         ) {
             QuorumController controller = controlEnv.activeController();
@@ -1149,9 +1118,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(3).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 build()
         ) {
             QuorumController active = controlEnv.activeController();
@@ -1320,9 +1286,6 @@ public class QuorumControllerTest {
                     initialSnapshot.tempDir.toPath(), new OffsetAndEpoch(0, 0))).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 setBootstrapMetadata(COMPLEX_BOOTSTRAP).
                 build()
         ) {
@@ -1345,9 +1308,6 @@ public class QuorumControllerTest {
             LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv.Builder(3).
                 build();
             QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv.Builder(logEnv).
-                setControllerBuilderInitializer(controllerBuilder ->
-                    controllerBuilder.setConfigSchema(SCHEMA)
-                ).
                 setBootstrapMetadata(COMPLEX_BOOTSTRAP).
                 build()
         ) {
diff --git a/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTestEnv.java b/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTestEnv.java
index dcea7ef21d6..8bece2bb86c 100644
--- a/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTestEnv.java
+++ b/metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTestEnv.java
@@ -17,6 +17,7 @@
 
 package org.apache.kafka.controller;
 
+import org.apache.kafka.metadata.FakeKafkaConfigSchema;
 import org.apache.kafka.metadata.bootstrap.BootstrapMetadata;
 import org.apache.kafka.metalog.LocalLogManagerTestEnv;
 import org.apache.kafka.raft.LeaderAndEpoch;
@@ -115,6 +116,7 @@ public class QuorumControllerTestEnv implements AutoCloseable {
                 MockFaultHandler nonFatalFaultHandler = new MockFaultHandler("nonFatalFaultHandler");
                 builder.setNonFatalFaultHandler(nonFatalFaultHandler);
                 builder.setEligibleLeaderReplicasEnabled(eligibleLeaderReplicasEnabled);
+                builder.setConfigSchema(FakeKafkaConfigSchema.INSTANCE);
                 nonFatalFaultHandlers.put(nodeId, fatalFaultHandler);
                 controllerBuilderInitializer.accept(builder);
                 this.controllers.add(builder.build());
diff --git a/metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java b/metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java
index df62af828e4..b50d32e16bc 100644
--- a/metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java
+++ b/metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.kafka.controller;
 
+import org.apache.kafka.clients.admin.AlterConfigOp;
 import org.apache.kafka.common.DirectoryId;
 import org.apache.kafka.common.ElectionType;
 import org.apache.kafka.common.TopicPartition;
@@ -83,6 +84,7 @@ import org.apache.kafka.metadata.AssignmentsHelper;
 import org.apache.kafka.metadata.BrokerHeartbeatReply;
 import org.apache.kafka.metadata.BrokerRegistration;
 import org.apache.kafka.metadata.BrokerRegistrationInControlledShutdownChange;
+import org.apache.kafka.metadata.FakeKafkaConfigSchema;
 import org.apache.kafka.metadata.LeaderRecoveryState;
 import org.apache.kafka.metadata.PartitionRegistration;
 import org.apache.kafka.metadata.RecordTestUtils;
@@ -104,6 +106,7 @@ import org.junit.jupiter.params.provider.ValueSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -171,6 +174,7 @@ public class ReplicationControlManagerTest {
             private MetadataVersion metadataVersion = MetadataVersion.latestTesting();
             private MockTime mockTime = new MockTime();
             private boolean isElrEnabled = false;
+            private final Map<String, Object> staticConfig = new HashMap<>();
 
             Builder setCreateTopicPolicy(CreateTopicPolicy createTopicPolicy) {
                 this.createTopicPolicy = Optional.of(createTopicPolicy);
@@ -187,6 +191,11 @@ public class ReplicationControlManagerTest {
                 return this;
             }
 
+            Builder setStaticConfig(String key, Object value) {
+                this.staticConfig.put(key, value);
+                return this;
+            }
+
             Builder setMockTime(MockTime mockTime) {
                 this.mockTime = mockTime;
                 return this;
@@ -196,14 +205,8 @@ public class ReplicationControlManagerTest {
                 return new ReplicationControlTestContext(metadataVersion,
                     createTopicPolicy,
                     mockTime,
-                    isElrEnabled);
-            }
-
-            ReplicationControlTestContext build(MetadataVersion metadataVersion) {
-                return new ReplicationControlTestContext(metadataVersion,
-                    createTopicPolicy,
-                    mockTime,
-                    isElrEnabled);
+                    isElrEnabled,
+                    staticConfig);
             }
         }
 
@@ -213,9 +216,7 @@ public class ReplicationControlManagerTest {
         final MockRandom random = new MockRandom();
         final FeatureControlManager featureControl;
         final ClusterControlManager clusterControl;
-        final ConfigurationControlManager configurationControl = new ConfigurationControlManager.Builder().
-            setSnapshotRegistry(snapshotRegistry).
-            build();
+        final ConfigurationControlManager configurationControl;
         final ReplicationControlManager replicationControl;
 
         void replay(List<ApiMessageAndVersion> records) {
@@ -228,9 +229,15 @@ public class ReplicationControlManagerTest {
             MetadataVersion metadataVersion,
             Optional<CreateTopicPolicy> createTopicPolicy,
             MockTime time,
-            Boolean isElrEnabled
+            boolean isElrEnabled,
+            Map<String, Object> staticConfig
         ) {
             this.time = time;
+            this.configurationControl = new ConfigurationControlManager.Builder().
+                setSnapshotRegistry(snapshotRegistry).
+                setStaticConfig(staticConfig).
+                setKafkaConfigSchema(FakeKafkaConfigSchema.INSTANCE).
+                build();
             this.featureControl = new FeatureControlManager.Builder().
                 setSnapshotRegistry(snapshotRegistry).
                 setQuorumFeatures(new QuorumFeatures(0,
@@ -483,6 +490,10 @@ public class ReplicationControlManagerTest {
             replay(singletonList(new ApiMessageAndVersion(configRecord, (short) 0)));
         }
 
+        void fenceBrokers(Integer... brokerIds) {
+            fenceBrokers(Utils.mkSet(brokerIds));
+        }
+
         void fenceBrokers(Set<Integer> brokerIds) {
             time.sleep(BROKER_SESSION_TIMEOUT_MS);
 
@@ -524,6 +535,11 @@ public class ReplicationControlManagerTest {
         }
     }
 
+    static CreateTopicsResponseData withoutConfigs(CreateTopicsResponseData data) {
+        data.topics().forEach(t -> t.configs().clear());
+        return data;
+    }
+
     private static class MockCreateTopicPolicy implements CreateTopicPolicy {
         private final List<RequestMetadata> expecteds;
         private final AtomicLong index = new AtomicLong(0);
@@ -598,7 +614,7 @@ public class ReplicationControlManagerTest {
             setNumPartitions(1).setReplicationFactor((short) 3).
             setErrorMessage(null).setErrorCode((short) 0).
             setTopicId(result3.response().topics().find("foo").topicId()));
-        assertEquals(expectedResponse3, result3.response());
+        assertEquals(expectedResponse3, withoutConfigs(result3.response()));
         ctx.replay(result3.records());
         assertEquals(new PartitionRegistration.Builder().setReplicas(new int[] {1, 2, 0}).
             setDirectories(new Uuid[] {
@@ -660,6 +676,9 @@ public class ReplicationControlManagerTest {
             setNumPartitions(1).setReplicationFactor((short) 3).
             setErrorMessage(null).setErrorCode((short) 0).
             setTopicId(result.response().topics().find("foo").topicId()));
+        for (CreatableTopicResult topic : result.response().topics()) {
+            topic.configs().clear();
+        }
         assertEquals(expectedResponse, result.response());
 
         ctx.replay(result.records());
@@ -1380,7 +1399,7 @@ public class ReplicationControlManagerTest {
             setNumPartitions(3).setReplicationFactor((short) 2).
             setErrorMessage(null).setErrorCode((short) 0).
             setTopicId(topicId));
-        assertEquals(expectedResponse, createResult.response());
+        assertEquals(expectedResponse, withoutConfigs(createResult.response()));
         // Until the records are replayed, no changes are made
         assertNull(replicationControl.getPartition(topicId, 0));
         assertEmptyTopicConfigs(ctx, "foo");
@@ -2245,7 +2264,9 @@ public class ReplicationControlManagerTest {
     @ParameterizedTest
     @ValueSource(booleans = {true, false})
     public void testElectUncleanLeaders_WithoutElr(boolean electAllPartitions) {
-        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder().build(MetadataVersion.IBP_3_6_IV1);
+        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder().
+            setMetadataVersion(MetadataVersion.IBP_3_6_IV1).
+            build();
         ReplicationControlManager replication = ctx.replicationControl;
         ctx.registerBrokers(0, 1, 2, 3, 4);
         ctx.unfenceBrokers(0, 1, 2, 3, 4);
@@ -2608,6 +2629,85 @@ public class ReplicationControlManagerTest {
         assertFalse(balanceResult.response());
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = {"none", "static", "dynamic_cluster", "dynamic_node", "dynamic_topic"})
+    public void testMaybeTriggerUncleanLeaderElectionForLeaderlessPartitions(String uncleanConfig) {
+        ReplicationControlTestContext.Builder ctxBuilder = new ReplicationControlTestContext.Builder();
+        if (uncleanConfig.equals("static")) {
+            ctxBuilder.setStaticConfig(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "true");
+        }
+        ReplicationControlTestContext ctx = ctxBuilder.build();
+        ReplicationControlManager replication = ctx.replicationControl;
+        ctx.registerBrokers(0, 1, 2, 3, 4);
+        ctx.unfenceBrokers(0, 1, 2, 3, 4);
+        Uuid fooId = ctx.createTestTopic("foo", new int[][]{
+            new int[]{1, 2, 4}, new int[]{1, 3, 4}, new int[]{0, 2, 4}}).topicId();
+        assertFalse(replication.areSomePartitionsLeaderless());
+        ctx.fenceBrokers(0, 1, 2, 3, 4);
+        assertTrue(replication.areSomePartitionsLeaderless());
+        for (int partitionId : Arrays.asList(0, 1, 2)) {
+            assertArrayEquals(new int[] {4}, ctx.replicationControl.getPartition(fooId, partitionId).isr);
+            assertEquals(-1, ctx.replicationControl.getPartition(fooId, partitionId).leader);
+        }
+
+        // Unfence broker 2. It is now available to be the leader for partition 0 and 2, after
+        // an unclean election.
+        ctx.unfenceBrokers(2);
+
+        if (uncleanConfig.equals("static")) {
+            // If we statically configured unclean leader election, the election already happened.
+            assertArrayEquals(new int[] {2}, ctx.replicationControl.getPartition(fooId, 0).isr);
+            assertEquals(2, ctx.replicationControl.getPartition(fooId, 0).leader);
+            assertArrayEquals(new int[] {4}, ctx.replicationControl.getPartition(fooId, 1).isr);
+            assertEquals(-1, ctx.replicationControl.getPartition(fooId, 1).leader);
+            assertArrayEquals(new int[] {2}, ctx.replicationControl.getPartition(fooId, 2).isr);
+            assertEquals(2, ctx.replicationControl.getPartition(fooId, 2).leader);
+        } else {
+            // Otherwise, check that the election did NOT happen.
+            for (int partitionId : Arrays.asList(0, 1, 2)) {
+                assertArrayEquals(new int[] {4}, ctx.replicationControl.getPartition(fooId, partitionId).isr);
+                assertEquals(-1, ctx.replicationControl.getPartition(fooId, partitionId).leader);
+            }
+        }
+
+        // If we're setting unclean leader election dynamically, do that here.
+        if (uncleanConfig.equals("dynamic_cluster")) {
+            ctx.replay(ctx.configurationControl.incrementalAlterConfigs(
+                Collections.singletonMap(new ConfigResource(ConfigResource.Type.BROKER, ""),
+                    Collections.singletonMap(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG,
+                        new AbstractMap.SimpleImmutableEntry<>(AlterConfigOp.OpType.SET, "true"))),
+                true).records());
+        } else if (uncleanConfig.equals("dynamic_node")) {
+            ctx.replay(ctx.configurationControl.incrementalAlterConfigs(
+                Collections.singletonMap(new ConfigResource(ConfigResource.Type.BROKER, "0"),
+                    Collections.singletonMap(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG,
+                        new AbstractMap.SimpleImmutableEntry<>(AlterConfigOp.OpType.SET, "true"))),
+                true).records());
+        } else if (uncleanConfig.equals("dynamic_topic")) {
+            ctx.replay(ctx.configurationControl.incrementalAlterConfigs(
+                Collections.singletonMap(new ConfigResource(ConfigResource.Type.TOPIC, "foo"),
+                    Collections.singletonMap(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG,
+                        new AbstractMap.SimpleImmutableEntry<>(AlterConfigOp.OpType.SET, "true"))),
+                true).records());
+        }
+        ControllerResult<Boolean> balanceResult = replication.maybeElectUncleanLeaders();
+        assertFalse(balanceResult.response());
+        if (uncleanConfig.equals("none") || uncleanConfig.equals("static")) {
+            assertEquals(0, balanceResult.records().size(), "Expected no records, but " +
+                balanceResult.records().size() + " were found.");
+        } else {
+            assertNotEquals(0, balanceResult.records().size(), "Expected some records, but " +
+                "none were found.");
+            ctx.replay(balanceResult.records());
+            assertArrayEquals(new int[] {2}, ctx.replicationControl.getPartition(fooId, 0).isr);
+            assertEquals(2, ctx.replicationControl.getPartition(fooId, 0).leader);
+            assertArrayEquals(new int[] {4}, ctx.replicationControl.getPartition(fooId, 1).isr);
+            assertEquals(-1, ctx.replicationControl.getPartition(fooId, 1).leader);
+            assertArrayEquals(new int[] {2}, ctx.replicationControl.getPartition(fooId, 2).isr);
+            assertEquals(2, ctx.replicationControl.getPartition(fooId, 2).leader);
+        }
+    }
+
     private void assertElectLeadersResponse(
         ElectLeadersResponseData expected,
         ElectLeadersResponseData actual
diff --git a/metadata/src/test/java/org/apache/kafka/controller/metrics/ControllerMetadataMetricsTest.java b/metadata/src/test/java/org/apache/kafka/controller/metrics/ControllerMetadataMetricsTest.java
index 984ad68d0c1..345f0264c5f 100644
--- a/metadata/src/test/java/org/apache/kafka/controller/metrics/ControllerMetadataMetricsTest.java
+++ b/metadata/src/test/java/org/apache/kafka/controller/metrics/ControllerMetadataMetricsTest.java
@@ -17,7 +17,10 @@
 
 package org.apache.kafka.controller.metrics;
 
+import org.apache.kafka.common.utils.MockTime;
+
 import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.Meter;
 import com.yammer.metrics.core.MetricName;
 import com.yammer.metrics.core.MetricsRegistry;
 
@@ -48,7 +51,8 @@ public class ControllerMetadataMetricsTest {
                         "kafka.controller:type=KafkaController,name=MetadataErrorCount",
                         "kafka.controller:type=KafkaController,name=OfflinePartitionsCount",
                         "kafka.controller:type=KafkaController,name=PreferredReplicaImbalanceCount",
-                        "kafka.controller:type=KafkaController,name=ZkMigrationState"
+                        "kafka.controller:type=KafkaController,name=ZkMigrationState",
+                        "kafka.controller:type=ControllerStats,name=UncleanLeaderElectionsPerSec"
                     )));
             }
             ControllerMetricsTestUtils.assertMetricsForTypeEqual(registry, "KafkaController",
@@ -174,4 +178,21 @@ public class ControllerMetadataMetricsTest {
             (m, v) -> m.addToPreferredReplicaImbalanceCount(v)
         );
     }
+
+    @SuppressWarnings("LocalVariableName")
+    @Test
+    public void testUpdateUncleanLeaderElection() {
+        MetricsRegistry registry = new MetricsRegistry();
+        MockTime time = new MockTime();
+        try (ControllerMetadataMetrics metrics = new ControllerMetadataMetrics(Optional.of(registry))) {
+            Meter UncleanLeaderElectionsPerSec = (Meter) registry
+                    .allMetrics()
+                    .get(metricName("ControllerStats", "UncleanLeaderElectionsPerSec"));
+            assertEquals(0, UncleanLeaderElectionsPerSec.count());
+            metrics.updateUncleanLeaderElection(2);
+            assertEquals(2, UncleanLeaderElectionsPerSec.count());
+        } finally {
+            registry.shutdown();
+        }
+    }
 }
diff --git a/metadata/src/test/java/org/apache/kafka/metadata/FakeKafkaConfigSchema.java b/metadata/src/test/java/org/apache/kafka/metadata/FakeKafkaConfigSchema.java
new file mode 100644
index 00000000000..c535e108878
--- /dev/null
+++ b/metadata/src/test/java/org/apache/kafka/metadata/FakeKafkaConfigSchema.java
@@ -0,0 +1,60 @@
+/*
+ * 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 org.apache.kafka.metadata;
+
+import org.apache.kafka.common.config.ConfigDef;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.server.config.ConfigSynonym;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.kafka.common.config.ConfigDef.Importance.HIGH;
+import static org.apache.kafka.common.config.ConfigDef.Type.BOOLEAN;
+import static org.apache.kafka.common.config.ConfigDef.Type.INT;
+import static org.apache.kafka.common.config.ConfigResource.Type.BROKER;
+import static org.apache.kafka.common.config.ConfigResource.Type.TOPIC;
+
+/**
+ * A fake KafkaConfigSchema object that can be used in tests.
+ */
+public class FakeKafkaConfigSchema {
+    public static final Map<ConfigResource.Type, ConfigDef> CONFIGS = new HashMap<>();
+
+    static {
+        CONFIGS.put(BROKER, new ConfigDef().
+            define("unclean.leader.election.enable", BOOLEAN, "false", HIGH, "").
+            define("min.insync.replicas", INT, "1", HIGH, ""));
+        CONFIGS.put(TOPIC, new ConfigDef().
+            define("unclean.leader.election.enable", BOOLEAN, "false", HIGH, "").
+            define("min.insync.replicas", INT, "1", HIGH, ""));
+    }
+
+    public static final Map<String, List<ConfigSynonym>> SYNONYMS = new HashMap<>();
+
+    static {
+        SYNONYMS.put("unclean.leader.election.enable",
+            Arrays.asList(new ConfigSynonym("unclean.leader.election.enable")));
+        SYNONYMS.put("min.insync.replicas",
+            Arrays.asList(new ConfigSynonym("min.insync.replicas")));
+    }
+
+    public static final KafkaConfigSchema INSTANCE = new KafkaConfigSchema(CONFIGS, SYNONYMS);
+}
diff --git a/server/src/main/java/org/apache/kafka/server/config/ReplicationConfigs.java b/server/src/main/java/org/apache/kafka/server/config/ReplicationConfigs.java
index aa061a0afdd..3bce284e47b 100644
--- a/server/src/main/java/org/apache/kafka/server/config/ReplicationConfigs.java
+++ b/server/src/main/java/org/apache/kafka/server/config/ReplicationConfigs.java
@@ -25,6 +25,8 @@ import org.apache.kafka.server.common.MetadataVersion;
 import org.apache.kafka.server.common.MetadataVersionValidator;
 import org.apache.kafka.storage.internals.log.LogConfig;
 
+import java.util.concurrent.TimeUnit;
+
 import static org.apache.kafka.common.config.ConfigDef.Importance.HIGH;
 import static org.apache.kafka.common.config.ConfigDef.Importance.MEDIUM;
 import static org.apache.kafka.common.config.ConfigDef.Range.atLeast;
@@ -115,6 +117,10 @@ public class ReplicationConfigs {
     public static final int LEADER_IMBALANCE_CHECK_INTERVAL_SECONDS_DEFAULT = 300;
     public static final String LEADER_IMBALANCE_CHECK_INTERVAL_SECONDS_DOC = "The frequency with which the partition rebalance check is triggered by the controller";
 
+    public static final String UNCLEAN_LEADER_ELECTION_INTERVAL_MS_CONFIG = "unclean.leader.election.interval.ms";
+    public static final long UNCLEAN_LEADER_ELECTION_INTERVAL_MS_DEFAULT = TimeUnit.MINUTES.toMillis(5);
+    public static final String UNCLEAN_LEADER_ELECTION_INTERVAL_MS_DOC = "The frequency with which the controller checks if it should perform an unclean leader election for leaderless partitions.";
+
     public static final String UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG = ServerTopicConfigSynonyms.serverSynonym(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG);
     public static final String UNCLEAN_LEADER_ELECTION_ENABLE_DOC = "Indicates whether to enable replicas not in the ISR set to be elected as leader as a last resort, even though doing so may result in data loss";
 
@@ -159,6 +165,7 @@ public class ReplicationConfigs {
             .define(AUTO_LEADER_REBALANCE_ENABLE_CONFIG, BOOLEAN, AUTO_LEADER_REBALANCE_ENABLE_DEFAULT, HIGH, AUTO_LEADER_REBALANCE_ENABLE_DOC)
             .define(LEADER_IMBALANCE_PER_BROKER_PERCENTAGE_CONFIG, INT, LEADER_IMBALANCE_PER_BROKER_PERCENTAGE_DEFAULT, HIGH, LEADER_IMBALANCE_PER_BROKER_PERCENTAGE_DOC)
             .define(LEADER_IMBALANCE_CHECK_INTERVAL_SECONDS_CONFIG, LONG, LEADER_IMBALANCE_CHECK_INTERVAL_SECONDS_DEFAULT, atLeast(1), HIGH, LEADER_IMBALANCE_CHECK_INTERVAL_SECONDS_DOC)
+            .defineInternal(UNCLEAN_LEADER_ELECTION_INTERVAL_MS_CONFIG, LONG, UNCLEAN_LEADER_ELECTION_INTERVAL_MS_DEFAULT, atLeast(1), MEDIUM, UNCLEAN_LEADER_ELECTION_INTERVAL_MS_DOC)
             .define(UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, BOOLEAN, LogConfig.DEFAULT_UNCLEAN_LEADER_ELECTION_ENABLE, HIGH, UNCLEAN_LEADER_ELECTION_ENABLE_DOC)
             .define(INTER_BROKER_SECURITY_PROTOCOL_CONFIG, STRING, INTER_BROKER_SECURITY_PROTOCOL_DEFAULT, ConfigDef.ValidString.in(Utils.enumOptions(SecurityProtocol.class)), MEDIUM, INTER_BROKER_SECURITY_PROTOCOL_DOC)
             .define(INTER_BROKER_PROTOCOL_VERSION_CONFIG, STRING, INTER_BROKER_PROTOCOL_VERSION_DEFAULT, new MetadataVersionValidator(), MEDIUM, INTER_BROKER_PROTOCOL_VERSION_DOC)
diff --git a/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestHarness.java b/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestHarness.java
index 91b24fc2b0d..fc21758518b 100644
--- a/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestHarness.java
+++ b/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestHarness.java
@@ -72,7 +72,7 @@ public abstract class TieredStorageTestHarness extends IntegrationTestHarness {
 
     @SuppressWarnings("deprecation")
     @Override
-    public Seq<Properties> kraftControllerConfigs() {
+    public Seq<Properties> kraftControllerConfigs(TestInfo testInfo) {
         return JavaConverters.asScalaBuffer(Collections.singletonList(overridingProps())).toSeq();
     }
 


(kafka) 01/02: KAFKA-15746: KRaft support in ControllerMutationQuotaTest (#16620)

Posted by cm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cmccabe pushed a commit to branch 3.9
in repository https://gitbox.apache.org/repos/asf/kafka.git

commit a4728f566f90150916126037dc8748ae74be4f4d
Author: Dmitry Werner <gr...@gmail.com>
AuthorDate: Tue Jul 30 19:04:16 2024 +0500

    KAFKA-15746: KRaft support in ControllerMutationQuotaTest (#16620)
    
    
    Reviewers: Mickael Maison <mi...@gmail.com>
---
 .../kafka/server/ControllerMutationQuotaTest.scala | 89 +++++++++++++++-------
 1 file changed, 61 insertions(+), 28 deletions(-)

diff --git a/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala b/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala
index 86a32d126c2..3c06b33640e 100644
--- a/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala
+++ b/core/src/test/scala/unit/kafka/server/ControllerMutationQuotaTest.scala
@@ -26,6 +26,7 @@ import org.apache.kafka.common.message.CreateTopicsRequestData
 import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic
 import org.apache.kafka.common.message.DeleteTopicsRequestData
 import org.apache.kafka.common.metrics.KafkaMetric
+import org.apache.kafka.common.network.ListenerName
 import org.apache.kafka.common.protocol.ApiKeys
 import org.apache.kafka.common.protocol.Errors
 import org.apache.kafka.common.quota.ClientQuotaAlteration
@@ -47,8 +48,11 @@ import org.apache.kafka.test.{TestUtils => JTestUtils}
 import org.junit.jupiter.api.Assertions.assertEquals
 import org.junit.jupiter.api.Assertions.assertTrue
 import org.junit.jupiter.api.Assertions.fail
-import org.junit.jupiter.api.{BeforeEach, Test, TestInfo}
+import org.junit.jupiter.api.{BeforeEach, TestInfo}
+import org.junit.jupiter.params.ParameterizedTest
+import org.junit.jupiter.params.provider.ValueSource
 
+import scala.collection.Seq
 import scala.jdk.CollectionConverters._
 
 object ControllerMutationQuotaTest {
@@ -106,6 +110,12 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     properties.put(QuotaConfigs.CONTROLLER_QUOTA_WINDOW_SIZE_SECONDS_CONFIG, ControllerQuotaWindowSizeSeconds.toString)
   }
 
+  override def kraftControllerConfigs(): Seq[Properties] = {
+    val props = super.kraftControllerConfigs()
+    props.head.setProperty(QuotaConfigs.NUM_CONTROLLER_QUOTA_SAMPLES_CONFIG, ControllerQuotaSamples.toString)
+    props
+  }
+
   @BeforeEach
   override def setUp(testInfo: TestInfo): Unit = {
     super.setUp(testInfo)
@@ -115,8 +125,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     waitUserQuota(ThrottledPrincipal.getName, ControllerMutationRate)
   }
 
-  @Test
-  def testSetUnsetQuota(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testSetUnsetQuota(quorum: String): Unit = {
     val rate = 1.5
     val principal = new KafkaPrincipal(KafkaPrincipal.USER_TYPE, "User")
     // Default Value
@@ -131,8 +142,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     waitUserQuota(principal.getName, Long.MaxValue)
   }
 
-  @Test
-  def testQuotaMetric(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testQuotaMetric(quorum: String): Unit = {
     asPrincipal(ThrottledPrincipal) {
       // Metric is lazily created
       assertTrue(quotaMetric(principal.getName).isEmpty)
@@ -153,8 +165,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testStrictCreateTopicsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testStrictCreateTopicsRequest(quorum: String): Unit = {
     asPrincipal(ThrottledPrincipal) {
       // Create two topics worth of 30 partitions each. As we use a strict quota, we
       // expect one to be created and one to be rejected.
@@ -176,8 +189,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testPermissiveCreateTopicsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testPermissiveCreateTopicsRequest(quorum: String): Unit = {
     asPrincipal(ThrottledPrincipal) {
       // Create two topics worth of 30 partitions each. As we use a permissive quota, we
       // expect both topics to be created.
@@ -189,8 +203,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testUnboundedCreateTopicsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testUnboundedCreateTopicsRequest(quorum: String): Unit = {
     asPrincipal(UnboundedPrincipal) {
       // Create two topics worth of 30 partitions each. As we use an user without quota, we
       // expect both topics to be created. The throttle time should be equal to 0.
@@ -200,8 +215,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testStrictDeleteTopicsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testStrictDeleteTopicsRequest(quorum: String): Unit = {
     asPrincipal(UnboundedPrincipal) {
       createTopics(TopicsWith30Partitions, StrictCreateTopicsRequestVersion)
     }
@@ -227,8 +243,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testPermissiveDeleteTopicsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testPermissiveDeleteTopicsRequest(quorum: String): Unit = {
     asPrincipal(UnboundedPrincipal) {
       createTopics(TopicsWith30Partitions, StrictCreateTopicsRequestVersion)
     }
@@ -244,8 +261,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testUnboundedDeleteTopicsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testUnboundedDeleteTopicsRequest(quorum: String): Unit = {
     asPrincipal(UnboundedPrincipal) {
       createTopics(TopicsWith30Partitions, StrictCreateTopicsRequestVersion)
 
@@ -257,8 +275,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testStrictCreatePartitionsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testStrictCreatePartitionsRequest(quorum: String): Unit = {
     asPrincipal(UnboundedPrincipal) {
       createTopics(TopicsWithOnePartition, StrictCreatePartitionsRequestVersion)
     }
@@ -284,8 +303,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testPermissiveCreatePartitionsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testPermissiveCreatePartitionsRequest(quorum: String): Unit = {
     asPrincipal(UnboundedPrincipal) {
       createTopics(TopicsWithOnePartition, StrictCreatePartitionsRequestVersion)
     }
@@ -301,8 +321,9 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
     }
   }
 
-  @Test
-  def testUnboundedCreatePartitionsRequest(): Unit = {
+  @ParameterizedTest
+  @ValueSource(strings = Array("zk", "kraft"))
+  def testUnboundedCreatePartitionsRequest(quorum: String): Unit = {
     asPrincipal(UnboundedPrincipal) {
       createTopics(TopicsWithOnePartition, StrictCreatePartitionsRequestVersion)
 
@@ -364,23 +385,31 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
   }
 
   private def waitUserQuota(user: String, expectedQuota: Double): Unit = {
-    val quotaManager = servers.head.quotaManagers.controllerMutation
+    val quotaManager = brokers.head.quotaManagers.controllerMutation
+    val controllerQuotaManager =
+      if (isKRaftTest()) Option(controllerServers.head.quotaManagers.controllerMutation)
+      else Option.empty
     var actualQuota = Double.MinValue
 
     TestUtils.waitUntilTrue(() => {
       actualQuota = quotaManager.quota(user, "").bound()
-      expectedQuota == actualQuota
+      if (controllerQuotaManager.isDefined)
+        expectedQuota == actualQuota && expectedQuota == controllerQuotaManager.get.quota(user, "").bound()
+      else
+        expectedQuota == actualQuota
     }, s"Quota of $user is not $expectedQuota but $actualQuota")
   }
 
   private def quotaMetric(user: String): Option[KafkaMetric] = {
-    val metrics = servers.head.metrics
+    val metrics =
+      if (isKRaftTest()) controllerServers.head.metrics
+      else brokers.head.metrics
     val metricName = metrics.metricName(
       "tokens",
       QuotaType.ControllerMutation.toString,
       "Tracking remaining tokens in the token bucket per user/client-id",
       Map(DefaultTags.User -> user, DefaultTags.ClientId -> "").asJava)
-    Option(servers.head.metrics.metric(metricName))
+    Option(metrics.metric(metricName))
   }
 
   private def waitQuotaMetric(user: String, expectedQuota: Double): Unit = {
@@ -416,6 +445,10 @@ class ControllerMutationQuotaTest extends BaseRequestTest {
 
   private def sendAlterClientQuotasRequest(entries: Iterable[ClientQuotaAlteration]): AlterClientQuotasResponse = {
     val request = new AlterClientQuotasRequest.Builder(entries.asJavaCollection, false).build()
-    connectAndReceive[AlterClientQuotasResponse](request, destination = controllerSocketServer)
+    connectAndReceive[AlterClientQuotasResponse](
+      request,
+      destination = controllerSocketServer,
+      if (isKRaftTest()) ListenerName.normalised("CONTROLLER") else listenerName
+    )
   }
 }