Skip to content

Commit eeb573f

Browse files
committed
Upgrade API: fix excessive logging and unnecessary template updates (#26698)
TemplateUpgradeService might get stuck in repeatedly upgrading templates after upgrade to 5.6.0. This is caused by shuffling mappings definition in the template during template serialization. This commit makes the template serialization consistent. Closes #26673
1 parent 3209bcd commit eeb573f

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.cluster.Diff;
2727
import org.elasticsearch.common.Nullable;
2828
import org.elasticsearch.common.bytes.BytesArray;
29-
import org.elasticsearch.common.bytes.BytesReference;
3029
import org.elasticsearch.common.collect.ImmutableOpenMap;
3130
import org.elasticsearch.common.collect.MapBuilder;
3231
import org.elasticsearch.common.compress.CompressedXContent;
@@ -399,7 +398,7 @@ public static void toInnerXContent(IndexTemplateMetaData indexTemplateMetaData,
399398
builder.startObject("mappings");
400399
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetaData.mappings()) {
401400
byte[] mappingSource = cursor.value.uncompressed();
402-
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(mappingSource), false).v2();
401+
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(mappingSource), true).v2();
403402
if (mapping.size() == 1 && mapping.containsKey(cursor.key)) {
404403
// the type name is the root value, reduce it
405404
mapping = (Map<String, Object>) mapping.get(cursor.key);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ public void clusterChanged(ClusterChangedEvent event) {
123123
lastTemplateMetaData = templates;
124124
Optional<Tuple<Map<String, BytesReference>, Set<String>>> changes = calculateTemplateChanges(templates);
125125
if (changes.isPresent()) {
126-
logger.info("Starting template upgrade to version {}, {} templates will be updated and {} will be removed",
127-
Version.CURRENT,
128-
changes.get().v1().size(),
129-
changes.get().v2().size());
130126
if (updatesInProgress.compareAndSet(0, changes.get().v1().size() + changes.get().v2().size())) {
127+
logger.info("Starting template upgrade to version {}, {} templates will be updated and {} will be removed",
128+
Version.CURRENT,
129+
changes.get().v1().size(),
130+
changes.get().v2().size());
131131
threadPool.generic().execute(() -> updateTemplates(changes.get().v1(), changes.get().v2()));
132132
}
133133
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.cluster.metadata;
20+
21+
import org.elasticsearch.common.bytes.BytesArray;
22+
import org.elasticsearch.common.bytes.BytesReference;
23+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
24+
import org.elasticsearch.common.xcontent.ToXContent;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
import org.elasticsearch.common.xcontent.XContentHelper;
27+
import org.elasticsearch.common.xcontent.XContentParser;
28+
import org.elasticsearch.common.xcontent.XContentType;
29+
import org.elasticsearch.common.xcontent.json.JsonXContent;
30+
import org.elasticsearch.test.ESTestCase;
31+
32+
import static java.util.Collections.singletonMap;
33+
import static org.hamcrest.CoreMatchers.equalTo;
34+
35+
public class IndexTemplateMetaDataTests extends ESTestCase {
36+
37+
public void testIndexTemplateMetaDataXContentRoundTrip() throws Exception {
38+
ToXContent.Params params = new ToXContent.MapParams(singletonMap("reduce_mappings", "true"));
39+
40+
String template = "{\"template\" : [ \".test-*\" ],\"order\" : 1000," +
41+
"\"settings\" : {\"number_of_shards\" : 1,\"number_of_replicas\" : 0}," +
42+
"\"mappings\" : {\"doc\" :" +
43+
"{\"properties\":{\"" +
44+
randomAlphaOfLength(10) + "\":{\"type\":\"text\"},\"" +
45+
randomAlphaOfLength(10) + "\":{\"type\":\"keyword\"}}" +
46+
"}}}";
47+
48+
BytesReference templateBytes = new BytesArray(template);
49+
final IndexTemplateMetaData indexTemplateMetaData;
50+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, templateBytes, XContentType.JSON)) {
51+
indexTemplateMetaData = IndexTemplateMetaData.Builder.fromXContent(parser, "test");
52+
}
53+
54+
final BytesReference templateBytesRoundTrip;
55+
try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent)) {
56+
builder.startObject();
57+
IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params);
58+
builder.endObject();
59+
templateBytesRoundTrip = builder.bytes();
60+
}
61+
62+
final IndexTemplateMetaData indexTemplateMetaDataRoundTrip;
63+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, templateBytesRoundTrip, XContentType.JSON)) {
64+
indexTemplateMetaDataRoundTrip = IndexTemplateMetaData.Builder.fromXContent(parser, "test");
65+
}
66+
assertThat(indexTemplateMetaData, equalTo(indexTemplateMetaDataRoundTrip));
67+
}
68+
69+
}

0 commit comments

Comments
 (0)