Repository: ansible/awx
File: awx/main/scheduler/task_manager_models.py (Python)

Code before:
                continue
            would_be_remaining = i.remaining_capacity - impact
            # hybrid nodes _always_ control their own tasks
            if add_hybrid_control_cost and i.node_type == 'hybrid':
                would_be_remaining -= settings.AWX_CONTROL_NODE_TASK_IMPACT
            if would_be_remaining >= 0 and (instance_most_capacity is None or would_be_remaining > most_remaining_capacity):
                instance_most_capacity = i
                most_remaining_capacity = would_be_remaining
        return instance_most_capacity

    def find_largest_idle_instance(self, instance_group_name, capacity_type='execution'):
        largest_instance = None
        instances = self.instance_groups[instance_group_name]['instances']
        for i in instances:
            if i.node_type not in (capacity_type, 'hybrid'):
                continue
            if (hasattr(i, 'jobs_running') and i.jobs_running == 0) or i.remaining_capacity == i.capacity:
                if largest_instance is None:
                    largest_instance = i
                elif i.capacity > largest_instance.capacity:
                    largest_instance = i
        return largest_instance

    def get_instance_groups_from_task_cache(self, task):
        igs = []
        for pk in task.preferred_instance_groups_cache:
            ig = self.pk_ig_map.get(pk, None)
            if ig:
                igs.append(ig)
            else:
                logger.warn(f"Unknown instance group with pk {pk} for task {task}")
        if len(igs) == 0:
            logger.warn(f"No instance groups in cache exist, defaulting to global instance groups for task {task}")
            return task.global_instance_groups
        return igs


Review comment (performance):
Imagine that you have a job from before the migration. In that case `task.preferred_instance_groups_cache` may be `None` because of the default you set. The _last_ thing we want is an uncaught exception here, so I would advocate for further, aggressive, data sanitization. For another pathway, someone could create a job with `awx-manage shell_plus`, and you don't want to grind to a halt because it didn't take the canonical creation path. In all of these cases, there's likely very little care for the specific IG / instance selection. A job went rogue, and the user just wants you to move it along and let the work happen.

Code after:
                continue
            would_be_remaining = i.remaining_capacity - impact
            # hybrid nodes _always_ control their own tasks
            if add_hybrid_control_cost and i.node_type == 'hybrid':
                would_be_remaining -= settings.AWX_CONTROL_NODE_TASK_IMPACT
            if would_be_remaining >= 0 and (instance_most_capacity is None or would_be_remaining > most_remaining_capacity):
                instance_most_capacity = i
                most_remaining_capacity = would_be_remaining
        return instance_most_capacity

    def find_largest_idle_instance(self, instance_group_name, capacity_type='execution'):
        largest_instance = None
        instances = self.instance_groups[instance_group_name]['instances']
        for i in instances:
            if i.node_type not in (capacity_type, 'hybrid'):
                continue
            if (hasattr(i, 'jobs_running') and i.jobs_running == 0) or i.remaining_capacity == i.capacity:
                if largest_instance is None:
                    largest_instance = i
                elif i.capacity > largest_instance.capacity:
                    largest_instance = i
        return largest_instance

    def get_instance_groups_from_task_cache(self, task):
        igs = []
        if task.preferred_instance_groups_cache:
            for pk in task.preferred_instance_groups_cache:
                ig = self.pk_ig_map.get(pk, None)
                if ig:
                    igs.append(ig)
                else:
                    logger.warn(f"Unknown instance group with pk {pk} for task {task}")
        if len(igs) == 0:
            logger.warn(f"No instance groups in cache exist, defaulting to global instance groups for task {task}")
            return task.global_instance_groups
        return igs