/* * 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. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.cluster.metadata; import org.opensearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest; import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.ClusterStateTaskExecutor; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.compress.CompressedXContent; import org.opensearch.core.index.Index; import org.opensearch.index.IndexService; import org.opensearch.plugins.Plugin; import org.opensearch.test.OpenSearchSingleNodeTestCase; import org.opensearch.test.InternalSettingsPlugin; import java.util.Collection; import java.util.Collections; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; public class MetadataMappingServiceTests extends OpenSearchSingleNodeTestCase { @Override protected Collection> getPlugins() { return Collections.singleton(InternalSettingsPlugin.class); } public void testMappingClusterStateUpdateDoesntChangeExistingIndices() throws Exception { final IndexService indexService = createIndex("test", client().admin().indices().prepareCreate("test").setMapping()); final CompressedXContent currentMapping = indexService.mapperService().documentMapper().mappingSource(); final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class); // TODO - it will be nice to get a random mapping generator final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest( "{ \"properties\": { \"field\": { \"type\": \"text\" }}}" ); request.indices(new Index[] { indexService.index() }); final ClusterStateTaskExecutor.ClusterTasksResult result = mappingService.putMappingExecutor .execute(clusterService.state(), Collections.singletonList(request)); // the task completed successfully assertThat(result.executionResults.size(), equalTo(1)); assertTrue(result.executionResults.values().iterator().next().isSuccess()); // the task really was a mapping update assertThat( indexService.mapperService().documentMapper().mappingSource(), not(equalTo(result.resultingState.metadata().index("test").mapping().source())) ); // since we never committed the cluster state update, the in-memory state is unchanged assertThat(indexService.mapperService().documentMapper().mappingSource(), equalTo(currentMapping)); } public void testClusterStateIsNotChangedWithIdenticalMappings() throws Exception { createIndex("test", client().admin().indices().prepareCreate("test")); final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class); final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest( "{ \"properties\" { \"field\": { \"type\": \"text\" }}}" ); ClusterState result = mappingService.putMappingExecutor.execute( clusterService.state(), Collections.singletonList(request) ).resultingState; assertFalse(result != clusterService.state()); ClusterState result2 = mappingService.putMappingExecutor.execute(result, Collections.singletonList(request)).resultingState; assertSame(result, result2); } public void testMappingVersion() throws Exception { final IndexService indexService = createIndex("test", client().admin().indices().prepareCreate("test")); final long previousVersion = indexService.getMetadata().getMappingVersion(); final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class); final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest( "{ \"properties\": { \"field\": { \"type\": \"text\" }}}" ); request.indices(new Index[] { indexService.index() }); final ClusterStateTaskExecutor.ClusterTasksResult result = mappingService.putMappingExecutor .execute(clusterService.state(), Collections.singletonList(request)); assertThat(result.executionResults.size(), equalTo(1)); assertTrue(result.executionResults.values().iterator().next().isSuccess()); assertThat(result.resultingState.metadata().index("test").getMappingVersion(), equalTo(1 + previousVersion)); } public void testMappingVersionUnchanged() throws Exception { final IndexService indexService = createIndex("test", client().admin().indices().prepareCreate("test").setMapping()); final long previousVersion = indexService.getMetadata().getMappingVersion(); final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class); final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest("{ \"properties\": {}}"); request.indices(new Index[] { indexService.index() }); final ClusterStateTaskExecutor.ClusterTasksResult result = mappingService.putMappingExecutor .execute(clusterService.state(), Collections.singletonList(request)); assertThat(result.executionResults.size(), equalTo(1)); assertTrue(result.executionResults.values().iterator().next().isSuccess()); assertThat(result.resultingState.metadata().index("test").getMappingVersion(), equalTo(previousVersion)); } }