Skip to content

Apache Lucene open-source search software

Support adaptive refresh in Searcher Managers. (#14443)

Vigya Sharmaf3796ecf22b6
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index fe5180769ce..e222d4ef55b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -90,6 +90,8 @@ Improvements
* GITHUB#14602: Refactor the expressions compiler to use official ClassData BSM with indexed lookup
(Uwe Schindler)
+* GITHUB#14443: Support adaptive refresh in Searcher Managers (Vigya Sharma)
+
Optimizations
---------------------
* GITHUB#14418: Quick exit on filter query matching no docs when rewriting knn query. (Pan Guixin)
diff --git a/lucene/core/src/java/org/apache/lucene/search/RefreshCommitSupplier.java b/lucene/core/src/java/org/apache/lucene/search/RefreshCommitSupplier.java
new file mode 100644
index 00000000000..9c95a620d52
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/search/RefreshCommitSupplier.java
@@ -0,0 +1,39 @@
+/*
+ * 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.DirectoryReader;
+import org.apache.lucene.index.IndexCommit;
+
+/**
+ * Expert: Interface to supply commit for searcher refresh.
+ *
+ * @lucene.experimental
+ */
+public interface RefreshCommitSupplier {
+
+ /**
+ * Expert: Returns the index commit that searcher should refresh on. A null return value (default)
+ * indicates reader should refresh on the latest commit.
+ *
+ * @param reader DirectoryReader to refresh
+ */
+ default IndexCommit getSearcherRefreshCommit(DirectoryReader reader) throws IOException {
+ return null;
+ }
+}
diff --git a/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java b/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java
index 072c13d9274..2d3c9f84d2d 100644
--- a/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java
+++ b/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java
@@ -18,6 +18,7 @@ package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexCommit;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
@@ -52,6 +53,7 @@ import org.apache.lucene.store.Directory;
public final class SearcherManager extends ReferenceManager<IndexSearcher> {
private final SearcherFactory searcherFactory;
+ private RefreshCommitSupplier refreshCommitSupplier = new RefreshCommitSupplier() {};
/**
* Creates and returns a new SearcherManager from the given {@link IndexWriter}.
@@ -88,6 +90,34 @@ public final class SearcherManager extends ReferenceManager<IndexSearcher> {
boolean writeAllDeletes,
SearcherFactory searcherFactory)
throws IOException {
+ this(writer, applyAllDeletes, writeAllDeletes, searcherFactory, null);
+ }
+
+ /**
+ * Expert: creates and returns a new SearcherManager from the given {@link IndexWriter},
+ * controlling whether past deletions should be applied.
+ *
+ * @param writer the IndexWriter to open the IndexReader from.
+ * @param applyAllDeletes If <code>true</code>, all buffered deletes will be applied (made
+ * visible) in the {@link IndexSearcher} / {@link DirectoryReader}. If <code>false</code>, the
+ * deletes may or may not be applied, but remain buffered (in IndexWriter) so that they will
+ * be applied in the future. Applying deletes can be costly, so if your app can tolerate
+ * deleted documents being returned you might gain some performance by passing <code>false
+ * </code>. See {@link DirectoryReader#openIfChanged(DirectoryReader, IndexWriter, boolean)}.
+ * @param writeAllDeletes If <code>true</code>, new deletes will be forcefully written to index
+ * files.
+ * @param searcherFactory An optional {@link SearcherFactory}. Pass <code>null</code> if you don't
+ * require the searcher to be warmed before going live or other custom behavior.
+ * @param refreshCommitSupplier supplier for providing the commit to refresh on
+ * @throws IOException if there is a low-level I/O error
+ */
+ public SearcherManager(
+ IndexWriter writer,
+ boolean applyAllDeletes,
+ boolean writeAllDeletes,
+ SearcherFactory searcherFactory,
+ RefreshCommitSupplier refreshCommitSupplier)
+ throws IOException {
if (searcherFactory == null) {
searcherFactory = new SearcherFactory();
}
@@ -95,6 +125,9 @@ public final class SearcherManager extends ReferenceManager<IndexSearcher> {
current =
getSearcher(
searcherFactory, DirectoryReader.open(writer, applyAllDeletes, writeAllDeletes), null);
+ if (refreshCommitSupplier != null) {
+ this.refreshCommitSupplier = refreshCommitSupplier;
+ }
}
/**
@@ -124,11 +157,32 @@ public final class SearcherManager extends ReferenceManager<IndexSearcher> {
*/
public SearcherManager(DirectoryReader reader, SearcherFactory searcherFactory)
throws IOException {
+ this(reader, searcherFactory, null);
+ }
+
+ /**
+ * Creates and returns a new SearcherManager from an existing {@link DirectoryReader}. Note that
+ * this steals the incoming reference.
+ *
+ * @param reader the DirectoryReader.
+ * @param searcherFactory An optional {@link SearcherFactory}. Pass <code>null</code> if you don't
+ * require the searcher to be warmed before going live or other custom behavior.
+ * @param refreshCommitSupplier supplier for providing the commit to refresh on
+ * @throws IOException if there is a low-level I/O error
+ */
+ public SearcherManager(
+ DirectoryReader reader,
+ SearcherFactory searcherFactory,
+ RefreshCommitSupplier refreshCommitSupplier)
+ throws IOException {
if (searcherFactory == null) {
searcherFactory = new SearcherFactory();
}
this.searcherFactory = searcherFactory;
this.current = getSearcher(searcherFactory, reader, null);
+ if (refreshCommitSupplier != null) {
+ this.refreshCommitSupplier = refreshCommitSupplier;
+ }
}
@Override
@@ -141,7 +195,9 @@ public final class SearcherManager extends ReferenceManager<IndexSearcher> {
final IndexReader r = referenceToRefresh.getIndexReader();
assert r instanceof DirectoryReader
: "searcher's IndexReader should be a DirectoryReader, but got " + r;
- final IndexReader newReader = DirectoryReader.openIfChanged((DirectoryReader) r);
+ DirectoryReader dr = (DirectoryReader) r;
+ IndexCommit refreshCommit = refreshCommitSupplier.getSearcherRefreshCommit(dr);
+ final IndexReader newReader = DirectoryReader.openIfChanged(dr, refreshCommit);
if (newReader == null) {
return null;
} else {
@@ -159,13 +215,21 @@ public final class SearcherManager extends ReferenceManager<IndexSearcher> {
return reference.getIndexReader().getRefCount();
}
+ /** Return index commit generation for current searcher. pkg-private for testing */
+ long getSearcherCommitGeneration() throws IOException {
+ IndexSearcher s = acquire();
+ long gen = ((DirectoryReader) s.getIndexReader()).getIndexCommit().getGeneration();
+ release(s);
+ return gen;
+ }
+
/**
* Returns <code>true</code> if no changes have occurred since this searcher ie. reader was
- * opened, otherwise <code>false</code>.
+ * opened, otherwise <code>false</code>. pkg-private for testing
*
* @see DirectoryReader#isCurrent()
*/
- public boolean isSearcherCurrent() throws IOException {
+ boolean isSearcherCurrent() throws IOException {
final IndexSearcher searcher = acquire();
try {
final IndexReader r = searcher.getIndexReader();
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java b/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java
index e44855afe87..29f49bd239f 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java
@@ -32,11 +32,13 @@ import org.apache.lucene.index.ConcurrentMergeScheduler;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.FilterDirectoryReader;
import org.apache.lucene.index.FilterLeafReader;
+import org.apache.lucene.index.IndexCommit;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoDeletionPolicy;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;
@@ -747,4 +749,64 @@ public class TestSearcherManager extends ThreadedIndexingAndSearchingTestCase {
writerRef.get().close();
dir.close();
}
+
+ /** Returns the first commit with generation higher than current reader commit */
+ public static class NextCommitSelector implements RefreshCommitSupplier {
+ @Override
+ public IndexCommit getSearcherRefreshCommit(DirectoryReader reader) throws IOException {
+ List<IndexCommit> commits = DirectoryReader.listCommits(reader.directory());
+ IndexCommit current = reader.getIndexCommit();
+ for (int i = 0; i < commits.size(); i++) {
+ IndexCommit commit = commits.get(i);
+ if (commit.getGeneration() > current.getGeneration()) {
+ return commit;
+ }
+ }
+ // we're already on latest commit
+ return null;
+ }
+ }
+
+ public void testStepWiseCommitRefresh() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w =
+ new IndexWriter(
+ dir,
+ newIndexWriterConfig(new MockAnalyzer(random()))
+ .setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE));
+ int docId = 0;
+ // create initial commit
+ for (int i = 0; i < 20; i++) {
+ Document doc = new Document();
+ doc.add(newStringField("docId", "doc-" + docId++, Field.Store.YES));
+ w.addDocument(doc);
+ }
+ w.commit();
+ SearcherManager sm =
+ new SearcherManager(DirectoryReader.open(dir), null, new NextCommitSelector());
+ final int numCommits = 5;
+ for (int i = 0; i < numCommits; i++) {
+ for (int j = 0; j < 20; j++) {
+ Document doc = new Document();
+ doc.add(newStringField("docId", "doc-" + docId++, Field.Store.YES));
+ w.addDocument(doc);
+ }
+ w.commit();
+ }
+
+ // maybeRefresh only refreshes on the next incremental commit
+ // so it takes us numCommits to get to latest
+ int stepsToCurrent = 0;
+ while (sm.isSearcherCurrent() == false) {
+ long oldGen = sm.getSearcherCommitGeneration();
+ sm.maybeRefreshBlocking();
+ long newGen = sm.getSearcherCommitGeneration();
+ assertTrue(newGen == oldGen + 1);
+ stepsToCurrent++;
+ }
+ assertEquals(numCommits, stepsToCurrent);
+ sm.close();
+ w.close();
+ dir.close();
+ }
}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/SearcherTaxonomyManager.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/SearcherTaxonomyManager.java
index 5b768f48a05..64f906a8f03 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/SearcherTaxonomyManager.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/SearcherTaxonomyManager.java
@@ -20,10 +20,12 @@ import java.io.IOException;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexCommit;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ReferenceManager;
+import org.apache.lucene.search.RefreshCommitSupplier;
import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.store.Directory;
@@ -50,6 +52,7 @@ public class SearcherTaxonomyManager
private final SearcherFactory searcherFactory;
private final long taxoEpoch;
private final DirectoryTaxonomyWriter taxoWriter;
+ private RefreshCommitSupplier refreshCommitSupplier = new RefreshCommitSupplier() {};
/** Creates near-real-time searcher and taxonomy reader from the corresponding writers. */
public SearcherTaxonomyManager(
@@ -68,6 +71,20 @@ public class SearcherTaxonomyManager
SearcherFactory searcherFactory,
DirectoryTaxonomyWriter taxoWriter)
throws IOException {
+ this(writer, applyAllDeletes, searcherFactory, taxoWriter, null);
+ }
+
+ /**
+ * Expert: creates near-real-time searcher and taxonomy reader from the corresponding writers,
+ * controlling whether deletes should be applied.
+ */
+ public SearcherTaxonomyManager(
+ IndexWriter writer,
+ boolean applyAllDeletes,
+ SearcherFactory searcherFactory,
+ DirectoryTaxonomyWriter taxoWriter,
+ RefreshCommitSupplier refreshCommitSupplier)
+ throws IOException {
if (searcherFactory == null) {
searcherFactory = new SearcherFactory();
}
@@ -80,6 +97,9 @@ public class SearcherTaxonomyManager
searcherFactory, DirectoryReader.open(writer, applyAllDeletes, false), null),
taxoReader);
this.taxoEpoch = taxoWriter.getTaxonomyEpoch();
+ if (refreshCommitSupplier != null) {
+ this.refreshCommitSupplier = refreshCommitSupplier;
+ }
}
/**
@@ -111,6 +131,19 @@ public class SearcherTaxonomyManager
public SearcherTaxonomyManager(
IndexReader reader, DirectoryTaxonomyReader taxoReader, SearcherFactory searcherFactory)
throws IOException {
+ this(reader, taxoReader, searcherFactory, null);
+ }
+
+ /**
+ * Creates this from already opened {@link IndexReader} and {@link DirectoryTaxonomyReader}
+ * instances. Note that the incoming readers will be closed when you call {@link #close}.
+ */
+ public SearcherTaxonomyManager(
+ IndexReader reader,
+ DirectoryTaxonomyReader taxoReader,
+ SearcherFactory searcherFactory,
+ RefreshCommitSupplier refreshCommitSupplier)
+ throws IOException {
if (searcherFactory == null) {
searcherFactory = new SearcherFactory();
}
@@ -120,6 +153,9 @@ public class SearcherTaxonomyManager
SearcherManager.getSearcher(searcherFactory, reader, null), taxoReader);
this.taxoWriter = null;
taxoEpoch = -1;
+ if (refreshCommitSupplier != null) {
+ this.refreshCommitSupplier = refreshCommitSupplier;
+ }
}
@Override
@@ -154,11 +190,16 @@ public class SearcherTaxonomyManager
// new reader that references ords not yet known to the
// taxonomy reader:
final IndexReader r = ref.searcher.getIndexReader();
- final IndexReader newReader = DirectoryReader.openIfChanged((DirectoryReader) r);
+ DirectoryReader dr = (DirectoryReader) r;
+ IndexCommit refreshCommit = refreshCommitSupplier.getSearcherRefreshCommit(dr);
+ IndexReader newReader = DirectoryReader.openIfChanged(dr, refreshCommit);
if (newReader == null) {
return null;
} else {
- DirectoryTaxonomyReader tr;
+ DirectoryTaxonomyReader tr = null;
+ // taxonomy should always be ahead of searchers. Otherwise, searchers
+ // might reference ordinals that taxonomy doesn't know of.
+ // To ensure this, we always refresh taxonomy on the latest commit.
try {
tr = TaxonomyReader.openIfChanged(ref.taxonomyReader);
} catch (Throwable t1) {
@@ -183,6 +224,47 @@ public class SearcherTaxonomyManager
}
}
+ /** Return index commit generation for current searcher. pkg-private for testing */
+ long getSearcherCommitGeneration() throws IOException {
+ SearcherAndTaxonomy sat = acquire();
+ long gen = ((DirectoryReader) sat.searcher.getIndexReader()).getIndexCommit().getGeneration();
+ release(sat);
+ return gen;
+ }
+
+ /**
+ * Returns <code>true</code> if no new changes have occurred since the current searcher (i.e.
+ * reader) was opened, <code>false</code> otherwise. pkg-private for testing
+ *
+ * @see DirectoryReader#isCurrent()
+ */
+ boolean isSearcherCurrent() throws IOException {
+ final SearcherAndTaxonomy sat = acquire();
+ try {
+ final IndexReader r = sat.searcher.getIndexReader();
+ assert r instanceof DirectoryReader
+ : "searcher's IndexReader should be a DirectoryReader, but got " + r;
+ return ((DirectoryReader) r).isCurrent();
+ } finally {
+ release(sat);
+ }
+ }
+
+ /**
+ * Returns <code>true</code> if no new changes have occurred since the current taxonomy reader was
+ * opened, <code>false</code> otherwise. pkg-private for testing
+ *
+ * @see DirectoryReader#isCurrent()
+ */
+ boolean isTaxonomyCurrent() throws IOException {
+ final SearcherAndTaxonomy sat = acquire();
+ try {
+ return sat.taxonomyReader.getInternalIndexReader().isCurrent();
+ } finally {
+ release(sat);
+ }
+ }
+
@Override
protected int getRefCount(SearcherAndTaxonomy reference) {
return reference.searcher.getIndexReader().getRefCount();
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestSearcherTaxonomyManager.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestSearcherTaxonomyManager.java
index 3f319acdb78..327ed8d727e 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestSearcherTaxonomyManager.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestSearcherTaxonomyManager.java
@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
import org.apache.lucene.facet.FacetField;
import org.apache.lucene.facet.FacetResult;
import org.apache.lucene.facet.FacetTestCase;
@@ -31,14 +32,19 @@ import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.facet.FacetsCollectorManager;
import org.apache.lucene.facet.FacetsConfig;
import org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager.SearcherAndTaxonomy;
+import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexCommit;
import org.apache.lucene.index.IndexNotFoundException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.NoDeletionPolicy;
import org.apache.lucene.index.SegmentInfos;
import org.apache.lucene.index.TieredMergePolicy;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ReferenceManager;
+import org.apache.lucene.search.RefreshCommitSupplier;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.util.LuceneTestCase;
@@ -356,4 +362,80 @@ public class TestSearcherTaxonomyManager extends FacetTestCase {
expectThrows(IndexNotFoundException.class, mgr::maybeRefreshBlocking);
IOUtils.close(w, tw, mgr, indexDir, taxoDir);
}
+
+ /** Returns the first commit with generation higher than current reader commit */
+ public static class NextCommitSelector implements RefreshCommitSupplier {
+ @Override
+ public IndexCommit getSearcherRefreshCommit(DirectoryReader reader) throws IOException {
+ List<IndexCommit> commits = DirectoryReader.listCommits(reader.directory());
+ IndexCommit current = reader.getIndexCommit();
+ for (int i = 0; i < commits.size(); i++) {
+ IndexCommit commit = commits.get(i);
+ if (commit.getGeneration() > current.getGeneration()) {
+ return commit;
+ }
+ }
+ // we're already on latest commit
+ return null;
+ }
+ }
+
+ public void testStepWiseCommitRefresh() throws Exception {
+ Directory dir = newDirectory();
+ Directory taxoDir = newDirectory();
+ IndexWriter w =
+ new IndexWriter(
+ dir,
+ newIndexWriterConfig(new MockAnalyzer(random()))
+ .setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE));
+ DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
+ int docId = 0;
+ // create initial commit
+ for (int i = 0; i < 20; i++) {
+ Document doc = new Document();
+ doc.add(newStringField("docId", "doc-" + docId++, Field.Store.YES));
+ w.addDocument(doc);
+ }
+ w.commit();
+ tw.commit();
+
+ int colorIndex = 0;
+ final String[] colors = new String[] {"red", "green", "blue", "yellow"};
+ FacetsConfig config = new FacetsConfig();
+ DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
+ SearcherTaxonomyManager sat =
+ new SearcherTaxonomyManager(
+ DirectoryReader.open(dir), taxoReader, null, new NextCommitSelector());
+
+ final int numCommits = 5;
+ for (int i = 0; i < numCommits; i++) {
+ for (int j = 0; j < 20; j++) {
+ Document doc = new Document();
+ doc.add(newStringField("docId", "doc-" + docId++, Field.Store.YES));
+ doc.add(new FacetField("Color", colors[colorIndex++ % colors.length]));
+ doc = config.build(tw, doc);
+ w.addDocument(doc);
+ }
+ w.commit();
+ tw.commit();
+ }
+
+ // maybeRefresh only refreshes on the next incremental commit
+ // so it takes us numCommits to get to latest
+ int stepsToCurrent = 0;
+ while (sat.isSearcherCurrent() == false) {
+ long oldGen = sat.getSearcherCommitGeneration();
+ sat.maybeRefreshBlocking();
+ long newGen = sat.getSearcherCommitGeneration();
+ assertEquals(newGen, oldGen + 1);
+ stepsToCurrent++;
+ assertTrue(sat.isTaxonomyCurrent());
+ }
+ assertEquals(numCommits, stepsToCurrent);
+ sat.close();
+ w.close();
+ tw.close();
+ dir.close();
+ taxoDir.close();
+ }
}