Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/138097.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 138097
summary: Bump anomalies index template version to install latest
area: Machine Learning
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public class MlIndexTemplateRegistry extends IndexTemplateRegistry {
* the state index has no mappings - its template basically just says
* this - hence there's no mappings version for the state index. Please
* add a comment with a reason each time the base number is incremented.
* 10000001: TODO - reason
*
* 10000001: ".reindexed-v7-ml-anomalies-*" added to ml-anomalies index pattern
*/
public static final int ML_INDEX_TEMPLATE_VERSION = 10000000 + AnomalyDetectorsIndex.RESULTS_INDEX_MAPPINGS_VERSION
public static final int ML_INDEX_TEMPLATE_VERSION = 10000001 + AnomalyDetectorsIndex.RESULTS_INDEX_MAPPINGS_VERSION
+ NotificationsIndex.NOTIFICATIONS_INDEX_MAPPINGS_VERSION + MlStatsIndex.STATS_INDEX_MAPPINGS_VERSION
+ NotificationsIndex.NOTIFICATIONS_INDEX_TEMPLATE_VERSION;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -82,6 +83,14 @@ public void testMappingsUpgrade() throws Exception {
assertLegacyIndicesRollover();
assertAnomalyIndicesRollover();
assertNotificationsIndexAliasCreated();
assertBusy(
() -> IndexMappingTemplateAsserter.assertTemplateVersionAndPattern(
client(),
".ml-anomalies-",
10000005,
List.of(".ml-anomalies-*", ".reindexed-v7-ml-anomalies-*")
)
);
break;
default:
throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,67 +95,42 @@ public static void assertMlMappingsMatchTemplates(RestClient client) throws Exce
}

/**
* Compares the mappings from the legacy template and the index and asserts
* they are the same. The assertion error message details the differences in
* the mappings.
*
* The Mappings, which are maps of maps, are flattened with the keys built
* from the keys of the sub-maps appended to the parent key.
* This makes diffing the 2 maps easier and diffs more comprehensible.
*
* The _meta field is not compared as it contains version numbers that
* change even when the mappings don't.
*
* Mistakes happen and some indices may be stuck with the incorrect mappings
* that cannot be fixed without re-index. In this case use the {@code exceptions}
* parameter to filter out fields in the index mapping that are not in the
* template. Each exception should be a '.' separated path to the value
* e.g. {@code properties.analysis.analysis_field.type}.
*
* @param client The rest client to use
* @param templateName The template
* @param indexName The index
* @param notAnErrorIfIndexDoesNotExist The index may or may not have been created from
* the template. If {@code true} then the missing
* index does not cause an error
* @param exceptions List of keys to ignore in the index mappings.
* Each key is a '.' separated path.
* @param allowSystemIndexWarnings Whether deprecation warnings for system index access should be allowed/expected.
* @param client The Rest client
* @param templateName Template
* @param version Expected template version
* @param indexPatterns Expected index patterns
* @throws Exception Assertion
*/
@SuppressWarnings("unchecked")
public static void assertLegacyTemplateMatchesIndexMappings(
RestClient client,
String templateName,
String indexName,
boolean notAnErrorIfIndexDoesNotExist,
Set<String> exceptions,
boolean allowSystemIndexWarnings
) throws Exception {
public static void assertTemplateVersionAndPattern(RestClient client, String templateName, int version, List<String> indexPatterns)
throws Exception {

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

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

Map<String, Object> templateMappings = (Map<String, Object>) XContentMapValues.extractValue(
ESRestTestCase.entityAsMap(templateResponse.get()),
templateName,
"mappings"
);
assertNotNull(templateMappings);
var responseMap = ESRestTestCase.entityAsMap(templateResponse.get());
Integer templateVersion = ((List<Integer>) XContentMapValues.extractValue(
responseMap,
"index_templates",
"index_template",
"version"
)).get(0);
assertEquals(version, templateVersion.intValue());

assertTemplateMatchesIndexMappingsCommon(
client,
templateName,
templateMappings,
indexName,
notAnErrorIfIndexDoesNotExist,
exceptions,
allowSystemIndexWarnings
);
var templateIndexPatterns = ((List<String>) XContentMapValues.extractValue(
responseMap,
"index_templates",
"index_template",
"index_patterns"
));

assertEquals(responseMap.toString(), templateIndexPatterns, indexPatterns);
}

/**
Expand Down