From 8f303c6e98b279e2c8c8f63d5930fa484fb192b5 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Wed, 20 Mar 2013 17:41:50 +0000
Subject: [PATCH] [BIS-374] Startup of DSS is flaky Make the connection of DSS
 to AS retriable.

SVN: 28661
---
 .../dss/generic/server/DataStoreServer.java   | 62 ++++++++++++++++---
 .../server/EncapsulatedOpenBISService.java    | 55 ++++++++++++++--
 2 files changed, 101 insertions(+), 16 deletions(-)

diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
index 46e3bbe87da..1d24af3cac8 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
@@ -56,10 +56,14 @@ import org.springframework.web.context.WebApplicationContext;
 import com.googlecode.jsonrpc4j.spring.JsonServiceExporter;
 import com.marathon.util.spring.StreamSupportingHttpInvokerServiceExporter;
 
+import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.common.api.IRpcServiceNameServer;
 import ch.systemsx.cisd.common.api.RpcServiceInterfaceVersionDTO;
+import ch.systemsx.cisd.common.api.retry.RetryCaller;
+import ch.systemsx.cisd.common.api.retry.config.RetryConfiguration;
 import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException;
 import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
+import ch.systemsx.cisd.common.logging.Log4jSimpleLogger;
 import ch.systemsx.cisd.common.logging.LogCategory;
 import ch.systemsx.cisd.common.logging.LogFactory;
 import ch.systemsx.cisd.common.logging.LogInitializer;
@@ -171,6 +175,7 @@ public class DataStoreServer
         } catch (final Exception ex)
         {
             operationLog.error("Failed to start server.", ex);
+            throw CheckedExceptionTunnel.wrapIfNecessary(ex);
         }
     }
 
@@ -426,23 +431,60 @@ public class DataStoreServer
             return configParams.isUseNIO() ? new SelectChannelConnector() : new SocketConnector();
         }
     }
-
-    private final static void selfTest(final ApplicationContext applicationContext)
+    
+    private static class RetryingSelfTest extends RetryCaller<Void, RuntimeException>
     {
-        IEncapsulatedOpenBISService dataSetService = applicationContext.getDataSetService();
-        final int version = dataSetService.getVersion();
-        if (IServer.VERSION != version)
+        private final ApplicationContext applicationContext;
+        
+        RetryingSelfTest(ApplicationContext applicationContext)
         {
-            throw new ConfigurationFailureException(
-                    "This client has the wrong service version for the server (client: "
-                            + IServer.VERSION + ", server: " + version + ").");
+            super(new RetryConfiguration()
+                {
+                    @Override
+                    public float getWaitingTimeBetweenRetriesIncreasingFactor()
+                    {
+                        return 2;
+                    }
+                    
+                    @Override
+                    public int getWaitingTimeBetweenRetries()
+                    {
+                        return 5000;
+                    }
+                    
+                    @Override
+                    public int getMaximumNumberOfRetries()
+                    {
+                        return 5;
+                    }
+                }, new Log4jSimpleLogger(operationLog));
+            this.applicationContext = applicationContext;
         }
-        if (operationLog.isInfoEnabled())
+
+        @Override
+        protected Void call() throws RuntimeException
         {
-            operationLog.info("openBIS service (interface version " + version + ") is reachable");
+            IEncapsulatedOpenBISService dataSetService = applicationContext.getDataSetService();
+            final int version = dataSetService.getVersion();
+            if (IServer.VERSION != version)
+            {
+                throw new ConfigurationFailureException(
+                        "This client has the wrong service version for the server (client: "
+                                + IServer.VERSION + ", server: " + version + ").");
+            }
+            if (operationLog.isInfoEnabled())
+            {
+                operationLog.info("openBIS service (interface version " + version + ") is reachable");
+            }
+            return null;
         }
     }
 
+    private final static void selfTest(final ApplicationContext applicationContext)
+    {
+        new RetryingSelfTest(applicationContext).callWithRetry();
+    }
+
     public static ConfigParameters getConfigParameters()
     {
         if (configParameters == null)
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java
index 3a64aac5aac..a1fc378dcf2 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java
@@ -25,7 +25,10 @@ import org.apache.commons.lang.time.DateUtils;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.FactoryBean;
 
+import ch.systemsx.cisd.common.api.retry.RetryCaller;
+import ch.systemsx.cisd.common.api.retry.config.RetryConfiguration;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
+import ch.systemsx.cisd.common.logging.Log4jSimpleLogger;
 import ch.systemsx.cisd.common.logging.LogCategory;
 import ch.systemsx.cisd.common.logging.LogFactory;
 import ch.systemsx.cisd.openbis.common.api.client.ServiceFinder;
@@ -114,15 +117,55 @@ public final class EncapsulatedOpenBISService implements IEncapsulatedOpenBISSer
 
     private IServiceConversationClientManagerLocal conversationClient;
 
-    public static IServiceForDataStoreServer createOpenBisService(String openBISURL, String timeout)
+    private static class RetryingOpenBisCreator extends
+            RetryCaller<IServiceForDataStoreServer, RuntimeException>
     {
-        OpenBisServiceFactory factory =
-                new OpenBisServiceFactory(openBISURL, ResourceNames.ETL_SERVICE_URL);
-        if (timeout.startsWith("$"))
+        private final String openBISURL;
+
+        private String timeout;
+
+        RetryingOpenBisCreator(String openBISURL, String timeout)
         {
-            return factory.createService();
+            super(new RetryConfiguration()
+                {
+                    @Override
+                    public float getWaitingTimeBetweenRetriesIncreasingFactor()
+                    {
+                        return 2;
+                    }
+
+                    @Override
+                    public int getWaitingTimeBetweenRetries()
+                    {
+                        return 5000;
+                    }
+
+                    @Override
+                    public int getMaximumNumberOfRetries()
+                    {
+                        return 5;
+                    }
+                }, new Log4jSimpleLogger(operationLog));
+            this.openBISURL = openBISURL;
+            this.timeout = timeout;
         }
-        return factory.createService(normalizeTimeout(timeout));
+
+        @Override
+        protected IServiceForDataStoreServer call() throws RuntimeException
+        {
+            OpenBisServiceFactory factory =
+                    new OpenBisServiceFactory(openBISURL, ResourceNames.ETL_SERVICE_URL);
+            if (timeout.startsWith("$"))
+            {
+                return factory.createService();
+            }
+            return factory.createService(normalizeTimeout(timeout));
+        }
+    }
+
+    public static IServiceForDataStoreServer createOpenBisService(String openBISURL, String timeout)
+    {
+        return new RetryingOpenBisCreator(openBISURL, timeout).callWithRetry();
     }
 
     /**
-- 
GitLab