Author: rwesten
Date: Thu Jan 16 08:06:20 2014
New Revision: 1558711

URL: http://svn.apache.org/r1558711
Log:
fix for STANBOL-1258 for the 0.12 branch. This also greatly imrpoves DEBUG 
level logging for the EntityCache. The FST Linking engine will now show INFO 
level logging with the size of the EntityCache during activation

Modified:
    
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java
    
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java
    
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java

Modified: 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java?rev=1558711&r1=1558710&r2=1558711&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java
 Thu Jan 16 08:06:20 2014
@@ -476,12 +476,13 @@ public class FstLinkingEngineComponent {
         } else {
             ecSize = -1;
         }
-        if(ecSize < 0){
-            documentCacheFactory = new 
FastLRUCacheManager(DEFAULT_ENTITY_CACHE_SIZE);
-        } else if(ecSize == 0){
+        if(ecSize == 0){
+            log.info(" ... EntityCache deactivated");
             documentCacheFactory = null;
         } else {
-            documentCacheFactory = new FastLRUCacheManager(ecSize);
+            int size = ecSize < 0 ? DEFAULT_ENTITY_CACHE_SIZE : ecSize;
+               log.info(" ... create EntityCache (size: {})",size);
+            documentCacheFactory = new FastLRUCacheManager(size);
         }
         
         //(7) parse the Entity type field
@@ -776,6 +777,10 @@ public class FstLinkingEngineComponent {
         engineMetadata = null;
         textProcessingConfig = null;
         entityLinkerConfig = null;
+        if(documentCacheFactory != null){
+               documentCacheFactory.close(); //ensure that old caches are 
cleared
+        }
+        documentCacheFactory = null;
         bundleContext = null;
         skipAltTokensConfig = null;
     }

Modified: 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java?rev=1558711&r1=1558710&r2=1558711&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java
 Thu Jan 16 08:06:20 2014
@@ -49,4 +49,9 @@ public interface EntityCacheManager {
      */
     RefCounted<EntityCache> getCache(Object version);
 
+    /**
+     * Called if the EntityCacheManager is no longer used
+     */
+    void close();
+    
 }

Modified: 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java?rev=1558711&r1=1558710&r2=1558711&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java
 Thu Jan 16 08:06:20 2014
@@ -20,13 +20,13 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Service;
 import org.apache.lucene.document.Document;
 import org.apache.solr.search.CacheRegenerator;
 import org.apache.solr.search.FastLRUCache;
 import org.apache.solr.search.SolrCache;
 import org.apache.solr.util.RefCounted;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of the {@link EntityCacheManager} based on the Solr
@@ -37,6 +37,8 @@ import org.apache.solr.util.RefCounted;
  */
 public class FastLRUCacheManager implements EntityCacheManager {
 
+       private final Logger log = LoggerFactory.getLogger(getClass());
+       
     RefCounted<EntityCache> current;
     private final CacheRegenerator regenerator;
     private final Map<String,String> config;
@@ -75,21 +77,49 @@ public class FastLRUCacheManager impleme
     public RefCounted<EntityCache> getCache(Object version) {
         if(current == null || !current.get().getVersion().equals(version)){
             if(current != null){
-                //the the old one as outdated!
-                ((RefCountedImpl)current).setOutdated();
+               log.debug(" > invalidate EntityCache for version {}", 
current.get().getVersion());
+               //remove the reference to the old instance. This will allow to
+               //destroy the old cache as soon as it is no longer used
+               current.decref(); 
+               log.debug("  ... {} remaining users for invalidated Cache", 
current.getRefcount());
+               current = null;
             }
             //create a new cache
+            log.debug(" > create EntityCache for version {}", version);
             SolrCache<Integer,Document> cache = new 
FastLRUCache<Integer,Document>();
             cache.init(config, null, regenerator);
             current = new RefCountedImpl(new SolrEntityCache(version, cache));
-        }
-        current.incref();
+            //add a reference to the new cache by this class. This will be 
removed
+            //as soon as the instance is outdated
+            current.incref(); 
+        }
+        current.incref(); //this increase is for the holder of the returned 
instance
+        log.debug(" > increase RefCount for EntityCache for version {} to {}", 
+                       version, current.getRefcount());
         return current;
     }
 
+    @Override
+    public void close() {
+       if(current != null){
+               current.decref();
+               current = null;
+       }
+    }
+    
+    @Override
+    protected void finalize() {
+       if(current != null){
+               log.warn("[finalize] EntityCache Manager was not closed. This 
can "
+                               + "cause Memory Leaks as Cached Entities will 
be kept in " 
+                               + "Memory until finalization!");
+       }
+       close();
+    }
+    
     /**
-     * {@link RefCounted} implementation that ensures that outdated caches are
-     * cleared and closed as soon as they are no longer in use.
+     * {@link RefCounted} implementation that closes the {@link 
SolrEntityCache}
+     * when {@link #close()} is called by the supoer implementation.
      * 
      * @author Rupert Westenthaler
      *
@@ -100,23 +130,23 @@ public class FastLRUCacheManager impleme
             super(resource);
         }
 
-        private boolean outdated;
-
-        /**
-         * Used by the manager implementation to set the RefCounted EntityCache
-         * as outdated
-         */
-        protected void setOutdated() {
-            outdated = true;
+        @Override
+        public void decref() {
+               super.decref();
+               if(log.isDebugEnabled()){
+                   log.debug(" > decrease RefCount for EntityCache for version 
{} to {}", 
+                               get().getVersion(), current.getRefcount());
+               }
         }
-
         /**
-         * clears the cache if outdated
+         * closes the {@link SolrEntityCache}
          */
         protected void close(){
-            if(outdated){
-                ((SolrEntityCache)get()).close();
-            }
+               if(log.isDebugEnabled()){
+                       log.debug(" > close EntityCache for version {}", 
+                                       current.get().getVersion());
+               }
+            ((SolrEntityCache)get()).close();
         }
 
     }


Reply via email to