ephraimbuddy commented on code in PR #74791: URL: https://github.com/apache/airflow/pull/74791#discussion_r4443836324
########## airflow-core/src/airflow/dag_processing/maintenance.py: ########## @@ -0,0 +1,94 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Bounded metadata maintenance owned by the scheduler, including absent bundles.""" + +from __future__ import annotations + +import logging +from datetime import timedelta + +from sqlalchemy import delete, select, tuple_ + +from airflow._shared.timezones import timezone +from airflow.configuration import conf +from airflow.jobs.job import Job, JobState +from airflow.models.dag import DagModel +from airflow.models.dagbundle import DagBundleModel +from airflow.models.dagwarning import DagWarning +from airflow.utils.session import create_session +from airflow.utils.sqlalchemy import with_row_locks + +log = logging.getLogger(__name__) + + +def cleanup_processor_metadata(batch_size: int = 500) -> None: + """Commit each bounded batch; a later interval picks up remaining rows.""" + with create_session() as session: + inactive = select(DagBundleModel.name).where(DagBundleModel.active.is_(False)) + query = ( + select(DagModel) + .where( + DagModel.is_stale.is_(False), + DagModel.bundle_name.in_(inactive), + ) + .order_by(DagModel.dag_id) + .limit(batch_size) + ) + for dag in session.scalars(with_row_locks(query, session=session, skip_locked=True)): + log.info( + "Deactivating Dag %s. Its bundle %s is no longer active.", + dag.dag_id, + dag.bundle_name, + ) + dag.is_stale = True + with create_session() as session: + warning_query = ( + select(DagWarning.dag_id, DagWarning.warning_type) + .join(DagModel) + .where( + DagModel.is_stale.is_(True), + ) + .order_by(DagWarning.dag_id, DagWarning.warning_type) + # Each key needs two bind parameters; older SQLite versions allow only 999. + .limit(min(batch_size, 499)) + ) + keys = session.execute( + with_row_locks(warning_query, of=DagWarning, session=session, skip_locked=True) + ).all() + if keys: + session.execute( + delete(DagWarning).where(tuple_(DagWarning.dag_id, DagWarning.warning_type).in_(keys)) + ) + with create_session() as session: + cutoff = timezone.utcnow() - timedelta(seconds=conf.getint("dag_processor", "job_heartbeat_timeout")) + job_query = ( + select(Job) + .where( + Job.job_type == "DagProcessorJob", + # API Jobs end with a terminal state; use its heartbeat index to skip completed history. + Job.state == JobState.RUNNING, + Job.session_id.is_not(None), + Job.end_date.is_(None), + Job.latest_heartbeat < cutoff, + ) + .order_by(Job.id) + .limit(batch_size) + ) + for job in session.scalars(with_row_locks(job_query, session=session, skip_locked=True)): + job.state = JobState.FAILED Review Comment: Done. Expiry now logs one warning per Job with `job_id`, `hostname`, `latest_heartbeat` and `job_heartbeat_timeout`. `DagParseCheckpoint` is now `DagProcessorPublicationReceipt`. Scheduler maintenance deletes the receipts of every ended Job in committed batches, not only the Jobs it expires in that run, so receipts left by an interrupted run are deleted on the next interval. --- Drafted-by: Claude Code (Opus 5.5); reviewed by @ephraimbuddy before posting ########## airflow-core/src/airflow/config_templates/config.yml: ########## @@ -3478,6 +3480,17 @@ dag_processor: type: integer example: ~ default: "30" + job_heartbeat_timeout: Review Comment: Went with the cross-check rather than deriving expiry from `health_check_threshold`. If `job_heartbeat_timeout` is below `health_check_threshold`, registration returns 503 `invalid_configuration` and scheduler maintenance skips expiry with a warning. The two option descriptions now point at each other: a same-session registration can release claims after `health_check_threshold`, before this timeout, so callbacks have to tolerate running again. --- Drafted-by: Claude Code (Opus 5.5); reviewed by @ephraimbuddy before posting -- 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]
