Andrushika opened a new pull request, #69107:
URL: https://github.com/apache/airflow/pull/69107
<!-- SPDX-License-Identifier: Apache-2.0
https://www.apache.org/licenses/LICENSE-2.0 -->
<!--
Thank you for contributing!
Please provide above a brief description of the changes made in this pull
request.
Write a good git commit message following this guide:
http://chris.beams.io/posts/git-commit/
Please make sure that your code changes are covered with tests.
And in case of new features or big changes remember to adjust the
documentation.
For user-facing UI changes, please attach before/after screenshots (or a
short
screen recording) so reviewers can assess the visual impact.
Feel free to ping (in general) for the review if you do not see reaction for
a few days
(72 Hours is the minimum reaction time you can expect from volunteers) - we
sometimes miss notifications.
In case of an existing issue, reference it using one of the following:
* closes: #ISSUE
* related: #ISSUE
-->
* closes: #69041
As the issue reported, users who use Keycloak as an auth backend are facing
over 10 seconds of UI loading speed.
I took a look and discovered that
`KeycloakAuthManager.filter_authorized_dag_ids()` wraps the result with
cache/single-flight, but on cache miss it falls back to the base auth manager
implementation. The base implementation checks each Dag ID individually, so
Keycloak sends one authorization HTTP request per Dag ID during that filter
call.
To address this problem, I tried to simulate and wrote a script for a local
benchmark. Here's the result:
(50ms latency per request, measured with `perf_kit.repeat_and_time.timing`)
| Dags count | Before | After |
| ---- | ---- | ---- |
| 25 | 1.36s | 0.16s |
| 100 | 5.42s | 0.55s |
| 250 | 13.51s | 1.37s |
The test script is as following:
<details>
```python
from __future__ import annotations
import time
from unittest.mock import Mock
from airflow.providers.keycloak.auth_manager.constants import (
CONF_CLIENT_ID_KEY,
CONF_CLIENT_SECRET_KEY,
CONF_REALM_KEY,
CONF_SECTION_NAME,
CONF_SERVER_URL_KEY,
)
from airflow.providers.keycloak.auth_manager.keycloak_auth_manager import
KeycloakAuthManager
from tests_common.test_utils.config import conf_vars
from tests_common.test_utils.perf.perf_kit.repeat_and_time import timing
LATENCY_SECONDS = 0.05
DAG_COUNTS = (25, 100, 250)
def build_slow_session(latency_seconds: float = LATENCY_SECONDS):
session = Mock()
def slow_post(*_, **__):
time.sleep(latency_seconds)
response = Mock()
response.status_code = 200
return response
session.post = slow_post
return session
def main() -> None:
with conf_vars(
{
(CONF_SECTION_NAME, CONF_CLIENT_ID_KEY): "client_id",
(CONF_SECTION_NAME, CONF_CLIENT_SECRET_KEY): "client_secret",
(CONF_SECTION_NAME, CONF_REALM_KEY): "realm",
(CONF_SECTION_NAME, CONF_SERVER_URL_KEY):
"http://server.invalid",
}
):
for dag_count in DAG_COUNTS:
dag_ids = {f"dag_{index}" for index in range(dag_count)}
manager = KeycloakAuthManager()
manager._http_session = build_slow_session()
user = Mock()
user.get_id.return_value = f"user_for_{dag_count}"
user.access_token = "token"
with timing():
manager.filter_authorized_dag_ids(dag_ids=dag_ids,
user=user, method="GET")
print(f" ({dag_count} dags @ {int(LATENCY_SECONDS * 1000)}ms
latency)\n")
if __name__ == "__main__":
main()
```
</details>
However, I found it hard to reproduce the production environment as
described in #69041... I would really appreciate it if someone could help check
if this really solved the problem.
---
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR,
please
change below checkbox to `[X]` followed by the name of the tool, uncomment
the "Generated-by".
-->
- [X] Yes (please specify the tool below)
Generated-by: Claude Code Opus 4.8 following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions),
reviewed by @Andrushika.
---
* Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information. Note: commit author/co-author name and email in commits
become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License
Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment:
`{pr_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
You can add this file in a follow-up commit after the PR is created so you
know the PR number.
--
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]