Skip to content

Commit 8ee2ee5

Browse files
committed
Revert "Clean up IndexTemplateMetaData xcontent serialization (#54003)"
This reverts commit 04c21af.
1 parent c1d8341 commit 8ee2ee5

File tree

9 files changed

+191
-125
lines changed

9 files changed

+191
-125
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/template/get/GetIndexTemplatesResponse.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
2323
import org.elasticsearch.common.io.stream.StreamInput;
2424
import org.elasticsearch.common.io.stream.StreamOutput;
25+
import org.elasticsearch.common.xcontent.ToXContent;
2526
import org.elasticsearch.common.xcontent.ToXContentObject;
2627
import org.elasticsearch.common.xcontent.XContentBuilder;
2728

@@ -30,6 +31,8 @@
3031
import java.util.List;
3132
import java.util.Objects;
3233

34+
import static java.util.Collections.singletonMap;
35+
3336
public class GetIndexTemplatesResponse extends ActionResponse implements ToXContentObject {
3437

3538
private final List<IndexTemplateMetaData> indexTemplates;
@@ -74,9 +77,11 @@ public int hashCode() {
7477

7578
@Override
7679
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
80+
params = new ToXContent.DelegatingMapParams(singletonMap("reduce_mappings", "true"), params);
81+
7782
builder.startObject();
7883
for (IndexTemplateMetaData indexTemplateMetaData : getIndexTemplates()) {
79-
indexTemplateMetaData.toXContent(builder, params);
84+
IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params);
8085
}
8186
builder.endObject();
8287
return builder;

server/src/main/java/org/elasticsearch/cluster/ClusterState.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import org.elasticsearch.common.io.stream.StreamOutput;
5454
import org.elasticsearch.common.io.stream.VersionedNamedWriteable;
5555
import org.elasticsearch.common.settings.Settings;
56-
import org.elasticsearch.common.xcontent.ToXContent;
5756
import org.elasticsearch.common.xcontent.ToXContentFragment;
5857
import org.elasticsearch.common.xcontent.XContentBuilder;
5958
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -427,10 +426,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
427426
coordinationMetaData().toXContent(builder, params);
428427
builder.endObject();
429428

430-
ToXContent.Params templateParams = new DelegatingMapParams(Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true"), params);
431429
builder.startObject("templates");
432430
for (ObjectCursor<IndexTemplateMetaData> cursor : metaData().templates().values()) {
433-
cursor.value.toXContent(builder, templateParams);
431+
IndexTemplateMetaData templateMetaData = cursor.value;
432+
IndexTemplateMetaData.Builder.toXContentWithTypes(templateMetaData, builder, params);
434433
}
435434
builder.endObject();
436435

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

Lines changed: 163 additions & 90 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,11 +1404,9 @@ public static void toXContent(MetaData metaData, XContentBuilder builder, ToXCon
14041404
builder.endObject();
14051405
}
14061406

1407-
ToXContent.Params typedParams
1408-
= new ToXContent.DelegatingMapParams(Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true"), params);
14091407
builder.startObject("templates");
14101408
for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) {
1411-
cursor.value.toXContent(builder, typedParams);
1409+
IndexTemplateMetaData.Builder.toXContentWithTypes(cursor.value, builder, params);
14121410
}
14131411
builder.endObject();
14141412

@@ -1473,7 +1471,7 @@ public static MetaData fromXContent(XContentParser parser, boolean preserveUnkno
14731471
builder.hashesOfConsistentSettings(parser.mapStrings());
14741472
} else if ("templates".equals(currentFieldName)) {
14751473
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
1476-
builder.put(IndexTemplateMetaData.fromXContent(parser, parser.currentName()));
1474+
builder.put(IndexTemplateMetaData.Builder.fromXContent(parser, parser.currentName()));
14771475
}
14781476
} else {
14791477
try {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import java.util.concurrent.atomic.AtomicInteger;
5858
import java.util.function.UnaryOperator;
5959

60+
import static java.util.Collections.singletonMap;
61+
6062
/**
6163
* Upgrades Templates on behalf of installed {@link Plugin}s when a node joins the cluster
6264
*/
@@ -246,12 +248,14 @@ Optional<Tuple<Map<String, BytesReference>, Set<String>>> calculateTemplateChang
246248
return Optional.empty();
247249
}
248250

249-
private static final ToXContent.Params PARAMS
250-
= new ToXContent.MapParams(Map.of(IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
251+
private static final ToXContent.Params PARAMS = new ToXContent.MapParams(singletonMap("reduce_mappings", "true"));
251252

252253
private BytesReference toBytesReference(IndexTemplateMetaData templateMetaData) {
253254
try {
254-
return XContentHelper.toXContent(templateMetaData, XContentType.JSON, PARAMS, false);
255+
return XContentHelper.toXContent((builder, params) -> {
256+
IndexTemplateMetaData.Builder.toInnerXContentWithTypes(templateMetaData, builder, params);
257+
return builder;
258+
}, XContentType.JSON, PARAMS, false);
255259
} catch (IOException ex) {
256260
throw new IllegalStateException("Cannot serialize template [" + templateMetaData.getName() + "]", ex);
257261
}

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import java.util.Arrays;
3636
import java.util.Collections;
37-
import java.util.Map;
3837

3938
import static org.hamcrest.CoreMatchers.equalTo;
4039
import static org.hamcrest.Matchers.contains;
@@ -55,23 +54,21 @@ public void testIndexTemplateMetaDataXContentRoundTrip() throws Exception {
5554
final IndexTemplateMetaData indexTemplateMetaData;
5655
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
5756
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateBytes, XContentType.JSON)) {
58-
indexTemplateMetaData = IndexTemplateMetaData.fromXContent(parser, "test");
57+
indexTemplateMetaData = IndexTemplateMetaData.Builder.fromXContent(parser, "test");
5958
}
6059

6160
final BytesReference templateBytesRoundTrip;
62-
final ToXContent.Params params = new ToXContent.MapParams(
63-
Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true", IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
6461
try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent)) {
6562
builder.startObject();
66-
indexTemplateMetaData.toXContent(builder, params);
63+
IndexTemplateMetaData.Builder.toXContentWithTypes(indexTemplateMetaData, builder, ToXContent.EMPTY_PARAMS);
6764
builder.endObject();
6865
templateBytesRoundTrip = BytesReference.bytes(builder);
6966
}
7067

7168
final IndexTemplateMetaData indexTemplateMetaDataRoundTrip;
7269
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
7370
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateBytesRoundTrip, XContentType.JSON)) {
74-
indexTemplateMetaDataRoundTrip = IndexTemplateMetaData.fromXContent(parser, "test");
71+
indexTemplateMetaDataRoundTrip = IndexTemplateMetaData.Builder.fromXContent(parser, "test");
7572
}
7673
assertThat(indexTemplateMetaData, equalTo(indexTemplateMetaDataRoundTrip));
7774
}
@@ -100,7 +97,7 @@ public void testValidateInvalidIndexPatterns() throws Exception {
10097
XContentHelper.createParser(NamedXContentRegistry.EMPTY,
10198
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithEmptyPattern), XContentType.JSON)) {
10299
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
103-
() -> IndexTemplateMetaData.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
100+
() -> IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
104101
assertThat(ex.getMessage(), equalTo("Index patterns must not be null or empty; got []"));
105102
}
106103

