|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.knn.index.codec; |
| 7 | + |
| 8 | +import lombok.extern.log4j.Log4j2; |
| 9 | +import org.apache.lucene.codecs.KnnVectorsWriter; |
| 10 | +import org.apache.lucene.codecs.hnsw.FlatFieldVectorsWriter; |
| 11 | +import org.apache.lucene.index.FieldInfo; |
| 12 | +import org.apache.lucene.index.MergeState; |
| 13 | +import org.apache.lucene.index.SegmentWriteState; |
| 14 | +import org.apache.lucene.search.DocIdSetIterator; |
| 15 | +import org.opensearch.common.Nullable; |
| 16 | +import org.opensearch.common.StopWatch; |
| 17 | +import org.opensearch.common.TriFunction; |
| 18 | +import org.opensearch.knn.index.VectorDataType; |
| 19 | +import org.opensearch.knn.index.codec.nativeindex.NativeIndexBuildStrategyFactory; |
| 20 | +import org.opensearch.knn.index.codec.nativeindex.NativeIndexWriter; |
| 21 | +import org.opensearch.knn.index.vectorvalues.KNNVectorValues; |
| 22 | +import org.opensearch.knn.plugin.stats.KNNGraphValue; |
| 23 | +import org.opensearch.knn.quantization.models.quantizationState.QuantizationState; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.function.Supplier; |
| 29 | + |
| 30 | +import static org.opensearch.knn.common.FieldInfoExtractor.extractVectorDataType; |
| 31 | +import static org.opensearch.knn.index.vectorvalues.KNNVectorValuesFactory.getKNNVectorValuesSupplierForMerge; |
| 32 | +import static org.opensearch.knn.index.vectorvalues.KNNVectorValuesFactory.getVectorValuesSupplier; |
| 33 | + |
| 34 | +@Log4j2 |
| 35 | +public abstract class AbstractNativeEnginesKnnVectorsWriter extends KnnVectorsWriter { |
| 36 | + protected <T> void doFlush( |
| 37 | + final FieldInfo fieldInfo, |
| 38 | + final FlatFieldVectorsWriter<?> fieldWriter, |
| 39 | + final T vectors, |
| 40 | + @Nullable final TriFunction<FieldInfo, Supplier<KNNVectorValues<?>>, Integer, QuantizationState> quantizationStateSupplier, |
| 41 | + final Integer approximateThreshold, |
| 42 | + final SegmentWriteState segmentWriteState, |
| 43 | + final NativeIndexBuildStrategyFactory nativeIndexBuildStrategyFactory, |
| 44 | + final Map<String, Object> buildStrategyParameters |
| 45 | + ) throws IOException { |
| 46 | + // Check total live docs first to avoid unnecessary supplier creation for empty fields |
| 47 | + final int totalLiveDocs; |
| 48 | + if (vectors instanceof Map) { |
| 49 | + totalLiveDocs = ((Map) vectors).size(); |
| 50 | + } else { |
| 51 | + totalLiveDocs = ((List) vectors).size(); |
| 52 | + } |
| 53 | + |
| 54 | + if (totalLiveDocs == 0) { |
| 55 | + log.debug("[Flush] No live docs for field {}", fieldInfo.getName()); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Get vector values supplier |
| 60 | + final VectorDataType vectorDataType = extractVectorDataType(fieldInfo); |
| 61 | + final Supplier<KNNVectorValues<?>> knnVectorValuesSupplier; |
| 62 | + if (vectors instanceof Map) { |
| 63 | + knnVectorValuesSupplier = getVectorValuesSupplier(vectorDataType, fieldWriter.getDocsWithFieldSet(), (Map) vectors); |
| 64 | + } else { |
| 65 | + knnVectorValuesSupplier = getVectorValuesSupplier(vectorDataType, fieldWriter.getDocsWithFieldSet(), (List) vectors); |
| 66 | + } |
| 67 | + |
| 68 | + QuantizationState quantizationState = null; |
| 69 | + if (quantizationStateSupplier != null) { |
| 70 | + // should skip graph building only for non quantization use case and if threshold is met |
| 71 | + quantizationState = quantizationStateSupplier.apply(fieldInfo, knnVectorValuesSupplier, totalLiveDocs); |
| 72 | + if (quantizationState == null && shouldSkipBuildingVectorDataStructure(totalLiveDocs, approximateThreshold)) { |
| 73 | + log.debug( |
| 74 | + "Skip building vector data structure for field: {}, as liveDoc: {} is less than the threshold {} during flush", |
| 75 | + fieldInfo.name, |
| 76 | + totalLiveDocs, |
| 77 | + approximateThreshold |
| 78 | + ); |
| 79 | + return; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + final NativeIndexWriter writer = NativeIndexWriter.getWriter( |
| 84 | + fieldInfo, |
| 85 | + segmentWriteState, |
| 86 | + quantizationState, |
| 87 | + nativeIndexBuildStrategyFactory, |
| 88 | + buildStrategyParameters |
| 89 | + ); |
| 90 | + |
| 91 | + final StopWatch stopWatch = new StopWatch().start(); |
| 92 | + writer.flushIndex(knnVectorValuesSupplier, totalLiveDocs); |
| 93 | + final long time_in_millis = stopWatch.stop().totalTime().millis(); |
| 94 | + KNNGraphValue.REFRESH_TOTAL_TIME_IN_MILLIS.incrementBy(time_in_millis); |
| 95 | + log.debug("Flush took {} ms for vector field [{}]", time_in_millis, fieldInfo.getName()); |
| 96 | + } |
| 97 | + |
| 98 | + protected void doMergeOneField( |
| 99 | + final FieldInfo fieldInfo, |
| 100 | + final MergeState mergeState, |
| 101 | + @Nullable final TriFunction<FieldInfo, Supplier<KNNVectorValues<?>>, Integer, QuantizationState> quantizationStateSupplier, |
| 102 | + final Integer approximateThreshold, |
| 103 | + final SegmentWriteState segmentWriteState, |
| 104 | + final NativeIndexBuildStrategyFactory nativeIndexBuildStrategyFactory, |
| 105 | + final Map<String, Object> buildStrategyParameters |
| 106 | + ) throws IOException { |
| 107 | + final VectorDataType vectorDataType = extractVectorDataType(fieldInfo); |
| 108 | + final Supplier<KNNVectorValues<?>> knnVectorValuesSupplier = getKNNVectorValuesSupplierForMerge( |
| 109 | + vectorDataType, |
| 110 | + fieldInfo, |
| 111 | + mergeState |
| 112 | + ); |
| 113 | + final int totalLiveDocs = getLiveDocs(knnVectorValuesSupplier.get()); |
| 114 | + if (totalLiveDocs == 0) { |
| 115 | + log.debug("[Merge] No live docs for field {}", fieldInfo.getName()); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + QuantizationState quantizationState = null; |
| 120 | + if (quantizationStateSupplier != null) { |
| 121 | + quantizationState = quantizationStateSupplier.apply(fieldInfo, knnVectorValuesSupplier, totalLiveDocs); |
| 122 | + // should skip graph building only for non quantization use case and if threshold is met |
| 123 | + if (quantizationState == null && shouldSkipBuildingVectorDataStructure(totalLiveDocs, approximateThreshold)) { |
| 124 | + log.debug( |
| 125 | + "Skip building vector data structure for field: {}, as liveDoc: {} is less than the threshold {} during merge", |
| 126 | + fieldInfo.name, |
| 127 | + totalLiveDocs, |
| 128 | + approximateThreshold |
| 129 | + ); |
| 130 | + return; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + final NativeIndexWriter writer = NativeIndexWriter.getWriter( |
| 135 | + fieldInfo, |
| 136 | + segmentWriteState, |
| 137 | + quantizationState, |
| 138 | + nativeIndexBuildStrategyFactory, |
| 139 | + buildStrategyParameters |
| 140 | + ); |
| 141 | + |
| 142 | + final StopWatch stopWatch = new StopWatch().start(); |
| 143 | + |
| 144 | + writer.mergeIndex(knnVectorValuesSupplier, totalLiveDocs); |
| 145 | + |
| 146 | + final long time_in_millis = stopWatch.stop().totalTime().millis(); |
| 147 | + KNNGraphValue.MERGE_TOTAL_TIME_IN_MILLIS.incrementBy(time_in_millis); |
| 148 | + log.debug("Merge took {} ms for vector field [{}]", time_in_millis, fieldInfo.getName()); |
| 149 | + } |
| 150 | + |
| 151 | + public static boolean shouldSkipBuildingVectorDataStructure(final long docCount, final int approximateThreshold) { |
| 152 | + if (approximateThreshold < 0) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + return docCount < approximateThreshold; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * The {@link KNNVectorValues} will be exhausted after this function run. So make sure that you are not sending the |
| 160 | + * vectorsValues object which you plan to use later |
| 161 | + */ |
| 162 | + public static int getLiveDocs(final KNNVectorValues<?> vectorValues) throws IOException { |
| 163 | + // Count all the live docs as there vectorValues.totalLiveDocs() just gives the cost for the FloatVectorValues, |
| 164 | + // and doesn't tell the correct number of docs, if there are deleted docs in the segment. So we are counting |
| 165 | + // the total live docs here. |
| 166 | + int liveDocs = 0; |
| 167 | + while (vectorValues.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { |
| 168 | + liveDocs++; |
| 169 | + } |
| 170 | + return liveDocs; |
| 171 | + } |
| 172 | +} |
0 commit comments