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
19 changes: 16 additions & 3 deletions airflow/jobs/scheduler_job_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from sqlalchemy import and_, delete, func, not_, or_, select, text, update
from sqlalchemy.engine import Result
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 @@ -1397,11 +1397,24 @@ def _schedule_dag_run(
callback: DagCallbackRequest | None = None

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:
self.log.info(
"DAG %s scheduling was skipped, probably because the DAG record was locked", dag_run.dag_id
)
return callback

if (
dag_run.start_date
Expand Down