Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -37,6 +37,7 @@
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BulkScorer;
import org.apache.lucene.search.CollectionStatistics;
Expand Down Expand Up @@ -69,6 +70,7 @@
import org.opensearch.common.lease.Releasable;
import org.opensearch.common.lucene.Lucene;
import org.opensearch.common.lucene.search.TopDocsAndMaxScore;
import org.opensearch.core.tasks.TaskCancelledException;
import org.opensearch.lucene.util.CombinedBitSet;
import org.opensearch.search.DocValueFormat;
import org.opensearch.search.SearchHits;
Expand Down Expand Up @@ -157,6 +159,13 @@ private ContextIndexSearcher(
setQueryCachingPolicy(queryCachingPolicy);
this.cancellable = cancellable;
this.searchContext = searchContext;
// Set the timeout on the IndexSearcher so that Lucene-native timeout-aware components
// (e.g. TimeLimitingKnnCollectorManager used by AbstractKnnVectorQuery) can enforce
// the query timeout. Without this, searcher.getTimeout() returns null and KNN vector
// searches ignore the configured query timeout entirely.
if (cancellable != null) {
setTimeout(cancellable);
}
}

public void setProfiler(QueryProfiler profiler) {
Expand Down Expand Up @@ -604,7 +613,23 @@ public DirectoryReader getDirectoryReader() {
return (DirectoryReader) reader;
}

private static class MutableQueryTimeout implements ExitableDirectoryReader.QueryCancellation {
/**
* A mutable timeout implementation that bridges OpenSearch's cancellation mechanism with Lucene's
* {@link QueryTimeout} interface.
* <p>
* This class implements both {@link ExitableDirectoryReader.QueryCancellation} (used by OpenSearch's
* {@link ExitableDirectoryReader} to check for cancellation while iterating terms, points, and stored fields)
* and {@link QueryTimeout} (used by Lucene's {@link org.apache.lucene.search.IndexSearcher} to enforce
* timeouts in components like {@link org.apache.lucene.search.TimeLimitingKnnCollectorManager} for KNN
* vector queries).
* <p>
* Cancellation runnables are added/removed dynamically via {@link #add} and {@link #remove}. When any
* runnable throws a {@link RuntimeException} (e.g. {@link org.opensearch.search.query.QueryPhase.TimeExceededException}),
* it signals that the query should be terminated.
*
* @opensearch.internal
*/
private static class MutableQueryTimeout implements ExitableDirectoryReader.QueryCancellation, QueryTimeout {

private final Set<Runnable> runnables = new HashSet<>();

Expand Down Expand Up @@ -632,6 +657,26 @@ public boolean isEnabled() {
return runnables.isEmpty() == false;
}

/**
* Implements {@link QueryTimeout#shouldExit()} by delegating to {@link #checkCancelled()}.
* Returns {@code true} if a registered cancellation runnable throws a
* {@link org.opensearch.search.query.QueryPhase.TimeExceededException} (timeout) or
* {@link org.opensearch.core.tasks.TaskCancelledException} (task cancellation),
* indicating that the query should be terminated early.
* <p>
* This is called by Lucene's {@link org.apache.lucene.search.TimeLimitingKnnCollectorManager}
* during KNN vector search to check whether the search should be terminated early.
*/
@Override
public boolean shouldExit() {
try {
checkCancelled();
} catch (QueryPhase.TimeExceededException | TaskCancelledException e) {
return true;
}
return false;
}

public void clear() {
runnables.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.BoostQuery;
Expand All @@ -72,11 +73,13 @@
import org.apache.lucene.util.SparseFixedBitSet;
import org.opensearch.ExceptionsHelper;
import org.opensearch.action.support.StreamSearchChannelListener;
import org.opensearch.common.CheckedConsumer;
import org.opensearch.common.lucene.index.OpenSearchDirectoryReader;
import org.opensearch.common.lucene.index.SequentialStoredFieldsLeafReader;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.io.IOUtils;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.core.tasks.TaskCancelledException;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.cache.bitset.BitsetFilterCache;
import org.opensearch.index.shard.IndexShard;
Expand All @@ -88,6 +91,7 @@
import org.opensearch.search.aggregations.metrics.InternalSum;
import org.opensearch.search.fetch.FetchSearchResult;
import org.opensearch.search.fetch.QueryFetchSearchResult;
import org.opensearch.search.query.QueryPhase;
import org.opensearch.search.query.QuerySearchResult;
import org.opensearch.test.IndexSettingsModule;
import org.opensearch.test.OpenSearchTestCase;
Expand Down Expand Up @@ -586,6 +590,92 @@ public void visit(QueryVisitor visitor) {
}
}

public void testTimeoutIsSetOnSearcher() throws Exception {
withContextIndexSearcher(searcher -> {
QueryTimeout timeout = searcher.getTimeout();
assertNotNull("setTimeout should have been called with MutableQueryTimeout", timeout);
});
}

public void testTimeoutShouldExitReturnsFalseWhenNoCancellations() throws Exception {
withContextIndexSearcher(searcher -> {
assertFalse("shouldExit should return false when no cancellations are registered", searcher.getTimeout().shouldExit());
});
}

public void testTimeoutShouldExitReturnsFalseWhenCancellationDoesNotThrow() throws Exception {
withContextIndexSearcher(searcher -> {
searcher.addQueryCancellation(() -> {});
assertFalse("shouldExit should return false when cancellation does not throw", searcher.getTimeout().shouldExit());
});
}

public void testTimeoutShouldExitReturnsTrueWhenTimeoutExceeded() throws Exception {
withContextIndexSearcher(searcher -> {
searcher.addQueryCancellation(() -> { throw new QueryPhase.TimeExceededException(); });
assertTrue("shouldExit should return true on TimeExceededException", searcher.getTimeout().shouldExit());
});
}

public void testTimeoutShouldExitReturnsTrueWhenTaskCancelled() throws Exception {
withContextIndexSearcher(searcher -> {
searcher.addQueryCancellation(() -> { throw new TaskCancelledException("cancelled"); });
assertTrue("shouldExit should return true on TaskCancelledException", searcher.getTimeout().shouldExit());
});
}

public void testTimeoutShouldExitDoesNotCatchUnrelatedExceptions() throws Exception {
withContextIndexSearcher(searcher -> {
searcher.addQueryCancellation(() -> { throw new NullPointerException("unrelated"); });
expectThrows(NullPointerException.class, () -> searcher.getTimeout().shouldExit());
});
}

public void testTimeoutShouldExitReflectsRemoval() throws Exception {
withContextIndexSearcher(searcher -> {
Runnable cancellation = searcher.addQueryCancellation(() -> { throw new QueryPhase.TimeExceededException(); });
assertTrue("shouldExit should return true while cancellation is active", searcher.getTimeout().shouldExit());

searcher.removeQueryCancellation(cancellation);
assertFalse("shouldExit should return false after cancellation is removed", searcher.getTimeout().shouldExit());
});
}

/**
* Helper that creates a {@link ContextIndexSearcher} backed by a single-doc index and a mocked
* {@link SearchContext}, then passes it to the provided consumer. All resources are closed
* automatically.
*/
private void withContextIndexSearcher(CheckedConsumer<ContextIndexSearcher, Exception> test) throws Exception {
try (
Directory directory = newDirectory();
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()))
) {
Document doc = new Document();
doc.add(new StringField("field", "value", Field.Store.NO));
writer.addDocument(doc);
writer.commit();

try (DirectoryReader reader = DirectoryReader.open(directory)) {
SearchContext searchContext = mock(SearchContext.class);
IndexShard indexShard = mock(IndexShard.class);
when(searchContext.indexShard()).thenReturn(indexShard);
when(searchContext.bucketCollectorProcessor()).thenReturn(SearchContext.NO_OP_BUCKET_COLLECTOR_PROCESSOR);

ContextIndexSearcher searcher = new ContextIndexSearcher(
reader,
IndexSearcher.getDefaultSimilarity(),
IndexSearcher.getDefaultQueryCache(),
IndexSearcher.getDefaultQueryCachingPolicy(),
true,
null,
searchContext
);
test.accept(searcher);
}
}
}

public void testSendBatchWithSingleAggregation() throws Exception {
try (
Directory directory = newDirectory();
Expand Down
Loading