Added queryTimeout to the IndexSearcher to ensure that queries that uses this timeout during search exit when queries times out - #21316
Conversation
PR Reviewer Guide 🔍(Review updated until commit 350affc)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to 350affc
Previous suggestionsSuggestions up to commit 73bb8c9
|
…ses this timeout during search exit when queries timesout Signed-off-by: Navneet Verma <navneev@amazon.com>
|
Updated code from the suggestion added by PR bot |
|
Persistent review updated to latest commit 350affc |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #21316 +/- ##
============================================
- Coverage 73.40% 73.37% -0.04%
+ Complexity 74092 74030 -62
============================================
Files 5948 5948
Lines 336527 336534 +7
Branches 48552 48553 +1
============================================
- Hits 247035 246931 -104
- Misses 69836 69907 +71
- Partials 19656 19696 +40 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ses this timeout during search exit when queries timesout (opensearch-project#21316) Signed-off-by: Navneet Verma <navneev@amazon.com>
Description
Added queryTimeout to the IndexSearcher to ensure that queries that uses this timeout during search exit when queries times out
Problem
OpenSearch implements its own query timeout/cancellation mechanism via
MutableQueryTimeoutandExitableDirectoryReader, which hooks into Lucene at the reader level (terms, points, bulk scoring). However, it never callsIndexSearcher.setTimeout()to set the timeout on Lucene'sIndexSearcher.This is a problem because Lucene's
AbstractKnnVectorQueryrelies onIndexSearcher.getTimeout()to enforce timeouts during KNN vector search. Specifically, inAbstractKnnVectorQuery.rewrite():Since
searcher.getTimeout()returnsnull,TimeLimitingKnnCollectorManagergets a nullQueryTimeout, and KNN vector searches completely ignore the query timeout. This means a slow KNN query can run indefinitely even when a user has set atimeouton their search request or the cluster hassearch.default_search_timeoutconfigured.Additionally,
ExitableDirectoryReader.ExitableLeafReaderdoes not wrapFloatVectorValues,ByteVectorValues, or any vector-related APIs, so there are no cancellation checks during HNSW graph traversal either.What's NOT covered by the existing timeout mechanism
Terms/TermsEnumExitableTerms,ExitableTermsEnum)PointValues/PointTreeExitablePointValues,ExitablePointTree)TimeLimitingKnnCollectorManagerIndexSearcher.getTimeout())getTimeout()returnsnullFix
Two changes in
ContextIndexSearcher.java:MutableQueryTimeoutnow implements both interfaces:ExitableDirectoryReader.QueryCancellation(OpenSearch's interface — existing)org.apache.lucene.index.QueryTimeout(Lucene's interface — new)The new
shouldExit()method delegates tocheckCancelled()— if any cancellation runnable throws, it returnstrue.setTimeout(cancellable)is called in theContextIndexSearcherconstructor so thatsearcher.getTimeout()returns theMutableQueryTimeoutinstance instead ofnull.Impact
TimeLimitingKnnCollectorManager.IndexSearcher.getTimeout()will automatically work.ExitableDirectoryReader+addQueryCancellationmechanism continues to work exactly as before for terms, points, and bulk scoring.Testing
Added 5 unit tests in
ContextIndexSearcherTests:testTimeoutIsSetOnSearchergetTimeout()returns non-null after construction — confirmssetTimeout(cancellable)is calledtestTimeoutShouldExitReturnsFalseWhenNoCancellationsshouldExit()returnsfalsewhen no cancellation runnables are registeredtestTimeoutShouldExitReturnsFalseWhenCancellationDoesNotThrowshouldExit()returnsfalsewhen a no-op cancellation is registered (query still within time)testTimeoutShouldExitReturnsTrueWhenCancellationThrowsshouldExit()returnstruewhen a cancellation runnable throws (timeout exceeded)testTimeoutShouldExitReflectsRemovalshouldExit()transitions fromtrue→falseafter the throwing cancellation is removed viaremoveQueryCancellation()Related Issues
NA
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.