seunggabi commented on PR #64109:
URL: https://github.com/apache/airflow/pull/64109#issuecomment-7171573070

   @Nataneljpwd @kaxil @eladkal This PR still leaves a deadlock in place, and I 
think it needs fixing before merge.
   
   The window ranks runs inside a dag by `last_scheduling_decision NULLS FIRST` 
**before** `run_after`:
   
   ```python
   order_by=[
       nulls_first(BackfillDagRun.sort_ordinal, session=session),
       nulls_first(cls.last_scheduling_decision, session=session),  # <- 
per-dag rank ignores date order
       DagRun.run_after,
   ],
   ```
   
   So whenever the earliest queued run of a dag already has a 
`last_scheduling_decision` (it was cleared, or marked queued again; see #77603) 
and a later run doesn't, the later run gets `rn = 1` and is the one that 
starts. With `depends_on_past=True` + `max_active_runs=1`, that's a deadlock: 
the later run is `running` and waits on the earlier one, and the earlier one 
stays `queued` forever because the only slot is taken. We hit this in 
production.
   
   **Repro against this PR's head (`3af7b2af7a`):** add this test to 
`test_scheduler_job.py`:
   
   ```python
   def test_start_queued_dagruns_starts_earliest_run_of_dag_first(self, 
dag_maker, session):
       with dag_maker("test_dag", max_active_runs=1, session=session):
           EmptyOperator(task_id="mytask", depends_on_past=True)
       for i in range(3):
           dag_maker.create_dagrun(
               run_id=f"dagrun_{i}",
               run_type=DagRunType.SCHEDULED,
               state=State.QUEUED,
               logical_date=DEFAULT_DATE + timedelta(days=i),
               session=session,
           )
       session.execute(
           update(DagRun).where(DagRun.run_id == 
"dagrun_0").values(last_scheduling_decision=timezone.utcnow())
       )
       session.flush()
   
       self.job_runner = SchedulerJobRunner(job=Job(), 
executors=[MockExecutor(do_update=False)])
       self.job_runner._start_queued_dagruns(session)
       session.flush()
   
       states = dict(session.execute(select(DagRun.run_id, DagRun.state)).all())
       assert states == {
           "dagrun_0": DagRunState.RUNNING,
           "dagrun_1": DagRunState.QUEUED,
           "dagrun_2": DagRunState.QUEUED,
       }
   ```
   
   Result:
   
   ```
   FAILED ...::test_start_queued_dagruns_starts_earliest_run_of_dag_first
   E      -  'dagrun_0': 'queued',     +  'dagrun_0': 'running',
   E      -  'dagrun_1': 'running',    +  'dagrun_1': 'queued',
   ```
   
   **Fix:** `last_scheduling_decision` is a fairness key *across* dags. It 
should stay in the outer `ORDER BY` and come out of the per-dag window. Inside 
a dag, the order should only be the order the runs are meant to start in:
   
   ```python
   func.row_number().over(
       partition_by=(DagRun.dag_id, DagRun.backfill_id),
       order_by=(BackfillDagRun.sort_ordinal, DagRun.run_after, DagRun.id),
   )
   ```
   
   - `sort_ordinal` keeps reverse backfills correct (@kaxil's point about 
`sort_ordinal=1` being the latest date).
   - `run_after` is never NULL, unlike `logical_date` for partitioned 
backfills, and `id` breaks ties so the ranking is deterministic.
   
   I ran this ordering on a branch off `main` (SQLite): the test above passes, 
and so do the existing `_start_queued_dagruns`, backfill and max_active_runs 
scheduler tests. I can push it as a suggestion or a follow-up commit if that 
helps get this merged.
   


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