From 9f2bba679ab597f1e50078a29d145100e3baed3c Mon Sep 17 00:00:00 2001
From: Ludovic Poitou <ludovic.poitou@forgerock.com>
Date: Tue, 19 Oct 2010 16:36:12 +0000
Subject: [PATCH] Make ConnectionPool implementation fully async and fix some race conditions in the unit tests.

---
 sdk/src/org/opends/sdk/AbstractLoadBalancingAlgorithm.java |   46 +++++++++++++++++++++++++++++-----------------
 1 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/sdk/src/org/opends/sdk/AbstractLoadBalancingAlgorithm.java b/sdk/src/org/opends/sdk/AbstractLoadBalancingAlgorithm.java
index 42c7f8c..47f6d88 100644
--- a/sdk/src/org/opends/sdk/AbstractLoadBalancingAlgorithm.java
+++ b/sdk/src/org/opends/sdk/AbstractLoadBalancingAlgorithm.java
@@ -173,10 +173,10 @@
       if (!isOperational.get()
           && (pendingConnectFuture == null || pendingConnectFuture.isDone()))
       {
-        if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
+        if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE))
         {
-          StaticUtils.DEBUG_LOG.finest(String
-              .format("Attempting connect on factory " + this));
+          StaticUtils.DEBUG_LOG.fine(String
+              .format("Attempting reconnect to offline factory " + this));
         }
         pendingConnectFuture = factory.getAsynchronousConnection(this);
       }
@@ -189,23 +189,29 @@
       if (isOperational.getAndSet(false))
       {
         // Transition from online to offline.
+        if (StaticUtils.DEBUG_LOG.isLoggable(Level.WARNING))
+        {
+          StaticUtils.DEBUG_LOG.warning(String.format("Connection factory "
+              + factory + " is no longer operational: " + error.getMessage()));
+        }
+
         synchronized (stateLock)
         {
           offlineFactoriesCount++;
           if (offlineFactoriesCount == 1)
           {
             // Enable monitoring.
+            if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE))
+            {
+              StaticUtils.DEBUG_LOG.fine(String
+                  .format("Starting monitoring thread"));
+            }
+
             monitoringFuture = scheduler.scheduleWithFixedDelay(
                 new MonitorRunnable(), 0, monitoringInterval,
                 monitoringIntervalTimeUnit);
           }
         }
-
-        if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE))
-        {
-          StaticUtils.DEBUG_LOG.fine(String.format("Connection factory "
-              + factory + " is no longer operational: " + error.getMessage()));
-        }
       }
     }
 
@@ -216,21 +222,27 @@
       if (!isOperational.getAndSet(true))
       {
         // Transition from offline to online.
+        if (StaticUtils.DEBUG_LOG.isLoggable(Level.INFO))
+        {
+          StaticUtils.DEBUG_LOG.info(String.format("Connection factory "
+              + factory + " is now operational"));
+        }
+
         synchronized (stateLock)
         {
           offlineFactoriesCount--;
           if (offlineFactoriesCount == 0)
           {
+            if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE))
+            {
+              StaticUtils.DEBUG_LOG.fine(String
+                  .format("Stopping monitoring thread"));
+            }
+
             monitoringFuture.cancel(false);
             monitoringFuture = null;
           }
         }
-
-        if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE))
-        {
-          StaticUtils.DEBUG_LOG.fine(String.format("Connection factory "
-              + factory + " is now operational"));
-        }
       }
     }
   }
@@ -278,14 +290,14 @@
 
   /**
    * Creates a new abstract load balancing algorithm which will monitor offline
-   * connection factories every 10 seconds using the default scheduler.
+   * connection factories every second using the default scheduler.
    *
    * @param factories
    *          The connection factories.
    */
   AbstractLoadBalancingAlgorithm(final Collection<ConnectionFactory> factories)
   {
-    this(factories, 10, TimeUnit.SECONDS, StaticUtils.getDefaultScheduler());
+    this(factories, 1, TimeUnit.SECONDS, StaticUtils.getDefaultScheduler());
   }
 
 

--
Gitblit v1.10.0