sewookjeong7 opened a new pull request, #77859: URL: https://github.com/apache/airflow/pull/77859
The missed-deadline step of the scheduler loop loads every expired, unhandled deadline together with its callback in one query, then calls `handle_miss` on each. If one deadline raises, while its callback is loaded or inside `handle_miss`, the exception escapes `_run_scheduler_loop` and the scheduler exits. The deadline is still expired and unhandled, so every restarted scheduler picks it up and exits again. Nothing is scheduled for the whole deployment until someone edits the metadata database. Two unrelated causes have already done this: migration `0094` stores callback `kwargs` in a shape `ExtendedJSON` cannot load (#69980, fixed in #69985), and `callback.type` stored as `'CallbackType.TRIGGER'` with mysqlclient 2.3.1 (#76926). We hit the first one in production right after upgrading 3.1.8 → 3.4.0: a Dag run that was running during the upgrade later missed its deadline, and the scheduler crash-looped with `KeyError: <Encoding.VAR: '__var'>` until the row was marked as missed by hand. Each deadline is now handled on its own: - The locked query (`FOR UPDATE SKIP LOCKED`, unchanged) selects only the deadline ids. Each deadline is then loaded, with its callback and Dag run, and handled inside its own savepoint. - If that fails, the savepoint is rolled back, the deadline is marked as missed and its callback as `FAILED` with plain `UPDATE`s (loading the callback may be what failed), and the loop moves on to the next deadline. `callback.output` gets only the exception type, e.g. `Not queued: handling the expired deadline raised KeyError`; the traceback goes to the scheduler log with the deadline id, Dag id and run id. - A new counter, `deadline_alerts.deadline_handling_failed` (tagged with `dag_id`), counts these deadlines, so operators can alert on the lost notification. The existing `callback_failed` metric cannot be used here because it builds its tags from `callback.data`. The savepoint is needed on PostgreSQL. When the error comes from the database, the transaction is aborted and the follow-up `UPDATE`s fail as well (`InFailedSqlTransaction` when the savepoint is removed); when `handle_miss` fails after queueing the callback, the savepoint also removes the trigger it already created. This is the same approach as `handle_miss` failing the callback for reserved serialization keys, and as #74446 for executor callbacks. A deadline that fails here is not retried, so its alert is not sent. That includes a transient database error in this step, which used to be retried after the scheduler restarted. **Performance.** The step now runs one query for the ids, then three queries and a savepoint per expired deadline instead of three queries in total. Each deadline goes through this step once. Timed with a throwaway script on a local PostgreSQL 16 container: the old loop body and the new method on the same rows, rolled back after each run, alternating, median of 7, repeated 3 times (ranges are the 3 medians). Control: `_enqueue_executor_callbacks` on the same session. | Expired deadlines in one loop | Before (ms) | After (ms) | Control (ms) | |---|---|---|---| | 0 | 1.11–1.16 | 1.01–1.06 | 1.13–1.17 | | 1 | 6.7–7.5 | 7.8–8.6 | 1.1–1.3 | | 10 | 30–37 | 54–64 | 1.2–1.7 | | 100 | 362–379 | 673–841 | 1.4–2.6 | A loop with no expired deadline, which is almost every loop, is not slower. Each expired deadline costs about 3 ms more on this machine, once. **Testing** - New test `test_expired_deadline_that_cannot_be_handled_does_not_stop_other_deadlines`, parametrized over three failures: callback data in the `0094` shape (`KeyError` while loading), `handle_miss` raising after it queued the callback, and the database rejecting a write in `handle_miss`. In each case the broken deadline ends up missed with its callback `FAILED` and no trigger, the other deadline's callback is queued, the metric is emitted once, and a second scheduler run does not pick the broken deadline up again. All three fail on `main`. - Deadline tests (`-k "expired_deadline or process_expired_deadlines"`): SQLite 7 passed, 1 skipped (row locking), with `uv run --project airflow-core pytest`; PostgreSQL 16: 8 passed, including `test_expired_deadline_locked_by_other_scheduler_is_skipped`; MySQL 8.0 (`breeze run --backend mysql`): 8 passed. - The commands `breeze verify` lists for this change: DB tests for API, Always, CLI, Other and Serialization pass; the Core DB tests pass with no failures, run in four parts because a single run ran out of the laptop's Docker memory; non-DB tests: 6147 passed; `shared/*` tests pass; `breeze build-docs apache-airflow task-sdk` succeeds. - `prek run --stage pre-commit` on the changed files: ruff, ruff-format, mypy for airflow-core and the metrics registry check pass. related: #69980, #69985, #74446, #76926 --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes — Claude Code (Opus 5.5) Generated-by: Claude Code (Opus 5.5) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions) -- 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]
