You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sf...@apache.org on 2015/03/31 23:00:49 UTC

[11/50] incubator-usergrid git commit: Delete app index on app delete.

Delete app index on app delete.


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

Branch: refs/heads/USERGRID-473
Commit: f007f47916a69b17234368572c33f69bc3ada2c7
Parents: 44deb61
Author: Dave Johnson <dm...@apigee.com>
Authored: Tue Mar 24 12:44:23 2015 -0400
Committer: Dave Johnson <dm...@apigee.com>
Committed: Tue Mar 24 12:44:23 2015 -0400

----------------------------------------------------------------------
 .../corepersistence/CpEntityManagerFactory.java | 35 ++++++----
 .../cassandra/EntityManagerFactoryImplIT.java   | 68 +++++++++++---------
 2 files changed, 61 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f007f479/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 2d155a8..9b7b2bb 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
@@ -307,7 +307,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
         // find application_info for application to delete
 
-        EntityManager em = getEntityManager(getManagementAppId());
+        final EntityManager em = getEntityManager(getManagementAppId());
 
         final Results results = em.searchCollection(em.getApplicationRef(), CpNamingUtils.APPLICATION_INFOS,
             Query.fromQL("select * where " + PROPERTY_APPLICATION_ID + " = " + applicationId.toString()));
@@ -344,6 +344,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
         final ApplicationEntityIndex entityIndex = managerCache.getEntityIndex(
             new ApplicationScopeImpl(new SimpleId(applicationId, TYPE_APPLICATION)));
+        entityIndex.deleteApplication();
 
         applicationIdCache.evictAppId(appInfoToDelete.getName());
     }
@@ -425,23 +426,29 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
     public Map<String, UUID> getApplications(boolean deleted) throws Exception {
 
-        Map<String, UUID> appMap = new HashMap<String, UUID>();
+        Map<String, UUID> appMap = new HashMap<>();
 
-        ApplicationScope appScope = CpNamingUtils.getApplicationScope(getManagementAppId());
-        GraphManager gm = managerCache.getGraphManager(appScope);
+        ApplicationScope mgmtAppScope = CpNamingUtils.getApplicationScope(getManagementAppId());
+        GraphManager gm = managerCache.getGraphManager(mgmtAppScope);
 
         EntityManager em = getEntityManager(getManagementAppId());
-        Application app = em.getApplication();
-        Id fromEntityId = new SimpleId( app.getUuid(), app.getType() );
+        Application mgmtApp = em.getApplication();
+        Id fromEntityId = new SimpleId( mgmtApp.getUuid(), mgmtApp.getType() );
 
         final String scopeName;
         final String edgeType;
+
         if ( deleted ) {
-            edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.DELETED_APPLICATION_INFOS);
-            scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(CpNamingUtils.DELETED_APPLICATION_INFOS);
+            edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(
+                CpNamingUtils.DELETED_APPLICATION_INFOS);
+            scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(
+                CpNamingUtils.DELETED_APPLICATION_INFOS);
+
         } else {
-            edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS );
-            scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(CpNamingUtils.APPLICATION_INFOS);
+            edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(
+                CpNamingUtils.APPLICATION_INFOS );
+            scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(
+                CpNamingUtils.APPLICATION_INFOS);
         }
 
         logger.debug("getApplications(): Loading edges of edgeType {} from {}:{}",
@@ -451,7 +458,9 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
                 fromEntityId, edgeType, Long.MAX_VALUE,
                 SearchByEdgeType.Order.DESCENDING, null ));
 
