Abdel39 opened a new pull request, #76315:
URL: https://github.com/apache/airflow/pull/76315

   `SchedulerJobRunner._activate_referenced_assets` has two problems when 
several schedulers run `_update_asset_orphanage`. We hit both on a deployment 
with 4 scheduler replicas once #75404 made the orphanage query finish instead 
of timing out.
   
   **Unique violation on `asset_active_pkey`.** Activation does a plain 
`session.add(AssetActive.for_asset(asset))`. Assets that are only referenced 
through an alias association (#58058) are activated only by this pass, so when 
two schedulers run it at the same time, both insert the same row and the loser 
fails the whole pass:
   
   ```
   IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value 
violates unique constraint "asset_active_pkey"
   DETAIL:  Key (name, uri)=(...) already exists.
   ```
   
   This now inserts with `ON CONFLICT DO NOTHING`, using the same per-dialect 
statement as `AssetModelOperation.activate_assets_if_possible`, moved to a 
shared `insert_active_assets` helper. The scheduler sorts its rows first, so 
two schedulers inserting the same rows can't deadlock on each other. The 
dag-processor path keeps its insertion order, since there the first of two 
conflicting assets wins.
   
   **Lazy loads for conflicting assets.** An asset whose name or URI is already 
taken never activates, so it comes back on every pass. Building its warning 
lazy-loaded `scheduled_dags`, `producing_tasks` and `consuming_tasks`, three 
queries per asset per pass. On our deployment that stalled the scheduler loop 
for up to ~150s per pass (p50 went from ~33s to ~76s between heartbeats), long 
enough for the other schedulers to mark it failed. The inactive-asset query now 
`selectinload`s the three relationships.
   
   Tests: existing asset orphanage and activation tests pass unchanged. Two new 
tests fail without this change: one inserts the same `asset_active` row from 
"another scheduler" right before activation (`IntegrityError` before), the 
other checks reference loading for 20 conflicting assets takes one query (20 
before).
   
   Would appreciate the `backport-to-v3-3-test` label if this looks right, 
since 3.3 is where this shows up together with the #75404 fix.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   ---
   
   🤖 Generated with [Claude Code](https://claude.com/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]

Reply via email to