prabhusneha commented on code in PR #45062:
URL: https://github.com/apache/airflow/pull/45062#discussion_r1905948459
##########
airflow/models/backfill.py:
##########
@@ -158,72 +151,125 @@ def validate_sort_ordinal(self, key, val):
def _create_backfill_dag_run(
*,
dag,
- info,
- reprocess_behavior: ReprocessBehavior,
+ dagrun_info_list,
+ reprocess_behavior: ReprocessBehavior | None = None,
backfill_id,
- dag_run_conf,
- backfill_sort_ordinal,
+ dag_run_conf: dict | None,
session,
-):
+ dry_run,
+) -> list[datetime]:
from airflow.models import DagRun
- with session.begin_nested() as nested:
- dr = session.scalar(
- with_row_locks(
- select(DagRun)
- .where(DagRun.logical_date == info.logical_date)
- .order_by(nulls_first(desc(DagRun.start_date),
session=session))
- .limit(1),
- session=session,
+ backfill_sort_ordinal = 0
+ logical_dates = []
+ dagrun_infos = list(dagrun_info_list)
+
+ if reprocess_behavior is None:
+ reprocess_behavior = ReprocessBehavior.NONE
+ if dag_run_conf is None:
+ dag_run_conf = {}
+
+ dag_run_ranked = (
+ select(
+ DagRun.logical_date,
+ DagRun.start_date,
+ DagRun.dag_id,
+ func.row_number()
+ .over(
+ partition_by=DagRun.logical_date,
+ order_by=(case([(DagRun.start_date.is_(None), 0)], else_=1),
DagRun.start_date.desc()),
)
+ .label("row_number"),
)
- if dr:
- non_create_reason = None
- if dr.state not in (DagRunState.SUCCESS, DagRunState.FAILED):
- non_create_reason = BackfillDagRunExceptionReason.IN_FLIGHT
- elif reprocess_behavior is ReprocessBehavior.NONE:
- non_create_reason =
BackfillDagRunExceptionReason.ALREADY_EXISTS
- elif reprocess_behavior is ReprocessBehavior.FAILED:
- if dr.state != DagRunState.FAILED:
+ .where(DagRun.dag_id == dag.dag_id)
+ .where(DagRun.logical_date.in_([info.logical_date for info in
dagrun_infos]))
+ .subquery()
+ )
+
+ existing_dag_runs = {
+ dr.logical_date: dr
+ for dr in session.scalars(
+ select(DagRun)
+ .join(
+ dag_run_ranked,
+ (DagRun.logical_date == dag_run_ranked.c.logical_date)
+ & (
+ (DagRun.start_date == dag_run_ranked.c.start_date)
+ | ((DagRun.start_date.is_(None)) &
(dag_run_ranked.c.start_date.is_(None)))
+ )
+ & (DagRun.dag_id == dag_run_ranked.c.dag_id),
+ )
+ .where(dag_run_ranked.c.row_number == 1)
+ ).all()
+ }
+
+ print(existing_dag_runs)
+
+ for info in dagrun_infos:
+ backfill_sort_ordinal += 1
+ dr = existing_dag_runs.get(info.logical_date)
+ non_create_reason = None
+
+ with session.begin_nested() as nested:
+ if dr:
+ if dr.state not in (DagRunState.SUCCESS, DagRunState.FAILED):
+ non_create_reason = BackfillDagRunExceptionReason.IN_FLIGHT
+ elif reprocess_behavior is ReprocessBehavior.NONE:
non_create_reason =
BackfillDagRunExceptionReason.ALREADY_EXISTS
- if non_create_reason:
- # rolling back here restores to start of this nested tran
- # which releases the lock on the latest dag run, since we
- # are not creating a new one
- nested.rollback()
+ elif reprocess_behavior is ReprocessBehavior.FAILED:
+ if dr.state != DagRunState.FAILED:
+ non_create_reason =
BackfillDagRunExceptionReason.ALREADY_EXISTS
+
+ if non_create_reason:
+ if not dry_run:
+ nested.rollback()
+ session.add(
Review Comment:
Removed the comment because the current implementation does not acquire a
lock on dagrun in the nested transaction.
There's a unique constraint on (dagId, run_id) which will cause the query to
fail.
https://github.com/astronomer/airflow/blob/main/airflow/models/dagrun.py#L177
Is there a need for a lock here?
--
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]