Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for query cancellation during rewrite #53166

Merged
merged 5 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.Weight;
import org.elasticsearch.action.search.SearchShardTask;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.lucene.Lucene;
Expand Down Expand Up @@ -110,7 +109,23 @@ public QueryPhase() {

@Override
public void preProcess(SearchContext context) {
context.preProcess(true);
final Runnable cancellation;
if (context.lowLevelCancellation()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to ignore this parameter. Let's leave it for now but I wonder if we should simply remove this setting in a follow up. @jpountz what do you think ? I can open an issue if you think this deserves a full discussion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the long term, I agree that we should remove it. The thing that isn't clear to me is how much it hurts terms-dict-intensive queries like fuzzy queries.

cancellation = context.searcher().addQueryCancellation(() -> {
if (context.getTask().isCancelled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's get the task once outside of the runnable like we do below ?

throw new TaskCancelledException("cancelled");
}
});
} else {
cancellation = null;
}
try {
context.preProcess(true);
} finally {
if (cancellation != null) {
context.searcher().removeQueryCancellation(cancellation);
}
}
}

@Override
Expand Down Expand Up @@ -265,9 +280,8 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
}

if (searchContext.lowLevelCancellation()) {
SearchShardTask task = searchContext.getTask();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight preference for this version. The task should never change during the execution of a search request.

searcher.addQueryCancellation(() -> {
if (task.isCancelled()) {
if (searchContext.getTask().isCancelled()) {
throw new TaskCancelledException("cancelled");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
Expand All @@ -75,6 +77,7 @@
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.index.query.ParsedQuery;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.search.ESToParentBlockJoinQuery;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.IndexShardTestCase;
Expand All @@ -83,6 +86,7 @@
import org.elasticsearch.search.internal.ScrollContext;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.sort.SortAndFormats;
import org.elasticsearch.tasks.TaskCancelledException;
import org.elasticsearch.test.TestSearchContext;

import java.io.IOException;
Expand Down Expand Up @@ -825,7 +829,55 @@ public void testMinScore() throws Exception {

reader.close();
dir.close();
}

public void testCancellationDuringPreprocess() throws IOException {
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig())) {

for (int i = 0; i < 10; i++) {
Document doc = new Document();
doc.add(new StringField("foo", "a".repeat(i), Store.NO));
w.addDocument(doc);
}
w.flush();
w.close();

try (IndexReader reader = DirectoryReader.open(dir)) {
TestSearchContext context = new TestSearchContextWithRewriteAndCancellation(
null, indexShard, newContextSearcher(reader));
PrefixQuery prefixQuery = new PrefixQuery(new Term("foo", "a"));
prefixQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
context.parsedQuery(new ParsedQuery(prefixQuery));
SearchShardTask task = mock(SearchShardTask.class);
when(task.isCancelled()).thenReturn(true);
context.setTask(task);
expectThrows(TaskCancelledException.class, () -> new QueryPhase().preProcess(context));
}
}
}

private static class TestSearchContextWithRewriteAndCancellation extends TestSearchContext {

private TestSearchContextWithRewriteAndCancellation(QueryShardContext queryShardContext,
IndexShard indexShard,
ContextIndexSearcher searcher) {
super(queryShardContext, indexShard, searcher);
}

@Override
public void preProcess(boolean rewrite) {
try {
searcher().rewrite(query());
} catch (IOException e) {
fail("IOException shouldn't be thrown");
}
}

@Override
public boolean lowLevelCancellation() {
return true;
}
}

private static ContextIndexSearcher newContextSearcher(IndexReader reader) throws IOException {
Expand Down