mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

boli
21.16.2009 f8beabcaa885bfef30f2bd4011d040186cf9ea78
Fix for issue 3011: 

Created custom DirectoryThreadGroup class that all DirectoryThreads will be a member of. All unhandled exceptions from DirectoryThreads will be handled by the handler in DirectoryThreadGroup. This keeps OpenDS from having to install a JVM wide uncaughtException handler.

Changed the configuration object reference a member variable in EntryCacheMonitorProvider instead of a static. This lets GC collected all ConfigEntry and other related objects after OpenDS is shutdown.
7 files modified
216 ■■■■ changed files
opends/src/server/org/opends/server/api/DirectoryThread.java 119 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/core/DirectoryServer.java 57 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/core/ServerShutdownMonitor.java 9 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java 6 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/loggers/debug/DebugAspect.java 14 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/monitors/EntryCacheMonitorProvider.java 6 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tasks/RestartTaskThread.java 5 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/api/DirectoryThread.java
@@ -22,7 +22,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 */
package org.opends.server.api;
@@ -33,7 +33,21 @@
import org.opends.server.backends.task.Task;
import org.opends.server.core.DirectoryServer;
import static org.opends.server.loggers.debug.DebugLogger.
    debugEnabled;
import static org.opends.server.loggers.debug.DebugLogger.getTracer;
import org.opends.server.loggers.debug.DebugTracer;
import static org.opends.server.loggers.ErrorLogger.logError;
import org.opends.server.types.DebugLogLevel;
import org.opends.server.types.DN;
import static org.opends.server.util.StaticUtils.stackTraceToString;
import static org.opends.server.util.ServerConstants.
    ALERT_TYPE_UNCAUGHT_EXCEPTION;
import static org.opends.server.util.ServerConstants.
    ALERT_DESCRIPTION_UNCAUGHT_EXCEPTION;
import org.opends.messages.Message;
import static org.opends.messages.CoreMessages.
    ERR_UNCAUGHT_THREAD_EXCEPTION;
/**
@@ -62,6 +76,18 @@
public class DirectoryThread
       extends Thread
{
  /**
   * The tracer object for the debug logger.
   */
  private static final DebugTracer TRACER = getTracer();
  /**
   * The directory thread group that all directory threads will be a
   * member of.
   */
  public static final DirectoryThreadGroup DIRECTORY_THREAD_GROUP =
      new DirectoryThreadGroup();
  // The stack trace taken at the time that this thread was created.
  private StackTraceElement[] creationStackTrace;
