Search in sources :

Example 1 with MODEL_INDEX_NAME

use of org.opensearch.knn.common.KNNConstants.MODEL_INDEX_NAME in project k-NN by opensearch-project.

the class UpdateModelMetadataTransportActionTests method testMasterOperation.

public void testMasterOperation() throws InterruptedException {
    // Setup the Model system index
    createIndex(MODEL_INDEX_NAME);
    // Setup the model
    String modelId = "test-model";
    ModelMetadata modelMetadata = new ModelMetadata(KNNEngine.DEFAULT, SpaceType.L2, 128, ModelState.CREATED, ZonedDateTime.now(ZoneOffset.UTC).toString(), "", "");
    // Get update  transport action
    UpdateModelMetadataTransportAction updateModelMetadataTransportAction = node().injector().getInstance(UpdateModelMetadataTransportAction.class);
    // Generate update request
    UpdateModelMetadataRequest updateModelMetadataRequest = new UpdateModelMetadataRequest(modelId, false, modelMetadata);
    // Get cluster state, update metadata, check cluster state - all asynchronously
    final CountDownLatch inProgressLatch1 = new CountDownLatch(1);
    client().admin().cluster().prepareState().execute(ActionListener.wrap(stateResponse1 -> {
        ClusterState clusterState1 = stateResponse1.getState();
        updateModelMetadataTransportAction.masterOperation(updateModelMetadataRequest, clusterState1, ActionListener.wrap(acknowledgedResponse -> {
            assertTrue(acknowledgedResponse.isAcknowledged());
            client().admin().cluster().prepareState().execute(ActionListener.wrap(stateResponse2 -> {
                ClusterState updatedClusterState = stateResponse2.getState();
                IndexMetadata indexMetadata = updatedClusterState.metadata().index(MODEL_INDEX_NAME);
                assertNotNull(indexMetadata);
                Map<String, String> modelMetadataMap = indexMetadata.getCustomData(MODEL_METADATA_FIELD);
                assertNotNull(modelMetadataMap);
                String modelAsString = modelMetadataMap.get(modelId);
                assertNotNull(modelAsString);
                ModelMetadata modelMetadataCopy = ModelMetadata.fromString(modelAsString);
                assertEquals(modelMetadata, modelMetadataCopy);
                inProgressLatch1.countDown();
            }, e -> fail("Update failed:" + e)));
        }, e -> fail("Update failed: " + e)));
    }, e -> fail("Update failed: " + e)));
    assertTrue(inProgressLatch1.await(60, TimeUnit.SECONDS));
    // Generate remove request
    UpdateModelMetadataRequest removeModelMetadataRequest = new UpdateModelMetadataRequest(modelId, true, modelMetadata);
    final CountDownLatch inProgressLatch2 = new CountDownLatch(1);
    client().admin().cluster().prepareState().execute(ActionListener.wrap(stateResponse1 -> {
        ClusterState clusterState1 = stateResponse1.getState();
        updateModelMetadataTransportAction.masterOperation(removeModelMetadataRequest, clusterState1, ActionListener.wrap(acknowledgedResponse -> {
            assertTrue(acknowledgedResponse.isAcknowledged());
            client().admin().cluster().prepareState().execute(ActionListener.wrap(stateResponse2 -> {
                ClusterState updatedClusterState = stateResponse2.getState();
                IndexMetadata indexMetadata = updatedClusterState.metadata().index(MODEL_INDEX_NAME);
                assertNotNull(indexMetadata);
                Map<String, String> modelMetadataMap = indexMetadata.getCustomData(MODEL_METADATA_FIELD);
                assertNotNull(modelMetadataMap);
                String modelAsString = modelMetadataMap.get(modelId);
                assertNull(modelAsString);
                inProgressLatch2.countDown();
            }, e -> fail("Update failed")));
        }, e -> fail("Update failed")));
    }, e -> fail("Update failed")));
    assertTrue(inProgressLatch2.await(60, TimeUnit.SECONDS));
}
Also used : IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MODEL_METADATA_FIELD(org.opensearch.knn.common.KNNConstants.MODEL_METADATA_FIELD) ThreadPool(org.opensearch.threadpool.ThreadPool) ZonedDateTime(java.time.ZonedDateTime) SpaceType(org.opensearch.knn.index.SpaceType) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ModelState(org.opensearch.knn.indices.ModelState) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterState(org.opensearch.cluster.ClusterState) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) Map(java.util.Map) MODEL_INDEX_NAME(org.opensearch.knn.common.KNNConstants.MODEL_INDEX_NAME) KNNSingleNodeTestCase(org.opensearch.knn.KNNSingleNodeTestCase) ActionListener(org.opensearch.action.ActionListener) ZoneOffset(java.time.ZoneOffset) KNNEngine(org.opensearch.knn.index.util.KNNEngine) ClusterState(org.opensearch.cluster.ClusterState) CountDownLatch(java.util.concurrent.CountDownLatch) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 ActionListener (org.opensearch.action.ActionListener)1 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)1 ClusterState (org.opensearch.cluster.ClusterState)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)1 KNNSingleNodeTestCase (org.opensearch.knn.KNNSingleNodeTestCase)1 MODEL_INDEX_NAME (org.opensearch.knn.common.KNNConstants.MODEL_INDEX_NAME)1 MODEL_METADATA_FIELD (org.opensearch.knn.common.KNNConstants.MODEL_METADATA_FIELD)1 SpaceType (org.opensearch.knn.index.SpaceType)1 KNNEngine (org.opensearch.knn.index.util.KNNEngine)1 ModelMetadata (org.opensearch.knn.indices.ModelMetadata)1 ModelState (org.opensearch.knn.indices.ModelState)1 ThreadPool (org.opensearch.threadpool.ThreadPool)1