KEYCLOAK-8555 queried only realms with user storage provider to speed up user storage sync bootstrap

This commit is contained in:
Gideon Caranzo 2018-10-12 16:39:49 -05:00 committed by Marek Posolda
parent 7a96911a83
commit 7d85ce93bb
6 changed files with 30 additions and 1 deletions

View file

@ -453,11 +453,21 @@ public class RealmCacheSession implements CacheRealmProvider {
return "realm.query.by.name." + name;
}
@Override
public List<RealmModel> getRealmsWithProviderType(Class<?> type) {
// Retrieve realms from backend
List<RealmModel> backendRealms = getRealmDelegate().getRealmsWithProviderType(type);
return getRealms(backendRealms);
}
@Override
public List<RealmModel> getRealms() {
// Retrieve realms from backend
List<RealmModel> backendRealms = getRealmDelegate().getRealms();
return getRealms(backendRealms);
}
private List<RealmModel> getRealms(List<RealmModel> backendRealms) {
// Return cache delegates to ensure cache invalidated during write operations
List<RealmModel> cachedRealms = new LinkedList<RealmModel>();
for (RealmModel realm : backendRealms) {

View file

@ -93,9 +93,20 @@ public class JpaRealmProvider implements RealmProvider {
return adapter;
}
@Override
public List<RealmModel> getRealmsWithProviderType(Class<?> providerType) {
TypedQuery<String> query = em.createNamedQuery("getRealmIdsWithProviderType", String.class);
query.setParameter("providerType", providerType.getName());
return getRealms(query);
}
@Override
public List<RealmModel> getRealms() {
TypedQuery<String> query = em.createNamedQuery("getAllRealmIds", String.class);
return getRealms(query);
}
private List<RealmModel> getRealms(TypedQuery<String> query) {
List<String> entities = query.getResultList();
List<RealmModel> realms = new ArrayList<RealmModel>();
for (String id : entities) {

View file

@ -51,6 +51,7 @@ import java.util.Set;
@NamedQueries({
@NamedQuery(name="getAllRealmIds", query="select realm.id from RealmEntity realm"),
@NamedQuery(name="getRealmIdByName", query="select realm.id from RealmEntity realm where realm.name = :name"),
@NamedQuery(name="getRealmIdsWithProviderType", query="select distinct c.realm.id from ComponentEntity c where c.providerType = :providerType"),
})
public class RealmEntity {
@Id

View file

@ -24,4 +24,10 @@
</update>
</changeSet>
<changeSet author="gideonray@gmail.com" id="4.6.0-KEYCLOAK-8555">
<createIndex tableName="COMPONENT" indexName="IDX_COMPONENT_PROVIDER_TYPE">
<column name="PROVIDER_TYPE" type="VARCHAR(255)"/>
</createIndex>
</changeSet>
</databaseChangeLog>

View file

@ -77,6 +77,7 @@ public interface RealmProvider extends Provider, ClientProvider {
List<RealmModel> getRealms();
List<RealmModel> getRealmsWithProviderType(Class<?> type);
boolean removeRealm(String id);
void close();

View file

@ -58,7 +58,7 @@ public class UserStorageSyncManager {
@Override
public void run(KeycloakSession session) {
List<RealmModel> realms = session.realms().getRealms();
List<RealmModel> realms = session.realms().getRealmsWithProviderType(UserStorageProvider.class);
for (final RealmModel realm : realms) {
List<UserStorageProviderModel> providers = realm.getUserStorageProviders();
for (final UserStorageProviderModel provider : providers) {