private boolean partitionChanged(String topic,
                                     TopicInfo topicInfo,
                                     BuiltInPartitioner.StickyPartitionInfo partitionInfo,
                                     Deque<ProducerBatch> deque, long nowMs,
                                     Cluster cluster) {
        if (topicInfo.builtInPartitioner.isPartitionChanged(partitionInfo)) {
            log.trace("Partition {} for topic {} switched by a concurrent append, retrying",
                    partitionInfo.partition(), topic);
            return true;
        }

        // We might have disabled partition switch if the queue had incomplete batches.
        // Check if all batches are full now and switch .
        if (allBatchesFull(deque)) {
            topicInfo.builtInPartitioner.updatePartitionInfo(partitionInfo, 0, cluster, true);
            if (topicInfo.builtInPartitioner.isPartitionChanged(partitionInfo)) {
                log.trace("Completed previously disabled switch for topic {} partition {}, retrying",
                        topic, partitionInfo.partition());
                return true;
            }
        }

        return false;
    }
Check if partition concurrently changed, or we need to complete previously disabled partition change.

@param topic The topic
@param topicInfo The topic info
@param partitionInfo The built-in partitioner's partition info
@param deque The partition queue
@param nowMs The current time, in milliseconds
@param cluster THe cluster metadata
@return 'true' if partition changed and we need to get new partition info and retry,
        'false' otherwise
clients/src/main/java/org/apache/kafka/clients/producer/internals/RecordAccumulator.java