From 0410bb30ad0456a4ff4702487d9119c5da1e989a Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 29 Aug 2013 21:48:20 +0000
Subject: [PATCH] Partial fix for OPENDJ-1112: LoadBalancing connection factories need better diagnostic messages

---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java |   64 ++++++++++++++++++++++++++++---
 1 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java
index 2a4aaed..97c5093 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java
@@ -167,10 +167,12 @@
 
             if (isOperational.getAndSet(false)) {
                 // Transition from online to offline.
-                if (DEBUG_LOG.isLoggable(Level.WARNING)) {
-                    DEBUG_LOG.warning(String.format(
-                            "Connection factory '%s' is no longer operational: %s", factory, error
-                                    .getMessage()));
+                synchronized (listenerLock) {
+                    try {
+                        listener.handleConnectionFactoryOffline(factory, error);
+                    } catch (Throwable t) {
+                        handleListenerException(t);
+                    }
                 }
 
                 synchronized (stateLock) {
@@ -192,9 +194,12 @@
         private void notifyOnline() {
             if (!isOperational.getAndSet(true)) {
                 // Transition from offline to online.
-                if (DEBUG_LOG.isLoggable(Level.INFO)) {
-                    DEBUG_LOG.info(String.format("Connection factory'%s' is now operational",
-                            factory));
+                synchronized (listenerLock) {
+                    try {
+                        listener.handleConnectionFactoryOnline(factory);
+                    } catch (Throwable t) {
+                        handleListenerException(t);
+                    }
                 }
 
                 synchronized (stateLock) {
@@ -210,6 +215,13 @@
                 }
             }
         }
+
+        private void handleListenerException(Throwable t) {
+            if (DEBUG_LOG.isLoggable(Level.SEVERE)) {
+                DEBUG_LOG.log(Level.SEVERE,
+                        "A run-time error occurred while processing a load-balancer event", t);
+            }
+        }
     }
 
     private final class MonitorRunnable implements Runnable {
@@ -225,6 +237,32 @@
         }
     }
 
+    /**
+     * A default event listener which just logs the event.
+     */
+    private static final LoadBalancerEventListener DEFAULT_LISTENER =
+            new LoadBalancerEventListener() {
+
+                @Override
+                public void handleConnectionFactoryOnline(ConnectionFactory factory) {
+                    // Transition from offline to online.
+                    if (DEBUG_LOG.isLoggable(Level.INFO)) {
+                        DEBUG_LOG.info(String.format("Connection factory'%s' is now operational",
+                                factory));
+                    }
+                }
+
+                @Override
+                public void handleConnectionFactoryOffline(ConnectionFactory factory,
+                        ErrorResultException error) {
+                    if (DEBUG_LOG.isLoggable(Level.WARNING)) {
+                        DEBUG_LOG.warning(String.format(
+                                "Connection factory '%s' is no longer operational: %s", factory,
+                                error.getMessage()));
+                    }
+                }
+            };
+
     private final List<MonitoredConnectionFactory> monitoredFactories;
     private final ReferenceCountedObject<ScheduledExecutorService>.Reference scheduler;
     private final Object stateLock = new Object();
@@ -237,6 +275,17 @@
     private volatile ErrorResultException lastFailure = null;
 
     /**
+     * The event listener which should be notified when connection factories go
+     * on or off-line.
+     */
+    private final LoadBalancerEventListener listener;
+
+    /**
+     * Ensures that events are notified one at a time.
+     */
+    private final Object listenerLock = new Object();
+
+    /**
      * Guarded by stateLock.
      */
     private int offlineFactoriesCount = 0;
@@ -269,6 +318,7 @@
         this.scheduler = DEFAULT_SCHEDULER.acquireIfNull(scheduler);
         this.monitoringInterval = interval;
         this.monitoringIntervalTimeUnit = unit;
+        this.listener = DEFAULT_LISTENER;
     }
 
     @Override

--
Gitblit v1.10.0