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


##########
airflow-core/tests/unit/jobs/test_scheduler_job.py:
##########
@@ -4072,6 +4092,99 @@ def 
test_build_schedulable_tis_query_excludes_only_starved_dag_run(self, dag_mak
 
         assert [ti.run_id for ti in session.scalars(query)] == 
[second_run.run_id]
 
+    @mock.patch.object(SchedulerJobRunner, "_build_schedulable_tis_query", 
autospec=True)
+    def 
test_select_task_instances_to_queue_excludes_locked_rows_no_longer_scheduled(
+        self, mock_build_query, dag_maker, mock_executors, session, caplog
+    ):
+        with dag_maker(dag_id="locked_rows_state", session=session):
+            EmptyOperator(task_id="scheduled_in_bulk")
+            EmptyOperator(task_id="marked_success")
+        dag_run = dag_maker.create_dagrun(run_type=DagRunType.SCHEDULED, 
session=session)
+        tis = {ti.task_id: ti for ti in 
dag_run.get_task_instances(session=session)}
+        tis["marked_success"].state = State.SCHEDULED
+        session.flush()
+        # Update the rows but not the TIs in the session, so the session keeps 
no state for
+        # "scheduled_in_bulk" and SCHEDULED for "marked_success".
+        for task_id, state in (("scheduled_in_bulk", State.SCHEDULED), 
("marked_success", State.SUCCESS)):
+            session.execute(
+                update(TaskInstance)
+                .where(TaskInstance.id == tis[task_id].id)
+                .values(state=state)
+                .execution_options(synchronize_session=False)
+            )
+        # Returns both rows, as a locking read does for a row the ranking 
subquery saw as SCHEDULED in the
+        # statement's snapshot and that was marked success before the lock. 
SQLite cannot reproduce that.
+        mock_build_query.return_value = 
select(TaskInstance).where(TaskInstance.dag_id == "locked_rows_state")
+        self.job_runner = SchedulerJobRunner(job=Job())
+
+        with caplog.at_level(logging.DEBUG, 
logger="airflow.jobs.scheduler_job_runner"):
+            queued_tis = self.job_runner._select_task_instances_to_queue(
+                32, make_pool_stats(), set(), session=session
+            )
+
+        assert [ti.task_id for ti in queued_tis] == ["scheduled_in_bulk"]
+        assert {"event": "Skipped locked task instances that are no longer 
SCHEDULED", "count": 1} in caplog
+
+    @pytest.mark.backend("postgres", "mysql")
+    @mock.patch(
+        "airflow.jobs.scheduler_job_runner.with_row_locks",
+        autospec=True,
+        side_effect=lambda query, **kwargs: with_row_locks(query, **{**kwargs, 
"skip_locked": False}),

Review Comment:
   Done



-- 
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