Skip to content

Commit 9b6f9a6

Browse files
committed
Adds graph build time metrics in NativeEngines990KnnVectorsWriter
Signed-off-by: Tejas Shah <[email protected]>
1 parent 2b303d9 commit 9b6f9a6

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

src/main/java/org/opensearch/knn/index/codec/KNN990Codec/NativeEngines990KnnVectorsWriter.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.apache.lucene.index.Sorter;
2525
import org.apache.lucene.util.IOUtils;
2626
import org.apache.lucene.util.RamUsageEstimator;
27+
import org.opensearch.common.StopWatch;
2728
import org.opensearch.knn.index.quantizationservice.QuantizationService;
2829
import org.opensearch.knn.index.VectorDataType;
2930
import org.opensearch.knn.index.codec.nativeindex.NativeIndexWriter;
3031
import org.opensearch.knn.index.vectorvalues.KNNVectorValues;
3132
import org.opensearch.knn.index.vectorvalues.KNNVectorValuesFactory;
33+
import org.opensearch.knn.plugin.stats.KNNGraphValue;
3234
import org.opensearch.knn.quantization.models.quantizationParams.QuantizationParams;
3335
import org.opensearch.knn.quantization.models.quantizationState.QuantizationState;
3436

@@ -78,7 +80,8 @@ public void flush(int maxDoc, final Sorter.DocMap sortMap) throws IOException {
7880
field.getFieldInfo(),
7981
(vectorDataType, fieldInfo, fieldVectorsWriter) -> getKNNVectorValues(vectorDataType, fieldVectorsWriter),
8082
NativeIndexWriter::flushIndex,
81-
field
83+
field,
84+
KNNGraphValue.REFRESH_TOTAL_TIME_IN_MILLIS
8285
);
8386
}
8487
}
@@ -88,7 +91,13 @@ public void mergeOneField(final FieldInfo fieldInfo, final MergeState mergeState
8891
// This will ensure that we are merging the FlatIndex during force merge.
8992
flatVectorsWriter.mergeOneField(fieldInfo, mergeState);
9093
// For merge, pick values from flat vector and reindex again. This will use the flush operation to create graphs
91-
trainAndIndex(fieldInfo, this::getKNNVectorValuesForMerge, NativeIndexWriter::mergeIndex, mergeState);
94+
trainAndIndex(
95+
fieldInfo,
96+
this::getKNNVectorValuesForMerge,
97+
NativeIndexWriter::mergeIndex,
98+
mergeState,
99+
KNNGraphValue.MERGE_TOTAL_TIME_IN_MILLIS
100+
);
92101

93102
}
94103

@@ -214,7 +223,8 @@ private <T, C> void trainAndIndex(
214223
final FieldInfo fieldInfo,
215224
final VectorValuesRetriever<VectorDataType, FieldInfo, C, KNNVectorValues<T>> vectorValuesRetriever,
216225
final IndexOperation<T> indexOperation,
217-
final C VectorProcessingContext
226+
final C VectorProcessingContext,
227+
final KNNGraphValue graphBuildTime
218228
) throws IOException {
219229
final VectorDataType vectorDataType = extractVectorDataType(fieldInfo);
220230
KNNVectorValues<T> knnVectorValues = vectorValuesRetriever.apply(vectorDataType, fieldInfo, VectorProcessingContext);
@@ -228,6 +238,12 @@ private <T, C> void trainAndIndex(
228238
: NativeIndexWriter.getWriter(fieldInfo, segmentWriteState);
229239

230240
knnVectorValues = vectorValuesRetriever.apply(vectorDataType, fieldInfo, VectorProcessingContext);
241+
242+
StopWatch stopWatch = new StopWatch();
243+
stopWatch.start();
231244
indexOperation.buildAndWrite(writer, knnVectorValues);
245+
long time_in_millis = stopWatch.totalTime().millis();
246+
graphBuildTime.set(graphBuildTime.getValue() + time_in_millis);
247+
log.warn("Graph build complete in " + time_in_millis + " ms for " + graphBuildTime.getName());
232248
}
233249
}

0 commit comments

Comments
 (0)