showuon commented on code in PR #16866:
URL: https://github.com/apache/kafka/pull/16866#discussion_r1728710829


##########
metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java:
##########
@@ -1627,26 +1613,63 @@ ControllerResult<Void> maybeFenceOneStaleBroker() {
         return ControllerResult.of(records, null);
     }
 
-    boolean arePartitionLeadersImbalanced() {
-        return !imbalancedPartitions.isEmpty();
+    boolean shouldScheduleAdjustPartitionLeaders() {
+        return !imbalancedPartitions.isEmpty() || 
brokersToIsrs.partitionsWithNoLeader().hasNext();
     }
 
     /**
-     * Attempt to elect a preferred leader for all topic partitions which have 
a leader that is not the preferred replica.
+     * Check if we can do an election for partitions with no leader or a 
leader other than the preferred one.
      *
      * 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 if there may be more available 
preferred replicas to elect as leader
      */
-    ControllerResult<Boolean> maybeBalancePartitionLeaders() {
+    ControllerResult<Boolean> maybeAdjustPartitionLeaders() {
         List<ApiMessageAndVersion> records = new ArrayList<>();
+        maybeTriggerUncleanLeaderElectionForLeaderlessPartitions(records, 
maxElectionsPerImbalance);
+        maybeTriggerLeaderChangeForPartitionsWithoutPreferredLeader(records, 
maxElectionsPerImbalance);
+        boolean rescheduleImmediately = 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.
+     */
+    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 of configuration.", topic.name, 
topicIdPartition.partitionId());

Review Comment:
   nit: to make it clear:
   "because of ~configuration~ `unclean.leader.election.enable` is disabled."



##########
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()).

Review Comment:
   Good catch! KRaft quorum doesn't rely on ISR at all.



##########
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 {

Review Comment:
   Thanks for creating a simple config schema to speed up the test.



##########
metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java:
##########
@@ -1627,26 +1613,63 @@ ControllerResult<Void> maybeFenceOneStaleBroker() {
         return ControllerResult.of(records, null);
     }
 
-    boolean arePartitionLeadersImbalanced() {
-        return !imbalancedPartitions.isEmpty();
+    boolean shouldScheduleAdjustPartitionLeaders() {
+        return !imbalancedPartitions.isEmpty() || 
brokersToIsrs.partitionsWithNoLeader().hasNext();
     }
 
     /**
-     * Attempt to elect a preferred leader for all topic partitions which have 
a leader that is not the preferred replica.
+     * Check if we can do an election for partitions with no leader or a 
leader other than the preferred one.
      *
      * 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 if there may be more available 
preferred replicas to elect as leader
      */
-    ControllerResult<Boolean> maybeBalancePartitionLeaders() {
+    ControllerResult<Boolean> maybeAdjustPartitionLeaders() {
         List<ApiMessageAndVersion> records = new ArrayList<>();
+        maybeTriggerUncleanLeaderElectionForLeaderlessPartitions(records, 
maxElectionsPerImbalance);
+        maybeTriggerLeaderChangeForPartitionsWithoutPreferredLeader(records, 
maxElectionsPerImbalance);
+        boolean rescheduleImmediately = 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.

Review Comment:
   nit: miss another param `maxElections`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to