Apache Solr open-source search software
SOLR-16693: timeAllowed: use TimeLimitingBulkScorer (#1450)
David Smileyc9d5bcbf8098
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 444ad605d08..afca3478b4c 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -81,6 +81,8 @@ OptimizationsSolr can also be started with the system property `solr.http.disableCookies` set to `true`, so that the internal clientsthat Solr uses to communicate between nodes use `HttpCookieStore.Empty`. (Tomás Fernández Löbbe)+* SOLR-16693: For query timeAllowed, switch from ExitableDirectoryReader to TimeLimitingBulkScorer (David Smiley)+Bug Fixes---------------------diff --git a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.javaindex c2d4d252aa7..6fe7dc4570c 100644--- a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java+++ b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java@@ -81,6 +81,7 @@ import org.apache.lucene.search.TopScoreDocCollector;import org.apache.lucene.search.TotalHitCountCollector;import org.apache.lucene.search.TotalHits;import org.apache.lucene.search.TotalHits.Relation;+import org.apache.lucene.search.Weight;import org.apache.lucene.store.AlreadyClosedException;import org.apache.lucene.store.Directory;import org.apache.lucene.util.Bits;@@ -133,6 +134,10 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIprivate static final Map<String, SolrCache<?, ?>> NO_GENERIC_CACHES = Collections.emptyMap();private static final SolrCache<?, ?>[] NO_CACHES = new SolrCache<?, ?>[0];+ // If you find this useful, let us know in [email protected]. Likely to be removed eventually.+ private static final boolean useExitableDirectoryReader =+ Boolean.getBoolean("solr.useExitableDirectoryReader");+private final SolrCore core;private final IndexSchema schema;private final SolrDocumentFetcher docFetcher;@@ -197,9 +202,11 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIprivate static DirectoryReader wrapReader(SolrCore core, DirectoryReader reader)throws IOException {assert reader != null;- return ExitableDirectoryReader.wrap(- UninvertingReader.wrap(reader, core.getLatestSchema().getUninversionMapper()),- SolrQueryTimeoutImpl.getInstance());+ reader = UninvertingReader.wrap(reader, core.getLatestSchema().getUninversionMapper());+ if (useExitableDirectoryReader) { // SOLR-16693 legacy; may be removed. Probably inefficient.+ reader = ExitableDirectoryReader.wrap(reader, SolrQueryTimeoutImpl.getInstance());+ }+ return reader;}/**@@ -704,23 +711,42 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIreturn qr;}- // FIXME: This option has been dead/noop since 3.1, should we re-enable or remove it?- // public Hits search(Query query, Filter filter, Sort sort) throws IOException {- // // todo - when Solr starts accepting filters, need to- // // change this conditional check (filter!=null) and create a new filter- // // that ANDs them together if it already exists.- //- // if (optimizer==null || filter!=null || !(query instanceof BooleanQuery)- // ) {- // return super.search(query,filter,sort);- // } else {- // Query[] newQuery = new Query[1];- // Filter[] newFilter = new Filter[1];- // optimizer.optimize((BooleanQuery)query, this, 0, newQuery, newFilter);- //- // return super.search(newQuery[0], newFilter[0], sort);- // }- // }+ @Override+ protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector)+ throws IOException {+ final var queryTimeout = SolrQueryTimeoutImpl.getInstance();+ if (useExitableDirectoryReader || queryTimeout.isTimeoutEnabled() == false) {+ // no timeout. Pass through to super class+ super.search(leaves, weight, collector);+ } else {+ // Timeout enabled! This impl is maybe a hack. Use Lucene's IndexSearcher timeout.+ // But only some queries have it so don't use on "this" (SolrIndexSearcher), not to mention+ // that timedOut() might race with concurrent queries (dubious design).+ // So we need to make a new IndexSearcher instead of using "this".+ new IndexSearcher(reader) { // cheap, actually!+ void searchWithTimeout() throws IOException {+ setTimeout(queryTimeout.makeLocalImpl());+ super.search(leaves, weight, collector); // FYI protected access+ if (timedOut()) {+ throw new TimeAllowedExceededFromScorerException("timeAllowed exceeded");+ }+ }+ }.searchWithTimeout();+ }+ }++ /**+ * Thrown when {@link org.apache.solr.common.params.CommonParams#TIME_ALLOWED} is exceeded.+ * Further, from the low level Lucene {@code org.apache.lucene.search.TimeLimitingBulkScorer}.+ * Extending {@code ExitableDirectoryReader.ExitingReaderException} is for legacy reasons.+ */+ public static class TimeAllowedExceededFromScorerException+ extends ExitableDirectoryReader.ExitingReaderException {++ public TimeAllowedExceededFromScorerException(String msg) {+ super(msg);+ }+ }/*** Retrieve the {@link Document} instance corresponding to the document id.diff --git a/solr/core/src/java/org/apache/solr/search/SolrQueryTimeoutImpl.java b/solr/core/src/java/org/apache/solr/search/SolrQueryTimeoutImpl.javaindex bc096ae2ef4..a30bb8d0f6c 100644--- a/solr/core/src/java/org/apache/solr/search/SolrQueryTimeoutImpl.java+++ b/solr/core/src/java/org/apache/solr/search/SolrQueryTimeoutImpl.java@@ -91,4 +91,17 @@ public class SolrQueryTimeoutImpl implements QueryTimeout {public String toString() {return "timeoutAt: " + getTimeoutAtNs() + " (System.nanoTime(): " + nanoTime() + ")";}++ /** Internal impl for speed only used when we know there's a timeout enabled. */+ QueryTimeout makeLocalImpl() {+ assert isTimeoutEnabled();+ return new QueryTimeout() {+ final long timeoutAt = getTimeoutAtNs();++ @Override+ public boolean shouldExit() {+ return timeoutAt - nanoTime() < 0L;+ }+ };+ }}