Skip to content

Commit 1b60429

Browse files
committed
Reduce reschedule startup queries in execution API
The run endpoint was doing two TaskReschedule round trips to compute values that can be fetched together. Collapsing them into one aggregate query keeps the rescheduled-sensor context fix intact while trimming overhead on task startup, and the test now proves we return the earliest reschedule timestamp instead of depending on insertion order.
1 parent fea9229 commit 1b60429

2 files changed

Lines changed: 12 additions & 17 deletions

File tree

airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,12 @@ def ti_run(
290290
xcom_query = xcom_query.where(XComModel.map_index == map_index)
291291

292292
xcom_keys = list(session.scalars(xcom_query))
293-
task_reschedule_count = (
294-
session.scalar(
295-
select(func.count(TaskReschedule.id)).where(TaskReschedule.ti_id == task_instance_id)
296-
)
297-
or 0
298-
)
299-
first_task_reschedule_start_date = None
300-
if task_reschedule_count > 0:
301-
first_task_reschedule_start_date = session.scalar(
302-
select(func.min(TaskReschedule.start_date)).where(TaskReschedule.ti_id == task_instance_id)
303-
)
293+
task_reschedule_count, first_task_reschedule_start_date = session.execute(
294+
select(
295+
func.count(TaskReschedule.id),
296+
func.min(TaskReschedule.start_date),
297+
).where(TaskReschedule.ti_id == task_instance_id)
298+
).one()
304299

305300
dr.team_name = get_team_name_for_ti(task_instance_id, session)
306301

airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,18 @@ def test_ti_run_state_includes_first_task_reschedule_start_date(
344344
)
345345
session.add_all(
346346
[
347-
TaskReschedule(
348-
ti_id=ti.id,
349-
start_date=first_reschedule_start_date,
350-
end_date=timezone.datetime(2024, 9, 30, 10, 1),
351-
reschedule_date=timezone.datetime(2024, 9, 30, 10, 2),
352-
),
353347
TaskReschedule(
354348
ti_id=ti.id,
355349
start_date=second_reschedule_start_date,
356350
end_date=timezone.datetime(2024, 9, 30, 11, 1),
357351
reschedule_date=timezone.datetime(2024, 9, 30, 11, 2),
358352
),
353+
TaskReschedule(
354+
ti_id=ti.id,
355+
start_date=first_reschedule_start_date,
356+
end_date=timezone.datetime(2024, 9, 30, 10, 1),
357+
reschedule_date=timezone.datetime(2024, 9, 30, 10, 2),
358+
),
359359
]
360360
)
361361
session.commit()

0 commit comments

Comments
 (0)