Search in sources :

Example 16 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingModelRequestTests method testValidation_invalid_descriptionToLong.

public void testValidation_invalid_descriptionToLong() {
    // Setup the training request
    String modelId = "test-model-id";
    KNNMethodContext knnMethodContext = mock(KNNMethodContext.class);
    when(knnMethodContext.validate()).thenReturn(null);
    when(knnMethodContext.isTrainingRequired()).thenReturn(true);
    int dimension = 10;
    String trainingIndex = "test-training-index";
    String trainingField = "test-training-field";
    String trainingFieldModeId = "training-field-model-id";
    char[] chars = new char[KNNConstants.MAX_MODEL_DESCRIPTION_LENGTH + 1];
    Arrays.fill(chars, 'a');
    String description = new String(chars);
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(modelId, knnMethodContext, dimension, trainingIndex, trainingField, null, description);
    // Mock the model dao to return metadata for modelId to recognize it is a duplicate
    ModelMetadata trainingFieldModelMetadata = mock(ModelMetadata.class);
    when(trainingFieldModelMetadata.getDimension()).thenReturn(dimension);
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.getMetadata(modelId)).thenReturn(null);
    when(modelDao.getMetadata(trainingFieldModeId)).thenReturn(trainingFieldModelMetadata);
    // Cluster service that wont produce validation exception
    ClusterService clusterService = getClusterServiceForValidReturns(trainingIndex, trainingField, dimension);
    // Initialize static components with the mocks
    TrainingModelRequest.initialize(modelDao, clusterService);
    // Test that validation produces model already exists error message
    ActionRequestValidationException exception = trainingModelRequest.validate();
    assertNotNull(exception);
    List<String> validationErrors = exception.validationErrors();
    assertEquals(1, validationErrors.size());
    assertTrue(validationErrors.get(0).contains("Description exceeds limit"));
}
Also used : KNNMethodContext(org.opensearch.knn.index.KNNMethodContext) ClusterService(org.opensearch.cluster.service.ClusterService) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) ModelDao(org.opensearch.knn.indices.ModelDao)

Example 17 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingModelRequestTests method testValidation_invalid_dimensionDoesNotMatch.

public void testValidation_invalid_dimensionDoesNotMatch() {
    // Check that validation produces exception when dimension does not match
    // Setup the training request
    String modelId = "test-model-id";
    KNNMethodContext knnMethodContext = mock(KNNMethodContext.class);
    when(knnMethodContext.validate()).thenReturn(null);
    when(knnMethodContext.isTrainingRequired()).thenReturn(true);
    int dimension = 10;
    String trainingIndex = "test-training-index";
    String trainingField = "test-training-field";
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(modelId, knnMethodContext, dimension, trainingIndex, trainingField, null, null);
    // Mock the model dao to return null so that no exception is produced
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.getMetadata(modelId)).thenReturn(null);
    // Return  mapping with different dimension
    Map<String, Object> mappingMap = ImmutableMap.of("properties", ImmutableMap.of(trainingField, ImmutableMap.of("type", KNNVectorFieldMapper.CONTENT_TYPE, KNNConstants.DIMENSION, dimension + 1)));
    MappingMetadata mappingMetadata = mock(MappingMetadata.class);
    when(mappingMetadata.getSourceAsMap()).thenReturn(mappingMap);
    IndexMetadata indexMetadata = mock(IndexMetadata.class);
    when(indexMetadata.mapping()).thenReturn(mappingMetadata);
    Metadata metadata = mock(Metadata.class);
    when(metadata.index(trainingIndex)).thenReturn(indexMetadata);
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.metadata()).thenReturn(metadata);
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(clusterState);
    // Initialize static components with the mocks
    TrainingModelRequest.initialize(modelDao, clusterService);
    // Test that validation produces model already exists error message
    ActionRequestValidationException exception = trainingModelRequest.validate();
    assertNotNull(exception);
    List<String> validationErrors = exception.validationErrors();
    assertEquals(1, validationErrors.size());
    assertTrue(validationErrors.get(0).contains("different from dimension"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) KNNMethodContext(org.opensearch.knn.index.KNNMethodContext) ClusterService(org.opensearch.cluster.service.ClusterService) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelDao(org.opensearch.knn.indices.ModelDao) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata)

