Hi all, I want to discuss a scheduler problem with partitioned assets, and I propose two changes to fix it. I need your opinion, mainly on the design for 3.5.
SHORT VERSION - Since 3.3.0, 500 pending partition Dag runs that wait by design can stop all newer runs. These runs never start, in all Dags. - The 3.4.1 fix (PR #76463) makes the scheduler examine all pending runs in turn. It has no migration. - The 3.5 design (PR #76706) makes the scheduler examine a pending run only when something changes for it. 1. THE PROBLEM Since Airflow 3.3.0 (PR #64571), each scheduler loop examines only the 500 oldest pending partition-driven Dag runs. Some pending runs wait by design. For example: - a daily rollup that waits for the last hours of the day; - a Dag scheduled on "A & B" that waits for B. When 500 runs like these exist, they use all 500 places in every loop. The scheduler then never examines newer runs, even when their data is complete. These runs never start, in all Dags. No log message and no metric shows the problem. The limit of 500 is a constant in the code, so users cannot change it. On MySQL, the same query also locks all pending rows, not only 500. As a result, a second scheduler cannot claim runs. Issue #76979 shows a related problem. A user changed a Dag from "A" to "A & B", and a pending run never started. The usual workaround is to publish B again. With 500 or more pending runs that wait, this workaround also fails. 2. THE FIX FOR 3.4.1 (PR #76463) This fix has no migration, no new option and no API change. - The scheduler examines all pending runs in turn, 500 in each loop. It does not stay on the 500 oldest. - It keeps the IDs of the pending runs in memory, about 100 bytes for each run. - It reads all the IDs every 30 seconds. Between these reads, it reads only the new rows. - New runs go first, up to half of each loop. A new run usually starts in the next loop. - The scheduler locks rows by ID with SKIP LOCKED, a maximum of 500 in each loop, on all databases. As a result, two schedulers claim different runs. - New metrics show the number of loops for one full rotation, and the age of the oldest pending run. There is one change in behaviour. When more than 500 runs are pending, Dag runs start in rotation order, not oldest first. Cost on PostgreSQL with 55,000 pending runs: - on main, the claim takes about 130 to 140 ms in each loop; - with this fix, it takes about 25 ms, and about 270 ms once every 30 seconds for the full read. 3. THE NEW DESIGN FOR 3.5 (PR #76706) The 3.4.1 fix still examines every pending run again and again, even when nothing changed for it. The new design examines a pending run only when a change can make it ready. - A new table holds requests to examine one pending run, or all pending runs of a Dag. - Three events write a request, in the same transaction as the change: - a new partition key log for the run, from the execution API, the REST API or the triggerer; - a change to the timetable of the Dag; - the activation of an asset that the Dag uses. - The writers only insert rows, so a producer task never waits for the scheduler. - A resync pass examines all pending runs at startup, then every hour by default. A new option controls the interval: [scheduler] asset_partition_resync_interval. This pass is a safety net for changes that write no request. Results on PostgreSQL with 55,000 pending runs: - the claim of an idle loop takes about 5 ms, against 130 to 140 ms on main; - the full scheduler loop takes about 250 ms on main, 210 ms with the 3.4.1 fix and 80 ms with the new design. Costs and limits: - It adds a table and a migration. - Some changes write no request, so they wait for the next resync pass. Examples: - key logs from a bulk insert; - components that still run 3.4 during an upgrade; - a custom mapper that reads the clock. - If every pending run has a request, a loop can cost more than on main. This continues until the scheduler has read all the requests, at 500 runs in each loop. - An upgrade needs no backfill. The startup pass of each scheduler examines the runs that were pending before the upgrade. 4. HOW WE TESTED We used a local deployment: API server, Dag processor, scheduler and LocalExecutor, on PostgreSQL 16 and MySQL 8.0. We used real Dags: - a Dag scheduled on "A & B"; - a daily rollup of hourly partitions; - a Dag scheduled on one asset; - producer Dags whose task emits a partition key. The events came from the REST API and from real producer task runs. We ran each scenario on main and on each PR: - 600 runs wait by design, then new runs become ready. On main, no run starts in 100 loops. With the 3.4.1 fix, all start in less than 0.6 seconds. With the 3.5 design, all start in about 1 second. - 1,500 ready runs are behind 600 runs that wait. On main, none starts. With each PR, all start in about 15 seconds. - Two schedulers have 1,500 ready runs behind 1,200 runs that wait. On main, none starts on PostgreSQL, and only 500 start on MySQL. With each PR, all start in 9 to 11 seconds, and no run starts two times. - The runs that wait belong to a paused Dag, and then we unpause the Dag. On main, the ready runs stop. With each PR, they continue to start. - Upgrade, for the 3.5 design only: we let main block the runs, then we upgraded. The startup pass started the blocked runs about 2 seconds after the scheduler started. - Issue #76979: neither PR links the old B event to the new condition. But with each PR, the workaround works: when you publish B again, the run starts. The unit tests of both PRs pass on SQLite, PostgreSQL and MySQL. We measured the cost with 1,050,000 partition rows on PostgreSQL and on MySQL. Links: - Issue #76979: https://github.com/apache/airflow/issues/76979 - PR #76463, the fix for 3.4.1: https://github.com/apache/airflow/pull/76463 - PR #76706, the design for 3.5: https://github.com/apache/airflow/pull/76706 - PR #64571, the change in 3.3.0: https://github.com/apache/airflow/pull/64571 Thanks, Hussein
