Repository navigation
Stop scheduler stalling on dag rows locked by the Dag processor - #76987
Open
Donniedarko45 wants to merge 1 commit into
Open
Donniedarko45 wants to merge 1 commit into
Donniedarko45 wants to merge 1 commit into
Conversation
Since 3.2 the scheduler writes dag.exceeds_max_non_backfill when a run starts or finishes. The Dag processor holds FOR NO KEY UPDATE on every dag row of a file for its whole parse transaction, which can last minutes for a file that generates many Dags. A scheduler write to one of those rows waits for the parse to commit, and the scheduling loop stalls with it. The scheduler no longer waits for that lock. If the row is held elsewhere it skips the write and recomputes the flag from fresh run counts at the start of the next loop, before Dag runs are created. While the row stays locked, dags_needing_dagruns skips it anyway, so a deferred write cannot lead to an extra Dag run.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes: #70614
Since 3.2 (#60006) the scheduler writes
dag.exceeds_max_non_backfillwhen a run starts or finishes. The Dag processor holdsFOR NO KEY UPDATEon everydagrow of a file for its whole parse transaction (DagModelOperation.find_orm_dags), which can last minutes for a file that generates many Dags. A scheduler write to one of those rows waits for the parse to commit, and the scheduling loop stalls with it. This is theLock/transactionidwait in the issue.Reproduced on Postgres 16 against
main, using the realfind_orm_dagsand_set_exceeds_max_active_runsin two sessions, with the parse transaction held for 8s:UPDATEsent, 0.00sLock/transactionid)UPDATEsent, 0.00sThe "flag unchanged" column is also why skipping redundant writes (#70712) can't fix this: SQLAlchemy already sends no
UPDATEwhen the value doesn't change, so the blocking write only happens when the flag really flips.Change
_set_exceeds_max_active_runsno longer waits for the row lock. When the flag needs to change, it first selects the row withSKIP LOCKED.dags_needing_dagruns, so that path behaves the same.Why a deferred write is safe
dags_needing_dagrunsskips it (it selects withSKIP LOCKEDtoo), so a flag that should have becomeTruecan't lead to an extra Dag run.Falsedelays run creation for that Dag by at most one scheduler loop.USE_ROW_LEVEL_LOCKINGoff) skip the lock check, so behaviour there is unchanged. MySQL 8 getsFOR UPDATE SKIP LOCKED.I considered narrowing the Dag processor's lock scope instead. It is the bigger lever, but it changes the parse transaction for every writer of
dag. This change only touches the scheduler side and doesn't stop that being done later.Tests
test_set_exceeds_max_active_runs_does_not_wait_on_dag_row_locked_elsewhere(Postgres/MySQL) holds the row the way the Dag processor does and asserts the scheduler returns without waiting. Without the change it fails withTimeoutError._do_schedulingrunning the retry before Dag run creation.test_scheduler_job.pyon SQLite and on Postgres 16, compared with the same command on unchangedmain: no new failures on either. The run without breeze has the same pre-existing environment failures on both.mypy-airflow-core.check-template-fields-validneeds breeze and was not run locally.)