Repository: usergrid Updated Branches: refs/heads/release-2.1.1 9b9f83559 -> 74b211320
Add caching around getOrganizationConfigForApplication. Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/f972e2e0 Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/f972e2e0 Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/f972e2e0 Branch: refs/heads/release-2.1.1 Commit: f972e2e06440de1e149d523d7d91aae9db585217 Parents: b3739dc Author: Michael Russo <[email protected]> Authored: Sat Mar 26 16:39:32 2016 -0700 Committer: Michael Russo <[email protected]> Committed: Sat Mar 26 16:39:32 2016 -0700 ---------------------------------------------------------------------- .../main/resources/usergrid-default.properties | 3 + .../cassandra/ManagementServiceImpl.java | 67 ++++++++++++++------ 2 files changed, 51 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/f972e2e0/stack/config/src/main/resources/usergrid-default.properties ---------------------------------------------------------------------- diff --git a/stack/config/src/main/resources/usergrid-default.properties b/stack/config/src/main/resources/usergrid-default.properties index 67c6a08..de5e94f 100644 --- a/stack/config/src/main/resources/usergrid-default.properties +++ b/stack/config/src/main/resources/usergrid-default.properties @@ -596,6 +596,9 @@ usergrid.auth.cache.time-to-live=3600 # all (= in + out)' usergrid.rest.default-connection-param=all +# Set the cache expiration for organization config per application +usergrid.orgconfig.cache.timeout=30000 + ############################## Usergrid Testing ############################# http://git-wip-us.apache.org/repos/asf/usergrid/blob/f972e2e0/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java ---------------------------------------------------------------------- diff --git a/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java b/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java index 65f4744..56e1de7 100644 --- a/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java +++ b/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java @@ -19,6 +19,9 @@ package org.apache.usergrid.management.cassandra; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.inject.Injector; @@ -76,6 +79,7 @@ import rx.Observable; import java.nio.ByteBuffer; import java.util.*; import java.util.Map.Entry; +import java.util.concurrent.TimeUnit; import static java.lang.Boolean.parseBoolean; import static org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString; @@ -138,6 +142,8 @@ public class ManagementServiceImpl implements ManagementService { public static final String REGISTRATION_REQUIRES_EMAIL_CONFIRMATION = "registration_requires_email_confirmation"; public static final String NOTIFY_ADMIN_OF_NEW_USERS = "notify_admin_of_new_users"; + public static final String ORG_CONFIG_CACHE_PROP = "usergrid.orgconfig.cache.timeout"; + protected ServiceManagerFactory smf; protected EntityManagerFactory emf; @@ -3376,25 +3382,8 @@ public class ManagementServiceImpl implements ManagementService { @Override public OrganizationConfig getOrganizationConfigForApplication( UUID applicationInfoId ) throws Exception { - if ( applicationInfoId != null && applicationInfoId != CpNamingUtils.MANAGEMENT_APPLICATION_ID) { - - final EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); - - Results r = em.getSourceEntities( - new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId), - ORG_APP_RELATIONSHIP, Group.ENTITY_TYPE, Level.ALL_PROPERTIES); - - Group org = (Group)r.getEntity(); - - if ( org != null ) { - Map<Object, Object> entityProperties = em.getDictionaryAsMap(org, ORGANIZATION_CONFIG_DICTIONARY); - return new OrganizationConfig(orgConfigProperties, org.getUuid(), org.getPath(), entityProperties, false); - } - - } - - // return the defaults - return new OrganizationConfig(orgConfigProperties); + // return from the cache. if the orgconfig cannot be loaded, defaults are loaded and cached + return orgConfigByAppCache.get( applicationInfoId ); } @@ -3413,7 +3402,47 @@ public class ManagementServiceImpl implements ManagementService { entry.getValue() ); } } + + // evict cache for this key if it exists + orgConfigByAppCache.invalidate( organizationConfig.getUuid() ); + } } + + private LoadingCache<UUID, OrganizationConfig> orgConfigByAppCache = + CacheBuilder.newBuilder().maximumSize( 1000 ) + .expireAfterWrite( Long.valueOf( System.getProperty(ORG_CONFIG_CACHE_PROP, "30000") ) , TimeUnit.MILLISECONDS) + .build( new CacheLoader<UUID, OrganizationConfig>() { + public OrganizationConfig load( UUID applicationInfoId ) { + + try { + + if (applicationInfoId != null && applicationInfoId != CpNamingUtils.MANAGEMENT_APPLICATION_ID) { + + final EntityManager em = emf.getEntityManager(smf.getManagementAppId()); + + Results r = em.getSourceEntities( + new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId), + ORG_APP_RELATIONSHIP, Group.ENTITY_TYPE, Level.ALL_PROPERTIES); + + Group org = (Group) r.getEntity(); + + if (org != null) { + Map<Object, Object> entityProperties = em.getDictionaryAsMap(org, ORGANIZATION_CONFIG_DICTIONARY); + return new OrganizationConfig(orgConfigProperties, org.getUuid(), org.getPath(), entityProperties, false); + } + + } + + return new OrganizationConfig(orgConfigProperties); + + }catch (Exception e){ + + return new OrganizationConfig(orgConfigProperties); + + } + }} + ); + }
