From 24d79de796dcba20a43f7325f220513f7773f1fb Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Fri, 25 May 2018 08:47:00 +0200 Subject: [PATCH] LUCENE-8144: Moved QueryCachingPolicy.ALWAYS_CACHE to the test framework. --- lucene/CHANGES.txt | 3 + lucene/MIGRATE.txt | 5 ++ .../lucene/search/QueryCachingPolicy.java | 13 ----- .../lucene/search/TestLRUQueryCache.java | 58 +++++++++++-------- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 5d5b7f25f1d..1b2ba844f0f 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -44,6 +44,9 @@ API Changes * LUCENE-8267: Memory codecs removed from the codebase (MemoryPostings, MemoryDocValues). (Dawid Weiss) +* LUCENE-8144: Moved QueryCachingPolicy.ALWAYS_CACHE to the test framework. + (Nhat Nguyen via Adrien Grand) + Changes in Runtime Behavior * LUCENE-7837: Indices that were created before the previous major version diff --git a/lucene/MIGRATE.txt b/lucene/MIGRATE.txt index 0f9fcf68cab..fb7ee086cbf 100644 --- a/lucene/MIGRATE.txt +++ b/lucene/MIGRATE.txt @@ -47,3 +47,8 @@ a boost of 1f. ## Memory codecs removed (LUCENE-8267) ## Memory codecs have been removed from the codebase (MemoryPostings, MemoryDocValues). + +## QueryCachingPolicy.ALWAYS_CACHE removed (LUCENE-8144) ## + +Caching everything is discouraged as it disables the ability to skip non-interesting documents. +ALWAYS_CACHE can be replaced by a UsageTrackingQueryCachingPolicy with an appropriate config. diff --git a/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java b/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java index fabd971b6e7..e6364fa2c31 100644 --- a/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java +++ b/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java @@ -31,19 +31,6 @@ import java.io.IOException; // TODO: add APIs for integration with IndexWriter.IndexReaderWarmer public interface QueryCachingPolicy { - /** A simple policy that caches all the provided filters on all segments. */ - public static final QueryCachingPolicy ALWAYS_CACHE = new QueryCachingPolicy() { - - @Override - public void onUse(Query query) {} - - @Override - public boolean shouldCache(Query query) throws IOException { - return true; - } - - }; - /** Callback that is called every time that a cached filter is used. * This is typically useful if the policy wants to track usage statistics * in order to make decisions. */ diff --git a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java index d0eaa4363be..b3e1ed3eacd 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java @@ -65,6 +65,18 @@ import org.apache.lucene.util.TestUtil; public class TestLRUQueryCache extends LuceneTestCase { + private static final QueryCachingPolicy ALWAYS_CACHE = new QueryCachingPolicy() { + + @Override + public void onUse(Query query) {} + + @Override + public boolean shouldCache(Query query) throws IOException { + return true; + } + + }; + private static final QueryCachingPolicy NEVER_CACHE = new QueryCachingPolicy() { @Override @@ -195,7 +207,7 @@ public class TestLRUQueryCache extends LuceneTestCase { searcher.search(new ConstantScoreQuery(green), 1); assertEquals(Collections.emptyList(), queryCache.cachedQueries()); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); searcher.search(new ConstantScoreQuery(red), 1); assertEquals(Collections.singletonList(red), queryCache.cachedQueries()); @@ -244,7 +256,7 @@ public class TestLRUQueryCache extends LuceneTestCase { final LRUQueryCache queryCache = new LRUQueryCache(Integer.MAX_VALUE, Long.MAX_VALUE, context -> true); searcher.setQueryCache(queryCache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); searcher.search(new BoostQuery(new ConstantScoreQuery(query1), random().nextFloat()), 1); assertEquals(1, queryCache.cachedQueries().size()); @@ -414,7 +426,7 @@ public class TestLRUQueryCache extends LuceneTestCase { final DirectoryReader reader = w.getReader(); final IndexSearcher searcher = new IndexSearcher(reader); searcher.setQueryCache(queryCache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); final int numQueries = atLeast(1000); for (int i = 0; i < numQueries; ++i) { @@ -532,7 +544,7 @@ public class TestLRUQueryCache extends LuceneTestCase { assertEquals(0, queryCache.getCacheSize()); // second pass, lookups + caching, only the first one is a miss - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); for (int i = 0; i < 10; ++i) { searcher.search(new ConstantScoreQuery(query), 1); } @@ -556,7 +568,7 @@ public class TestLRUQueryCache extends LuceneTestCase { assertEquals(1 * segmentCount, queryCache.getCacheSize()); // fourth pass with a different filter which will trigger evictions since the size is 1 - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); for (int i = 0; i < 10; ++i) { searcher.search(new ConstantScoreQuery(query2), 1); } @@ -698,7 +710,7 @@ public class TestLRUQueryCache extends LuceneTestCase { for (IndexSearcher searcher : Arrays.asList(searcher1, searcher2)) { searcher.setQueryCache(queryCache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); } // search on searcher1 @@ -801,7 +813,7 @@ public class TestLRUQueryCache extends LuceneTestCase { final LRUQueryCache queryCache = new LRUQueryCache(1000000, 10000000, context -> true); searcher.setQueryCache(queryCache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); BooleanQuery.Builder bq = new BooleanQuery.Builder(); TermQuery should = new TermQuery(new Term("foo", "baz")); @@ -920,7 +932,7 @@ public class TestLRUQueryCache extends LuceneTestCase { uncachedSearcher.setQueryCache(null); cachedSearcher = newSearcher(reader); cachedSearcher.setQueryCache(queryCache); - cachedSearcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + cachedSearcher.setQueryCachingPolicy(ALWAYS_CACHE); } final Query q = buildRandomQuery(0); assertEquals(uncachedSearcher.count(q), cachedSearcher.count(q)); @@ -983,7 +995,7 @@ public class TestLRUQueryCache extends LuceneTestCase { final LRUQueryCache queryCache = new LRUQueryCache(1, 10000, context -> true); final IndexSearcher searcher = newSearcher(reader); searcher.setQueryCache(queryCache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); BadQuery query = new BadQuery(); searcher.count(query); @@ -1017,7 +1029,7 @@ public class TestLRUQueryCache extends LuceneTestCase { final LRUQueryCache queryCache = new LRUQueryCache(1, 1, context -> random().nextBoolean()); final IndexSearcher searcher = newSearcher(reader); searcher.setQueryCache(queryCache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); searcher.count(new MatchAllDocsQuery()); assertEquals(0, queryCache.getCacheCount()); @@ -1152,7 +1164,7 @@ public class TestLRUQueryCache extends LuceneTestCase { bulkScorerCalled.set(false); weight = searcher.createWeight(new MatchAllDocsQuery(), ScoreMode.COMPLETE_NO_SCORES, 1); weight = new WeightWrapper(weight, scorerCalled, bulkScorerCalled); - weight = cache.doCache(weight, QueryCachingPolicy.ALWAYS_CACHE); + weight = cache.doCache(weight, ALWAYS_CACHE); weight.scorer(leaf); assertEquals(true, bulkScorerCalled.get()); assertEquals(false, scorerCalled.get()); @@ -1177,7 +1189,7 @@ public class TestLRUQueryCache extends LuceneTestCase { }; searcher.setQueryCache(queryCache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); Query query = new DummyQuery(); searcher.count(query); @@ -1196,7 +1208,7 @@ public class TestLRUQueryCache extends LuceneTestCase { w.addDocument(new Document()); DirectoryReader reader = w.getReader(); IndexSearcher searcher = newSearcher(reader); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); LRUQueryCache cache = new LRUQueryCache(2, 10000, new LRUQueryCache.MinSegmentSizePredicate(2, 0f)); searcher.setQueryCache(cache); @@ -1217,7 +1229,7 @@ public class TestLRUQueryCache extends LuceneTestCase { reader.close(); reader = w.getReader(); searcher = newSearcher(reader); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); cache = new LRUQueryCache(2, 10000, new LRUQueryCache.MinSegmentSizePredicate(0, .6f)); searcher.setQueryCache(cache); searcher.count(new DummyQuery()); @@ -1267,7 +1279,7 @@ public class TestLRUQueryCache extends LuceneTestCase { w.addDocument(new Document()); DirectoryReader reader = new DummyDirectoryReader(w.getReader()); IndexSearcher searcher = newSearcher(reader); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); // don't cache if the reader does not expose a cache helper assertNull(reader.leaves().get(0).reader().getCoreCacheHelper()); @@ -1331,7 +1343,7 @@ public class TestLRUQueryCache extends LuceneTestCase { w.addDocument(new Document()); DirectoryReader reader = w.getReader(); IndexSearcher searcher = newSearcher(reader); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); LRUQueryCache cache = new LRUQueryCache(2, 10000, context -> true); searcher.setQueryCache(cache); @@ -1491,7 +1503,7 @@ public class TestLRUQueryCache extends LuceneTestCase { // we need to be single threaded to ensure that LRUQueryCache doesn't skip the cache // due to thread contention IndexSearcher searcher = new AssertingIndexSearcher(random(), reader); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); LRUQueryCache cache = new LRUQueryCache(1, 10000, context -> true); searcher.setQueryCache(cache); @@ -1509,7 +1521,7 @@ public class TestLRUQueryCache extends LuceneTestCase { reader.close(); reader = DirectoryReader.open(w); searcher = new AssertingIndexSearcher(random(), reader); // no newSearcher(reader) - see comment above - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); searcher.setQueryCache(cache); assertEquals(2, searcher.count(query)); @@ -1518,7 +1530,7 @@ public class TestLRUQueryCache extends LuceneTestCase { reader.close(); reader = DirectoryReader.open(w); searcher = new AssertingIndexSearcher(random(), reader); // no newSearcher(reader) - see comment above - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); searcher.setQueryCache(cache); assertEquals(2, searcher.count(query)); @@ -1529,7 +1541,7 @@ public class TestLRUQueryCache extends LuceneTestCase { reader.close(); reader = DirectoryReader.open(w); searcher = new AssertingIndexSearcher(random(), reader); // no newSearcher(reader) - see comment above - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); searcher.setQueryCache(cache); assertEquals(2, searcher.count(query)); @@ -1550,7 +1562,7 @@ public class TestLRUQueryCache extends LuceneTestCase { IndexWriter w = new IndexWriter(dir, iwc); LRUQueryCache queryCache = new LRUQueryCache(10, 1000 * 1000, ctx -> true); IndexSearcher.setDefaultQueryCache(queryCache); - IndexSearcher.setDefaultQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + IndexSearcher.setDefaultQueryCachingPolicy(ALWAYS_CACHE); SearcherManager sm = new SearcherManager(w, new SearcherFactory()); @@ -1622,7 +1634,7 @@ public class TestLRUQueryCache extends LuceneTestCase { LRUQueryCache cache = new LRUQueryCache(1, 100000, context -> true); IndexSearcher searcher = new AssertingIndexSearcher(random(), reader); searcher.setQueryCache(cache); - searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + searcher.setQueryCachingPolicy(ALWAYS_CACHE); Query query = new ConstantScoreQuery(new BooleanQuery.Builder() .add(new BoostQuery(new TermQuery(new Term("field", "foo")), 3), Occur.SHOULD) @@ -1634,7 +1646,7 @@ public class TestLRUQueryCache extends LuceneTestCase { IndexSearcher noCacheHelperSearcher = new AssertingIndexSearcher(random(), noCacheReader); noCacheHelperSearcher.setQueryCache(cache); - noCacheHelperSearcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE); + noCacheHelperSearcher.setQueryCachingPolicy(ALWAYS_CACHE); noCacheHelperSearcher.search(query, 1); Thread t = new Thread(() -> {