Slow query when checking if a realm has brokers and brokering is enabled

Closes #37062

Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
Co-authored-by: Alexander Schwartz <alexander.schwartz@gmx.net>
This commit is contained in:
Pedro Igor 2025-02-05 10:49:32 -03:00 committed by GitHub
parent d7b0137a01
commit 1cb7a4736c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -45,6 +45,7 @@ public class InfinispanIdentityProviderStorageProvider implements IdentityProvid
private static final String IDP_ALIAS_KEY_SUFFIX = ".idp.alias";
private static final String IDP_ORG_ID_KEY_SUFFIX = ".idp.orgId";
private static final String IDP_LOGIN_SUFFIX = ".idp.login";
private static final String IDP_ENABLED_KEY_SUFFIX = ".idp.enabled";
private final KeycloakSession session;
private final IdentityProviderStorageProvider idpDelegate;
@ -78,6 +79,10 @@ public class InfinispanIdentityProviderStorageProvider implements IdentityProvid
return realm.getId() + IDP_LOGIN_SUFFIX + "." + fetchMode;
}
public static String cacheKeyIsEnabled(RealmModel realm) {
return realm.getId() + IDP_ENABLED_KEY_SUFFIX;
}
@Override
public IdentityProviderModel create(IdentityProviderModel model) {
registerCountInvalidation();
@ -164,6 +169,26 @@ public class InfinispanIdentityProviderStorageProvider implements IdentityProvid
return createOrganizationAwareIdentityProviderModel(cached.getIdentityProvider());
}
@Override
public boolean isIdentityFederationEnabled() {
String cacheKey = cacheKeyIsEnabled(getRealm());
if (isInvalid(cacheKey)) {
return idpDelegate.isIdentityFederationEnabled();
}
CachedCount cached = realmCache.getCache().get(cacheKey, CachedCount.class);
if (cached == null) {
Long loaded = realmCache.getCache().getCurrentRevision(cacheKey);
long count = idpDelegate.getAllStream(Map.of(), 0, 1).count();
cached = new CachedCount(loaded, getRealm(), cacheKey, count);
realmCache.getCache().addRevisioned(cached, realmCache.getStartupRevision());
}
return cached.getCount() > 0;
}
@Override
public Stream<IdentityProviderModel> getByOrganization(String orgId, Integer first, Integer max) {
RealmModel realm = getRealm();
@ -369,6 +394,7 @@ public class InfinispanIdentityProviderStorageProvider implements IdentityProvid
private void registerCountInvalidation() {
realmCache.registerInvalidation(cacheKeyIdpCount(getRealm()));
realmCache.registerInvalidation(cacheKeyIsEnabled(getRealm()));
}
private void registerIDPMapperInvalidation(IdentityProviderMapperModel mapper) {

View File

@ -198,7 +198,7 @@ public interface IdentityProviderStorageProvider extends Provider {
* otherwise.
*/
default boolean isIdentityFederationEnabled() {
return count() > 0;
return getAllStream(Map.of(), 0, 1).findFirst().isPresent();
}
/**