kaxil commented on code in PR #72054:
URL: https://github.com/apache/airflow/pull/72054#discussion_r4743608741


##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -946,8 +946,14 @@ def get_queued_dag_runs_to_set_running(cls, session: 
Session) -> ScalarResult[Da
                 # capacity for 1.  this could be improved.
                 coalesce(running_drs.c.num_running, text("0"))
                 < coalesce(Backfill.max_active_runs, DagModel.max_active_runs),
-                # don't set paused dag runs as running
-                not_(coalesce(cast("ColumnElement[bool]", Backfill.is_paused), 
False)),
+                # don't set paused dag runs as running, but a completed 
backfill's
+                # is_paused flag is stale: a run cleared after the backfill was
+                # cancelled (cancel sets is_paused=True and completed_at) must 
still
+                # be schedulable, otherwise it is stuck in queued forever.
+                or_(
+                    Backfill.completed_at.isnot(None),

Review Comment:
   Fine to leave the pre-upgrade orphans out, they need a cancel inside the 
creation window (or an older API server next to a new scheduler during a 
rolling upgrade). Could you note them in the PR description, and update it for 
the new 409 on cancel and the renamed test? It still describes 
`test_backfill_runs_started_when_backfill_completed_despite_paused_flag`.



##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/backfills.py:
##########
@@ -247,6 +247,12 @@ def cancel_backfill(backfill_id: NonNegativeInt, session: 
SessionDep) -> Backfil
         raise HTTPException(status.HTTP_404_NOT_FOUND, BACKFILL_NOT_FOUND)
     if b.completed_at is not None:
         raise HTTPException(status.HTTP_409_CONFLICT, "Backfill is already 
completed.")
+    # The backfill row is committed before its Dag runs and associations are 
created. Cancelling
+    # in that window would fail no runs, and the runs would later start under 
a completed backfill.
+    if not session.scalar(select(exists().where(BackfillDagRun.backfill_id == 
b.id))):

Review Comment:
   Hmm, one case this guard doesn't cover is a creation that runs past 2 
minutes. `_mark_backfills_complete` can't see the uncommitted `BackfillDagRun` 
rows, so it marks the backfill complete mid-creation and the user can't cancel 
at all (and with the new `completed_at` check in the scheduler query, a pause 
made during creation gets ignored too). Probably a separate issue for the sweep 
though, wdyt?



-- 
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]

Reply via email to