Search in sources :

Example 6 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project k-NN by opensearch-project.

the class TrainingModelRequestTests method testValidation_invalid_trainingFieldNotKnnVector.

public void testValidation_invalid_trainingFieldNotKnnVector() {
    // Check that validation produces exception when field is not knn_vector
    // 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 type
    Map<String, Object> mappingMap = ImmutableMap.of("properties", ImmutableMap.of(trainingField, ImmutableMap.of("type", "int", 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);
    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("not of type"));
}
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 7 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project k-NN by opensearch-project.

the class TrainingModelRequestTests method getClusterServiceForValidReturns.

/**
 * This method produces a cluster service that will mock so that there are no validation exceptions.
 *
 * @param trainingIndex Name of training index
 * @param trainingField Name of training field
 * @param dimension Expected dimension
 * @return ClusterService
 */
private ClusterService getClusterServiceForValidReturns(String trainingIndex, String trainingField, int dimension) {
    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);
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.metadata()).thenReturn(metadata);
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(clusterState);
    return clusterService;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterService(org.opensearch.cluster.service.ClusterService) 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) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata)

Example 8 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project anomaly-detection by opensearch-project.

the class UpdateMappingTests method testUpdateMapping.

@SuppressWarnings({ "serial", "unchecked" })
public void testUpdateMapping() throws IOException {
    doAnswer(invocation -> {
        ActionListener<GetAliasesResponse> listener = (ActionListener<GetAliasesResponse>) invocation.getArgument(1);
        ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder();
        List<AliasMetadata> aliasMetadata = new ArrayList<>();
        aliasMetadata.add(AliasMetadata.builder(ADIndex.RESULT.name()).build());
        builder.put(resultIndexName, aliasMetadata);
        listener.onResponse(new GetAliasesResponse(builder.build()));
        return null;
    }).when(indicesAdminClient).getAliases(any(GetAliasesRequest.class), any());
    IndexMetadata indexMetadata = IndexMetadata.builder(resultIndexName).putAlias(AliasMetadata.builder(ADIndex.RESULT.getIndexName())).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).putMapping(new MappingMetadata("type", new HashMap<String, Object>() {

        {
            put(AnomalyDetectionIndices.META, new HashMap<String, Object>() {

                {
                    // version 1 will cause update
                    put(CommonName.SCHEMA_VERSION_FIELD, 1);
                }
            });
        }
    })).build();
    ImmutableOpenMap.Builder<String, IndexMetadata> openMapBuilder = ImmutableOpenMap.builder();
    openMapBuilder.put(resultIndexName, indexMetadata);
    Metadata metadata = Metadata.builder().indices(openMapBuilder.build()).build();
    when(clusterState.getMetadata()).thenReturn(metadata);
    when(clusterState.metadata()).thenReturn(metadata);
    adIndices.update();
    verify(indicesAdminClient, times(1)).putMapping(any(), any());
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) GetAliasesResponse(org.opensearch.action.admin.indices.alias.get.GetAliasesResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) Mockito.anyString(org.mockito.Mockito.anyString) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) ActionListener(org.opensearch.action.ActionListener) ArrayList(java.util.ArrayList) List(java.util.List) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata)

Example 9 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project anomaly-detection by opensearch-project.

the class TestHelpers method createIndexBlockedState.

public static ClusterState createIndexBlockedState(String indexName, Settings hackedSettings, String alias) {
    ClusterState blockedClusterState = null;
    IndexMetadata.Builder builder = IndexMetadata.builder(indexName);
    if (alias != null) {
        builder.putAlias(AliasMetadata.builder(alias));
    }
    IndexMetadata indexMetaData = builder.settings(Settings.builder().put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(hackedSettings)).build();
    Metadata metaData = Metadata.builder().put(indexMetaData, false).build();
    blockedClusterState = ClusterState.builder(new ClusterName("test cluster")).metadata(metaData).blocks(ClusterBlocks.builder().addBlocks(indexMetaData)).build();
    return blockedClusterState;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Metadata(org.opensearch.cluster.metadata.Metadata) FieldMappingMetadata(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 10 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project anomaly-detection by opensearch-project.

the class CustomIndexTests method testInCorrectMapping.

public void testInCorrectMapping() throws IOException {
    Map<String, Object> mappings = new HashMap<>();
    Map<String, Object> past_mapping = new HashMap<>();
    past_mapping.put("type", "nested");
    Map<String, Object> past_nested_mapping = new HashMap<>();
    past_mapping.put(CommonName.PROPERTIES, past_nested_mapping);
    past_nested_mapping.put("data", Collections.singletonMap("type", "double"));
    past_nested_mapping.put("feature_id", Collections.singletonMap("type", "keyword"));
    mappings.put(AnomalyResult.PAST_VALUES_FIELD, past_mapping);
    Map<String, Object> attribution_mapping = new HashMap<>();
    past_mapping.put("type", "nested");
    Map<String, Object> attribution_nested_mapping = new HashMap<>();
    attribution_mapping.put(CommonName.PROPERTIES, attribution_nested_mapping);
    attribution_nested_mapping.put("data", Collections.singletonMap("type", "double"));
    attribution_nested_mapping.put("feature_id", Collections.singletonMap("type", "keyword"));
    mappings.put(AnomalyResult.RELEVANT_ATTRIBUTION_FIELD, attribution_mapping);
    IndexMetadata indexMetadata1 = new IndexMetadata.Builder(customIndexName).settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)).putMapping(new MappingMetadata("type1", Collections.singletonMap(CommonName.PROPERTIES, mappings))).build();
    when(clusterService.state()).thenReturn(ClusterState.builder(clusterName).metadata(Metadata.builder().put(indexMetadata1, true).build()).build());
    assertTrue(!adIndices.isValidResultIndexMapping(customIndexName));
}
Also used : HashMap(java.util.HashMap) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata)

Aggregations

IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)403 ClusterState (org.opensearch.cluster.ClusterState)144 Metadata (org.opensearch.cluster.metadata.Metadata)126 Settings (org.opensearch.common.settings.Settings)125 IndexSettings (org.opensearch.index.IndexSettings)87 Index (org.opensearch.index.Index)80 ShardRouting (org.opensearch.cluster.routing.ShardRouting)65 ShardId (org.opensearch.index.shard.ShardId)61 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)53 IOException (java.io.IOException)49 ArrayList (java.util.ArrayList)49 HashMap (java.util.HashMap)49 HashSet (java.util.HashSet)48 Matchers.containsString (org.hamcrest.Matchers.containsString)47 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)46 RoutingTable (org.opensearch.cluster.routing.RoutingTable)46 Map (java.util.Map)42 ClusterService (org.opensearch.cluster.service.ClusterService)40 List (java.util.List)38 ActionListener (org.opensearch.action.ActionListener)35