Skip to content

Commit 9bc24c3

Browse files
committed
iter
1 parent a5864db commit 9bc24c3

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

qa/vector/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ tasks.named("dependencyLicenses").configure {
1515
mapping from: /lucene-.*/, to: 'lucene'
1616
}
1717

18+
tasks.named('forbiddenApisMain').configure {
19+
enabled = false
20+
}
21+
1822
dependencies {
1923
api "org.apache.lucene:lucene-core:${versions.lucene}"
2024
api "org.apache.lucene:lucene-queries:${versions.lucene}"

qa/vector/src/main/java/org/elasticsearch/test/knn/CmdLineArgs.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.VectorEncoding;
1313
import org.apache.lucene.index.VectorSimilarityFunction;
1414
import org.elasticsearch.common.Strings;
15+
import org.elasticsearch.core.PathUtils;
1516
import org.elasticsearch.xcontent.ObjectParser;
1617
import org.elasticsearch.xcontent.ParseField;
1718
import org.elasticsearch.xcontent.ToXContentObject;
@@ -158,12 +159,12 @@ static class Builder {
158159
private int dimensions;
159160

160161
public Builder setDocVectors(String docVectors) {
161-
this.docVectors = Path.of(docVectors);
162+
this.docVectors = PathUtils.get(docVectors);
162163
return this;
163164
}
164165

165166
public Builder setQueryVectors(String queryVectors) {
166-
this.queryVectors = Path.of(queryVectors);
167+
this.queryVectors = PathUtils.get(queryVectors);
167168
return this;
168169
}
169170

qa/vector/src/main/java/org/elasticsearch/test/knn/KnnIndexTester.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
1616
import org.elasticsearch.common.Strings;
1717
import org.elasticsearch.common.logging.LogConfigurator;
18+
import org.elasticsearch.core.PathUtils;
1819
import org.elasticsearch.index.codec.vectors.ES813Int8FlatVectorFormat;
1920
import org.elasticsearch.index.codec.vectors.ES814HnswScalarQuantizedVectorsFormat;
2021
import org.elasticsearch.index.codec.vectors.IVFVectorsFormat;
@@ -133,8 +134,8 @@ public static void main(String[] args) throws Exception {
133134
}
134135
String jsonConfig = args[0];
135136
// Parse command line arguments
136-
Path jsonConfigPath = Path.of(jsonConfig);
137-
if (jsonConfigPath.toFile().exists() == false) {
137+
Path jsonConfigPath = PathUtils.get(jsonConfig);
138+
if (Files.exists(jsonConfigPath) == false) {
138139
throw new IllegalArgumentException("JSON config file does not exist: " + jsonConfigPath);
139140
}
140141
// Parse the JSON config file to get command line arguments
@@ -161,7 +162,7 @@ public static void main(String[] args) throws Exception {
161162
Results result = new Results(cmdLineArgs.indexType().name().toLowerCase(Locale.ROOT), cmdLineArgs.numDocs());
162163
System.out.println("Running KNN index tester with arguments: " + cmdLineArgs);
163164
Codec codec = createCodec(cmdLineArgs);
164-
Path indexPath = Path.of(formatIndexPath(cmdLineArgs));
165+
Path indexPath = PathUtils.get(formatIndexPath(cmdLineArgs));
165166
if (cmdLineArgs.reindex() || cmdLineArgs.forceMerge()) {
166167
KnnIndexer knnIndexer = new KnnIndexer(
167168
cmdLineArgs.docVectors(),
@@ -253,7 +254,7 @@ private String formatRow(String[] values, int[] widths) {
253254
for (int i = 0; i < values.length; i++) {
254255
// Left-align text column (index_type), right-align numeric columns
255256
String format = (i == 0) ? "%-" + widths[i] + "s" : "%" + widths[i] + "s";
256-
row.append(String.format(format, values[i]));
257+
row.append(Strings.format(format, values[i]));
257258

258259
// Add separation between columns
259260
if (i < values.length - 1) {

qa/vector/src/main/java/org/elasticsearch/test/knn/KnnIndexer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535
import org.apache.lucene.index.VectorSimilarityFunction;
3636
import org.apache.lucene.store.FSDirectory;
3737
import org.apache.lucene.util.PrintStreamInfoStream;
38+
import org.elasticsearch.common.io.Channels;
3839

3940
import java.io.IOException;
4041
import java.io.UncheckedIOException;
4142
import java.nio.ByteBuffer;
4243
import java.nio.ByteOrder;
4344
import java.nio.channels.FileChannel;
45+
import java.nio.file.Files;
4446
import java.nio.file.Path;
4547
import java.util.ArrayList;
4648
import java.util.List;
@@ -122,11 +124,7 @@ public boolean isEnabled(String component) {
122124
similarityFunction
123125
);
124126

125-
if (indexPath.toFile().mkdirs()) {
126-
logger.info("Created index directory: %s", indexPath);
127-
} else {
128-
logger.info("Index directory already exists: %s", indexPath);
129-
}
127+
Files.createDirectory(indexPath);
130128

131129
long start = System.nanoTime();
132130
try (
@@ -149,7 +147,7 @@ public boolean isEnabled(String component) {
149147
);
150148

151149
VectorReader inReader = VectorReader.create(in, dim, vectorEncoding);
152-
try (ExecutorService exec = Executors.newFixedThreadPool(numIndexThreads)) {
150+
try (ExecutorService exec = Executors.newFixedThreadPool(numIndexThreads, r -> new Thread(r, "KnnIndexer-Thread"))) {
153151
AtomicInteger numDocsIndexed = new AtomicInteger();
154152
List<Future<?>> threads = new ArrayList<>();
155153
for (int i = 0; i < numIndexThreads; i++) {
@@ -294,12 +292,12 @@ void reset() throws IOException {
294292
}
295293

296294
private void readNext() throws IOException {
297-
int bytesRead = this.input.read(bytes);
295+
int bytesRead = Channels.readFromFileChannel(this.input, this.input.position(), bytes);
298296
if (bytesRead < bytes.capacity()) {
299297
// wrap around back to the start of the file if we hit the end:
300298
logger.warn("VectorReader hit EOF when reading " + this.input + "; now wrapping around to start of file again");
301299
this.input.position(0);
302-
bytesRead = this.input.read(bytes);
300+
bytesRead = Channels.readFromFileChannel(this.input, this.input.position(), bytes);
303301
if (bytesRead < bytes.capacity()) {
304302
throw new IllegalStateException(
305303
"vector file " + input + " doesn't even have enough bytes for a single vector? got bytesRead=" + bytesRead

qa/vector/src/main/java/org/elasticsearch/test/knn/KnnSearcher.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.lucene.store.Directory;
4141
import org.apache.lucene.store.FSDirectory;
4242
import org.apache.lucene.store.MMapDirectory;
43+
import org.elasticsearch.core.PathUtils;
4344
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
4445
import org.elasticsearch.search.profile.query.QueryProfiler;
4546
import org.elasticsearch.search.vectors.ESKnnByteVectorQuery;
@@ -58,7 +59,6 @@
5859
import java.nio.channels.FileChannel;
5960
import java.nio.file.Files;
6061
import java.nio.file.Path;
61-
import java.nio.file.Paths;
6262
import java.nio.file.attribute.FileTime;
6363
import java.util.ArrayList;
6464
import java.util.HashSet;
@@ -119,8 +119,10 @@ void runSearch(KnnIndexTester.Results finalResults) throws IOException {
119119
TopDocs[] results = new TopDocs[numQueryVectors];
120120
int[][] resultIds = new int[numQueryVectors][];
121121
long elapsed, totalCpuTimeMS, totalVisited = 0;
122-
ExecutorService executorService = Executors.newFixedThreadPool(searchThreads);
123-
try (FileChannel input = FileChannel.open(queryPath)) {
122+
try (
123+
FileChannel input = FileChannel.open(queryPath);
124+
ExecutorService executorService = Executors.newFixedThreadPool(searchThreads, r -> new Thread(r, "KnnSearcher-Thread"))
125+
) {
124126
long queryPathSizeInBytes = input.size();
125127
logger.info(
126128
"queryPath size: "
@@ -177,13 +179,10 @@ void runSearch(KnnIndexTester.Results finalResults) throws IOException {
177179
);
178180
}
179181
}
180-
} finally {
181-
executorService.shutdown();
182182
}
183183
logger.info("checking results");
184184
int[][] nn = getOrCalculateExactNN();
185-
float recall = checkResults(resultIds, nn, topK);
186-
finalResults.avgRecall = recall;
185+
finalResults.avgRecall = checkResults(resultIds, nn, topK);
187186
finalResults.qps = (1000f * numQueryVectors) / elapsed;
188187
finalResults.avgLatency = (float) elapsed / numQueryVectors;
189188
finalResults.averageVisited = (double) totalVisited / numQueryVectors;
@@ -206,7 +205,7 @@ private int[][] getOrCalculateExactNN() throws IOException {
206205
36
207206
);
208207
String nnFileName = "nn-" + hash + ".bin";
209-
Path nnPath = Paths.get("target/" + nnFileName);
208+
Path nnPath = PathUtils.get("target/" + nnFileName);
210209
if (Files.exists(nnPath) && isNewer(nnPath, docPath, indexPath, queryPath)) {
211210
logger.info("read pre-cached exact match vectors from cache file \"" + nnPath + "\"");
212211
return readExactNN(nnPath);

0 commit comments

Comments
 (0)