You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2014/05/21 09:21:35 UTC

svn commit: r1596497 - in /lucene/dev/branches/lucene5650/solr/core/src: java/org/apache/solr/spelling/suggest/ test/org/apache/solr/spelling/suggest/

Author: dweiss
Date: Wed May 21 07:21:35 2014
New Revision: 1596497

URL: http://svn.apache.org/r1596497
Log:
SOLR-6100, LUCENE-5650: fix an uncloseable file leak in solr suggesters.

Modified:
    lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/SolrSuggester.java
    lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java
    lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestAnalyzeInfixSuggestions.java
    lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java

Modified: lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/SolrSuggester.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/SolrSuggester.java?rev=1596497&r1=1596496&r2=1596497&view=diff
==============================================================================
--- lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/SolrSuggester.java (original)
+++ lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/SolrSuggester.java Wed May 21 07:21:35 2014
@@ -17,6 +17,7 @@ package org.apache.solr.spelling.suggest
  * limitations under the License.
  */
 
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -28,6 +29,7 @@ import org.apache.lucene.search.suggest.
 import org.apache.lucene.search.suggest.Lookup;
 import org.apache.lucene.util.IOUtils;
 import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.CloseHook;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.search.SolrIndexSearcher;
 import org.slf4j.Logger;
@@ -101,7 +103,22 @@ public class SolrSuggester {
     // initialize appropriate lookup instance
     factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class);
     lookup = factory.create(config, core);
-    
+    core.addCloseHook(new CloseHook() {
+      @Override
+      public void preClose(SolrCore core) {
+        if (lookup != null && lookup instanceof Closeable) {
+          try {
+            ((Closeable) lookup).close();
+          } catch (IOException e) {
+            LOG.warn("Could not close the suggester lookup.", e);
+          }
+        }
+      }
+      
+      @Override
+      public void postClose(SolrCore core) {}
+    });
+
     // if store directory is provided make it or load up the lookup with its content
     if (store != null) {
       storeDir = new File(store);

Modified: lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java?rev=1596497&r1=1596496&r2=1596497&view=diff
==============================================================================
--- lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java (original)
+++ lucene/dev/branches/lucene5650/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java Wed May 21 07:21:35 2014
@@ -17,6 +17,7 @@
 
 package org.apache.solr.spelling.suggest;
 
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -33,13 +34,14 @@ import org.apache.lucene.search.spell.Di
 import org.apache.lucene.search.spell.HighFrequencyDictionary;
 import org.apache.lucene.search.spell.SuggestMode;
 import org.apache.lucene.search.suggest.FileDictionary;
-import org.apache.lucene.search.suggest.Lookup.LookupResult;
 import org.apache.lucene.search.suggest.Lookup;
+import org.apache.lucene.search.suggest.Lookup.LookupResult;
 import org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester;
 import org.apache.lucene.search.suggest.fst.WFSTCompletionLookup;
 import org.apache.lucene.util.CharsRef;
 import org.apache.lucene.util.IOUtils;
 import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.CloseHook;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.search.SolrIndexSearcher;
 import org.apache.solr.spelling.SolrSpellChecker;
@@ -103,6 +105,22 @@ public class Suggester extends SolrSpell
     factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class);
     
     lookup = factory.create(config, core);
+    core.addCloseHook(new CloseHook() {
+      @Override
+      public void preClose(SolrCore core) {
+        if (lookup != null && lookup instanceof Closeable) {
+          try {
+            ((Closeable) lookup).close();
+          } catch (IOException e) {
+            LOG.warn("Could not close the suggester lookup.", e);
+          }
+        }
+      }
+      
+      @Override
+      public void postClose(SolrCore core) {}
+    });
+    
     String store = (String)config.get(STORE_DIR);
     if (store != null) {
       storeDir = new File(store);
@@ -120,6 +138,7 @@ public class Suggester extends SolrSpell
         }
       }
     }
+    
     return name;
   }
   

