This is an automated email from the ASF dual-hosted git repository.
vatsrahul1001 pushed a commit to branch v3-4-test
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/v3-4-test by this push:
new f58dc17c02f [v3-4-test] Load consumed asset events of a run in bulk
when a task starts (#76513) (#76680)
f58dc17c02f is described below
commit f58dc17c02f51628c1513d70fa6cae01faae67f8
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Nov 25 17:13:05 2026 +0530
[v3-4-test] Load consumed asset events of a run in bulk when a task starts
(#76513) (#76680)
`ti_run` returns the asset events the Dag run consumed, with the asset
and the source aliases of each event. It loaded the events, but not
those two relationships, so building the response queried the database
once per event for each of them. With tens of thousands of consumed
events, the call took longer than the client timeout, and the task
failed before it started.
(cherry picked from commit 54f9083f75396dbbdfceaae44a9f04994c0bf2b1)
Co-authored-by: Andrew Chang <[email protected]>
---
.../execution_api/routes/task_instances.py | 5 ++-
.../versions/head/test_task_instances.py | 47 ++++++++++++++++++++++
2 files changed, 50 insertions(+), 2 deletions(-)
diff --git
a/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py
b/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py
index a20580ece0a..7670774d42d 100644
---
a/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py
+++
b/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py
@@ -93,7 +93,7 @@ from
airflow.api_fastapi.execution_api.services.task_instances import (
from airflow.api_fastapi.execution_api.versions.v2026_10_30 import
IdentifyArchivedTaskStateUpdates
from airflow.configuration import conf
from airflow.exceptions import InvalidPartitionKeyError, TaskNotFound
-from airflow.models.asset import AssetActive
+from airflow.models.asset import AssetActive, AssetEvent
from airflow.models.base import ID_LEN
from airflow.models.dag import DagModel
from airflow.models.dagbag import DBDagBag
@@ -319,7 +319,8 @@ def ti_run(
select(DR)
.filter_by(dag_id=ti.dag_id, run_id=ti.run_id)
.options(
- joinedload(DR.consumed_asset_events),
+
joinedload(DR.consumed_asset_events).selectinload(AssetEvent.asset),
+
joinedload(DR.consumed_asset_events).selectinload(AssetEvent.source_aliases),
*eager_load_teams(DR.dag_model),
*dag_tag_options,
)
diff --git
a/airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py
b/airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py
index 336d7c28592..965488774c6 100644
---
a/airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py
+++
b/airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py
@@ -793,6 +793,53 @@ class TestTIRunState:
events = response.json()["dag_run"]["consumed_asset_events"]
assert [e["partition_key"] for e in events] == ["2024-01-15"]
+ def test_ti_run_loads_consumed_event_assets_and_aliases_in_bulk(
+ self, client, session, create_task_instance
+ ):
+ ti = create_task_instance(
+ task_id="test_consumed_events_in_bulk",
+ state=State.QUEUED,
+ session=session,
+ )
+ assets = [
+ AssetModel(name=f"upstream_{i}", uri=f"s3://bucket/upstream_{i}",
group="asset", extra={})
+ for i in range(3)
+ ]
+ alias = AssetAliasModel(name="upstream_alias", group="asset")
+ session.add_all([*assets, *(AssetActive.for_asset(asset) for asset in
assets), alias])
+ session.flush()
+ for asset in assets:
+ event = AssetEvent(asset_id=asset.id)
+ event.source_aliases.append(alias)
+ ti.dag_run.consumed_asset_events.append(event)
+ session.commit()
+
+ with (
+ capture_orm_selects("asset") as asset_selects,
+ capture_orm_selects("asset_alias") as alias_selects,
+ ):
+ response = client.patch(
+ f"/execution/task-instances/{ti.id}/run",
+ json={
+ "state": "running",
+ "hostname": "h",
+ "unixname": "u",
+ "pid": 1,
+ "start_date": "2024-09-30T12:00:00Z",
+ },
+ )
+
+ assert response.status_code == 200
+ events = response.json()["dag_run"]["consumed_asset_events"]
+ assert sorted(event["asset"]["name"] for event in events) == [
+ "upstream_0",
+ "upstream_1",
+ "upstream_2",
+ ]
+ assert [event["source_aliases"] for event in events] == [[{"name":
"upstream_alias"}]] * 3
+ assert len(asset_selects) == 1
+ assert len(alias_selects) == 1
+
def test_ti_run_missing_dagrun_returns_404(self, client, session,
create_task_instance):
"""A missing DagRun must surface as a clean 404, not an internal
500."""
ti = create_task_instance(