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/04/07 15:20:43 UTC
[02/14] incubator-usergrid git commit: refactor delete application
refactor delete application
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/8b5ad3bd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/8b5ad3bd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/8b5ad3bd
Branch: refs/heads/USERGRID-533-create-app-test
Commit: 8b5ad3bd79075667f54e740136c29a9c3f7c591f
Parents: 602a0a9
Author: Shawn Feldman <sf...@apache.org>
Authored: Mon Apr 6 13:58:43 2015 -0600
Committer: Shawn Feldman <sf...@apache.org>
Committed: Mon Apr 6 13:58:43 2015 -0600
----------------------------------------------------------------------
.../corepersistence/CpEntityManager.java | 2 +-
.../corepersistence/CpEntityManagerFactory.java | 121 +++++++++++--------
.../cassandra/EntityManagerFactoryImplIT.java | 31 +++--
3 files changed, 95 insertions(+), 59 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8b5ad3bd/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
index 5647d18..a254a5f 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
@@ -616,6 +616,7 @@ public class CpEntityManager implements EntityManager {
//delete from our UUID index
MapManager mm = getMapManagerForTypes();
mm.delete( entityRef.getUuid().toString() );
+
}
@@ -640,7 +641,6 @@ public class CpEntityManager implements EntityManager {
if ( entity != null ) {
decrementEntityCollection( Schema.defaultCollectionName( entityId.getType() ) );
-
// and finally...
return ecm.delete( entityId );
}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8b5ad3bd/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 1a9091e..647629d 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
@@ -31,6 +31,7 @@ import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.usergrid.persistence.graph.impl.SimpleEdge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
@@ -302,46 +303,73 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
// find application_info for application to delete
- final EntityManager em = getEntityManager(getManagementAppId());
- final Results results = em.searchCollection(em.getApplicationRef(), CpNamingUtils.APPLICATION_INFOS,
- Query.fromQL("select * where " + PROPERTY_APPLICATION_ID + " = " + applicationId.toString()));
- Entity appInfoToDelete = results.getEntity();
+ final EntityManager managementEm = getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
+ final Application app = managementEm.getApplication();
+ final Id managementAppId = new SimpleId( app.getUuid(), app.getType() );
+ final ApplicationScope managementAppScope = new ApplicationScopeImpl(managementAppId);
+ final GraphManager managementGraphManager = managerCache.getGraphManager(managementAppScope);
- // ensure that there is not already a deleted app with the same name
+ String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS);
- final EntityRef alias = em.getAlias(
- CpNamingUtils.DELETED_APPLICATION_INFO, appInfoToDelete.getName());
- if ( alias != null ) {
- throw new ConflictException("Cannot delete app with same name as already deleted app");
- }
-
- // make a copy of the app to delete application_info entity
- // and put it in a deleted_application_info collection
-
- Entity deletedApp = em.create(
- CpNamingUtils.DELETED_APPLICATION_INFO, appInfoToDelete.getProperties());
-
- // copy its connections too
+ Observable<Edge> appInfoEdges = managementGraphManager.loadEdgesFromSource(new SimpleSearchByEdgeType(
+ managementAppId, edgeType, Long.MAX_VALUE,
+ SearchByEdgeType.Order.DESCENDING, null));
- final Set<String> connectionTypes = em.getConnectionTypes(appInfoToDelete);
- for ( String connType : connectionTypes ) {
- final Results connResults =
- em.getConnectedEntities(appInfoToDelete, connType, null, Query.Level.ALL_PROPERTIES);
- for ( Entity entity : connResults.getEntities() ) {
- em.createConnection( deletedApp, connType, entity );
+ appInfoEdges.flatMap(appInfoEdge -> {
+ try {
+ Entity appInfoToDelete = managementEm.get(appInfoEdge.getTargetNode().getUuid());
+ final Id deletedAppId = new SimpleId(appInfoToDelete.getUuid(), appInfoToDelete.getType());
+ final ApplicationScope deletedAppScope = new ApplicationScopeImpl(deletedAppId);
+ final ApplicationEntityIndex aei = entityIndexFactory.createApplicationEntityIndex(deletedAppScope);
+
+ // ensure that there is not already a deleted app with the same name
+ final EntityRef alias = managementEm.getAlias(
+ CpNamingUtils.DELETED_APPLICATION_INFO, appInfoToDelete.getName());
+ if (alias != null) {
+ throw new ConflictException("Cannot delete app with same name as already deleted app");
+ }
+ // make a copy of the app to delete application_info entity
+ // and put it in a deleted_application_info collection
+
+ final Entity deletedApp = managementEm.create(
+ CpNamingUtils.DELETED_APPLICATION_INFO, appInfoToDelete.getProperties());
+ // copy its connections too
+
+ final Set<String> connectionTypes = managementEm.getConnectionTypes(appInfoToDelete);
+ Observable copyConnections = Observable.from(connectionTypes).doOnNext(connType -> {
+ try {
+ final Results connResults =
+ managementEm.getConnectedEntities(appInfoToDelete, connType, null, Query.Level.ALL_PROPERTIES);
+ connResults.getEntities().forEach(entity -> {
+ try {
+ managementEm.createConnection(deletedApp, connType, entity);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ });
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ final Observable nodes = managementGraphManager.deleteNode(deletedAppId, Long.MAX_VALUE);
+ final Observable deleteIndex = aei.deleteApplication();
+
+ return Observable.concat(copyConnections, nodes, deleteIndex)
+ .doOnCompleted(() -> {
+ try {
+ managementEm.delete(appInfoToDelete);
+ applicationIdCache.evictAppId(appInfoToDelete.getName());
+ }catch (Exception e){
+ throw new RuntimeException(e);
+ }
+ });
+ } catch (Exception e) {
+ throw new RuntimeException(e);
}
- }
-
- // delete the app from the application_info collection
-
- em.delete(appInfoToDelete);
- applicationIdCache.evictAppId(appInfoToDelete.getName());
+ }).toBlocking().lastOrDefault(null);
-
- // TODO: clean up all graph edges from application_info to connected entities
-
- // TODO: purge application indexes?
}
@@ -453,31 +481,28 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
// 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() ) {
-
- Edge edge = iter.next();
+ edges.doOnNext(edge -> {
Id targetId = edge.getTargetNode();
- logger.debug("getApplications(): Processing edge from {}:{} to {}:{}", new Object[] {
+ logger.debug("getApplications(): Processing edge from {}:{} to {}:{}", new Object[]{
edge.getSourceNode().getType(), edge.getSourceNode().getUuid(),
edge.getTargetNode().getType(), edge.getTargetNode().getUuid()
});
org.apache.usergrid.persistence.model.entity.Entity appInfo =
- managerCache.getEntityCollectionManager( appScope ).load( targetId )
- .toBlocking().lastOrDefault(null);
+ managerCache.getEntityCollectionManager(appScope).load(targetId)
+ .toBlocking().lastOrDefault(null);
- if ( appInfo == null ) {
- logger.warn("Application {} in index but not found in collections", targetId );
- continue;
+ if (appInfo == null) {
+ logger.warn("Application {} has edge but not found in em", targetId);
+ return;
}
UUID applicationId = UUIDUtils.tryExtractUUID(
- appInfo.getField( PROPERTY_APPLICATION_ID ).getValue().toString() );
+ appInfo.getField(PROPERTY_APPLICATION_ID).getValue().toString());
- appMap.put( (String)appInfo.getField( PROPERTY_NAME ).getValue(), applicationId);
- }
+ appMap.put((String) appInfo.getField(PROPERTY_NAME).getValue(), applicationId);
+ }).toBlocking().lastOrDefault(null);
return appMap;
}
@@ -669,7 +694,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
return Arrays.asList(
managerCache.getEntityIndex( // management app
- new ApplicationScopeImpl(new SimpleId(getManagementAppId(), "application"))));
+ CpNamingUtils.getApplicationScope(getManagementAppId())));
}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8b5ad3bd/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 b64f0b5..33a59e1 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
@@ -23,12 +23,11 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
+import org.apache.usergrid.Application;
+import org.apache.usergrid.CoreApplication;
import org.apache.usergrid.persistence.*;
import org.apache.usergrid.utils.UUIDUtils;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -65,6 +64,10 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
emf = ConcurrentProcessSingleton.getInstance().getSpringResource().getBean( EntityManagerFactory.class );
}
+ @Rule
+ public Application app = new CoreApplication( setup );
+
+
@BeforeClass
public static void setup() throws Exception {
@@ -132,9 +135,12 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
Thread.sleep( 500 );
}
+ this.app.refreshIndex();
+
// delete the application
- setup.getEmf().deleteApplication( deletedAppId );
+ setup.getEmf().deleteApplication(deletedAppId);
+ this.app.refreshIndex();
// wait for it to appear in delete apps list
@@ -152,6 +158,7 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
return found;
}
};
+ this.app.refreshIndex();
boolean found = false;
for ( int i=0; i<maxRetries; i++) {
@@ -163,6 +170,7 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
}
}
assertTrue( "Deleted app must be found in in deleted apps collection", found );
+ this.app.refreshIndex();
// attempt to get entities in application's collections in various ways should all fail
@@ -177,6 +185,7 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
assertFalse( "Lookup of deleted app must fail", found );
// app must not be found in apps collection
+ this.app.refreshIndex();
for ( int i=0; i<maxRetries; i++ ) {
found = findApps.call( deletedAppId, emf.getApplications() );
@@ -186,18 +195,19 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
break;
}
}
- assertFalse( "Deleted app must not be found in apps collection", found );
+ assertFalse("Deleted app must not be found in apps collection", found);
// restore the app
- emf.refreshIndex();
+ this.app.refreshIndex();
+
- emf.restoreApplication( deletedAppId );
+ emf.restoreApplication(deletedAppId);
emf.rebuildAllIndexes(new EntityManagerFactory.ProgressObserver() {
@Override
public void onProgress(EntityRef entity) {
- logger.debug("Reindexing {}:{}", entity.getType(), entity.getUuid() );
+ logger.debug("Reindexing {}:{}", entity.getType(), entity.getUuid());
}
});
@@ -213,6 +223,7 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
}
}
assertFalse("Restored app found in deleted apps collection", found);
+ this.app.refreshIndex();
for(int i=0;i<maxRetries;i++){
found = findApps.call(deletedAppId,setup.getEmf().getApplications());
@@ -225,7 +236,7 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
assertTrue("Restored app not found in apps collection", found);
// TODO: this assertion should work!
- //assertNotNull(setup.getEmf().lookupApplication( orgName + "/" + appName ));
+ assertNotNull(setup.getEmf().lookupApplication( orgName + "/" + appName ));
}