Use the same MultiReader trick for QueryUtils, too. Now FC insanity caused by randomly wrapping readers with MultiReaders and modified segment combinations below no longer cause insanity. We still can check for insanity in production, but tests that wrap randomly for testing purposed (and behave insane) don't fail anymore.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1294266 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-02-27 18:45:39 +00:00
parent d41c7651d4
commit dc0c24053e
3 changed files with 32 additions and 20 deletions

View File

@ -116,7 +116,6 @@ public class CheckHits {
Assert.assertEquals("Wrap Reader " + i + ": " +
query.toString(defaultFieldName),
correct, actual);
QueryUtils.purgeFieldCache(s.getIndexReader()); // our wrapping can create insanity otherwise
}
}

View File

@ -114,13 +114,9 @@ public class QueryUtils {
checkFirstSkipTo(q1,s);
checkSkipTo(q1,s);
if (wrap) {
IndexSearcher wrapped;
check(random, q1, wrapped = wrapUnderlyingReader(random, s, -1), false);
purgeFieldCache(wrapped.getIndexReader()); // our wrapping can create insanity otherwise
check(random, q1, wrapped = wrapUnderlyingReader(random, s, 0), false);
purgeFieldCache(wrapped.getIndexReader()); // our wrapping can create insanity otherwise
check(random, q1, wrapped = wrapUnderlyingReader(random, s, +1), false);
purgeFieldCache(wrapped.getIndexReader()); // our wrapping can create insanity otherwise
check(random, q1, wrapUnderlyingReader(random, s, -1), false);
check(random, q1, wrapUnderlyingReader(random, s, 0), false);
check(random, q1, wrapUnderlyingReader(random, s, +1), false);
}
checkExplanations(q1,s);
@ -138,6 +134,27 @@ public class QueryUtils {
FieldCache.DEFAULT.purge(SlowCompositeReaderWrapper.wrap(r));
}
/** This is a MultiReader that can be used for randomly wrapping other readers
* without creating FieldCache insanity.
* The trick is to use an opaque/fake cache key. */
public static class FCInvisibleMultiReader extends MultiReader {
private final Object cacheKey = new Object();
public FCInvisibleMultiReader(IndexReader... readers) throws IOException {
super(readers);
}
@Override
public Object getCoreCacheKey() {
return cacheKey;
}
@Override
public Object getCombinedCoreAndDeletesKey() {
return cacheKey;
}
}
/**
* Given an IndexSearcher, returns a new IndexSearcher whose IndexReader
* is a MultiReader containing the Reader of the original IndexSearcher,
@ -157,17 +174,17 @@ public class QueryUtils {
IndexReader[] readers = new IndexReader[] {
edge < 0 ? r : emptyReaders[0],
emptyReaders[0],
new MultiReader(edge < 0 ? emptyReaders[4] : emptyReaders[0],
new FCInvisibleMultiReader(edge < 0 ? emptyReaders[4] : emptyReaders[0],
emptyReaders[0],
0 == edge ? r : emptyReaders[0]),
0 < edge ? emptyReaders[0] : emptyReaders[7],
emptyReaders[0],
new MultiReader(0 < edge ? emptyReaders[0] : emptyReaders[5],
new FCInvisibleMultiReader(0 < edge ? emptyReaders[0] : emptyReaders[5],
emptyReaders[0],
0 < edge ? r : emptyReaders[0])
};
IndexSearcher out = LuceneTestCase.newSearcher(new MultiReader(readers));
IndexSearcher out = LuceneTestCase.newSearcher(new FCInvisibleMultiReader(readers));
out.setSimilarity(s.getSimilarity());
return out;
}

View File

@ -57,7 +57,6 @@ import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.CompositeReader;
import org.apache.lucene.index.FieldFilterAtomicReader;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReader.ReaderClosedListener;
@ -83,6 +82,7 @@ import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.RandomSimilarityProvider;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.search.QueryUtils.FCInvisibleMultiReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.FlushInfo;
@ -1364,14 +1364,10 @@ public abstract class LuceneTestCase extends Assert {
new ParallelCompositeReader((CompositeReader) r);
break;
case 2:
// Häckidy-Hick-Hack: this will create FC insanity, so we patch MultiReader to
// return a fake cache key, so insanity checker cannot walk along our reader:
r = new MultiReader(r) {
private final Object cacheKey = new Object();
@Override public Object getCoreCacheKey() { return cacheKey; }
@Override public Object getCombinedCoreAndDeletesKey() { return cacheKey; }
@Override public String toString() { return "MultiReader(" + subReaders[0] + ")"; }
};
// Häckidy-Hick-Hack: a standard MultiReader will cause FC insanity, so we use
// QueryUtils' reader with a fake cache key, so insanity checker cannot walk
// along our reader:
r = new FCInvisibleMultiReader(r);
break;
case 3:
final AtomicReader ar = SlowCompositeReaderWrapper.wrap(r);