77package org .elasticsearch .xpack .ml .integration ;
88
99import org .elasticsearch .ResourceAlreadyExistsException ;
10+ import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequest ;
11+ import org .elasticsearch .action .admin .indices .alias .get .GetAliasesResponse ;
1012import org .elasticsearch .action .admin .indices .settings .get .GetSettingsResponse ;
1113import org .elasticsearch .action .support .IndicesOptions ;
14+ import org .elasticsearch .cluster .metadata .AliasMetadata ;
1215import org .elasticsearch .cluster .metadata .IndexMetadata ;
1316import org .elasticsearch .cluster .service .ClusterService ;
17+ import org .elasticsearch .common .collect .ImmutableOpenMap ;
1418import org .elasticsearch .common .settings .Settings ;
1519import org .elasticsearch .common .util .concurrent .EsExecutors ;
20+ import org .elasticsearch .index .query .QueryBuilders ;
1621import org .elasticsearch .threadpool .ThreadPool ;
1722import org .elasticsearch .xpack .ml .MachineLearning ;
1823import org .elasticsearch .xpack .ml .MlDailyMaintenanceService ;
1924import org .elasticsearch .xpack .ml .MlInitializationService ;
2025import org .junit .Before ;
2126
22- import java .util .Arrays ;
23- import java .util .Collections ;
27+ import java .util .List ;
2428import java .util .stream .Stream ;
2529
30+ import static java .util .stream .Collectors .toList ;
2631import static org .elasticsearch .cluster .metadata .IndexMetadata .SETTING_INDEX_HIDDEN ;
2732import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
33+ import static org .hamcrest .Matchers .contains ;
2834import static org .hamcrest .Matchers .equalTo ;
2935import static org .hamcrest .Matchers .is ;
3036import static org .hamcrest .Matchers .notNullValue ;
@@ -47,36 +53,33 @@ public void setUpMocks() {
4753 }
4854
4955 public void testThatMlIndicesBecomeHiddenWhenTheNodeBecomesMaster () throws Exception {
50- String [] mlHiddenIndexNames = {
56+ List < String > mlHiddenIndexNames = List . of (
5157 ".ml-anomalies-7" ,
5258 ".ml-state-000001" ,
5359 ".ml-stats-000001" ,
5460 ".ml-notifications-000002" ,
55- ".ml-annotations-6" };
56- String [] otherIndexNames = { "some-index-1" , "some-other-index-2" };
57- String [] allIndexNames = Stream .concat (Arrays .stream (mlHiddenIndexNames ), Arrays .stream (otherIndexNames )).toArray (String []::new );
61+ ".ml-annotations-000001"
62+ );
63+ List <String > otherIndexNames = List .of ("some-index-1" , "some-other-index-2" );
64+ List <String > allIndexNames = Stream .concat (mlHiddenIndexNames .stream (), otherIndexNames .stream ()).collect (toList ());
5865
5966 for (String indexName : mlHiddenIndexNames ) {
6067 try {
61- assertAcked ( prepareCreate ( indexName ). setSettings ( Collections . singletonMap (SETTING_INDEX_HIDDEN , randomBoolean ())));
68+ createIndex ( indexName , Settings . builder (). put (SETTING_INDEX_HIDDEN , randomBoolean ()). build ( ));
6269 } catch (ResourceAlreadyExistsException e ) {
63- logger .info ("Index " + indexName + "already exists: {}" , e .getDetailedMessage ());
70+ logger .info ("Index " + indexName + " already exists: {}" , e .getDetailedMessage ());
6471 }
6572 }
66- createIndex (otherIndexNames );
73+ for (String indexName : otherIndexNames ) {
74+ createIndex (indexName , Settings .EMPTY );
75+ }
6776
68- GetSettingsResponse settingsResponse = client ().admin ()
69- .indices ()
70- .prepareGetSettings (allIndexNames )
71- .setIndicesOptions (IndicesOptions .LENIENT_EXPAND_OPEN_CLOSED_HIDDEN )
72- .get ();
73- assertThat (settingsResponse , is (notNullValue ()));
77+ ImmutableOpenMap <String , Settings > indexToSettings = getIndexToSettingsMap (allIndexNames );
7478 for (String indexName : mlHiddenIndexNames ) {
75- Settings settings = settingsResponse .getIndexToSettings ().get (indexName );
76- assertThat (settings , is (notNullValue ()));
79+ assertThat (indexToSettings .get (indexName ), is (notNullValue ()));
7780 }
7881 for (String indexName : otherIndexNames ) {
79- Settings settings = settingsResponse . getIndexToSettings () .get (indexName );
82+ Settings settings = indexToSettings .get (indexName );
8083 assertThat (settings , is (notNullValue ()));
8184 assertThat (
8285 "Index " + indexName + " expected not to be hidden but was" ,
@@ -85,17 +88,13 @@ public void testThatMlIndicesBecomeHiddenWhenTheNodeBecomesMaster() throws Excep
8588 );
8689 }
8790
91+ assertFalse (mlInitializationService .areMlInternalIndicesHidden ());
8892 mlInitializationService .onMaster ();
8993 assertBusy (() -> assertTrue (mlInitializationService .areMlInternalIndicesHidden ()));
9094
91- settingsResponse = client ().admin ()
92- .indices ()
93- .prepareGetSettings (allIndexNames )
94- .setIndicesOptions (IndicesOptions .LENIENT_EXPAND_OPEN_CLOSED_HIDDEN )
95- .get ();
96- assertThat (settingsResponse , is (notNullValue ()));
95+ indexToSettings = getIndexToSettingsMap (allIndexNames );
9796 for (String indexName : mlHiddenIndexNames ) {
98- Settings settings = settingsResponse . getIndexToSettings () .get (indexName );
97+ Settings settings = indexToSettings .get (indexName );
9998 assertThat (settings , is (notNullValue ()));
10099 assertThat (
101100 "Index " + indexName + " expected to be hidden but wasn't, settings = " + settings ,
@@ -104,7 +103,7 @@ public void testThatMlIndicesBecomeHiddenWhenTheNodeBecomesMaster() throws Excep
104103 );
105104 }
106105 for (String indexName : otherIndexNames ) {
107- Settings settings = settingsResponse . getIndexToSettings () .get (indexName );
106+ Settings settings = indexToSettings .get (indexName );
108107 assertThat (settings , is (notNullValue ()));
109108 assertThat (
110109 "Index " + indexName + " expected not to be hidden but was, settings = " + settings ,
@@ -114,6 +113,113 @@ public void testThatMlIndicesBecomeHiddenWhenTheNodeBecomesMaster() throws Excep
114113 }
115114 }
116115
116+ public void testThatMlAliasesBecomeHiddenWhenTheNodeBecomesMaster () throws Exception {
117+ List <String > mlHiddenIndexNames = List .of (
118+ ".ml-anomalies-7" ,
119+ ".ml-state-000001" ,
120+ ".ml-stats-000001" ,
121+ ".ml-notifications-000002" ,
122+ ".ml-annotations-000001"
123+ );
124+ for (String indexName : mlHiddenIndexNames ) {
125+ try {
126+ createIndex (indexName , Settings .builder ().put (SETTING_INDEX_HIDDEN , randomBoolean ()).build ());
127+ } catch (ResourceAlreadyExistsException e ) {
128+ logger .info ("Index " + indexName + " already exists: {}" , e .getDetailedMessage ());
129+ }
130+ }
131+
132+ IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest ().addAliasAction (
133+ IndicesAliasesRequest .AliasActions .add ().index (".ml-anomalies-7" ).alias (".ml-anomalies-write" ).writeIndex (true )
134+ )
135+ .addAliasAction (
136+ IndicesAliasesRequest .AliasActions .add ()
137+ .index (".ml-state-000001" )
138+ .alias (".ml-state-write" )
139+ .filter (QueryBuilders .termQuery ("a" , "b" ))
140+ )
141+ .addAliasAction (
142+ IndicesAliasesRequest .AliasActions .add ()
143+ .index (".ml-stats-000001" )
144+ .alias (".ml-stats-write" )
145+ .indexRouting ("some-index-routing" )
146+ )
147+ .addAliasAction (
148+ IndicesAliasesRequest .AliasActions .add ()
149+ .index (".ml-notifications-000002" )
150+ .alias (".ml-notifications-write" )
151+ .searchRouting ("some-search-routing" )
152+ )
153+ .addAliasAction (
154+ IndicesAliasesRequest .AliasActions .add ()
155+ .index (".ml-annotations-000001" )
156+ .alias (".ml-annotations-write" )
157+ .writeIndex (true )
158+ .filter (QueryBuilders .termQuery ("a" , "b" ))
159+ .indexRouting ("some-index-routing" )
160+ .searchRouting ("some-search-routing" )
161+ );
162+ assertAcked (client ().admin ().indices ().aliases (indicesAliasesRequest ).get ());
163+
164+ assertFalse (mlInitializationService .areMlInternalIndicesHidden ());
165+ mlInitializationService .onMaster ();
166+ assertBusy (() -> assertTrue (mlInitializationService .areMlInternalIndicesHidden ()));
167+
168+ ImmutableOpenMap <String , List <AliasMetadata >> indexToAliasesMap = getIndexToAliasesMap (mlHiddenIndexNames );
169+ assertThat ("Aliases were: " + indexToAliasesMap , indexToAliasesMap .size (), is (equalTo (5 )));
170+ assertThat (
171+ indexToAliasesMap .get (".ml-anomalies-7" ),
172+ contains (AliasMetadata .builder (".ml-anomalies-write" ).isHidden (true ).writeIndex (true ).build ())
173+ );
174+ assertThat (
175+ indexToAliasesMap .get (".ml-state-000001" ),
176+ contains (AliasMetadata .builder (".ml-state-write" ).isHidden (true ).filter (QueryBuilders .termQuery ("a" , "b" ).toString ()).build ())
177+ );
178+ assertThat (
179+ indexToAliasesMap .get (".ml-stats-000001" ),
180+ contains (AliasMetadata .builder (".ml-stats-write" ).isHidden (true ).indexRouting ("some-index-routing" ).build ())
181+ );
182+ assertThat (
183+ indexToAliasesMap .get (".ml-notifications-000002" ),
184+ contains (AliasMetadata .builder (".ml-notifications-write" ).isHidden (true ).searchRouting ("some-search-routing" ).build ())
185+ );
186+ assertThat (
187+ indexToAliasesMap .get (".ml-annotations-000001" ),
188+ contains (
189+ AliasMetadata .builder (".ml-annotations-read" ).isHidden (true ).build (),
190+ AliasMetadata .builder (".ml-annotations-write" )
191+ .isHidden (true )
192+ .writeIndex (true )
193+ .filter (QueryBuilders .termQuery ("a" , "b" ).toString ())
194+ .indexRouting ("some-index-routing" )
195+ .searchRouting ("some-search-routing" )
196+ .build ()
197+ )
198+ );
199+ }
200+
201+ private static ImmutableOpenMap <String , Settings > getIndexToSettingsMap (List <String > indexNames ) {
202+ GetSettingsResponse getSettingsResponse = client ().admin ()
203+ .indices ()
204+ .prepareGetSettings ()
205+ .setIndices (indexNames .toArray (String []::new ))
206+ .setIndicesOptions (IndicesOptions .LENIENT_EXPAND_OPEN_CLOSED_HIDDEN )
207+ .get ();
208+ assertThat (getSettingsResponse , is (notNullValue ()));
209+ return getSettingsResponse .getIndexToSettings ();
210+ }
211+
212+ private static ImmutableOpenMap <String , List <AliasMetadata >> getIndexToAliasesMap (List <String > indexNames ) {
213+ GetAliasesResponse getAliasesResponse = client ().admin ()
214+ .indices ()
215+ .prepareGetAliases ()
216+ .setIndices (indexNames .toArray (String []::new ))
217+ .setIndicesOptions (IndicesOptions .LENIENT_EXPAND_OPEN_CLOSED_HIDDEN )
218+ .get ();
219+ assertThat (getAliasesResponse , is (notNullValue ()));
220+ return getAliasesResponse .getAliases ();
221+ }
222+
117223 @ Override
118224 public Settings indexSettings () {
119225 return Settings .builder ().put (super .indexSettings ()).put (IndexMetadata .SETTING_DATA_PATH , (String ) null ).build ();
0 commit comments