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 38f7603255d6a..d6e80909f8cf2 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 @@ -241,8 +241,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 671c920270a9f..1beefc63f708d 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 + var autoFollowCoordinator = createAutoFollowCoordinator(); + + // with some initial patterns + var pattern1 = createAutoFollowPattern("remote1", "logs-*"); + var pattern2 = createAutoFollowPattern("remote2", "logs-*"); + var pattern3 = createAutoFollowPattern("remote2", "metrics-*");// same remote + autoFollowCoordinator.updateAutoFollowers( + createClusterStateWith(Map.of("pattern1", pattern1, "pattern2", pattern2, "pattern3", pattern3)) + ); + var initialAutoFollowers = autoFollowCoordinator.getAutoFollowers(); + + // when resetting the state + autoFollowCoordinator.updateAutoFollowers(createClusterStateWith(null)); + var 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); + var autoFollowCoordinator = createAutoFollowCoordinator(); + autoFollowCoordinator.updateAutoFollowers(createClusterStateWith(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); + var autoFollowCoordinator = createAutoFollowCoordinator(); + autoFollowCoordinator.updateAutoFollowers(createClusterStateWith(null)); + + assertThat(autoFollowCoordinator.getAutoFollowers().keySet(), empty()); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); } @@ -2044,9 +2062,11 @@ private AutoFollowCoordinator createAutoFollowCoordinator() { } private ClusterState createClusterStateWith(Map patterns) { - return ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Map.of(), Map.of()))) - .build(); + var builder = ClusterState.builder(new ClusterName("remote")); + if (patterns != null) { + builder.metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Map.of(), 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 86ee465127a36..fb127b2547883 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 @@ -77,6 +77,8 @@ public static AutoFollowMetadata fromXContent(XContentParser parser) throws IOEx return PARSER.parse(parser, null); } + public static final AutoFollowMetadata EMPTY = new AutoFollowMetadata(Map.of(), Map.of(), Map.of()); + private final Map patterns; private final Map> followedLeaderIndexUUIDs; private final Map> headers;