24
24
import java .util .stream .Collectors ;
25
25
import java .util .stream .Stream ;
26
26
27
+ import static org .elasticsearch .common .xcontent .support .XContentMapValues .extractValue ;
28
+
27
29
public class MlMappingsUpgradeIT extends AbstractUpgradeTestCase {
28
30
29
31
private static final String JOB_ID = "ml-mappings-upgrade-job" ;
@@ -48,7 +50,9 @@ public void testMappingsUpgrade() throws Exception {
48
50
// We don't know whether the job is on an old or upgraded node, so cannot assert that the mappings have been upgraded
49
51
break ;
50
52
case UPGRADED :
51
- assertUpgradedMappings ();
53
+ assertUpgradedResultsMappings ();
54
+ closeAndReopenTestJob ();
55
+ assertUpgradedConfigMappings ();
52
56
break ;
53
57
default :
54
58
throw new UnsupportedOperationException ("Unknown cluster type [" + CLUSTER_TYPE + "]" );
@@ -77,8 +81,21 @@ private void createAndOpenTestJob() throws IOException {
77
81
assertEquals (200 , response .getStatusLine ().getStatusCode ());
78
82
}
79
83
84
+ // Doing this should force the config index mappings to be upgraded,
85
+ // when the finished time is cleared on reopening the job
86
+ private void closeAndReopenTestJob () throws IOException {
87
+
88
+ Request closeJob = new Request ("POST" , "_ml/anomaly_detectors/" + JOB_ID + "/_close" );
89
+ Response response = client ().performRequest (closeJob );
90
+ assertEquals (200 , response .getStatusLine ().getStatusCode ());
91
+
92
+ Request openJob = new Request ("POST" , "_ml/anomaly_detectors/" + JOB_ID + "/_open" );
93
+ response = client ().performRequest (openJob );
94
+ assertEquals (200 , response .getStatusLine ().getStatusCode ());
95
+ }
96
+
80
97
@ SuppressWarnings ("unchecked" )
81
- private void assertUpgradedMappings () throws Exception {
98
+ private void assertUpgradedResultsMappings () throws Exception {
82
99
83
100
assertBusy (() -> {
84
101
Request getMappings = new Request ("GET" , XPackRestTestHelper .resultsWriteAlias (JOB_ID ) + "/_mappings" );
@@ -97,17 +114,34 @@ private void assertUpgradedMappings() throws Exception {
97
114
}
98
115
}
99
116
assertNotNull (indexLevel );
100
- Map <String , Object > mappingsLevel = (Map <String , Object >) indexLevel .get ("mappings" );
101
- assertNotNull (mappingsLevel );
102
117
103
- Map <String , Object > metaLevel = (Map <String , Object >) mappingsLevel .get ("_meta" );
104
- assertEquals (Collections .singletonMap ("version" , Version .CURRENT .toString ()), metaLevel );
105
- Map <String , Object > propertiesLevel = (Map <String , Object >) mappingsLevel .get ("properties" );
106
- assertNotNull (propertiesLevel );
118
+ assertEquals (Version .CURRENT .toString (), extractValue ("mappings._meta.version" , indexLevel ));
119
+
120
+ // TODO: as the years go by, the field we assert on here should be changed
121
+ // to the most recent field we've added that is NOT of type "keyword"
122
+ assertEquals ("Incorrect type for peak_model_bytes in " + responseLevel , "long" ,
123
+ extractValue ("mappings.properties.model_size_stats.properties.peak_model_bytes.type" , indexLevel ));
124
+ });
125
+ }
126
+
127
+ @ SuppressWarnings ("unchecked" )
128
+ private void assertUpgradedConfigMappings () throws Exception {
129
+
130
+ assertBusy (() -> {
131
+ Request getMappings = new Request ("GET" , ".ml-config/_mappings" );
132
+ Response response = client ().performRequest (getMappings );
133
+
134
+ Map <String , Object > responseLevel = entityAsMap (response );
135
+ assertNotNull (responseLevel );
136
+ Map <String , Object > indexLevel = (Map <String , Object >) responseLevel .get (".ml-config" );
137
+ assertNotNull (indexLevel );
138
+
139
+ assertEquals (Version .CURRENT .toString (), extractValue ("mappings._meta.version" , indexLevel ));
140
+
107
141
// TODO: as the years go by, the field we assert on here should be changed
108
142
// to the most recent field we've added that is NOT of type "keyword"
109
- Map < String , Object > fieldLevel = ( Map < String , Object >) propertiesLevel . get ( "multi_bucket_impact" );
110
- assertEquals ( Collections . singletonMap ( " type" , "double" ), fieldLevel );
143
+ assertEquals ( "Incorrect type for annotations_enabled in " + responseLevel , "boolean" ,
144
+ extractValue ( "mappings.properties.model_plot_config.properties.annotations_enabled. type" , indexLevel ) );
111
145
});
112
146
}
113
147
}
0 commit comments