From 32c23c24107b3bceddba195fb84be44db4454298 Mon Sep 17 00:00:00 2001 From: Gabriel Lavoie Date: Wed, 30 Nov 2016 10:21:21 -0500 Subject: [PATCH] KEYCLOAK-4002: realmRevisions cache too small with high number of realms. - Increased the hardcoded default value to help running master with high number of realms. - Added a value computation based on the realms cache max size (to match the userRevisions cache configuration pattern). - Computed revisions cache size is now 2 times the configured maximum cache size. - Added a maximum realms cache size configuration to the different standalone.xml templates. - Added a missing users cache size configuration to standalone.xsl. --- .../demo-dist/src/main/xslt/standalone.xsl | 8 +++- .../src/main/cli/keycloak-install-ha.cli | 1 + .../src/main/cli/keycloak-install.cli | 1 + ...ltInfinispanConnectionProviderFactory.java | 38 ++++++++++++------- .../InfinispanConnectionProvider.java | 2 +- .../keycloak-infinispan.xml | 8 +++- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/distribution/demo-dist/src/main/xslt/standalone.xsl b/distribution/demo-dist/src/main/xslt/standalone.xsl index b75b775dc1..882d1b18f5 100755 --- a/distribution/demo-dist/src/main/xslt/standalone.xsl +++ b/distribution/demo-dist/src/main/xslt/standalone.xsl @@ -82,8 +82,12 @@ - - + + + + + + diff --git a/distribution/server-overlay/src/main/cli/keycloak-install-ha.cli b/distribution/server-overlay/src/main/cli/keycloak-install-ha.cli index a3b85f17c7..b7f2631198 100644 --- a/distribution/server-overlay/src/main/cli/keycloak-install-ha.cli +++ b/distribution/server-overlay/src/main/cli/keycloak-install-ha.cli @@ -3,6 +3,7 @@ embed-server --server-config=standalone-ha.xml /subsystem=infinispan/cache-container=keycloak:add(jndi-name="infinispan/Keycloak") /subsystem=infinispan/cache-container=keycloak/transport=TRANSPORT:add(lock-timeout=60000) /subsystem=infinispan/cache-container=keycloak/local-cache=realms:add() +/subsystem=infinispan/cache-container=keycloak/local-cache=realms/eviction=EVICTION:add(max-entries=10000,strategy=LRU) /subsystem=infinispan/cache-container=keycloak/local-cache=users:add() /subsystem=infinispan/cache-container=keycloak/local-cache=users/eviction=EVICTION:add(max-entries=10000,strategy=LRU) /subsystem=infinispan/cache-container=keycloak/distributed-cache=sessions:add(mode="SYNC",owners="1") diff --git a/distribution/server-overlay/src/main/cli/keycloak-install.cli b/distribution/server-overlay/src/main/cli/keycloak-install.cli index d62bcf1b66..835320d9de 100644 --- a/distribution/server-overlay/src/main/cli/keycloak-install.cli +++ b/distribution/server-overlay/src/main/cli/keycloak-install.cli @@ -2,6 +2,7 @@ embed-server --server-config=standalone.xml /subsystem=datasources/data-source=KeycloakDS/:add(connection-url="jdbc:h2:${jboss.server.data.dir}/keycloak;AUTO_SERVER=TRUE",jta=false,driver-name=h2,jndi-name=java:jboss/datasources/KeycloakDS,password=sa,user-name=sa,use-java-context=true) /subsystem=infinispan/cache-container=keycloak:add(jndi-name="infinispan/Keycloak") /subsystem=infinispan/cache-container=keycloak/local-cache=realms:add() +/subsystem=infinispan/cache-container=keycloak/local-cache=realms/eviction=EVICTION:add(max-entries=10000,strategy=LRU) /subsystem=infinispan/cache-container=keycloak/local-cache=users:add() /subsystem=infinispan/cache-container=keycloak/local-cache=users/eviction=EVICTION:add(max-entries=10000,strategy=LRU) /subsystem=infinispan/cache-container=keycloak/local-cache=sessions:add() diff --git a/model/infinispan/src/main/java/org/keycloak/connections/infinispan/DefaultInfinispanConnectionProviderFactory.java b/model/infinispan/src/main/java/org/keycloak/connections/infinispan/DefaultInfinispanConnectionProviderFactory.java index 7781e3ac7d..f9a30703ac 100755 --- a/model/infinispan/src/main/java/org/keycloak/connections/infinispan/DefaultInfinispanConnectionProviderFactory.java +++ b/model/infinispan/src/main/java/org/keycloak/connections/infinispan/DefaultInfinispanConnectionProviderFactory.java @@ -103,15 +103,20 @@ public class DefaultInfinispanConnectionProviderFactory implements InfinispanCon cacheManager = (EmbeddedCacheManager) new InitialContext().lookup(cacheContainerLookup); containerManaged = true; - cacheManager.defineConfiguration(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, getRevisionCacheConfig(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_DEFAULT_MAX)); - cacheManager.getCache(InfinispanConnectionProvider.REALM_CACHE_NAME, true); + long realmRevisionsMaxEntries = cacheManager.getCache(InfinispanConnectionProvider.REALM_CACHE_NAME).getCacheConfiguration().eviction().maxEntries(); + realmRevisionsMaxEntries = realmRevisionsMaxEntries > 0 + ? 2 * realmRevisionsMaxEntries + : InfinispanConnectionProvider.REALM_REVISIONS_CACHE_DEFAULT_MAX; - long maxEntries = cacheManager.getCache(InfinispanConnectionProvider.USER_CACHE_NAME).getCacheConfiguration().eviction().maxEntries(); - if (maxEntries <= 0) { - maxEntries = InfinispanConnectionProvider.USER_REVISIONS_CACHE_DEFAULT_MAX; - } + cacheManager.defineConfiguration(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, getRevisionCacheConfig(realmRevisionsMaxEntries)); + cacheManager.getCache(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, true); - cacheManager.defineConfiguration(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, getRevisionCacheConfig(maxEntries)); + long userRevisionsMaxEntries = cacheManager.getCache(InfinispanConnectionProvider.USER_CACHE_NAME).getCacheConfiguration().eviction().maxEntries(); + userRevisionsMaxEntries = userRevisionsMaxEntries > 0 + ? 2 * userRevisionsMaxEntries + : InfinispanConnectionProvider.USER_REVISIONS_CACHE_DEFAULT_MAX; + + cacheManager.defineConfiguration(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, getRevisionCacheConfig(userRevisionsMaxEntries)); cacheManager.getCache(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, true); cacheManager.getCache(InfinispanConnectionProvider.AUTHORIZATION_CACHE_NAME, true); cacheManager.getCache(InfinispanConnectionProvider.KEYS_CACHE_NAME, true); @@ -189,15 +194,20 @@ public class DefaultInfinispanConnectionProviderFactory implements InfinispanCon counterConfigBuilder.transaction().transactionManagerLookup(new DummyTransactionManagerLookup()); counterConfigBuilder.transaction().lockingMode(LockingMode.PESSIMISTIC); - cacheManager.defineConfiguration(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, getRevisionCacheConfig(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_DEFAULT_MAX)); - cacheManager.getCache(InfinispanConnectionProvider.REALM_CACHE_NAME, true); + long realmRevisionsMaxEntries = cacheManager.getCache(InfinispanConnectionProvider.REALM_CACHE_NAME).getCacheConfiguration().eviction().maxEntries(); + realmRevisionsMaxEntries = realmRevisionsMaxEntries > 0 + ? 2 * realmRevisionsMaxEntries + : InfinispanConnectionProvider.REALM_REVISIONS_CACHE_DEFAULT_MAX; - long maxEntries = cacheManager.getCache(InfinispanConnectionProvider.USER_CACHE_NAME).getCacheConfiguration().eviction().maxEntries(); - if (maxEntries <= 0) { - maxEntries = InfinispanConnectionProvider.USER_REVISIONS_CACHE_DEFAULT_MAX; - } + cacheManager.defineConfiguration(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, getRevisionCacheConfig(realmRevisionsMaxEntries)); + cacheManager.getCache(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, true); - cacheManager.defineConfiguration(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, getRevisionCacheConfig(maxEntries)); + long userRevisionsMaxEntries = cacheManager.getCache(InfinispanConnectionProvider.USER_CACHE_NAME).getCacheConfiguration().eviction().maxEntries(); + userRevisionsMaxEntries = userRevisionsMaxEntries > 0 + ? 2 * userRevisionsMaxEntries + : InfinispanConnectionProvider.USER_REVISIONS_CACHE_DEFAULT_MAX; + + cacheManager.defineConfiguration(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, getRevisionCacheConfig(userRevisionsMaxEntries)); cacheManager.getCache(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, true); cacheManager.defineConfiguration(InfinispanConnectionProvider.KEYS_CACHE_NAME, getKeysCacheConfig()); diff --git a/model/infinispan/src/main/java/org/keycloak/connections/infinispan/InfinispanConnectionProvider.java b/model/infinispan/src/main/java/org/keycloak/connections/infinispan/InfinispanConnectionProvider.java index 5173c3c277..7c255fd0b8 100755 --- a/model/infinispan/src/main/java/org/keycloak/connections/infinispan/InfinispanConnectionProvider.java +++ b/model/infinispan/src/main/java/org/keycloak/connections/infinispan/InfinispanConnectionProvider.java @@ -27,7 +27,7 @@ public interface InfinispanConnectionProvider extends Provider { String REALM_CACHE_NAME = "realms"; String REALM_REVISIONS_CACHE_NAME = "realmRevisions"; - int REALM_REVISIONS_CACHE_DEFAULT_MAX = 10000; + int REALM_REVISIONS_CACHE_DEFAULT_MAX = 20000; String USER_CACHE_NAME = "users"; String USER_REVISIONS_CACHE_NAME = "userRevisions"; diff --git a/wildfly/server-subsystem/src/main/resources/subsystem-templates/keycloak-infinispan.xml b/wildfly/server-subsystem/src/main/resources/subsystem-templates/keycloak-infinispan.xml index 3c8f429e1d..6bd68545ff 100755 --- a/wildfly/server-subsystem/src/main/resources/subsystem-templates/keycloak-infinispan.xml +++ b/wildfly/server-subsystem/src/main/resources/subsystem-templates/keycloak-infinispan.xml @@ -25,7 +25,9 @@ - + + + @@ -92,7 +94,9 @@ - + + +