8000 Avoid having N+1 database calls when fetching composite roles by ahus1 · Pull Request #46622 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ public RealmModel getRealm(String id) {
return adapter;
}

/**
* Cache that we've triggered an admin role invalidation in this session already.
* This avoids loading the admin role again from the database
*/
private boolean adminRoleInvalidated;

private RealmAdapter prepareCachedRealm(String id, KeycloakSession session) {
CachedRealm cached = cache.get(id, CachedRealm.class);
RealmAdapter adapter;
Expand All @@ -493,17 +499,15 @@ private RealmAdapter prepareCachedRealm(String id, KeycloakSession session) {
return null;
}

// to accommodate imports of new realms, check to see if the master admin role is up-to-date
if (!model.getName().equals(Config.getAdminRealm())) {
// To accommodate imports of new realms in a separate process, always evict the admin role.
// As an alternative, one could iterate through all composite roles of the Admin role, but that would
// load about 20 roles per realm to the cache which is also inefficient.
if (!adminRoleInvalidated && !model.getName().equals(Config.getAdminRealm())) {
RealmModel adminRealm = session.realms().getRealmByName(Config.getAdminRealm());
if (adminRealm != null) {
ClientModel clientModel = session.clients().getClientByClientId(adminRealm, model.getName() + "-realm");
if (clientModel != null) {
RoleModel adminRole = adminRealm.getRole(AdminRoles.ADMIN);
if (adminRole.getCompositesStream().noneMatch(r -> (r.isClientRole() && r.getContainerId().equals(clientModel.getId())))) {
registerRoleInvalidation(adminRole.getId(), adminRole.getName(), adminRole.getContainerId());
}
}
RoleModel adminRole = adminRealm.getRole(AdminRoles.ADMIN);
invalidateRole(adminRole.getId());
adminRoleInvalidated = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void setName(String name) {
@Override
public boolean isComposite() {
if (isUpdated()) return updated.isComposite();
return cached.isComposite();
return cached.isComposite(session, this::getRoleModel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class CachedRole extends AbstractRevisioned implements InRealm {
final protected String name;
final protected String realm;
final protected String description;
protected boolean composite;
final protected LazyLoader<RoleModel, Set<String>> composites;
/**
* Use this so the cache invalidation can retrieve any previously cached role mappings to determine if this
Expand All @@ -49,15 +48,10 @@ public class CachedRole extends AbstractRevisioned implements InRealm {

public CachedRole(long revision, RoleModel model, RealmModel realm) {
super(revision, model.getId());
composite = model.isComposite();
description = model.getDescription();
name = model.getName();
this.realm = realm.getId();
if (composite) {
composites = new DefaultLazyLoader<>(roleModel -> roleModel.getCompositesStream().map(RoleModel::getId).collect(Collectors.toSet()), HashSet::new);
} else {
composites = new DefaultLazyLoader<>(roleModel -> new HashSet<>(), HashSet::new);
}
composites = new DefaultLazyLoader<>(roleModel -> roleModel.getCompositesStream().map(RoleModel::getId).collect(Collectors.toSet()), HashSet::new);
attributes = new DefaultLazyLoader<>(roleModel -> new MultivaluedHashMap<>(roleModel.getAttributes()), MultivaluedHashMap::new);
}

Expand All @@ -73,13 +67,12 @@ public String getDescription() {
return description;
}

public boolean isComposite() {
return composite;
public boolean isComposite(KeycloakSession session, Supplier<RoleModel> roleModel) {
return !getComposites(session, roleModel).isEmpty();
}

public Set<String> getComposites(KeycloakSession session, Supplier<RoleModel> roleModel) {
cachedComposites = composites.get(session, roleModel);
composite = !cachedComposites.isEmpty();
return cachedComposites;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public void setName(String name) {

@Override
public boolean isComposite() {
return getChildRoles().findAny().isPresent();
return StreamsUtil.closing(em.createNamedQuery("getChildRoles", RoleEntity.class)
.setMaxResults(1)
.setParameter("parentRoleId", getId()).getResultStream()).findAny().isPresent();
}

@Override
Expand Down
Loading
0