Repository navigation
Conversation
|
Fork CI: green. CI for the latest commit passed in your fork. The pull request is ready for maintainer review. 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. Fork CI tested this branch merged with Latest run: https://github.com/Dev-iL/airflow/actions/runs/44622540938 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. |
ashb
left a comment
There was a problem hiding this comment.
Shouldn't the same treatment be applied to XCom responses as well?
|
The pydantic docs say this https://pydantic.dev/docs/validation/latest/concepts/performance/#dont-do-validation-when-you-dont-have-to-use-any-to-keep-the-value-unchanged
|
Yes, this is noted in the description under the "Not in this PR" section:
I agree the same cost most likely applies. I scoped this PR to the state stores because those are the routes I measured. I’d like to measure the XCom response paths and check their value sources before applying the same treatment in a follow-up.
I tried plain |
Pydantic checks and serializes a JsonValue field by calling a Python function for every element of the value, and a state store request runs that walk several times: FastAPI validates the body, cadwyn dumps and re-validates it for the version migration, and a read validates and serializes the response. For a value of a few hundred kilobytes this costs tens of milliseconds of CPU per request, and because the framework does this work on the event loop, it holds up every other request the API server is handling, even though the routes themselves are synchronous. These values are already decoded JSON, either a request body or a value the state backend decoded from storage, so the walk has nothing to reject. Passing them through unchanged removes that cost and keeps the published OpenAPI schema, and therefore the generated Task SDK models, the same. related: apache#67799
4446eb0 to
99ad649
Compare
related: #67799
A task state store or asset state store request with a large value spends most of its CPU time in pydantic's checks on that value, not in the database. This PR adds a
DecodedJsonValuetype for already-decoded JSON and uses it for thevaluefield of the four Execution API state store models:TaskStateStoreResponse,TaskStateStorePutBodyAssetStateStoreResponse,AssetStateStorePutBodyFor a 689 KB value, the median event loop stall drops by 70–85% (GET 126–153 ms → 34–35 ms, PUT 81–90 ms → 12–24 ms), and the median request duration by 39–68%. The published OpenAPI schema does not change, so the generated Task SDK models and the HTTP contract stay the same.
Details
JsonValuewith a Python discriminator function (pydantic.types._get_type_name) that it calls once for every element, and it walks the value again to serialize it. A state store request does this several times: FastAPI validates the body, cadwyn dumps and re-validates it for the version migration, and a read validates and serializes the response. For a 100,000-element value, the loop-thread profile at the measured base below shows 400,004 calls for a GET and 300,003 for a PUT. Onmainthese routes are synchronous, but this framework work runs on the event loop, so it already blocks every other request there.DecodedJsonValuewraps theJsonValueschema with a validator that returns the value unchanged and a serializer that passes it through. The JSON schema is generated from the wrappedJsonValue, so the field still references theJsonValuecomponent. The OpenAPI document of the Execution API app is identical before and after this change, and thegenerate-tasksdk-datamodelshook produces no diff.nulland NaN values still run.to_pythonin JSON mode, which no longer calls back into Python, and the stdlibjsonencoder. Each takes about 14 ms for a 689 KB value under the profiler.Behaviour differences:
get_valuereturns. With the built-in backends, this is the decoded JSON from storage. A custom backend that overridesget_valueand returns a Python object that is not JSON, such as adatetime, got a 500 before, because the response model rejected it. Now pydantic serializes it by inferring its type, as it does for anAnyfield (adatetimebecomes an ISO 8601 string). I left this unguarded becauseget_valueis typed to returnJsonValue, so such a backend already breaks its contract.Not in this PR:
execution_api/datamodels/xcom.py) and the public API state store models have the sameJsonValuefields. I want to measure those routes before changing them, and I will open follow-ups.JsonValuePython serialization pydantic/pydantic#13995 ("Improve performance ofJsonValuePython serialization") on 2026-10-14. It is not in a release yet (the latest is 2.14.3), and it covers serialization only. Validation, which is most of the PUT cost, still walks every element.Benchmarks and Testing
Event-loop stall measurements
The harness is the one from #75381. Each request runs over HTTP through
TestClientfrom a separate thread. A 1 ms ticker on the server loop records its longest sleep per request. Every case asserts the status and the returned or stored value. 10 warm requests per case, cold request dropped, 2 rounds per version run interleaved in one container. Values are integer arrays.mainat5addde0337, with synchronous routes. This PR's parent (ce559e40ef) is 18 commits newer; none of them touchapi_fastapi, the state store code oruv.lock.airflow-core/srcdiff, applied unchanged.Each stall cell shows
median / maxin ms, and each duration cell the median request duration in ms, for round 1; round 2.Python 3.14.10, SQLAlchemy 2.0.57, FastAPI 0.162.0, cadwyn 8.2.0, pydantic 2.14.2, Breeze CI image. These are small local samples of event-loop availability, not throughput. Only the task state store routes were measured; the asset state store routes use the same type.
SQLite (aiosqlite)
PostgreSQL (psycopg async)
Small values do not change beyond noise. The PostgreSQL PUT rounds of this PR differ (81 ms and 161 ms for 689 KB); both are below base.
Tests
TestDecodedJsonValuehas three tests:test_validation_returns_decoded_containers_unchangedasserts that validation returns the same list or dict object.test_serialization_skips_the_json_value_checksserializes adatetimewith pydantic's serializer warnings turned into errors. PlainJsonValueproduces the same output but warns that the value is none of its members.test_model_field_publishes_the_json_value_schemacompares the JSON schema of a model field, in validation and serialization mode, with a plainJsonValuefield. The comparison has to be on a model field: for a bareTypeAdapter,AnyandJsonValuepublish the same schema.Each test fails under at least one of the implementations I tried and rejected:
DecodedJsonValuedefined asJsonValueAny(drops the$ref)SkipValidation[JsonValue](addstitle: Value)The existing route tests cover the null and NaN rejection and the round trip of stored values through both versions of the API.
Checks run on this revision, in Breeze with Python 3.14.10:
tests/unit/api_fastapi/common/test_types.py,execution_api/versions/head/test_task_state_store.py,execution_api/versions/head/test_asset_state_store.py,execution_api/versions/v2026_10_30/test_task_state_store.pyandexecution_api/test_app.py: 153 passed on each backend:sqlite/sqlite+aiosqlite)postgresql+psycopg/postgresql+psycopg_async)mysql/mysql+asyncmy)prek run mypy-airflow-coreon the changed files: no issues.prek run --stage pre-commiton the changed files: passed, includinggenerate-tasksdk-datamodelswith no diff.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Opus 5.5) following the guidelines
{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.