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

@ -452,12 +452,22 @@ public class RealmCacheSession implements CacheRealmProvider {
static String getRealmByNameCacheKey(String name) { static String getRealmByNameCacheKey(String name) {
return "realm.query.by.name." + name; 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 @Override
public List<RealmModel> getRealms() { public List<RealmModel> getRealms() {
// Retrieve realms from backend // Retrieve realms from backend
List<RealmModel> backendRealms = getRealmDelegate().getRealms(); 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 // Return cache delegates to ensure cache invalidated during write operations
List<RealmModel> cachedRealms = new LinkedList<RealmModel>(); List<RealmModel> cachedRealms = new LinkedList<RealmModel>();
for (RealmModel realm : backendRealms) { for (RealmModel realm : backendRealms) {

View file

@ -92,10 +92,21 @@ public class JpaRealmProvider implements RealmProvider {
RealmAdapter adapter = new RealmAdapter(session, em, realm); RealmAdapter adapter = new RealmAdapter(session, em, realm);
return adapter; 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 @Override
public List<RealmModel> getRealms() { public List<RealmModel> getRealms() {
TypedQuery<String> query = em.createNamedQuery("getAllRealmIds", String.class); TypedQuery<String> query = em.createNamedQuery("getAllRealmIds", String.class);
return getRealms(query);
}
private List<RealmModel> getRealms(TypedQuery<String> query) {
List<String> entities = query.getResultList(); List<String> entities = query.getResultList();
List<RealmModel> realms = new ArrayList<RealmModel>(); List<RealmModel> realms = new ArrayList<RealmModel>();
for (String id : entities) { for (String id : entities) {

View file

@ -51,6 +51,7 @@ import java.util.Set;
@NamedQueries({ @NamedQueries({
@NamedQuery(name="getAllRealmIds", query="select realm.id from RealmEntity realm"), @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="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 { public class RealmEntity {
@Id @Id

View file

@ -23,5 +23,11 @@
<where>NAME LIKE 'group.resource.%'</where> <where>NAME LIKE 'group.resource.%'</where>
</update> </update>
</changeSet> </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> </databaseChangeLog>

View file

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

View file

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