This is an automated email from the ASF dual-hosted git repository.

xiangfu0 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 70e476d576b Reuse the committed HNSW searcher for unfiltered search on 
consuming segments (#20439)
70e476d576b is described below

commit 70e476d576b28a58d661a70e0c5dcc0b46236b2b
Author: Arunkumar Saravanan <[email protected]>
AuthorDate: Wed Dec 2 02:43:54 2026 +0530

    Reuse the committed HNSW searcher for unfiltered search on consuming 
segments (#20439)
    
    To reproduce the overhead, run repeated unfiltered vector searches on a 
consuming segment. Each query used to reopen the committed Lucene index.
    Reuse the searcher across queries and refresh it after each commit. If a 
refresh fails, the next unfiltered query retries even when ingestion is idle.
---
 .../realtime/impl/vector/MutableVectorIndex.java   | 55 ++++++++++++++++++---
 .../impl/vector/MutableVectorIndexTest.java        | 56 +++++++++++++++++++++-
 2 files changed, 102 insertions(+), 9 deletions(-)

diff --git 
a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndex.java
 
b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndex.java
index f85b263a383..8c6f3a55194 100644
--- 
a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndex.java
+++ 
b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndex.java
@@ -128,6 +128,12 @@ public class MutableVectorIndex
   // Near-real-time searcher over the writer, used by the filtered search path 
(upsert doc-ids snapshot
   // enforcement) so uncommitted rows are visible; reopened by _reopenThread, 
reused across queries
   private final SearcherManager _searcherManager;
+  /// Searcher over the last commit, used by the unfiltered search path. 
Refreshed by the indexing thread right after
+  /// each commit, so unfiltered queries see exactly the committed rows while 
reusing open segment readers. Opening a
+  /// `DirectoryReader` per query instead maps, header-checks and unmaps every 
index file on every search.
+  private final SearcherManager _committedSearcherManager;
+  /// A failed commit refresh must be retried even if no more rows arrive.
+  private volatile boolean _committedSearcherRefreshFailed;
   /// Performs every near-real-time reopen on one background thread. Query 
threads never flush the writer
   /// themselves: they publish the generation they need and block until this 
thread has reopened past it, so N
   /// concurrent queries share one reopen instead of forcing N.
@@ -199,6 +205,7 @@ public class MutableVectorIndex
     FSDirectory indexDirectory = null;
     IndexWriter indexWriter = null;
     SearcherManager searcherManager = null;
+    SearcherManager committedSearcherManager = null;
     Thread reopenThread = null;
     try {
       // segment generation is always in V1 and later we convert (as part of 
post creation processing)
@@ -213,13 +220,14 @@ public class MutableVectorIndex
           
VectorIndexUtils.getIndexWriterConfig(vectorIndexConfig).setOpenMode(IndexWriterConfig.OpenMode.CREATE));
       indexWriter.commit();
       searcherManager = new SearcherManager(indexWriter, false, false, null);
+      committedSearcherManager = new SearcherManager(indexDirectory, null);
       reopenThread = new Thread(this::reopenLoop, "vector-nrt-reopen-" + 
segmentName + "-" + vectorColumn);
       reopenThread.setDaemon(true);
       _lastCommitTime = System.currentTimeMillis();
     } catch (Exception e) {
       // IndexWriter does not close the Directory passed to it, so both need 
to be closed.
       try {
-        IOUtils.close(searcherManager, indexWriter, indexDirectory);
+        IOUtils.close(committedSearcherManager, searcherManager, indexWriter, 
indexDirectory);
       } catch (Exception closeEx) {
         e.addSuppressed(closeEx);
       }
@@ -230,6 +238,7 @@ public class MutableVectorIndex
     _indexDirectory = indexDirectory;
     _indexWriter = indexWriter;
     _searcherManager = searcherManager;
+    _committedSearcherManager = committedSearcherManager;
     _reopenThread = reopenThread;
     // Started last: the loop reads final fields assigned above, so publishing 
`this` to another thread any
     // earlier would race construction. Guarded because this is where 
OutOfMemoryError: unable to create native
@@ -238,7 +247,7 @@ public class MutableVectorIndex
     try {
       _reopenThread.start();
     } catch (Throwable t) {
-      IOUtils.closeWhileHandlingException(_searcherManager, _indexWriter, 
_indexDirectory);
+      IOUtils.closeWhileHandlingException(_committedSearcherManager, 
_searcherManager, _indexWriter, _indexDirectory);
       deleteIndexDir();
       throw t;
     }
@@ -269,6 +278,12 @@ public class MutableVectorIndex
         _indexWriter.commit();
         _lastCommitTime = System.currentTimeMillis();
         LOGGER.debug("Committed index for column: {}, segment: {}", 
_vectorColumn, _segmentName);
+        try {
+          refreshCommittedSearcher(true);
+        } catch (IOException e) {
+          LOGGER.warn("Failed to refresh committed HNSW searcher for segment: 
{}, column: {}", _segmentName,
+              _vectorColumn, e);
+        }
       }
     } catch (Exception e) {
       throw new RuntimeException(
@@ -276,6 +291,27 @@ public class MutableVectorIndex
     }
   }
 
