uranusjr commented on a change in pull request #17945:
URL: https://github.com/apache/airflow/pull/17945#discussion_r700869586
##########
File path: airflow/jobs/scheduler_job.py
##########
@@ -970,48 +971,61 @@ def _start_queued_dagruns(
session: Session,
) -> int:
"""Find DagRuns in queued state and decide moving them to running
state"""
- dag_runs = self._get_next_dagruns_to_examine(State.QUEUED, session)
-
- active_runs_of_dags = defaultdict(
- lambda: 0,
- session.query(DagRun.dag_id, func.count('*'))
- .filter( # We use `list` here because SQLA doesn't accept a set
- # We use set to avoid duplicate dag_ids
- DagRun.dag_id.in_(list({dr.dag_id for dr in dag_runs})),
- DagRun.state == State.RUNNING,
- )
- .group_by(DagRun.dag_id)
- .all(),
+ active_dags = (
+ session.query(DM)
+ .filter(DM.is_active == expression.true(), DM.is_paused ==
expression.false())
+ .all()
)
+ max_number = conf.getint('scheduler',
'max_dagruns_per_loop_to_schedule', fallback=20)
+ # There's a possibility of exceeding this max_number or going below it
as a result of the logic below
+ if len(active_dags) > max_number:
+ max_number = 1
+ elif len(active_dags) > 0:
+ max_number = round(max_number / len(active_dags))
- def _update_state(dag_run):
- dag_run.state = State.RUNNING
- dag_run.start_date = timezone.utcnow()
- expected_start_date =
dag.following_schedule(dag_run.execution_date)
- if expected_start_date:
- schedule_delay = dag_run.start_date - expected_start_date
- Stats.timing(
- f'dagrun.schedule_delay.{dag.dag_id}',
- schedule_delay,
- )
+ for dag_model in active_dags:
- for dag_run in dag_runs:
- try:
- dag = dag_run.dag = self.dagbag.get_dag(dag_run.dag_id,
session=session)
- except SerializedDagNotFound:
- self.log.exception("DAG '%s' not found in serialized_dag
table", dag_run.dag_id)
- continue
- active_runs = active_runs_of_dags[dag_run.dag_id]
- if dag.max_active_runs and active_runs >= dag.max_active_runs:
- self.log.debug(
- "DAG %s already has %d active runs, not moving any more
runs to RUNNING state %s",
- dag.dag_id,
- active_runs,
- dag_run.execution_date,
+ queued_dagruns =
DR.get_next_queued_dagruns_for_dag(dag_model.dag_id, session, max_number)
+
+ active_runs_of_dags = defaultdict(
+ lambda: 0,
+ session.query(DagRun.dag_id, func.count('*'))
+ .filter(
+ DagRun.dag_id == dag_model.dag_id,
+ DagRun.state == State.RUNNING,
)
Review comment:
```suggestion
active_runs_of_dags = defaultdict(
int,
session.query(DagRun.dag_id, func.count('*'))
.filter(
DagRun.dag_id == dag_model.dag_id,
DagRun.state == State.RUNNING,
)
```
(Not particularly hard on this, just personaly preference.)
--
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]