vincbeck commented on code in PR #77247:
URL: https://github.com/apache/airflow/pull/77247#discussion_r4631587506
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -847,8 +847,23 @@ def _select_task_instances_to_queue(
timer.start()
try:
- locked_query = with_row_locks(query, of=TI, session=session,
skip_locked=True)
- task_instances_to_examine = session.scalars(locked_query).all()
+ # The ranking subquery reads the statement's snapshot, but a
locking read returns the latest
+ # version of the rows it locks, which can have left SCHEDULED
since. The state is read as a
+ # column so that the check sees the locked row whatever TIs
the session already holds. It is
+ # not checked in the outer WHERE: when the statistics show no
SCHEDULED rows, PostgreSQL
+ # then joins through the ti_state index instead of the primary
key.
+ locked_query = with_row_locks(
Review Comment:
Yes, I considered it. In the scheduler loop both behave the same:
_do_scheduling calls session.expunge_all() right before the critical section,
so the session holds no TIs at this point and every TI, including its pool,
priority_weight and try_number, is built from the locked row anyway. Reading
the state as a column keeps _select_task_instances_to_queue correct whatever
the session already holds, which matters for callers that keep TIs in the
session (the tests do).
I didn't use populate_existing because it refreshes every attribute of the
TIs the session already holds, and of the Dag runs loaded with them, and drops
changes that are not flushed yet (our sessions don't autoflush). Reading one
column changes only what the check needs.
--
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]