-
Notifications
You must be signed in to change notification settings - Fork 227
Add index for storing template models #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
8845b9d
e6690a5
d8c299a
ddc331c
498b47d
276cc32
a5d1a19
92d70c3
9d1e958
4013600
e5a1cb5
2041807
9ffc046
199d5f2
ee89aff
e0aa7fa
9bc647c
aa6bef6
e696cd6
4c3fecd
a143e87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| * Modifications Copyright OpenSearch Contributors. See | ||
| * GitHub history for details. | ||
| */ | ||
|
|
||
| package org.opensearch.knn.indices; | ||
|
|
||
| import com.google.common.base.Charsets; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.io.Resources; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.action.ActionListener; | ||
| import org.opensearch.action.DocWriteRequest; | ||
| import org.opensearch.action.admin.indices.create.CreateIndexRequest; | ||
| import org.opensearch.action.admin.indices.create.CreateIndexResponse; | ||
| import org.opensearch.action.delete.DeleteAction; | ||
| import org.opensearch.action.delete.DeleteRequestBuilder; | ||
| import org.opensearch.action.delete.DeleteResponse; | ||
| import org.opensearch.action.get.GetAction; | ||
| import org.opensearch.action.get.GetRequestBuilder; | ||
| import org.opensearch.action.get.GetResponse; | ||
| import org.opensearch.action.index.IndexRequestBuilder; | ||
| import org.opensearch.action.index.IndexResponse; | ||
| import org.opensearch.action.support.WriteRequest; | ||
| import org.opensearch.client.Client; | ||
| import org.opensearch.cluster.service.ClusterService; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.common.xcontent.XContentType; | ||
| import org.opensearch.knn.common.KNNConstants; | ||
| import org.opensearch.knn.index.util.KNNEngine; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.util.Base64; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static org.opensearch.knn.index.KNNSettings.MODEL_INDEX_NUMBER_OF_REPLICAS_SETTING; | ||
| import static org.opensearch.knn.index.KNNSettings.MODEL_INDEX_NUMBER_OF_SHARDS_SETTING; | ||
|
|
||
| /** | ||
| * ModelIndex is a singleton class that controls operations on the model system index | ||
| */ | ||
| public class ModelIndex { | ||
|
jmazanec15 marked this conversation as resolved.
Outdated
|
||
| public static Logger logger = LogManager.getLogger(ModelIndex.class); | ||
|
|
||
| //TODO: Should these be stored in constants or in the class? In PR get feedback | ||
| public static final String MODEL_INDEX_MAPPING = "mappings/model-index.json"; | ||
|
jmazanec15 marked this conversation as resolved.
Outdated
|
||
| public static final String MODEL_INDEX_NAME = ".knn-model-index"; | ||
|
|
||
| private int numberOfShards; | ||
| private int numberOfReplicas; | ||
|
|
||
| private static ModelIndex INSTANCE; | ||
| private static Client client; | ||
| private static ClusterService clusterService; | ||
| private static Settings settings; | ||
|
|
||
| /** | ||
| * Make sure we just have one instance of model index | ||
| * | ||
| * @return ModelIndex instance | ||
| */ | ||
| public static synchronized ModelIndex getInstance() { | ||
| if (INSTANCE == null) { | ||
| INSTANCE = new ModelIndex(); | ||
| } | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| public static void initialize(Client client, ClusterService clusterService, Settings settings) { | ||
| ModelIndex.client = client; | ||
| ModelIndex.clusterService = clusterService; | ||
| ModelIndex.settings = settings; | ||
| } | ||
|
|
||
| private ModelIndex() { | ||
| numberOfShards = MODEL_INDEX_NUMBER_OF_SHARDS_SETTING.get(settings); | ||
| numberOfReplicas = MODEL_INDEX_NUMBER_OF_REPLICAS_SETTING.get(settings); | ||
|
|
||
| clusterService.getClusterSettings().addSettingsUpdateConsumer(MODEL_INDEX_NUMBER_OF_SHARDS_SETTING, | ||
| it -> numberOfShards = it); | ||
| clusterService.getClusterSettings().addSettingsUpdateConsumer(MODEL_INDEX_NUMBER_OF_REPLICAS_SETTING, | ||
| it -> numberOfReplicas = it); | ||
| } | ||
|
|
||
| /** | ||
| * Creates model index. It is possible that the 2 threads call this function simulateously. In this case, one | ||
| * thread will throw a ResourceAlreadyExistsException. This should be caught and handled. | ||
| * | ||
| * @param actionListener CreateIndexResponse listener | ||
| * @throws IOException thrown when get mapping fails | ||
| */ | ||
| public void createModelIndex(ActionListener<CreateIndexResponse> actionListener) throws IOException { | ||
|
jmazanec15 marked this conversation as resolved.
Outdated
|
||
| CreateIndexRequest request = new CreateIndexRequest(MODEL_INDEX_NAME) | ||
| .mapping("_doc", getMapping(), XContentType.JSON) | ||
| .settings(Settings.builder() | ||
| .put("index.hidden", true) | ||
| .put("index.number_of_shards", this.numberOfShards) | ||
| .put("index.number_of_replicas", this.numberOfReplicas) | ||
| ); | ||
| client.admin().indices().create(request, actionListener); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the model index exists | ||
| * | ||
| * @return true if the model index exists; false otherwise | ||
| */ | ||
| public boolean modelIndexExists() { | ||
|
jmazanec15 marked this conversation as resolved.
Outdated
|
||
| return clusterService.state().getRoutingTable().hasIndex(MODEL_INDEX_NAME); | ||
| } | ||
|
|
||
| /** | ||
| * Put a model into the system index. Non-blocking | ||
| * | ||
| * @param modelId Id of model to create | ||
| * @param modelBlob byte array of model | ||
| * @param listener handles index response | ||
| */ | ||
| public void putModel(String modelId, KNNEngine knnEngine, byte[] modelBlob, ActionListener<IndexResponse> listener) { | ||
| String base64Model = Base64.getEncoder().encodeToString(modelBlob); | ||
|
|
||
| Map<String, Object> parameters = ImmutableMap.of( | ||
| KNNConstants.KNN_ENGINE, knnEngine.getName(), | ||
| KNNConstants.MODEL_BLOB_PARAMETER, base64Model | ||
| ); | ||
|
|
||
| IndexRequestBuilder indexRequestBuilder = client.prepareIndex(MODEL_INDEX_NAME, "_doc"); | ||
| indexRequestBuilder.setId(modelId); | ||
| indexRequestBuilder.setSource(parameters); | ||
|
|
||
| putModel(indexRequestBuilder, listener); | ||
| } | ||
|
|
||
| /** | ||
| * Put a model into the system index. Non-blocking. When no id is passed in, OpenSearch will generate the id | ||
| * automatically. The id can be retrieved in the IndexResponse. | ||
| * | ||
| * @param modelBlob byte array of model | ||
| * @param listener handles index response | ||
| */ | ||
| public void putModel(KNNEngine knnEngine, byte[] modelBlob, ActionListener<IndexResponse> listener) { | ||
|
jmazanec15 marked this conversation as resolved.
Outdated
|
||
| String base64Model = Base64.getEncoder().encodeToString(modelBlob); | ||
|
|
||
| Map<String, Object> parameters = ImmutableMap.of( | ||
| KNNConstants.KNN_ENGINE, knnEngine.getName(), | ||
| KNNConstants.MODEL_BLOB_PARAMETER, base64Model | ||
| ); | ||
|
|
||
| IndexRequestBuilder indexRequestBuilder = client.prepareIndex(MODEL_INDEX_NAME, "_doc"); | ||
| indexRequestBuilder.setSource(parameters); | ||
|
|
||
| putModel(indexRequestBuilder, listener); | ||
| } | ||
|
|
||
| private void putModel(IndexRequestBuilder indexRequestBuilder, ActionListener<IndexResponse> listener) { | ||
| if (!modelIndexExists()) { | ||
| throw new IllegalStateException("Cannot put model in index before index has been initialized"); | ||
| } | ||
|
|
||
| // Fail if the id already exists. Models are not updateable | ||
| indexRequestBuilder.setOpType(DocWriteRequest.OpType.CREATE); | ||
| indexRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
| indexRequestBuilder.execute(listener); | ||
| } | ||
|
|
||
| /** | ||
| * Get a model from the system index. Call blocks. | ||
| * | ||
| * @param modelId to retrieve | ||
| * @return byte array representing the model | ||
| * @throws ExecutionException thrown on search | ||
| * @throws InterruptedException thrown on search | ||
| */ | ||
| public byte[] getModel(String modelId) throws ExecutionException, InterruptedException { | ||
| if (!modelIndexExists()) { | ||
| throw new IllegalStateException("Cannot get model. Model index does not exist."); | ||
|
jmazanec15 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /* | ||
| GET /<model_index>/<modelId>?source_includes=<model_blob>&_local | ||
| */ | ||
| GetRequestBuilder getRequestBuilder = new GetRequestBuilder(client, GetAction.INSTANCE, MODEL_INDEX_NAME) | ||
| .setId(modelId) | ||
| .setFetchSource(KNNConstants.MODEL_BLOB_PARAMETER, null) | ||
| .setPreference("_local"); | ||
| GetResponse getResponse = getRequestBuilder.execute().get(); | ||
|
|
||
| Object blob = getResponse.getSourceAsMap().get(KNNConstants.MODEL_BLOB_PARAMETER); | ||
|
|
||
| if (blob == null) { | ||
| throw new IllegalArgumentException("ModelID: \"" + modelId + "\" is not present in index"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this message be more user focused? Something like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can update. |
||
| } | ||
|
|
||
| return Base64.getDecoder().decode((String) blob); | ||
| } | ||
|
|
||
| private String getMapping() throws IOException { | ||
| URL url = ModelIndex.class.getClassLoader().getResource(ModelIndex.MODEL_INDEX_MAPPING); | ||
| assert url != null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. asserts are sometimes disabled on production hosts. Can we do manual check and throw exception?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, will update. |
||
| return Resources.toString(url, Charsets.UTF_8); | ||
| } | ||
|
|
||
| /** | ||
| * Delete model from index | ||
| * | ||
| * @param modelId to delete | ||
| * @param listener handles delete response | ||
| */ | ||
| public void deleteModel(String modelId, ActionListener<DeleteResponse> listener) { | ||
| if (!modelIndexExists()) { | ||
| throw new IllegalStateException("Cannot delete model. Model index does not exist."); | ||
| } | ||
|
|
||
| DeleteRequestBuilder deleteRequestBuilder = new DeleteRequestBuilder(client, DeleteAction.INSTANCE, | ||
| MODEL_INDEX_NAME); | ||
| deleteRequestBuilder.setId(modelId); | ||
| deleteRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
| deleteRequestBuilder.execute(listener); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "properties": { | ||
| "engine": { | ||
| "type": "keyword" | ||
| }, | ||
| "model_blob": { | ||
| "type": "binary" | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default should be 1 replica?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will update.