@@ -115,7 +112,7 @@ DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithEmpty
115112
XContentHelper.createParser(NamedXContentRegistry.EMPTY,
116113
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithoutPattern), XContentType.JSON)) {
117114
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
118-
() -> IndexTemplateMetaData.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
115+
() -> IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
119116
assertThat(ex.getMessage(), equalTo("Index patterns must not be null or empty; got null"));
120117
}
121118
}
@@ -125,7 +122,7 @@ public void testParseTemplateWithAliases() throws Exception {
125122
try (XContentParser parser =
126123
XContentHelper.createParser(NamedXContentRegistry.EMPTY,
127124
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateInJSON), XContentType.JSON)) {
128-
IndexTemplateMetaData template = IndexTemplateMetaData.fromXContent(parser, randomAlphaOfLengthBetween(1, 100));
125+
IndexTemplateMetaData template = IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100));
129126
assertThat(template.aliases().containsKey("log"), equalTo(true));
130127
assertThat(template.patterns(), contains("pattern-1"));
131128
}
@@ -160,14 +157,12 @@ public void testFromToXContent() throws Exception {
160157
templateBuilder.putMapping("doc", "{\"doc\":{\"properties\":{\"type\":\"text\"}}}");
161158
}
162159
IndexTemplateMetaData template = templateBuilder.build();
163-
final ToXContent.Params params = new ToXContent.MapParams(
164-
Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true", IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
165160
XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.JSON.xContent()));
166161
builder.startObject();
167-
template.toXContent(builder, params);
162+
IndexTemplateMetaData.Builder.toXContentWithTypes(template, builder, ToXContent.EMPTY_PARAMS);
168163
builder.endObject();
169164
try (XContentParser parser = createParser(shuffleXContent(builder))) {
170-
IndexTemplateMetaData parsed = IndexTemplateMetaData.fromXContent(parser, templateName);
165+
IndexTemplateMetaData parsed = IndexTemplateMetaData.Builder.fromXContent(parser, templateName);
171166
assertThat(parsed, equalTo(template));
172167
}
173168
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/TemplateUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void loadTemplateIntoMap(String resource, Map<String, IndexTemplat
4747
final String template = loadTemplate(resource, version, versionProperty);
4848
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
4949
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template)) {
50-
map.put(templateName, IndexTemplateMetaData.fromXContent(parser, templateName));
50+
map.put(templateName, IndexTemplateMetaData.Builder.fromXContent(parser, templateName));
5151
} catch (IOException e) {
5252
// TODO: should we handle this with a thrown exception?
5353
logger.error("Error loading template [{}] as part of metadata upgrading", templateName);

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResource.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.common.unit.TimeValue;
1919
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2020
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
21-
import org.elasticsearch.common.xcontent.ToXContent;
2221
import org.elasticsearch.common.xcontent.XContentBuilder;
2322
import org.elasticsearch.common.xcontent.XContentFactory;
2423
import org.elasticsearch.common.xcontent.XContentParser;
@@ -108,13 +107,10 @@ protected void doPublish(final RestClient client, final ActionListener<Boolean>
108107
HttpEntity templateToHttpEntity() {
109108
// the internal representation of a template has type nested under mappings.
110109
// this uses xContent to help remove the type before sending to the remote cluster
111-
ToXContent.Params params = new ToXContent.MapParams(Map.of(IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
112110
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
113111
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template.get())) {
114112
XContentBuilder builder = JsonXContent.contentBuilder();
115-
builder.startObject();
116-
IndexTemplateMetaData.fromXContent(parser, templateName).toXContent(builder, params);
117-
builder.endObject();
113+
IndexTemplateMetaData.Builder.removeType(IndexTemplateMetaData.Builder.fromXContent(parser, templateName), builder);
118114
return new StringEntity(BytesReference.bytes(builder).utf8ToString(), ContentType.APPLICATION_JSON);
119115
} catch (IOException ex) {
120116
throw new IllegalStateException("Cannot serialize template [" + templateName + "] for monitoring export", ex);

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.common.util.concurrent.ThreadContext;
2727
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2828
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
29-
import org.elasticsearch.common.xcontent.ToXContent;
3029
import org.elasticsearch.common.xcontent.XContentBuilder;
3130
import org.elasticsearch.common.xcontent.XContentFactory;
3231
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -972,10 +971,7 @@ private String getExternalTemplateRepresentation(String internalRepresentation)
972971
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
973972
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, internalRepresentation)) {
974973
XContentBuilder builder = JsonXContent.contentBuilder();
975-
ToXContent.Params params = new ToXContent.MapParams(Map.of(IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
976-
builder.startObject();
977-
IndexTemplateMetaData.fromXContent(parser, "").toXContent(builder, params);
978-
builder.endObject();
974+
IndexTemplateMetaData.Builder.removeType(IndexTemplateMetaData.Builder.fromXContent(parser, ""), builder);
979975
return BytesReference.bytes(builder).utf8ToString();
980976
}
981977
}

0 commit comments

Comments
 (0)