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

   With the FAB auth manager, the Dag processor calls 
`FabAuthManager.sync_dag_perms` for every Dag it parses, whether or not the 
serialized Dag changed. `sync_perm_for_dag` then calls `create_permission` for 
each of the 7 Dag-level permissions (3 on `DAG:<dag_id>`, 4 on `DAG 
Run:<dag_id>`), and each call runs 3 SELECTs (action, resource, permission) 
before finding that the permission already exists. After the first parse, that 
is 21 queries per Dag per parse that change nothing.
   
   `sync_perm_for_dag` now loads the Dag's existing `(action, resource)` pairs 
with one query and calls `create_permission` only for the missing ones. A new 
Dag, or one whose permission was deleted, still goes through 
`create_permission` unchanged, so its `IntegrityError` handling for concurrent 
writers is unaffected. The `access_control` handling is not touched.
   
   The query filters on action names as well as resource names. 
`ab_permission_view` only has the `UNIQUE (permission_id, view_menu_id)` index; 
with resource names alone SQLite scans the whole table for every Dag, and a 
version of this change that filtered on resource names only was slower than 
`main` at 10,000 Dags. Postgres and MySQL use an index either way, so the extra 
filter costs nothing there.
   
   Measured locally with a throwaway script calling `sync_perm_for_dag` for 
Dags whose permissions already exist. SQLite: 10,000 Dags, all synced. Postgres 
and MySQL (Breeze): 10,000 Dags in the DB, 1,000 of them synced.
   
   | Backend | Before | After |
   |---|---|---|
   | SQLite | 21 queries, 5.66 ms/Dag | 1 query, 0.29 ms/Dag |
   | Postgres 14 | 21 queries, 11.70 ms/Dag | 1 query, 0.65 ms/Dag |
   | MySQL 8.0 | 21 queries, 18.76 ms/Dag | 1 query, 0.53 ms/Dag |
   
   A Dag seen for the first time runs one extra query (45 instead of 44); that 
path runs once per Dag.
   
   Calling `sync_dag_perms` only when the serialized Dag changed would remove 
these queries entirely, but it would change behavior for every auth manager: a 
Dag permission deleted by an admin would no longer be recreated on the next 
parse. This change keeps the current behavior and stays inside the FAB provider.
   
   The new test fails on `main` with 21 queries (all permissions exist) and 24 
queries (one permission missing).
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Opus 5.5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)


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