Skip to content

Commit 9d0e544

Browse files
committed
[CCR] Fail with a descriptive error if leader index does not exist (#33797)
Closes #33737
1 parent 570f7c2 commit 9d0e544

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626

2727
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
28+
import static org.hamcrest.Matchers.containsString;
2829
import static org.hamcrest.Matchers.endsWith;
2930
import static org.hamcrest.Matchers.equalTo;
3031
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@@ -82,6 +83,18 @@ public void testFollowIndex() throws Exception {
8283
}
8384
}
8485

86+
public void testFollowNonExistingLeaderIndex() throws Exception {
87+
assumeFalse("Test should only run when both clusters are running", runningAgainstLeaderCluster);
88+
ResponseException e = expectThrows(ResponseException.class,
89+
() -> followIndex("leader_cluster:non-existing-index", "non-existing-index"));
90+
assertThat(e.getMessage(), containsString("no such index"));
91+
assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(404));
92+
93+
e = expectThrows(ResponseException.class, () -> createAndFollowIndex("leader_cluster:non-existing-index", "non-existing-index"));
94+
assertThat(e.getMessage(), containsString("no such index"));
95+
assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(404));
96+
}
97+
8598
public void testAutoFollowPatterns() throws Exception {
8699
assumeFalse("Test should only run when both clusters are running", runningAgainstLeaderCluster);
87100

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

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.cluster.metadata.IndexMetaData;
2727
import org.elasticsearch.common.util.concurrent.ThreadContext;
2828
import org.elasticsearch.common.CheckedConsumer;
29+
import org.elasticsearch.index.IndexNotFoundException;
2930
import org.elasticsearch.index.engine.CommitStats;
3031
import org.elasticsearch.index.engine.Engine;
3132
import org.elasticsearch.index.shard.ShardId;
@@ -110,6 +111,11 @@ public <T> void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUU
110111
onFailure,
111112
leaderClusterState -> {
112113
IndexMetaData leaderIndexMetaData = leaderClusterState.getMetaData().index(leaderIndex);
114+
if (leaderIndexMetaData == null) {
115+
onFailure.accept(new IndexNotFoundException(leaderIndex));
116+
return;
117+
}
118+
113119
final Client leaderClient = client.getRemoteClusterClient(clusterAlias);
114120
fetchLeaderHistoryUUIDs(leaderClient, leaderIndexMetaData, onFailure, historyUUIDs -> {
115121
consumer.accept(historyUUIDs, leaderIndexMetaData);

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

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.common.UUIDs;
2929
import org.elasticsearch.common.inject.Inject;
3030
import org.elasticsearch.common.settings.Settings;
31+
import org.elasticsearch.index.IndexNotFoundException;
3132
import org.elasticsearch.license.LicenseUtils;
3233
import org.elasticsearch.threadpool.ThreadPool;
3334
import org.elasticsearch.transport.RemoteClusterAware;
@@ -121,6 +122,11 @@ private void createFollowerIndexAndFollowLocalIndex(
121122
// following an index in local cluster, so use local cluster state to fetch leader index metadata
122123
final String leaderIndex = request.getFollowRequest().getLeaderIndex();
123124
final IndexMetaData leaderIndexMetadata = state.getMetaData().index(leaderIndex);
125+
if (leaderIndexMetadata == null) {
126+
listener.onFailure(new IndexNotFoundException(leaderIndex));
127+
return;
128+
}
129+
124130
Consumer<String[]> handler = historyUUIDs -> {
125131
createFollowerIndex(leaderIndexMetadata, historyUUIDs, request, listener);
126132
};

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

+9
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,21 @@ public void testFollowNonExistentIndex() throws Exception {
429429
// Leader index does not exist.
430430
FollowIndexAction.Request followRequest1 = createFollowRequest("non-existent-leader", "test-follower");
431431
expectThrows(IndexNotFoundException.class, () -> client().execute(FollowIndexAction.INSTANCE, followRequest1).actionGet());
432+
expectThrows(IndexNotFoundException.class,
433+
() -> client().execute(CreateAndFollowIndexAction.INSTANCE, new CreateAndFollowIndexAction.Request(followRequest1))
434+
.actionGet());
432435
// Follower index does not exist.
433436
FollowIndexAction.Request followRequest2 = createFollowRequest("non-test-leader", "non-existent-follower");
434437
expectThrows(IndexNotFoundException.class, () -> client().execute(FollowIndexAction.INSTANCE, followRequest2).actionGet());
438+
expectThrows(IndexNotFoundException.class,
439+
() -> client().execute(CreateAndFollowIndexAction.INSTANCE, new CreateAndFollowIndexAction.Request(followRequest2))
440+
.actionGet());
435441
// Both indices do not exist.
436442
FollowIndexAction.Request followRequest3 = createFollowRequest("non-existent-leader", "non-existent-follower");
437443
expectThrows(IndexNotFoundException.class, () -> client().execute(FollowIndexAction.INSTANCE, followRequest3).actionGet());
444+
expectThrows(IndexNotFoundException.class,
445+
() -> client().execute(CreateAndFollowIndexAction.INSTANCE, new CreateAndFollowIndexAction.Request(followRequest3))
446+
.actionGet());
438447
}
439448

440449
public void testFollowIndex_lowMaxTranslogBytes() throws Exception {

0 commit comments

Comments
 (0)