+  /// Serialize the refresh and its outcome so a query retry cannot clear a 
later commit's failure.
+  private synchronized void refreshCommittedSearcher(boolean newCommit)
+      throws IOException {
+    if (!newCommit && !_committedSearcherRefreshFailed) {
+      return; // Another query already recovered the searcher.
+    }
+    try {
+      doCommittedReopen();
+      _committedSearcherRefreshFailed = false;
+    } catch (IOException e) {
+      _committedSearcherRefreshFailed = true;
+      throw e;
+    }
+  }
+
+  @VisibleForTesting
+  void doCommittedReopen()
+      throws IOException {
+    _committedSearcherManager.maybeRefreshBlocking();
+  }
+
   @Override
   public MutableRoaringBitmap getDocIds(float[] vector, int topK) {
     return submitSearch(vector, topK, null);
@@ -412,9 +448,14 @@ public class MutableVectorIndex
       }
     }
     // The unfiltered path keeps the cheaper last-committed view (bounded by 
commitIntervalMs / commitDocs)
-    try (DirectoryReader directoryReader = 
DirectoryReader.open(_indexDirectory)) {
-      return search(new IndexSearcher(directoryReader), vector, topK, 
efSearch, useRelativeDistance,
-          useBoundedQueue, null);
+    if (_committedSearcherRefreshFailed) {
+      refreshCommittedSearcher(false);
+    }
+    IndexSearcher indexSearcher = _committedSearcherManager.acquire();
+    try {
+      return search(indexSearcher, vector, topK, efSearch, 
useRelativeDistance, useBoundedQueue, null);
+    } finally {
+      _committedSearcherManager.release(indexSearcher);
     }
   }
 
@@ -705,12 +746,12 @@ public class MutableVectorIndex
     try {
       _indexWriter.commit();
       // IndexWriter does not close the Directory passed to it, so both need 
to be closed.
-      IOUtils.close(_searcherManager, _indexWriter, _indexDirectory);
+      IOUtils.close(_committedSearcherManager, _searcherManager, _indexWriter, 
_indexDirectory);
     } catch (Exception e) {
       // commit() can also fail unchecked (for example, after a tragic writer 
event). Close every remaining resource
       // so the SearcherManager cannot pin an NRT reader and its file handles 
for the life of the process. All close()
       // implementations are idempotent, so this is a no-op for resources that 
were already closed above.
-      IOUtils.closeWhileHandlingException(_searcherManager, _indexWriter, 
_indexDirectory);
+      IOUtils.closeWhileHandlingException(_committedSearcherManager, 
_searcherManager, _indexWriter, _indexDirectory);
       throw e instanceof RuntimeException ? (RuntimeException) e : new 
