Skip to content

Fix trigger API scalability for large DAG authorization sets - #76965

Open
adiranjan25 wants to merge 4 commits into
apache:mainfrom
adiranjan25:fix/airflow-76959-trigger-filter-scalability
Open

adiranjan25 wants to merge 4 commits into
apache:mainfrom
adiranjan25:fix/airflow-76959-trigger-filter-scalability

Conversation

@adiranjan25

Copy link
Copy Markdown
Contributor

Fixes #76959

Avoid expanding large authorized DAG and asset sets into SQL IN clauses when listing or retrieving triggers.

Authorization is evaluated using trigger ownership and watcher information, while preserving SQL filtering, deterministic ordering, exact counts, and pagination.

Validation:

  • 56 trigger API tests passed
  • Regression test with 70,000 authorized DAG IDs
  • Multi-watcher pagination regression test
  • Ruff lint and formatting checks passed

@henry3260 henry3260 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@@ -365,6 +365,29 @@ def test_filters(self, test_client, triggers, params, expected):
assert response.status_code == 200
assert sorted(_name(t) for t in response.json()["triggers"]) == expected

def test_pagination_counts_multi_watcher_trigger_once(self, test_client, session, triggers):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test that mocks get_authorized_dag_ids so some triggers are hidden, then walks the pages with limit/offset and asserts total_entries and the order?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @henry3260 . Added test_pagination_excludes_unauthorized_triggers, which mocks get_authorized_dag_ids, verifies hidden triggers are excluded, walks the results using limit/offset, and checks ordering and total_entries. The updated trigger API test suite passes locally (57 tests).

if owner_ref.get("callback_id") is not None:
return dag_id in self.callback_dag_ids
return bool(watched_asset_ids & self.asset_ids)

def to_orm(self, select: Select) -> Select:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove PermittedTriggerFilter.to_orm() and _select_triggers_watching()? This PR replaces to_orm() with allows(), so neither has a caller left, and keeping both means the same permission rule lives in two places.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @henry3260 . Removed PermittedTriggerFilter.to_orm() and _select_triggers_watching(), along with the unused imports. I also removed the OrmClause inheritance because to_orm() was its required abstract method. The existing allows() implementation remains unchanged. Ruff checks and all 57 trigger API tests pass locally.

@adiranjan25

Copy link
Copy Markdown
Contributor Author

Hi @henry3260, thanks again for the review. I've pushed commit bb11e8bd addressing both comments, including the authorization-aware pagination test and removal of the unused filter methods.

All 57 trigger API tests and Ruff checks passed locally. CI workflow approval is currently pending.

Could you please take another look when convenient? Thanks!

@seanghaeli seanghaeli left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on! Looking good, few things: Needs a rebase now that #76567 is in: owner_ref is now used_by_ref, and owner_still_refers_expression is now still_used_expression.

current_owner_ref = None
current_assets = set()

def include_candidate():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rows of one trigger come back next to each other, because the sort always ends with the id, so itertools.groupby(candidate_rows, key=itemgetter(0)) could replace include_candidate and the current_* state.

assert paged_ids == expected_ids

@mock.patch(f"{BASE_AUTH}.get_authorized_dag_ids", autospec=True)
def test_pagination_excludes_unauthorized_triggers(self, mock_dag_ids, test_client, session, triggers):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hides only task triggers. A callback and a watcher the caller can't read in the same walk would page all three branches of allows.

),
)
)
def allows(self, *, dag_id: str | None, owner_ref: dict | None, watched_asset_ids: set[int]) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#76560 adds the same check on a loaded trigger as PermittedTriggerFilter.permits(trigger), so whichever of the two lands second should use the other's.

@adiranjan25
adiranjan25 force-pushed the fix/airflow-76959-trigger-filter-scalability branch from bb11e8b to 5310697 Compare November 21, 2026 22:35
@adiranjan25

Copy link
Copy Markdown
Contributor Author

Hi @seanghaeli, thank you for the detailed review and suggestions.

I've rebased PR #76965 onto the latest upstream main and force-pushed the updated implementation (5310697d).

Changes completed:

  • Updated the trigger ownership references to used_by_ref and still_used_expression() following UI: Add a Triggers page to browse every trigger of every kind #76567.
  • Replaced manual candidate grouping with itertools.groupby() and retained trigger ID as a secondary ordering key.
  • Expanded pagination regression tests to cover ascending/descending sorting, multi-watcher deduplication, authorization filtering, and mixed task/callback/asset-watcher visibility.
  • Verified pagination ordering, offsets, and total_entries.

Validation: All 71 trigger API tests pass locally. Ruff lint, formatting, and whitespace checks also pass.

I've also noted the overlapping authorization logic in #76560 and will coordinate reuse depending on which PR merges first.

The new mixed-authorization test covers visible and hidden triggers, though I can further extend it to explicitly hide all three trigger categories if needed.

CI workflow approval is currently pending.

Would appreciate another review when convenient. Thank you!

@adiranjan25

Copy link
Copy Markdown
Contributor Author

Thanks @seanghaeli for the review.

I've further strengthened the pagination regression tests with three authorization scenarios, including hidden task, callback, and asset-watcher triggers.

Validation:

  • All 3 focused authorization tests passed.
  • All 73 trigger API tests passed.
  • Ruff lint passed and formatting was applied.
  • No whitespace errors reported.

The updated tests verify authorization-aware pagination, ordering, offsets, and total_entries.

Would appreciate another review when convenient. Thanks!

@adiranjan25

Copy link
Copy Markdown
Contributor Author

Hi @seanghaeli,

I noticed that the Tests (AMD) and CodeQL workflows for the latest commit (7ded48fd) both report action_required.

The implementation has been updated to address the previous review feedback, including the strengthened authorization-aware pagination tests.

Local validation:

  • All 73 trigger API tests passed.
  • All 3 focused authorization regression tests passed.
  • Ruff lint and formatting checks passed.
  • Git whitespace validation passed.

Could you please approve the CI workflows when convenient? If the existing workflow runs have expired, I would appreciate your guidance on triggering fresh runs.

I'm happy to address any additional review feedback or actionable CI failures.

Thank you again for your time and guidance!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API need workflow approval

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trigger routes bind every readable Dag and fail past the Postgres parameter limit

3 participants