Abdulrehman-PIAIC80387 commented on code in PR #68705:
URL: https://github.com/apache/airflow/pull/68705#discussion_r3758751455
##########
airflow-core/src/airflow/models/backfill.py:
##########
@@ -677,34 +677,41 @@ def _create_backfill(
)
session.add(br)
session.commit()
+ backfill_id = br.id
- session.scalars(select(DagModel).where(DagModel.dag_id ==
dag_id)).one()
-
- dagrun_info_list = _get_info_list(
- from_date=from_date,
- to_date=to_date,
- reverse=reverse,
- dag=dag,
- )
- if not dagrun_info_list:
- raise RuntimeError(f"No runs to create for Dag {dag_id}")
+ try:
+ session.scalars(select(DagModel).where(DagModel.dag_id ==
dag_id)).one()
- first_info = dagrun_info_list[0]
- if first_info.partition_key:
- _create_runs_partitioned(
- br=br,
+ dagrun_info_list = _get_info_list(
+ from_date=from_date,
+ to_date=to_date,
+ reverse=reverse,
dag=dag,
- dagrun_info_list=dagrun_info_list,
- session=session,
- )
- else:
- _create_runs_non_partitioned(
- br=br,
- dag=dag,
- dagrun_info_list=dagrun_info_list,
- run_on_latest_version=run_on_latest_version,
- session=session,
)
+ if not dagrun_info_list:
+ raise RuntimeError(f"No runs to create for Dag {dag_id}")
+
+ first_info = dagrun_info_list[0]
+ if first_info.partition_key:
+ _create_runs_partitioned(
+ br=br,
+ dag=dag,
+ dagrun_info_list=dagrun_info_list,
+ session=session,
+ )
+ else:
+ _create_runs_non_partitioned(
+ br=br,
+ dag=dag,
+ dagrun_info_list=dagrun_info_list,
+ run_on_latest_version=run_on_latest_version,
+ session=session,
+ )
+ except Exception:
+ session.rollback()
+ session.execute(sa.delete(Backfill).where(Backfill.id ==
backfill_id))
Review Comment:
Agreed, and done — `session.flush()` populates `backfill.id`,
`create_session` rolls back on exception, and the whole `try`/`except` plus the
manual delete are gone.
While rebasing onto latest `main` I found this now also supersedes the
best-effort `_cleanup_partial_backfill` helper added in #67900: with a single
transaction there is no partial state to compensate for, so the helper is
removed too (net −85 lines). The 503 mapping at the route boundary is
untouched. Its three helper-level unit tests are replaced by one behavioural
test that drives a lock error through the endpoint on the third run — after two
runs already exist in the transaction — and asserts the `Backfill`, `DagRun`,
`TaskInstance` and `BackfillDagRun` rows are all gone.
One thing worth flagging: the comment on the early commit claimed it
prevented duplicate active backfills for a dag. It didn't really — two requests
can both read `num_active == 0` before either commits — but flushing does widen
that window. Closing it properly needs a DB-level guard (partial unique index
on `dag_id` where `completed_at IS NULL`, or a row lock), which I've left out
of this PR to keep it focused. Happy to do it as a follow-up if you think it's
worth it.
---
Drafted-by: Claude Code (Fable 5) (no human review before posting)
--
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]