9
9
import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
10
10
11
11
import org .elasticsearch .action .admin .indices .alias .Alias ;
12
+ import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequest ;
13
+ import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequestBuilder ;
12
14
import org .elasticsearch .action .admin .indices .create .CreateIndexAction ;
13
15
import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
16
+ import org .elasticsearch .action .admin .indices .get .GetIndexResponse ;
14
17
import org .elasticsearch .action .index .IndexRequest ;
15
18
import org .elasticsearch .action .index .IndexResponse ;
16
19
import org .elasticsearch .action .search .SearchPhaseExecutionException ;
23
26
import org .elasticsearch .common .collect .ImmutableOpenMap ;
24
27
import org .elasticsearch .common .settings .Settings ;
25
28
import org .elasticsearch .rest .RestStatus ;
26
- import org .elasticsearch .test .ESIntegTestCase ;
27
29
import org .elasticsearch .xpack .core .XPackSettings ;
28
30
import org .elasticsearch .xpack .core .action .SetResetModeActionRequest ;
29
31
import org .elasticsearch .xpack .core .ml .action .SetResetModeAction ;
33
35
import org .elasticsearch .xpack .ml .notifications .AnomalyDetectionAuditor ;
34
36
35
37
import java .util .ArrayList ;
38
+ import java .util .Arrays ;
36
39
import java .util .Collections ;
37
40
import java .util .List ;
38
41
@@ -54,22 +57,99 @@ public void testNotCreatedWhenNoOtherMlIndices() {
54
57
55
58
// Ask a few times to increase the chance of failure if the .ml-annotations index is created when no other ML index exists
56
59
for (int i = 0 ; i < 10 ; ++i ) {
57
- assertFalse (annotationsIndexExists ());
60
+ assertFalse (annotationsIndexExists (AnnotationIndex . LATEST_INDEX_NAME ));
58
61
assertEquals (0 , numberOfAnnotationsAliases ());
59
62
}
60
63
}
61
64
62
65
public void testCreatedWhenAfterOtherMlIndex () throws Exception {
63
- AnomalyDetectionAuditor auditor = new AnomalyDetectionAuditor (client (), getInstanceFromNode (ClusterService .class ));
64
- auditor .info ("whatever" , "blah" );
66
+ // Creating a document in the .ml-notifications-000002 index should cause .ml-annotations
67
+ // to be created, as it should get created as soon as any other ML index exists
68
+ createAnnotation ();
69
+
70
+ assertBusy (() -> {
71
+ assertTrue (annotationsIndexExists (AnnotationIndex .LATEST_INDEX_NAME ));
72
+ assertEquals (2 , numberOfAnnotationsAliases ());
73
+ });
74
+ }
65
75
76
+ public void testReindexing () throws Exception {
66
77
// Creating a document in the .ml-notifications-000002 index should cause .ml-annotations
67
78
// to be created, as it should get created as soon as any other ML index exists
79
+ createAnnotation ();
68
80
69
81
assertBusy (() -> {
70
- assertTrue (annotationsIndexExists ());
82
+ assertTrue (annotationsIndexExists (AnnotationIndex . LATEST_INDEX_NAME ));
71
83
assertEquals (2 , numberOfAnnotationsAliases ());
72
84
});
85
+
86
+ client ().execute (SetUpgradeModeAction .INSTANCE , new SetUpgradeModeAction .Request (true )).actionGet ();
87
+
88
+ String reindexedIndexName = ".reindexed-v7-ml-annotations-6" ;
89
+ createReindexedIndex (reindexedIndexName );
90
+
91
+ IndicesAliasesRequestBuilder indicesAliasesRequestBuilder = client ().admin ()
92
+ .indices ()
93
+ .prepareAliases ()
94
+ .addAliasAction (
95
+ IndicesAliasesRequest .AliasActions .add ().index (reindexedIndexName ).alias (AnnotationIndex .READ_ALIAS_NAME ).isHidden (true )
96
+ )
97
+ .addAliasAction (
98
+ IndicesAliasesRequest .AliasActions .add ().index (reindexedIndexName ).alias (AnnotationIndex .WRITE_ALIAS_NAME ).isHidden (true )
99
+ )
100
+ .addAliasAction (IndicesAliasesRequest .AliasActions .removeIndex ().index (AnnotationIndex .LATEST_INDEX_NAME ))
101
+ .addAliasAction (
102
+ IndicesAliasesRequest .AliasActions .add ().index (reindexedIndexName ).alias (AnnotationIndex .LATEST_INDEX_NAME ).isHidden (true )
103
+ );
104
+
105
+ client ().admin ().indices ().aliases (indicesAliasesRequestBuilder .request ()).actionGet ();
106
+
107
+ client ().execute (SetUpgradeModeAction .INSTANCE , new SetUpgradeModeAction .Request (false )).actionGet ();
108
+
109
+ // Ask a few times to increase the chance of failure if the .ml-annotations index is created when no other ML index exists
110
+ for (int i = 0 ; i < 10 ; ++i ) {
111
+ assertFalse (annotationsIndexExists (AnnotationIndex .LATEST_INDEX_NAME ));
112
+ assertTrue (annotationsIndexExists (reindexedIndexName ));
113
+ // Aliases should be read, write and original name
114
+ assertEquals (3 , numberOfAnnotationsAliases ());
115
+ }
116
+ }
117
+
118
+ public void testReindexingWithLostAliases () throws Exception {
119
+ // Creating a document in the .ml-notifications-000002 index should cause .ml-annotations
120
+ // to be created, as it should get created as soon as any other ML index exists
121
+ createAnnotation ();
122
+
123
+ assertBusy (() -> {
124
+ assertTrue (annotationsIndexExists (AnnotationIndex .LATEST_INDEX_NAME ));
125
+ assertEquals (2 , numberOfAnnotationsAliases ());
126
+ });
127
+
128
+ client ().execute (SetUpgradeModeAction .INSTANCE , new SetUpgradeModeAction .Request (true )).actionGet ();
129
+
130
+ String reindexedIndexName = ".reindexed-v7-ml-annotations-6" ;
131
+ createReindexedIndex (reindexedIndexName );
132
+
133
+ IndicesAliasesRequestBuilder indicesAliasesRequestBuilder = client ().admin ()
134
+ .indices ()
135
+ .prepareAliases ()
136
+ // The difference compared to the standard reindexing test is that the read and write aliases are not correctly set up.
137
+ // The annotations index maintenance code should add them back.
138
+ .addAliasAction (IndicesAliasesRequest .AliasActions .removeIndex ().index (AnnotationIndex .LATEST_INDEX_NAME ))
139
+ .addAliasAction (
140
+ IndicesAliasesRequest .AliasActions .add ().index (reindexedIndexName ).alias (AnnotationIndex .LATEST_INDEX_NAME ).isHidden (true )
141
+ );
142
+
143
+ client ().admin ().indices ().aliases (indicesAliasesRequestBuilder .request ()).actionGet ();
144
+
145
+ client ().execute (SetUpgradeModeAction .INSTANCE , new SetUpgradeModeAction .Request (false )).actionGet ();
146
+
147
+ assertBusy (() -> {
148
+ assertFalse (annotationsIndexExists (AnnotationIndex .LATEST_INDEX_NAME ));
149
+ assertTrue (annotationsIndexExists (reindexedIndexName ));
150
+ // Aliases should be read, write and original name
151
+ assertEquals (3 , numberOfAnnotationsAliases ());
152
+ });
73
153
}
74
154
75
155
public void testAliasesMovedFromOldToNew () throws Exception {
@@ -91,7 +171,7 @@ public void testAliasesMovedFromOldToNew() throws Exception {
91
171
// When this happens the read alias should be changed to cover both indices, and the write alias should be
92
172
// switched to only point at the new index.
93
173
assertBusy (() -> {
94
- assertTrue (annotationsIndexExists ());
174
+ assertTrue (annotationsIndexExists (AnnotationIndex . LATEST_INDEX_NAME ));
95
175
ImmutableOpenMap <String , List <AliasMetadata >> aliases = client ().admin ()
96
176
.indices ()
97
177
.prepareGetAliases (AnnotationIndex .READ_ALIAS_NAME , AnnotationIndex .WRITE_ALIAS_NAME )
@@ -124,11 +204,9 @@ public void testNotCreatedWhenAfterOtherMlIndexAndUpgradeInProgress() throws Exc
124
204
client ().execute (SetUpgradeModeAction .INSTANCE , new SetUpgradeModeAction .Request (true )).actionGet ();
125
205
126
206
try {
127
- AnomalyDetectionAuditor auditor = new AnomalyDetectionAuditor (client (), getInstanceFromNode (ClusterService .class ));
128
- auditor .info ("whatever" , "blah" );
129
-
130
207
// Creating a document in the .ml-notifications-000002 index would normally cause .ml-annotations
131
208
// to be created, but in this case it shouldn't as we're doing an upgrade
209
+ createAnnotation ();
132
210
133
211
assertBusy (() -> {
134
212
try {
@@ -137,7 +215,7 @@ public void testNotCreatedWhenAfterOtherMlIndexAndUpgradeInProgress() throws Exc
137
215
} catch (SearchPhaseExecutionException e ) {
138
216
throw new AssertionError ("Notifications index exists but shards not yet ready - continuing busy wait" , e );
139
217
}
140
- assertFalse (annotationsIndexExists ());
218
+ assertFalse (annotationsIndexExists (AnnotationIndex . LATEST_INDEX_NAME ));
141
219
assertEquals (0 , numberOfAnnotationsAliases ());
142
220
});
143
221
} finally {
@@ -162,23 +240,30 @@ public void testNotCreatedWhenAfterOtherMlIndexAndResetInProgress() throws Excep
162
240
assertBusy (() -> {
163
241
SearchResponse response = client ().search (new SearchRequest (".ml-state" )).actionGet ();
164
242
assertEquals (1 , response .getHits ().getHits ().length );
165
- assertFalse (annotationsIndexExists ());
243
+ assertFalse (annotationsIndexExists (AnnotationIndex . LATEST_INDEX_NAME ));
166
244
assertEquals (0 , numberOfAnnotationsAliases ());
167
245
});
168
246
} finally {
169
247
client ().execute (SetResetModeAction .INSTANCE , SetResetModeActionRequest .disabled (true )).actionGet ();
170
248
}
171
249
}
172
250
173
- private boolean annotationsIndexExists () {
174
- return ESIntegTestCase .indexExists (AnnotationIndex .LATEST_INDEX_NAME , client ());
251
+ private boolean annotationsIndexExists (String expectedName ) {
252
+ GetIndexResponse getIndexResponse = client ().admin ()
253
+ .indices ()
254
+ .prepareGetIndex ()
255
+ .setIndices (AnnotationIndex .LATEST_INDEX_NAME )
256
+ .setIndicesOptions (IndicesOptions .LENIENT_EXPAND_OPEN )
257
+ .execute ()
258
+ .actionGet ();
259
+ return Arrays .asList (getIndexResponse .getIndices ()).contains (expectedName );
175
260
}
176
261
177
262
private int numberOfAnnotationsAliases () {
178
263
int count = 0 ;
179
264
ImmutableOpenMap <String , List <AliasMetadata >> aliases = client ().admin ()
180
265
.indices ()
181
- .prepareGetAliases (AnnotationIndex .READ_ALIAS_NAME , AnnotationIndex .WRITE_ALIAS_NAME )
266
+ .prepareGetAliases (AnnotationIndex .READ_ALIAS_NAME , AnnotationIndex .WRITE_ALIAS_NAME , AnnotationIndex . LATEST_INDEX_NAME )
182
267
.setIndicesOptions (IndicesOptions .LENIENT_EXPAND_OPEN_CLOSED_HIDDEN )
183
268
.get ()
184
269
.getAliases ();
@@ -192,4 +277,24 @@ private int numberOfAnnotationsAliases() {
192
277
}
193
278
return count ;
194
279
}
280
+
281
+ private void createReindexedIndex (String reindexedIndexName ) {
282
+ CreateIndexRequest createIndexRequest = new CreateIndexRequest (reindexedIndexName ).mapping (AnnotationIndex .annotationsMapping ())
283
+ .settings (
284
+ Settings .builder ()
285
+ .put (IndexMetadata .SETTING_AUTO_EXPAND_REPLICAS , "0-1" )
286
+ .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , "1" )
287
+ .put (IndexMetadata .SETTING_INDEX_HIDDEN , true )
288
+ );
289
+
290
+ client ().admin ().indices ().create (createIndexRequest ).actionGet ();
291
+
292
+ // At this point the upgrade assistant would reindex the old index into the new index but there's
293
+ // no point in this test as there's nothing in the old index.
294
+ }
295
+
296
+ private void createAnnotation () {
297
+ AnomalyDetectionAuditor auditor = new AnomalyDetectionAuditor (client (), getInstanceFromNode (ClusterService .class ));
298
+ auditor .info ("whatever" , "blah" );
299
+ }
195
300
}
0 commit comments