Pushkal-Gupta opened a new pull request, #77727:
URL: https://github.com/apache/airflow/pull/77727
`check_trigger_timeouts` runs on a timer in the scheduler's main loop, every
15 seconds by default, and fails every deferred task whose trigger timeout has
passed with one unbounded `UPDATE`:
```python
session.execute(
update(TI)
.where(TI.state == TaskInstanceState.DEFERRED, TI.trigger_timeout <
timezone.utcnow())
.values(state=TaskInstanceState.SCHEDULED,
next_method=TRIGGER_FAIL_REPR, ...)
)
```
Normally that touches a handful of rows. The problem is what happens when it
does not. A triggerer that stays down long enough expires every deferred task
at once, so the next tick takes the whole backlog in a single statement: row
locks held on `task_instance` for all of it, in one transaction, while the
scheduler's main loop waits on the statement to come back. Scheduling for
everything else waits behind it, and concurrent writers to those rows block too.
The method right below it already knows about this.
`check_awaiting_input_timeouts` was written as the HITL counterpart, says in
its own docstring that it mirrors `check_trigger_timeouts`, and bounds itself:
```python
# Bound the batch so a single scheduler tick cannot lock/process an unbounded
# backlog of timed-out tasks (which would block concurrent responses/clears);
# any remaining rows are handled on subsequent ticks.
.limit(100)
```
Same loop, same table, same hazard, and the same tradeoff is already
accepted there. `check_trigger_timeouts` just never got the bound.
This is also the rule the repo sets for itself in `AGENTS.md`: a bulk
`DELETE` or `UPDATE` in the scheduler loop or a `call_regular_interval`
callback has to be batched, and must never be a single unbounded bulk write
against a user-driven table.
## What this changes
The sweep now picks up at most `_TRIGGER_TIMEOUT_UPDATE_CHUNK_SIZE` (100)
timed-out task instances per tick, the same bound as the HITL sweep next door.
Anything left over is taken by the following tick, 15 seconds later.
The ids are selected first and the update is then keyed on them. It cannot
be one statement because MySQL will not let an `UPDATE` select from the table
it is updating, and the two-step is why the `state` and `trigger_timeout`
predicates are repeated in the `UPDATE`: the write stays correct on its own if
a row moves on between the two queries.
`timezone.utcnow()` was being called twice in one statement, once for the
cutoff and once for `scheduled_dttm`. It is now read once into `now` and used
for both, so the cutoff the rows were chosen by is the timestamp they get.
No `session.commit()` between batches, because this takes a `session`
parameter and `airflow-core` functions that do must not commit. That is the
same reason the HITL sweep does one bounded batch per tick rather than looping,
and it is why this follows that shape rather than the loop-and-commit shape in
`db_cleanup`.
No new index. The selective predicate here is `state`, which `ti_state`
already covers.
## Why 100 and not a config setting
It matches the HITL sweep, so the two halves of the same mechanism drain at
the same rate. It is an internal bound on one statement rather than something
an operator should have to reason about, which is the same call the file makes
for `_ADRQ_DELETE_CHUNK_SIZE` and `_ADOPTED_TI_UPDATE_CHUNK_SIZE`.
## Verification
Reverting the source file fails the new test on behaviour, not on setup:
```
assert remaining > 0, "one tick failed the whole backlog instead of a
bounded batch"
E AssertionError: one tick failed the whole backlog instead of a bounded
batch
E assert 0 > 0
```
The test defers 101 task instances past their timeout, which has to be more
than the bound for the batching to be visible at all. One tick has to leave
some behind and has to make progress, then further ticks drain the rest. It
patches nothing, so it would still hold if the bound were retuned.
The two tests already covering this method pass unchanged, including
`test_retry_on_db_error_when_update_timeout_triggers`, which exercises the
`run_with_db_retries` path the new query sits inside.
`test_scheduler_job.py` runs 662 passed, 20 skipped, 4 failed. Those 4 are
`test_task_instances_changed_after_they_are_queued_are_not_enqueued`, both
parameters of
`test_task_instances_deleted_while_their_workloads_are_built_are_not_enqueued`,
and `test_dag_deleted_while_workloads_are_built_with_a_pinned_bundle_version`.
All 4 fail the same way on a clean tree with this change fully reverted, so
they are inherited from main and have nothing to do with this. Everything else
passes.
`ruff format`, `ruff check`, the prek hooks for both files, and
`mypy-airflow-core` all pass. No lockfile change.
## Left alone
`_remove_unreferenced_triggers` has the same shape, an unbounded `DELETE` on
`trigger` on the parsing cleanup timer, and wants the same treatment. It is a
separate table and a separate statement, so it is not in here.
`adopt_or_reset_orphaned_tasks` also writes without a bound, but only to
`job`, which has a row per scheduler and executor rather than per task, and it
already takes row locks with `skip_locked` for the task instance half. Leaving
it.
No newsfragment: this is a bug fix, with no change to what an operator
configures or sees.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes - Claude Code (Opus 5)
--
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]