|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Warmup API |
| 4 | +parent: KNN |
| 5 | +nav_order: 5 |
| 6 | +has_children: false |
| 7 | +has_toc: false |
| 8 | +has_math: false |
| 9 | +--- |
| 10 | + |
| 11 | +# Warmup API operation for the k-NN plugin |
| 12 | + |
| 13 | +The Hierarchical Navigable Small World (HNSW) graphs that are used to perform an approximate k-Nearest Neighbor (k-NN) search are stored as `.hnsw` files with other Apache Lucene segment files. In order for you to perform a search on these graphs using the k-NN plugin, these files need to be loaded into native memory. |
| 14 | + |
| 15 | +If the plugin has not loaded the graphs into native memory, it loads them when it receives a search request. This loading time can cause high latency during initial queries. To avoid this situation, users often run random queries during a warmup period. After this warmup period, the graphs are loaded into native memory and their production workloads can begin. This loading process is indirect and requires extra effort. |
| 16 | + |
| 17 | +As an alternative, you can avoid this latency issue by running the k-NN plugin warmup API operation on whatever indices you're interested in searching. This operation loads all the graphs for all of the shards (primaries and replicas) of all the indices specified in the request into native memory. |
| 18 | + |
| 19 | +After the process finishes, you can start searching against the indices with no initial latency penalties. The warmup API operation is idempotent, so if a segment's graphs are already loaded into memory, this operation has no impact on those graphs. It only loads graphs that aren't currently in memory. |
| 20 | + |
| 21 | +## Usage |
| 22 | +This request performs a warmup on three indices: |
| 23 | + |
| 24 | +```json |
| 25 | +GET /_opendistro/_knn/warmup/index1,index2,index3?pretty |
| 26 | +{ |
| 27 | + "_shards" : { |
| 28 | + "total" : 6, |
| 29 | + "successful" : 6, |
| 30 | + "failed" : 0 |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +`total` indicates how many shards the k-NN plugin attempted to warm up. The response also includes the number of shards the plugin succeeded and failed to warm up. |
| 36 | + |
| 37 | +The call does not return until the warmup operation is complete or the request times out. If the request times out, the operation still continues on the cluster. To monitor the warmup operation, use the Elasticsearch `_tasks` API: |
| 38 | + |
| 39 | +```json |
| 40 | +GET /_tasks |
| 41 | +``` |
| 42 | + |
| 43 | +After the operation has finished, use the [k-NN `_stats` API operation](../settings#statistics) to see what the k-NN plugin loaded into the graph. |
| 44 | + |
| 45 | +## Best practices |
| 46 | +For the warmup API to function properly, follow these best practices. |
| 47 | + |
| 48 | +First, don't run merge operations on indices that you want to warm up. During merge, the k-NN plugin creates new segments, and old segments are (sometimes) deleted. For example, you could encounter a situation in which the warmup API operation loads graphs A and B into native memory, but segment C is created from segments A and B being merged. The graphs for A and B would no longer be in memory, and graph C would also not be in memory. In this case, the initial penalty for loading graph C is still present. |
| 49 | + |
| 50 | +Second, confirm that all graphs you want to warm up can fit into native memory. For more information about the native memory limit, see the [knn.memory.circuit_breaker.limit statistic](../settings/#cluster-settings). High graph memory usage causes cache thrashing, which can lead to operations constantly failing and attempting to run again. |
| 51 | + |
| 52 | +Finally, don't index any documents that you want to load into the cache. Writing new information to segments prevents the warmup API operation from loading the graphs until they're searchable. This means that you would have to run the warmup operation again after indexing finishes. |
0 commit comments