CalvinConfluent commented on code in PR #14593: URL: https://github.com/apache/kafka/pull/14593#discussion_r1369067852
########## metadata/src/main/java/org/apache/kafka/controller/PartitionChangeBuilder.java: ########## @@ -253,17 +269,30 @@ private ElectionResult electAnyLeader() { return new ElectionResult(NO_LEADER, false); } + private boolean canElectLastKnownLeader() { + return eligibleLeaderReplicasEnabled && lastKnownLeaderEnabled && targetElr.isEmpty() && targetIsr.isEmpty() + && partition.lastKnownElr.length == 1 && isAcceptableLeader.test(partition.lastKnownElr[0]); + } + private boolean isValidNewLeader(int replica) { - return targetIsr.contains(replica) && isAcceptableLeader.test(replica); + // The valid new leader should be in either ISR or in ELR when ISR is empty. + return (targetIsr.contains(replica) || (targetIsr.isEmpty() && targetElr.contains(replica))) && isAcceptableLeader.test(replica); } private void tryElection(PartitionChangeRecord record) { ElectionResult electionResult = electLeader(); if (electionResult.node != partition.leader) { // generating log messages for partition elections can get expensive on large clusters, // so only log clean elections at TRACE level; log unclean elections at INFO level - // to ensure the message is emitted since an unclean election can lead to data loss. - if (electionResult.unclean) { + // to ensure the message is emitted since an unclean election can lead to data loss; + if (targetElr.contains(electionResult.node)) { + targetIsr = Collections.singletonList(electionResult.node); + targetElr = targetElr.stream().filter(replica -> replica != electionResult.node).collect(Collectors.toList()); + targetLastKnownElr = targetLastKnownElr.stream() + .filter(replica -> replica != electionResult.node).collect(Collectors.toList()); Review Comment: Good catch. A replica can't be in targetElr and targetLastKnownElr. Removed. -- 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