-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Fix backfill completion race #68729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix backfill completion race #68729
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,7 +87,7 @@ | |
| TaskOutletAssetReference, | ||
| ) | ||
| from airflow.models.asset_state_store import AssetStateStoreModel | ||
| from airflow.models.backfill import Backfill, BackfillDagRun | ||
| from airflow.models.backfill import Backfill, BackfillDagRun, BackfillDagRunExceptionReason | ||
| from airflow.models.callback import Callback, CallbackKey, CallbackType, ExecutorCallback | ||
| from airflow.models.connection_test import ( | ||
| ACTIVE_STATES as CONNECTION_TEST_ACTIVE_STATES, | ||
|
|
@@ -2347,7 +2347,6 @@ def _create_dagruns_for_dags(self, guard: CommitProhibitorGuard, session: Sessio | |
| def _mark_backfills_complete(self, *, session: Session = NEW_SESSION) -> None: | ||
| """Mark completed backfills as completed.""" | ||
| self.log.debug("checking for completed backfills.") | ||
| unfinished_states = (DagRunState.RUNNING, DagRunState.QUEUED) | ||
| now = timezone.utcnow() | ||
| # todo: AIP-78 simplify this function to an update statement | ||
| initializing_cutoff = now - timedelta(minutes=2) | ||
|
|
@@ -2362,8 +2361,38 @@ def _mark_backfills_complete(self, *, session: Session = NEW_SESSION) -> None: | |
| Backfill.created_at < initializing_cutoff, | ||
| ), | ||
| ~exists( | ||
| select(DagRun.id).where( | ||
| and_(DagRun.backfill_id == Backfill.id, DagRun.state.in_(unfinished_states)) | ||
| select(BackfillDagRun.id) | ||
| .join(DagRun, DagRun.id == BackfillDagRun.dag_run_id, isouter=True) | ||
| .where( | ||
| BackfillDagRun.backfill_id == Backfill.id, | ||
| or_( | ||
| and_( | ||
| BackfillDagRun.dag_run_id.is_(None), | ||
| BackfillDagRun.exception_reason.is_(None), | ||
| ), | ||
| DagRun.state.in_(State.unfinished_dr_states), | ||
| and_( | ||
| BackfillDagRun.dag_run_id.is_(None), | ||
| BackfillDagRun.exception_reason == BackfillDagRunExceptionReason.IN_FLIGHT, | ||
| exists( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a correlated Could you paste the compiled SQL ( Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting |
||
| select(DagRun.id).where( | ||
| DagRun.dag_id == Backfill.dag_id, | ||
| DagRun.state.in_(State.unfinished_dr_states), | ||
| or_( | ||
| and_( | ||
| BackfillDagRun.logical_date.is_not(None), | ||
| DagRun.logical_date == BackfillDagRun.logical_date, | ||
| ), | ||
| and_( | ||
| BackfillDagRun.logical_date.is_(None), | ||
| BackfillDagRun.partition_key.is_not(None), | ||
| DagRun.partition_key == BackfillDagRun.partition_key, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The outer correlation is fine — The inner one I'm less sure about: it scans Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting |
||
| ), | ||
| ), | ||
| ) | ||
| ), | ||
| ), | ||
| ), | ||
| ) | ||
| ), | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test covers the
logical_datebranch well, including the transition from "stays active" to "completes once the run succeeds". Thepartition_keybranch —logical_date IS NULLwith a non-nullpartition_key— has no coverage, and it's the harder of the two to reason about.Since the partitioned path is the reason that branch exists, a companion test there would be worth adding.
Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting