-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Fix deadline serialization, repr, and prune edge cases #70421
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
Merged
o-nikolas
merged 8 commits into
apache:main
from
aws-mwaa:feature/deadline-robustness-followup
Aug 17, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0d7910
Harden deadline serialization, repr, and prune paths
seanghaeli 620598f
Trim comment
seanghaeli 43d03f4
Trim comment
seanghaeli 385914d
Merge branch 'main' of https://github.com/apache/airflow into feature…
seanghaeli e3af694
Re-trigger CI
seanghaeli c18bdde
Address review nits: trim test docstrings and rename scratch identifiers
seanghaeli f77e56e
Cover VariableInterval interval shape in DeadlineAlert repr test
seanghaeli dad01c8
Drop redundant setup-side DB clean in prune deadlines test
seanghaeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # 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. | ||
| """Coverage for ``Deadline.prune_deadlines``: on-time/overdue/pending selection, | ||
| callback cascade behaviour, and concurrent-mutation edge cases.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import timedelta | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
| import time_machine | ||
| from sqlalchemy import select | ||
|
|
||
| from airflow.models import DagRun | ||
| from airflow.models.callback import Callback | ||
| from airflow.models.deadline import Deadline | ||
| from airflow.providers.standard.operators.empty import EmptyOperator | ||
| from airflow.sdk.definitions.callback import AsyncCallback | ||
| from airflow.utils.state import DagRunState | ||
|
|
||
| from tests_common.test_utils import db | ||
| from unit.models import DEFAULT_DATE | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| DAG_ID = "prune_deadlines_test_dag" | ||
|
|
||
|
|
||
| async def _prune_test_callback(): | ||
| pass | ||
|
|
||
|
|
||
| CALLBACK_PATH = f"{__name__}.{_prune_test_callback.__name__}" | ||
|
|
||
|
|
||
| def _clean_db(): | ||
| db.clear_db_dags() | ||
| db.clear_db_runs() | ||
| db.clear_db_deadline() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def dagrun(session, dag_maker): | ||
| with dag_maker(DAG_ID): | ||
| EmptyOperator(task_id="task_id") | ||
| with time_machine.travel(DEFAULT_DATE): | ||
| dag_maker.create_dagrun(state=DagRunState.QUEUED, logical_date=DEFAULT_DATE) | ||
| session.commit() | ||
| return session.scalars(select(DagRun)).one() | ||
|
|
||
|
|
||
| def _make_deadline(session: Session, *, dagrun_id: int, deadline_time, state=None) -> Deadline: | ||
| deadline = Deadline( | ||
| deadline_time=deadline_time, | ||
| callback=AsyncCallback(CALLBACK_PATH), | ||
| dagrun_id=dagrun_id, | ||
| dag_id=DAG_ID, | ||
| deadline_alert_id=None, | ||
| ) | ||
| session.add(deadline) | ||
| session.flush() | ||
| if state is not None: | ||
| deadline.callback.state = state | ||
| session.add(deadline.callback) | ||
| session.flush() | ||
| return deadline | ||
|
|
||
|
|
||
| @pytest.mark.db_test | ||
| class TestPruneDeadlines: | ||
| @staticmethod | ||
| def teardown_method(): | ||
| _clean_db() | ||
|
|
||
| def test_handle_miss_then_prune_does_not_delete_missed(self, dagrun, session): | ||
| """ | ||
| Inverse race: handle_miss marks the deadline missed and queues the callback | ||
| BEFORE the on-time prune runs. prune must NOT delete a deadline already marked | ||
| ``missed`` — its callback is owned by the scheduler/triggerer, and cascade-deleting | ||
| the deadline would silently drop that queued callback (a lost-callback window). | ||
|
|
||
| prune_deadlines explicitly filters ``~Deadline.missed`` so a missed deadline (and its | ||
| queued callback) survives even if the DagRun's end_date would otherwise match the | ||
| on-time predicate. (In the real scheduler a missed deadline always has | ||
| ``deadline_time < now <= end_date`` so it can't match the on-time predicate anyway; | ||
| the explicit filter makes the invariant robust to future callers and clock skew.) | ||
| """ | ||
| d = _make_deadline(session, dagrun_id=dagrun.id, deadline_time=DEFAULT_DATE + timedelta(hours=1)) | ||
| # Set missed + queued callback directly to test the prune ``~missed`` guard in isolation. | ||
| d.callback.queue(session=session) | ||
| d.missed = True | ||
| session.add_all([d, d.callback]) | ||
| session.flush() | ||
| assert d.missed is True | ||
| deadline_id = d.id | ||
| callback_id = d.callback.id | ||
|
|
||
| # DagRun reports on-time completion (end_date <= deadline_time) — the exact condition | ||
| # that would have pruned the row before the ~missed guard was added. | ||
| dagrun.end_date = DEFAULT_DATE | ||
| session.add(dagrun) | ||
| session.flush() | ||
|
|
||
| deleted = Deadline.prune_deadlines(session=session, conditions={DagRun.id: dagrun.id}) | ||
| session.flush() | ||
|
|
||
| # The missed deadline and its queued callback both survive. | ||
| assert deleted == 0 | ||
| assert session.get(Deadline, deadline_id) is not None | ||
| assert session.get(Callback, callback_id) is not None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.