diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index 2c578a6d2ebdb..634b4a2e7ccb0 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -259,8 +259,8 @@ void autoFollowIndices() { if (leaderClusterState != null) { assert e == null; final List followedIndices = autoFollowMetadata.getFollowedLeaderIndexUUIDs().get(clusterAlias); - final List leaderIndicesToFollow = - getLeaderIndicesToFollow(autoFollowPattern, leaderClusterState, followerClusterState, followedIndices); + final List leaderIndicesToFollow = getLeaderIndicesToFollow(clusterAlias, autoFollowPattern, + leaderClusterState, followerClusterState, followedIndices); if (leaderIndicesToFollow.isEmpty()) { finalise(slot, new AutoFollowResult(clusterAlias)); } else { @@ -331,17 +331,18 @@ private void finalise(int slot, AutoFollowResult result) { } } - static List getLeaderIndicesToFollow(AutoFollowPattern autoFollowPattern, + static List getLeaderIndicesToFollow(String clusterAlias, + AutoFollowPattern autoFollowPattern, ClusterState leaderClusterState, ClusterState followerClusterState, List followedIndexUUIDs) { List leaderIndicesToFollow = new ArrayList<>(); for (IndexMetaData leaderIndexMetaData : leaderClusterState.getMetaData()) { - // We should not automatically follow a leader index that is also a follow index and - // that both leader and follow index are in the same cluster otherwise this can - // result into an index creation explosion. + // If an auto follow pattern has been set up for the local cluster then + // we should not automatically follow a leader index that is also a follow index because + // this can result into an index creation explosion. if (leaderIndexMetaData.getCustomData(Ccr.CCR_CUSTOM_METADATA_KEY) != null && - leaderClusterState.getClusterName().equals(followerClusterState.getClusterName())) { + clusterAlias.equals("_local_")) { continue; } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java index dda192aa8b234..81bc9d923519a 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java @@ -311,7 +311,8 @@ public void testGetLeaderIndicesToFollow() { .metaData(imdBuilder) .build(); - List result = AutoFollower.getLeaderIndicesToFollow(autoFollowPattern, leaderState, followerState, Collections.emptyList()); + List result = AutoFollower.getLeaderIndicesToFollow("remote", autoFollowPattern, leaderState, followerState, + Collections.emptyList()); result.sort(Comparator.comparing(Index::getName)); assertThat(result.size(), equalTo(5)); assertThat(result.get(0).getName(), equalTo("metrics-0")); @@ -321,7 +322,7 @@ public void testGetLeaderIndicesToFollow() { assertThat(result.get(4).getName(), equalTo("metrics-4")); List followedIndexUUIDs = Collections.singletonList(leaderState.metaData().index("metrics-2").getIndexUUID()); - result = AutoFollower.getLeaderIndicesToFollow(autoFollowPattern, leaderState, followerState, followedIndexUUIDs); + result = AutoFollower.getLeaderIndicesToFollow("remote", autoFollowPattern, leaderState, followerState, followedIndexUUIDs); result.sort(Comparator.comparing(Index::getName)); assertThat(result.size(), equalTo(4)); assertThat(result.get(0).getName(), equalTo("metrics-0")); @@ -331,13 +332,6 @@ public void testGetLeaderIndicesToFollow() { } public void testGetLeaderIndicesToFollowDoNotSelectFollowIndicesInTheSameCluster() { - AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null, null); - ClusterState followerState = ClusterState.builder(new ClusterName("name")) - .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(Collections.singletonMap("remote", autoFollowPattern), Collections.emptyMap()))) - .build(); - MetaData.Builder imdBuilder = MetaData.builder(); imdBuilder.put(IndexMetaData.builder("metrics-0") .settings(settings(Version.CURRENT)) @@ -349,11 +343,17 @@ public void testGetLeaderIndicesToFollowDoNotSelectFollowIndicesInTheSameCluster .numberOfShards(1) .numberOfReplicas(0)); - ClusterState leaderState = ClusterState.builder(new ClusterName("name")) + AutoFollowPattern autoFollowPattern = + new AutoFollowPattern(Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null, null); + imdBuilder.putCustom(AutoFollowMetadata.TYPE, + new AutoFollowMetadata(Collections.singletonMap("remote", autoFollowPattern), Collections.emptyMap())); + + ClusterState clusterState = ClusterState.builder(new ClusterName("name")) .metaData(imdBuilder) .build(); - List result = AutoFollower.getLeaderIndicesToFollow(autoFollowPattern, leaderState, followerState, Collections.emptyList()); + List result = AutoFollower.getLeaderIndicesToFollow("_local_", autoFollowPattern, clusterState, + clusterState, Collections.emptyList()); result.sort(Comparator.comparing(Index::getName)); assertThat(result.size(), equalTo(1)); assertThat(result.get(0).getName(), equalTo("metrics-0"));