5
5
*/
6
6
package org .elasticsearch .xpack .ccr ;
7
7
8
+ import org .apache .logging .log4j .message .ParameterizedMessage ;
8
9
import org .elasticsearch .ElasticsearchException ;
9
10
import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
10
11
import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
11
12
import org .elasticsearch .action .admin .indices .exists .indices .IndicesExistsRequest ;
12
13
import org .elasticsearch .client .Client ;
13
14
import org .elasticsearch .cluster .metadata .IndexMetaData ;
14
15
import org .elasticsearch .cluster .metadata .MetaData ;
16
+ import org .elasticsearch .common .CheckedRunnable ;
15
17
import org .elasticsearch .common .Strings ;
16
18
import org .elasticsearch .common .regex .Regex ;
17
19
import org .elasticsearch .common .settings .Settings ;
@@ -82,7 +84,7 @@ public void testAutoFollow() throws Exception {
82
84
assertTrue (followerClient ().admin ().indices ().exists (request ).actionGet ().isExists ());
83
85
});
84
86
createLeaderIndex ("transactions-201901" , leaderIndexSettings );
85
- assertBusy (() -> {
87
+ assertLongBusy (() -> {
86
88
AutoFollowStats autoFollowStats = getAutoFollowStats ();
87
89
assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (2L ));
88
90
@@ -105,7 +107,7 @@ public void testCleanFollowedLeaderIndexUUIDs() throws Exception {
105
107
106
108
putAutoFollowPatterns ("my-pattern" , new String [] {"logs-*" });
107
109
createLeaderIndex ("logs-201901" , leaderIndexSettings );
108
- assertBusy (() -> {
110
+ assertLongBusy (() -> {
109
111
AutoFollowStats autoFollowStats = getAutoFollowStats ();
110
112
assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
111
113
@@ -126,7 +128,7 @@ public void testCleanFollowedLeaderIndexUUIDs() throws Exception {
126
128
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest ("logs-201901" );
127
129
assertAcked (leaderClient ().admin ().indices ().delete (deleteIndexRequest ).actionGet ());
128
130
129
- assertBusy (() -> {
131
+ assertLongBusy (() -> {
130
132
AutoFollowMetadata autoFollowMetadata = getFollowerCluster ().clusterService ().state ()
131
133
.metaData ()
132
134
.custom (AutoFollowMetadata .TYPE );
@@ -152,15 +154,15 @@ public void testAutoFollowManyIndices() throws Exception {
152
154
MetaData [] metaData = new MetaData [1 ];
153
155
AutoFollowStats [] autoFollowStats = new AutoFollowStats [1 ];
154
156
try {
155
- assertBusy (() -> {
157
+ assertLongBusy (() -> {
156
158
metaData [0 ] = getFollowerCluster ().clusterService ().state ().metaData ();
157
159
autoFollowStats [0 ] = getAutoFollowStats ();
158
160
159
161
assertThat (metaData [0 ].indices ().size (), equalTo ((int ) expectedVal1 ));
160
162
AutoFollowMetadata autoFollowMetadata = metaData [0 ].custom (AutoFollowMetadata .TYPE );
161
163
assertThat (autoFollowMetadata .getFollowedLeaderIndexUUIDs ().get ("my-pattern" ), hasSize ((int ) expectedVal1 ));
162
164
assertThat (autoFollowStats [0 ].getNumberOfSuccessfulFollowIndices (), equalTo (expectedVal1 ));
163
- }, 30 , TimeUnit . SECONDS );
165
+ });
164
166
} catch (AssertionError ae ) {
165
167
logger .warn ("indices={}" , Arrays .toString (metaData [0 ].indices ().keys ().toArray (String .class )));
166
168
logger .warn ("auto follow stats={}" , Strings .toString (autoFollowStats [0 ]));
@@ -171,15 +173,15 @@ public void testAutoFollowManyIndices() throws Exception {
171
173
// then the leader index created after that should never be auto followed:
172
174
deleteAutoFollowPattern ("my-pattern" );
173
175
try {
174
- assertBusy (() -> {
176
+ assertLongBusy (() -> {
175
177
metaData [0 ] = getFollowerCluster ().clusterService ().state ().metaData ();
176
178
autoFollowStats [0 ] = getAutoFollowStats ();
177
179
178
180
assertThat (metaData [0 ].indices ().size (), equalTo ((int )expectedVal1 ));
179
181
AutoFollowMetadata autoFollowMetadata = metaData [0 ].custom (AutoFollowMetadata .TYPE );
180
182
assertThat (autoFollowMetadata .getFollowedLeaderIndexUUIDs ().get ("my-pattern" ), nullValue ());
181
183
assertThat (autoFollowStats [0 ].getAutoFollowedClusters ().size (), equalTo (0 ));
182
- }, 30 , TimeUnit . SECONDS );
184
+ });
183
185
} catch (AssertionError ae ) {
184
186
logger .warn ("indices={}" , Arrays .toString (metaData [0 ].indices ().keys ().toArray (String .class )));
185
187
logger .warn ("auto follow stats={}" , Strings .toString (autoFollowStats [0 ]));
@@ -195,27 +197,21 @@ public void testAutoFollowManyIndices() throws Exception {
195
197
}
196
198
long expectedVal2 = numIndices ;
197
199
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
+ });
219
215
}
220
216
221
217
public void testAutoFollowParameterAreDelegated () throws Exception {
@@ -333,7 +329,7 @@ public void testConflictingPatterns() throws Exception {
333
329
putAutoFollowPatterns ("my-pattern2" , new String [] {"logs-2018*" });
334
330
335
331
createLeaderIndex ("logs-201701" , leaderIndexSettings );
336
- assertBusy (() -> {
332
+ assertLongBusy (() -> {
337
333
AutoFollowStats autoFollowStats = getAutoFollowStats ();
338
334
assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
339
335
assertThat (autoFollowStats .getNumberOfFailedFollowIndices (), equalTo (0L ));
@@ -343,7 +339,7 @@ public void testConflictingPatterns() throws Exception {
343
339
assertTrue (followerClient ().admin ().indices ().exists (request ).actionGet ().isExists ());
344
340
345
341
createLeaderIndex ("logs-201801" , leaderIndexSettings );
346
- assertBusy (() -> {
342
+ assertLongBusy (() -> {
347
343
AutoFollowStats autoFollowStats = getAutoFollowStats ();
348
344
assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
349
345
assertThat (autoFollowStats .getNumberOfFailedFollowIndices (), greaterThanOrEqualTo (1L ));
@@ -414,15 +410,15 @@ public void testPauseAndResumeAutoFollowPattern() throws Exception {
414
410
415
411
// create the auto follow pattern
416
412
putAutoFollowPatterns ("test-pattern" , new String []{"test-*" , "tests-*" });
417
- assertBusy (() -> {
413
+ assertLongBusy (() -> {
418
414
final AutoFollowStats autoFollowStats = getAutoFollowStats ();
419
415
assertThat (autoFollowStats .getAutoFollowedClusters ().size (), equalTo (1 ));
420
416
assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (0L ));
421
417
});
422
418
423
419
// index created in the remote cluster are auto followed
424
420
createLeaderIndex ("test-new-index-is-auto-followed" , leaderIndexSettings );
425
- assertBusy (() -> {
421
+ assertLongBusy (() -> {
426
422
final AutoFollowStats autoFollowStats = getAutoFollowStats ();
427
423
assertThat (autoFollowStats .getAutoFollowedClusters ().size (), equalTo (1 ));
428
424
assertThat (autoFollowStats .getNumberOfSuccessfulFollowIndices (), equalTo (1L ));
@@ -463,10 +459,11 @@ public void testPauseAndResumeAutoFollowPattern() throws Exception {
463
459
464
460
// resume the auto follow pattern, indices created while the pattern was paused are picked up for auto-following
465
461
resumeAutoFollowPattern ("test-pattern" );
466
- assertBusy (() -> {
462
+ assertLongBusy (() -> {
467
463
final Client client = followerClient ();
468
464
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 ));
470
467
for (int i = 0 ; i < nbIndicesCreatedWhilePaused ; i ++) {
471
468
IndicesExistsRequest request = new IndicesExistsRequest ("copy-test-index-created-while-pattern-is-paused-" + i );
472
469
assertTrue (followerClient ().admin ().indices ().exists (request ).actionGet ().isExists ());
@@ -528,31 +525,27 @@ public void testPauseAndResumeWithMultipleAutoFollowPatterns() throws Exception
528
525
createNewLeaderIndicesThread .start ();
529
526
530
527
// wait for 3 leader indices to be created on the remote cluster
531
- latchThree .await (30L , TimeUnit .SECONDS );
528
+ latchThree .await (60L , TimeUnit .SECONDS );
532
529
assertThat (leaderIndices .get (), greaterThanOrEqualTo (3 ));
533
- assertBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (3L )),
534
- 30L , TimeUnit .SECONDS );
530
+ assertLongBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (3L )));
535
531
536
532
// now pause some random patterns
537
533
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 ))));
541
536
542
537
// wait for more leader indices to be created on the remote cluster
543
- latchSix .await (30L , TimeUnit .SECONDS );
538
+ latchSix .await (60L , TimeUnit .SECONDS );
544
539
assertThat (leaderIndices .get (), greaterThanOrEqualTo (6 ));
545
540
546
541
// resume auto follow patterns
547
542
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 ())));
550
544
551
545
// wait for more leader indices to be created on the remote cluster
552
- latchNine .await (30L , TimeUnit .SECONDS );
546
+ latchNine .await (60L , TimeUnit .SECONDS );
553
547
assertThat (leaderIndices .get (), greaterThanOrEqualTo (9 ));
554
- assertBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (9L )),
555
- 30L , TimeUnit .SECONDS );
548
+ assertLongBusy (() -> assertThat (getAutoFollowStats ().getNumberOfSuccessfulFollowIndices (), greaterThanOrEqualTo (9L )));
556
549
557
550
running .set (false );
558
551
createNewLeaderIndicesThread .join ();
@@ -617,4 +610,21 @@ private AutoFollowMetadata.AutoFollowPattern getAutoFollowPattern(final String n
617
610
assertTrue (response .getAutoFollowPatterns ().containsKey (name ));
618
611
return response .getAutoFollowPatterns ().get (name );
619
612
}
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
+ }
620
630
}
0 commit comments