Skip to content

Clone IndexInput when creating MemorySegmentPostingsVisitor #129690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ NeighborQueue scorePostingLists(FieldInfo fieldInfo, KnnCollector knnCollector,
PostingVisitor getPostingVisitor(FieldInfo fieldInfo, IndexInput indexInput, float[] target, IntPredicate needsScoring)
throws IOException {
FieldEntry entry = fields.get(fieldInfo.number);
return new MemorySegmentPostingsVisitor(target, indexInput, entry, fieldInfo, needsScoring);
return new MemorySegmentPostingsVisitor(target, indexInput.clone(), entry, fieldInfo, needsScoring);
}

// TODO can we do this in off-heap blocks?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.lang.String.format;
import static org.elasticsearch.index.codec.vectors.IVFVectorsFormat.MAX_VECTORS_PER_CLUSTER;
Expand Down Expand Up @@ -128,4 +129,49 @@ public void testSimpleOffHeapSize() throws IOException {
}
}
}

// this is a modified version of lucene's TestSearchWithThreads test case
public void testWithThreads() throws Exception {
final int numThreads = random().nextInt(2, 5);
final int numSearches = atLeast(100);
final int numDocs = atLeast(1000);
final int dimensions = random().nextInt(12, 500);
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {
for (int docCount = 0; docCount < numDocs; docCount++) {
final Document doc = new Document();
doc.add(new KnnFloatVectorField("f", randomVector(dimensions), VectorSimilarityFunction.EUCLIDEAN));
w.addDocument(doc);
}
w.forceMerge(1);
try (IndexReader reader = DirectoryReader.open(w)) {
final AtomicBoolean failed = new AtomicBoolean();
Thread[] threads = new Thread[numThreads];
for (int threadID = 0; threadID < numThreads; threadID++) {
threads[threadID] = new Thread(() -> {
try {
long totSearch = 0;
for (; totSearch < numSearches && failed.get() == false; totSearch++) {
float[] vector = randomVector(dimensions);
LeafReader leafReader = getOnlyLeafReader(reader);
leafReader.searchNearestVectors("f", vector, 10, leafReader.getLiveDocs(), Integer.MAX_VALUE);
}
assertTrue(totSearch > 0);
} catch (Exception exc) {
failed.set(true);
throw new RuntimeException(exc);
}
});
threads[threadID].setDaemon(true);
}

for (Thread t : threads) {
t.start();
}

for (Thread t : threads) {
t.join();
}
}
}
}
}