Skip to content

Commit 1193e5e

Browse files
8silvergunnanaones
andauthored
Fix scheduler MySQL task instance index hint (#66785)
Co-authored-by: nanaones <nara03050@gmail.com>
1 parent da03584 commit 1193e5e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

airflow-core/src/airflow/jobs/scheduler_job_runner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,6 @@ def _executable_task_instances_to_queued(self, max_tis: int, session: Session) -
632632
# Select only rows where row_number <= max_active_tasks.
633633
query = (
634634
select(TI)
635-
.with_hint(TI, "USE INDEX (ti_state)", dialect_name="mysql")
636635
.select_from(ranked_query)
637636
.join(
638637
TI,

airflow-core/tests/unit/jobs/test_scheduler_job.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import pytest
3838
import time_machine
3939
from sqlalchemy import delete, func, select, update
40+
from sqlalchemy.dialects import mysql
4041
from sqlalchemy.orm import joinedload
4142

4243
from airflow import settings
@@ -1248,6 +1249,34 @@ def test_find_executable_task_instances_backfill(self, dag_maker):
12481249
assert {x.key for x in queued_tis} == {ti_non_backfill.key, ti_backfill.key}
12491250
session.rollback()
12501251

1252+
def test_find_executable_task_instances_mysql_hint_only_applies_to_inner_query(self, dag_maker, session):
1253+
dag_id = "SchedulerJobTest.test_find_executable_task_instances_mysql_hint_only_applies_to_inner_query"
1254+
task_id = "dummy"
1255+
with dag_maker(dag_id=dag_id, max_active_tasks=16):
1256+
task = EmptyOperator(task_id=task_id)
1257+
1258+
scheduler_job = Job()
1259+
self.job_runner = SchedulerJobRunner(job=scheduler_job)
1260+
1261+
dag_run = dag_maker.create_dagrun(run_type=DagRunType.SCHEDULED)
1262+
ti = dag_run.get_task_instance(task.task_id)
1263+
ti.state = State.SCHEDULED
1264+
session.merge(ti)
1265+
session.flush()
1266+
1267+
captured_queries = []
1268+
1269+
def capture_locked_query(query, **kwargs):
1270+
captured_queries.append(query)
1271+
return query
1272+
1273+
with mock.patch("airflow.jobs.scheduler_job_runner.with_row_locks", side_effect=capture_locked_query):
1274+
queued_tis = self.job_runner._executable_task_instances_to_queued(max_tis=32, session=session)
1275+
1276+
assert {queued_ti.key for queued_ti in queued_tis} == {ti.key}
1277+
compiled_query = str(captured_queries[0].compile(dialect=mysql.dialect()))
1278+
assert compiled_query.count("USE INDEX (ti_state)") == 1
1279+
12511280
def test_find_executable_task_instances_pool(self, dag_maker):
12521281
dag_id = "SchedulerJobTest.test_find_executable_task_instances_pool"
12531282
task_id_1 = "dummy"

0 commit comments

Comments
 (0)