Skip to content

Commit 15023ba

Browse files
authored
Disable caching when queries are profiled (#48195)
This change disables the query and request cache when profile is set to true in the request. This means that profiled queries will not check caches to execute the query and the result will never be added in the cache either. Closes #33298
1 parent 19b6b36 commit 15023ba

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
lines changed

server/src/main/java/org/elasticsearch/indices/IndicesService.java

+5
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,11 @@ public boolean canCache(ShardSearchRequest request, SearchContext context) {
12451245
return false;
12461246
}
12471247

1248+
// Profiled queries should not use the cache
1249+
if (request.source() != null && request.source().profile()) {
1250+
return false;
1251+
}
1252+
12481253
IndexSettings settings = context.indexShard().indexSettings();
12491254
// if not explicitly set in the request, use the index setting, if not, use the request
12501255
if (request.requestCache() == null) {

server/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public Weight createWeight(Query query, ScoreMode scoreMode, float boost) throws
120120
timer.start();
121121
final Weight weight;
122122
try {
123-
weight = super.createWeight(query, scoreMode, boost);
123+
weight = query.createWeight(this, scoreMode, boost);
124124
} finally {
125125
timer.stop();
126126
profiler.pollLastElement();

server/src/main/java/org/elasticsearch/search/profile/query/ProfileWeight.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void extractTerms(Set<Term> set) {
120120

121121
@Override
122122
public boolean isCacheable(LeafReaderContext ctx) {
123-
return subQueryWeight.isCacheable(ctx);
123+
return false;
124124
}
125125

126126
}

server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java

+42-2
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,49 @@ public void testCacheWithFilteredAlias() {
414414
assertCacheState(client, "index", 2, 2);
415415
}
416416

417+
public void testProfileDisableCache() throws Exception {
418+
Client client = client();
419+
assertAcked(
420+
client.admin().indices().prepareCreate("index")
421+
.addMapping("_doc", "k", "type=keyword")
422+
.setSettings(
423+
Settings.builder()
424+
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
425+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
426+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
427+
)
428+
.get()
429+
);
430+
indexRandom(true, client.prepareIndex("index").setSource("k", "hello"));
431+
ensureSearchable("index");
432+
433+
int expectedHits = 0;
434+
int expectedMisses = 0;
435+
for (int i = 0; i < 5; i++) {
436+
boolean profile = i % 2 == 0;
437+
SearchResponse resp = client.prepareSearch("index")
438+
.setRequestCache(true)
439+
.setProfile(profile)
440+
.setQuery(QueryBuilders.termQuery("k", "hello"))
441+
.get();
442+
assertSearchResponse(resp);
443+
ElasticsearchAssertions.assertAllSuccessful(resp);
444+
assertThat(resp.getHits().getTotalHits().value, equalTo(1L));
445+
if (profile == false) {
446+
if (i == 1) {
447+
expectedMisses ++;
448+
} else {
449+
expectedHits ++;
450+
}
451+
}
452+
assertCacheState(client, "index", expectedHits, expectedMisses);
453+
}
454+
}
455+
417456
private static void assertCacheState(Client client, String index, long expectedHits, long expectedMisses) {
418-
RequestCacheStats requestCacheStats = client.admin().indices().prepareStats(index).setRequestCache(true).get().getTotal()
419-
.getRequestCache();
457+
RequestCacheStats requestCacheStats = client.admin().indices().prepareStats(index)
458+
.setRequestCache(true)
459+
.get().getTotal().getRequestCache();
420460
// Check the hit count and miss count together so if they are not
421461
// correct we can see both values
422462
assertEquals(Arrays.asList(expectedHits, expectedMisses, 0L),

server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerIT.java

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class QueryProfilerIT extends ESIntegTestCase {
5454
* This test simply checks to make sure nothing crashes. Test indexes 100-150 documents,
5555
* constructs 20-100 random queries and tries to profile them
5656
*/
57-
@AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/LUCENE-8658")
5857
public void testProfileQuery() throws Exception {
5958
createIndex("test");
6059
ensureGreen();

server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerTests.java

+12-19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.lucene.index.Term;
3131
import org.apache.lucene.search.Explanation;
3232
import org.apache.lucene.search.IndexSearcher;
33+
import org.apache.lucene.search.LRUQueryCache;
3334
import org.apache.lucene.search.LeafCollector;
3435
import org.apache.lucene.search.Query;
3536
import org.apache.lucene.search.QueryCachingPolicy;
@@ -47,6 +48,7 @@
4748
import org.elasticsearch.search.internal.ContextIndexSearcher;
4849
import org.elasticsearch.search.profile.ProfileResult;
4950
import org.elasticsearch.test.ESTestCase;
51+
import org.junit.After;
5052
import org.junit.AfterClass;
5153
import org.junit.BeforeClass;
5254

@@ -81,7 +83,16 @@ public static void setup() throws IOException {
8183
reader = w.getReader();
8284
w.close();
8385
searcher = new ContextIndexSearcher(reader, IndexSearcher.getDefaultSimilarity(),
84-
IndexSearcher.getDefaultQueryCache(), MAYBE_CACHE_POLICY);
86+
IndexSearcher.getDefaultQueryCache(), ALWAYS_CACHE_POLICY);
87+
}
88+
89+
@After
90+
public void checkNoCache() {
91+
LRUQueryCache cache = (LRUQueryCache) searcher.getQueryCache();
92+
assertThat(cache.getHitCount(), equalTo(0L));
93+
assertThat(cache.getCacheCount(), equalTo(0L));
94+
assertThat(cache.getTotalCount(), equalTo(cache.getMissCount()));
95+
assertThat(cache.getCacheSize(), equalTo(0L));
8596
}
8697

8798
@AfterClass
@@ -158,10 +169,6 @@ public void testUseIndexStats() throws IOException {
158169

159170
public void testApproximations() throws IOException {
160171
QueryProfiler profiler = new QueryProfiler();
161-
// disable query caching since we want to test approximations, which won't
162-
// be exposed on a cached entry
163-
ContextIndexSearcher searcher = new ContextIndexSearcher(reader, IndexSearcher.getDefaultSimilarity(),
164-
null, MAYBE_CACHE_POLICY);
165172
searcher.setProfiler(profiler);
166173
Query query = new RandomApproximationQuery(new TermQuery(new Term("foo", "bar")), random());
167174
searcher.count(query);
@@ -184,7 +191,6 @@ public void testApproximations() throws IOException {
184191

185192
long rewriteTime = profiler.getRewriteTime();
186193
assertThat(rewriteTime, greaterThan(0L));
187-
188194
}
189195

190196
public void testCollector() throws IOException {
@@ -288,17 +294,4 @@ public boolean shouldCache(Query query) throws IOException {
288294
}
289295

290296
};
291-
292-
private static final QueryCachingPolicy NEVER_CACHE_POLICY = new QueryCachingPolicy() {
293-
294-
@Override
295-
public void onUse(Query query) {}
296-
297-
@Override
298-
public boolean shouldCache(Query query) throws IOException {
299-
return false;
300-
}
301-
302-
};
303-
304297
}

0 commit comments

Comments
 (0)