ControllerResult<Boolean> maybeBalancePartitionLeaders() {
        // AutoMQ inject start
        if (featureControl.autoMQVersion().isReassignmentV1Supported()) {
            // AutoMQ isr/replica only has a single node
            return ControllerResult.of(Collections.emptyList(), false);
        }
        // AutoMQ inject end

        List<ApiMessageAndVersion> records = new ArrayList<>();

        boolean rescheduleImmediately = false;
        for (TopicIdPartition topicPartition : imbalancedPartitions) {
            if (records.size() >= maxElectionsPerImbalance) {
                rescheduleImmediately = true;
                break;
            }

            TopicControlInfo topic = topics.get(topicPartition.topicId());
            if (topic == null) {
                log.error("Skipping unknown imbalanced topic {}", topicPartition);
                continue;
            }

            PartitionRegistration partition = topic.parts.get(topicPartition.partitionId());
            if (partition == null) {
                log.error("Skipping unknown imbalanced partition {}", topicPartition);
                continue;
            }

            // Attempt to perform a preferred leader election
            new PartitionChangeBuilder(
                partition,
                topicPartition.topicId(),
                topicPartition.partitionId(),
                new LeaderAcceptor(clusterControl, partition),
                featureControl.metadataVersion(),
                getTopicEffectiveMinIsr(topic.name)
            )
                .setElection(PartitionChangeBuilder.Election.PREFERRED)
                .setZkMigrationEnabled(clusterControl.zkRegistrationAllowed())
                .setEligibleLeaderReplicasEnabled(isElrEnabled())
                .setDefaultDirProvider(clusterDescriber)
                .build().ifPresent(records::add);
        }

        return ControllerResult.of(records, rescheduleImmediately);
    }
Attempt to elect a preferred leader for all topic partitions which have a leader that is not the preferred replica.

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
metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java