diogosilva30 opened a new issue, #77497:
URL: https://github.com/apache/airflow/issues/77497
### Under which category would you file this issue?
Providers
### Apache Airflow Provider(s)
edge3
### Versions of Apache Airflow Providers
apache-airflow-providers-edge3 5.1.0 (the same code is on `main`, 5.2.0)
### Apache Airflow version
3.4.0
### What happened and how to reproduce it?
With two schedulers on MySQL 8.4 (`READ-COMMITTED`) and about 7,500 task
completions per hour, each scheduler exits every few hours. I confirmed four
such exits in two days, all from the same call path:
```
MySQLdb.OperationalError: (1213, 'Deadlock found when trying to get lock;
try restarting transaction')
SchedulerJobRunner._run_scheduler_loop
BaseExecutor.heartbeat -> EdgeExecutor.sync
EdgeExecutor._purge_jobs -> _delete_job
[SQL: DELETE FROM edge_logs WHERE edge_logs.dag_id = %s AND edge_logs.run_id
= %s AND edge_logs.task_id = %s AND edge_logs.map_index = %s AND
edge_logs.try_number = %s AND NOT (EXISTS (SELECT edge_job.dag_id ...
```
Nothing in `sync()` handles the error, so it escapes `heartbeat` and the
scheduler process exits 1 and restarts.
With `innodb_print_all_deadlocks` on I captured one report (trimmed; names
replaced). Two transactions, one per scheduler, each running the statement
above for a different job of a different Dag:
- TX1: `DELETE FROM edge_logs ... AND NOT EXISTS (SELECT ... FROM edge_job
WHERE <same key> AND task_instance_id != <ti>)` for `dag_a / task_a`, try 2. 98
lock structures, 406 row locks. Holds exclusive locks on `edge_job` rows. Waits
for a **shared** lock (`lock mode S locks rec but not gap`) on an `edge_job`
record of the same task's later run.
- TX2: the same statement for `dag_b / task_b`, try 1. 95 lock structures,
350 row locks. Holds the exclusive lock TX1 waits for, and waits for a shared
lock on an `edge_job` row held by TX1.
- MySQL rolled back TX2.
**What I think is happening (from this one report; not reproduced):**
1. `_purge_jobs` selects every purge-eligible `edge_job` row with `FOR
UPDATE SKIP LOCKED` and keeps those exclusive locks until `sync()` commits, so
each scheduler holds hundreds of rows. With two schedulers the claimed sets
interleave in primary-key order (adjacent runs of one task are neighbours).
2. The `NOT EXISTS (SELECT ... FROM edge_job ...)` subquery inside the
`DELETE` takes shared locks on `edge_job` rows even under `READ-COMMITTED`, and
its range scan reaches the record just past its own key range. That record
belongs to a job the other scheduler has locked.
3. Each scheduler waits on a row the other holds.
The more jobs a `sync()` purges, the longer the locks are held and the more
likely the cycle.
**Possible fixes:**
- Do the sibling check with a plain `SELECT` (no locks) and delete the
`edge_logs` rows by key. A plain `SELECT` can't see a sibling that another
scheduler deleted but has not committed, so the logs of two siblings purged by
different schedulers could both be kept; a recheck after commit covers that.
- Roll back and retry on the next heartbeat when `sync()` hits a deadlock,
instead of exiting (the work is idempotent and polled every few seconds).
- Serialize the purge across schedulers, for example with an advisory lock,
so the purges never run concurrently. This removes the concurrency but needs a
lock id and a non-blocking acquire, which core's `create_global_lock` does not
offer today.
A candidate for the first two is in #77496. I have not reproduced the
deadlock with two schedulers in a test, so I would like maintainers' view on
the lock analysis and on which direction fits before it merges.
(`revoke_task` also calls `_delete_job` under a blocking `with_for_update()`
and has the same exposure.)
### What you think should happen instead?
A deadlock in the executor's periodic purge should not take the scheduler
down, and two schedulers purging should not deadlock each other.
### Operating System
Linux (container)
### Deployment
Other 3rd-party Helm chart
### Deployment details
Kubernetes, 2 schedulers, EdgeExecutor with about 60 edge workers, Cloud SQL
for MySQL 8.4 (1 vCPU, 3.75 GB), `READ-COMMITTED`.
### Anything else?
Analysis prepared with Claude Code.
--
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]