Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix future DagRun rarely triggered by race conditions when max_active_runs reached its upper limit. #31414

Merged
merged 8 commits into from Aug 8, 2023
15 changes: 13 additions & 2 deletions airflow/jobs/scheduler_job_runner.py
Expand Up @@ -34,7 +34,7 @@

from sqlalchemy import and_, func, not_, or_, text
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import Query, Session, load_only, make_transient, selectinload
from sqlalchemy.orm import Query, Session, joinedload, load_only, make_transient, selectinload
from sqlalchemy.sql import expression

from airflow import settings
Expand Down Expand Up @@ -1375,10 +1375,21 @@ def _schedule_dag_run(

dag = dag_run.dag = self.dagbag.get_dag(dag_run.dag_id, session=session)
dag_model = DM.get_dagmodel(dag_run.dag_id, session)
# Adopt row locking to account for inconsistencies when next_dagrun_create_after = None
query = (
session.query(DagModel)
.filter(DagModel.dag_id == dag_run.dag_id)
.options(joinedload(DagModel.parent_dag))
)
dag_model = with_row_locks(
query, of=DagModel, session=session, **skip_locked(session=session)
).one_or_none()

if not dag or not dag_model:
if not dag:
self.log.error("Couldn't find DAG %s in DAG bag or database!", dag_run.dag_id)
return callback
if not dag_model:
return callback
uranusjr marked this conversation as resolved.
Show resolved Hide resolved

if (
dag_run.start_date
Expand Down