Repository navigation
Description
Apache Airflow version
2.10.5
If "Other Airflow 2 version" selected, which one?
No response
What happened?
Two DAGs each receive a large batch of DAG Runs. The number of runs for each DAG exceeds max_dagruns_per_loop_to_schedule. Each DAG run is very short, shorter than the heartrate of this Airflow deployment. Both DAGs have a max_active_runs that is far less than dagruns_per_loop.
So: max_active_runs < max_dagruns_per_loop_to_schedule < number of queued DAG runs.
Each scheduler loop, there are a very small number of DAG Run "slots" for the first DAG, so the check coalesce(running_drs.c.num_running, text("0")) < coalesce(Backfill.max_active_runs, DagModel.max_active_runs), does not apply. But then all the DAG runs that are considered are from the first DAG. So Second DAG effectively has to wait for nearly all of First DAG's runs to complete before any of its runs are moved from queued to running.
What you think should happen instead?
I think the "most correct" thing to do is to change the global yes/no for a DAG being included in the check on the basis of max_active_runs to some kind of limit on the number for that DAG that can be included. I can't see a good way to do this in SQL but others may have insight.
Alternatively, because this is predominantly a problem when a single DAG dominates the scheduler's attention, we could add an explicit check to see if the result of the DAG run query contains only the a single DAG, and if so re-run the query with that DAG excluded.
How to reproduce
- Create two DAGs with a single, simple task.
- Set max_active_runs=100
- Set max_dagruns_per_loop_to_schedule=2000
- Start 5000 Runs of the first DAG
- Start 5000 Runs of the second DAG
- Hard to reproduce: keep the heartrate of the scheduler low enough that Runs complete within one scheduler loop.
Operating System
Debian GNU/Linux 12 (bookworm)
Versions of Apache Airflow Providers
No response
Deployment
Astronomer
Deployment details
No response
Anything else?
No response
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
collinmcnulty commentedon Apr 21, 2025
The workaround we're using is to set
max_dagruns_per_loop_to_scheduleto a value greater than the largest number of Runs we'll get for any individual DAG.uranusjr commentedon Apr 21, 2025
The weird thing to me is we seems to have a mechanism for this
airflow/airflow-core/src/airflow/models/dagrun.py
Lines 522 to 539 in e4957ff
This should prioritize runs we have not touched (nulls first), or touched the least recently. So even if a run is not seen in an iteration, runs before it (and thus touched) should be deprioritized until all other runs were also touched, and so on, so each run should get a chance at some point. Maybe the
last_scheduling_decisiontimestamp is not updated correctly?uranusjr commentedon Apr 22, 2025
I tried to reduce this to a reasonable minimal scenario. This is against main since we did not notice any significant logic changes in the area.
1and2both have one single task. Both dags setmax_active_runs=1. The task simply doessleep(5).AIRFLOW__SCHEDULER__MAX_DAGRUNS_PER_LOOP_TO_SCHEDULEto1.1, and then 10 for2, both via API (I used a Python script withrequestsso the ordering is deterministic).After triggering, I can observe the scheduler actually alternates between the two dags. It picks up a run from the first dag, and then a run from the second, the first, the second, and so on. Interetingly, I did not see any logs on
max_active_runsat all; the scheduler seems to just do it out of the box.So I think we’re missing something. Not sure what…
DAG code
Triggering script
scheduler-output.txt
eladkal commentedon Apr 28, 2025
It seems that starvation can happen. Also reported in #45636
Given 2 pools
default_poolandspecial_poolwith 2 DAGs:The scheduler1 dag creates many dagruns as it has catchup. Once those runs are created there is large "backlog" of tasks to complete, create a run in scheduler2. The task sleep_it2 will not be executed till all runs of sleep_it1 are finished. That is though in each loop of the scheduler it can not really schedule tasks of sleep_it1 but can schedule sleep_it2.
I waited for more than 30 min and sleep_it2 did not get scheduled though it can be.
This happens both in Airflow 2 and Airflow 3.
collinmcnulty commentedon Apr 28, 2025
Aha, yes. That must be the missing ingredient that made it "hard" to reproduce. Still don't fully understand why each DAG Run in DAG 1 don't get marked with scheduling_decision and then DAG 2 is checked. It seems like the scheduler should be able to check all runs faster than 30 minutes.
eladkal commentedon Apr 28, 2025
Because when we read records from the database in the scheduler critical section we read as bulk. We don't have extra logic as we want this part to be as fast as possible. The logic comes after and you apply it on the bulk records you read (
AIRFLOW__SCHEDULER__MAX_TIS_PER_QUERY)jscheffl commentedon Apr 28, 2025
We also had such issue (had a talk about this in https://airflowsummit.org/sessions/2024/how-we-tuned-our-airflow-to-make-1-2-million-dag-runs-per-day/) and I think my colleguage @AutomationDev85 supplied a fix to Airflow 2.6 or 2.7... we our problem in this area were gone.
But one important remark: We ensured that the amount of parallel DAG runs is limited. We triggered new DAG runs in state "queued" and not "running", then the scheduler saves efforts in scheduling "not ready" runs. And was only adding "running" state at a point when sufficient DAGs have been worked-off. Can you try this?
eladkal commentedon Apr 29, 2025
Your approach definitely will work. In practice you bypassed the scheduler :)
But I can't really use it. The environment is shared by many dag authors. I can't predict when bottle necks will occur and I don't want to act as a "police" over the cluster. My goal is to find a native solution, I think by now we know this is more common than just a unique edge case so we need to find a proper solutions without workarounds.
IndiTheo commentedon May 1, 2025
Looks convincing. According to OP, the second DAG's runs are stuck at queued and not getting scheduled, so it shouldn't be related to
get_running_dag_runs_to_examineor pools or any issues with the critical section, because queued -> running transition for DAG runs happens much earlier than we get to any scheduling decisions for tasks (like creating TIs or scheduling them in CS). The whole logic for transitioning DAG runs to running is inSchedulerJobRunner._start_queued_dagrunsandDagRun.get_queued_dag_runs_to_set_running, and the described behaviour is a consequence of the optimistic scheduling strategy used across Airflow not being able to handle a variety of edge cases.We first pull at most
DEFAULT_DAGRUNS_TO_EXAMINEDAG runs whose DAGs are not yet maxed out:airflow/airflow-core/src/airflow/models/dagrun.py
Lines 602 to 611 in e4957ff
And then go over each one of them increasing the currently scheduled DAGrun count and dropping once we can't stuff any more runs:
airflow/airflow-core/src/airflow/jobs/scheduler_job_runner.py
Line 1740 in 65be581
airflow/airflow-core/src/airflow/jobs/scheduler_job_runner.py
Lines 1762 to 1773 in 65be581
It's indeed the same as #45636, but now with DAG runs instead of tasks. If the original issue can be efficiently solved with windowing, I guess this one can also benefit from it, like:
As windowing may introduce additional overhead which sometimes won't result in higher throughput, assuming there aren't so many DAG runs in the system, I'm looking for a compromise between the optimistic and the windowing (pessimistic) approaches. It's possible to have it configurable, though it will make the system harder and harder to tweak to everyone's needs, which is not desirable. I hope the windowing can be done in such a way that will be negligible for the query time, by the help of correct indexing, or completely changing some core concurrency parameters, like introducing
DEFAULT_DAGS_TO_EXAMINEin favor ofDEFAULT_DAGRUNS_TO_EXAMINE, where the new parameter limits the number of windows in the query. Some research is needed to find out.26 remaining items
amoghrajesh commentedon Jun 4, 2025
Reducing to medium priority as we cannot repro it easily.
dstandish commentedon Jun 5, 2025
Yeah Collin I think we need clarification on the repro scenario / under what conditions starvation occurs.
Let me go through your post carefully
What state are the "received" in? I assume you mean triggered via API?
What is the signifigance of the runs being very short? What do you think that has to do with this?
Why? So that we can be confident that the scheduler should fetch some of these runs in the query?
Could it be that what you were observing had to do with last scheduling decision not getting updated because tasks were backed up somehow?
I guess either way, this seems like a very rare edge case scenario and, if two people tried and failed it, not sure it's worth contiuning to try without new information.
amoghrajesh commentedon Jun 11, 2025
Removing the milestone as we do not have a clear repro environment for this. We can revisit on the milestone once we can repro it easily/
amoghrajesh commentedon Mar 25, 2026
Since this has not yet been picked up and we are approaching the 3.2 release, I am moving it to the 3.3 milestone.
Nataneljpwd commentedon Mar 25, 2026
Hello, I have an open PR for this issue @amoghrajesh
#64109