77
88import org .apache .logging .log4j .Logger ;
99import org .elasticsearch .action .admin .indices .alias .Alias ;
10+ import org .elasticsearch .action .admin .indices .alias .get .GetAliasesResponse ;
1011import org .elasticsearch .action .admin .indices .create .CreateIndexResponse ;
12+ import org .elasticsearch .action .admin .indices .get .GetIndexResponse ;
1113import org .elasticsearch .action .admin .indices .template .get .GetIndexTemplatesResponse ;
1214import org .elasticsearch .action .search .SearchRequestBuilder ;
1315import org .elasticsearch .action .search .SearchResponse ;
1416import org .elasticsearch .action .support .IndicesOptions ;
1517import org .elasticsearch .analysis .common .CommonAnalysisPlugin ;
1618import org .elasticsearch .cluster .ClusterState ;
1719import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
20+ import org .elasticsearch .cluster .metadata .MappingMetaData ;
1821import org .elasticsearch .cluster .routing .IndexRoutingTable ;
1922import org .elasticsearch .common .collect .Tuple ;
2023import org .elasticsearch .common .network .NetworkModule ;
@@ -192,7 +195,7 @@ public void _setup() throws Exception {
192195 internalCluster ().setDisruptionScheme (ice );
193196 ice .startDisrupting ();
194197 }
195-
198+ stopWatcher ();
196199 createWatcherIndicesOrAliases ();
197200 startWatcher ();
198201 }
@@ -219,13 +222,19 @@ private void createWatcherIndicesOrAliases() throws Exception {
219222 // alias for .watches, setting the index template to the same as well
220223 String watchIndexName ;
221224 String triggeredWatchIndexName ;
222- if (rarely ()) {
223- watchIndexName = ".watches-alias-index" ;
224- CreateIndexResponse response = client ().admin ().indices ().prepareCreate (watchIndexName )
225+ if (randomBoolean ()) {
226+ // Create an index to get the template
227+ String tempIndex = ".watches" + randomAlphaOfLength (5 ).toLowerCase (Locale .ROOT );
228+ CreateIndexResponse response = client ().admin ().indices ().prepareCreate (tempIndex )
225229 .setCause ("Index to test aliases with .watches index" )
226230 .addAlias (new Alias (Watch .INDEX ))
227231 .get ();
228232 assertAcked (response );
233+
234+ // Now replace it with a randomly named index
235+ watchIndexName = randomAlphaOfLengthBetween (5 ,10 ).toLowerCase (Locale .ROOT );
236+ replaceWatcherIndexWithRandomlyNamedIndex (Watch .INDEX , watchIndexName , Watch .DOC_TYPE );
237+
229238 logger .info ("set alias for .watches index to [{}]" , watchIndexName );
230239 } else {
231240 watchIndexName = Watch .INDEX ;
@@ -237,13 +246,19 @@ private void createWatcherIndicesOrAliases() throws Exception {
237246 }
238247
239248 // alias for .triggered-watches, ensuring the index template is set appropriately
240- if (rarely ()) {
241- triggeredWatchIndexName = ".triggered_watches-alias-index" ;
242- CreateIndexResponse response = client ().admin ().indices ().prepareCreate (triggeredWatchIndexName )
249+ if (randomBoolean ()) {
250+ String tempIndex = ".triggered_watches-alias-index" ;
251+ CreateIndexResponse response = client ().admin ().indices ().prepareCreate (tempIndex )
243252 .setCause ("Index to test aliases with .triggered-watches index" )
244253 .addAlias (new Alias (TriggeredWatchStoreField .INDEX_NAME ))
245254 .get ();
246255 assertAcked (response );
256+
257+ // Now replace it with a randomly-named index
258+ triggeredWatchIndexName = randomValueOtherThan (watchIndexName ,
259+ () -> randomAlphaOfLengthBetween (5 ,10 ).toLowerCase (Locale .ROOT ));
260+ replaceWatcherIndexWithRandomlyNamedIndex (TriggeredWatchStoreField .INDEX_NAME , triggeredWatchIndexName ,
261+ TriggeredWatchStoreField .DOC_TYPE );
247262 logger .info ("set alias for .triggered-watches index to [{}]" , triggeredWatchIndexName );
248263 } else {
249264 triggeredWatchIndexName = TriggeredWatchStoreField .INDEX_NAME ;
@@ -257,6 +272,38 @@ private void createWatcherIndicesOrAliases() throws Exception {
257272 }
258273 }
259274
275+ public void replaceWatcherIndexWithRandomlyNamedIndex (String originalIndexOrAlias , String to , String docType ) {
276+ GetIndexResponse index = client ().admin ().indices ().prepareGetIndex ().setIndices (originalIndexOrAlias ).get ();
277+ MappingMetaData mapping = index .getMappings ().get (index .getIndices ()[0 ]).get (docType );
278+
279+ Settings settings = index .getSettings ().get (index .getIndices ()[0 ]);
280+ Settings .Builder newSettings = Settings .builder ().put (settings );
281+ newSettings .remove ("index.provided_name" );
282+ newSettings .remove ("index.uuid" );
283+ newSettings .remove ("index.creation_date" );
284+ newSettings .remove ("index.version.created" );
285+
286+ CreateIndexResponse createIndexResponse = client ().admin ().indices ().prepareCreate (to )
287+ .addMapping (docType , mapping .sourceAsMap ())
288+ .setSettings (newSettings )
289+ .get ();
290+ assertTrue (createIndexResponse .isAcknowledged ());
291+ ensureGreen (to );
292+
293+ AtomicReference <String > originalIndex = new AtomicReference <>(originalIndexOrAlias );
294+ boolean watchesIsAlias = client ().admin ().indices ().prepareAliasesExist (originalIndexOrAlias ).get ().isExists ();
295+ if (watchesIsAlias ) {
296+ GetAliasesResponse aliasesResponse = client ().admin ().indices ().prepareGetAliases (originalIndexOrAlias ).get ();
297+ assertEquals (1 , aliasesResponse .getAliases ().size ());
298+ aliasesResponse .getAliases ().forEach ((aliasRecord ) -> {
299+ assertEquals (1 , aliasRecord .value .size ());
300+ originalIndex .set (aliasRecord .key );
301+ });
302+ }
303+ client ().admin ().indices ().prepareDelete (originalIndex .get ()).get ();
304+ client ().admin ().indices ().prepareAliases ().addAlias (to , originalIndexOrAlias ).get ();
305+ }
306+
260307 protected TimeWarp timeWarp () {
261308 assert timeWarped () : "cannot access TimeWarp when test context is not time warped" ;
262309 return timeWarp ;
0 commit comments