From a1393c28fbaa1385f5a14aa7810034a729bae074 Mon Sep 17 00:00:00 2001
From: Fabio Pistolesi <fabio.pistolesi@forgerock.com>
Date: Thu, 16 Apr 2015 17:00:49 +0000
Subject: [PATCH] OPENDJ-1941 CR-6639  dsconfig: impossible to edit a persistit backend configuration

---
 opendj-server-legacy/src/main/java/org/opends/server/core/BackendConfigManager.java           |   53 +++++++++++++-------------
 opendj-server-legacy/src/main/java/org/opends/server/backends/persistit/PersistItStorage.java |   13 +++++-
 opendj-server-legacy/src/main/java/org/opends/server/core/MemoryQuota.java                    |    8 +--
 3 files changed, 40 insertions(+), 34 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/persistit/PersistItStorage.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/persistit/PersistItStorage.java
index f1a333b..97d581d 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/persistit/PersistItStorage.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/persistit/PersistItStorage.java
@@ -784,9 +784,18 @@
 
   /** {@inheritDoc} */
   @Override
-  public boolean isConfigurationChangeAcceptable(PersistitBackendCfg cfg, List<LocalizableMessage> unacceptableReasons)
+  public boolean isConfigurationChangeAcceptable(PersistitBackendCfg newCfg,
+      List<LocalizableMessage> unacceptableReasons)
   {
-    return checkConfigurationDirectories(cfg, unacceptableReasons);
+    long newSize = computeSize(newCfg);
+    long oldSize = computeSize(config);
+    return (newSize <= oldSize || memQuota.isMemoryAvailable(newSize - oldSize))
+        && checkConfigurationDirectories(newCfg, unacceptableReasons);
+  }
+
+  private long computeSize(PersistitBackendCfg cfg)
+  {
+    return cfg.getDBCacheSize() > 0 ? cfg.getDBCacheSize() : memQuota.memPercentToBytes(cfg.getDBCachePercent());
   }
 
   /**
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/BackendConfigManager.java b/opendj-server-legacy/src/main/java/org/opends/server/core/BackendConfigManager.java
index 7f66dc7..7bd9d36 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/BackendConfigManager.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/BackendConfigManager.java
@@ -356,37 +356,36 @@
         }
       }
     }
-
-
-    // See if the entry contains an attribute that specifies the class name
-    // for the backend implementation.  If it does, then load it and make sure
-    // that it's a valid backend implementation.  There is no such attribute,
-    // the specified class cannot be loaded, or it does not contain a valid
-    // backend implementation, then log an error and skip it.
-    String className = configEntry.getJavaClass();
-    try
+    else if (configEntry.isEnabled())
     {
-      Class<Backend<?>> backendClass = loadBackendClass(className);
-      if (! Backend.class.isAssignableFrom(backendClass))
+      /*
+       * If the backend was not enabled, it has not been registered with directory server, so
+       * no listeners will be registered at the lower layers. Verify as it was an add.
+       */
+      String className = configEntry.getJavaClass();
+      try
       {
-        unacceptableReason.add(ERR_CONFIG_BACKEND_CLASS_NOT_BACKEND.get(className, backendDN));
-        return false;
-      }
+        Class<Backend<BackendCfg>> backendClass = loadBackendClass(className);
+        if (! Backend.class.isAssignableFrom(backendClass))
+        {
+          unacceptableReason.add(ERR_CONFIG_BACKEND_CLASS_NOT_BACKEND.get(className, backendDN));
+          return false;
+        }
 
-      Backend b = backendClass.newInstance();
-      if (! b.isConfigurationAcceptable(configEntry, unacceptableReason, serverContext))
+        Backend<BackendCfg> b = backendClass.newInstance();
+        if (! b.isConfigurationAcceptable(configEntry, unacceptableReason, serverContext))
+        {
+          return false;
+        }
+      }
+      catch (Exception e)
       {
+        logger.traceException(e);
+        unacceptableReason.add(
+            ERR_CONFIG_BACKEND_CANNOT_INSTANTIATE.get(className, backendDN, stackTraceToSingleLineString(e)));
         return false;
       }
     }
-    catch (Exception e)
-    {
-      logger.traceException(e);
-      unacceptableReason.add(
-          ERR_CONFIG_BACKEND_CANNOT_INSTANTIATE.get(className, backendDN, stackTraceToSingleLineString(e)));
-      return false;
-    }
-
 
     // If we've gotten to this point, then it is acceptable as far as we are
     // concerned.  If it is unacceptable according to the configuration for that
@@ -777,7 +776,7 @@
     // backend implementation, then log an error and skip it.
     String className = cfg.getJavaClass();
 
-    Backend<?> backend;
+    Backend<BackendCfg> backend;
     try
     {
       backend = loadBackendClass(className).newInstance();
@@ -900,9 +899,9 @@
   }
 
   @SuppressWarnings("unchecked")
-  private Class<Backend<?>> loadBackendClass(String className) throws Exception
+  private Class<Backend<BackendCfg>> loadBackendClass(String className) throws Exception
   {
-    return (Class<Backend<?>>) DirectoryServer.loadClass(className);
+    return (Class<Backend<BackendCfg>>) DirectoryServer.loadClass(className);
   }
 
   private WritabilityMode toWritabilityMode(BackendCfgDefn.WritabilityMode writabilityMode)
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/MemoryQuota.java b/opendj-server-legacy/src/main/java/org/opends/server/core/MemoryQuota.java
index 5076700..a1546a0 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/MemoryQuota.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/MemoryQuota.java
@@ -41,8 +41,6 @@
  */
 public final class MemoryQuota
 {
-  private static final double INIT_FUDGE_FACTOR = 0.9;
-  private static final double FUDGE_FACTOR = 1.3;
   private static final long ONE_MEGABYTE = 1024 * 1024;
 
   private Semaphore reservedMemory;
@@ -55,7 +53,7 @@
   public MemoryQuota()
   {
     allowOvercommit = System.getProperty(ENABLE_MEMORY_OVERCOMMIT) != null;
-    reservableMemory = (int)(INIT_FUDGE_FACTOR * (getOldGenInfo().getMax() / ONE_MEGABYTE));
+    reservableMemory = (int)(Math.pow(Math.E / Math.PI, 2) * (getOldGenInfo().getMax() / ONE_MEGABYTE));
     reservedMemory = new Semaphore(reservableMemory, true);
   }
 
@@ -117,7 +115,7 @@
     {
       return true;
     }
-    return reservedMemory.tryAcquire((int)(FUDGE_FACTOR * size / ONE_MEGABYTE));
+    return reservedMemory.tryAcquire((int)(size / ONE_MEGABYTE));
   }
 
   /**
@@ -163,6 +161,6 @@
     {
       return;
     }
-    reservedMemory.release((int)(FUDGE_FACTOR * size / ONE_MEGABYTE));
+    reservedMemory.release((int)(size / ONE_MEGABYTE));
   }
 }

--
Gitblit v1.10.0