Skip to content

Commit 3b52bfc

Browse files
committed
[CCR] AutoFollowCoordinator should tolerate that auto follow patterns may be removed (#35945)
AutoFollowCoordinator should take into account that after auto following an index and while updating that a leader index has been followed, that the auto follow pattern may have been removed via delete auto follow patterns api. Also fixed a bug that when a remote cluster connection has been removed, the auto follow coordinator does not die when it tries get a remote client for that cluster. Closes #35480
1 parent 53f9f2d commit 3b52bfc

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,22 @@ public void checkRemoteClusterLicenseAndFetchClusterState(
161161
final ClusterStateRequest request,
162162
final Consumer<Exception> onFailure,
163163
final Consumer<ClusterState> leaderClusterStateConsumer) {
164-
checkRemoteClusterLicenseAndFetchClusterState(
164+
try {
165+
Client remoteClient = systemClient(client.getRemoteClusterClient(clusterAlias));
166+
checkRemoteClusterLicenseAndFetchClusterState(
165167
client,
166168
clusterAlias,
167-
systemClient(client.getRemoteClusterClient(clusterAlias)),
169+
remoteClient,
168170
request,
169171
onFailure,
170172
leaderClusterStateConsumer,
171173
CcrLicenseChecker::clusterStateNonCompliantRemoteLicense,
172174
e -> clusterStateUnknownRemoteLicense(clusterAlias, e));
175+
} catch (Exception e) {
176+
// client.getRemoteClusterClient(...) can fail with a IllegalArgumentException if remote
177+
// connection is unknown
178+
onFailure.accept(e);
179+
}
173180
}
174181

175182
/**

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ static Function<ClusterState, ClusterState> recordLeaderIndexAsFollowFunction(St
403403
return currentState -> {
404404
AutoFollowMetadata currentAutoFollowMetadata = currentState.metaData().custom(AutoFollowMetadata.TYPE);
405405
Map<String, List<String>> newFollowedIndexUUIDS = new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs());
406+
if (newFollowedIndexUUIDS.containsKey(name) == false) {
407+
// A delete auto follow pattern request can have removed the auto follow pattern while we want to update
408+
// the auto follow metadata with the fact that an index was successfully auto followed. If this
409+
// happens, we can just skip this step.
410+
return currentState;
411+
}
412+
406413
newFollowedIndexUUIDS.compute(name, (key, existingUUIDs) -> {
407414
assert existingUUIDs != null;
408415
List<String> newUUIDs = new ArrayList<>(existingUUIDs);

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040
import java.util.function.Consumer;
4141
import java.util.function.Function;
4242

43+
import static org.elasticsearch.xpack.ccr.action.AutoFollowCoordinator.AutoFollower.recordLeaderIndexAsFollowFunction;
4344
import static org.hamcrest.Matchers.equalTo;
4445
import static org.hamcrest.Matchers.is;
46+
import static org.hamcrest.Matchers.notNullValue;
4547
import static org.hamcrest.Matchers.nullValue;
4648
import static org.hamcrest.Matchers.sameInstance;
4749
import static org.mockito.Matchers.anyString;
@@ -384,6 +386,33 @@ public void testGetLeaderIndicesToFollow_shardsNotStarted() {
384386
assertThat(result.get(1).getName(), equalTo("index2"));
385387
}
386388

389+
public void testRecordLeaderIndexAsFollowFunction() {
390+
AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(Collections.emptyMap(),
391+
Collections.singletonMap("pattern1", Collections.emptyList()), Collections.emptyMap());
392+
ClusterState clusterState = new ClusterState.Builder(new ClusterName("name"))
393+
.metaData(new MetaData.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata))
394+
.build();
395+
Function<ClusterState, ClusterState> function = recordLeaderIndexAsFollowFunction("pattern1", new Index("index1", "index1"));
396+
397+
ClusterState result = function.apply(clusterState);
398+
AutoFollowMetadata autoFollowMetadataResult = result.metaData().custom(AutoFollowMetadata.TYPE);
399+
assertThat(autoFollowMetadataResult.getFollowedLeaderIndexUUIDs().get("pattern1"), notNullValue());
400+
assertThat(autoFollowMetadataResult.getFollowedLeaderIndexUUIDs().get("pattern1").size(), equalTo(1));
401+
assertThat(autoFollowMetadataResult.getFollowedLeaderIndexUUIDs().get("pattern1").get(0), equalTo("index1"));
402+
}
403+
404+
public void testRecordLeaderIndexAsFollowFunctionNoEntry() {
405+
AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(),
406+
Collections.emptyMap());
407+
ClusterState clusterState = new ClusterState.Builder(new ClusterName("name"))
408+
.metaData(new MetaData.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata))
409+
.build();
410+
Function<ClusterState, ClusterState> function = recordLeaderIndexAsFollowFunction("pattern1", new Index("index1", "index1"));
411+
412+
ClusterState result = function.apply(clusterState);
413+
assertThat(result, sameInstance(clusterState));
414+
}
415+
387416
public void testGetFollowerIndexName() {
388417
AutoFollowPattern autoFollowPattern = new AutoFollowPattern("remote", Collections.singletonList("metrics-*"), null, null,
389418
null, null, null, null, null, null, null, null, null);

0 commit comments

Comments
 (0)