|
34 | 34 | import java.io.IOException;
|
35 | 35 | import java.util.List;
|
36 | 36 | import java.util.Locale;
|
| 37 | +import java.util.concurrent.atomic.AtomicBoolean; |
37 | 38 |
|
38 | 39 | import static java.lang.String.format;
|
39 | 40 | import static org.elasticsearch.index.codec.vectors.IVFVectorsFormat.MAX_VECTORS_PER_CLUSTER;
|
@@ -128,4 +129,49 @@ public void testSimpleOffHeapSize() throws IOException {
|
128 | 129 | }
|
129 | 130 | }
|
130 | 131 | }
|
| 132 | + |
| 133 | + // this is a modified version of lucene's TestSearchWithThreads test case |
| 134 | + public void testWithThreads() throws Exception { |
| 135 | + final int numThreads = random().nextInt(2, 5); |
| 136 | + final int numSearches = atLeast(100); |
| 137 | + final int numDocs = atLeast(1000); |
| 138 | + final int dimensions = random().nextInt(12, 500); |
| 139 | + try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) { |
| 140 | + for (int docCount = 0; docCount < numDocs; docCount++) { |
| 141 | + final Document doc = new Document(); |
| 142 | + doc.add(new KnnFloatVectorField("f", randomVector(dimensions), VectorSimilarityFunction.EUCLIDEAN)); |
| 143 | + w.addDocument(doc); |
| 144 | + } |
| 145 | + w.forceMerge(1); |
| 146 | + try (IndexReader reader = DirectoryReader.open(w)) { |
| 147 | + final AtomicBoolean failed = new AtomicBoolean(); |
| 148 | + Thread[] threads = new Thread[numThreads]; |
| 149 | + for (int threadID = 0; threadID < numThreads; threadID++) { |
| 150 | + threads[threadID] = new Thread(() -> { |
| 151 | + try { |
| 152 | + long totSearch = 0; |
| 153 | + for (; totSearch < numSearches && failed.get() == false; totSearch++) { |
| 154 | + float[] vector = randomVector(dimensions); |
| 155 | + LeafReader leafReader = getOnlyLeafReader(reader); |
| 156 | + leafReader.searchNearestVectors("f", vector, 10, leafReader.getLiveDocs(), Integer.MAX_VALUE); |
| 157 | + } |
| 158 | + assertTrue(totSearch > 0); |
| 159 | + } catch (Exception exc) { |
| 160 | + failed.set(true); |
| 161 | + throw new RuntimeException(exc); |
| 162 | + } |
| 163 | + }); |
| 164 | + threads[threadID].setDaemon(true); |
| 165 | + } |
| 166 | + |
| 167 | + for (Thread t : threads) { |
| 168 | + t.start(); |
| 169 | + } |
| 170 | + |
| 171 | + for (Thread t : threads) { |
| 172 | + t.join(); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
131 | 177 | }
|
0 commit comments