Skip to content

Commit 5d492e8

Browse files
authored
Add a cluster deprecation check for index templates containing multiple types (#72540)
Using an index template that creates multiple mapping types will already throw an error in 7.x. This upgrade check should help filter out any unused templates with multiple types that are still lurking in the cluster state.
1 parent 0e86cf6 commit 5d492e8

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.elasticsearch.cluster.ClusterState;
1313
import org.elasticsearch.cluster.metadata.MappingMetadata;
1414
import org.elasticsearch.common.Strings;
15+
import org.elasticsearch.common.collect.ImmutableOpenMap;
16+
import org.elasticsearch.common.compress.CompressedXContent;
1517
import org.elasticsearch.common.unit.TimeValue;
1618
import org.elasticsearch.common.xcontent.XContentHelper;
1719
import org.elasticsearch.index.IndexSettings;
@@ -167,4 +169,24 @@ static DeprecationIssue checkPollIntervalTooLow(ClusterState state) {
167169
}
168170
return null;
169171
}
172+
173+
static DeprecationIssue checkTemplatesWithMultipleTypes(ClusterState state) {
174+
Set<String> templatesWithMultipleTypes = new HashSet<>();
175+
state.getMetadata().getTemplates().forEach((templateCursor) -> {
176+
String templateName = templateCursor.key;
177+
ImmutableOpenMap<String, CompressedXContent> mappings = templateCursor.value.mappings();
178+
if (mappings != null && mappings.size() > 1) {
179+
templatesWithMultipleTypes.add(templateName);
180+
}
181+
});
182+
if (templatesWithMultipleTypes.isEmpty()) {
183+
return null;
184+
}
185+
return new DeprecationIssue(DeprecationIssue.Level.CRITICAL,
186+
"Some index templates contain multiple mapping types",
187+
"https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html",
188+
"Index templates " + templatesWithMultipleTypes
189+
+ " define multiple types and so will cause errors when used in index creation"
190+
);
191+
}
170192
}

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ private DeprecationChecks() {
3838
ClusterDeprecationChecks::checkUserAgentPipelines,
3939
ClusterDeprecationChecks::checkTemplatesWithTooManyFields,
4040
ClusterDeprecationChecks::checkPollIntervalTooLow,
41-
ClusterDeprecationChecks::checkTemplatesWithFieldNamesDisabled
41+
ClusterDeprecationChecks::checkTemplatesWithFieldNamesDisabled,
42+
ClusterDeprecationChecks::checkTemplatesWithMultipleTypes
4243
));
4344

4445

x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.cluster.metadata.Metadata;
1414
import org.elasticsearch.common.Strings;
1515
import org.elasticsearch.common.bytes.BytesArray;
16+
import org.elasticsearch.common.collect.ImmutableOpenMap;
1617
import org.elasticsearch.common.settings.Settings;
1718
import org.elasticsearch.common.xcontent.XContentBuilder;
1819
import org.elasticsearch.common.xcontent.XContentType;
@@ -21,7 +22,6 @@
2122
import org.elasticsearch.ingest.IngestService;
2223
import org.elasticsearch.test.ESTestCase;
2324
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
24-
import org.hamcrest.Matchers;
2525

2626
import java.io.IOException;
2727
import java.util.Collections;
@@ -32,6 +32,8 @@
3232
import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING;
3333
import static org.elasticsearch.xpack.deprecation.DeprecationChecks.CLUSTER_SETTINGS_CHECKS;
3434
import static org.elasticsearch.xpack.deprecation.IndexDeprecationChecksTests.addRandomFields;
35+
import static org.hamcrest.Matchers.equalTo;
36+
import static org.hamcrest.Matchers.hasSize;
3537

3638
public class ClusterDeprecationChecksTests extends ESTestCase {
3739

@@ -272,7 +274,41 @@ public void testPollIntervalTooLow() {
272274
.metadata(okMetadata)
273275
.build();
274276
List<DeprecationIssue> noIssues = DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(okState));
275-
assertThat(noIssues, Matchers.hasSize(0));
277+
assertThat(noIssues, hasSize(0));
276278
}
277279
}
280+
281+
public void testIndexTemplatesWithMultipleTypes() throws IOException {
282+
283+
IndexTemplateMetadata multipleTypes = IndexTemplateMetadata.builder("multiple-types")
284+
.patterns(Collections.singletonList("foo"))
285+
.putMapping("type1", "{\"type1\":{}}")
286+
.putMapping("type2", "{\"type2\":{}}")
287+
.build();
288+
IndexTemplateMetadata singleType = IndexTemplateMetadata.builder("single-type")
289+
.patterns(Collections.singletonList("foo"))
290+
.putMapping("type1", "{\"type1\":{}}")
291+
.build();
292+
ImmutableOpenMap<String, IndexTemplateMetadata> templates = ImmutableOpenMap.<String, IndexTemplateMetadata>builder()
293+
.fPut("multiple-types", multipleTypes)
294+
.fPut("single-type", singleType)
295+
.build();
296+
Metadata badMetadata = Metadata.builder()
297+
.templates(templates)
298+
.build();
299+
ClusterState badState = ClusterState.builder(new ClusterName("test")).metadata(badMetadata).build();
300+
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(badState));
301+
assertThat(issues, hasSize(1));
302+
assertThat(issues.get(0).getDetails(),
303+
equalTo("Index templates [multiple-types] define multiple types and so will cause errors when used in index creation"));
304+
305+
Metadata goodMetadata = Metadata.builder()
306+
.templates(ImmutableOpenMap.<String, IndexTemplateMetadata>builder().fPut("single-type", singleType).build())
307+
.build();
308+
ClusterState goodState = ClusterState.builder(new ClusterName("test")).metadata(goodMetadata).build();
309+
assertThat(
310+
DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(goodState)),
311+
hasSize(0)
312+
);
313+
}
278314
}

0 commit comments

Comments
 (0)