14
14
import org .elasticsearch .action .ActionListener ;
15
15
import org .elasticsearch .action .admin .cluster .state .ClusterStateRequest ;
16
16
import org .elasticsearch .action .admin .cluster .state .ClusterStateResponse ;
17
+ import org .elasticsearch .action .support .GroupedActionListener ;
17
18
import org .elasticsearch .client .Client ;
18
19
import org .elasticsearch .cluster .ClusterChangedEvent ;
19
20
import org .elasticsearch .cluster .ClusterState ;
@@ -428,23 +429,21 @@ private void checkAutoFollowPattern(String autoFollowPattenName,
428
429
MetaData remoteMetadata ,
429
430
MetaData localMetadata ,
430
431
Consumer <AutoFollowResult > resultHandler ) {
432
+ final GroupedActionListener <Tuple <Index , Exception >> groupedListener = new GroupedActionListener <>(
433
+ ActionListener .wrap (
434
+ rs -> resultHandler .accept (new AutoFollowResult (autoFollowPattenName , new ArrayList <>(rs ))),
435
+ e -> { throw new AssertionError ("must never happen" , e ); }),
436
+ leaderIndicesToFollow .size (), Collections .emptyList ());
431
437
432
- final CountDown leaderIndicesCountDown = new CountDown (leaderIndicesToFollow .size ());
433
- final AtomicArray <Tuple <Index , Exception >> results = new AtomicArray <>(leaderIndicesToFollow .size ());
434
- for (int i = 0 ; i < leaderIndicesToFollow .size (); i ++) {
435
- final Index indexToFollow = leaderIndicesToFollow .get (i );
436
- final int slot = i ;
437
-
438
+ for (final Index indexToFollow : leaderIndicesToFollow ) {
438
439
List <String > otherMatchingPatterns = patternsForTheSameRemoteCluster .stream ()
439
440
.filter (otherPattern -> otherPattern .v2 ().match (indexToFollow .getName ()))
440
441
.map (Tuple ::v1 )
441
442
.collect (Collectors .toList ());
442
443
if (otherMatchingPatterns .size () != 0 ) {
443
- results .set (slot , new Tuple <>(indexToFollow , new ElasticsearchException ("index to follow [" + indexToFollow .getName () +
444
- "] for pattern [" + autoFollowPattenName + "] matches with other patterns " + otherMatchingPatterns + "" )));
445
- if (leaderIndicesCountDown .countDown ()) {
446
- resultHandler .accept (new AutoFollowResult (autoFollowPattenName , results .asList ()));
447
- }
444
+ groupedListener .onResponse (
445
+ new Tuple <>(indexToFollow , new ElasticsearchException ("index to follow [" + indexToFollow .getName () +
446
+ "] for pattern [" + autoFollowPattenName + "] matches with other patterns " + otherMatchingPatterns + "" )));
448
447
} else {
449
448
final Settings leaderIndexSettings = remoteMetadata .getIndexSafe (indexToFollow ).getSettings ();
450
449
if (IndexSettings .INDEX_SOFT_DELETES_SETTING .get (leaderIndexSettings ) == false ) {
@@ -456,28 +455,15 @@ private void checkAutoFollowPattern(String autoFollowPattenName,
456
455
if (error != null ) {
457
456
failure .addSuppressed (error );
458
457
}
459
- results .set (slot , new Tuple <>(indexToFollow , failure ));
460
- if (leaderIndicesCountDown .countDown ()) {
461
- resultHandler .accept (new AutoFollowResult (autoFollowPattenName , results .asList ()));
462
- }
458
+ groupedListener .onResponse (new Tuple <>(indexToFollow , failure ));
463
459
});
464
- continue ;
465
460
} else if (leaderIndexAlreadyFollowed (autoFollowPattern , indexToFollow , localMetadata )) {
466
- updateAutoFollowMetadata (recordLeaderIndexAsFollowFunction (autoFollowPattenName , indexToFollow ), error -> {
467
- results .set (slot , new Tuple <>(indexToFollow , error ));
468
- if (leaderIndicesCountDown .countDown ()) {
469
- resultHandler .accept (new AutoFollowResult (autoFollowPattenName , results .asList ()));
470
- }
471
- });
472
- continue ;
461
+ updateAutoFollowMetadata (recordLeaderIndexAsFollowFunction (autoFollowPattenName , indexToFollow ),
462
+ error -> groupedListener .onResponse (new Tuple <>(indexToFollow , error )));
463
+ } else {
464
+ followLeaderIndex (autoFollowPattenName , remoteCluster , indexToFollow , autoFollowPattern , headers ,
465
+ error -> groupedListener .onResponse (new Tuple <>(indexToFollow , error )));
473
466
}
474
-
475
- followLeaderIndex (autoFollowPattenName , remoteCluster , indexToFollow , autoFollowPattern , headers , error -> {
476
- results .set (slot , new Tuple <>(indexToFollow , error ));
477
- if (leaderIndicesCountDown .countDown ()) {
478
- resultHandler .accept (new AutoFollowResult (autoFollowPattenName , results .asList ()));
479
- }
480
- });
481
467
}
482
468
}
483
469
}
0 commit comments