-        //TODO This is wrong, and will result in OOM if there are too many applications.  This needs to stream properly with a buffer
+        // TODO This is wrong, and will result in OOM if there are too many applications.
+        // This needs to stream properly with a buffer
+
         Iterator<Edge> iter = edges.toBlocking().getIterator();
         while ( iter.hasNext() ) {
 
@@ -464,8 +473,8 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
             });
 
             CollectionScope collScope = new CollectionScopeImpl(
-                appScope.getApplication(),
-                appScope.getApplication(),
+                mgmtAppScope.getApplication(),
+                mgmtAppScope.getApplication(),
                 scopeName);
 
             org.apache.usergrid.persistence.model.entity.Entity appInfo =

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f007f479/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java b/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
index ff3dc2b..d52f3f6 100644
--- a/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
@@ -103,13 +103,17 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
     @Test
     public void testDeleteApplication() throws Exception {
 
+        int maxRetries = 10;
+
         String rand = UUIDGenerator.newTimeUUID().toString();
 
         // create an application with a collection and an entity
 
-        final UUID applicationId = setup.createApplication( "test-org-" + rand, "test-app-" + rand );
+        String appName = "test-app-" + rand;
+        String orgName = "test-org-" + rand;
+        final UUID deletedAppId = setup.createApplication( orgName, appName );
 
-        EntityManager em = setup.getEmf().getEntityManager( applicationId );
+        EntityManager em = setup.getEmf().getEntityManager(deletedAppId);
 
         Map<String, Object> properties1 = new LinkedHashMap<String, Object>();
         properties1.put( "Name", "12 Angry Men" );
@@ -121,16 +125,18 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
         properties2.put( "Year", 1992 );
         Entity film2 = em.create( "film", properties2 );
 
-        app.refreshIndex();
-
-        // TODO: this assertion should work!
-        //assertNotNull( "cannot lookup app by name", setup.getEmf().lookupApplication("test-app-" + rand) );
+        for ( int j=0; j<maxRetries; j++ ) {
+            if ( setup.getEmf().lookupApplication( orgName + "/" + appName ) != null ) {
+                break;
+            }
+            Thread.sleep( 500 );
+        }
 
         // delete the application
 
-        setup.getEmf().deleteApplication( applicationId );
+        setup.getEmf().deleteApplication( deletedAppId );
 
-        app.refreshIndex();
+        // wait for it to appear in delete apps list
 
         Func2<UUID, Map<String, UUID> ,Boolean> findApps = new Func2<UUID,Map<String, UUID> ,Boolean>() {
             @Override
@@ -148,31 +154,35 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
         };
 
         boolean found = false;
-        for(int i=0;i<10;i++){
-            found = findApps.call(applicationId,emf.getDeletedApplications());
-            if(found){
+        for ( int i=0; i<maxRetries; i++) {
+            found = findApps.call( deletedAppId, emf.getDeletedApplications() );
+            if ( found ) {
                 break;
-            } else{
-              Thread.sleep(500);
+            } else {
+                Thread.sleep( 500 );
             }
         }
-
         assertTrue( "Deleted app must be found in in deleted apps collection", found );
 
         // attempt to get entities in application's collections in various ways should all fail
 
-        assertNull( setup.getEmf().lookupApplication("test-app-" + rand) );
+        assertNull( setup.getEmf().lookupApplication( orgName + "/" + appName ));
+
+        // app must not be found in apps collection
 
-        Map<String, UUID> appMap = setup.getEmf().getApplications();
-        for ( String appName : appMap.keySet() ) {
-            UUID appId = appMap.get( appName );
-            assertNotEquals( appId, applicationId );
-            assertNotEquals( appName, "test-app-" + rand );
+        for ( int i=0; i<maxRetries; i++ ) {
+            found = findApps.call( deletedAppId, emf.getApplications() );
+            if ( found ) {
+                Thread.sleep( 500 );
+            } else {
+                break;
+            }
         }
+        assertFalse( "Deleted app must not be found in apps collection", found );
 
         // restore the app
 
-        emf.restoreApplication( applicationId );
+        emf.restoreApplication( deletedAppId );
 
         emf.rebuildAllIndexes(new EntityManagerFactory.ProgressObserver() {
             @Override
@@ -184,18 +194,18 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
         // test to see that app now works and is happy
 
         // it should not be found in the deleted apps collection
-        for(int i=0;i<10;i++){
-            found = findApps.call(applicationId,emf.getDeletedApplications());
-            if(!found){
+        for ( int i=0; i<maxRetries; i++ ) {
+            found = findApps.call( deletedAppId, emf.getDeletedApplications() );
+            if ( !found ) {
                 break;
-            } else{
-                Thread.sleep(500);
+            } else {
+                Thread.sleep( 500 );
             }
         }
         assertFalse("Restored app found in deleted apps collection", found);
 
-        for(int i=0;i<10;i++){
-            found = findApps.call(applicationId,setup.getEmf().getApplications());
+        for(int i=0;i<maxRetries;i++){
+            found = findApps.call(deletedAppId,setup.getEmf().getApplications());
             if(!found){
                 break;
             } else{
@@ -205,7 +215,7 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
         assertTrue("Restored app not found in apps collection", found);
 
         // TODO: this assertion should work!
-        //assertNotNull(setup.getEmf().lookupApplication("test-app-" + rand));
+        //assertNotNull(setup.getEmf().lookupApplication( orgName + "/" + appName ));
     }