Skip to content

Migrate asset state store API endpoint to an async db session - #74540

Draft
Prab-27 wants to merge 3 commits into
apache:mainfrom
Prab-27:async-assets-state-store
Draft

Prab-27 wants to merge 3 commits into
apache:mainfrom
Prab-27:async-assets-state-store

Conversation

@Prab-27

@Prab-27 Prab-27 commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Convert Asset state store — routes/asset_state_store.py endpoint to AsyncSessionDep

related : #67799


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

  • Read the 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) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@Prab-27 Prab-27 changed the title Migrate asset state store to an Async db session Migrate asset state store API endpoint to an async db session Oct 10, 2026
@Prab-27
Prab-27 force-pushed the async-assets-state-store branch from fcf3abb to a0d2de8 Compare October 11, 2026 03:36
@Prab-27
Prab-27 marked this pull request as ready for review October 11, 2026 06:04

@Dev-iL Dev-iL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I asked my agent to review the PR using the airflow-async-endpoints skill. TL;DR - the changes are good, with a couple of performance-related suggestions.

I'm attaching the review report (review.md), including helper scripts (*.py) and instructions on how to reproduce it locally: pr74540-review.zip

@@ -138,28 +143,28 @@ def get_asset_state_store_by_name(
return AssetStateStoreResponse(value=json.loads(value))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Converting this handler to async def moves json.loads(value) and AssetStateStoreResponse validation from FastAPI's worker thread onto the server event loop. With a stored 100,000-integer array (688,890 JSON bytes), two SQLite benchmark rounds increased median event-loop delay from 110–113 ms on the base to 152–155 ms here. Offloading parsing and response-model construction together reduced it to 112–129 ms; the remaining delay includes existing framework serialization. Larger stored values therefore delay other requests handled by the same loop.

Suggestion: move parsing and response-model construction into run_in_threadpool after the awaited backend read, and apply the same change to the URI handler at asset_state_store.py:235.

asset_id = _resolve_asset_id_by_name(name, session)
value = get_state_backend().get(AssetScope(asset_id=asset_id), key, session=session)
asset_id = await _resolve_asset_id_by_name(name, session)
value = await get_state_backend().aget(AssetScope(asset_id=asset_id), key, session=session)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Both converted GET routes call MetastoreBackend._aget_asset_state_store at state/metastore.py:551, which selects the entire 13-column AssetStateStoreModel but returns only row.value.

Suggestions: select AssetStateStoreModel.value and return the scalar directly, keeping the existing filters and session. The value column is non-null, so missing keys still return None. A comparative HTTP probe on SQLite, PostgreSQL, and MySQL preserved the returned values and missing-key responses while reducing selected columns from 13 to 1 and ORM loads from one to zero per successful GET. Apply this to both selectors through the shared helper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Dev-iL , Got it!
Apologies for the delay - I'll work on these updates and aim to wrap them up in a couple of days

@Prab-27 Prab-27 closed this Nov 1, 2026
@Prab-27 Prab-27 reopened this Nov 1, 2026
@github-actions github-actions Bot added the fork ci required Gated by AIP-120: CI must pass in the contributor's fork. Managed by the fork CI reconciler. label Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fork CI: waiting for a push. The workflow is enabled in your fork, but it has not run for the latest commit of this pull request. Push the branch again to start it. If you already pushed, verify in your fork's Actions tab that workflows are not disabled by GitHub's fork banner.

Pull requests from contributors run their CI in the contributor's own fork. GitHub Actions is free and has no minute limit on public forks, while the CI capacity of the Apache Software Foundation is shared by every ASF project and is finite.

How this works: CI in your fork.


This comment was posted automatically by the fork CI monitor from templates drafted with an AI-assisted tool, so it may contain mistakes. An Apache Airflow maintainer, a real person, will review the pull request once its fork CI is green. Why the first pass is automated.

@github-actions
github-actions Bot marked this pull request as draft November 1, 2026 04:20
@Prab-27
Prab-27 force-pushed the async-assets-state-store branch 2 times, most recently from aa8cfb7 to 446150d Compare November 3, 2026 03:55
@github-actions github-actions Bot added the ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. label Nov 3, 2026
@github-actions
github-actions Bot marked this pull request as ready for review November 3, 2026 04:50
@Prab-27
Prab-27 marked this pull request as draft November 3, 2026 16:47
@github-actions github-actions Bot removed the ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. label Nov 10, 2026
@Prab-27
Prab-27 force-pushed the async-assets-state-store branch from 446150d to 3966b3c Compare November 27, 2026 13:25
@Prab-27
Prab-27 marked this pull request as ready for review November 27, 2026 13:26
@github-actions
github-actions Bot marked this pull request as draft November 27, 2026 13:27
@Prab-27

Prab-27 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Do we need to create a async decorator for this async function ?

def allow_asset_watcher(endpoint: Callable[_P, _R]) -> Callable[_P, _R]:

(I discussed with claude but didn't understand !) Would love to hear from you !

@dabla dabla left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Addressed

  • Selecting only value in _aget_asset_state_store (thread r4280952204): done in 3966b3c517; the backend tests in airflow-core/tests/unit/state pass on this head.

Open

  • Comment [1]: both GET routes return the undecoded JSON string; 9 route tests fail.
  • Comments [2] and [3]: nits.
  • JSON decoding and response validation on the event loop (thread r4280775014): no reply and no change yet. With aget_value the decoding still runs on the loop, so the point stands as raised.
  • Your question about allow_asset_watcher: no async variant is needed. It does not wrap the endpoint; it sets an attribute on the function and returns the same function, so FastAPI still sees the async def and awaits it. The six decorated routes pass their tests, the watcher and the rejected-identity cases included.
  • CI has not run on this head: the fork CI is waiting for a push.

Reviewed at 3966b3c517 from the diff plus the full source of both files and the shared backend base class. Each sync call was matched to its async twin (set/aset, set_asset_state_store/aset_asset_state_store, delete/adelete, clear/aclear agree in arguments and writer fields; get_value/aget do not, comment [1]). Tests run locally on SQLite: the route tests (34 passed, 9 failed) and, with the fix of comment [1] applied, the route tests plus airflow-core/tests/unit/state (110 passed, 3 skipped). Not covered: PostgreSQL and MySQL, and the event-loop delay measurement from the earlier review was not repeated.

asset_id = _resolve_asset_id_by_name(name, session)
if (value := get_state_backend().get_value(AssetScope(asset_id=asset_id), key, session=session)) is None:
asset_id = await _resolve_asset_id_by_name(name, session)
if (value := await get_state_backend().aget(AssetScope(asset_id=asset_id), key, session=session)) is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[blocker] aget returns the stored JSON string, get_value returned the decoded value: both GET routes now answer with the wrong value.

main calls get_value(), which decodes what get() returns. aget() is the async twin of get(), not of get_value(), so the response now carries the raw string ({"value": "\"2026-04-29\""} instead of {"value": "2026-04-29"}, a dict comes back as a string), and a stored JSON null answers 200 instead of 404. The async twin already exists: aget_value. It also keeps the override point for a backend that stores values natively.

if (value := await get_state_backend().aget_value(AssetScope(asset_id=asset_id), key, session=session)) is None:

The same change is needed in get_asset_state_store_by_uri (line 233). On this head, 9 of the 43 tests in test_asset_state_store.py fail (test_get_returns_value, test_get_stored_null_returns_404, test_get_asset_name_with_slashes, the four test_put_*_roundtrip / test_put_overwrites_existing, for by-name and by-uri); with aget_value in both handlers all 43 pass.

"""Return exact writer attribution for the execution identified by the token."""
row = session.execute(
async def _fetch_ti_writer_fields(token: TIToken, session: AsyncSessionDep) -> _TIWriterFields:
"""Return (dag_id, run_id, task_id, map_index) for the TI identified by the token."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[nit] The rebase brought back an older docstring.

main says "Return exact writer attribution for the execution identified by the token." The function returns eight fields (task_instance_id, region_id, region_index and try_number included), so the four-field list is no longer true. Keep the docstring of main.

) -> str | None:
row = await session.scalar(
select(TaskStateStoreModel).where(
select(TaskStateStoreModel.value).where(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[nit] Three of the four readers now select only value; the sync _get_task_state_store (line 249, not in the diff) still loads the whole row.

Either change that one too, so the sync and async task readers stay the same, or leave the task-state readers out of this PR, which is about the asset state store routes. In the three changed readers row now holds the value itself, so return await session.scalar(...) (and return session.scalar(...)) says it without the misleading name.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk fork ci required Gated by AIP-120: CI must pass in the contributor's fork. Managed by the fork CI reconciler.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants