Skip to content

Commit ae0d0ca

Browse files
authored
Improve error message when pausing index (#48915)
Throw an appropriate error message when the follower index is not found or is a regular index.
1 parent 547f300 commit ae0d0ca

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
import org.elasticsearch.cluster.ClusterState;
1414
import org.elasticsearch.cluster.block.ClusterBlockException;
1515
import org.elasticsearch.cluster.block.ClusterBlockLevel;
16+
import org.elasticsearch.cluster.metadata.IndexMetaData;
1617
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
1718
import org.elasticsearch.cluster.service.ClusterService;
1819
import org.elasticsearch.common.inject.Inject;
1920
import org.elasticsearch.common.io.stream.StreamInput;
21+
import org.elasticsearch.index.IndexNotFoundException;
2022
import org.elasticsearch.persistent.PersistentTasksCustomMetaData;
2123
import org.elasticsearch.persistent.PersistentTasksService;
2224
import org.elasticsearch.tasks.Task;
2325
import org.elasticsearch.threadpool.ThreadPool;
2426
import org.elasticsearch.transport.TransportService;
27+
import org.elasticsearch.xpack.ccr.Ccr;
2528
import org.elasticsearch.xpack.core.ccr.action.PauseFollowAction;
2629

2730
import java.io.IOException;
@@ -57,11 +60,19 @@ protected AcknowledgedResponse read(StreamInput in) throws IOException {
5760

5861
@Override
5962
protected void masterOperation(Task task, PauseFollowAction.Request request,
60-
ClusterState state,
61-
ActionListener<AcknowledgedResponse> listener) throws Exception {
63+
ClusterState state, ActionListener<AcknowledgedResponse> listener) {
64+
final IndexMetaData followerIMD = state.metaData().index(request.getFollowIndex());
65+
if (followerIMD == null) {
66+
listener.onFailure(new IndexNotFoundException(request.getFollowIndex()));
67+
return;
68+
}
69+
if (followerIMD.getCustomData(Ccr.CCR_CUSTOM_METADATA_KEY) == null) {
70+
listener.onFailure(new IllegalArgumentException("index [" + request.getFollowIndex() + "] is not a follower index"));
71+
return;
72+
}
6273
PersistentTasksCustomMetaData persistentTasksMetaData = state.metaData().custom(PersistentTasksCustomMetaData.TYPE);
6374
if (persistentTasksMetaData == null) {
64-
listener.onFailure(new IllegalArgumentException("no shard follow tasks for [" + request.getFollowIndex() + "]"));
75+
listener.onFailure(new IllegalArgumentException("no shard follow tasks found"));
6576
return;
6677
}
6778

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public void testFollowIndexWithNestedField() throws Exception {
576576

577577
public void testUnfollowNonExistingIndex() {
578578
PauseFollowAction.Request unfollowRequest = new PauseFollowAction.Request("non-existing-index");
579-
expectThrows(IllegalArgumentException.class,
579+
expectThrows(IndexNotFoundException.class,
580580
() -> followerClient().execute(PauseFollowAction.INSTANCE, unfollowRequest).actionGet());
581581
}
582582

@@ -817,6 +817,27 @@ public void testDeleteFollowerIndex() throws Exception {
817817
ensureNoCcrTasks();
818818
}
819819

820+
public void testPauseIndex() throws Exception {
821+
assertAcked(leaderClient().admin().indices().prepareCreate("leader")
822+
.setSettings(Settings.builder()
823+
.put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true)
824+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
825+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
826+
.build()));
827+
followerClient().execute(PutFollowAction.INSTANCE, putFollow("leader", "follower")).get();
828+
assertAcked(followerClient().admin().indices().prepareCreate("regular-index"));
829+
assertAcked(followerClient().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("follower")).actionGet());
830+
assertThat(expectThrows(IllegalArgumentException.class, () -> followerClient().execute(
831+
PauseFollowAction.INSTANCE, new PauseFollowAction.Request("follower")).actionGet()).getMessage(),
832+
equalTo("no shard follow tasks for [follower]"));
833+
assertThat(expectThrows(IllegalArgumentException.class, () -> followerClient().execute(
834+
PauseFollowAction.INSTANCE, new PauseFollowAction.Request("regular-index")).actionGet()).getMessage(),
835+
equalTo("index [regular-index] is not a follower index"));
836+
assertThat(expectThrows(IndexNotFoundException.class, () -> followerClient().execute(
837+
PauseFollowAction.INSTANCE, new PauseFollowAction.Request("xyz")).actionGet()).getMessage(),
838+
equalTo("no such index [xyz]"));
839+
}
840+
820841
public void testUnfollowIndex() throws Exception {
821842
String leaderIndexSettings = getIndexSettings(1, 0, singletonMap(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "true"));
822843
assertAcked(leaderClient().admin().indices().prepareCreate("index1").setSource(leaderIndexSettings, XContentType.JSON).get());

0 commit comments

Comments
 (0)