boolean hasLeaderChangedForTheOngoingRetry() {
        int attempts = attempts();
        boolean isRetry = attempts >= 1;
        if (!isRetry)
            return false;
        return attempts == attemptsWhenLeaderLastChanged;
    }
It will return true, for a when batch is being retried, it will be retried to a newer leader.
clients/src/main/java/org/apache/kafka/clients/producer/internals/ProducerBatch.java