usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mru...@apache.org
Subject [01/26] usergrid git commit: Move the initial get of management app to the CpEntityManagerFactory with retries, and caching for the management app itself.
Date Fri, 03 Jun 2016 14:45:51 GMT
Repository: usergrid
Updated Branches:
  refs/heads/release-2.1.1 0970e1dfa -> a1cb1f5f4


Move the initial get of management app to the CpEntityManagerFactory with retries, and caching
for the management app itself.


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

Branch: refs/heads/release-2.1.1
Commit: e0c0c875271cda47f9baf9072f029a92921fd1be
Parents: 7fdca3d
Author: Dave Johnson <snoopdave@apache.org>
Authored: Mon May 16 16:10:07 2016 -0400
Committer: Dave Johnson <snoopdave@apache.org>
Committed: Mon May 16 16:10:07 2016 -0400

----------------------------------------------------------------------
 .../corepersistence/CpEntityManager.java        |  52 ++++-----
 .../corepersistence/CpEntityManagerFactory.java | 112 ++++++++++++++-----
 .../usergrid/services/ServiceManager.java       |  59 ++--------
 3 files changed, 113 insertions(+), 110 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/e0c0c875/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 68f5d71..c8f3253 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
@@ -36,6 +36,7 @@ import java.util.TreeSet;
 import java.util.UUID;
 import java.util.stream.Collectors;
 
+import org.apache.usergrid.persistence.*;
 import org.apache.usergrid.persistence.collection.EntitySet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -48,24 +49,7 @@ import org.apache.usergrid.corepersistence.service.CollectionService;
 import org.apache.usergrid.corepersistence.service.ConnectionService;
 import org.apache.usergrid.corepersistence.util.CpEntityMapUtils;
 import org.apache.usergrid.corepersistence.util.CpNamingUtils;
-import org.apache.usergrid.persistence.AggregateCounter;
-import org.apache.usergrid.persistence.AggregateCounterSet;
-import org.apache.usergrid.persistence.CollectionRef;
-import org.apache.usergrid.persistence.ConnectedEntityRef;
-import org.apache.usergrid.persistence.ConnectionRef;
-import org.apache.usergrid.persistence.Entity;
-import org.apache.usergrid.persistence.EntityFactory;
-import org.apache.usergrid.persistence.EntityManager;
-import org.apache.usergrid.persistence.EntityRef;
-import org.apache.usergrid.persistence.IndexBucketLocator;
-import org.apache.usergrid.persistence.Query;
 import org.apache.usergrid.persistence.Query.Level;
-import org.apache.usergrid.persistence.RelationManager;
-import org.apache.usergrid.persistence.Results;
-import org.apache.usergrid.persistence.Schema;
-import org.apache.usergrid.persistence.SimpleEntityRef;
-import org.apache.usergrid.persistence.SimpleRoleRef;
-import org.apache.usergrid.persistence.TypedEntity;
 import org.apache.usergrid.persistence.cassandra.ApplicationCF;
 import org.apache.usergrid.persistence.cassandra.CassandraPersistenceUtils;
 import org.apache.usergrid.persistence.cassandra.CassandraService;
