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 8f1a798271a91..d48250d08b05c 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 @@ -246,8 +246,10 @@ synchronized void updateStats(List results) { } void updateAutoFollowers(ClusterState followerClusterState) { - final AutoFollowMetadata autoFollowMetadata = followerClusterState.getMetadata().custom(AutoFollowMetadata.TYPE); - if (autoFollowMetadata == null) { + final AutoFollowMetadata autoFollowMetadata = followerClusterState.getMetadata() + .custom(AutoFollowMetadata.TYPE, AutoFollowMetadata.EMPTY); + + if (autoFollowMetadata.getPatterns().isEmpty() && this.autoFollowers.isEmpty()) { return; } 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 9cd9cb303234f..e8291f881f227 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 @@ -1147,25 +1147,43 @@ public void testUpdateAutoFollowers() { assertThat(removedAutoFollower2.removed, is(true)); } + public void testUpdateAutoFollowersRevertMetadata() { + // given coordinator with some initial patterns + AutoFollowCoordinator autoFollowCoordinator = createAutoFollowCoordinator(); + + // with some initial patterns + AutoFollowPattern pattern1 = createAutoFollowPattern("remote1", "logs-*"); + AutoFollowPattern pattern2 = createAutoFollowPattern("remote2", "logs-*"); + AutoFollowPattern pattern3 = createAutoFollowPattern("remote2", "metrics-*");// same remote + autoFollowCoordinator.updateAutoFollowers( + createClusterStateWith(org.elasticsearch.core.Map.of("pattern1", pattern1, "pattern2", pattern2, "pattern3", pattern3)) + ); + Map initialAutoFollowers = autoFollowCoordinator.getAutoFollowers(); + + // when resetting the state + autoFollowCoordinator.updateAutoFollowers(createClusterStateWith(null)); + Map newAutoFollowers = autoFollowCoordinator.getAutoFollowers(); + + // then auto-followers are removed + assertThat(newAutoFollowers.entrySet(), empty()); + // and auto-followers are stopped + assertThat(initialAutoFollowers.get("remote1").removed, equalTo(true)); + assertThat(initialAutoFollowers.get("remote2").removed, equalTo(true)); + } + public void testUpdateAutoFollowersNoPatterns() { AutoFollowCoordinator autoFollowCoordinator = createAutoFollowCoordinator(); - ClusterState clusterState = ClusterState.builder(new ClusterName("remote")) - .metadata( - Metadata.builder() - .putCustom( - AutoFollowMetadata.TYPE, - new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()) - ) - ) - .build(); - autoFollowCoordinator.updateAutoFollowers(clusterState); + autoFollowCoordinator.updateAutoFollowers(createClusterStateWith(org.elasticsearch.core.Map.of())); + + assertThat(autoFollowCoordinator.getAutoFollowers().keySet(), empty()); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); } public void testUpdateAutoFollowersNoAutoFollowMetadata() { AutoFollowCoordinator autoFollowCoordinator = createAutoFollowCoordinator(); - ClusterState clusterState = ClusterState.builder(new ClusterName("remote")).build(); - autoFollowCoordinator.updateAutoFollowers(clusterState); + autoFollowCoordinator.updateAutoFollowers(createClusterStateWith(null)); + + assertThat(autoFollowCoordinator.getAutoFollowers().keySet(), empty()); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); } @@ -2137,12 +2155,17 @@ private AutoFollowCoordinator createAutoFollowCoordinator() { } private ClusterState createClusterStateWith(Map patterns) { - return ClusterState.builder(new ClusterName("remote")) - .metadata( + ClusterState.Builder builder = ClusterState.builder(new ClusterName("remote")); + if (patterns != null) { + builder.metadata( Metadata.builder() - .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap())) - ) - .build(); + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, org.elasticsearch.core.Map.of(), org.elasticsearch.core.Map.of()) + ) + ); + } + return builder.build(); } private AutoFollowPattern createAutoFollowPattern(String remoteCluster, String pattern) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java index d7aea07c78343..876a054207a6d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java @@ -78,6 +78,12 @@ public static AutoFollowMetadata fromXContent(XContentParser parser) throws IOEx return PARSER.parse(parser, null); } + public static final AutoFollowMetadata EMPTY = new AutoFollowMetadata( + org.elasticsearch.core.Map.of(), + org.elasticsearch.core.Map.of(), + org.elasticsearch.core.Map.of() + ); + private final Map patterns; private final Map> followedLeaderIndexUUIDs; private final Map> headers;