Modified: lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestAnalyzeInfixSuggestions.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestAnalyzeInfixSuggestions.java?rev=1596497&r1=1596496&r2=1596497&view=diff
==============================================================================
--- lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestAnalyzeInfixSuggestions.java (original)
+++ lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestAnalyzeInfixSuggestions.java Wed May 21 07:21:35 2014
@@ -1,10 +1,7 @@
 package org.apache.solr.spelling.suggest;
 
-import java.io.File;
-
 import org.apache.solr.SolrTestCaseJ4;
 import org.apache.solr.common.params.SpellingParams;
-import org.junit.AfterClass;
 import org.junit.BeforeClass;
 
 /*
@@ -33,16 +30,6 @@ public class TestAnalyzeInfixSuggestions
     assertQ(req("qt", URI_DEFAULT, "q", "", SpellingParams.SPELLCHECK_BUILD, "true"));
   }
   
-  @AfterClass
-  public static void afterClass() throws Exception {
-    File indexPathDir = new File("analyzingInfixSuggesterIndexDir");
-    File indexPathDirTmp = new File("analyzingInfixSuggesterIndexDir.tmp");
-    if (indexPathDir.exists())
-      recurseDelete(indexPathDir);
-    if (indexPathDirTmp.exists())
-      recurseDelete(indexPathDirTmp);
-  }
-  
   public void testSingle() throws Exception {
     
     assertQ(req("qt", URI_DEFAULT, "q", "japan", SpellingParams.SPELLCHECK_COUNT, "1"),

Modified: lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java?rev=1596497&r1=1596496&r2=1596497&view=diff
==============================================================================
--- lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java (original)
+++ lucene/dev/branches/lucene5650/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java Wed May 21 07:21:35 2014
@@ -17,10 +17,7 @@ package org.apache.solr.spelling.suggest
  * limitations under the License.
  */
 
-import java.io.File;
-
 import org.apache.solr.SolrTestCaseJ4;
-import org.junit.AfterClass;
 import org.junit.BeforeClass;
 
 public class TestBlendedInfixSuggestions extends SolrTestCaseJ4 {
@@ -31,17 +28,7 @@ public class TestBlendedInfixSuggestions
     initCore("solrconfig-phrasesuggest.xml","schema-phrasesuggest.xml");
     assertQ(req("qt", URI, "q", "", SuggesterParams.SUGGEST_BUILD_ALL, "true"));
   }
-  
-  @AfterClass
-  public static void afterClass() throws Exception {
-    File indexPathDir = new File("blendedInfixSuggesterIndexDir");
-    File indexPathDirTmp = new File("blendedInfixSuggesterIndexDir.tmp");
-    if (indexPathDir.exists())
-      recurseDelete(indexPathDir);
-    if (indexPathDirTmp.exists())
-      recurseDelete(indexPathDirTmp);
-  }
-  
+
   public void testLinearBlenderType() {
     assertQ(req("qt", URI, "q", "the", SuggesterParams.SUGGEST_COUNT, "10", SuggesterParams.SUGGEST_DICT, "blended_infix_suggest_linear"),
         "//lst[@name='suggest']/lst[@name='blended_infix_suggest_linear']/lst[@name='the']/int[@name='numFound'][.='3']",
@@ -55,7 +42,6 @@ public class TestBlendedInfixSuggestions
         "//lst[@name='suggest']/lst[@name='blended_infix_suggest_linear']/lst[@name='the']/arr[@name='suggestions']/lst[3]/long[@name='weight'][.='7']",
         "//lst[@name='suggest']/lst[@name='blended_infix_suggest_linear']/lst[@name='the']/arr[@name='suggestions']/lst[3]/str[@name='payload'][.='star']"
     );
-    
   }
   
   public void testReciprocalBlenderType() {
@@ -97,5 +83,4 @@ public class TestBlendedInfixSuggestions
         "//lst[@name='suggest']/lst[@name='blended_infix_suggest_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[3]/str[@name='payload'][.='star']"
     );
   }
-  
 }