@@ -230,6 +214,9 @@ public class CpEntityManager implements EntityManager {
 
     private EntityCollectionManager ecm;
 
+    private CpEntityManagerFactory emf;
+
+
     //    /** Short-term cache to keep us from reloading same Entity during single request.
*/
 //    private LoadingCache<EntityScope, org.apache.usergrid.persistence.model.entity.Entity>
entityCache;
 
@@ -242,18 +229,20 @@ public class CpEntityManager implements EntityManager {
      * @param metricsFactory
      * @param applicationId
      */
-    public CpEntityManager( final CassandraService cass,
-                            final CounterUtils counterUtils,
-                            final AsyncEventService indexService,
-                            final ManagerCache managerCache,
-                            final MetricsFactory metricsFactory,
-                            final EntityManagerFig entityManagerFig,
-                            final GraphManagerFactory graphManagerFactory,
-                            final CollectionService collectionService,
-                            final ConnectionService connectionService,
-                            final IndexSchemaCacheFactory indexSchemaCacheFactory,
-                            final UUID applicationId ) {
-
+    public CpEntityManager(final CpEntityManagerFactory emf,
+                           final CassandraService cass,
+                           final CounterUtils counterUtils,
+                           final AsyncEventService indexService,
+                           final ManagerCache managerCache,
+                           final MetricsFactory metricsFactory,
+                           final EntityManagerFig entityManagerFig,
+                           final GraphManagerFactory graphManagerFactory,
+                           final CollectionService collectionService,
+                           final ConnectionService connectionService,
+                           final IndexSchemaCacheFactory indexSchemaCacheFactory,
+                           final UUID applicationId ) {
+
+        this.emf = emf;
         this.entityManagerFig = entityManagerFig;
 
         Preconditions.checkNotNull( cass, "cass must not be null" );
@@ -270,8 +259,6 @@ public class CpEntityManager implements EntityManager {
         this.connectionService = connectionService;
         this.collectionService = collectionService;
 
-
-
         this.managerCache = managerCache;
         this.applicationId = applicationId;
         this.indexService = indexService;
@@ -755,6 +742,9 @@ public class CpEntityManager implements EntityManager {
     @Override
     public Application getApplication() throws Exception {
         if ( application == null ) {
+            if ( CpNamingUtils.MANAGEMENT_APPLICATION_ID.equals( applicationId )) {
+                return emf.getManagementApplication();
+            }
             application = get( applicationId, Application.class );
         }
         return application;

http://git-wip-us.apache.org/repos/asf/usergrid/blob/e0c0c875/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 91a936d..b61be01 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
@@ -16,11 +16,7 @@
 package org.apache.usergrid.corepersistence;
 
 
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-import java.util.UUID;
+import java.util.*;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -113,6 +109,8 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
     private final ApplicationIdCache applicationIdCache;
     //private final IndexSchemaCache indexSchemaCache;
 
+    Application managementApp = null;
+
     private ManagerCache managerCache;
 
     private CassandraService cassandraService;
@@ -126,8 +124,11 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
     private final GraphManagerFactory graphManagerFactory;
     private final IndexSchemaCacheFactory indexSchemaCacheFactory;
 
-    public CpEntityManagerFactory( final CassandraService cassandraService, final CounterUtils
counterUtils,
-                                   final Injector injector ) {
+    public static final String MANAGEMENT_APP_MAX_RETRIES= "management.app.max.retries";
+
+
+    public CpEntityManagerFactory(
+        final CassandraService cassandraService, final CounterUtils counterUtils, final Injector
injector ) {
 
         this.cassandraService = cassandraService;
         this.counterUtils = counterUtils;
@@ -142,12 +143,12 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
         this.connectionService = injector.getInstance( ConnectionService.class );
         this.indexSchemaCacheFactory = injector.getInstance( IndexSchemaCacheFactory.class
);
 
-        //this line always needs to be last due to the temporary cicular dependency until
spring is removed
+        // this line always needs to be last due to the temporary circular dependency until
spring is removed
 
         this.applicationIdCache = injector.getInstance(ApplicationIdCacheFactory.class).getInstance(
             getManagementEntityManager() );
 
-
+        initMgmtAppInternal();
     }
 
 
@@ -164,21 +165,61 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
 
     private void initMgmtAppInternal() {
 
+        Properties properties = cassandraService.getProperties();
+
+        Integer maxRetries;
+        try {
+            Object maxRetriesObject = properties.get( MANAGEMENT_APP_MAX_RETRIES ).toString();
+            maxRetries = Integer.parseInt( maxRetriesObject.toString() );
+        } catch ( NumberFormatException nfe ) {
+            maxRetries = 20;
+        }
+
         EntityManager em = getEntityManager(getManagementAppId());
+
+        int retryCount = 0;
+
+        while ( managementApp != null && retryCount++ <= maxRetries ) {
+
+            try {
+                managementApp = em.getApplication();
+
+                if ( managementApp == null ) {
+
+                    logger.warn( "Management application not found, attempting creation"
);
+
+                    Map mgmtAppProps = new HashMap<String, Object>();
+                    mgmtAppProps.put( PROPERTY_NAME, CassandraService.MANAGEMENT_APPLICATION
);
+                    em.create( getManagementAppId(), TYPE_APPLICATION, mgmtAppProps );
+                    managementApp = em.getApplication();
+                }
+
+            } catch ( Throwable t ) {
+                logger.warn("Error getting or creating management application after " + retryCount
+ " retries", t);
+            }
+        }
+
         indexService.queueInitializeApplicationIndex(CpNamingUtils.getApplicationScope(getManagementAppId()));
 
+        if ( managementApp == null ) {
+            throw new RuntimeException("FATAL ERROR: Failed to get or create management app");
+        }
+    }
+
+
+    public Application getManagementApplication() {
+
+        Application ret = null;
+        EntityManager em = getEntityManager(getManagementAppId());
         try {
-            if ( em.getApplication() == null ) {
-                logger.info("Creating management application");
-                Map mgmtAppProps = new HashMap<String, Object>();
-                mgmtAppProps.put(PROPERTY_NAME, CassandraService.MANAGEMENT_APPLICATION);
-                em.create( getManagementAppId(), TYPE_APPLICATION, mgmtAppProps);
-                em.getApplication();
-            }
+            ret = em.getApplication();
+            managementApp = ret;
 
-        } catch (Exception ex) {
-            throw new RuntimeException("Fatal error creating management application", ex);
+        } catch (Exception e) {
+            logger.warn("Error getting management app, returning cached copy version");
         }
+
+        return managementApp;
     }
 
 
@@ -201,9 +242,19 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
 
 
     private EntityManager _getEntityManager( UUID applicationId ) {
-        EntityManager em = new CpEntityManager(cassandraService, counterUtils, indexService,
managerCache,
-            metricsFactory, entityManagerFig, graphManagerFactory,  collectionService, connectionService,indexSchemaCacheFactory,
applicationId );
-
+        EntityManager em = new CpEntityManager(
+            this,
+            cassandraService,
+            counterUtils,
+            indexService,
+            managerCache,
+            metricsFactory,
+            entityManagerFig,
+            graphManagerFactory,
+            collectionService,
+            connectionService,
+            indexSchemaCacheFactory,
+            applicationId );
         return em;
     }
 
@@ -215,7 +266,8 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
     @Override
     public Entity createApplicationV2(
-        String orgName, String name, UUID applicationId, Map<String, Object> properties,
boolean forMigration) throws Exception {
+        String orgName, String name, UUID applicationId, Map<String, Object> properties,
boolean forMigration)
+        throws Exception {
 
         String appName = buildAppName( orgName, name );
 
@@ -317,8 +369,9 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
         // find application_info for application to delete
 
-        migrateAppInfo(applicationId, CpNamingUtils.APPLICATION_INFO, CpNamingUtils.DELETED_APPLICATION_INFOS,
CpNamingUtils.DELETED_APPLICATION_INFO).toBlocking()
-            .lastOrDefault( null );
+        migrateAppInfo(
+            applicationId, CpNamingUtils.APPLICATION_INFO, CpNamingUtils.DELETED_APPLICATION_INFOS,
+            CpNamingUtils.DELETED_APPLICATION_INFO).toBlocking().lastOrDefault( null );
     }
 
     //TODO: return status for restore
@@ -360,9 +413,13 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
      * @return
      * @throws Exception
      */
-    private Observable migrateAppInfo(final UUID applicationUUID,  final String deleteTypeName,
final String createCollectionName, final String createTypeName ) throws Exception {
+    private Observable migrateAppInfo(
+        final UUID applicationUUID,  final String deleteTypeName, final String createCollectionName,
+        final String createTypeName ) throws Exception {
+
+        final ApplicationScope managementAppScope =
+            CpNamingUtils.getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
 
-        final ApplicationScope managementAppScope = CpNamingUtils.getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
         final EntityManager managementEm = getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
 
         //the application id we will be removing
@@ -417,7 +474,8 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
         final Id managementAppId = CpNamingUtils.getManagementApplicationId();
         final EntityIndex aei = getManagementIndex();
         final GraphManager managementGraphManager = managerCache.getGraphManager(managementAppScope);
-        final Edge createEdge = CpNamingUtils.createCollectionEdge(managementAppId, createCollectionName,
createApplicationId);
+        final Edge createEdge =
+            CpNamingUtils.createCollectionEdge(managementAppId, createCollectionName, createApplicationId);
 
         final Observable createNodeGraph = managementGraphManager.writeEdge(createEdge);
 

http://git-wip-us.apache.org/repos/asf/usergrid/blob/e0c0c875/stack/services/src/main/java/org/apache/usergrid/services/ServiceManager.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/ServiceManager.java
b/stack/services/src/main/java/org/apache/usergrid/services/ServiceManager.java
index ef1baaf..993bf35 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/ServiceManager.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/ServiceManager.java
@@ -68,9 +68,6 @@ public class ServiceManager {
     public static final String APPLICATION_REQUESTS_PER = APPLICATION_REQUESTS + ".";
     public static final String IMPL = "Impl";
 
-    public static final String SERVICE_MANAGER_RETRY_INTERVAL = "service.manager.retry.interval";
-
-    public static final String SERVICE_MANAGER_MAX_RETRIES= "service.manager.max.retries";
 
     private Application application;
 
@@ -99,69 +96,27 @@ public class ServiceManager {
         this.qm = qm;
         this.properties = properties;
 
-        Integer retryInterval;
-        try {
-            Object retryIntervalObject = properties.get( SERVICE_MANAGER_RETRY_INTERVAL ).toString();
-            retryInterval = Integer.parseInt( retryIntervalObject.toString() );
-        } catch ( NumberFormatException nfe ) {
-            retryInterval = 15000;
-        }
-
-        Integer maxRetries;
-        try {
-            Object maxRetriesObject = properties.get( SERVICE_MANAGER_MAX_RETRIES ).toString();
-            maxRetries = Integer.parseInt( maxRetriesObject.toString() );
-        } catch ( NumberFormatException nfe ) {
-            maxRetries = 5;
-        }
-
         if ( em != null ) {
-
             try {
-                int retryCount = 0;
-                boolean appNotFound = true;
-
-                while ( appNotFound && retryCount <= maxRetries ) {
-
-                    application = em.getApplication();
-
-                    if ( application != null ) {
-                        appNotFound = false;
-                        applicationId = application.getUuid();
-
-                    } else {
-                        // Cassandra may be alive but responding very slowly, let's wait
and retry
-                        logger.error("STARTUP PROBLEM: Cannot get application by UUID. Will
retry in {} seconds #{}",
-                            retryInterval/1000, retryCount);
-                        Thread.sleep( retryInterval );
-                        retryCount++;
-                    }
-                }
-
-                if ( application == null ) {
-                    Exception e = new RuntimeException(
-                        "STARTUP FAILURE: application id {" + em.getApplicationId()
-                            + "} is returning null after " + retryCount + " retries" );
+                application = em.getApplication();
+                if(application == null){
+                    Exception e = new RuntimeException("application id {"+em.getApplicationId()+"}
is returning null");
+                    logger.error("Failed to get application",e);
                     throw e;
                 }
-
-            } catch ( RuntimeException re ) {
-                logger.error( "ServiceManager init failure", re );
-                throw re;
-
-            } catch ( Exception e ) {
+                applicationId = application.getUuid();
+            }
+            catch ( Exception e ) {
                 logger.error( "ServiceManager init failure", e );
                 throw new RuntimeException( e );
             }
         }
-
         if ( properties != null ) {
             String packages = properties.getProperty( SERVICE_PACKAGE_PREFIXES );
             if ( !StringUtils.isEmpty( packages ) ) {
                 setServicePackagePrefixes( packages );
             }
         }
-
         return this;
     }
 


Mime
View raw message