vigyasharma commented on code in PR #13202: URL: https://github.com/apache/lucene/pull/13202#discussion_r1536974998
########## lucene/core/src/java/org/apache/lucene/search/TimeLimitingKnnCollectorManager.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.search; + +import java.io.IOException; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.QueryTimeout; +import org.apache.lucene.search.knn.KnnCollectorManager; + +/** A {@link KnnCollectorManager} that collects results with a timeout. */ +public class TimeLimitingKnnCollectorManager implements KnnCollectorManager { + private final KnnCollectorManager delegate; + private final QueryTimeout queryTimeout; + + public TimeLimitingKnnCollectorManager(KnnCollectorManager delegate, QueryTimeout timeout) { + this.delegate = delegate; + this.queryTimeout = timeout; + } + + /** Get the {@link QueryTimeout} for terminating graph searches. */ + public QueryTimeout getQueryTimeout() { + return queryTimeout; + } + + @Override + public KnnCollector newCollector(int visitedLimit, LeafReaderContext context) throws IOException { + KnnCollector collector = delegate.newCollector(visitedLimit, context); + if (queryTimeout == null) { + return collector; + } + return new KnnCollector() { + @Override + public boolean earlyTerminated() { + return queryTimeout.shouldExit() || collector.earlyTerminated(); + } + + @Override + public void incVisitedCount(int count) { + collector.incVisitedCount(count); + } + + @Override + public long visitedCount() { + return collector.visitedCount(); + } + + @Override + public long visitLimit() { + return collector.visitLimit(); + } + + @Override + public int k() { + return collector.k(); + } + + @Override + public boolean collect(int docId, float similarity) { + return collector.collect(docId, similarity); + } + + @Override + public float minCompetitiveSimilarity() { + return collector.minCompetitiveSimilarity(); + } + + @Override + public TopDocs topDocs() { + if (queryTimeout.shouldExit()) { + // Quickly exit + return TopDocsCollector.EMPTY_TOPDOCS; + } Review Comment: I still think `topDocs()` from the collector should return whatever it collected, and set the `relation` to GTE. With `EMPTY_TOPDOCS`, the query gets rewritten as `MatchNoDocsQuery` which is misleads debugging and further masks the timeout occurrence. We can let the results get discarded by `TimeLimitingBulkScorer` for now, while we handle that in a separate change (since it is at par with existing behavior). I'm open to more opinions on this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org