jsancio commented on a change in pull request #11893: URL: https://github.com/apache/kafka/pull/11893#discussion_r829532685
########## File path: metadata/src/main/java/org/apache/kafka/controller/PartitionChangeBuilder.java ########## @@ -104,53 +117,73 @@ public PartitionChangeBuilder setTargetAdding(List<Integer> targetAdding) { return this; } - boolean shouldTryElection() { - // If the new isr doesn't have the current leader, we need to try to elect a new - // one. Note: this also handles the case where the current leader is NO_LEADER, - // since that value cannot appear in targetIsr. - if (!targetIsr.contains(partition.leader)) return true; - - // Check if we want to try to get away from a non-preferred leader. - if (alwaysElectPreferredIfPossible && !partition.hasPreferredLeader()) return true; + // VisibleForTesting + static class ElectionResult { + final int node; + final boolean unclean; - return false; + private ElectionResult(int node, boolean unclean) { + this.node = node; + this.unclean = unclean; + } } - class BestLeader { - final int node; - final boolean unclean; + // VisibleForTesting + /** + * Perform leader election based on the partition state and leader election type. + * + * See documentation for the Election type to see more details on the election types supported. + */ + ElectionResult electLeader() { + // 1. Check if the election is not PREFERRED and we already have a valid leader + if (election != Election.PREFERRED && targetIsr.contains(partition.leader) && isAcceptableLeader.apply(partition.leader)) { + // Don't consider a new leader since the current leader meets all the constraints + return new ElectionResult(partition.leader, false); + } - BestLeader() { - for (int replica : targetReplicas) { - if (targetIsr.contains(replica) && isAcceptableLeader.apply(replica)) { - this.node = replica; - this.unclean = false; - return; - } - } - if (uncleanElectionOk.get()) { - for (int replica : targetReplicas) { - if (isAcceptableLeader.apply(replica)) { - this.node = replica; - this.unclean = true; - return; - } - } + // 2. Attempt preferred replica election + int preferredReplica = targetReplicas.get(0); + if (targetIsr.contains(preferredReplica) && isAcceptableLeader.apply(preferredReplica)) { + return new ElectionResult(preferredReplica, false); + } + + // 3. Preferred replica was not elected, only continue if the current leader is not a valid leader + if (targetIsr.contains(partition.leader) && isAcceptableLeader.apply(partition.leader)) { Review comment: @junrao how about this algorithm: https://github.com/apache/kafka/pull/11893/commits/4ef0b161c2f44933139989ec7e680f2c05839cc5 Is it easier to read and understand? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org