You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2015/03/30 21:58:18 UTC

[14/50] incubator-usergrid git commit: Change OrgApplicationCache to ApplicationId cache because we need no org lookup, and update it to use EM alias instead of Query to lookup app UUID.

Change OrgApplicationCache to ApplicationId cache because we need no org lookup, and update it to use EM alias instead of Query to lookup app UUID.


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/ae111dac
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/ae111dac
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/ae111dac

Branch: refs/heads/two-dot-o-dev
Commit: ae111dac87bbdf5df0ba172ccbc267f4f9c4ca32
Parents: 07f1f62
Author: Dave Johnson <dm...@apigee.com>
Authored: Tue Mar 10 11:07:36 2015 -0400
Committer: Dave Johnson <dm...@apigee.com>
Committed: Tue Mar 10 11:07:36 2015 -0400

----------------------------------------------------------------------
 .../corepersistence/ApplicationIdCache.java     |  52 ++++++
 .../corepersistence/ApplicationIdCacheImpl.java | 106 +++++++++++
 .../corepersistence/CpEntityManagerFactory.java | 108 +----------
 .../corepersistence/OrgApplicationCache.java    |  67 -------
 .../OrgApplicationCacheImpl.java                | 181 -------------------
 5 files changed, 163 insertions(+), 351 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ae111dac/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCache.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCache.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCache.java
new file mode 100644
index 0000000..71637bd
--- /dev/null
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCache.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.usergrid.corepersistence;
+
+
+import java.util.UUID;
+
+import org.apache.usergrid.persistence.Entity;
+
+import com.google.common.base.Optional;
+
+
+/**
+ * A simple cache interface for looking up entities from an EM
+ */
+public interface ApplicationIdCache {
+
+
+    /**
+     * Evict the application by name
+     */
+    public Optional<UUID> getApplicationId(final String applicationName);
+
+
+    /**
+     * Evict the app id by the name
+     */
+    public void evictAppId(final String applicationName);
+
+
+    /**
+     * Evict all caches
+     */
+    public void evictAll();
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ae111dac/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
new file mode 100644
index 0000000..d415259
--- /dev/null
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.usergrid.corepersistence;
+
+
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.usergrid.corepersistence.util.CpNamingUtils;
+import org.apache.usergrid.persistence.Entity;
+import org.apache.usergrid.persistence.EntityManager;
+import org.apache.usergrid.persistence.EntityManagerFactory;
+import org.apache.usergrid.persistence.EntityRef;
+
+import com.google.common.base.Optional;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+
+
+/**
+ * Implements the org app cache for faster runtime lookups.  These values are immutable, so this LRU cache can stay
+ * full for the duration of the execution
+ */
+public class ApplicationIdCacheImpl implements ApplicationIdCache {
+
+
+    /**
+     * Cache the pointer to our root entity manager for reference
+     */
+    private final EntityManager rootEm;
+
+    private final LoadingCache<String, Optional<UUID>> appCache =
+        CacheBuilder.newBuilder().maximumSize( 10000 ).build( new CacheLoader<String, Optional<UUID>>() {
+            @Override
+            public Optional<UUID> load( final String key ) throws Exception {
+                return fetchApplicationId( key );
+            }
+        } );
+
+
+    public ApplicationIdCacheImpl(final EntityManagerFactory emf) {
+        this.rootEm = emf.getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);
+    }
+
+    @Override
+    public Optional<UUID> getApplicationId( final String applicationName ) {
+        try {
+            return appCache.get( applicationName );
+        }
+        catch ( ExecutionException e ) {
+            throw new RuntimeException( "Unable to load org cache", e );
+        }
+    }
+
+
+    /**
+     * Fetch our application id
+     */
+    private Optional<UUID> fetchApplicationId( final String applicationName ) {
+
+        try {
+            UUID applicationId = null;
+
+            final EntityRef alias = rootEm.getAlias( CpNamingUtils.APPLICATION_INFO, applicationName );
+            if ( alias != null ) {
+                Entity entity = rootEm.get(alias);
+                applicationId = (UUID) entity.getProperty("uuid");
+            }
+
+            return Optional.of( applicationId );
+        }
+        catch ( Exception e ) {
+            throw new RuntimeException( "Unable to retrieve application id", e );
+        }
+    }
+
+
+    @Override
+    public void evictAppId( final String applicationName ) {
+        appCache.invalidate( applicationName );
+    }
+
+
+    @Override
+    public void evictAll() {
+        appCache.invalidateAll();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ae111dac/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
index 78f2095..e435cb2 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
@@ -34,13 +34,11 @@ import org.apache.usergrid.persistence.collection.EntityCollectionManager;
 import org.apache.usergrid.persistence.collection.impl.CollectionScopeImpl;
 import org.apache.usergrid.persistence.collection.serialization.impl.migration.EntityIdScope;
 import org.apache.usergrid.persistence.core.metrics.MetricsFactory;
-import org.apache.usergrid.persistence.core.migration.data.DataMigrationManager;
 import org.apache.usergrid.persistence.core.migration.data.MigrationDataProvider;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
 import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl;
 import org.apache.usergrid.persistence.core.util.Health;
 import org.apache.usergrid.persistence.entities.Application;
-import org.apache.usergrid.persistence.entities.Group;
 import org.apache.usergrid.persistence.exceptions.ApplicationAlreadyExistsException;
 import org.apache.usergrid.persistence.exceptions.DuplicateUniquePropertyExistsException;
 import org.apache.usergrid.persistence.exceptions.EntityNotFoundException;
@@ -95,7 +93,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
             }
         });
 
