Skip to content

Migrate Execution API asset event routes to async DB session - #77736

Open
RehanAhmad25 wants to merge 2 commits into
apache:mainfrom
RehanAhmad25:async/67799-asset-events-routes
Open

RehanAhmad25 wants to merge 2 commits into
apache:mainfrom
RehanAhmad25:async/67799-asset-events-routes

Conversation

@RehanAhmad25

Copy link
Copy Markdown
Contributor

Related: #67799

Converts the two routes in execution_api/routes/asset_events.py to async:

  • GET /asset-events/by-asset - get_asset_event_by_asset_name_uri
  • GET /asset-events/by-asset-alias - get_asset_event_by_asset_alias

What changed

  • Routes and the shared helper are async def with AsyncSessionDep. The query adds selectinload for AssetEvent.asset and AssetEvent.created_dagruns, since both are lazy and the response reads them. Full entities are kept because the response needs every event field, four asset fields and the dag run list.
  • The partition_key_regexp_pattern filter's dependency took a sync SessionDep and applied apply_regex_query_timeout to it. Left as is, the async route would run its query on a different session than the one carrying the timeout. Added async counterparts next to the sync ones: apply_regex_query_timeout_async, aregex_param_factory and QueryAssetEventPartitionKeyRegexAsync. The core API keeps using the sync versions. Pattern validation moved into a small shared _build_regex_param so both factories behave the same.

Tests

  • Existing contract tests for head and 2025-09-23 unchanged and passing.
  • New: no sync SQL on the event loop for both routes (with and without a regex pattern).
  • New (postgres/mysql only): the session that runs the asset_event query is the one that had the timeout set.
  • New unit tests for apply_regex_query_timeout_async and aregex_param_factory, mirroring the sync ones.
Backend Async driver Result
SQLite sqlite+aiosqlite 451 passed, 4 skipped
PostgreSQL 14 postgresql+psycopg_async 455 passed
MySQL 8.0 mysql+asyncmy 455 passed

Command: pytest on the two test_asset_events.py files, common/test_parameters.py, utils/test_sqlalchemy.py and core_api/routes/public/test_assets.py.

Note

A regex request opens two sessions: Cadwyn solves the dependency once on its own and once sharing the route's session. I checked the base revision and it does the same with the sync dependency, so this PR keeps parity; the query is still covered by the timeout. Happy to look at skipping the first pass in a follow-up if that's wanted.

I haven't run the event loop stall harness from the SKILL. These routes return all events by default, so I can add a measurement if reviewers want one.


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

Generated-by: Claude (Anthropic) following the guidelines

@RehanAhmad25

Copy link
Copy Markdown
Contributor Author

@Dev-iL could you approve the workflow runs when you get a chance? Thanks!

@Dev-iL

Dev-iL commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

@Dev-iL could you approve the workflow runs when you get a chance? Thanks!

I believe thst require a maintainer. My abilities are limited to adding/removing tags.

@RehanAhmad25

Copy link
Copy Markdown
Contributor Author

@Dev-iL could you approve the workflow runs when you get a chance? Thanks!

I believe thst require a maintainer. My abilities are limited to adding/removing tags.

Got it, thanks for clarifying! Would also appreciate a review whenever you have time.

@RehanAhmad25

Copy link
Copy Markdown
Contributor Author

Did a self-review pass and pushed a few fixes in 669c61f:

  • The timeout restore no longer hides the original DB error when the query fails. On Postgres the failed query has already aborted the transaction, so the restore failed too and replaced the real error (e.g. the statement timeout itself). The restore is still attempted, but a failure there is swallowed so the original exception propagates. On Postgres the setting is transaction-local and goes away with the rollback; on MySQL the restore still runs normally, since max_execution_time stays on the pooled connection.
  • A tiny positive regexp_query_timeout (e.g. 0.0004) no longer rounds down to 0 ms, which would disable the timeout on both backends. It's clamped to 1 ms now.
  • The sync apply_regex_query_timeout had the same two issues, so I fixed both helpers the same way. Happy to split the sync change into its own PR if you'd prefer.
  • Renamed aregex_param_factory to regex_param_factory_async to match the _async naming used elsewhere (create_session_async, get_query_count_async, paginated_select_async).
  • Tightened the tests: the session test now keys statements by the session object instead of id() (so a closed session's id can't be reused by the next one), and checks the timeout is set before the asset_event query and restored after it. Added unit tests for the failed-restore and sub-ms cases.

Re-tested:

Backend Driver Result
SQLite sqlite+aiosqlite 457 passed, 4 skipped
PostgreSQL 14 psycopg_async 461 passed
MySQL 8.0 asyncmy 461 passed

test_event_loop_blocking.py: 4 passed, 22 xfailed (same as before, all pre-existing).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants