Jackie-Jiang commented on code in PR #20299:
URL: https://github.com/apache/pinot/pull/20299#discussion_r4622789392


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/vector/lucene99/MmapHnswGraphBuilder.java:
##########
@@ -0,0 +1,404 @@
+/**
+ * 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.pinot.segment.local.segment.creator.impl.vector.lucene99;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.SplittableRandom;
+import javax.annotation.Nullable;
+import org.apache.lucene.codecs.KnnVectorsWriter.MergedVectorValues;
+import org.apache.lucene.codecs.hnsw.HnswGraphProvider;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.MergeState;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.hnsw.CloseableRandomVectorScorerSupplier;
+import org.apache.lucene.util.hnsw.HnswGraph;
+import org.apache.lucene.util.hnsw.HnswGraphBuilder;
+import org.apache.lucene.util.hnsw.IncrementalHnswGraphMerger;
+import org.apache.lucene.util.hnsw.NeighborArray;
+import org.apache.lucene.util.hnsw.NeighborQueue;
+import org.apache.lucene.util.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
+
+
+/// Serial Lucene 9.12 HNSW merge algorithm using mapped neighbor rows. Reuses 
Lucene's selection
+/// of a compatible source graph and mapping of its vector ordinals into the 
merged segment.
+final class MmapHnswGraphBuilder extends IncrementalHnswGraphMerger {
+  private static final int MIN_CACHED_VECTOR_DIMENSION = 256;
+
+  private final MmapHnswGraph _graph;
+  private final SplittableRandom _random = new 
SplittableRandom(HnswGraphBuilder.randSeed);
+  private final double _levelMultiplier;
+  private final FixedBitSet _visited;
+  private final NeighborQueue _frontier;
+  private final NeighborQueue _entryCandidates = new NeighborQueue(1, false);
+  private final NeighborQueue _beamCandidates;
+
+  static MmapHnswGraph build(Path scratchDir, FieldInfo fieldInfo, 
CloseableRandomVectorScorerSupplier scorerSupplier,
+      MergeState mergeState, int maxConn, int beamWidth)
+      throws IOException {
+    int count = scorerSupplier.totalVectorCount();
+    MmapHnswGraph graph = MmapHnswGraph.open(scratchDir, count, maxConn);

Review Comment:
   Nonblocking cleanup: let `MmapHnswVectorsWriter.mergeOneField()` open the 
graph in its existing try-with-resources block, and have the builder populate 
the supplied graph. The writer already owns the graph immediately after this 
call returns, so keeping ownership there would remove the manual 
failure-close/suppression block here and make the lifetime easier to follow.



##########
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/creator/HnswVectorIndexCreatorTest.java:
##########
@@ -69,6 +99,103 @@ public void tearDown()
     FileUtils.deleteDirectory(INDEX_DIR);
   }
 
+  @Test
+  public void testMmapWriterSelection()
+      throws IOException {
+    HnswVectorsFormat mmapFormat = new HnswVectorsFormat(16, 100, 1024, 
INDEX_DIR.toPath());
+    HnswVectorsFormat heapFormat = new HnswVectorsFormat(16, 100, 1024);
+    IOContext forcedMerge = new IOContext(new MergeInfo(4, 0, false, 1));
+    assertFalse(usesHeapWriter(mmapFormat, forcedMerge));
+    assertTrue(usesHeapWriter(heapFormat, forcedMerge));
+    assertTrue(usesHeapWriter(mmapFormat, IOContext.DEFAULT));
+    assertTrue(usesHeapWriter(mmapFormat, new IOContext(new MergeInfo(4, 0, 
false, -1))));
+  }
+
+  private static boolean usesHeapWriter(HnswVectorsFormat format, IOContext 
context)
+      throws IOException {
+    try (ByteBuffersDirectory directory = new ByteBuffersDirectory()) {
+      SegmentInfo segmentInfo = new SegmentInfo(directory, Version.LATEST, 
Version.LATEST, "_0", 4, false, false,
+          Codec.getDefault(), Map.of(), StringHelper.randomId(), Map.of(), 
null);
+      SegmentWriteState state = new SegmentWriteState(InfoStream.NO_OUTPUT, 
directory, segmentInfo, FieldInfos.EMPTY,
+          null, context);
+      try (KnnVectorsWriter writer = format.fieldsWriter(state)) {
+        return writer instanceof Lucene99HnswVectorsWriter;
+      }
+    }
+  }
+
+  @Test
+  public void testForcedMergeIndexCanBeRead()

Review Comment:
   Nonblocking cleanup: rename this to `testMmapForcedMergeMatchesHeap`, since 
it verifies graph and nearest-document parity, beyond just readability. Also 
consider changing the helper's inverted `optOut` parameter to `useMmap` and 
reversing its callers so the heap/mmap selection reads more directly.



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/vector/lucene99/MmapHnswGraphBuilder.java:
##########
@@ -0,0 +1,404 @@
+/**
+ * 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.pinot.segment.local.segment.creator.impl.vector.lucene99;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.SplittableRandom;
+import javax.annotation.Nullable;
+import org.apache.lucene.codecs.KnnVectorsWriter.MergedVectorValues;
+import org.apache.lucene.codecs.hnsw.HnswGraphProvider;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.MergeState;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.hnsw.CloseableRandomVectorScorerSupplier;
+import org.apache.lucene.util.hnsw.HnswGraph;
+import org.apache.lucene.util.hnsw.HnswGraphBuilder;
+import org.apache.lucene.util.hnsw.IncrementalHnswGraphMerger;
+import org.apache.lucene.util.hnsw.NeighborArray;
+import org.apache.lucene.util.hnsw.NeighborQueue;
+import org.apache.lucene.util.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
+
+
+/// Serial Lucene 9.12 HNSW merge algorithm using mapped neighbor rows. Reuses 
Lucene's selection
+/// of a compatible source graph and mapping of its vector ordinals into the 
merged segment.
+final class MmapHnswGraphBuilder extends IncrementalHnswGraphMerger {
+  private static final int MIN_CACHED_VECTOR_DIMENSION = 256;
+
+  private final MmapHnswGraph _graph;
+  private final SplittableRandom _random = new 
SplittableRandom(HnswGraphBuilder.randSeed);
+  private final double _levelMultiplier;
+  private final FixedBitSet _visited;
+  private final NeighborQueue _frontier;
+  private final NeighborQueue _entryCandidates = new NeighborQueue(1, false);
+  private final NeighborQueue _beamCandidates;
+
+  static MmapHnswGraph build(Path scratchDir, FieldInfo fieldInfo, 
CloseableRandomVectorScorerSupplier scorerSupplier,
+      MergeState mergeState, int maxConn, int beamWidth)
+      throws IOException {
+    int count = scorerSupplier.totalVectorCount();
+    MmapHnswGraph graph = MmapHnswGraph.open(scratchDir, count, maxConn);
+    try {
+      new MmapHnswGraphBuilder(graph, fieldInfo, scorerSupplier, maxConn, 
beamWidth).build(mergeState, count);
+      return graph;
+    } catch (IOException | RuntimeException | Error e) {
+      try {
+        graph.close();
+      } catch (IOException | RuntimeException closeFailure) {
+        e.addSuppressed(closeFailure);
+      }
+      throw e;
+    }
+  }
+
+  private MmapHnswGraphBuilder(MmapHnswGraph graph, FieldInfo fieldInfo,
+      CloseableRandomVectorScorerSupplier scorerSupplier, int maxConn, int 
beamWidth) {
+    super(fieldInfo, fieldInfo.getVectorDimension() >= 
MIN_CACHED_VECTOR_DIMENSION
+        ? new ScoreCachingSupplier(scorerSupplier) : scorerSupplier, maxConn, 
beamWidth);
+    _graph = graph;
+    _levelMultiplier = maxConn == 1 ? 1 : 1 / Math.log(maxConn);
+    _visited = new FixedBitSet(graph.maxNodeId() + 1);
+    _frontier = new NeighborQueue(beamWidth, true);
+    _beamCandidates = new NeighborQueue(beamWidth, false);
+  }
+
+  private void build(MergeState mergeState, int count)
+      throws IOException {
+    for (int i = 0; i < mergeState.liveDocs.length; i++) {
+      if (MergedVectorValues.hasVectorValues(mergeState.fieldInfos[i], 
fieldInfo.name)) {
+        addReader(mergeState.knnVectorsReaders[i], mergeState.docMaps[i], 
mergeState.liveDocs[i]);
+      }
+    }
+    if (initReader != null) {
+      DocIdSetIterator mergedValues = switch (fieldInfo.getVectorEncoding()) {
+        case BYTE -> MergedVectorValues.mergeByteVectorValues(fieldInfo, 
mergeState);
+        case FLOAT32 -> MergedVectorValues.mergeFloatVectorValues(fieldInfo, 
mergeState);
+      };
+      initialize(((HnswGraphProvider) initReader).getGraph(fieldInfo.name),
+          getNewOrdMapping(mergedValues, new FixedBitSet(count)));

Review Comment:
   Nonblocking cleanup: this `FixedBitSet` is populated by `getNewOrdMapping()` 
and then discarded. The existing `_visited` bitset could serve as scratch here 
because `searchLevel()` clears it before every use. A short comment explaining 
that the initialized-node output is unused would make the reuse clear.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to