Example 18 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingModelRequestTests method testValidation_invalid_trainingFieldDoesNotExist.

public void testValidation_invalid_trainingFieldDoesNotExist() {
    // Check that validation produces exception when the training field doesnt exist
    // Setup the training request
    String modelId = "test-model-id";
    KNNMethodContext knnMethodContext = mock(KNNMethodContext.class);
    when(knnMethodContext.validate()).thenReturn(null);
    when(knnMethodContext.isTrainingRequired()).thenReturn(true);
    int dimension = 10;
    String trainingIndex = "test-training-index";
    String trainingField = "test-training-field";
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(modelId, knnMethodContext, dimension, trainingIndex, trainingField, null, null);
    // Mock the model dao to return null so that no exception is produced
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.getMetadata(modelId)).thenReturn(null);
    // Return empty mapping so that training field does not exist
    MappingMetadata mappingMetadata = mock(MappingMetadata.class);
    when(mappingMetadata.getSourceAsMap()).thenReturn(Collections.emptyMap());
    IndexMetadata indexMetadata = mock(IndexMetadata.class);
    when(indexMetadata.mapping()).thenReturn(mappingMetadata);
    Metadata metadata = mock(Metadata.class);
    when(metadata.index(trainingIndex)).thenReturn(indexMetadata);
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.metadata()).thenReturn(metadata);
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(clusterState);
    // Initialize static components with the mocks
    TrainingModelRequest.initialize(modelDao, clusterService);
    // Test that validation produces model already exists error message
    ActionRequestValidationException exception = trainingModelRequest.validate();
    assertNotNull(exception);
    List<String> validationErrors = exception.validationErrors();
    assertEquals(1, validationErrors.size());
    assertTrue(validationErrors.get(0).contains("does not exist"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) KNNMethodContext(org.opensearch.knn.index.KNNMethodContext) ClusterService(org.opensearch.cluster.service.ClusterService) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelDao(org.opensearch.knn.indices.ModelDao) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata)

Example 19 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingModelRequestTests method testValidation_invalid_preferredNodeDoesNotExist.