RuntimeException(e);
     } finally {
       deleteIndexDir();
diff --git 
a/pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndexTest.java
 
b/pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndexTest.java
index 23a6c59cbfc..1f0fdaa055a 100644
--- 
a/pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndexTest.java
+++ 
b/pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/vector/MutableVectorIndexTest.java
@@ -33,6 +33,7 @@ import java.util.concurrent.Phaser;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 import 
org.apache.pinot.segment.local.realtime.impl.invertedindex.RealtimeLuceneTextIndexSearcherPool;
 import org.apache.pinot.segment.spi.index.creator.VectorIndexConfig;
@@ -43,6 +44,9 @@ import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertThrows;
+
 
 public class MutableVectorIndexTest {
   private static final String COLUMN_NAME = "embedding";
@@ -115,6 +119,7 @@ public class MutableVectorIndexTest {
   private static MutableVectorIndex create2DIndex(long commitDocs, int 
docIdOffset) {
     Map<String, String> properties = new HashMap<>();
     properties.put("commitDocs", String.valueOf(commitDocs));
+    properties.put("commitIntervalMs", 
String.valueOf(TimeUnit.DAYS.toMillis(1)));
     properties.put("vectorIndexType", "HNSW");
     properties.put("vectorDimension", "2");
     VectorIndexConfig config = new VectorIndexConfig(false, "HNSW", 2, 1,
@@ -237,10 +242,57 @@ public class MutableVectorIndexTest {
     }
   }
 
+  @Test
+  public void testUnfilteredSearchSeesEachCommitButNotUncommittedDocs() {
+    // commitDocs=4 commits on the 4th and 8th adds
+    MutableVectorIndex index = create2DIndex(4, 0);
+    try {
+      float[] query = {1.0F, 0.0F};
+      Assert.assertEquals(index.getDocIds(query, 10).toArray(), new int[]{0, 
1, 2, 3});
+
+      // The 5th and 6th adds are not committed, so the unfiltered committed 
view must not see them yet
+      addVector(index, new float[]{0.5F, 0.5F}, 4);
+      addVector(index, new float[]{-1.0F, 0.0F}, 5);
+      Assert.assertEquals(index.getDocIds(query, 10).toArray(), new int[]{0, 
1, 2, 3});
+
+      // The 8th add commits, and the reused searcher must be refreshed to 
that commit
+      addVector(index, new float[]{0.5F, -0.5F}, 6);
+      addVector(index, new float[]{-0.5F, 0.5F}, 7);
+      Assert.assertEquals(index.getDocIds(query, 10).toArray(), new int[]{0, 
1, 2, 3, 4, 5, 6, 7});
+    } finally {
+      index.close();
+    }
+  }
+
+  @Test
+  public void testUnfilteredSearchRetriesFailedCommittedReopen() {
+    AtomicInteger reopenAttempts = new AtomicInteger();
+    VectorIndexConfig config = new VectorIndexConfig(false, "HNSW", 2, 1,
+        VectorIndexConfig.VectorDistanceFunction.EUCLIDEAN, 
Map.of("commitDocs", "1"));
+    try (MutableVectorIndex index =
+        new MutableVectorIndex("mutableVectorIndexCommitRetryTest_" + 
System.nanoTime(), COLUMN_NAME, config) {
+          @Override
+          void doCommittedReopen()
+              throws IOException {
+            if (reopenAttempts.incrementAndGet() <= 2) {
+              throw new IOException("injected committed reopen failure");
+            }
+            super.doCommittedReopen();
+          }
+        }) {
+      float[] vector = {1.0F, 0.0F};
+      addVector(index, vector, 10);
+      assertEquals(reopenAttempts.get(), 1);
+      assertThrows(RuntimeException.class, () -> index.getDocIds(vector, 1));
+      assertEquals(index.getDocIds(vector, 1), bitmapOf(10));
+      assertEquals(index.getDocIds(vector, 1), bitmapOf(10));
+      assertEquals(reopenAttempts.get(), 3, "Queries should reuse the 
recovered searcher until the next commit");
+    }
+  }
+
   @Test
   public void testFilteredSearchSeesUncommittedDocs() {
-    // commitDocs is far larger than the number of added docs and 
commitIntervalMs defaults to 10s, so no
-    // commit has happened: NRT visibility is what makes the filtered search 
see the rows at all
+    // No commit has occurred, so only the NRT reader can see the rows.
     MutableVectorIndex index = create2DIndex(1_000_000, 0);
     try {
       ImmutableRoaringBitmap filtered =


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to