-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix #7399: LDAP excessive CPU usage when AUTH_LDAP_FIND_GROUP_PERMS is enabled #7676
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
|
@tobiasge - Maybe you could have a look, as you are the one that worked on the LDAP part mentioned. |
|
Nice work, @kkthxbye-code! Tagging @heroin-moose @buzzingbren and @lastorel as well to see if thy can help test per their comments on #7399. If we can confirm that the fix works for others (e.g. that these aren't actually separate bugs), I don't see an issue with merging this. I may take a stab at optimizing the queries further when I have a chance. |
|
Hi @kkthxbye-code and @jeremystretch, thanks for linking to this. |
|
My first thought was "Why |
|
@buzzingbren - Yeah, the API performance issue when using LDAP is not related to this fix. The closest issue might be #6926 - but a new issue with your specific case might be in order. I haven't noticed any performance issue with the API, but I'm not really hammering it. If you create an issue or add a comment to #6926 with more details about what your setup is and what you are doing when experiencing slowdown, I'll be happy to try to debug it. This issue fixed in this PR will manifest if the following is true:
If this is true, the number of ObjectPermissions returned by the query will be equal to ObjectPermissions * Other users in the same group as you - so if you have 200 object permissions and 100 other netbox users are in the same group as you, the query will return 20000 ObjectPermissions, causing the CPU usage when it's iterating them here: netbox/netbox/netbox/authentication.py Lines 36 to 42 in 8230099
|
…IND_GROUP_PERMS is enabled
3305fee to
830cf4b
Compare
Fixes: #7399
This should fix the excessive CPU usage when enabling AUTH_LDAP_FIND_GROUP_PERMS and having a combination of many object permissions and many users in the same group.
First, this is not an optimal fix, feel free to close the PR if a better fix is suggested. The reason for the bad performance is the filter added in 8230099:
netbox/netbox/netbox/authentication.py
Lines 174 to 180 in 8230099
When added to the original query:
netbox/netbox/netbox/authentication.py
Lines 23 to 34 in 8230099
The resulting SQL is essentially:
The outer join for auth_user_group essentially duplicates each users_objectpermission row once for each other user that is allocated to one of the groups not filtered out in the auth_group join.
In my test instance, with 327 objectpermissions in the netbox-user group, 30 other users has that same group, which makes the query return 30 duplicates per objectpermission totalling 9810 permissionobjects. The fix caused a load of the rack page with 50 objects per page to decrease from 1400 ms to 600 ms.
I'm not sure how to fix it in a smart way, as I have a hard time seing how the relationship is supposed to work. Even the original query is iffy I think, as I'm pretty sure it can return duplicate permissions as well if user bound objectpermissions are used.
The fix just makes the postgres remove the duplicates, which fixes the performance regression when using AUTH_LDAP_FIND_GROUP_PERMS. The query time increase should be negligible even in extreme cases.
Still someone should probably rework the queries.
Note: While the fix is pretty straightforward and I can't see how it could mess anything up, I would appreciate if someone else could test it before deciding to merge.