Skip to content

Add a cluster deprecation check for index templates containing multiple types #72540

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

Merged
Merged
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 @@ -12,6 +12,8 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.IndexSettings;
Expand Down Expand Up @@ -167,4 +169,24 @@ static DeprecationIssue checkPollIntervalTooLow(ClusterState state) {
}
return null;
}

static DeprecationIssue checkTemplatesWithMultipleTypes(ClusterState state) {
Set<String> templatesWithMultipleTypes = new HashSet<>();
state.getMetadata().getTemplates().forEach((templateCursor) -> {
String templateName = templateCursor.key;
ImmutableOpenMap<String, CompressedXContent> mappings = templateCursor.value.mappings();
if (mappings != null && mappings.size() > 1) {
templatesWithMultipleTypes.add(templateName);
}
});
if (templatesWithMultipleTypes.isEmpty()) {
return null;
}
return new DeprecationIssue(DeprecationIssue.Level.CRITICAL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my knowledge, does CRITICAL mean we'll prevent the upgrade from proceeding?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I've asked in the core-features channel and will try to find out!

"Some index templates contain multiple mapping types",
"https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html",
"Index templates " + templatesWithMultipleTypes
+ " define multiple types and so will cause errors when used in index creation"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ private DeprecationChecks() {
ClusterDeprecationChecks::checkUserAgentPipelines,
ClusterDeprecationChecks::checkTemplatesWithTooManyFields,
ClusterDeprecationChecks::checkPollIntervalTooLow,
ClusterDeprecationChecks::checkTemplatesWithFieldNamesDisabled
ClusterDeprecationChecks::checkTemplatesWithFieldNamesDisabled,
ClusterDeprecationChecks::checkTemplatesWithMultipleTypes
));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -21,7 +22,6 @@
import org.elasticsearch.ingest.IngestService;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
import org.hamcrest.Matchers;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -32,6 +32,8 @@
import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING;
import static org.elasticsearch.xpack.deprecation.DeprecationChecks.CLUSTER_SETTINGS_CHECKS;
import static org.elasticsearch.xpack.deprecation.IndexDeprecationChecksTests.addRandomFields;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

public class ClusterDeprecationChecksTests extends ESTestCase {

Expand Down Expand Up @@ -272,7 +274,41 @@ public void testPollIntervalTooLow() {
.metadata(okMetadata)
.build();
List<DeprecationIssue> noIssues = DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(okState));
assertThat(noIssues, Matchers.hasSize(0));
assertThat(noIssues, hasSize(0));
}
}

public void testIndexTemplatesWithMultipleTypes() throws IOException {

IndexTemplateMetadata multipleTypes = IndexTemplateMetadata.builder("multiple-types")
.patterns(Collections.singletonList("foo"))
.putMapping("type1", "{\"type1\":{}}")
.putMapping("type2", "{\"type2\":{}}")
.build();
IndexTemplateMetadata singleType = IndexTemplateMetadata.builder("single-type")
.patterns(Collections.singletonList("foo"))
.putMapping("type1", "{\"type1\":{}}")
.build();
ImmutableOpenMap<String, IndexTemplateMetadata> templates = ImmutableOpenMap.<String, IndexTemplateMetadata>builder()
.fPut("multiple-types", multipleTypes)
.fPut("single-type", singleType)
.build();
Metadata badMetadata = Metadata.builder()
.templates(templates)
.build();
ClusterState badState = ClusterState.builder(new ClusterName("test")).metadata(badMetadata).build();
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(badState));
assertThat(issues, hasSize(1));
assertThat(issues.get(0).getDetails(),
equalTo("Index templates [multiple-types] define multiple types and so will cause errors when used in index creation"));

Metadata goodMetadata = Metadata.builder()
.templates(ImmutableOpenMap.<String, IndexTemplateMetadata>builder().fPut("single-type", singleType).build())
.build();
ClusterState goodState = ClusterState.builder(new ClusterName("test")).metadata(goodMetadata).build();
assertThat(
DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(goodState)),
hasSize(0)
);
}
}