Apache Solr open-source search software
LUCENE-1798: add infoStream to FieldCache to print any increase in insanity when it happens
Michael McCandless53dab68854dc
diff --git a/CHANGES.txt b/CHANGES.txtindex 83c5b275c6c..e9db97beddf 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -729,6 +729,11 @@ New featuresValueSource, but takes care when composite (multi-segment) arepassed to not double RAM usage in the FieldCache. (ChrisHostetter, Mark Miller, Mike McCandless)++37. LUCENE-1798: Added FieldCache.set/getInfoStream, which uses+ FieldCacheSanityChecker to detect when a new cache entry has+ caused additional insanity, printing the details at the time that+ it happens. (Chris Hostetter, Mike McCandless)Optimizationsdiff --git a/src/java/org/apache/lucene/search/FieldCache.java b/src/java/org/apache/lucene/search/FieldCache.javaindex 81f13d7f7e9..9475439a1ab 100644--- a/src/java/org/apache/lucene/search/FieldCache.java+++ b/src/java/org/apache/lucene/search/FieldCache.java@@ -25,6 +25,7 @@ import org.apache.lucene.analysis.NumericTokenStream; // for javadocsimport java.io.IOException;import java.io.Serializable;+import java.io.PrintStream;import java.text.DecimalFormat;@@ -615,5 +616,13 @@ public interface FieldCache {*/public abstract void purgeAllCaches();+ /**+ * If non-null, FieldCacheImpl will warn whenever+ * entries are created that are not sane according to+ * {@link FieldCacheSanityChecker}.+ */+ public void setInfoStream(PrintStream stream);+ /** @see setInfoStream */+ public PrintStream getInfoStream();}diff --git a/src/java/org/apache/lucene/search/FieldCacheImpl.java b/src/java/org/apache/lucene/search/FieldCacheImpl.javaindex 65404739c44..325286a1f6f 100644--- a/src/java/org/apache/lucene/search/FieldCacheImpl.java+++ b/src/java/org/apache/lucene/search/FieldCacheImpl.java@@ -18,21 +18,22 @@ package org.apache.lucene.search;*/import java.io.IOException;+import java.io.PrintStream;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;-import java.util.Set;import java.util.WeakHashMap;-import org.apache.lucene.document.NumericField;+import org.apache.lucene.document.NumericField; // javadocimport org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermDocs;import org.apache.lucene.index.TermEnum;import org.apache.lucene.util.StringHelper;+import org.apache.lucene.util.FieldCacheSanityChecker;/*** Expert: The default cache implementation, storing all values in memory.@@ -208,12 +209,39 @@ class FieldCacheImpl implements ExtendedFieldCache {synchronized (readerCache) {innerCache.put(key, progress.value);}++ // Only check if key.custom (the parser) is+ // non-null; else, we check twice for a single+ // call to FieldCache.getXXX+ if (key.custom != null && wrapper != null) {+ final PrintStream infoStream = wrapper.getInfoStream();+ if (infoStream != null) {+ printNewInsanity(infoStream, progress.value);+ }+ }}return progress.value;}}return value;}++ private void printNewInsanity(PrintStream infoStream, Object value) {+ final FieldCacheSanityChecker.Insanity[] insanities = FieldCacheSanityChecker.checkSanity(wrapper);+ for(int i=0;i<insanities.length;i++) {+ final FieldCacheSanityChecker.Insanity insanity = insanities[i];+ final CacheEntry[] entries = insanity.getCacheEntries();+ for(int j=0;j<entries.length;j++) {+ if (entries[j].getValue() == value) {+ // OK this insanity involves our entry+ infoStream.println("WARNING: new FieldCache insanity created\nDetails: " + insanity.toString());+ infoStream.println("\nStack:\n");+ new Throwable().printStackTrace(infoStream);+ break;+ }+ }+ }+ }}/** Expert: Every composite-key in the internal cache is of this type. */@@ -811,6 +839,15 @@ class FieldCacheImpl implements ExtendedFieldCache {return retArray;}};-++ private volatile PrintStream infoStream;++ public void setInfoStream(PrintStream stream) {+ infoStream = stream;+ }++ public PrintStream getInfoStream() {+ return infoStream;+ }}diff --git a/src/test/org/apache/lucene/search/TestFieldCache.java b/src/test/org/apache/lucene/search/TestFieldCache.javaindex a1cb5069741..7f08af7ff26 100644--- a/src/test/org/apache/lucene/search/TestFieldCache.java+++ b/src/test/org/apache/lucene/search/TestFieldCache.java@@ -25,6 +25,8 @@ import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.LuceneTestCase;import java.io.IOException;+import java.io.ByteArrayOutputStream;+import java.io.PrintStream;public class TestFieldCache extends LuceneTestCase {protected IndexReader reader;@@ -58,6 +60,18 @@ public class TestFieldCache extends LuceneTestCase {reader = IndexReader.open(directory);}+ public void testInfoStream() throws Exception {+ try {+ FieldCache cache = FieldCache.DEFAULT;+ ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);+ cache.setInfoStream(new PrintStream(bos));+ double [] doubles = cache.getDoubles(reader, "theDouble");+ float [] floats = cache.getFloats(reader, "theDouble");+ assertTrue(bos.toString().indexOf("WARNING") != -1);+ } finally {+ FieldCache.DEFAULT.purgeAllCaches();+ }+ }public void test() throws IOException {FieldCache cache = FieldCache.DEFAULT;