Search in sources :

Example 41 with AliasMetadata

use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.

the class GetAliasesResponseTests method mutateAliasMetadata.

public static AliasMetadata mutateAliasMetadata(AliasMetadata alias) {
    boolean changeAlias = randomBoolean();
    AliasMetadata.Builder builder = AliasMetadata.builder(changeAlias ? randomAlphaOfLengthBetween(2, 5) : alias.getAlias());
    builder.searchRouting(alias.searchRouting());
    builder.indexRouting(alias.indexRouting());
    builder.filter(alias.filter());
    if (false == changeAlias) {
        if (randomBoolean()) {
            builder.searchRouting(alias.searchRouting() + randomAlphaOfLengthBetween(1, 3));
        } else {
            builder.indexRouting(alias.indexRouting() + randomAlphaOfLengthBetween(1, 3));
        }
    }
    return builder.build();
}
Also used : Builder(org.opensearch.cluster.metadata.AliasMetadata.Builder) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata)

Example 42 with AliasMetadata

use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.

the class SimpleIndexTemplateIT method testMultipleAliasesPrecedence.

public void testMultipleAliasesPrecedence() throws Exception {
    client().admin().indices().preparePutTemplate("template1").setPatterns(Collections.singletonList("*")).setOrder(0).addAlias(new Alias("alias1")).addAlias(new Alias("{index}-alias")).addAlias(new Alias("alias3").filter(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("test")))).addAlias(new Alias("alias4")).get();
    client().admin().indices().preparePutTemplate("template2").setPatterns(Collections.singletonList("te*")).setOrder(1).addAlias(new Alias("alias1").routing("test")).addAlias(new Alias("alias3")).get();
    assertAcked(prepareCreate("test").addAlias(new Alias("test-alias").searchRouting("test-routing")));
    ensureGreen();
    GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().addIndices("test").get();
    assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(4));
    for (AliasMetadata aliasMetadata : getAliasesResponse.getAliases().get("test")) {
        assertThat(aliasMetadata.alias(), anyOf(equalTo("alias1"), equalTo("test-alias"), equalTo("alias3"), equalTo("alias4")));
        if ("alias1".equals(aliasMetadata.alias())) {
            assertThat(aliasMetadata.indexRouting(), equalTo("test"));
            assertThat(aliasMetadata.searchRouting(), equalTo("test"));
        } else if ("alias3".equals(aliasMetadata.alias())) {
            assertThat(aliasMetadata.filter(), nullValue());
        } else if ("test-alias".equals(aliasMetadata.alias())) {
            assertThat(aliasMetadata.indexRouting(), nullValue());
            assertThat(aliasMetadata.searchRouting(), equalTo("test-routing"));
        }
    }
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) GetAliasesResponse(org.opensearch.action.admin.indices.alias.get.GetAliasesResponse) Alias(org.opensearch.action.admin.indices.alias.Alias)

Example 43 with AliasMetadata

use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.

the class TransportSimulateIndexTemplateAction method resolveTemplate.

/**
 * Take a template and index name as well as state where the template exists, and return a final
 * {@link Template} that represents all the resolved Settings, Mappings, and Aliases
 */
public static Template resolveTemplate(final String matchingTemplate, final String indexName, final ClusterState simulatedState, final NamedXContentRegistry xContentRegistry, final IndicesService indicesService, final AliasValidator aliasValidator) throws Exception {
    Settings settings = resolveSettings(simulatedState.metadata(), matchingTemplate);
    List<Map<String, AliasMetadata>> resolvedAliases = MetadataIndexTemplateService.resolveAliases(simulatedState.metadata(), matchingTemplate);
    // create the index with dummy settings in the cluster state so we can parse and validate the aliases
    Settings dummySettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(settings).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    final IndexMetadata indexMetadata = IndexMetadata.builder(indexName).settings(dummySettings).build();
    final ClusterState tempClusterState = ClusterState.builder(simulatedState).metadata(Metadata.builder(simulatedState.metadata()).put(indexMetadata, true).build()).build();
    List<AliasMetadata> aliases = indicesService.withTempIndexService(indexMetadata, tempIndexService -> MetadataCreateIndexService.resolveAndValidateAliases(indexName, Collections.emptySet(), resolvedAliases, tempClusterState.metadata(), aliasValidator, xContentRegistry, // shard id and the current timestamp
    tempIndexService.newQueryShardContext(0, null, () -> 0L, null)));
    Map<String, AliasMetadata> aliasesByName = aliases.stream().collect(Collectors.toMap(AliasMetadata::getAlias, Function.identity()));
    // empty request mapping as the user can't specify any explicit mappings via the simulate api
    List<Map<String, Object>> mappings = MetadataCreateIndexService.collectV2Mappings("{}", simulatedState, matchingTemplate, xContentRegistry, indexName);
    CompressedXContent mergedMapping = indicesService.<CompressedXContent, Exception>withTempIndexService(indexMetadata, tempIndexService -> {
        MapperService mapperService = tempIndexService.mapperService();
        for (Map<String, Object> mapping : mappings) {
            if (mapping.isEmpty() == false) {
                mapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MapperService.MergeReason.INDEX_TEMPLATE);
            }
        }
        DocumentMapper documentMapper = mapperService.documentMapper();
        return documentMapper != null ? documentMapper.mappingSource() : null;
    });
    return new Template(settings, mergedMapping, aliasesByName);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) Template(org.opensearch.cluster.metadata.Template) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) MetadataIndexTemplateService.findV2Template(org.opensearch.cluster.metadata.MetadataIndexTemplateService.findV2Template) CompressedXContent(org.opensearch.common.compress.CompressedXContent) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) HashMap(java.util.HashMap) Map(java.util.Map) MetadataIndexTemplateService.resolveSettings(org.opensearch.cluster.metadata.MetadataIndexTemplateService.resolveSettings) Settings(org.opensearch.common.settings.Settings) MapperService(org.opensearch.index.mapper.MapperService)

