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
  • Loading branch information
idegtiarenko committed Dec 7, 2021
1 parent 87f5a7c commit d324b1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,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
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));
}

Expand Down Expand Up @@ -2044,9 +2062,11 @@ private AutoFollowCoordinator createAutoFollowCoordinator() {
}

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

0 comments on commit d324b1e

Please sign in to comment.