vladosh84 commented on issue #77237:
URL: https://github.com/apache/airflow/issues/77237#issuecomment-7101694673

   We hit the same cleanup on **MySQL 8.4** (InnoDB, REPEATABLE READ) after 
upgrading from 3.3.2 to 3.4.0. With about 15k task runs per hour, it showed up 
as lock contention more than as read cost.
   
   Slow-log digests for one 10-minute window, about an hour after the migration:
   
   | Statement | Calls | Total query time | Total lock time | Avg per call |
   |---|---|---|---|---|
   | `DELETE FROM rtif_v2 WHERE EXISTS (...)` | 4,350 | 953 s | 805 s | 0.22 s |
   | `SELECT rtif_v2.id FROM rtif_v2 WHERE EXISTS (...)` 
(`synchronize_session="fetch"`) | 4,350 | 308 s | 0 s | 0.07 s |
   | `DELETE FROM rtif_v1 WHERE dag_id = ? AND task_id = ? AND ...` (legacy 
table, same call) | 4,403 | 194 s | 189 s | 0.04 s |
   
   MySQL-specific points:
   
   - On InnoDB under REPEATABLE READ the `DELETE ... WHERE EXISTS` takes locks 
on the rows and gaps it reads, not only on the row it deletes. Cleanups for 
different tasks, and `rtif_v2` inserts from other tasks, wait on each other. 
Most of the DELETE's time was lock wait (805 s of 953 s).
   - The `fetch` synchronization adds a second statement with the same shape to 
every call.
   - Over one 15-minute window the DELETE ran about 10.8k times with about 
7,000 s of total lock wait. The task start/finish calls to the execution API 
exceeded the default `[workers] execution_api_timeout` (5 s), and tasks failed.
   
   Workaround that worked for us: `[core] 
num_dag_runs_to_retain_rendered_fields = 0`. `delete_old_records` then returns 
early. rtif DELETEs dropped from about 4,350 per 10 minutes to 0 and the task 
failures stopped. Rendered fields are then removed only by `airflow db clean`, 
through the FK cascade from `task_instance`.
   
   For the fix, it would help if the new query avoids a locking read over other 
tasks' rows on MySQL. For example: select the `task_instance.id`s to drop first 
(a plain read through the `task_instance` index), then `DELETE FROM rtif_v2 
WHERE task_instance_id IN (...)`, which locks only the rows it deletes. 
Dropping `synchronize_session="fetch"` would save the extra SELECT.
   
   Environment: Airflow 3.4.0 / task-sdk 1.4.0, official Helm chart 1.22.0 on 
Kubernetes, CeleryExecutor, MySQL 8.4.9 (Azure Database for MySQL Flexible 
Server), SQLAlchemy 2.0.55, mysqlclient 2.3.0, Python 3.14.
   


-- 
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]

Reply via email to