Example 44 with AliasMetadata

use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.

the class IndicesClientIT method testGetAliasesNonExistentIndexOrAlias.

public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
    /*
         * This test is quite extensive as this is the only way we can check that we haven't slid out of sync with the server
         * because the server renders the xcontent in a spot that is difficult for us to access in a unit test.
         */
    String alias = "alias";
    String index = "index";
    {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index);
        GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
        assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
        assertThat(getAliasesResponse.getException().getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]"));
    }
    {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest(alias);
        GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
        assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
        assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing"));
        assertThat(getAliasesResponse.getException(), nullValue());
    }
    createIndex(index, Settings.EMPTY);
    client().performRequest(new Request(HttpPut.METHOD_NAME, index + "/_alias/" + alias));
    {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index");
        GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
        assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
        assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
        assertThat(getAliasesResponse.getError(), nullValue());
        assertThat(getAliasesResponse.getException().getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
    }
    {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index").aliases(alias);
        GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
        assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
        assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
        assertThat(getAliasesResponse.getException().getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
    }
    {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("non_existent_index*");
        GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
        assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK));
        assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
        assertThat(getAliasesResponse.getException(), nullValue());
        assertThat(getAliasesResponse.getError(), nullValue());
    }
    {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index).aliases(alias, "non_existent_alias");
        GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
        assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
        assertThat(getAliasesResponse.getError(), equalTo("alias [non_existent_alias] missing"));
        assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
        assertThat(getAliasesResponse.getAliases().get(index).size(), equalTo(1));
        AliasMetadata aliasMetadata = getAliasesResponse.getAliases().get(index).iterator().next();
        assertThat(aliasMetadata, notNullValue());
        assertThat(aliasMetadata.alias(), equalTo(alias));
    /*
            This is the above response in json format:
            {
             "error": "alias [something] missing",
             "status": 404,
             "index": {
               "aliases": {
                 "alias": {}
               }
             }
            }
            */
    }
    {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("non_existent_alias*");
        GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
        assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK));
        assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
    }
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) WriteRequest(org.opensearch.action.support.WriteRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) IndexRequest(org.opensearch.action.index.IndexRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 45 with AliasMetadata

use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.

the class ClusterClientIT method testComponentTemplates.

public void testComponentTemplates() throws Exception {
    String templateName = "my-template";
    Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
    CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
    AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
    Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias));
    ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>());
    PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate);
    AcknowledgedResponse response = execute(putComponentTemplateRequest, highLevelClient().cluster()::putComponentTemplate, highLevelClient().cluster()::putComponentTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName);
    boolean exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
    assertTrue(exist);
    GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName);
    GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync);
    assertThat(getResponse.getComponentTemplates().size(), equalTo(1));
    assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true));
    assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate));
    DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName);
    response = execute(deleteComponentTemplateRequest, highLevelClient().cluster()::deleteComponentTemplate, highLevelClient().cluster()::deleteComponentTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync));
    assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
    exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
    assertFalse(exist);
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ComponentTemplatesExistRequest(org.opensearch.client.indices.ComponentTemplatesExistRequest) GetComponentTemplatesRequest(org.opensearch.client.indices.GetComponentTemplatesRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Template(org.opensearch.cluster.metadata.Template) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) DeleteComponentTemplateRequest(org.opensearch.client.indices.DeleteComponentTemplateRequest) CompressedXContent(org.opensearch.common.compress.CompressedXContent) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) GetComponentTemplatesResponse(org.opensearch.client.indices.GetComponentTemplatesResponse) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings) Settings(org.opensearch.common.settings.Settings) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Aggregations

AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)52 List (java.util.List)21 Settings (org.opensearch.common.settings.Settings)16 ArrayList (java.util.ArrayList)13 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)13 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)12 Template (org.opensearch.cluster.metadata.Template)10 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 MappingMetadata (org.opensearch.cluster.metadata.MappingMetadata)8 Metadata (org.opensearch.cluster.metadata.Metadata)8 CompressedXContent (org.opensearch.common.compress.CompressedXContent)8 IOException (java.io.IOException)7 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)7 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)7 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6 ActionListener (org.opensearch.action.ActionListener)6 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)6