Fixes issue with incorrectly not rolling version when no app-infos are present.


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

Branch: refs/heads/USERGRID-933
Commit: 9294af73ff1af563fd06d9b5dc6a04e37dc76788
Parents: 65d2608
Author: Todd Nine <tn...@apigee.com>
Authored: Thu Sep 17 11:53:09 2015 -0600
Committer: Todd Nine <tn...@apigee.com>
Committed: Thu Sep 17 11:53:09 2015 -0600

----------------------------------------------------------------------
 .../management/AppInfoMigrationPlugin.java      | 234 +++++++++----------
 1 file changed, 116 insertions(+), 118 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/9294af73/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
----------------------------------------------------------------------
diff --git 
a/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
 
b/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
index ff73402..54779b9 100644
--- 
a/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
+++ 
b/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
@@ -20,11 +20,21 @@
 package org.apache.usergrid.management;
 
 
-import com.google.common.base.Optional;
-import com.google.inject.Inject;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.BeanFactory;
+
 import org.apache.usergrid.corepersistence.util.CpEntityMapUtils;
 import org.apache.usergrid.corepersistence.util.CpNamingUtils;
-import org.apache.usergrid.persistence.*;
+import org.apache.usergrid.persistence.EntityFactory;
+import org.apache.usergrid.persistence.EntityManager;
+import org.apache.usergrid.persistence.EntityManagerFactory;
+import org.apache.usergrid.persistence.EntityRef;
+import org.apache.usergrid.persistence.Schema;
 import org.apache.usergrid.persistence.collection.EntityCollectionManager;
 import 
org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory;
 import 
org.apache.usergrid.persistence.core.migration.data.MigrationInfoSerialization;
@@ -42,21 +52,17 @@ import 
org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdge;
 import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
 import org.apache.usergrid.persistence.model.entity.Entity;
 import org.apache.usergrid.persistence.model.entity.Id;
-import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.apache.usergrid.utils.UUIDUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.BeanFactory;
-import rx.Observable;
 
-import java.util.HashMap;
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.atomic.AtomicInteger;
+import com.google.common.base.Optional;
+import com.google.inject.Inject;
+
+import rx.Observable;
 
 import static 
org.apache.usergrid.corepersistence.util.CpNamingUtils.getApplicationScope;
 import static org.apache.usergrid.persistence.Schema.PROPERTY_NAME;
 
+
 /**
  * Migration of appinfos collection to application_info collection.
  *
@@ -68,7 +74,7 @@ public class AppInfoMigrationPlugin implements 
MigrationPlugin {
     /** Old and deprecated SYSTEM_APP */
     public static final UUID SYSTEM_APP_ID = UUID.fromString( 
"b6768a08-b5d5-11e3-a495-10ddb1de66c3" );
 
-    private static final Logger logger = 
LoggerFactory.getLogger(AppInfoMigrationPlugin.class);
+    private static final Logger logger = LoggerFactory.getLogger( 
AppInfoMigrationPlugin.class );
 
     public static String PLUGIN_NAME = "appinfo-migration";
 
