vincbeck commented on code in PR #75381:
URL: https://github.com/apache/airflow/pull/75381#discussion_r4668219239
##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/task_state_store.py:
##########
@@ -47,55 +49,63 @@
)
-def _get_task_scope_for_ti(task_instance_id: UUID, session: Session) ->
TaskScope:
- ti = session.get(TI, task_instance_id)
- if ti is None or ti.working_set is not True:
+async def _get_task_scope_for_ti(task_instance_id: UUID, session:
AsyncSession) -> TaskScope:
+ row = (
+ await session.execute(
+ select(TI.dag_id, TI.run_id, TI.task_id, TI.region_index,
TI.region_id).where(
+ TI.id == task_instance_id, TI.working_set.is_(True)
+ )
+ )
+ ).one_or_none()
+ if row is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={
"reason": "not_found",
"message": f"Task instance {task_instance_id} not found",
},
)
+ dag_id, run_id, task_id, region_index, region_id = row
return TaskScope(
- dag_id=ti.dag_id,
- run_id=ti.run_id,
- task_id=ti.task_id,
- map_index=ti.region_index,
- region_id=ti.region_id,
+ dag_id=dag_id,
+ run_id=run_id,
+ task_id=task_id,
+ region_index=region_index,
+ region_id=region_id,
)
@router.get("/{task_instance_id}/{key:path}")
-def get_task_state_store(
+async def get_task_state_store(
task_instance_id: UUID,
key: Annotated[str, Path(min_length=1)],
- session: SessionDep,
+ session: AsyncSessionDep,
) -> TaskStateStoreResponse:
"""Get value for a task state store key."""
- scope = _get_task_scope_for_ti(task_instance_id, session)
- if (value := get_state_backend().get_value(scope, key, session=session))
is None:
+ scope = await _get_task_scope_for_ti(task_instance_id, session)
+ if (value := await get_state_backend().aget_value(scope, key,
session=session)) is None:
Review Comment:
A custom backend that overrides only `get_value` to skip decoding now gets
the default `json.loads` here. Can you update `task-and-asset-state-store.rst`
(it still says the Execution API reads through `get_value`) to say task state
reads go through `aget_value`, so both need to be overridden?
##########
shared/state/tests/state/test_state.py:
##########
@@ -253,3 +257,25 @@ async def test_aget_value(self, stored, expected):
backend = _StoredValueBackend(stored)
assert await backend.aget_value(AssetScope(name="asset"), "key") ==
expected
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("stored", ['{"a": [1]}', "invalid"])
Review Comment:
nit: the `invalid` case only tests that `json.loads` raises, I would drop it
and remove the if/else from the test
##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/task_state_store.py:
##########
@@ -47,55 +49,63 @@
)
-def _get_task_scope_for_ti(task_instance_id: UUID, session: Session) ->
TaskScope:
- ti = session.get(TI, task_instance_id)
- if ti is None or ti.working_set is not True:
+async def _get_task_scope_for_ti(task_instance_id: UUID, session:
AsyncSession) -> TaskScope:
+ row = (
+ await session.execute(
+ select(TI.dag_id, TI.run_id, TI.task_id, TI.region_index,
TI.region_id).where(
+ TI.id == task_instance_id, TI.working_set.is_(True)
+ )
+ )
+ ).one_or_none()
+ if row is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={
"reason": "not_found",
"message": f"Task instance {task_instance_id} not found",
},
)
+ dag_id, run_id, task_id, region_index, region_id = row
return TaskScope(
- dag_id=ti.dag_id,
- run_id=ti.run_id,
- task_id=ti.task_id,
- map_index=ti.region_index,
- region_id=ti.region_id,
+ dag_id=dag_id,
+ run_id=run_id,
+ task_id=task_id,
+ region_index=region_index,
+ region_id=region_id,
)
@router.get("/{task_instance_id}/{key:path}")
-def get_task_state_store(
+async def get_task_state_store(
task_instance_id: UUID,
key: Annotated[str, Path(min_length=1)],
- session: SessionDep,
+ session: AsyncSessionDep,
) -> TaskStateStoreResponse:
"""Get value for a task state store key."""
- scope = _get_task_scope_for_ti(task_instance_id, session)
- if (value := get_state_backend().get_value(scope, key, session=session))
is None:
+ scope = await _get_task_scope_for_ti(task_instance_id, session)
+ if (value := await get_state_backend().aget_value(scope, key,
session=session)) is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={
"reason": "not_found",
"message": f"Task state store key {key!r} not found",
},
)
- return TaskStateStoreResponse(value=value)
+ # Validating a large value into the response model would block the event
loop for every other request.
+ return await run_in_threadpool(TaskStateStoreResponse, value=value)
@router.put("/{task_instance_id}/{key:path}",
status_code=status.HTTP_204_NO_CONTENT)
-def set_task_state_store(
+async def set_task_state_store(
task_instance_id: UUID,
key: Annotated[str, Path(min_length=1)],
body: TaskStateStorePutBody,
- session: SessionDep,
+ session: AsyncSessionDep,
) -> None:
"""Set a task state store key, creating or updating the row."""
- scope = _get_task_scope_for_ti(task_instance_id, session)
+ scope = await _get_task_scope_for_ti(task_instance_id, session)
try:
- get_state_backend().set(
+ await get_state_backend().aset(
Review Comment:
About the PUT stall you raised: I am fine leaving it as is since the cost
comes from the framework body handling and not from the route itself. Can you
create an issue to track it? It probably affects every async Execution API
route with a large `JsonValue` body.
--
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]