Apache Lucene open-source search software
Add exponential growth to TimeLimitingBulkScorer (#11984)
Costin Leau4eba6a12843b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 0c3acec854c..6dd69fa32ae 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -125,6 +125,10 @@ API Changes* GITHUB#11962: VectorValues#cost() now delegates to VectorValues#size().(Adrien Grand)++* GITHUB#11984: Improved TimeLimitBulkScorer to check the timeout at exponantial rate.+ (Costin Leau)+New Features---------------------diff --git a/lucene/core/src/java/org/apache/lucene/search/TimeLimitingBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/TimeLimitingBulkScorer.javaindex 83687491a3d..5e33884ea4f 100644--- a/lucene/core/src/java/org/apache/lucene/search/TimeLimitingBulkScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/TimeLimitingBulkScorer.java@@ -61,8 +61,15 @@ final class TimeLimitingBulkScorer extends BulkScorer {@Overridepublic int score(LeafCollector collector, Bits acceptDocs, int min, int max) throws IOException {+ int interval = INTERVAL;while (min < max) {- final int newMax = (int) Math.min((long) min + INTERVAL, max);+ final int newMax = (int) Math.min((long) min + interval, max);+ final int newInterval =+ interval + (interval >> 1); // increase the interval by 50% on each iteration+ // overflow check+ if (interval < newInterval) {+ interval = newInterval;+ }if (queryTimeout.shouldExit()) {throw new TimeLimitingBulkScorer.TimeExceededException();}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestTimeLimitingBulkScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestTimeLimitingBulkScorer.javaindex 7e6677fd8d8..cce82cd34ac 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestTimeLimitingBulkScorer.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestTimeLimitingBulkScorer.java@@ -17,15 +17,18 @@package org.apache.lucene.search;+import java.io.IOException;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.QueryTimeout;+import org.apache.lucene.index.QueryTimeoutImpl;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.tests.analysis.MockAnalyzer;import org.apache.lucene.tests.util.LuceneTestCase;+import org.apache.lucene.util.Bits;/** Tests the {@link TimeLimitingBulkScorer}. */public class TestTimeLimitingBulkScorer extends LuceneTestCase {@@ -62,6 +65,51 @@ public class TestTimeLimitingBulkScorer extends LuceneTestCase {directory.close();}+ public void testExponentialRate() throws Exception {+ int MAX_DOCS = DocIdSetIterator.NO_MORE_DOCS - 1;+ var bulkScorer =+ new BulkScorer() {+ int expectedInterval = TimeLimitingBulkScorer.INTERVAL;+ int lastMax = 0;+ int lastInterval = 0;++ @Override+ public int score(LeafCollector collector, Bits acceptDocs, int min, int max)+ throws IOException {+ var difference = max - min;+ // the rate shouldn't overflow - only increase or remain equal+ assertTrue("Rate should only go up", difference >= lastInterval);+ // make sure no documents end up being skipped+ assertTrue("Documents skipped", lastMax == min);+ // the difference should be exactly the step except the last run where the difference is+ // smaller since there are not enough docs+ assertTrue(+ "Incorrect rate encountered",+ max == MAX_DOCS ? expectedInterval >= difference : expectedInterval == difference);++ lastMax = max;+ lastInterval = difference;++ // use integer sum since the exponential growth formula yields different result due to+ // rounding+ expectedInterval = expectedInterval + expectedInterval / 2;+ // overflow - stop at the previous one+ if (expectedInterval < 0) {+ expectedInterval = lastInterval;+ }+ return max;+ }++ @Override+ public long cost() {+ return 1;+ }+ };++ var scorer = new TimeLimitingBulkScorer(bulkScorer, new QueryTimeoutImpl(-1));+ scorer.score(dummyCollector(), new Bits.MatchAllBits(Integer.MAX_VALUE), 0, MAX_DOCS);+ }+private static QueryTimeout countingQueryTimeout(int timeallowed) {return new QueryTimeout() {@@ -77,4 +125,16 @@ public class TestTimeLimitingBulkScorer extends LuceneTestCase {}};}++ private static LeafCollector dummyCollector() {+ return new SimpleCollector() {+ @Override+ public ScoreMode scoreMode() {+ return ScoreMode.COMPLETE_NO_SCORES;+ }++ @Override+ public void collect(int doc) throws IOException {}+ };+ }}