[jboss-cvs] JBossAS SVN: r94996 - projects/cluster/ha-server-cache-jbc/trunk/src/main/java/org/jboss/web/tomcat/service/session/distributedcache/impl/jbc.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Oct 15 22:17:55 EDT 2009


Author: bstansberry at jboss.com
Date: 2009-10-15 22:17:55 -0400 (Thu, 15 Oct 2009)
New Revision: 94996

Modified:
   projects/cluster/ha-server-cache-jbc/trunk/src/main/java/org/jboss/web/tomcat/service/session/distributedcache/impl/jbc/JBossCacheWrapper.java
Log:
Add a random backoff before retrying

Modified: projects/cluster/ha-server-cache-jbc/trunk/src/main/java/org/jboss/web/tomcat/service/session/distributedcache/impl/jbc/JBossCacheWrapper.java
===================================================================
--- projects/cluster/ha-server-cache-jbc/trunk/src/main/java/org/jboss/web/tomcat/service/session/distributedcache/impl/jbc/JBossCacheWrapper.java	2009-10-16 02:17:16 UTC (rev 94995)
+++ projects/cluster/ha-server-cache-jbc/trunk/src/main/java/org/jboss/web/tomcat/service/session/distributedcache/impl/jbc/JBossCacheWrapper.java	2009-10-16 02:17:55 UTC (rev 94996)
@@ -22,6 +22,7 @@
 package org.jboss.web.tomcat.service.session.distributedcache.impl.jbc;
 
 import java.util.Map;
+import java.util.Random;
 
 import org.jboss.cache.Cache;
 import org.jboss.cache.CacheException;
@@ -35,7 +36,9 @@
    private static final String RETRY_FAIL_MSG = 
       "Continued to catch TimeoutException during " + 
        RETRY + " retry attempts. Giving up.";
+   private static final int[] BACK_OFF_INTERVALS = { 10, 100 };
    private Cache<Object, Object> plainCache_;
+   private final Random random = new Random(System.currentTimeMillis());
    
    JBossCacheWrapper(Cache<Object, Object> cache)
    {
@@ -48,7 +51,7 @@
       for (int i = 0; i < RETRY; i++)
       {
          try
-         {            
+         {             
             if (gravitate)
             {            
                plainCache_.getInvocationContext().getOptionOverrides()
@@ -64,6 +67,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return null;
+         }
       }      
       throw new RuntimeException(RETRY_FAIL_MSG, ex);      
    }
@@ -109,6 +117,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return null;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
@@ -140,6 +153,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
@@ -170,6 +188,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
@@ -199,6 +222,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return null;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
@@ -226,6 +254,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return null;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
@@ -254,6 +287,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
@@ -280,6 +318,11 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
@@ -300,9 +343,40 @@
          {
             ex = e;
          }
+         
+         if (!backOff(i))
+         {
+            return;
+         }
       }
       
       throw new RuntimeException(RETRY_FAIL_MSG, ex);
    }
-
+   
+   /**
+    * Causes the calling thread to sleep a random amount of time up to
+    * BACK_OFF_INTERVALS[attempt].
+    * 
+    * @param attempt zero based count of the retry count after failure of which
+    *                this method was invoked
+    *                
+    * @return <code>true</code> if it is safe for the operation to retry; false
+    *         if an InterruptedException was caught while sleeping
+    */
+   private boolean backOff(int attempt)
+   {
+      if (attempt < BACK_OFF_INTERVALS.length)
+      {
+         try
+         {
+            Thread.sleep(random.nextInt(BACK_OFF_INTERVALS[attempt]));
+         }
+         catch (InterruptedException e)
+         {
+            Thread.currentThread().interrupt();
+            return false;
+         }
+      }
+      return true;
+   }
 }




More information about the jboss-cvs-commits mailing list