Repository navigation
Avoid having N+1 database calls when fetching composite roles - #46622
Conversation
Closes keycloak#46605 Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
| if (clientModel != null) { | ||
| RoleModel adminRole = adminRealm.getRole(AdminRoles.ADMIN); | ||
| if (adminRole.getCompositesStream().noneMatch(r -> (r.isClientRole() && r.getContainerId().equals(clientModel.getId())))) { | ||
| if (containers == null) { |
There was a problem hiding this comment.
This should limit the potential reloading of admin composite roles due to newly cached realms to just once per session.
Newly cached realms are caused either by a realm being added, or because an existing realm was evicted from cache. If I'm understanding correctly all resources in the realm cache are being treated equally - that is loading a lot of entries, such as composite roles, will cause the eviction of realms and the admin role. Other than adjusting the realm cache size, do we have some additional controls over cache tiers or weighting for eviction by type (it seems realms are substancially more heavy-weight than most entries)?
Without adjustments to what is being evicted, the other approach of simply invalidating the admin role when a newly cached realm is created may be better than this in practice (at least for non-admin heavy workloads) because if realms are routinely being evicted, then so is the master admin role.
Alternatively is there any way to create a cache entry for the admin role that cannot be evicted? That would address the performance issue here and reduce the overhead with frequent admin requests once the cache is thrashing.
There was a problem hiding this comment.
Infinispan will cache evict the "least frequently used" item, so this should help a bit.
Still, I'll rewrite the code to always invalidate the admin role.
There was a problem hiding this comment.
Infinispan will cache evict the "least frequently used" item, so this should help a bit.
Is it worth a follow-up about tuning this further?
Another possible followup - even with light-weight tokens it seems like creating the server side identity for someone with the master admin role is expensive (at least in terms of cache entires) when there are a lot of realms. Should we consider partitioning the load of composites by realm, rather than materializing all of them for light-weight tokens? Or do we already caution against using someone with the master admin role to perform admin operations in this scenario?
There was a problem hiding this comment.
Is it worth a follow-up about tuning this further?
IMHO no.
For light-weight tokens it seems like creating the server side identity for someone with the master admin role is expensive
It is very expensive as it will evaluate all roles by iterating through them recursively. There were some ideas to maybe implement a recursive query in the past, but we did never implement that.
See https://vladmihalcea.com/hibernate-with-recursive-query/ for more information, and #46653 for a follow-up issue.
Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
There was a problem hiding this comment.
Looks good, just one nit and some questions about follow-ups.
| if (clientModel != null) { | ||
| RoleModel adminRole = adminRealm.getRole(AdminRoles.ADMIN); | ||
| if (adminRole.getCompositesStream().noneMatch(r -> (r.isClientRole() && r.getContainerId().equals(clientModel.getId())))) { | ||
| if (containers == null) { |
There was a problem hiding this comment.
Infinispan will cache evict the "least frequently used" item, so this should help a bit.
Is it worth a follow-up about tuning this further?
Another possible followup - even with light-weight tokens it seems like creating the server side identity for someone with the master admin role is expensive (at least in terms of cache entires) when there are a lot of realms. Should we consider partitioning the load of composites by realm, rather than materializing all of them for light-weight tokens? Or do we already caution against using someone with the master admin role to perform admin operations in this scenario?
…ispan/RealmCacheSession.java Co-authored-by: Steven Hawkins <shawkins@redhat.com> Signed-off-by: Alexander Schwartz <alexander.schwartz@gmx.net>
Closes #46605