@@ -85,27 +91,23 @@ public class AppInfoMigrationPlugin implements 
MigrationPlugin {
     final private GraphManagerFactory graphManagerFactory;
     private final ManagementService managementService;
 
+
     @Inject
-    public AppInfoMigrationPlugin(
-        EntityManagerFactory emf,
-        MigrationInfoSerialization migrationInfoSerialization,
-        EntityCollectionManagerFactory entityCollectionManagerFactory,
-        GraphManagerFactory graphManagerFactory,
-        BeanFactory beanFactory) {
+    public AppInfoMigrationPlugin( EntityManagerFactory emf, 
MigrationInfoSerialization migrationInfoSerialization,
+                                   EntityCollectionManagerFactory 
entityCollectionManagerFactory,
+                                   GraphManagerFactory graphManagerFactory, 
BeanFactory beanFactory ) {
 
         this.emf = emf;
         this.migrationInfoSerialization = migrationInfoSerialization;
         this.entityCollectionManagerFactory = entityCollectionManagerFactory;
         this.graphManagerFactory = graphManagerFactory;
-        this.managementService = beanFactory.getBean(ManagementService.class);
+        this.managementService = beanFactory.getBean( ManagementService.class 
);
     }
 
-    public AppInfoMigrationPlugin(
-        EntityManagerFactory emf,
-        MigrationInfoSerialization migrationInfoSerialization,
-        EntityCollectionManagerFactory entityCollectionManagerFactory,
-        GraphManagerFactory graphManagerFactory,
-        ManagementService managementService) {
+
+    public AppInfoMigrationPlugin( EntityManagerFactory emf, 
MigrationInfoSerialization migrationInfoSerialization,
+                                   EntityCollectionManagerFactory 
entityCollectionManagerFactory,
+                                   GraphManagerFactory graphManagerFactory, 
ManagementService managementService ) {
 
         this.emf = emf;
         this.migrationInfoSerialization = migrationInfoSerialization;
@@ -134,12 +136,12 @@ public class AppInfoMigrationPlugin implements 
MigrationPlugin {
 
 
     @Override
-    public void run(ProgressObserver observer) {
+    public void run( ProgressObserver observer ) {
 
-        final int version = migrationInfoSerialization.getVersion(getName());
+        final int version = migrationInfoSerialization.getVersion( getName() );
 
-        if (version == getMaxVersion()) {
-            logger.debug("Skipping Migration Plugin: " + getName());
+        if ( version == getMaxVersion() ) {
+            logger.debug( "Skipping Migration Plugin: " + getName() );
             return;
         }
 
@@ -147,80 +149,78 @@ public class AppInfoMigrationPlugin implements 
MigrationPlugin {
         AtomicInteger count = new AtomicInteger();
         //get old app infos to migrate
         final Observable<Entity> oldAppInfos = getOldAppInfos();
-        oldAppInfos
-            .doOnNext(oldAppInfoEntity -> {
-                try {
-                    migrateAppInfo(oldAppInfoEntity, observer);
+        oldAppInfos.doOnNext( oldAppInfoEntity -> {
+            try {
+                migrateAppInfo( oldAppInfoEntity, observer );
+                count.incrementAndGet();
+            }
+            catch ( Exception e ) {
+                if ( e.getCause() instanceof ApplicationAlreadyExistsException 
) {
                     count.incrementAndGet();
-                }catch (Exception e){
-                    if(e.getCause() instanceof 
ApplicationAlreadyExistsException){
-                        count.incrementAndGet();
-                        logger.info("Application already 
migrated",oldAppInfoEntity.getId().getUuid());
-                    }else {
-                        logger.error("Failed to migrate app info" + 
oldAppInfoEntity.getId().getUuid(), e);
-                        throw new RuntimeException(e);
-                    }
+                    logger.info( "Application already migrated", 
oldAppInfoEntity.getId().getUuid() );
                 }
-
-            })
-            .doOnCompleted(() -> {
-                if(count.get()>0) {
-                    migrationInfoSerialization.setVersion(getName(), 
getMaxVersion());
-                }else{
-                    logger.error("Failed to migrate any app infos");
+                else {
+                    logger.error( "Failed to migrate app info" + 
oldAppInfoEntity.getId().getUuid(), e );
+                    throw new RuntimeException( e );
                 }
-                observer.complete();
-            }).toBlocking().lastOrDefault(null);
-
+            }
+        } ).doOnCompleted( () -> {
+            migrationInfoSerialization.setVersion( getName(), getMaxVersion() 
);
+            observer.complete();
+        } ).toBlocking().lastOrDefault( null );
     }
 
 
-    private void migrateAppInfo( 
org.apache.usergrid.persistence.model.entity.Entity oldAppInfoEntity, 
ProgressObserver observer) {
+    private void migrateAppInfo( 
org.apache.usergrid.persistence.model.entity.Entity oldAppInfoEntity,
+                                 ProgressObserver observer ) {
         // Get appinfos from the Graph, we don't expect many so use iterator
         final EntityManager managementEm = emf.getManagementEntityManager();
 
-        Map oldAppInfoMap = CpEntityMapUtils.toMap(oldAppInfoEntity);
+        Map oldAppInfoMap = CpEntityMapUtils.toMap( oldAppInfoEntity );
 
-        final String name = (String) oldAppInfoMap.get(PROPERTY_NAME);
+        final String name = ( String ) oldAppInfoMap.get( PROPERTY_NAME );
 
         try {
-            final String orgName = name.split("/")[0];
-            final String appName = name.split("/")[1];
-            UUID applicationId = getUuid(oldAppInfoMap,"applicationUuid") ;
-            applicationId = applicationId == null ?  
getUuid(oldAppInfoMap,"appUuid") : applicationId ;
+            final String orgName = name.split( "/" )[0];
+            final String appName = name.split( "/" )[1];
+            UUID applicationId = getUuid( oldAppInfoMap, "applicationUuid" );
+            applicationId = applicationId == null ? getUuid( oldAppInfoMap, 
"appUuid" ) : applicationId;
             //get app info from graph to see if it has been migrated already
-            org.apache.usergrid.persistence.Entity appInfo = 
getApplicationInfo(applicationId);
-            if (appInfo == null) {
+            org.apache.usergrid.persistence.Entity appInfo = 
getApplicationInfo( applicationId );
+            if ( appInfo == null ) {
 
-                observer.update(getMaxVersion(), "Created application_info for 
" + appName);
+                observer.update( getMaxVersion(), "Created application_info 
for " + appName );
                 // create org->app connections, but not for apps in dummy 
"usergrid" internal organization
-                if(!orgName.equals("usergrid")) { //avoid management org
+                if ( !orgName.equals( "usergrid" ) ) { //avoid management org
 
-                    EntityRef orgRef = 
managementEm.getAlias(Group.ENTITY_TYPE, orgName);
+                    EntityRef orgRef = managementEm.getAlias( 
Group.ENTITY_TYPE, orgName );
                     // create and connect new APPLICATION_INFO oldAppInfo to 
Organization
-                    managementService.createApplication(orgRef.getUuid(), 
name, applicationId, null);
+                    managementService.createApplication( orgRef.getUuid(), 
name, applicationId, null );
                 }
-            } else {
+            }
+            else {
                 //already migrated don't do anything
-                observer.update(getMaxVersion(), "Received existing 
application_info for " + appName + " don't do anything");
+                observer.update( getMaxVersion(),
+                    "Received existing application_info for " + appName + " 
don't do anything" );
             }
-
-        } catch (Exception e) {
+        }
+        catch ( Exception e ) {
             String msg = "Exception writing application_info for " + name;
-            logger.error(msg, e);
-            observer.failed(getMaxVersion(), msg);
-            throw new RuntimeException(e);
+            logger.error( msg, e );
+            observer.failed( getMaxVersion(), msg );
+            throw new RuntimeException( e );
         }
-
     }
 
-    private UUID getUuid(Map oldAppInfoMap, String key) {
+
+    private UUID getUuid( Map oldAppInfoMap, String key ) {
         UUID applicationId;
-        Object uuidObject = oldAppInfoMap.get(key);
-        if (uuidObject instanceof UUID) {
-            applicationId = (UUID) uuidObject;
-        } else {
-            applicationId = uuidObject == null ? null : 
UUIDUtils.tryExtractUUID(uuidObject.toString());
+        Object uuidObject = oldAppInfoMap.get( key );
+        if ( uuidObject instanceof UUID ) {
+            applicationId = ( UUID ) uuidObject;
+        }
+        else {
+            applicationId = uuidObject == null ? null : 
UUIDUtils.tryExtractUUID( uuidObject.toString() );
         }
         return applicationId;
     }
@@ -231,84 +231,82 @@ public class AppInfoMigrationPlugin implements 
MigrationPlugin {
      */
     private org.apache.usergrid.persistence.Entity getApplicationInfo( final 
UUID appId ) throws Exception {
 
-        final ApplicationScope managementAppScope = 
getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
-        final EntityCollectionManager managementCollectionManager = 
entityCollectionManagerFactory.createCollectionManager(managementAppScope);
+        final ApplicationScope managementAppScope = getApplicationScope( 
CpNamingUtils.MANAGEMENT_APPLICATION_ID );
+        final EntityCollectionManager managementCollectionManager =
+            entityCollectionManagerFactory.createCollectionManager( 
managementAppScope );
 
-        Observable<Edge> edgesObservable = getApplicationInfoEdges(appId);
+        Observable<Edge> edgesObservable = getApplicationInfoEdges( appId );
         //get the graph for all app infos
-        Observable<org.apache.usergrid.persistence.model.entity.Entity> 
entityObs =
-            edgesObservable.flatMap(edge -> {
-                final Id appInfoId = edge.getTargetNode();
-                return managementCollectionManager
-                    .load(appInfoId)
-                    .filter( entity ->{
-                        //check for app id
-                        return  entity != null ? 
entity.getId().getUuid().equals(appId) : false;
-                    });
-            });
+        Observable<org.apache.usergrid.persistence.model.entity.Entity> 
entityObs = edgesObservable.flatMap( edge -> {
+            final Id appInfoId = edge.getTargetNode();
+            return managementCollectionManager.load( appInfoId ).filter( 
entity -> {
+                //check for app id
+                return entity != null ? entity.getId().getUuid().equals( appId 
) : false;
+            } );
+        } );
 
         // don't expect many applications, so we block
         org.apache.usergrid.persistence.model.entity.Entity applicationInfo =
-            entityObs.toBlocking().lastOrDefault(null);
+            entityObs.toBlocking().lastOrDefault( null );
 
         if ( applicationInfo == null ) {
             return null;
         }
 
-        Class clazz = 
Schema.getDefaultSchema().getEntityClass(applicationInfo.getId().getType());
+        Class clazz = Schema.getDefaultSchema().getEntityClass( 
applicationInfo.getId().getType() );
 
-        org.apache.usergrid.persistence.Entity entity = 
EntityFactory.newEntity(
-            applicationInfo.getId().getUuid(), 
applicationInfo.getId().getType(), clazz);
+        org.apache.usergrid.persistence.Entity entity =
+            EntityFactory.newEntity( applicationInfo.getId().getUuid(), 
applicationInfo.getId().getType(), clazz );
 
         entity.setProperties( CpEntityMapUtils.toMap( applicationInfo ) );
 
         return entity;
-
     }
 
 
     /**
      * Use Graph to get old appinfos from the old and deprecated System App.
      */
-    public Observable<org.apache.usergrid.persistence.model.entity.Entity> 
getOldAppInfos( ) {
+    public Observable<org.apache.usergrid.persistence.model.entity.Entity> 
getOldAppInfos() {
 
-        final ApplicationScope systemAppScope = 
getApplicationScope(SYSTEM_APP_ID);
+        final ApplicationScope systemAppScope = getApplicationScope( 
SYSTEM_APP_ID );
 
         final EntityCollectionManager systemCollectionManager =
-            
entityCollectionManagerFactory.createCollectionManager(systemAppScope);
+            entityCollectionManagerFactory.createCollectionManager( 
systemAppScope );
 
-        final GraphManager gm = 
graphManagerFactory.createEdgeManager(systemAppScope);
+        final GraphManager gm = graphManagerFactory.createEdgeManager( 
systemAppScope );
 
-        String edgeType = 
CpNamingUtils.getEdgeTypeFromCollectionName("appinfos");
+        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( 
"appinfos" );
 
         Id rootAppId = systemAppScope.getApplication();
 
-        final SimpleSearchByEdgeType simpleSearchByEdgeType =  new 
SimpleSearchByEdgeType(
-            rootAppId, edgeType, Long.MAX_VALUE, 
SearchByEdgeType.Order.DESCENDING, Optional.absent());
+        final SimpleSearchByEdgeType simpleSearchByEdgeType =
+            new SimpleSearchByEdgeType( rootAppId, edgeType, Long.MAX_VALUE, 
SearchByEdgeType.Order.DESCENDING,
+                Optional.absent() );
 
         Observable<org.apache.usergrid.persistence.model.entity.Entity> 
entityObs =
-            gm.loadEdgesFromSource( simpleSearchByEdgeType )
-                .flatMap(edge -> {
-                    final Id appInfoId = edge.getTargetNode();
+            gm.loadEdgesFromSource( simpleSearchByEdgeType ).flatMap( edge -> {
+                final Id appInfoId = edge.getTargetNode();
 
-                    return systemCollectionManager.load(appInfoId)
-                        .filter(entity -> (entity != null));
-                });
+                return systemCollectionManager.load( appInfoId ).filter( 
entity -> ( entity != null ) );
+            } );
 
         return entityObs;
     }
 
-    public Observable<Edge> getApplicationInfoEdges(final UUID applicationId) {
-        final ApplicationScope managementAppScope = 
getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
-        final GraphManager gm = 
graphManagerFactory.createEdgeManager(managementAppScope);
 
-        String edgeType = 
CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS);
+    public Observable<Edge> getApplicationInfoEdges( final UUID applicationId 
) {
+        final ApplicationScope managementAppScope = getApplicationScope( 
CpNamingUtils.MANAGEMENT_APPLICATION_ID );
+        final GraphManager gm = graphManagerFactory.createEdgeManager( 
managementAppScope );
+
+        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( 
CpNamingUtils.APPLICATION_INFOS );
 
 
-        final SimpleSearchByEdge simpleSearchByEdgeType =  new 
SimpleSearchByEdge(
-            
CpNamingUtils.generateApplicationId(CpNamingUtils.MANAGEMENT_APPLICATION_ID), 
edgeType, CpNamingUtils.generateApplicationId( applicationId ), Long.MAX_VALUE, 
SearchByEdgeType.Order.DESCENDING,
-            Optional.absent());
+        final SimpleSearchByEdge simpleSearchByEdgeType =
+            new SimpleSearchByEdge( CpNamingUtils.generateApplicationId( 
CpNamingUtils.MANAGEMENT_APPLICATION_ID ),
+                edgeType, CpNamingUtils.generateApplicationId( applicationId 
), Long.MAX_VALUE,
+                SearchByEdgeType.Order.DESCENDING, Optional.absent() );
 
-        return gm.loadEdgeVersions(simpleSearchByEdgeType  );
+        return gm.loadEdgeVersions( simpleSearchByEdgeType );
     }
 }

Reply via email to