You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "dstandish (via GitHub)" <gi...@apache.org> on 2025/12/22 16:49:46 UTC
[PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
dstandish opened a new pull request, #59724:
URL: https://github.com/apache/airflow/pull/59724
Currently it's a bit complicated and confusing. It remains so after this update, but hopefully a little improved.
For example we have an argument "last automated dagrun" that takes not a dag run but a data interval.
And, when we call it, in places in the scheduler, we may call it with a run that is not actually "the last automated dagrun" -- it could be any dag run the scheduler touches, which of course could be a very old dag run.
I don't solve that problem here.
But I do try to make things a little clearer and a little simpler at the scheduler call sites.
In particular, there were two things that bothered me.
1. in the function `_should_update_dag_next_dagruns`, we did not only *check* whether we should update. In some branches we actually *did* the update then said "no, don't update." I pull this update higher up where it's more visible.
2. it's not obvious that the `calculate_dagrun_date_fields` function actually mutates the dag. It just says "calculate," right? So, to improve this slightly, and to reduce noise a bit at call site, I wrap this (along with the update mentioned above) in a function called `_update_next_dagrun_fields`. At least it's clear we are (at least probably) updating the dag fields. This has the side benefit bringing all invocations of `calculate_dagrun_date_fields` into one function in the scheduler (which is invoked in a few places).
So hopefully modest clarity improvement, and hopefully no behavior change.
Will have to update some tests but will wait for them to fail first.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2645737644
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1815,9 +1816,10 @@ def _create_dag_runs(self, dag_models: Collection[DagModel], session: Session) -
# we need to set DagModel.next_dagrun_info if the DagRun already exists or if we
# create a new one. This is so that in the next scheduling loop we try to create new runs
# instead of falling in a loop of IntegrityError.
Review Comment:
do you mean because i added the if condition here?
```
if created_dag_run:
self._update_next_dagrun_fields(
serdag=serdag,
dag_model=dag_model,
session=session,
active_non_backfill_runs=active_runs_of_dags[serdag.dag_id],
data_interval=data_interval,
)
```
> Would that cause a bug where a DagRun exists but DagModel.next_dagrun wasn't updated (the edge case mentioned in the comment), the scheduler will get stuck in an infinite loop attempting to create the same dag run repeatedly?
No this is not possible because the dag processor, every time it processes a dag, it runs dag_model.calculate_dagrun_date_fields and if it needs to be incremented, it will be.
Basically, if the scheduler is already trying to create the dagrun that already exists, then something effed up, or something weird happed (like manual trigger of a future logical date).
The most common scenario would be, I believe, that the scheduler _decremented_ next_dagrun.
This can happen in the scheduler because suppose it creates 5 runs for catchup, but then an older one finishes after a later one -- then the scheduler may set the next_dagrun backward because of the way this code works.
**I think we should add logic so that the scheduler is never setting the next_dagrun backward**. but i am not trying to change that here.
So ultimately, I don't think it makes much difference whether we conditionally update based on created or not -- but it doesn't hurt to always update in this context, so I removed the condition.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish merged PR #59724:
URL: https://github.com/apache/airflow/pull/59724
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2650621067
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2253,47 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval | None,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
Review Comment:
nit: we use `dag` and `DAG` in the comments of this method
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2650626454
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2253,47 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval | None,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
+
+ If dag exceeds max active runs, set to None.
+
+ If dag's timetable not schedulable, don't update.
+
+ Otherwise, update via ``DagModel.calculate_dagrun_date_fields``.
+ """
+ exceeds_max, active_runs = self._exceeds_max_active_runs(
+ dag_model=dag_model,
+ active_non_backfill_runs=active_non_backfill_runs,
+ session=session,
+ )
+ if exceeds_max:
+ self.log.info(
+ "Dag exceeds max_active_runs; not creating any more runs",
+ dag_id=dag_model.dag_id,
+ active_runs=active_runs,
+ max_active_runs=dag_model.max_active_runs,
+ )
+ # null out next_dagrun_create_after so scheduler will not examine this dag
+ # this is periodically reconsidered in the scheduler and dag processor.
+ dag_model.next_dagrun_create_after = None
+ return
+
+ # If the DAG never schedules skip save runtime
+ if not serdag.timetable.can_be_scheduled:
+ return
Review Comment:
I guess we're moving it here since we believe it's not going to happen. If not so, move it to above and exit early might be better
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2253,47 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval | None,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
Review Comment:
nit: we use `dag` and `DAG` in the comments
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2643511623
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1923,43 +1926,26 @@ def _create_dag_runs_asset_triggered(
dag_run.consumed_asset_events.extend(asset_events)
session.execute(delete(AssetDagRunQueue).where(AssetDagRunQueue.target_dag_id == dag_run.dag_id))
- def _should_update_dag_next_dagruns(
+ def _finished_and_automated(
self,
dag: SerializedDAG,
- dag_model: DagModel,
*,
last_dag_run: DagRun | None = None,
- active_non_backfill_runs: int | None = None,
- session: Session,
) -> bool:
"""Check if the dag's next_dagruns_create_after should be updated."""
Review Comment:
yup thanks
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2644528648
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1815,9 +1816,10 @@ def _create_dag_runs(self, dag_models: Collection[DagModel], session: Session) -
# we need to set DagModel.next_dagrun_info if the DagRun already exists or if we
# create a new one. This is so that in the next scheduling loop we try to create new runs
# instead of falling in a loop of IntegrityError.
Review Comment:
Would that cause a bug where a `DagRun` exists but `DagModel.next_dagrun` wasn't updated (the edge case mentioned in the comment), the scheduler will get stuck in an infinite loop attempting to create the same dag run repeatedly.
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1815,9 +1816,10 @@ def _create_dag_runs(self, dag_models: Collection[DagModel], session: Session) -
# we need to set DagModel.next_dagrun_info if the DagRun already exists or if we
# create a new one. This is so that in the next scheduling loop we try to create new runs
# instead of falling in a loop of IntegrityError.
Review Comment:
Would that cause a bug where a `DagRun` exists but `DagModel.next_dagrun` wasn't updated (the edge case mentioned in the comment), the scheduler will get stuck in an infinite loop attempting to create the same dag run repeatedly?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2644530120
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -3008,6 +3023,23 @@ def _try_to_load_executor(self, ti: TaskInstance, session, team_name=NOTSET) ->
return executor
+ def _exceeds_max_active_runs(
+ self,
+ *,
+ dag_model,
Review Comment:
```suggestion
dag_model: DagModel,
```
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2644529341
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2255,43 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
+
+ The ``dag_run`` param is only to be given when
Review Comment:
incomplete sentence.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2641900596
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1923,43 +1926,26 @@ def _create_dag_runs_asset_triggered(
dag_run.consumed_asset_events.extend(asset_events)
session.execute(delete(AssetDagRunQueue).where(AssetDagRunQueue.target_dag_id == dag_run.dag_id))
- def _should_update_dag_next_dagruns(
+ def _finished_and_automated(
self,
dag: SerializedDAG,
- dag_model: DagModel,
*,
last_dag_run: DagRun | None = None,
- active_non_backfill_runs: int | None = None,
- session: Session,
) -> bool:
"""Check if the dag's next_dagruns_create_after should be updated."""
Review Comment:
This docstring should probably be updated to match the func name?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2645710548
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2255,43 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
+
+ The ``dag_run`` param is only to be given when
Review Comment:
and the reason you needed to not give it unconditionally is because when you have just createed the run, it's always not in terminated state. but the "should update" logic before would check that the state of the given run must be terminal, else it would not increment next dag run. so if, in the dag creation context, we were to give it what it asked for (the last dag run) it would have done the wrong thing. this is why the logic needed to be pulled out so the evaluation happens outside of the function, and the function always does what it says it's going to do.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2643528908
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1923,43 +1926,26 @@ def _create_dag_runs_asset_triggered(
dag_run.consumed_asset_events.extend(asset_events)
session.execute(delete(AssetDagRunQueue).where(AssetDagRunQueue.target_dag_id == dag_run.dag_id))
- def _should_update_dag_next_dagruns(
+ def _finished_and_automated(
self,
dag: SerializedDAG,
- dag_model: DagModel,
*,
last_dag_run: DagRun | None = None,
- active_non_backfill_runs: int | None = None,
- session: Session,
) -> bool:
"""Check if the dag's next_dagruns_create_after should be updated."""
Review Comment:
i will just remove that function and inline that small bit of code
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2644528341
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1815,9 +1816,10 @@ def _create_dag_runs(self, dag_models: Collection[DagModel], session: Session) -
# we need to set DagModel.next_dagrun_info if the DagRun already exists or if we
# create a new one. This is so that in the next scheduling loop we try to create new runs
# instead of falling in a loop of IntegrityError.
Review Comment:
This isn't true anymore with the changes.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2645748184
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2254,47 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
+
+ If dag exceeds max active runs, don't update.
+
+ If dag's timetable not schedulable, don't update.
+
+ Otherwise, update.
+ """
+ exceeds_max, active_runs = self._exceeds_max_active_runs(
+ dag_model=dag_model,
+ active_non_backfill_runs=active_non_backfill_runs,
+ session=session,
+ )
+ if exceeds_max:
+ self.log.info(
+ "Dag exceeds max_active_runs; not creating any more runs",
+ dag_id=dag_model.dag_id,
+ active_runs=active_runs,
+ max_active_runs=dag_model.max_active_runs,
+ )
+ # null out next_dagrun_create_after so scheduler will not examine this dag
+ # this is periodically reconsidered in the scheduler and dag processor.
+ dag_model.next_dagrun_create_after = None
+ return
+
+ # If the DAG never schedules skip save runtime
+ if not serdag.timetable.can_be_scheduled:
+ return
Review Comment:
If it can't be scheduled, how did it even get 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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2645747097
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2254,47 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
+
+ If dag exceeds max active runs, don't update.
+
+ If dag's timetable not schedulable, don't update.
+
+ Otherwise, update.
+ """
+ exceeds_max, active_runs = self._exceeds_max_active_runs(
+ dag_model=dag_model,
+ active_non_backfill_runs=active_non_backfill_runs,
+ session=session,
+ )
+ if exceeds_max:
+ self.log.info(
+ "Dag exceeds max_active_runs; not creating any more runs",
+ dag_id=dag_model.dag_id,
+ active_runs=active_runs,
+ max_active_runs=dag_model.max_active_runs,
+ )
+ # null out next_dagrun_create_after so scheduler will not examine this dag
+ # this is periodically reconsidered in the scheduler and dag processor.
+ dag_model.next_dagrun_create_after = None
+ return
+
+ # If the DAG never schedules skip save runtime
+ if not serdag.timetable.can_be_scheduled:
+ return
Review Comment:
@kaxil seems that in the "not schedulable" case we should set None too 🤔
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Clarify a bit the code re updating next_dag run and next run after [airflow]
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #59724:
URL: https://github.com/apache/airflow/pull/59724#discussion_r2645704871
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2277,6 +2255,43 @@ def _schedule_dag_run(
return callback_to_run
+ def _update_next_dagrun_fields(
+ self,
+ *,
+ serdag: SerializedDAG,
+ dag_model: DagModel,
+ session: Session,
+ active_non_backfill_runs: int | None = None,
+ data_interval: DataInterval,
+ ):
+ """
+ Conditionally update fields next_dagrun and next_dagrun_create_after on dag table.
+
+ The ``dag_run`` param is only to be given when
Review Comment:
that's because in writing the docstring i was like, this is dumb -- we should not do this :) then i went ahead and pulled that param out.
basically, you were supposed to _not_ give the dag run when you created the dag run, and you _were_ supposed to give it when you didn't (i.e. when just making scheduling decisions). but that is confusing behavior so i pulled that logic out.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org