From 55b5528cb49321dc0973de6c42df96e4c39f7523 Mon Sep 17 00:00:00 2001
From: Ludovic Poitou <ludovic.poitou@forgerock.com>
Date: Mon, 16 Apr 2012 10:45:10 +0000
Subject: [PATCH] Fix OPENDJ-471. FIFOEntryCache may leave stalled data on low memory conditions. Now, when low memory conditions are detected, the FIFOEntryCache removes the previous version of the updated entry if it exists, instead of just removing the first-in entry.

---
 opendj-sdk/opends/src/server/org/opends/server/extensions/FIFOEntryCache.java |   38 +++++++++++++++++++++++++++++---------
 1 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/extensions/FIFOEntryCache.java b/opendj-sdk/opends/src/server/org/opends/server/extensions/FIFOEntryCache.java
index 4efd241..9bf4b42 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/extensions/FIFOEntryCache.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/extensions/FIFOEntryCache.java
@@ -23,7 +23,7 @@
  *
  *
  *      Copyright 2006-2008 Sun Microsystems, Inc.
- *      Portions Copyright 2011 ForgeRock AS
+ *      Portions Copyright 2011-2012 ForgeRock AS
  */
 package org.opends.server.extensions;
 
@@ -347,18 +347,38 @@
       long usedMemory = runtime.totalMemory() - runtime.freeMemory();
       if (usedMemory > maxAllowedMemory)
       {
-        Iterator<CacheEntry> iterator = dnMap.values().iterator();
-        if (iterator.hasNext())
+        CacheEntry cachedEntry = dnMap.remove(entry.getDN());
+        if (cachedEntry == null)
         {
-          CacheEntry ce = iterator.next();
-          iterator.remove();
-
-          HashMap<Long,CacheEntry> m = idMap.get(ce.getBackend());
-          if (m != null)
+          // The current entry wasn't there, let's remove an existing entry.
+          Iterator<CacheEntry> iterator = dnMap.values().iterator();
+          if (iterator.hasNext())
           {
-            m.remove(ce.getEntryID());
+            CacheEntry ce = iterator.next();
+            iterator.remove();
+
+            HashMap<Long,CacheEntry> m = idMap.get(ce.getBackend());
+            if (m != null)
+            {
+              m.remove(ce.getEntryID());
+            }
           }
         }
+        else
+        {
+          // Try to remove the entry from the ID list as well.
+          Map<Long,CacheEntry> map = idMap.get(backend);
+          if (map != null)
+          {
+            map.remove(cacheEntry.getEntryID());
+            // If this backend becomes empty now remove it from the idMap map.
+            if (map.isEmpty())
+            {
+              idMap.remove(backend);
+            }
+          }
+        }
+
       }
       else
       {

--
Gitblit v1.10.0