vincbeck commented on code in PR #77546:
URL: https://github.com/apache/airflow/pull/77546#discussion_r4701188609


##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1411,25 +1452,162 @@ def _critical_section_enqueue_task_instances(self, 
session: Session) -> int:
             max_tis = min(self.job.max_tis_per_query, self._parallelism - 
num_occupied_slots)
         if max_tis <= 0:
             self.log.debug("max_tis query size is less than or equal to zero. 
No query will be performed!")
-            return 0
+            return []
 
         pools, max_tis, starved_pools = self._acquire_pool_capacity(max_tis, 
session=session)
         if max_tis == 0:
-            return 0
-        queued_tis = self._select_task_instances_to_queue(max_tis, pools, 
starved_pools, session=session)
+            return []
+        return self._select_task_instances_to_queue(max_tis, pools, 
starved_pools, session=session)
 
+    def _enqueue_workloads(self, queued_tis: list[TI], *, session: Session) -> 
int:
+        """
+        Build the workloads of TaskInstances already committed as QUEUED and 
enqueue them in their executors.
+
+        :return: How many workloads were enqueued.
+        """
+        # Committing the QUEUED state released the row locks the selection 
took, so another transaction may
+        # have changed or deleted some of these task instances since. Building 
the workloads does not lock
+        # them again: it only builds workloads for the ones still queued by 
this scheduler.
+        still_queued_ids = set(
+            session.scalars(
+                select(TI.id).where(
+                    TI.id.in_([ti.id for ti in queued_tis]),
+                    TI.state == TaskInstanceState.QUEUED,
+                    TI.queued_by_job_id == self.job.id,
+                )
+            )
+        )
+        if len(still_queued_ids) < len(queued_tis):
+            skipped = [ti for ti in queued_tis if ti.id not in 
still_queued_ids]
+            self.log.info(
+                "Not enqueuing %s of %s task instances changed or deleted 
after they were queued",
+                len(skipped),
+                len(queued_tis),
+            )
+            self.log.debug("Task instances changed or deleted after they were 
queued: %s", skipped)
+            queued_tis = [ti for ti in queued_tis if ti.id in still_queued_ids]
         # Sort queued TIs to their respective executor
         executor_to_queued_tis = self._executor_to_workloads(queued_tis, 
session)
+        num_enqueued = 0
         for executor, queued_tis_per_executor in 
executor_to_queued_tis.items():
             self.log.info(
                 "Trying to enqueue tasks: %s for executor: %s",
                 queued_tis_per_executor,
                 executor,
             )
 
-            
self._enqueue_task_instances_with_queued_state(queued_tis_per_executor, 
executor, session=session)
+            num_enqueued += self._enqueue_task_instances_with_queued_state(
+                queued_tis_per_executor, executor, session=session
+            )
+        return num_enqueued
 
-        return len(queued_tis)
+    def _enqueue_workloads_or_reset(

Review Comment:
   Agree, the PR is already big, let's do it as follow-up, easier to review



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to