-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Cache user object fetched per request in FAB auth manager for improved performance. #60274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
vincbeck
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As brought up in the related issue, we need to think about the case when permissions are updated
We need to think about permissions change
|
Mypy related error as below but not reproducible locally. Do I need to add stub pypi packages in pyproject.toml? Running mypy locally |
|
Single-file type checking is limited. Try using the suggested prek command locally and see if you can reproduce the error. |
|
@uranusjr Thanks, I am able to reproduce it locally. I am adding cachetools dependency. I do see Lines 1004 to 1006 in fb21525
airflow/devel-common/pyproject.toml Lines 107 to 130 in fb21525
|
…ing for the user object again.
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py
Show resolved
Hide resolved
potiuk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
CC: @jscheffl -> good idea with the warning in release notes. Maybe we should use more of those - to just recall some non-obvious things (though in this case it's pretty clear it's a new feature and unless we have a breaking change, we will end up with 3.2.0 for FAB provider.
Kudos to @eladkal who pinged me on slack about this. Actually his proposal was that we should consider a |
We could, yes. |
When a request is authenticated the user object is created from jwt token twice in the middleware layer and in the GetUserDep dependency resolution. Each token maps to a single user and hence cache the token's id to the user object so that it's reused.
TTLCachefrom cachetools is used to ensure the cached user objects expire to be fetched again. The TTL value is configurable throughfab.cache_ttlconfiguration with a default of 30 seconds. Each worker and instance of auth manager has its own cache and they are not persisted to be reset as workers restart. In case this needs to be disabled then users can set the cache_ttl as 0. I have added test to ensure the cache is expired. In case of deployments with frequent roles and permission changes users can set this to lower value at the cost of frequent db queries. For deployments with lesser number of changes to roles and permissions this can be set to a higher value for better performance. This should mitigate the need to invalidate caches on roles and permission changes. If required this can be documented with more detail in changelog just to avoid confusion.https://cachetools.readthedocs.io/en/latest/#cachetools.cachedmethod
closes #60265