Skip to content

Add fast-path heartbeat UPDATE to avoid row lock in the common case#65029

Merged
ephraimbuddy merged 5 commits intoapache:mainfrom
astronomer:improve-ti_heartbeat
Apr 13, 2026
Merged

Add fast-path heartbeat UPDATE to avoid row lock in the common case#65029
ephraimbuddy merged 5 commits intoapache:mainfrom
astronomer:improve-ti_heartbeat

Conversation

@ephraimbuddy
Copy link
Copy Markdown
Contributor

The ti_heartbeat endpoint now attempts a single guarded UPDATE (matching id, state, hostname, and pid) before falling back to the existing SELECT FOR UPDATE path. When the task is still running on the expected host this returns immediately, eliminating the row lock and a round trip for the vast majority of heartbeat calls.

@boring-cyborg boring-cyborg bot added area:API Airflow's REST/HTTP API area:task-sdk labels Apr 10, 2026
Copy link
Copy Markdown
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

@kaxil kaxil requested a review from Copilot April 10, 2026 19:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR optimizes the ti_heartbeat endpoint by attempting a guarded UPDATE first (matching id, state, hostname, pid) to avoid SELECT ... FOR UPDATE row locking in the common “still running on same worker” case.

Changes:

  • Add a fast-path guarded UPDATE for heartbeat updates and return early if it affects a row.
  • Log and fall back to the existing SELECT ... FOR UPDATE diagnostic/validation path when the fast path doesn’t match.
  • Add unit tests covering both the fast-path success case and the fast-path miss → fallback case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py Adds fast-path guarded heartbeat UPDATE and fallback behavior.
airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py Adds tests to validate fast-path behavior and fallback on miss.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

ephraimbuddy and others added 4 commits April 13, 2026 09:35
The ti_heartbeat endpoint now attempts a single guarded UPDATE
(matching id, state, hostname, and pid) before falling back to the
existing SELECT FOR UPDATE path. When the task is still running on
the expected host this returns immediately, eliminating the row lock
and a round trip for the vast majority of heartbeat calls.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@ephraimbuddy ephraimbuddy force-pushed the improve-ti_heartbeat branch from c196e03 to b7e5820 Compare April 13, 2026 08:38
@eladkal eladkal added this to the Airflow 3.2.1 milestone Apr 13, 2026
@eladkal eladkal added type:bug-fix Changelog: Bug Fixes backport-to-v3-2-test Mark PR with this label to backport to v3-2-test branch labels Apr 13, 2026
@ephraimbuddy ephraimbuddy merged commit c97d1a5 into apache:main Apr 13, 2026
142 checks passed
@ephraimbuddy ephraimbuddy deleted the improve-ti_heartbeat branch April 13, 2026 13:35
github-actions bot pushed a commit that referenced this pull request Apr 13, 2026
…ommon case (#65029)

* Add fast-path heartbeat UPDATE to avoid row lock in the common case

The ti_heartbeat endpoint now attempts a single guarded UPDATE
(matching id, state, hostname, and pid) before falling back to the
existing SELECT FOR UPDATE path. When the task is still running on
the expected host this returns immediately, eliminating the row lock
and a round trip for the vast majority of heartbeat calls.

* fixup! Add fast-path heartbeat UPDATE to avoid row lock in the common case

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix static checks

* fixup! Fix static checks

---------
(cherry picked from commit c97d1a5)

Co-authored-by: Ephraim Anierobi <splendidzigy24@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

Backport successfully created: v3-2-test

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
v3-2-test PR Link

ephraimbuddy added a commit that referenced this pull request Apr 14, 2026
…ommon case (#65029)

* Add fast-path heartbeat UPDATE to avoid row lock in the common case

The ti_heartbeat endpoint now attempts a single guarded UPDATE
(matching id, state, hostname, and pid) before falling back to the
existing SELECT FOR UPDATE path. When the task is still running on
the expected host this returns immediately, eliminating the row lock
and a round trip for the vast majority of heartbeat calls.

* fixup! Add fast-path heartbeat UPDATE to avoid row lock in the common case

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix static checks

* fixup! Fix static checks

---------
(cherry picked from commit c97d1a5)

Co-authored-by: Ephraim Anierobi <splendidzigy24@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
ephraimbuddy added a commit that referenced this pull request Apr 14, 2026
…ommon case (#65029)

* Add fast-path heartbeat UPDATE to avoid row lock in the common case

The ti_heartbeat endpoint now attempts a single guarded UPDATE
(matching id, state, hostname, and pid) before falling back to the
existing SELECT FOR UPDATE path. When the task is still running on
the expected host this returns immediately, eliminating the row lock
and a round trip for the vast majority of heartbeat calls.

* fixup! Add fast-path heartbeat UPDATE to avoid row lock in the common case

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix static checks

* fixup! Fix static checks

---------
(cherry picked from commit c97d1a5)

Co-authored-by: Ephraim Anierobi <splendidzigy24@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
ephraimbuddy added a commit that referenced this pull request Apr 14, 2026
…ommon case (#65029)

* Add fast-path heartbeat UPDATE to avoid row lock in the common case

The ti_heartbeat endpoint now attempts a single guarded UPDATE
(matching id, state, hostname, and pid) before falling back to the
existing SELECT FOR UPDATE path. When the task is still running on
the expected host this returns immediately, eliminating the row lock
and a round trip for the vast majority of heartbeat calls.

* fixup! Add fast-path heartbeat UPDATE to avoid row lock in the common case

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix static checks

* fixup! Fix static checks

---------
(cherry picked from commit c97d1a5)

Co-authored-by: Ephraim Anierobi <splendidzigy24@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
vatsrahul1001 added a commit that referenced this pull request Apr 15, 2026
…ommon case (#65029) (#65137)

* Add fast-path heartbeat UPDATE to avoid row lock in the common case

The ti_heartbeat endpoint now attempts a single guarded UPDATE
(matching id, state, hostname, and pid) before falling back to the
existing SELECT FOR UPDATE path. When the task is still running on
the expected host this returns immediately, eliminating the row lock
and a round trip for the vast majority of heartbeat calls.

* fixup! Add fast-path heartbeat UPDATE to avoid row lock in the common case

* Apply suggestions from code review



* Fix static checks

* fixup! Fix static checks

---------
(cherry picked from commit c97d1a5)

Co-authored-by: Ephraim Anierobi <splendidzigy24@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Rahul Vats <43964496+vatsrahul1001@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API area:task-sdk backport-to-v3-2-test Mark PR with this label to backport to v3-2-test branch type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants