Skip to content

Adding a deprecation info api check for custom types in templates #80676

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 3 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 @@ -22,6 +22,7 @@
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.FieldNamesFieldMapper;
import org.elasticsearch.index.mapper.GeoShapeFieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.ingest.IngestService;
import org.elasticsearch.ingest.PipelineConfiguration;
import org.elasticsearch.xcontent.XContentType;
Expand All @@ -37,6 +38,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -210,28 +212,56 @@ static DeprecationIssue checkPollIntervalTooLow(ClusterState state) {
return null;
}

static DeprecationIssue checkTemplatesWithMultipleTypes(ClusterState state) {
Set<String> templatesWithMultipleTypes = new HashSet<>();
static DeprecationIssue checkTemplatesWithCustomAndMultipleTypes(ClusterState state) {
Set<String> templatesWithMultipleTypes = new TreeSet<>();
Set<String> templatesWithCustomTypes = new TreeSet<>();
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 (mappings != null) {
if (mappings.size() > 1) {
templatesWithMultipleTypes.add(templateName);
}
boolean hasCustomType = mappings.stream().anyMatch(mapping -> {
String typeName = mapping.getKey();
return MapperService.SINGLE_MAPPING_NAME.equals(typeName) == false;
});
if (hasCustomType) {
templatesWithCustomTypes.add(templateName);
}
}
});
if (templatesWithMultipleTypes.isEmpty()) {
return null;
final DeprecationIssue deprecationIssue;
if (templatesWithMultipleTypes.isEmpty() && templatesWithCustomTypes.isEmpty()) {
deprecationIssue = null;
} else if (templatesWithMultipleTypes.isEmpty()) {
deprecationIssue = new DeprecationIssue(
DeprecationIssue.Level.CRITICAL,
"Custom mapping types in index templates are deprecated",
"https://ela.st/es-deprecation-7-custom-types",
"Update or remove the following index templates before upgrading to 8.0: "
+ templatesWithCustomTypes
+ ". See https://ela.st/es-deprecation-7-removal-of-types for alternatives to mapping types.",
false,
null
);
} else {
// There were multiple mapping types, so at least one of them had to be a custom type as well
Set<String> allBadTemplates = new TreeSet<>();
allBadTemplates.addAll(templatesWithMultipleTypes);
allBadTemplates.addAll(templatesWithCustomTypes);
deprecationIssue = new DeprecationIssue(
DeprecationIssue.Level.CRITICAL,
"Multiple mapping types and custom mapping types in index templates and indices are deprecated",
"https://ela.st/es-deprecation-7-multiple-types",
"Update or remove the following index templates before upgrading to 8.0: "
+ templatesWithMultipleTypes
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be allBadTemplates instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I'll fix that.

+ ". See https://ela.st/es-deprecation-7-removal-of-types for alternatives to mapping types.",
false,
null
);
}
return new DeprecationIssue(
DeprecationIssue.Level.CRITICAL,
"Multiple mapping types in index templates and indices is not supported",
"https://ela.st/es-deprecation-7-multiple-types",
"Update or remove the following index templates before upgrading to 8.0: "
+ templatesWithMultipleTypes
+ ". See https://ela.st/es-deprecation-7-removal-of-types for alternatives to mapping types.",
false,
null
);
return deprecationIssue;
}

static DeprecationIssue checkClusterRoutingAllocationIncludeRelocationsSetting(final ClusterState clusterState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private DeprecationChecks() {}
ClusterDeprecationChecks::checkTemplatesWithTooManyFields,
ClusterDeprecationChecks::checkPollIntervalTooLow,
ClusterDeprecationChecks::checkTemplatesWithFieldNamesDisabled,
ClusterDeprecationChecks::checkTemplatesWithMultipleTypes,
ClusterDeprecationChecks::checkTemplatesWithCustomAndMultipleTypes,
ClusterDeprecationChecks::checkClusterRoutingAllocationIncludeRelocationsSetting,
ClusterDeprecationChecks::checkGeoShapeTemplates,
ClusterDeprecationChecks::checkSparseVectorTemplates,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public void testIndexTemplatesWithMultipleTypes() throws IOException {
.build();
IndexTemplateMetadata singleType = IndexTemplateMetadata.builder("single-type")
.patterns(Collections.singletonList("foo"))
.putMapping("type1", "{\"type1\":{}}")
.putMapping("_doc", "{\"type1\":{}}")
.build();
ImmutableOpenMap<String, IndexTemplateMetadata> templates = ImmutableOpenMap.<String, IndexTemplateMetadata>builder()
.fPut("multiple-types", multipleTypes)
Expand Down Expand Up @@ -375,6 +375,37 @@ public void testIndexTemplatesWithMultipleTypes() throws IOException {
assertThat(DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(goodState)), hasSize(0));
}

public void testIndexTemplatesWithCustomTypes() throws IOException {
IndexTemplateMetadata template1 = IndexTemplateMetadata.builder("template1")
.patterns(Collections.singletonList("foo"))
.putMapping("type1", "{\"type1\":{}}")
.build();
IndexTemplateMetadata template2 = IndexTemplateMetadata.builder("template2")
.patterns(Collections.singletonList("foo"))
.putMapping("type2", "{\"type2\":{}}")
.build();
IndexTemplateMetadata template3 = IndexTemplateMetadata.builder("template3")
.patterns(Collections.singletonList("foo"))
.putMapping("_doc", "{\"_doc\":{}}")
.build();
ImmutableOpenMap<String, IndexTemplateMetadata> templates = ImmutableOpenMap.<String, IndexTemplateMetadata>builder()
.fPut("template1", template1)
.fPut("template2", template2)
.fPut("template3", template3)
.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(
"Update or remove the following index templates before upgrading to 8.0: [template1, template2]. See "
+ "https://ela.st/es-deprecation-7-removal-of-types for alternatives to mapping types."
)
);
}

public void testClusterRoutingAllocationIncludeRelocationsSetting() {
boolean settingValue = randomBoolean();
String settingKey = CLUSTER_ROUTING_ALLOCATION_INCLUDE_RELOCATIONS_SETTING.getKey();
Expand Down