Skip to content

Commit fec2213

Browse files
committed
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 d3bc9b7 commit fec2213

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
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.threadpool.ThreadPool;
2325
import org.elasticsearch.transport.TransportService;
26+
import org.elasticsearch.xpack.ccr.Ccr;
2427
import org.elasticsearch.xpack.core.ccr.action.PauseFollowAction;
2528

2629
import java.io.IOException;
@@ -58,9 +61,18 @@ protected AcknowledgedResponse read(StreamInput in) throws IOException {
5861
protected void masterOperation(PauseFollowAction.Request request,
5962
ClusterState state,
6063
ActionListener<AcknowledgedResponse> listener) throws Exception {
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+
}
6173
PersistentTasksCustomMetaData persistentTasksMetaData = state.metaData().custom(PersistentTasksCustomMetaData.TYPE);
6274
if (persistentTasksMetaData == null) {
63-
listener.onFailure(new IllegalArgumentException("no shard follow tasks for [" + request.getFollowIndex() + "]"));
75+
listener.onFailure(new IllegalArgumentException("no shard follow tasks found"));
6476
return;
6577
}
6678

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ public void testFollowIndexWithNestedField() throws Exception {
577577

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

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

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

0 commit comments

Comments
 (0)