Flush scheduled DagRun creation state promptly - #71331
Open
fat-catTW wants to merge 1 commit into
Open
Conversation
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.
Why
SchedulerJobRunner._create_dag_runs()had a TODO asking whether the scheduler should callsession.flush()orsession.expunge_all()while creating scheduled DagRuns.The concern behind the TODO is that the scheduler may iterate over multiple Dags, create scheduled DagRuns, update
DagModelscheduling fields, and create task instance rows in the same SQLAlchemy session. For larger Dags or larger batches of Dags, leaving ORM state pending until the outer transaction boundary can make it harder to reason about session growth and memory behavior.This PR resolves that TODO by flushing after each successful scheduled DagRun creation and related
DagModelscheduling-state update.Why
flush()Instead ofexpunge_all()The narrower operation is enough for the state observed in this path.
session.flush()writes pending ORM changes to the database transaction while keeping objects attached to the session. That directly addresses the observed pending/dirty scheduler state after creating a scheduled DagRun and updating the correspondingDagModel.session.expunge_all()does something broader: it detaches every ORM object from the session. That can reduce identity-map retention, but it also carries more behavioral risk because later scheduler code may still expect ORM objects to be attached, refreshable, or tracked by the session.To decide whether
expunge_all()was needed, this path was checked for retained ORM objects after DagRun creation. If creating larger Dags left one trackedDagRunorTaskInstanceobject per created row,flush()would not address that memory growth because it does not clear the session identity map.A local pressure check with 20 Dags and 500 tasks per Dag created 10,000 TaskInstance rows. After
_create_dag_runs()returned, the observed session state was:Was generative AI tooling used to co-author this PR?
[X]Yes (please specify the tool below)Generated-by: [Codex] following the guidelines
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.