Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit a deprecation warning when a typed index template is triggered. #37741

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public ImmutableOpenMap<String, AliasMetaData> getAliases() {
return this.aliases;
}

public boolean isTypeless() {
return mappings.size() == 0 ||
(mappings.size()) == 1 && mappings.containsKey(MapperService.SINGLE_MAPPING_NAME);
}

public static Builder builder(String name) {
return new Builder(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package org.elasticsearch.cluster.metadata;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ResourceAlreadyExistsException;
Expand Down Expand Up @@ -53,6 +53,7 @@
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -100,6 +101,12 @@
*/
public class MetaDataCreateIndexService {
private static final Logger logger = LogManager.getLogger(MetaDataCreateIndexService.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
LogManager.getLogger(MetaDataCreateIndexService.class));

static final String TYPES_DEPRECATION_MESSAGE = "[types removal] During index creation, an index " +
"template was triggered that contains a custom type. Index templates should no longer include a " +
"type name in the mappings, as types are deprecated and will be removed in the next major version.";

public static final int MAX_INDEX_NAME_BYTES = 255;

Expand Down Expand Up @@ -312,6 +319,12 @@ public ClusterState execute(ClusterState currentState) throws Exception {
if (recoverFromIndex == null) {
// apply templates, merging the mappings into the request mapping if exists
for (IndexTemplateMetaData template : templates) {

if (!template.isTypeless()) {
deprecationLogger.deprecatedAndMaybeLog("trigger_template_with_types",
TYPES_DEPRECATION_MESSAGE);
}

templateNames.add(template.getName());
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
String mappingString = cursor.value.string();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.util.function.Supplier;

import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
import static org.elasticsearch.cluster.metadata.MetaDataCreateIndexService.TYPES_DEPRECATION_MESSAGE;
import static org.elasticsearch.test.hamcrest.CollectionAssertions.hasAllKeys;
import static org.elasticsearch.test.hamcrest.CollectionAssertions.hasKey;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -127,15 +128,15 @@ public void testMatchTemplates() throws Exception {
public void testApplyDataFromTemplate() throws Exception {
addMatchingTemplate(builder -> builder
.putAlias(AliasMetaData.builder("alias1"))
.putMapping("mapping1", createMapping())
.putMapping("_doc", createMapping())
.settings(Settings.builder().put("key1", "value1"))
);

final ClusterState result = executeTask();

assertThat(result.metaData().index("test").getAliases(), hasKey("alias1"));
assertThat(result.metaData().index("test").getSettings().get("key1"), equalTo("value1"));
assertThat(getMappingsFromResponse(), Matchers.hasKey("mapping1"));
assertThat(getMappingsFromResponse(), Matchers.hasKey("_doc"));
}

public void testApplyDataFromRequest() throws Exception {
Expand All @@ -162,7 +163,7 @@ public void testRequestDataHavePriorityOverTemplateData() throws Exception {

addMatchingTemplate(builder -> builder
.putAlias(AliasMetaData.builder("alias1").searchRouting("fromTpl").build())
.putMapping("mapping1", tplMapping)
.putMapping("_doc", tplMapping)
.settings(Settings.builder().put("key1", "tplValue"))
);

Expand Down Expand Up @@ -235,6 +236,17 @@ public void testTemplateOrder2() throws Exception {
assertThat(result.metaData().index("test").getAliases().get("alias1").getSearchRouting(), equalTo("3"));
}

public void testTypesDeprecation() throws Exception {
addMatchingTemplate(builder -> builder
.putMapping("custom_type", "{}")
.settings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 10))
);

executeTask();
assertThat(getMappingsFromResponse(), Matchers.hasKey("custom_type"));
assertWarnings(TYPES_DEPRECATION_MESSAGE);
}

public void testRequestStateOpen() throws Exception {
when(request.state()).thenReturn(IndexMetaData.State.OPEN);

Expand Down