xxntti3n opened a new pull request, #77453: URL: https://github.com/apache/airflow/pull/77453
Every running task updates `task_instance.last_heartbeat_at` on each heartbeat (every 5s by default). That column is covered by the `ti_heartbeat` index, so PostgreSQL can never apply the update as HOT: each heartbeat writes a new entry into every index on `task_instance` and leaves dead entries behind. The index is not used by the zombie scan (it filters on `state` first and is served by `ti_state`). This PR does two things: - Skips the heartbeat write while the stored value is newer than the new `[execution_api] heartbeat_write_interval` (default 30s, `0` writes on every heartbeat). The effective interval is capped at half of `[scheduler] task_instance_heartbeat_timeout`, so the stored value cannot lag a live task by more than the scheduler tolerates. The throttled path still returns 204 and the existing 404/409/410 diagnostics run unchanged when the fast path matches no row. - Drops the `ti_heartbeat` index (migration `0158`), so the writes that remain can be HOT. closes: #77363 **Behaviour to review** - A skipped heartbeat is answered after an unlocked read, so a worker whose task instance was cleared learns of it on the next written heartbeat instead of the current one (up to the write interval later, not one heartbeat). - The API server and the scheduler must use the same `task_instance_heartbeat_timeout`; the config description says so. - Dead-task detection can happen up to about the write interval earlier or later than the exact timeout. **Verification (PostgreSQL 17, real `task_instance` schema, 200k rows, 3000 heartbeat updates of one running row)** | | updates | HOT | |---|---|---| | without `ti_heartbeat` (this PR) | 3000 | 3000 | | with `ti_heartbeat` (before) | 3000 | 0 | Single run on one container; `fillfactor` and long-term index bloat were not measured, and the Cloud SQL numbers in the issue are not reproduced here. Checks run: - `ruff format --check` / `ruff check` on the changed Python files: clean. - `test_task_instances.py` (execution API): 367 passed, 6 skipped (SQLite). The new parametrized `test_ti_heartbeat_write_is_throttled` was also run on PostgreSQL; temporarily reverting the throttle makes its skip cases fail. - Scheduler heartbeat/zombie tests: 51 passed, 4 skipped. `test_db.py`: 35 passed, 2 skipped. - Migration upgrade, downgrade and re-upgrade on PostgreSQL 17. - `airflow config lint` and a regenerated `migrations-ref.rst` (run by hand). Not run locally: - The `update-migration-references` and `check-default-configuration` prek hooks (they need the breeze CI image and could not mount the Docker socket here), and the full `breeze` test suite. - `mypy-airflow-core` reports 15 errors in 4 files not touched by this PR; the count is the same without my changes. - Not verified on MySQL. --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes — Claude Code (Sonnet 5.5) Generated-by: Claude Code (Sonnet 5.5) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions) --- * Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information. Note: commit author/co-author name and email in commits become permanently public when merged. * For fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed. * When adding dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x). * For significant user-facing changes create newsfragment: `{pr_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments). You can add this file in a follow-up commit after the PR is created so you know the PR number. -- 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]
