pierrejeambrun commented on code in PR #67900:
URL: https://github.com/apache/airflow/pull/67900#discussion_r3506533683
##########
airflow-core/src/airflow/models/backfill.py:
##########
@@ -690,24 +694,63 @@ def _create_backfill(
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,
- )
+ try:
+ 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 OperationalError as e:
+ if is_lock_not_available_error(e):
+ # Lock error: clean up the orphan so the user can retry. The
+ # helper is best-effort; if it fails the original error still
+ # surfaces and the route returns 503.
+ _cleanup_partial_backfill(br, session)
+ raise
return br
+def _cleanup_partial_backfill(br: Backfill, session: Session) -> None:
+ """
+ Remove a partially-created backfill so the user can retry cleanly.
+
+ Called after a database-lock ``OperationalError`` inside
+ ``_create_backfill`` while the session is in a deactivated state
+ (SQLAlchemy deactivates a session after a flush failure until
+ ``rollback()`` is called). Rolls back the failed transaction first,
+ then deletes the ``Backfill`` row plus any partial ``BackfillDagRun``
+ and ``dag_run`` rows. Best-effort: a failed cleanup just means the
+ orphan stays in the DB; the caller still re-raises the original
+ ``OperationalError`` and the route returns 503, so the worst case is
+ no worse than before.
+ """
+ from airflow.models.dagrun import DagRun
+
+ try:
+ # The session is in a deactivated state because the caller just
+ # raised from a failed transaction. SQLAlchemy requires an
+ # explicit rollback before any further operation; without this
+ # the first session.execute() would raise InvalidRequestError and
+ # the cleanup would be a silent no-op.
Review Comment:
Make those less verbose
--
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]