public void testValidation_invalid_preferredNodeDoesNotExist() {
    // Check that validation produces exception preferred node does not exist
    // Setup the training request
    String modelId = "test-model-id";
    KNNMethodContext knnMethodContext = mock(KNNMethodContext.class);
    when(knnMethodContext.validate()).thenReturn(null);
    when(knnMethodContext.isTrainingRequired()).thenReturn(true);
    int dimension = 10;
    String trainingIndex = "test-training-index";
    String trainingField = "test-training-field";
    String preferredNode = "preferred-node";
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(modelId, knnMethodContext, dimension, trainingIndex, trainingField, preferredNode, null);
    // Mock the model dao to return metadata for modelId to recognize it is a duplicate
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.getMetadata(modelId)).thenReturn(null);
    // This cluster service mocking should not produce exception
    Map<String, Object> mappingMap = ImmutableMap.of("properties", ImmutableMap.of(trainingField, ImmutableMap.of("type", KNNVectorFieldMapper.CONTENT_TYPE, KNNConstants.DIMENSION, dimension)));
    MappingMetadata mappingMetadata = mock(MappingMetadata.class);
    when(mappingMetadata.getSourceAsMap()).thenReturn(mappingMap);
    IndexMetadata indexMetadata = mock(IndexMetadata.class);
    when(indexMetadata.mapping()).thenReturn(mappingMetadata);
    Metadata metadata = mock(Metadata.class);
    when(metadata.index(trainingIndex)).thenReturn(indexMetadata);
    // Empty set of data nodes to produce exception
    DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
    when(discoveryNodes.getDataNodes()).thenReturn(ImmutableOpenMap.of());
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.metadata()).thenReturn(metadata);
    when(clusterState.nodes()).thenReturn(discoveryNodes);
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(clusterState);
    // Initialize static components with the mocks
    TrainingModelRequest.initialize(modelDao, clusterService);
    // Test that validation produces model already exists error message
    ActionRequestValidationException exception = trainingModelRequest.validate();
    assertNotNull(exception);
    List<String> validationErrors = exception.validationErrors();
    assertEquals(1, validationErrors.size());
    assertTrue(validationErrors.get(0).contains("Preferred node"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) KNNMethodContext(org.opensearch.knn.index.KNNMethodContext) ClusterService(org.opensearch.cluster.service.ClusterService) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelDao(org.opensearch.knn.indices.ModelDao) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 20 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingModelRequestTests method testValidation_valid_trainingIndexBuiltFromModel.

public void testValidation_valid_trainingIndexBuiltFromModel() {
    // Setup the training request
    String modelId = "test-model-id";
    KNNMethodContext knnMethodContext = mock(KNNMethodContext.class);
    when(knnMethodContext.validate()).thenReturn(null);
    when(knnMethodContext.isTrainingRequired()).thenReturn(true);
    int dimension = 10;
    String trainingIndex = "test-training-index";
    String trainingField = "test-training-field";
    String trainingFieldModeId = "training-field-model-id";
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(modelId, knnMethodContext, dimension, trainingIndex, trainingField, null, null);
    // Mock the model dao to return metadata for modelId to recognize it is a duplicate
    ModelMetadata trainingFieldModelMetadata = mock(ModelMetadata.class);
    when(trainingFieldModelMetadata.getDimension()).thenReturn(dimension);
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.getMetadata(modelId)).thenReturn(null);
    when(modelDao.getMetadata(trainingFieldModeId)).thenReturn(trainingFieldModelMetadata);
    // Return model id instead of dimension directly
    Map<String, Object> mappingMap = ImmutableMap.of("properties", ImmutableMap.of(trainingField, ImmutableMap.of("type", KNNVectorFieldMapper.CONTENT_TYPE, KNNConstants.MODEL_ID, trainingFieldModeId)));
    MappingMetadata mappingMetadata = mock(MappingMetadata.class);
    when(mappingMetadata.getSourceAsMap()).thenReturn(mappingMap);
    IndexMetadata indexMetadata = mock(IndexMetadata.class);
    when(indexMetadata.mapping()).thenReturn(mappingMetadata);
    Metadata metadata = mock(Metadata.class);
    when(metadata.index(trainingIndex)).thenReturn(indexMetadata);
    DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
    when(discoveryNodes.getDataNodes()).thenReturn(ImmutableOpenMap.of());
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.metadata()).thenReturn(metadata);
    when(clusterState.nodes()).thenReturn(discoveryNodes);
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(clusterState);
    // Initialize static components with the mocks
    TrainingModelRequest.initialize(modelDao, clusterService);
    // Test that validation produces model already exists error message
    ActionRequestValidationException exception = trainingModelRequest.validate();
    assertNull(exception);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) KNNMethodContext(org.opensearch.knn.index.KNNMethodContext) ClusterService(org.opensearch.cluster.service.ClusterService) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) ModelDao(org.opensearch.knn.indices.ModelDao) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Aggregations

ClusterService (org.opensearch.cluster.service.ClusterService)296 ThreadPool (org.opensearch.threadpool.ThreadPool)123 Settings (org.opensearch.common.settings.Settings)115 ClusterState (org.opensearch.cluster.ClusterState)106 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)103 TestThreadPool (org.opensearch.threadpool.TestThreadPool)93 TransportService (org.opensearch.transport.TransportService)86 ClusterSettings (org.opensearch.common.settings.ClusterSettings)76 ActionListener (org.opensearch.action.ActionListener)75 Before (org.junit.Before)66 ActionFilters (org.opensearch.action.support.ActionFilters)66 CountDownLatch (java.util.concurrent.CountDownLatch)65 HashSet (java.util.HashSet)63 TimeValue (org.opensearch.common.unit.TimeValue)61 IOException (java.io.IOException)56 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)53 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)48 Collections (java.util.Collections)47 ClusterName (org.opensearch.cluster.ClusterName)47 Metadata (org.opensearch.cluster.metadata.Metadata)47