feat(dag_command.py): change to use bulk clear#68280
Draft
FrankYang0529 wants to merge 2 commits into
Draft
Conversation
Signed-off-by: PoAn Yang <payang@apache.org>
Lee-W
reviewed
Jun 9, 2026
| session: Session, | ||
| ) -> int: | ||
| """Clear task instances for the given run_ids in chunks instead of one transaction per run.""" | ||
| from airflow.utils.state import TaskInstanceState |
Member
Author
There was a problem hiding this comment.
Sorry for that. It's not a valid exception. Move import to top of file.
Line 137 in 6132261
| cleared = 0 | ||
| for chunk_start in range(0, len(run_ids), _RUN_CHUNK_SIZE): | ||
| chunk_run_ids = run_ids[chunk_start : chunk_start + _RUN_CHUNK_SIZE] | ||
| ti_query = select(TaskInstance).where(TaskInstance.run_id.in_(chunk_run_ids)) |
Member
Author
There was a problem hiding this comment.
Good catch. Updated it.
Signed-off-by: PoAn Yang <payang@apache.org>
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: #67484
Why
airflow dags clear(#66004) loopsdag.clear(run_id=...)once per matched run, issuing a separate SELECT + flush for each. A wide--partition-date-*window or broad--partition-keytherefore costs N independent transactions.How
Replace the per-run loop with a
_bulk_clear_runshelper:run_idsin chunks of_RUN_CHUNK_SIZE (500), fetching all matching TIs for each chunk with a single query.clear_task_instancesin one call.Two new tests:
test_clears_multiple_runs_in_one_batch: asserts that 3 runs fitting in one chunk result in exactly oneclear_task_instancescall.test_chunks_on_run_boundaries_clears_each_run_once: patches_RUN_CHUNK_SIZEto 2. 3 runs span 2 chunks.Was generative AI tooling used to co-author this PR?
Claude Code with Claude Opus 4.8
{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.