Skip to content

Commit

Permalink
Introduce constants for trivial QueryCachingPolicies (#86344)
Browse files Browse the repository at this point in the history
Just a random find:
Cleaning this up, mainly to simplify the already far too complex `IndexShard` constructor.
  • Loading branch information
original-brownbear committed May 2, 2022
1 parent 75ae3f8 commit e4df5fd
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.index.cache.query;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCachingPolicy;

public enum TrivialQueryCachingPolicy implements QueryCachingPolicy {
ALWAYS(true),
NEVER(false);

private final boolean shouldCache;

TrivialQueryCachingPolicy(boolean shouldCache) {
this.shouldCache = shouldCache;
}

@Override
public void onUse(Query query) {}

@Override
public boolean shouldCache(Query query) {
return shouldCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.ReferenceManager;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
Expand Down Expand Up @@ -63,6 +62,7 @@
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.cache.query.TrivialQueryCachingPolicy;
import org.elasticsearch.index.mapper.DocumentParser;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.LuceneDocument;
Expand Down Expand Up @@ -638,18 +638,6 @@ private ExternalReaderManager createReaderManager(RefreshWarmerListener external
}
}

private static final QueryCachingPolicy NEVER_CACHE_POLICY = new QueryCachingPolicy() {
@Override
public void onUse(Query query) {

}

@Override
public boolean shouldCache(Query query) {
return false;
}
};

public final AtomicLong translogGetCount = new AtomicLong(); // number of times realtime get was done on translog
public final AtomicLong translogInMemorySegmentsCount = new AtomicLong(); // number of times in-memory index needed to be created

Expand All @@ -675,7 +663,7 @@ private GetResult getFromTranslog(
ElasticsearchDirectoryReader.wrap(inMemoryReader, shardId),
config().getSimilarity(),
null /*query cache disabled*/,
NEVER_CACHE_POLICY,
TrivialQueryCachingPolicy.NEVER,
inMemoryReader
);
final Searcher wrappedSearcher = searcherWrapper.apply(searcher);
Expand Down
14 changes: 2 additions & 12 deletions server/src/main/java/org/elasticsearch/index/shard/IndexShard.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.SegmentInfos;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.ReferenceManager;
import org.apache.lucene.search.Sort;
Expand Down Expand Up @@ -80,6 +79,7 @@
import org.elasticsearch.index.bulk.stats.ShardBulkStats;
import org.elasticsearch.index.cache.IndexCache;
import org.elasticsearch.index.cache.bitset.ShardBitsetFilterCache;
import org.elasticsearch.index.cache.query.TrivialQueryCachingPolicy;
import org.elasticsearch.index.cache.request.ShardRequestCache;
import org.elasticsearch.index.codec.CodecService;
import org.elasticsearch.index.engine.CommitStats;
Expand Down Expand Up @@ -373,17 +373,7 @@ public IndexShard(
// the query cache is a node-level thing, however we want the most popular filters
// to be computed on a per-shard basis
if (IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.get(settings)) {
cachingPolicy = new QueryCachingPolicy() {
@Override
public void onUse(Query query) {

}

@Override
public boolean shouldCache(Query query) {
return true;
}
};
cachingPolicy = TrivialQueryCachingPolicy.ALWAYS;
} else {
cachingPolicy = new UsageTrackingQueryCachingPolicy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.suggest.document.Completion90PostingsFormat;
import org.apache.lucene.search.suggest.document.SuggestField;
import org.apache.lucene.store.Directory;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.index.cache.query.TrivialQueryCachingPolicy;
import org.elasticsearch.search.suggest.completion.CompletionStats;
import org.elasticsearch.test.ESTestCase;

Expand Down Expand Up @@ -53,16 +52,6 @@ public PostingsFormat getPostingsFormatForField(String field) {
}
});

final QueryCachingPolicy queryCachingPolicy = new QueryCachingPolicy() {
@Override
public void onUse(Query query) {}

@Override
public boolean shouldCache(Query query) {
return false;
}
};

try (Directory directory = newDirectory(); IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig)) {

final Document document = new Document();
Expand All @@ -79,7 +68,7 @@ public boolean shouldCache(Query query) {
openCloseCounter.countOpened();
try {
final DirectoryReader directoryReader = DirectoryReader.open(indexWriter);
return new Engine.Searcher("test", directoryReader, null, null, queryCachingPolicy, () -> {
return new Engine.Searcher("test", directoryReader, null, null, TrivialQueryCachingPolicy.NEVER, () -> {
openCloseCounter.countClosed();
IOUtils.close(directoryReader);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
Expand All @@ -30,6 +29,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.index.cache.query.QueryCacheStats;
import org.elasticsearch.index.cache.query.TrivialQueryCachingPolicy;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.test.ESTestCase;

Expand Down Expand Up @@ -82,20 +82,6 @@ public boolean isCacheable(LeafReaderContext ctx) {

}

private static QueryCachingPolicy alwaysCachePolicy() {
return new QueryCachingPolicy() {
@Override
public void onUse(Query query) {

}

@Override
public boolean shouldCache(Query query) {
return true;
}
};
}

public void testBasics() throws IOException {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
Expand All @@ -105,7 +91,7 @@ public void testBasics() throws IOException {
ShardId shard = new ShardId("index", "_na_", 0);
r = ElasticsearchDirectoryReader.wrap(r, shard);
IndexSearcher s = new IndexSearcher(r);
s.setQueryCachingPolicy(alwaysCachePolicy());
s.setQueryCachingPolicy(TrivialQueryCachingPolicy.ALWAYS);

Settings settings = Settings.builder()
.put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10)
Expand Down Expand Up @@ -176,7 +162,7 @@ public void testTwoShards() throws IOException {
ShardId shard1 = new ShardId("index", "_na_", 0);
r1 = ElasticsearchDirectoryReader.wrap(r1, shard1);
IndexSearcher s1 = new IndexSearcher(r1);
s1.setQueryCachingPolicy(alwaysCachePolicy());
s1.setQueryCachingPolicy(TrivialQueryCachingPolicy.ALWAYS);

Directory dir2 = newDirectory();
IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig());
Expand All @@ -186,7 +172,7 @@ public void testTwoShards() throws IOException {
ShardId shard2 = new ShardId("index", "_na_", 1);
r2 = ElasticsearchDirectoryReader.wrap(r2, shard2);
IndexSearcher s2 = new IndexSearcher(r2);
s2.setQueryCachingPolicy(alwaysCachePolicy());
s2.setQueryCachingPolicy(TrivialQueryCachingPolicy.ALWAYS);

Settings settings = Settings.builder()
.put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10)
Expand Down Expand Up @@ -304,7 +290,7 @@ public void testStatsOnEviction() throws IOException {
ShardId shard1 = new ShardId("index", "_na_", 0);
r1 = ElasticsearchDirectoryReader.wrap(r1, shard1);
IndexSearcher s1 = new IndexSearcher(r1);
s1.setQueryCachingPolicy(alwaysCachePolicy());
s1.setQueryCachingPolicy(TrivialQueryCachingPolicy.ALWAYS);

Directory dir2 = newDirectory();
IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig());
Expand All @@ -314,7 +300,7 @@ public void testStatsOnEviction() throws IOException {
ShardId shard2 = new ShardId("index", "_na_", 1);
r2 = ElasticsearchDirectoryReader.wrap(r2, shard2);
IndexSearcher s2 = new IndexSearcher(r2);
s2.setQueryCachingPolicy(alwaysCachePolicy());
s2.setQueryCachingPolicy(TrivialQueryCachingPolicy.ALWAYS);

Settings settings = Settings.builder()
.put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10)
Expand Down Expand Up @@ -389,15 +375,7 @@ public void testDelegatesScorerSupplier() throws Exception {
ShardId shard = new ShardId("index", "_na_", 0);
r = ElasticsearchDirectoryReader.wrap(r, shard);
IndexSearcher s = new IndexSearcher(r);
s.setQueryCachingPolicy(new QueryCachingPolicy() {
@Override
public boolean shouldCache(Query query) throws IOException {
return false; // never cache
}

@Override
public void onUse(Query query) {}
});
s.setQueryCachingPolicy(TrivialQueryCachingPolicy.NEVER);

Settings settings = Settings.builder()
.put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.lucene.search.LRUQueryCache;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
Expand All @@ -35,6 +34,7 @@
import org.apache.lucene.tests.search.RandomApproximationQuery;
import org.apache.lucene.tests.util.TestUtil;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.index.cache.query.TrivialQueryCachingPolicy;
import org.elasticsearch.search.internal.ContextIndexSearcher;
import org.elasticsearch.search.profile.ProfileResult;
import org.elasticsearch.test.ESTestCase;
Expand Down Expand Up @@ -75,7 +75,7 @@ public static void setup() throws IOException {
reader,
IndexSearcher.getDefaultSimilarity(),
IndexSearcher.getDefaultQueryCache(),
ALWAYS_CACHE_POLICY,
TrivialQueryCachingPolicy.ALWAYS,
true
);
}
Expand Down Expand Up @@ -296,15 +296,4 @@ public void testScorerSupplier() throws IOException {
dir.close();
}

private static final QueryCachingPolicy ALWAYS_CACHE_POLICY = new QueryCachingPolicy() {

@Override
public void onUse(Query query) {}

@Override
public boolean shouldCache(Query query) throws IOException {
return true;
}

};
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCache;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
Expand Down Expand Up @@ -69,6 +68,7 @@
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.cache.bitset.BitsetFilterCache;
import org.elasticsearch.index.cache.query.DisabledQueryCache;
import org.elasticsearch.index.cache.query.TrivialQueryCachingPolicy;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
import org.elasticsearch.index.mapper.BinaryFieldMapper;
Expand Down Expand Up @@ -372,23 +372,13 @@ private SubSearchContext buildSubSearchContext(
) {
SearchContext ctx = mock(SearchContext.class);
QueryCache queryCache = new DisabledQueryCache(indexSettings);
QueryCachingPolicy queryCachingPolicy = new QueryCachingPolicy() {
@Override
public void onUse(Query query) {}

@Override
public boolean shouldCache(Query query) {
// never cache a query
return false;
}
};
try {
when(ctx.searcher()).thenReturn(
new ContextIndexSearcher(
searchExecutionContext.searcher().getIndexReader(),
searchExecutionContext.searcher().getSimilarity(),
queryCache,
queryCachingPolicy,
TrivialQueryCachingPolicy.NEVER,
false
)
);
Expand Down

0 comments on commit e4df5fd

Please sign in to comment.