From ca3f8259151698a329c6b02940ac5bda4fa00435 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 6 Nov 2018 14:53:29 +0100 Subject: [PATCH 1/3] [CCR] Fail with a better error if leader index is red as part of fetching history uuids from leader index. --- .../xpack/ccr/CcrLicenseChecker.java | 5 +++ .../xpack/ccr/IndexFollowingIT.java | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java index d2c86e69fbd5d..02f0c6a5815e7 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java @@ -244,6 +244,11 @@ public void fetchLeaderHistoryUUIDs( String leaderIndex = leaderIndexMetaData.getIndex().getName(); CheckedConsumer indicesStatsHandler = indicesStatsResponse -> { IndexStats indexStats = indicesStatsResponse.getIndices().get(leaderIndex); + if (indexStats == null) { + onFailure.accept(new IllegalArgumentException("no index shards available, is the leader index red?")); + return; + } + String[] historyUUIDs = new String[leaderIndexMetaData.getNumberOfShards()]; for (IndexShardStats indexShardStats : indexStats) { for (ShardStats shardStats : indexShardStats) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java index 8f42787010c16..71be66804495a 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java @@ -9,8 +9,11 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest; import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; +import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.admin.indices.stats.ShardStats; @@ -19,6 +22,7 @@ import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -552,6 +556,34 @@ public void testUnknownClusterAlias() throws Exception { assertThat(e.getMessage(), equalTo("unknown cluster alias [another_cluster]")); } + public void testLeaderIndexRed() throws Exception { + try { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.transientSettings(Settings.builder().put("cluster.routing.allocation.enable", "none")); + assertAcked(leaderClient().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + assertAcked(leaderClient().admin().indices().prepareCreate("index1") + .setWaitForActiveShards(ActiveShardCount.NONE) + .setSettings(Settings.builder() + .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) + .build())); + + final PutFollowAction.Request followRequest = putFollow("index1", "index2"); + Exception e = expectThrows(IllegalArgumentException.class, + () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest).actionGet()); + assertThat(e.getMessage(), equalTo("no index shards available, is the leader index red?")); + + IndicesExistsResponse existsResponse = followerClient().admin().indices().exists(new IndicesExistsRequest("index2")).actionGet(); + assertThat(existsResponse.isExists(), is(false)); + } finally { + // Always unset allocation enable setting to avoid other assertions from failing too when this test fails: + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.transientSettings(Settings.builder().put("cluster.routing.allocation.enable", (String) null)); + assertAcked(leaderClient().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } + } + private CheckedRunnable assertTask(final int numberOfPrimaryShards, final Map numDocsPerShard) { return () -> { final ClusterState clusterState = followerClient().admin().cluster().prepareState().get().getState(); From a0e34cf78158c4b6de7bef7205dd92a67554431d Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 6 Nov 2018 15:43:07 +0100 Subject: [PATCH 2/3] adjusted error message --- .../java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java | 2 +- .../test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java index 02f0c6a5815e7..5753f17c351df 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java @@ -245,7 +245,7 @@ public void fetchLeaderHistoryUUIDs( CheckedConsumer indicesStatsHandler = indicesStatsResponse -> { IndexStats indexStats = indicesStatsResponse.getIndices().get(leaderIndex); if (indexStats == null) { - onFailure.accept(new IllegalArgumentException("no index shards available, is the leader index red?")); + onFailure.accept(new IllegalArgumentException("no index stats available for the leader index")); return; } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java index 71be66804495a..2a1a17ae7557c 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java @@ -572,7 +572,7 @@ public void testLeaderIndexRed() throws Exception { final PutFollowAction.Request followRequest = putFollow("index1", "index2"); Exception e = expectThrows(IllegalArgumentException.class, () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest).actionGet()); - assertThat(e.getMessage(), equalTo("no index shards available, is the leader index red?")); + assertThat(e.getMessage(), equalTo("no index stats available for the leader index")); IndicesExistsResponse existsResponse = followerClient().admin().indices().exists(new IndicesExistsRequest("index2")).actionGet(); assertThat(existsResponse.isExists(), is(false)); From af41ba59e883a3fdfd4fecc3af852363bde9695b Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 7 Nov 2018 07:40:18 +0100 Subject: [PATCH 3/3] fixed checkstyle violation --- .../java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java index 2a1a17ae7557c..45360041c4d27 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java @@ -574,7 +574,8 @@ public void testLeaderIndexRed() throws Exception { () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest).actionGet()); assertThat(e.getMessage(), equalTo("no index stats available for the leader index")); - IndicesExistsResponse existsResponse = followerClient().admin().indices().exists(new IndicesExistsRequest("index2")).actionGet(); + IndicesExistsResponse existsResponse = followerClient().admin().indices().exists(new IndicesExistsRequest("index2")) + .actionGet(); assertThat(existsResponse.isExists(), is(false)); } finally { // Always unset allocation enable setting to avoid other assertions from failing too when this test fails: