Skip to content

Commit 68c3a1e

Browse files
committed
[ML] Bump anomalies index template version (elastic#138097)
1 parent 49302cf commit 68c3a1e

File tree

4 files changed

+41
-51
lines changed

4 files changed

+41
-51
lines changed

docs/changelog/138097.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 138097
2+
summary: Bump anomalies index template version to install latest
3+
area: Machine Learning
4+
type: bug
5+
issues: []

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistry.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ public class MlIndexTemplateRegistry extends IndexTemplateRegistry {
4040
* the state index has no mappings - its template basically just says
4141
* this - hence there's no mappings version for the state index. Please
4242
* add a comment with a reason each time the base number is incremented.
43-
* 10000001: TODO - reason
43+
*
44+
* 10000001: ".reindexed-v7-ml-anomalies-*" added to ml-anomalies index pattern
4445
*/
45-
public static final int ML_INDEX_TEMPLATE_VERSION = 10000000 + AnomalyDetectorsIndex.RESULTS_INDEX_MAPPINGS_VERSION
46+
public static final int ML_INDEX_TEMPLATE_VERSION = 10000001 + AnomalyDetectorsIndex.RESULTS_INDEX_MAPPINGS_VERSION
4647
+ NotificationsIndex.NOTIFICATIONS_INDEX_MAPPINGS_VERSION + MlStatsIndex.STATS_INDEX_MAPPINGS_VERSION
4748
+ NotificationsIndex.NOTIFICATIONS_INDEX_TEMPLATE_VERSION;
4849

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlMappingsUpgradeIT.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.util.Collection;
22+
import java.util.List;
2223
import java.util.Map;
2324
import java.util.stream.Collectors;
2425
import java.util.stream.Stream;
@@ -71,6 +72,14 @@ public void testMappingsUpgrade() throws Exception {
7172
assertMlLegacyTemplatesDeleted();
7273
IndexMappingTemplateAsserter.assertMlMappingsMatchTemplates(client());
7374
assertNotificationsIndexAliasCreated();
75+
assertBusy(
76+
() -> IndexMappingTemplateAsserter.assertTemplateVersionAndPattern(
77+
client(),
78+
".ml-anomalies-",
79+
10000005,
80+
List.of(".ml-anomalies-*", ".reindexed-v7-ml-anomalies-*")
81+
)
82+
);
7483
break;
7584
default:
7685
throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]");

x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/IndexMappingTemplateAsserter.java

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -95,67 +95,42 @@ public static void assertMlMappingsMatchTemplates(RestClient client) throws Exce
9595
}
9696

9797
/**
98-
* Compares the mappings from the legacy template and the index and asserts
99-
* they are the same. The assertion error message details the differences in
100-
* the mappings.
101-
*
102-
* The Mappings, which are maps of maps, are flattened with the keys built
103-
* from the keys of the sub-maps appended to the parent key.
104-
* This makes diffing the 2 maps easier and diffs more comprehensible.
105-
*
106-
* The _meta field is not compared as it contains version numbers that
107-
* change even when the mappings don't.
108-
*
109-
* Mistakes happen and some indices may be stuck with the incorrect mappings
110-
* that cannot be fixed without re-index. In this case use the {@code exceptions}
111-
* parameter to filter out fields in the index mapping that are not in the
112-
* template. Each exception should be a '.' separated path to the value
113-
* e.g. {@code properties.analysis.analysis_field.type}.
11498
*
115-
* @param client The rest client to use
116-
* @param templateName The template
117-
* @param indexName The index
118-
* @param notAnErrorIfIndexDoesNotExist The index may or may not have been created from
119-
* the template. If {@code true} then the missing
120-
* index does not cause an error
121-
* @param exceptions List of keys to ignore in the index mappings.
122-
* Each key is a '.' separated path.
123-
* @param allowSystemIndexWarnings Whether deprecation warnings for system index access should be allowed/expected.
99+
* @param client The Rest client
100+
* @param templateName Template
101+
* @param version Expected template version
102+
* @param indexPatterns Expected index patterns
103+
* @throws Exception Assertion
124104
*/
125105
@SuppressWarnings("unchecked")
126-
public static void assertLegacyTemplateMatchesIndexMappings(
127-
RestClient client,
128-
String templateName,
129-
String indexName,
130-
boolean notAnErrorIfIndexDoesNotExist,
131-
Set<String> exceptions,
132-
boolean allowSystemIndexWarnings
133-
) throws Exception {
106+
public static void assertTemplateVersionAndPattern(RestClient client, String templateName, int version, List<String> indexPatterns)
107+
throws Exception {
134108

135109
AtomicReference<Response> templateResponse = new AtomicReference<>();
136110

137111
ESRestTestCase.assertBusy(() -> {
138-
Request getTemplate = new Request("GET", "_template/" + templateName);
112+
Request getTemplate = new Request("GET", "_index_template/" + templateName);
139113
templateResponse.set(client.performRequest(getTemplate));
140114
assertEquals("missing template [" + templateName + "]", 200, templateResponse.get().getStatusLine().getStatusCode());
141115
});
142116

143-
Map<String, Object> templateMappings = (Map<String, Object>) XContentMapValues.extractValue(
144-
ESRestTestCase.entityAsMap(templateResponse.get()),
145-
templateName,
146-
"mappings"
147-
);
148-
assertNotNull(templateMappings);
117+
var responseMap = ESRestTestCase.entityAsMap(templateResponse.get());
118+
Integer templateVersion = ((List<Integer>) XContentMapValues.extractValue(
119+
responseMap,
120+
"index_templates",
121+
"index_template",
122+
"version"
123+
)).getFirst();
124+
assertEquals(version, templateVersion.intValue());
149125

150-
assertTemplateMatchesIndexMappingsCommon(
151-
client,
152-
templateName,
153-
templateMappings,
154-
indexName,
155-
notAnErrorIfIndexDoesNotExist,
156-
exceptions,
157-
allowSystemIndexWarnings
158-
);
126+
var templateIndexPatterns = ((List<String>) XContentMapValues.extractValue(
127+
responseMap,
128+
"index_templates",
129+
"index_template",
130+
"index_patterns"
131+
));
132+
133+
assertEquals(responseMap.toString(), templateIndexPatterns, indexPatterns);
159134
}
160135

161136
/**

0 commit comments

Comments
 (0)