Service manager's init() method now retries if unable to get application.

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

Branch: refs/heads/usergrid-1268-akka-211
Commit: 223521e4150e520c92560bbea873b6a024e18a0e
Parents: 29e940e
Author: Dave Johnson <snoopd...@apache.org>
Authored: Thu May 5 12:40:29 2016 -0400
Committer: Dave Johnson <snoopd...@apache.org>
Committed: Thu May 5 12:40:29 2016 -0400

----------------------------------------------------------------------
 .../main/resources/usergrid-default.properties  |  5 ++
 .../usergrid/services/ServiceManager.java       | 52 +++++++++++++++++---
 2 files changed, 49 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/223521e4/stack/config/src/main/resources/usergrid-default.properties
----------------------------------------------------------------------
diff --git a/stack/config/src/main/resources/usergrid-default.properties 
b/stack/config/src/main/resources/usergrid-default.properties
index 4f57cdd..35e12ae 100644
--- a/stack/config/src/main/resources/usergrid-default.properties
+++ b/stack/config/src/main/resources/usergrid-default.properties
@@ -628,6 +628,11 @@ usergrid.auth.cache.inmemory.size=3000
 # all (= in + out)'
 usergrid.rest.default-connection-param=all
 
+# In a busy cluster the Service Manager's init() may fail (usually a time-out) 
on the first
+# attempt to communicate with Cassandra or ElasticSearch so we retry on 
failure, using an
+# interval that is longer than the default (10s) Cassandra connection time-out.
+service.manager.retry.interval=15000
+service.manager.max.retries=5
 
 
 ##############################  Usergrid Testing  #############################

http://git-wip-us.apache.org/repos/asf/usergrid/blob/223521e4/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 a9892f5..1ed73d6 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
@@ -50,7 +50,6 @@ import static 
org.apache.usergrid.utils.InflectionUtils.pluralize;
 
 public class ServiceManager {
 
-
     private static final Logger logger = LoggerFactory.getLogger( 
ServiceManager.class );
 
     /** A pointer that signals we couldn't find a class */
@@ -69,6 +68,10 @@ 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;
 
     private UUID applicationId;
@@ -96,27 +99,60 @@ 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 {
-                application = em.getApplication();
-                if(application == null){
-                    Exception e = new RuntimeException("application id 
{"+em.getApplicationId()+"} is returning null");
-                    logger.error("Failed to get application",e);
+                int retryCount = 0;
+                boolean appNotFound = true;
+
+                while ( appNotFound && retryCount <= maxRetries ) {
+
+                    application = em.getApplication();
+
+                    if ( application != null ) {
+                        appNotFound = false;
+                        applicationId = application.getUuid();
+                    } else {
+                        Thread.sleep( retryInterval );
+                        retryCount++;
+                    }
+                }
+
+                if ( application == null ) {
+                    Exception e = new RuntimeException( "application id {" + 
em.getApplicationId() + "} is returning null" );
+                    logger.error( "Failed to get application", e );
                     throw e;
                 }
-                applicationId = application.getUuid();
-            }
-            catch ( Exception e ) {
+
+            } 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;
     }
 

Reply via email to