55 */
66package org .elasticsearch .xpack .ccr ;
77
8+ import org .apache .logging .log4j .message .ParameterizedMessage ;
89import org .elasticsearch .ElasticsearchException ;
910import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
1011import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
1112import org .elasticsearch .action .admin .indices .exists .indices .IndicesExistsRequest ;
1213import org .elasticsearch .client .Client ;
1314import org .elasticsearch .cluster .metadata .IndexMetaData ;
1415import org .elasticsearch .cluster .metadata .MetaData ;
16+ import org .elasticsearch .common .CheckedRunnable ;
1517import org .elasticsearch .common .Strings ;
1618import org .elasticsearch .common .regex .Regex ;
1719import org .elasticsearch .common .settings .Settings ;
@@ -82,7 +84,7 @@ public void testAutoFollow() throws Exception {
8284 assertTrue (followerClient ().admin ().indices ().exists (request ).actionGet ().isExists ());
8385 });
8486 createLeaderIndex ("transactions-201901" , leaderIndexSettings );
85- assertBusy (() -> {
87+ assertLongBusy (() -> {
8688 AutoFollowStats autoFollowStats = getAutoFollowStats ();
8789 assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (2L ));
8890
@@ -105,7 +107,7 @@ public void testCleanFollowedLeaderIndexUUIDs() throws Exception {
105107
106108 putAutoFollowPatterns ("my-pattern" , new String [] {"logs-*" });
107109 createLeaderIndex ("logs-201901" , leaderIndexSettings );
108- assertBusy (() -> {
110+ assertLongBusy (() -> {
109111 AutoFollowStats autoFollowStats = getAutoFollowStats ();
110112 assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
111113
@@ -126,7 +128,7 @@ public void testCleanFollowedLeaderIndexUUIDs() throws Exception {
126128 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest ("logs-201901" );
127129 assertAcked (leaderClient ().admin ().indices ().delete (deleteIndexRequest ).actionGet ());
128130
129- assertBusy (() -> {
131+ assertLongBusy (() -> {
130132 AutoFollowMetadata autoFollowMetadata = getFollowerCluster ().clusterService ().state ()
131133 .metaData ()
132134 .custom (AutoFollowMetadata .TYPE );
@@ -152,15 +154,15 @@ public void testAutoFollowManyIndices() throws Exception {
152154 MetaData [] metaData = new MetaData [1 ];
153155 AutoFollowStats [] autoFollowStats = new AutoFollowStats [1 ];
154156 try {
155- assertBusy (() -> {
157+ assertLongBusy (() -> {
156158 metaData [0 ] = getFollowerCluster ().clusterService ().state ().metaData ();
157159 autoFollowStats [0 ] = getAutoFollowStats ();
158160
159161 assertThat (metaData [0 ].indices ().size (), equalTo ((int ) expectedVal1 ));
160162 AutoFollowMetadata autoFollowMetadata = metaData [0 ].custom (AutoFollowMetadata .TYPE );
161163 assertThat (autoFollowMetadata .getFollowedLeaderIndexUUIDs ().get ("my-pattern" ), hasSize ((int ) expectedVal1 ));
162164 assertThat (autoFollowStats [0 ].getNumberOfSuccessfulFollowIndices (), equalTo (expectedVal1 ));
163- }, 30 , TimeUnit . SECONDS );
165+ });
164166 } catch (AssertionError ae ) {
165167 logger .warn ("indices={}" , Arrays .toString (metaData [0 ].indices ().keys ().toArray (String .class )));
166168 logger .warn ("auto follow stats={}" , Strings .toString (autoFollowStats [0 ]));
@@ -171,15 +173,15 @@ public void testAutoFollowManyIndices() throws Exception {
171173 // then the leader index created after that should never be auto followed:
172174 deleteAutoFollowPattern ("my-pattern" );
173175 try {
174- assertBusy (() -> {
176+ assertLongBusy (() -> {
175177 metaData [0 ] = getFollowerCluster ().clusterService ().state ().metaData ();
176178 autoFollowStats [0 ] = getAutoFollowStats ();
177179
178180 assertThat (metaData [0 ].indices ().size (), equalTo ((int )expectedVal1 ));
179181 AutoFollowMetadata autoFollowMetadata = metaData [0 ].custom (AutoFollowMetadata .TYPE );
180182 assertThat (autoFollowMetadata .getFollowedLeaderIndexUUIDs ().get ("my-pattern" ), nullValue ());
181183 assertThat (autoFollowStats [0 ].getAutoFollowedClusters ().size (), equalTo (0 ));
182- }, 30 , TimeUnit . SECONDS );
184+ });
183185 } catch (AssertionError ae ) {
184186 logger .warn ("indices={}" , Arrays .toString (metaData [0 ].indices ().keys ().toArray (String .class )));
185187 logger .warn ("auto follow stats={}" , Strings .toString (autoFollowStats [0 ]));
@@ -195,27 +197,21 @@ public void testAutoFollowManyIndices() throws Exception {
195197 }
196198 long expectedVal2 = numIndices ;
197199
198- try {
199- assertBusy (() -> {
200- metaData [0 ] = getFollowerCluster ().clusterService ().state ().metaData ();
201- autoFollowStats [0 ] = getAutoFollowStats ();
202-
203- assertThat (metaData [0 ].indices ().size (), equalTo ((int ) expectedVal2 ));
204- AutoFollowMetadata autoFollowMetadata = metaData [0 ].custom (AutoFollowMetadata .TYPE );
205- // expectedVal2 + 1, because logs-does-not-count is also marked as auto followed.
206- // (This is because indices created before a pattern exists are not auto followed and are just marked as such.)
207- assertThat (autoFollowMetadata .getFollowedLeaderIndexUUIDs ().get ("my-pattern" ), hasSize ((int ) expectedVal2 + 1 ));
208- long count = Arrays .stream (metaData [0 ].getConcreteAllIndices ()).filter (s -> s .startsWith ("copy-" )).count ();
209- assertThat (count , equalTo (expectedVal2 ));
210- // Ensure that there are no auto follow errors:
211- // (added specifically to see that there are no leader indices auto followed multiple times)
212- assertThat (autoFollowStats [0 ].getRecentAutoFollowErrors ().size (), equalTo (0 ));
213- }, 30 , TimeUnit .SECONDS );
214- } catch (AssertionError ae ) {
215- logger .warn ("indices={}" , Arrays .toString (metaData [0 ].indices ().keys ().toArray (String .class )));
216- logger .warn ("auto follow stats={}" , Strings .toString (autoFollowStats [0 ]));
217- throw ae ;
218- }
200+ assertLongBusy (() -> {
201+ metaData [0 ] = getFollowerCluster ().clusterService ().state ().metaData ();
202+ autoFollowStats [0 ] = getAutoFollowStats ();
203+
204+ assertThat (metaData [0 ].indices ().size (), equalTo ((int ) expectedVal2 ));
205+ AutoFollowMetadata autoFollowMetadata = metaData [0 ].custom (AutoFollowMetadata .TYPE );
206+ // expectedVal2 + 1, because logs-does-not-count is also marked as auto followed.
207+ // (This is because indices created before a pattern exists are not auto followed and are just marked as such.)
208+ assertThat (autoFollowMetadata .getFollowedLeaderIndexUUIDs ().get ("my-pattern" ), hasSize ((int ) expectedVal2 + 1 ));
209+ long count = Arrays .stream (metaData [0 ].getConcreteAllIndices ()).filter (s -> s .startsWith ("copy-" )).count ();
210+ assertThat (count , equalTo (expectedVal2 ));
211+ // Ensure that there are no auto follow errors:
212+ // (added specifically to see that there are no leader indices auto followed multiple times)
213+ assertThat (autoFollowStats [0 ].getRecentAutoFollowErrors ().size (), equalTo (0 ));
214+ });
219215 }
220216
221217 public void testAutoFollowParameterAreDelegated () throws Exception {
@@ -333,7 +329,7 @@ public void testConflictingPatterns() throws Exception {
333329 putAutoFollowPatterns ("my-pattern2" , new String [] {"logs-2018*" });
334330
335331 createLeaderIndex ("logs-201701" , leaderIndexSettings );
336- assertBusy (() -> {
332+ assertLongBusy (() -> {
337333 AutoFollowStats autoFollowStats = getAutoFollowStats ();
338334 assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
339335 assertThat (autoFollowStats .getNumberOfFailedFollowIndices (), equalTo (0L ));
@@ -343,7 +339,7 @@ public void testConflictingPatterns() throws Exception {
343339 assertTrue (followerClient ().admin ().indices ().exists (request ).actionGet ().isExists ());
344340
345341 createLeaderIndex ("logs-201801" , leaderIndexSettings );
346- assertBusy (() -> {
342+ assertLongBusy (() -> {
347343 AutoFollowStats autoFollowStats = getAutoFollowStats ();
348344 assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
349345 assertThat (autoFollowStats .getNumberOfFailedFollowIndices (), greaterThanOrEqualTo (1L ));
@@ -414,15 +410,15 @@ public void testPauseAndResumeAutoFollowPattern() throws Exception {
414410
415411 // create the auto follow pattern
416412 putAutoFollowPatterns ("test-pattern" , new String []{"test-*" , "tests-*" });
417- assertBusy (() -> {
413+ assertLongBusy (() -> {
418414 final AutoFollowStats autoFollowStats = getAutoFollowStats ();
419415 assertThat (autoFollowStats .getAutoFollowedClusters ().size (), equalTo (1 ));
420416 assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (0L ));
421417 });
422418
423419 // index created in the remote cluster are auto followed
424420 createLeaderIndex ("test-new-index-is-auto-followed" , leaderIndexSettings );
425- assertBusy (() -> {
421+ assertLongBusy (() -> {
426422 final AutoFollowStats autoFollowStats = getAutoFollowStats ();
427423 assertThat (autoFollowStats .getAutoFollowedClusters ().size (), equalTo (1 ));
428424 assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
@@ -463,10 +459,11 @@ public void testPauseAndResumeAutoFollowPattern() throws Exception {
463459
464460 // resume the auto follow pattern, indices created while the pattern was paused are picked up for auto-following
465461 resumeAutoFollowPattern ("test-pattern" );
466- assertBusy (() -> {
462+ assertLongBusy (() -> {
467463 final Client client = followerClient ();
468464 assertThat (getAutoFollowStats ().getAutoFollowedClusters ().size (), equalTo (1 ));
469- assertThat (client .admin ().indices ().prepareStats ("copy-*" ).get ().getIndices ().size (), equalTo (1 + nbIndicesCreatedWhilePaused ));
465+ assertThat (client .admin ().cluster ().prepareState ().clear ().setIndices ("copy-*" ).setMetaData (true ).get ()
466+ .getState ().getMetaData ().getIndices ().size (), equalTo (1 + nbIndicesCreatedWhilePaused ));
470467 for (int i = 0 ; i < nbIndicesCreatedWhilePaused ; i ++) {
471468 IndicesExistsRequest request = new IndicesExistsRequest ("copy-test-index-created-while-pattern-is-paused-" + i );
472469 assertTrue (followerClient ().admin ().indices ().exists (request ).actionGet ().isExists ());
@@ -528,31 +525,27 @@ public void testPauseAndResumeWithMultipleAutoFollowPatterns() throws Exception
528525 createNewLeaderIndicesThread .start ();
529526
530527 // wait for 3 leader indices to be created on the remote cluster
531- latchThree .await (30L , TimeUnit .SECONDS );
528+ latchThree .await (60L , TimeUnit .SECONDS );
532529 assertThat (leaderIndices .get (), greaterThanOrEqualTo (3 ));
533- assertBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (3L )),
534- 30L , TimeUnit .SECONDS );
530+ assertLongBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (3L )));
535531
536532 // now pause some random patterns
537533 pausedAutoFollowerPatterns .forEach (this ::pauseAutoFollowPattern );
538- assertBusy (() -> autoFollowPatterns .forEach (pattern ->
539- assertThat (getAutoFollowPattern (pattern ).isActive (), equalTo (pausedAutoFollowerPatterns .contains (pattern ) == false ))),
540- 30L , TimeUnit .SECONDS );
534+ assertLongBusy (() -> autoFollowPatterns .forEach (pattern ->
535+ assertThat (getAutoFollowPattern (pattern ).isActive (), equalTo (pausedAutoFollowerPatterns .contains (pattern ) == false ))));
541536
542537 // wait for more leader indices to be created on the remote cluster
543- latchSix .await (30L , TimeUnit .SECONDS );
538+ latchSix .await (60L , TimeUnit .SECONDS );
544539 assertThat (leaderIndices .get (), greaterThanOrEqualTo (6 ));
545540
546541 // resume auto follow patterns
547542 pausedAutoFollowerPatterns .forEach (this ::resumeAutoFollowPattern );
548- assertBusy (() -> autoFollowPatterns .forEach (pattern -> assertTrue (getAutoFollowPattern (pattern ).isActive ())),
549- 30L , TimeUnit .SECONDS );
543+ assertLongBusy (() -> autoFollowPatterns .forEach (pattern -> assertTrue (getAutoFollowPattern (pattern ).isActive ())));
550544
551545 // wait for more leader indices to be created on the remote cluster
552- latchNine .await (30L , TimeUnit .SECONDS );
546+ latchNine .await (60L , TimeUnit .SECONDS );
553547 assertThat (leaderIndices .get (), greaterThanOrEqualTo (9 ));
554- assertBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (9L )),
555- 30L , TimeUnit .SECONDS );
548+ assertLongBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (9L )));
556549
557550 running .set (false );
558551 createNewLeaderIndicesThread .join ();
@@ -617,4 +610,21 @@ private AutoFollowMetadata.AutoFollowPattern getAutoFollowPattern(final String n
617610 assertTrue (response .getAutoFollowPatterns ().containsKey (name ));
618611 return response .getAutoFollowPatterns ().get (name );
619612 }
613+
614+ private void assertLongBusy (CheckedRunnable <Exception > codeBlock ) throws Exception {
615+ try {
616+ assertBusy (codeBlock , 60L , TimeUnit .SECONDS );
617+ } catch (AssertionError ae ) {
618+ AutoFollowStats autoFollowStats = null ;
619+ try {
620+ autoFollowStats = getAutoFollowStats ();
621+ } catch (Exception e ) {
622+ ae .addSuppressed (e );
623+ }
624+ final AutoFollowStats finalAutoFollowStats = autoFollowStats ;
625+ logger .warn (() -> new ParameterizedMessage ("AssertionError when waiting for auto-follower, auto-follow stats are: {}" ,
626+ finalAutoFollowStats != null ? Strings .toString (finalAutoFollowStats ) : "null" ), ae );
627+ throw ae ;
628+ }
629+ }
620630}
0 commit comments