Skip to content

Commit 0257de8

Browse files
authored
Emit warnings when index templates have multiple mappings (#50982)
Index templates created in the 5x line can still be present in the cluster state through multiple upgrades, and may have more than one mapping defined. 8x will stop supporting templates with multiple mappings, and we should emit deprecation warnings in 7x clusters to give users a chance to update their templates before upgrading.
1 parent 1536c3e commit 0257de8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.carrotsearch.hppc.cursors.ObjectCursor;
2222
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
23+
import org.apache.logging.log4j.LogManager;
2324
import org.elasticsearch.ElasticsearchParseException;
2425
import org.elasticsearch.Version;
2526
import org.elasticsearch.cluster.AbstractDiffable;
@@ -32,6 +33,7 @@
3233
import org.elasticsearch.common.compress.CompressedXContent;
3334
import org.elasticsearch.common.io.stream.StreamInput;
3435
import org.elasticsearch.common.io.stream.StreamOutput;
36+
import org.elasticsearch.common.logging.DeprecationLogger;
3537
import org.elasticsearch.common.settings.Settings;
3638
import org.elasticsearch.common.util.set.Sets;
3739
import org.elasticsearch.common.xcontent.ToXContent;
@@ -51,6 +53,8 @@
5153

5254
public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaData> {
5355

56+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(IndexTemplateMetaData.class));
57+
5458
private final String name;
5559

5660
private final int order;
@@ -97,6 +101,11 @@ public IndexTemplateMetaData(String name, int order, Integer version,
97101
this.patterns = patterns;
98102
this.settings = settings;
99103
this.mappings = mappings;
104+
if (this.mappings.size() > 1) {
105+
deprecationLogger.deprecatedAndMaybeLog("index-templates",
106+
"Index template {} contains multiple typed mappings; templates in 8x will only support a single mapping",
107+
name);
108+
}
100109
this.aliases = aliases;
101110
}
102111

server/src/test/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaDataTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.common.xcontent.json.JsonXContent;
3333
import org.elasticsearch.test.ESTestCase;
3434

35+
import java.io.IOException;
3536
import java.util.Arrays;
3637
import java.util.Collections;
3738

@@ -168,4 +169,15 @@ public void testFromToXContent() throws Exception {
168169
assertThat(parsed, equalTo(template));
169170
}
170171
}
172+
173+
public void testDeprecationWarningsOnMultipleMappings() throws IOException {
174+
IndexTemplateMetaData.Builder builder = IndexTemplateMetaData.builder("my-template");
175+
builder.patterns(Arrays.asList("a", "b"));
176+
builder.putMapping("type1", "{\"type1\":{}}");
177+
builder.putMapping("type2", "{\"type2\":{}}");
178+
builder.build();
179+
180+
assertWarnings("Index template my-template contains multiple typed mappings; " +
181+
"templates in 8x will only support a single mapping");
182+
}
171183
}

0 commit comments

Comments
 (0)