@@ -71,7 +97,73 @@
  // A reference to the thread that was used to create this thread.
  private Thread parentThread;
  /**
   * A thread group for all directory threads. This implements a
   * custom unhandledException handler that logs the error.
   */
  private static class DirectoryThreadGroup extends ThreadGroup
      implements AlertGenerator
  {
    private final LinkedHashMap<String,String> alerts;
    /**
     * Private constructor for DirectoryThreadGroup.
     */
    private DirectoryThreadGroup()
    {
      super("Directory Server Thread Group");
      alerts = new LinkedHashMap<String,String>();
      alerts.put(ALERT_TYPE_UNCAUGHT_EXCEPTION,
          ALERT_DESCRIPTION_UNCAUGHT_EXCEPTION);
    }
    /**
     * {@inheritDoc}
     */
    public DN getComponentEntryDN() {
      return DN.NULL_DN;
    }
    /**
     * {@inheritDoc}
     */
    public String getClassName() {
      return "org.oepnds.server.api.DirectoryThread";
    }
    /**
     * {@inheritDoc}
     */
    public LinkedHashMap<String, String> getAlerts() {
      return alerts;
    }
    /**
     * Provides a means of handling a case in which a thread is about
     * to die because of an unhandled exception.  This method does
     * nothing to try to prevent the death of that thread, but will
     * at least log it so that it can be available for debugging
     * purposes.
     *
     * @param  t  The thread that threw the exception.
     * @param  e  The exception that was thrown but not properly
     *            handled.
     */
    @Override
    public void uncaughtException(Thread t, Throwable e)
    {
      if (debugEnabled())
      {
        TRACER.debugCaught(DebugLogLevel.ERROR, e);
      }
      Message message = ERR_UNCAUGHT_THREAD_EXCEPTION.get(
          t.getName(), stackTraceToString(e));
      logError(message);
      DirectoryServer.sendAlertNotification(this,
          ALERT_TYPE_UNCAUGHT_EXCEPTION, message);
    }
  }
  /**
   * Creates a new instance of this directory thread with the
@@ -83,7 +175,7 @@
   */
  public DirectoryThread(Runnable target, String threadName)
  {
    super (DirectoryServer.getDirectoryThreadGroup(), target,
    super (DIRECTORY_THREAD_GROUP, target,
           threadName);
@@ -99,26 +191,7 @@
   */
  protected DirectoryThread(String threadName)
  {
    super(DirectoryServer.getDirectoryThreadGroup(), threadName);
    init();
  }
  /**
   * Creates a new instance of this directory thread with the
   * specified name as a part of the given thread group.
   *
   * @param  threadGroup  The thread group in which this thread is to
   *                      be placed.
   * @param  threadName   The human-readable name to use for this
   *                      thread for debugging purposes.
   */
  protected DirectoryThread(ThreadGroup threadGroup,
                            String threadName)
  {
    super(threadGroup, threadName);
    super(DIRECTORY_THREAD_GROUP, threadName);
    init();
opends/src/server/org/opends/server/core/DirectoryServer.java
@@ -242,7 +242,7 @@
 * components.
 */
public class DirectoryServer
       implements Thread.UncaughtExceptionHandler, AlertGenerator
       implements AlertGenerator
{
  /**
   * The tracer object for the debug logger.
@@ -717,10 +717,6 @@
  private SynchronizationProviderConfigManager
               synchronizationProviderConfigManager;
  // The thread group for all threads associated with the Directory Server.
  private ThreadGroup directoryThreadGroup;
  // Registry for base DN and naming context information.
  private BaseDnRegistry baseDnRegistry;
@@ -1038,23 +1034,12 @@
    }
    // Create the thread group that should be used for all Directory Server
    // threads.
    directoryThreadGroup = new ThreadGroup("Directory Server Thread Group");
    // Add a shutdown hook so that the server can be notified when the JVM
    // starts shutting down.
    shutdownHook = new DirectoryServerShutdownHook();
    Runtime.getRuntime().addShutdownHook(shutdownHook);
    // Register this class as the default uncaught exception handler for the
    // JVM.  The uncaughtException method will be called if a thread dies
    // because it did not properly handle an exception.
    Thread.setDefaultUncaughtExceptionHandler(this);
    // Create the MBean server that we will use for JMX interaction.
    initializeJMX();
@@ -2912,20 +2897,6 @@
  /**
   * Retrieves the thread group that should be used by all threads associated
   * with the Directory Server.
   *
   * @return  The thread group that should be used by all threads associated
   *          with the Directory Server.
   */
  public static ThreadGroup getDirectoryThreadGroup()
  {
    return directoryServer.directoryThreadGroup;
  }
  /**
   * Retrieves a reference to the Directory Server configuration handler.
   *
   * @return  A reference to the Directory Server configuration handler.
@@ -9090,8 +9061,6 @@
    alerts.put(ALERT_TYPE_SERVER_STARTED, ALERT_DESCRIPTION_SERVER_STARTED);
    alerts.put(ALERT_TYPE_SERVER_SHUTDOWN, ALERT_DESCRIPTION_SERVER_SHUTDOWN);
    alerts.put(ALERT_TYPE_UNCAUGHT_EXCEPTION,
               ALERT_DESCRIPTION_UNCAUGHT_EXCEPTION);
    alerts.put(ALERT_TYPE_ENTERING_LOCKDOWN_MODE,
               ALERT_DESCRIPTION_ENTERING_LOCKDOWN_MODE);
    alerts.put(ALERT_TYPE_LEAVING_LOCKDOWN_MODE,
@@ -9103,30 +9072,6 @@
  /**
   * Provides a means of handling a case in which a thread is about to die
   * because of an unhandled exception.  This method does nothing to try to
   * prevent the death of that thread, but will at least log it so that it can
   * be available for debugging purposes.
   *
   * @param  thread     The thread that threw the exception.
   * @param  exception  The exception that was thrown but not properly handled.
   */
  public void uncaughtException(Thread thread, Throwable exception)
  {
    if (debugEnabled())
    {
      TRACER.debugCaught(DebugLogLevel.ERROR, exception);
    }
    Message message = ERR_UNCAUGHT_THREAD_EXCEPTION.get(
        thread.getName(), stackTraceToString(exception));
    logError(message);
    sendAlertNotification(this, ALERT_TYPE_UNCAUGHT_EXCEPTION, message);
  }
  /**
   * Indicates whether the server is currently in the process of shutting down.
   * @return <CODE>true</CODE> if this server is currently in the process of
   * shutting down and <CODE>false</CODE> otherwise.
opends/src/server/org/opends/server/core/ServerShutdownMonitor.java
@@ -22,12 +22,13 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 */
package org.opends.server.core;
import org.opends.server.api.DirectoryThread;
import java.util.Iterator;
import java.util.LinkedList;
@@ -38,7 +39,7 @@
 * shutdown process and may help nudge it along if it appears to get hung.
 */
public class ServerShutdownMonitor
       extends Thread
       extends DirectoryThread
{
  // Indicates whether the monitor has completed and the shutdown may be
  // finalized with a call to System.exit;
@@ -56,7 +57,7 @@
   */
  public ServerShutdownMonitor()
  {
    setName("Directory Server Shutdown Monitor");
    super("Directory Server Shutdown Monitor");
    setDaemon(true);
@@ -71,7 +72,7 @@
    // we'll make sure to allocate enough room for double the threads that we
    // think are currently running.
    threadList = new LinkedList<Thread>();
    ThreadGroup threadGroup = DirectoryServer.getDirectoryThreadGroup();
    ThreadGroup threadGroup = DirectoryThread.DIRECTORY_THREAD_GROUP;
    Thread[] threadArray = new Thread[threadGroup.activeCount() * 2];
    int numThreads = threadGroup.enumerate(threadArray, true);
    for (int i=0; i < numThreads; i++)
opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java
@@ -22,7 +22,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 */
package org.opends.server.extensions;
import org.opends.messages.Message;
@@ -43,6 +43,7 @@
import org.opends.server.admin.std.server.SoftReferenceEntryCacheCfg;
import org.opends.server.api.Backend;
import org.opends.server.api.EntryCache;
import org.opends.server.api.DirectoryThread;
import org.opends.server.config.ConfigException;
import org.opends.server.core.DirectoryServer;
import org.opends.server.loggers.debug.DebugTracer;
@@ -128,7 +129,8 @@
      )
      throws ConfigException, InitializationException
  {
    cleanerThread = new Thread(this, "Soft Reference Entry Cache Cleaner");
    cleanerThread = new DirectoryThread(this,
        "Soft Reference Entry Cache Cleaner");
    cleanerThread.setDaemon(true);
    cleanerThread.start();
opends/src/server/org/opends/server/loggers/debug/DebugAspect.java
@@ -22,7 +22,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 */
package org.opends.server.loggers.debug;
@@ -83,15 +83,6 @@
  }
  /**
   * Pointcut for matching the getDirectoryThreadGroup method.
   */
  @Pointcut("execution(public static ThreadGroup org.opends.server." +
      "core.DirectoryServer.getDirectoryThreadGroup(..))")
  private void getThreadGroupMethod()
  {
  }
  /**
   * Pointcut for matching all getDebugProperties() methods.
   * TODO: Make this less general. Find out if pointcut matches
   * subclass methods.
@@ -120,8 +111,7 @@
   * debug logger.
   */
  @Pointcut("toStringMethod() || getMessageMethod() || " +
      "getDebugPropertiesMethod() || debugRelatedClasses() || " +
      "getThreadGroupMethod()")
      "getDebugPropertiesMethod() || debugRelatedClasses()")
  private void excluded()
  {
  }
opends/src/server/org/opends/server/monitors/EntryCacheMonitorProvider.java
@@ -22,7 +22,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2008 Sun Microsystems, Inc.
 *      Copyright 2008-2009 Sun Microsystems, Inc.
 */
package org.opends.server.monitors;
@@ -58,8 +58,8 @@
  // The entry cache with which this monitor is associated.
  private EntryCache<? extends EntryCacheCfg> entryCache;
  // Global entry cache monitor configuration.
  private static EntryCacheMonitorProviderCfg monitorConfiguration;
  // Entry cache monitor configuration.
  private EntryCacheMonitorProviderCfg monitorConfiguration;
  /**
   * Creates default instance of this monitor provider.
opends/src/server/org/opends/server/tasks/RestartTaskThread.java
@@ -22,7 +22,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 */
package org.opends.server.tasks;
import org.opends.messages.Message;
@@ -30,6 +30,7 @@
import org.opends.server.core.DirectoryServer;
import org.opends.server.api.DirectoryThread;
@@ -49,7 +50,7 @@
 * exit before we get a chance to restart it if all non-daemon threads go away.
 */
public class RestartTaskThread
       extends Thread
       extends DirectoryThread
{
  /**
   * The fully-qualified name of this class.