|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.knn.index.codec.nativeindex.remote; |
| 7 | + |
| 8 | +import lombok.extern.log4j.Log4j2; |
| 9 | +import org.opensearch.common.StopWatch; |
| 10 | +import org.opensearch.knn.index.codec.nativeindex.model.BuildIndexParams; |
| 11 | +import org.opensearch.knn.index.vectorvalues.KNNVectorValues; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +import static org.opensearch.knn.index.codec.util.KNNCodecUtil.initializeVectorValues; |
| 16 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.BUILD_REQUEST_FAILURE_COUNT; |
| 17 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.BUILD_REQUEST_SUCCESS_COUNT; |
| 18 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.INDEX_BUILD_FAILURE_COUNT; |
| 19 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.INDEX_BUILD_SUCCESS_COUNT; |
| 20 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.READ_FAILURE_COUNT; |
| 21 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.READ_SUCCESS_COUNT; |
| 22 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.READ_TIME; |
| 23 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.REMOTE_INDEX_BUILD_CURRENT_FLUSH_OPERATIONS; |
| 24 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.REMOTE_INDEX_BUILD_CURRENT_FLUSH_SIZE; |
| 25 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.REMOTE_INDEX_BUILD_CURRENT_MERGE_OPERATIONS; |
| 26 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.REMOTE_INDEX_BUILD_CURRENT_MERGE_SIZE; |
| 27 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.REMOTE_INDEX_BUILD_FLUSH_TIME; |
| 28 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.REMOTE_INDEX_BUILD_MERGE_TIME; |
| 29 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.WAITING_TIME; |
| 30 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.WRITE_FAILURE_COUNT; |
| 31 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.WRITE_SUCCESS_COUNT; |
| 32 | +import static org.opensearch.knn.plugin.stats.KNNRemoteIndexBuildValue.WRITE_TIME; |
| 33 | + |
| 34 | +/** |
| 35 | + * Class to handle all metric collection for the remote index build. |
| 36 | + * Each phase has its own StopWatch and `start` and `end` methods. |
| 37 | + */ |
| 38 | +@Log4j2 |
| 39 | +public class RemoteIndexBuildMetrics { |
| 40 | + private final StopWatch overallStopWatch; |
| 41 | + private final StopWatch writeStopWatch; |
| 42 | + private final StopWatch buildRequestStopWatch; |
| 43 | + private final StopWatch waiterStopWatch; |
| 44 | + private final StopWatch readStopWatch; |
| 45 | + private long size; |
| 46 | + private boolean isFlush; |
| 47 | + private String fieldName; |
| 48 | + |
| 49 | + public RemoteIndexBuildMetrics() { |
| 50 | + this.overallStopWatch = new StopWatch(); |
| 51 | + this.writeStopWatch = new StopWatch(); |
| 52 | + this.buildRequestStopWatch = new StopWatch(); |
| 53 | + this.waiterStopWatch = new StopWatch(); |
| 54 | + this.readStopWatch = new StopWatch(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Helper method to collect remote index build metrics on start |
| 59 | + */ |
| 60 | + public void startRemoteIndexBuildMetrics(BuildIndexParams indexInfo) throws IOException { |
| 61 | + KNNVectorValues<?> knnVectorValues = indexInfo.getKnnVectorValuesSupplier().get(); |
| 62 | + initializeVectorValues(knnVectorValues); |
| 63 | + this.size = (long) indexInfo.getTotalLiveDocs() * knnVectorValues.bytesPerVector(); |
| 64 | + this.isFlush = indexInfo.isFlush(); |
| 65 | + this.fieldName = indexInfo.getFieldName(); |
| 66 | + overallStopWatch.start(); |
| 67 | + if (isFlush) { |
| 68 | + REMOTE_INDEX_BUILD_CURRENT_FLUSH_OPERATIONS.increment(); |
| 69 | + REMOTE_INDEX_BUILD_CURRENT_FLUSH_SIZE.incrementBy(size); |
| 70 | + } else { |
| 71 | + REMOTE_INDEX_BUILD_CURRENT_MERGE_OPERATIONS.increment(); |
| 72 | + REMOTE_INDEX_BUILD_CURRENT_MERGE_SIZE.incrementBy(size); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Repository read phase metric helpers |
| 77 | + public void startRepositoryWriteMetrics() { |
| 78 | + writeStopWatch.start(); |
| 79 | + } |
| 80 | + |
| 81 | + public void endRepositoryWriteMetrics(boolean success) { |
| 82 | + long time_in_millis = writeStopWatch.stop().totalTime().millis(); |
| 83 | + if (success) { |
| 84 | + WRITE_SUCCESS_COUNT.increment(); |
| 85 | + WRITE_TIME.incrementBy(time_in_millis); |
| 86 | + log.debug("Repository write took {} ms for vector field [{}]", time_in_millis, fieldName); |
| 87 | + } else { |
| 88 | + WRITE_FAILURE_COUNT.increment(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Build request phase metric helpers |
| 93 | + public void startBuildRequestMetrics() { |
| 94 | + buildRequestStopWatch.start(); |
| 95 | + } |
| 96 | + |
| 97 | + public void endBuildRequestMetrics(boolean success) { |
| 98 | + long time_in_millis = buildRequestStopWatch.stop().totalTime().millis(); |
| 99 | + if (success) { |
| 100 | + BUILD_REQUEST_SUCCESS_COUNT.increment(); |
| 101 | + log.debug("Submit vector build took {} ms for vector field [{}]", time_in_millis, fieldName); |
| 102 | + } else { |
| 103 | + BUILD_REQUEST_FAILURE_COUNT.increment(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Await index build phase metric helpers |
| 108 | + public void startWaitingMetrics() { |
| 109 | + waiterStopWatch.start(); |
| 110 | + } |
| 111 | + |
| 112 | + public void endWaitingMetrics() { |
| 113 | + long time_in_millis = waiterStopWatch.stop().totalTime().millis(); |
| 114 | + WAITING_TIME.incrementBy(time_in_millis); |
| 115 | + log.debug("Await vector build took {} ms for vector field [{}]", time_in_millis, fieldName); |
| 116 | + } |
| 117 | + |
| 118 | + // Repository read phase metric helpers |
| 119 | + public void startRepositoryReadMetrics() { |
| 120 | + readStopWatch.start(); |
| 121 | + } |
| 122 | + |
| 123 | + public void endRepositoryReadMetrics(boolean success) { |
| 124 | + long time_in_millis = readStopWatch.stop().totalTime().millis(); |
| 125 | + if (success) { |
| 126 | + READ_SUCCESS_COUNT.increment(); |
| 127 | + READ_TIME.incrementBy(time_in_millis); |
| 128 | + log.debug("Repository read took {} ms for vector field [{}]", time_in_millis, fieldName); |
| 129 | + } else { |
| 130 | + READ_FAILURE_COUNT.increment(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Helper method to collect overall remote index build metrics |
| 136 | + */ |
| 137 | + public void endRemoteIndexBuildMetrics(boolean wasSuccessful) { |
| 138 | + long time_in_millis = overallStopWatch.stop().totalTime().millis(); |
| 139 | + if (wasSuccessful) { |
| 140 | + INDEX_BUILD_SUCCESS_COUNT.increment(); |
| 141 | + log.debug("Remote index build succeeded after {} ms for vector field [{}]", time_in_millis, fieldName); |
| 142 | + } else { |
| 143 | + INDEX_BUILD_FAILURE_COUNT.increment(); |
| 144 | + log.warn("Remote index build failed after {} ms for vector field [{}]", time_in_millis, fieldName); |
| 145 | + } |
| 146 | + if (isFlush) { |
| 147 | + REMOTE_INDEX_BUILD_CURRENT_FLUSH_OPERATIONS.decrement(); |
| 148 | + REMOTE_INDEX_BUILD_CURRENT_FLUSH_SIZE.decrementBy(size); |
| 149 | + REMOTE_INDEX_BUILD_FLUSH_TIME.incrementBy(time_in_millis); |
| 150 | + } else { |
| 151 | + REMOTE_INDEX_BUILD_CURRENT_MERGE_OPERATIONS.decrement(); |
| 152 | + REMOTE_INDEX_BUILD_CURRENT_MERGE_SIZE.decrementBy(size); |
| 153 | + REMOTE_INDEX_BUILD_MERGE_TIME.incrementBy(time_in_millis); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments