vladosh84 opened a new issue, #77493:
URL: https://github.com/apache/airflow/issues/77493
### Under which category would you file this issue?
Airflow Core
### Apache Airflow version
3.4.0
### What happened and how to reproduce it?
In 3.4.0, KubernetesPodOperator writes the submitted pod's identity to
`task_state_store`
(`_persist_pod_identity_to_task_state_store`). The worker calls
`PUT /execution/store/ti/{ti_id}/pod_identifier`, and the API server runs a
MySQL upsert
(`INSERT ... ON DUPLICATE KEY UPDATE`, from `airflow/state/metastore.py`).
`task_state_store.dag_run_id` has a foreign key to `dag_run.id`. On InnoDB
the FK check takes a shared
lock on the parent `dag_run` row. The scheduler loop takes exclusive locks
on the `dag_run` rows it
processes (`SELECT ... FOR UPDATE SKIP LOCKED`, then `UPDATE dag_run SET
last_scheduling_decision ...`)
and holds them until it commits at the end of the iteration. Any KPO task of
a DAG run the scheduler is
working on waits for that commit.
The upsert itself examines 0 rows, and all of its time is lock wait. From
`performance_schema`/slow-log digests over one hour on our production
database:
| Statement | Calls | Total query time | Total lock time | Avg |
|---|---|---|---|---|
| `INSERT INTO task_state_store ... ON DUPLICATE KEY UPDATE` | 7,952 |
22,375 s | 22,388 s | 2.8 s |
| All other statements (5.33M calls) | | about 3,200 s | about 90 s | about
7 rows examined per query |
So `task_state_store` is about 99.6% of all lock time on the metadata DB.
Over a full day the hourly
average stayed between 2.9 and 4.1 s per upsert, at about 7,800 upserts per
hour.
`sys.innodb_lock_waits` during a burst: 20 waiters on `dag_run` PRIMARY, all
blocked by one connection
holding `X,REC_NOT_GAP`. That connection belonged to the scheduler, with a
17 s old transaction and the
connection in `Sleep`. Its statement history was `SELECT ... FROM dag_run
...` →
`UPDATE dag_run SET last_scheduling_decision = ...` → (idle) → `COMMIT`. The
scheduler's queries
finish in milliseconds, but the row locks are held while the scheduler does
in-process work between
them until the loop commits.
Effects:
- With the default `[workers] execution_api_timeout` (5 s), the PUT timed
out on the worker
(`ReadTimeout`), and KPO task tries failed.
- In bursts the API server's DB connection pool ran out, because connections
were parked in lock waits.
Execution API calls then failed after the pool timeout with `QueuePool
limit of size 5 overflow 10
reached, connection timed out` (HTTP 500), including some that waited 50 s
(`innodb_lock_wait_timeout`).
- Scheduler loop p99 went from about 7 s on 3.3.2 to 13–18 s on 3.4.0. The
mean was unchanged at about
0.8 s.
The table size of `dag_run`/`task_instance` is not the cause: the FK check
is a primary-key lookup, and
the scheduler's own queries examine a handful of rows each. The wait is
bounded by how long the
scheduler keeps its transaction open.
**How to reproduce**
1. Airflow 3.4.0, MySQL 8.4, CeleryExecutor,
apache-airflow-providers-cncf-kubernetes 10.25.0.
2. Have many active DAG runs (hundreds) with KubernetesPodOperator tasks, so
the scheduler loop takes
seconds.
3. Watch `performance_schema.data_lock_waits` / `sys.innodb_lock_waits`: KPO
upserts into
`task_state_store` wait on `dag_run` PRIMARY rows held by the scheduler's
connection.
### What you think should happen instead?
Writing task state from a running task should not have to wait for the
scheduler's transaction on the
same DAG run. Possible directions:
- drop the FK from `task_state_store.dag_run_id` to `dag_run.id` (or don't
enforce it at the DB level)
and clean up orphans in `airflow db clean`;
- key `task_state_store` on the task instance instead of the DAG run, so the
parent row is not one the
scheduler locks;
- shorten the scheduler's hold on `dag_run` rows (commit before the non-DB
work in the loop);
- let KubernetesPodOperator skip persisting the pod identifier when it isn't
needed.
### Operating System
Debian 13 (trixie), Python 3.14.10
### Deployment
Official Apache Airflow Helm Chart
### Apache Airflow Provider(s)
cncf-kubernetes, mysql
### Versions of Apache Airflow Providers
apache-airflow-providers-cncf-kubernetes==10.25.0
apache-airflow-providers-mysql==6.7.1
apache-airflow-providers-common-sql==2.4.0
apache-airflow-providers-celery==3.26.0
### Official Helm Chart version
1.22.0 (latest released)
### Kubernetes Version
1.35.9
### Helm Chart configuration
Used as a subchart. CeleryExecutor, 2 schedulers, external MySQL 8.4.9
(Azure Database for MySQL Flexible Server, InnoDB, REPEATABLE READ,
innodb_autoinc_lock_mode = 2). Default [workers] execution_api_timeout (5 s)
when the failures were seen.
### Docker Image customizations
Custom image on Debian 13 with apache-airflow 3.4.0 / task-sdk 1.4.0,
SQLAlchemy 2.0.55, mysqlclient 2.3.0, celery 5.6.3, kubernetes (Python client)
36.0.3.
### Anything else?
Mitigations we applied:
- `[workers] execution_api_timeout = 30` stops the worker-side timeouts. It
doesn't reduce the waits.
- `[scheduler] max_dagruns_per_loop_to_schedule = 5` (default 20). Comparing
the hour after the change
with the hour before, average lock wait per upsert went from 2.8 s to 2.0
s (22,388 s over 7,952
calls → 15,223 s over 7,440), and scheduler loop p99 from 15.3 s to 13.3
s. It helps, but upserts
still wait about 2 s.
Code at 3.4.0:
- `providers/cncf/kubernetes/.../operators/pod.py`:
`_persist_pod_identity_to_task_state_store`
- `airflow-core/src/airflow/state/metastore.py`: MySQL upsert
(`on_duplicate_key_update`)
### Are you willing to submit PR?
- [ ] Yes I am willing to submit a PR!
### Code of Conduct
- [x] I agree to follow this project's [Code of
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
--
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]