-    private final OrgApplicationCache orgApplicationCache;
+    private final ApplicationIdCache orgApplicationCache;
 
 
     private ManagerCache managerCache;
@@ -116,13 +114,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
         this.injector = injector;
         this.managerCache = injector.getInstance( ManagerCache.class );
         this.metricsFactory = injector.getInstance( MetricsFactory.class );
-
-        // can be removed after everybody moves to Usergrid 2.0, default is true
-        Properties configProps = cassandraService.getProperties();
-        if ( configProps.getProperty("usergrid.twodoto.appinfo.migration", "true").equals("true")) {
-            migrateOldAppInfos();
-        }
-        this.orgApplicationCache = new OrgApplicationCacheImpl( this );
+        this.orgApplicationCache = new ApplicationIdCacheImpl( this );
     }
 
 
@@ -236,9 +228,6 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
         EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);
 
-        // Ensure our management system exists before creating our application
-        init();
-
         final String appName = buildAppName( organizationName, name );
 
         // check for pre-existing application
@@ -247,32 +236,10 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
             throw new ApplicationAlreadyExistsException( appName );
         }
 
-        getSetup().setupApplicationKeyspace( applicationId, appName );
-
-        final Optional<UUID> cachedValue = orgApplicationCache.getOrganizationId( organizationName );
-
-        if ( !cachedValue.isPresent() ) {
-
-
-            // create new org because the specified one does not exist
-            final String orgName = organizationName;
+        // create application info entity in the management app
 
+        getSetup().setupApplicationKeyspace( applicationId, appName );
 
-
-            try {
-                final Entity orgInfo = em.create( "organization", new HashMap<String, Object>() {{
-                    put( PROPERTY_NAME, orgName );
-                }} );
-                //evit so it's re-loaded later
-                orgApplicationCache.evictOrgId( name );
-            }
-            catch ( DuplicateUniquePropertyExistsException e ) {
-                //swallow, if it exists, just get it
-                orgApplicationCache.evictOrgId( organizationName );
-            }
-        }
-
-        // create appinfo entry in the system app
         final UUID appId = applicationId;
         Map<String, Object> appInfoMap = new HashMap<String, Object>() {{
             put( PROPERTY_NAME, appName );
@@ -298,7 +265,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
         appEm.resetRoles();
         appEm.refreshIndex();
 
-        logger.info("Initialized application {}", appName );
+        logger.info("Initialized application {}", appName);
 
         //evict app Id from cache
         orgApplicationCache.evictAppId( appName );
@@ -483,71 +450,6 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
     }
 
 
-    private void migrateOldAppInfos() {
-
-        EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);
-
-        Query q = Query.fromQL("select *");
-        Results results = null;
-        try {
-            results = em.searchCollection(em.getApplicationRef(), "appinfos", q);
-        } catch (Exception e) {
-            logger.error("Error reading old appinfos collection, not migrating", e);
-            return;
-        }
-
-        if ( !results.isEmpty() ) {
-
-            // applications still found in old appinfos collection, migrate them.
-            logger.info("Migrating old appinfos");
-
-            for ( Entity oldAppInfo : results.getEntities() ) {
-
-                final String appName = oldAppInfo.getName();
-
-                UUID applicationId = null, organizationId = null;
-                Object uuidObject = oldAppInfo.getProperty("applicationUuid");
-                if (uuidObject instanceof UUID) {
-                    applicationId = (UUID) uuidObject;
-                } else {
-                    applicationId = UUIDUtils.tryExtractUUID(uuidObject.toString());
-                }
-                uuidObject = oldAppInfo.getProperty("organizationUuid");
-                if (uuidObject instanceof UUID) {
-                    organizationId = (UUID) uuidObject;
-                } else {
-                    organizationId = UUIDUtils.tryExtractUUID(uuidObject.toString());
-                }
-
-                // create and connect new APPLICATION_INFO oldAppInfo to Organization
-
-                final UUID appId = applicationId;
-                Map<String, Object> appInfoMap = new HashMap<String, Object>() {{
-                    put(PROPERTY_NAME, appName);
-                    put(PROPERTY_UUID, appId);
-                }};
-
-                final Entity appInfo;
-                try {
-                    appInfo = em.create(appId, CpNamingUtils.APPLICATION_INFO, appInfoMap);
-                    em.createConnection(new SimpleEntityRef(Group.ENTITY_TYPE, organizationId), "owns", appInfo);
-                    em.delete( oldAppInfo );
-                    logger.info("Migrated old appinfo for app {}", appName);
-
-                } catch (Exception e) {
-                    logger.error("Error migration application " + appName + " continuing ", e);
-                }
-            }
-
-            em.refreshIndex();
-
-        } else {
-            logger.info("No old appinfos found, no need for migration");
-        }
-
-    }
-
-
     @Override
     public void setup() throws Exception {
         getSetup().init();

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ae111dac/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCache.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCache.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCache.java
deleted file mode 100644
index b20dbe1..0000000
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCache.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.usergrid.corepersistence;
-
-
-import java.util.UUID;
-
-import org.apache.usergrid.persistence.Entity;
-
-import com.google.common.base.Optional;
-
-
-/**
- * A simple cache interface for looking up entities from an EM
- */
-public interface OrgApplicationCache {
-
-
-    /**
-     * Get an entity by it's alias property.  The result is cached. To clear it call evict or evict all
-     * @param
-     * @return
-     */
-    public Optional<UUID> getOrganizationId(final String orgName);
-
-    /**
-     * Evict the org by name
-     * @param orgName
-     */
-    public void evictOrgId(final String orgName);
-
-    /**
-     * Evict the application by name
-     * @param applicationName
-     * @return
-     */
-    public Optional<UUID> getApplicationId(final String applicationName);
-
-
-    /**
-     * Evict the app id by the name
-     */
-    public void evictAppId(final String applicationname);
-
-
-    /**
-     * Evict all caches
-     */
-    public void evictAll();
-}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ae111dac/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCacheImpl.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCacheImpl.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCacheImpl.java
deleted file mode 100644
index 23410d8..0000000
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/OrgApplicationCacheImpl.java
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.usergrid.corepersistence;
-
-
-import java.util.UUID;
-import java.util.concurrent.ExecutionException;
-
-import org.apache.usergrid.corepersistence.util.CpNamingUtils;
-import org.apache.usergrid.persistence.Entity;
-import org.apache.usergrid.persistence.EntityManager;
-import org.apache.usergrid.persistence.EntityManagerFactory;
-import org.apache.usergrid.persistence.EntityRef;
-import org.apache.usergrid.persistence.Results;
-import org.apache.usergrid.persistence.index.query.Query;
-import org.apache.usergrid.utils.UUIDUtils;
-
-import com.google.common.base.Optional;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-
-import static org.apache.usergrid.persistence.Schema.PROPERTY_NAME;
-
-
-/**
- * Implements the org app cache for faster runtime lookups.  These values are immutable, so this LRU cache can stay
- * full for the duration of the execution
- */
-public class OrgApplicationCacheImpl implements OrgApplicationCache {
-
-
-    /**
-     * Cache the pointer to our root entity manager for reference
-     */
-    private final EntityManager rootEm;
-
-    private final LoadingCache<String, Optional<UUID>> orgCache =
-        CacheBuilder.newBuilder().maximumSize( 10000 ).build( new CacheLoader<String, Optional<UUID>>() {
-            @Override
-            public Optional<UUID> load( final String key ) throws Exception {
-                return fetchOrganizationId( key );
-            }
-        } );
-
-
-    private final LoadingCache<String, Optional<UUID>> appCache =
-        CacheBuilder.newBuilder().maximumSize( 10000 ).build( new CacheLoader<String, Optional<UUID>>() {
-            @Override
-            public Optional<UUID> load( final String key ) throws Exception {
-                return fetchApplicationId( key );
-            }
-        } );
-
-
-    public OrgApplicationCacheImpl( final EntityManagerFactory emf ) {
-        this.rootEm = emf.getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);
-    }
-
-
-    @Override
-    public Optional<UUID> getOrganizationId( final String orgName ) {
-        try {
-            return orgCache.get( orgName );
-        }
-        catch ( ExecutionException e ) {
-            throw new RuntimeException( "Unable to load org cache", e );
-        }
-    }
-
-
-    /**
-     * Fetches the organization
-     */
-    private Optional<UUID> fetchOrganizationId( final String orgName ) {
-
-        try {
-            final EntityRef alias = rootEm.getAlias( "organizations", orgName );
-
-            if ( alias == null ) {
-                return Optional.absent();
-            }
-
-            final Entity entity;
-
-            entity = rootEm.get( alias );
-
-
-            if ( entity == null ) {
-                return Optional.absent();
-            }
-
-            return Optional.of( entity.getUuid() );
-        }
-        catch ( Exception e ) {
-            throw new RuntimeException( "Unable to load organization Id for caching", e );
-        }
-    }
-
-
-    @Override
-    public void evictOrgId( final String orgName ) {
-        orgCache.invalidate( orgName );
-    }
-
-
-    @Override
-    public Optional<UUID> getApplicationId( final String applicationName ) {
-        try {
-            return appCache.get( applicationName );
-        }
-        catch ( ExecutionException e ) {
-            throw new RuntimeException( "Unable to load org cache", e );
-        }
-    }
-
-
-    /**
-     * Fetch our application id
-     */
-    private Optional<UUID> fetchApplicationId( final String applicationName ) {
-
-        try {
-            Query q = Query.fromQL( PROPERTY_NAME + " = '" + applicationName + "'" );
-
-
-            Results results = rootEm.searchCollection( rootEm.getApplicationRef(), "appinfos", q );
-
-            if ( results.isEmpty() ) {
-                return Optional.absent();
-            }
-
-            Entity entity = results.iterator().next();
-            Object uuidObject = entity.getProperty( "applicationUuid" );
-
-            final UUID value;
-            if ( uuidObject instanceof UUID ) {
-                value = ( UUID ) uuidObject;
-            }
-            else {
-                value = UUIDUtils.tryExtractUUID( entity.getProperty( "applicationUuid" ).toString() );
-            }
-
-
-            return Optional.of( value );
-        }
-        catch ( Exception e ) {
-            throw new RuntimeException( "Unable to retreive application id", e );
-        }
-    }
-
-
-    @Override
-    public void evictAppId( final String applicationName ) {
-        appCache.invalidate( applicationName );
-    }
-
-
-    @Override
-    public void evictAll() {
-        orgCache.invalidateAll();
-        appCache.invalidateAll();
-    }
-}