Skip to content

Commit

Permalink
Stop auto-followers when metadata is resetted (elastic#81290)
Browse files Browse the repository at this point in the history
(cherry picked from commit bdc1b73)
  • Loading branch information
idegtiarenko committed Dec 7, 2021
1 parent 6a25058 commit fa606aa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ synchronized void updateStats(List<AutoFollowResult> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, AutoFollower> initialAutoFollowers = autoFollowCoordinator.getAutoFollowers();

// when resetting the state
autoFollowCoordinator.updateAutoFollowers(createClusterStateWith(null));
Map<String, AutoFollower> 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));
}

Expand Down Expand Up @@ -2137,12 +2155,17 @@ private AutoFollowCoordinator createAutoFollowCoordinator() {
}

private ClusterState createClusterStateWith(Map<String, AutoFollowPattern> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, AutoFollowPattern> patterns;
private final Map<String, List<String>> followedLeaderIndexUUIDs;
private final Map<String, Map<String, String>> headers;
Expand Down

0 comments on commit fa606aa

Please sign in to comment.