rmuir commented on code in PR #11867: URL: https://github.com/apache/lucene/pull/11867#discussion_r1001217406
########## lucene/core/src/test/org/apache/lucene/document/TestManyKnnVectors.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.document; + +import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.index.VectorValues; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.tests.util.LuceneTestCase; +import org.apache.lucene.tests.util.LuceneTestCase.Monster; +import org.apache.lucene.tests.util.TestUtil; + +import java.io.IOException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Paths; + +import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS; + + +/** + * Tests a large dataset of kNN vectors to check for issues that only show up when + * segments are very large, like overflow. The dataset is based on the StackOverflow + * track from Elasticsearch's rally benchmarks: https://github.com/elastic/rally-tracks/tree/master/so_vector. + * + * Steps to run the test + * 1. Download the dataset: wget https://rally-tracks.elastic.co/so_vector/documents.bin + * 2. Move the dataset to the resources folder: mv documents.bin lucene/core/src/resources/ + * 3. Start the test: + * ./gradlew test --tests TestManyKnnVectors.testLargeSegment -Dtests.monster=true -Dtests.verbose=true \ + * -Dorg.gradle.jvmargs="-Xms2g -Xmx2g" --max-workers=1 + */ +@TimeoutSuite(millis = 10_800_000) // 3 hour timeout +@Monster("takes ~2 hours and needs 2GB heap") +public class TestManyKnnVectors extends LuceneTestCase { + public void testLargeSegment() throws Exception { + IndexWriterConfig iwc = new IndexWriterConfig(); + iwc.setCodec(TestUtil.getDefaultCodec()); // Make sure to use the default codec instead of a random one + iwc.setRAMBufferSizeMB(200); // Use a 200MB buffer to create larger initial segments + + String fieldName = "field"; + VectorSimilarityFunction similarityFunction = VectorSimilarityFunction.DOT_PRODUCT; + + URL documentsPath = getClass().getClassLoader().getResource("documents.bin"); + assertNotNull(documentsPath); + + try (FileChannel input = FileChannel.open(Paths.get(documentsPath.toURI())); + Directory dir = FSDirectory.open(createTempDir("ManyKnnVectors")); + IndexWriter iw = new IndexWriter(dir, iwc)) { + + // This data is enough to trigger the overflow bug in issue #11858, + // since 1_000_000 * 768 * 4 > Integer.MAX_VALUE + int numVectors = 1_000_000; + int dims = 768; + + VectorReader vectorReader = new VectorReader(input, dims); + for (int i = 0; i < numVectors; i++) { + float[] vector = vectorReader.next(); + Document doc = new Document(); + doc.add(new KnnVectorField(fieldName, vector, similarityFunction)); + iw.addDocument(doc); + if (VERBOSE && i % 10_000 == 0) { + System.out.println("Indexed " + i + " vectors out of " + numVectors); + } + } Review Comment: We can improve the output for this long-running test. I had to fill in the gaps with `jstack` otherwise: I would also consider changing the loop to be `for (int i = 1; i <= numVectors; i++)`. Then the print will say "Indexed 1000000 vectors out of 1000000 vectors" at the very end, so that you know indexing is complete. This does not happen today. Maybe also here before the `forceMerge`: ``` if (VERBOSE) { System.out.println("forceMerge()ing to one segment..."); } ``` -- 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: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org